Skip to content
This repository has been archived by the owner on Jul 19, 2021. It is now read-only.

Commit

Permalink
more tests + testability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jeroenpeeters committed Mar 31, 2017
1 parent 6d39c31 commit 8d4482b
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 54 deletions.
75 changes: 38 additions & 37 deletions src/coffee/compose.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -64,47 +64,48 @@ module.exports = (config) ->
null
service.volumes = service.volumes.filter((s) -> s) if service.volumes

augmentCompose: (instance, options, doc) ->
addNetworkContainer = (serviceName, service) ->
if service.labels['bigboat.service.type'] in ['service', 'oneoff']
labels = _.extend {}, service.labels,
'bigboat.service.type': 'net'
subDomain = "#{instance}.#{config.domain}.#{config.tld}"
netcontainer =
image: 'ictu/pipes:1'
environment: eth0_pipework_cmd: networkValue
hostname: "#{serviceName}.#{subDomain}"
dns_search: subDomain
network_mode: 'none'
cap_add: ["NET_ADMIN"]
labels: labels
stop_signal: 'SIGKILL'
if config.net_container?.healthcheck
netcontainer.healthcheck = config.net_container.healthcheck

if service.container_name
netcontainer['container_name'] = "#{service.container_name}-net"

doc.services["bb-net-#{serviceName}"] = netcontainer
# remove the hostname if set in the service, the hostname is set from
# the network container
delete service.hostname
delete service.net
service.network_mode = "service:bb-net-#{serviceName}"

depends_on = composeLib.transformDependsOnToObject(service.depends_on) or {}
depends_on["bb-net-#{serviceName}"] = if netcontainer.healthcheck
condition: 'service_healthy'
else
condition: 'service_started'
service.depends_on = depends_on

else service.network_mode = "service:bb-net-#{service.labels['bigboat.service.name']}"
_addNetworkContainer: addNetworkContainer = (serviceName, service, instance, doc) ->
if service.labels['bigboat.service.type'] in ['service', 'oneoff']
labels = _.extend {}, service.labels,
'bigboat.service.type': 'net'
subDomain = "#{instance}.#{config.domain}.#{config.tld}"
netcontainer =
image: 'ictu/pipes:1'
environment: eth0_pipework_cmd: networkValue
hostname: "#{serviceName}.#{subDomain}"
dns_search: subDomain
network_mode: 'none'
cap_add: ["NET_ADMIN"]
labels: labels
stop_signal: 'SIGKILL'
if config.net_container?.healthcheck
netcontainer.healthcheck = config.net_container.healthcheck

if service.container_name
netcontainer['container_name'] = "#{service.container_name}-net"

doc.services["bb-net-#{serviceName}"] = netcontainer
# remove the hostname if set in the service, the hostname is set from
# the network container
delete service.hostname
delete service.net
service.network_mode = "service:bb-net-#{serviceName}"

depends_on = composeLib.transformDependsOnToObject(service.depends_on) or {}
depends_on["bb-net-#{serviceName}"] = if netcontainer.healthcheck
condition: 'service_healthy'
else
condition: 'service_started'
service.depends_on = depends_on

else service.network_mode = "service:bb-net-#{service.labels['bigboat.service.name']}"

augmentCompose: (instance, options, doc) ->
console.log 'wiee!'
for serviceName, service of doc.services
migrateLinksToDependsOn serviceName, service
addExtraLabels serviceName, service
addNetworkContainer serviceName, service
addNetworkContainer serviceName, service, instance, doc
addVolumeMapping serviceName, service, options
addDockerMapping serviceName, service
restrictCompose serviceName, service
Expand Down
94 changes: 77 additions & 17 deletions tests/coffee/compose.test.coffee
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
assert = require 'assert'
td = require 'testdouble'
compose = require '../../src/coffee/compose.coffee'

describe 'Compose', ->
describe 'augmentCompose', ->
it 'should set the compose version to 2.1', ->
doc = version: '1.0'
compose({}).augmentCompose '', {}, doc
assert.equal doc.version, '2.1'
it 'should delete the volumes section from the compose file', ->
doc = volumes: {}
assert.equal doc.volumes?, true
compose({}).augmentCompose '', {}, doc
assert.equal doc.volumes?, false
it 'should delete the networks section from the compose file', ->
doc = networks: {}
assert.equal doc.networks?, true
compose({}).augmentCompose '', {}, doc
assert.equal doc.networks?, false

