Skip to content

Commit

Permalink
fix: fix mapping ipv6-based port mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveb-p authored and AlexZeitler committed May 11, 2021
1 parent e7013df commit 48c9f08
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 43 deletions.
32 changes: 15 additions & 17 deletions src/port-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,22 @@ const mapPorts = (
const result = !ports
? []
: (() => {
return ports.split(',').map((untypedPort) => {
const exposedFragments = untypedPort.trim().split('->')
return ports.split(',').map((untypedPort) => {
const exposedFragments = untypedPort.trim().split('->')

console.log(exposedFragments)

const [port, protocol] =
exposedFragments.length === 1
? exposedFragments[0].split('/')
: exposedFragments[1].split('/')
const [address, mappedPort] =
exposedFragments.length === 2 ? exposedFragments[0].split(':') : []
return {
exposed: { port: Number(port), protocol },
...(address &&
mappedPort && { mapped: { port: Number(mappedPort), address } })
}
})
})()
const [port, protocol] =
exposedFragments.length === 1
? exposedFragments[0].split('/')
: exposedFragments[1].split('/')
const [address, mappedPort] =
exposedFragments.length === 2 ? exposedFragments[0].split(':') : []
return {
exposed: { port: Number(port), protocol },
...(address &&
mappedPort && { mapped: { port: Number(mappedPort), address } })
}
})
})()
return result
}

Expand Down
34 changes: 15 additions & 19 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Docker from 'dockerode'
import * as compose from '../src/index'
import * as path from 'path'
import { readFile } from 'fs'
import { mapPorts, mapPsOutput } from '../src/index'
import { mapPsOutput } from '../src/index'
const docker = new Docker()

// Docker commands, especially builds, can take some time. This makes sure that they can take the time they need.
Expand Down Expand Up @@ -94,25 +94,21 @@ test('ensure exit code is returned correctly', async (): Promise<void> => {
})

describe('starts containers properly with --build and --timeout options', (): void => {
beforeEach(
async (): Promise<void> => {
await compose.down({
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose-build.yml'
})
}
)
beforeEach(async (): Promise<void> => {
await compose.down({
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose-build.yml'
})
})

afterEach(
async (): Promise<void> => {
await compose.down({
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose-build.yml'
})
}
)
afterEach(async (): Promise<void> => {
await compose.down({
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose-build.yml'
})
})

test('ensure container gets started with --build option', async (): Promise<void> => {
await compose.upAll({
Expand Down
18 changes: 11 additions & 7 deletions test/port-mapper.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import mapPorts from "../src/port-mapper";
import mapPorts from '../src/port-mapper'

test('map ports for empty string', () => {
expect(mapPorts('')).toEqual([])
Expand Down Expand Up @@ -33,22 +33,26 @@ test('map multiple tcp ports exposed on ivp4 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' },
mapped: { address: '0.0.0.0', port: 443 },
mapped: { address: '0.0.0.0', port: 443 }
},
{
exposed: { port: 443, protocol: 'tcp' },
mapped: { address: ':::', port: 443 },
mapped: { address: ':::', port: 443 }
},
{
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' },
mapped: { address: ':::', port: 80 },
},
mapped: { address: ':::', port: 80 }
}
])
})

0 comments on commit 48c9f08

Please sign in to comment.