/
test.js
175 lines (144 loc) · 3.83 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var cp = require('child_process')
var dowding = require('./')
var tape = require('tape')
var async = require('async')
var allServers = [{
hostname:'node1',
docker:'192.168.8.120:2375'
},{
hostname:'node2',
docker:'192.168.8.121:2375'
},{
hostname:'node3',
docker:'192.168.8.122:2375'
}]
var jobs = ['dowdingtest.1', 'dowdingtest.2', 'dowdingtest.3']
var scheduler = dowding({
inventory:function(done){
done(null, allServers)
}
})
function getTestCommand(name, server){
return 'docker -H tcp://' + server + ' run --name ' + name + ' -d binocarlos/bring-a-ping --timeout 1000'
}
// run 3 containers under the same namespace and make sure they are allocated across
// the 3 servers evenly
function runExclusion(t, done){
var allocated = {}
async.forEachSeries(jobs, function(job, nextJob){
scheduler.allocate({
name:job
}, function(err, server){
allocated[job] = server
var command = getTestCommand(job, server.docker)
cp.exec(command, function(err, stdout, stderr){
if(stderr) err = stderr.toString()
if(err) return nextJob(err)
nextJob()
})
})
}, function(err){
if(err) return done(err)
done(null, allocated)
})
}
function killExclusion(done){
async.forEachSeries(allServers, function(server, nextServer){
async.forEachSeries(jobs, function(job, nextJob){
cp.exec('docker -H tcp://' + server.docker + ' stop ' + job, function(){
cp.exec('docker -H tcp://' + server.docker + ' rm ' + job, function(){
nextJob()
})
})
}, function(){
nextServer()
})
}, function(err){
done()
})
}
tape('leastBusy returns one server', function(t){
scheduler.leastBusy(function(err, server){
t.ok(server.hostname.indexOf('node')==0, 'server has hostname')
t.end()
})
})
tape('cleanup exclusions', function(t){
killExclusion(function(){
t.end()
})
})
tape('mutual exclusion', function(t){
// lets run it 10 times to avoid it randomly having allocated correctly
var counter = 0
function nextTest(){
counter++
if(counter<=10){
runExclusion(t, function(err, allocations){
var servers = {
node1:0,
node2:0,
node3:0
}
Object.keys(allocations || {}).forEach(function(key){
var allocation = allocations[key]
servers[allocation.hostname]++
})
t.equal(servers.node1, 1, 'one job allocated to node1')
t.equal(servers.node2, 1, 'one job allocated to node2')
t.equal(servers.node3, 1, 'one job allocated to node3')
killExclusion(function(){
nextTest()
})
})
}
else{
// now try a 4th job which should error
runExclusion(t, function(err, allocations){
scheduler.allocate({
name:'dowdingtest.4'
}, function(err, server){
t.equal(err, 'no server found', 'no space for 4th job')
killExclusion(function(){
t.end()
})
})
})
}
}
nextTest()
})
tape('parents', function(t){
runExclusion(t, function(err, allocations){
async.forEachSeries(jobs, function(job, next){
scheduler.allocate({
parent:job
}, function(err, server){
if(err) return next(err)
var allocation = allocations[job]
t.equal(allocation.hostname, server.hostname, allocation.hostname + ' = ' + server.hostname)
next()
})
}, function(err){
if(err){
t.fail(err, 'parents')
t.end()
return
}
t.end()
})
})
})
tape('non existent parents', function(t){
scheduler.allocate({
parent:'doesnotexist'
}, function(err, server){
t.equal(err, 'no container: doesnotexist')
t.end()
})
})
tape('cleanup exclusions', function(t){
killExclusion(function(){
t.end()
})
})