mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-01-31 08:25:36 +00:00
refactor: Generate and normalise PortMaps from compose ports in-class
Change-type: patch Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
parent
e82749e63a
commit
dc34025545
@ -30,7 +30,7 @@ interface PortRange {
|
||||
export class PortMap {
|
||||
private ports: PortRange;
|
||||
|
||||
public constructor(portStrOrObj: string | PortRange) {
|
||||
private constructor(portStrOrObj: string | PortRange) {
|
||||
if (_.isString(portStrOrObj)) {
|
||||
this.parsePortString(portStrOrObj);
|
||||
} else {
|
||||
@ -139,10 +139,11 @@ export class PortMap {
|
||||
}, []);
|
||||
}
|
||||
|
||||
public static normaliseComposePorts(portMaps: PortMap[]): PortMap[] {
|
||||
return _.sortBy(portMaps, p => {
|
||||
return p.ports.internalStart;
|
||||
});
|
||||
public static fromComposePorts(ports: string[]): PortMap[] {
|
||||
return _(ports)
|
||||
.map(p => new PortMap(p))
|
||||
.sortBy(p => p.ports.internalStart)
|
||||
.value();
|
||||
}
|
||||
|
||||
private parsePortString(portStr: string): void {
|
||||
|
@ -253,9 +253,7 @@ export class Service {
|
||||
|
||||
let portMaps: PortMap[] = [];
|
||||
if (config.ports != null) {
|
||||
portMaps = PortMap.normaliseComposePorts(
|
||||
_.map(config.ports, p => new PortMap(p)),
|
||||
);
|
||||
portMaps = PortMap.fromComposePorts(config.ports);
|
||||
}
|
||||
delete config.ports;
|
||||
|
||||
|
@ -248,8 +248,7 @@ describe 'Ports', ->
|
||||
portBindings = require('./data/ports/not-ascending/port-bindings.json')
|
||||
compose = require('./data/ports/not-ascending/compose.json')
|
||||
portMapsCurrent = PortMap.fromDockerOpts(portBindings)
|
||||
portMapsTarget = compose.ports.map((p) -> new PortMap(p))
|
||||
portMapsTarget = PortMap.normaliseComposePorts(portMapsTarget)
|
||||
portMapsTarget = PortMap.fromComposePorts(compose.ports)
|
||||
|
||||
expect(portMapsTarget).to.deep.equal(portMapsCurrent)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user