fix: fix mapping ipv6-based port mappings

This commit is contained in:
Paweł Niedzielski 2021-05-10 22:25:35 +02:00 committed by Alexander Zeitler
parent e7013dfab1
commit 48c9f0841a
3 changed files with 41 additions and 43 deletions

View file

@ -7,24 +7,22 @@ const mapPorts = (
const result = !ports const result = !ports
? [] ? []
: (() => { : (() => {
return ports.split(',').map((untypedPort) => { return ports.split(',').map((untypedPort) => {
const exposedFragments = untypedPort.trim().split('->') const exposedFragments = untypedPort.trim().split('->')
console.log(exposedFragments) const [port, protocol] =
exposedFragments.length === 1
const [port, protocol] = ? exposedFragments[0].split('/')
exposedFragments.length === 1 : exposedFragments[1].split('/')
? exposedFragments[0].split('/') const [address, mappedPort] =
: exposedFragments[1].split('/') exposedFragments.length === 2 ? exposedFragments[0].split(':') : []
const [address, mappedPort] = return {
exposedFragments.length === 2 ? exposedFragments[0].split(':') : [] exposed: { port: Number(port), protocol },
return { ...(address &&
exposed: { port: Number(port), protocol }, mappedPort && { mapped: { port: Number(mappedPort), address } })
...(address && }
mappedPort && { mapped: { port: Number(mappedPort), address } }) })
} })()
})
})()
return result return result
} }

View file

@ -2,7 +2,7 @@ import Docker from 'dockerode'
import * as compose from '../src/index' import * as compose from '../src/index'
import * as path from 'path' import * as path from 'path'
import { readFile } from 'fs' import { readFile } from 'fs'
import { mapPorts, mapPsOutput } from '../src/index' import { mapPsOutput } from '../src/index'
const docker = new Docker() const docker = new Docker()
// Docker commands, especially builds, can take some time. This makes sure that they can take the time they need. // Docker commands, especially builds, can take some time. This makes sure that they can take the time they need.
@ -94,25 +94,21 @@ test('ensure exit code is returned correctly', async (): Promise<void> => {
}) })
describe('starts containers properly with --build and --timeout options', (): void => { describe('starts containers properly with --build and --timeout options', (): void => {
beforeEach( beforeEach(async (): Promise<void> => {
async (): Promise<void> => { await compose.down({
await compose.down({ cwd: path.join(__dirname),
cwd: path.join(__dirname), log: logOutput,
log: logOutput, config: 'docker-compose-build.yml'
config: 'docker-compose-build.yml' })
}) })
}
)
afterEach( afterEach(async (): Promise<void> => {
async (): Promise<void> => { await compose.down({
await compose.down({ cwd: path.join(__dirname),
cwd: path.join(__dirname), log: logOutput,
log: logOutput, config: 'docker-compose-build.yml'
config: 'docker-compose-build.yml' })
}) })
}
)
test('ensure container gets started with --build option', async (): Promise<void> => { test('ensure container gets started with --build option', async (): Promise<void> => {
await compose.upAll({ await compose.upAll({

View file

@ -1,4 +1,4 @@
import mapPorts from "../src/port-mapper"; import mapPorts from '../src/port-mapper'
test('map ports for empty string', () => { test('map ports for empty string', () => {
expect(mapPorts('')).toEqual([]) expect(mapPorts('')).toEqual([])
@ -33,22 +33,26 @@ test('map multiple tcp ports exposed on ivp4 interfaces', () => {
}) })
test('map multiple tcp ports exposed on ipv4 and ipv6 interfaces', () => { test('map multiple tcp ports exposed on ipv4 and ipv6 interfaces', () => {
expect(mapPorts('0.0.0.0:443->443/tcp,:::443->443/tcp, 0.0.0.0:80->80/tcp,:::80->80/tcp')).toEqual([ expect(
mapPorts(
'0.0.0.0:443->443/tcp,:::443->443/tcp, 0.0.0.0:80->80/tcp,:::80->80/tcp'
)
).toEqual([
{ {
exposed: { port: 443, protocol: 'tcp' }, exposed: { port: 443, protocol: 'tcp' },
mapped: { address: '0.0.0.0', port: 443 }, mapped: { address: '0.0.0.0', port: 443 }
}, },
{ {
exposed: { port: 443, protocol: 'tcp' }, exposed: { port: 443, protocol: 'tcp' },
mapped: { address: ':::', port: 443 }, mapped: { address: ':::', port: 443 }
}, },
{ {
exposed: { port: 80, protocol: 'tcp' }, exposed: { port: 80, protocol: 'tcp' },
mapped: { address: '0.0.0.0', port: 80 }, mapped: { address: '0.0.0.0', port: 80 }
}, },
{ {
exposed: { port: 80, protocol: 'tcp' }, exposed: { port: 80, protocol: 'tcp' },
mapped: { address: ':::', port: 80 }, mapped: { address: ':::', port: 80 }
}, }
]) ])
}) })