describe '_restrictCompose', ->
it 'should drop certain service capabilities', ->
service =
Expand All @@ -20,22 +35,6 @@ describe 'Compose', ->
compose({})._restrictCompose '', service
assert.deepEqual service, this_is_not_dropped: 1

describe 'augmentCompose', ->
it 'should set the compose version to 2.1', ->
doc = version: '1.0'
compose({}).augmentCompose '', {}, doc
assert.equal doc.version, '2.1'
it 'should delete the volumes section from the compose file', ->
doc = volumes: {}
assert.equal doc.volumes?, true
compose({}).augmentCompose '', {}, doc
assert.equal doc.volumes?, false
it 'should delete the networks section from the compose file', ->
doc = networks: {}
assert.equal doc.networks?, true
compose({}).augmentCompose '', {}, doc
assert.equal doc.networks?, false

describe '_migrateLinksToDependsOn', ->
it 'should leaf the document untouched when there are no links', ->
doc = services: www: image: 'someimage'
Expand Down Expand Up @@ -132,3 +131,64 @@ describe 'Compose', ->
service = volumes: ['../../my-malicious-volume/:/internal']
c._addVolumeMapping '', service, storageBucket: 'bucket1'
assert.deepEqual service, volumes: []

describe '_addNetworkContainer', ->
invokeTestSubject = (service, cfgNetContainer) ->
doc = services: {}
config = domain: 'google', tld: 'com', host_if: 'eth12', vlan: 1234, net_container: cfgNetContainer
compose(config)._addNetworkContainer 'service1', service, 'instance2', doc
doc
containerTest = (serviceType) ->
service =
labels:
'bigboat.service.type': serviceType
doc = invokeTestSubject service
assert.equal service.network_mode, 'service:bb-net-service1'
assert.deepEqual service.depends_on, 'bb-net-service1': condition: 'service_started'
assert.deepEqual doc.services['bb-net-service1'],
image: 'ictu/pipes:1'
environment: eth0_pipework_cmd: "eth12 -i eth0 @CONTAINER_NAME@ dhclient @1234"
hostname: 'service1.instance2.google.com'
dns_search: 'instance2.google.com'
network_mode: 'none'
cap_add: ['NET_ADMIN']
labels: 'bigboat.service.type': 'net'
stop_signal: 'SIGKILL'
it 'should should add a network container for compose service of type \'service\'', ->
containerTest 'service'
it 'should should add a network container for compose service of type \'oneoff\'', ->
containerTest 'oneoff'
it 'should inherit all labels from the service container, except the bigboat.service.type label', ->
service =
labels:
'bigboat.service.type': 'service'
some_other_label: 'value'
doc = invokeTestSubject service
assert.deepEqual doc.services['bb-net-service1'].labels,
'bigboat.service.type': 'net'
some_other_label: 'value'

it 'should set the netcontainer healthcheck when configured', ->
service =
labels:
'bigboat.service.type': 'service'
doc = invokeTestSubject service, healthcheck: 'some-check'
assert.equal doc.services['bb-net-service1'].healthcheck, 'some-check'
assert.deepEqual service.depends_on, 'bb-net-service1': condition: 'service_healthy'

it 'should use the container_name from the service, if any, to populate the netcontainer name', ->
service =
labels:
'bigboat.service.type': 'oneoff'
container_name: 'some-name'
doc = invokeTestSubject service
assert.equal doc.services['bb-net-service1'].container_name, 'some-name-net'

it 'should simply change the network_mode to use an existing netcontainer when the service type is anything other than service or oneoff', ->
service =
labels:
'bigboat.service.type': 'something-else'
'bigboat.service.name': 'myservice'
doc = invokeTestSubject service
assert.equal service.network_mode, 'service:bb-net-myservice'
assert.deepEqual doc, services: {}

0 comments on commit 8d4482b

Please sign in to comment.