mirror of
https://github.com/actions/runner-container-hooks.git
synced 2025-12-14 00:26:44 +00:00
fixing issue related to setting hostPort and containerPort when format is port/proto (#38)
* fixing issue related to setting hostPort and containerPort when format is port/proto * added one more test case and refactored containerPorts to be without regexp * added throw on ports outside of (0,65536) range with test * repaired error message and added tests to multi splits. refactored port checking
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import * as fs from 'fs'
|
||||
import { POD_VOLUME_NAME } from '../src/k8s'
|
||||
import { containerPorts, POD_VOLUME_NAME } from '../src/k8s'
|
||||
import { containerVolumes, writeEntryPointScript } from '../src/k8s/utils'
|
||||
import { TestHelper } from './test-setup'
|
||||
|
||||
@@ -152,5 +152,73 @@ describe('k8s utils', () => {
|
||||
volumes = containerVolumes([], false, false)
|
||||
expect(volumes.every(e => e.name === POD_VOLUME_NAME)).toBeTruthy()
|
||||
})
|
||||
|
||||
it('should parse container ports', () => {
|
||||
const tt = [
|
||||
{
|
||||
spec: '8080:80',
|
||||
want: {
|
||||
containerPort: 80,
|
||||
hostPort: 8080,
|
||||
protocol: 'TCP'
|
||||
}
|
||||
},
|
||||
{
|
||||
spec: '8080:80/udp',
|
||||
want: {
|
||||
containerPort: 80,
|
||||
hostPort: 8080,
|
||||
protocol: 'UDP'
|
||||
}
|
||||
},
|
||||
{
|
||||
spec: '8080/udp',
|
||||
want: {
|
||||
containerPort: 8080,
|
||||
hostPort: undefined,
|
||||
protocol: 'UDP'
|
||||
}
|
||||
},
|
||||
{
|
||||
spec: '8080',
|
||||
want: {
|
||||
containerPort: 8080,
|
||||
hostPort: undefined,
|
||||
protocol: 'TCP'
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
for (const tc of tt) {
|
||||
const got = containerPorts({ portMappings: [tc.spec] })
|
||||
for (const [key, value] of Object.entries(tc.want)) {
|
||||
expect(got[0][key]).toBe(value)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
it('should throw when ports are out of range (0, 65536)', () => {
|
||||
expect(() => containerPorts({ portMappings: ['65536'] })).toThrow()
|
||||
expect(() => containerPorts({ portMappings: ['0'] })).toThrow()
|
||||
expect(() => containerPorts({ portMappings: ['65536/udp'] })).toThrow()
|
||||
expect(() => containerPorts({ portMappings: ['0/udp'] })).toThrow()
|
||||
expect(() => containerPorts({ portMappings: ['1:65536'] })).toThrow()
|
||||
expect(() => containerPorts({ portMappings: ['65536:1'] })).toThrow()
|
||||
expect(() => containerPorts({ portMappings: ['1:65536/tcp'] })).toThrow()
|
||||
expect(() => containerPorts({ portMappings: ['65536:1/tcp'] })).toThrow()
|
||||
expect(() => containerPorts({ portMappings: ['1:'] })).toThrow()
|
||||
expect(() => containerPorts({ portMappings: [':1'] })).toThrow()
|
||||
expect(() => containerPorts({ portMappings: ['1:/tcp'] })).toThrow()
|
||||
expect(() => containerPorts({ portMappings: [':1/tcp'] })).toThrow()
|
||||
})
|
||||
|
||||
it('should throw on multi ":" splits', () => {
|
||||
expect(() => containerPorts({ portMappings: ['1:1:1'] })).toThrow()
|
||||
})
|
||||
|
||||
it('should throw on multi "/" splits', () => {
|
||||
expect(() => containerPorts({ portMappings: ['1:1/tcp/udp'] })).toThrow()
|
||||
expect(() => containerPorts({ portMappings: ['1/tcp/udp'] })).toThrow()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user