-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDocker.test.js
More file actions
673 lines (583 loc) · 19.7 KB
/
Docker.test.js
File metadata and controls
673 lines (583 loc) · 19.7 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
require('rubico/global')
const assert = require('assert')
const Test = require('thunk-test')
const Docker = require('./Docker')
const resolvePath = require('./internal/resolvePath')
const sleep = require('./internal/sleep')
const Readable = require('./Readable')
const test1 = Test('Docker - prune', async function integration() {
const docker = new Docker({ apiVersion: '1.44' })
await docker.leaveSwarm({ force: true }).catch(() => {})
await docker.pruneContainers()
await docker.pruneVolumes()
await docker.pruneImages()
await docker.pruneNetworks()
{
const data = await docker.pruneContainers()
assert.equal(data.ContainersDeleted.length, 0)
assert.equal(data.SpaceReclaimed, 0)
}
{
const data = await docker.pruneVolumes()
assert.equal(data.VolumesDeleted.length, 0)
assert.equal(data.SpaceReclaimed, 0)
}
{
const data = await docker.pruneImages()
assert.equal(data.ImagesDeleted.length, 0)
assert.equal(data.SpaceReclaimed, 0)
}
{
const data = await docker.pruneNetworks()
assert.equal(data.NetworksDeleted.length, 0)
}
}).case()
const test2 = new Test('Docker - auth', async function integration() {
const docker = new Docker({ apiVersion: '1.44' })
const data = await docker.auth({
username: 'admin',
password: 'password',
email: 'test@example.com',
serveraddress: 'localhost:5000',
})
assert.equal(typeof data.Status, 'string')
}).case()
const test3 = new Test('Docker - image', async function integration() {
const docker = new Docker({ apiVersion: '1.44' })
await docker.removeImage('presidium-test:test').catch(() => {})
await docker.removeImage('localhost:5000/presidium-test:test').catch(() => {})
{ // pull node-15:alpine
const dataStream = await docker.pullImage('node:15-alpine')
dataStream.pipe(process.stdout)
await new Promise(resolve => dataStream.on('end', resolve))
}
{
const dataStream = await docker.buildImage(__dirname, {
image: 'presidium-test:test',
archive: {
Dockerfile: `
FROM node:15-alpine
WORKDIR /opt
COPY . .
EXPOSE 8888`,
},
})
dataStream.pipe(process.stdout)
await new Promise(resolve => {
dataStream.on('end', resolve)
})
}
{
const data = await docker.listImages()
assert(data.length >= 2)
for (const imageData of data) {
assert.equal(typeof imageData.Id, 'string')
assert.equal(typeof imageData.ParentId, 'string')
assert(['string', 'number'].includes(typeof imageData.Created))
assert.equal(typeof imageData.Size, 'number')
assert.equal(typeof imageData.SharedSize, 'number')
}
}
{
const data = await docker.tagImage(
'presidium-test:test',
'localhost:5000/presidium-test:test'
)
assert.equal(Object.keys(data).length, 0)
}
{
const dataStream = await docker.pushImage({
image: 'presidium-test:test',
registry: 'localhost:5000',
identitytoken: this.identitytoken,
})
dataStream.pipe(process.stdout)
await new Promise(resolve => {
dataStream.on('end', resolve)
})
}
{
const data = await docker.inspectImage('localhost:5000/presidium-test:test')
assert.equal(typeof data.Id, 'string')
assert(['string', 'number'].includes(typeof data.Created))
assert.equal(typeof data.Size, 'number')
assert.equal(typeof data.Architecture, 'string')
assert.equal(typeof data.Os, 'string')
}
{
const data1 = await docker.removeImage('presidium-test:test')
assert(data1.length > 0)
for (const item of data1) {
assert(item.Untagged || item.Deleted)
}
const data2 = await docker.removeImage('localhost:5000/presidium-test:test')
assert(data2.length > 0)
for (const item of data2) {
assert(item.Untagged || item.Deleted)
}
}
}).case()
const test4 = new Test('Docker - container', async function integration() {
const docker = new Docker({ apiVersion: '1.44' })
{
const data = await docker.createContainer({
name: `test-alpine-3-${Date.now()}`,
image: 'node:15-alpine',
cmd: ['node', '-e', 'console.log(\'test0\')'],
rm: true,
})
assert.equal(typeof data.Id, 'string')
assert(Array.isArray(data.Warnings))
const containerId = data.Id
const inspectData = await docker.inspectContainer(containerId)
assert.notEqual(inspectData.HostConfig.NanoCpus, 1e9)
assert.notEqual(inspectData.HostConfig.Memory, 256e6)
const attachDataStream = await docker.attachContainer(containerId)
const emptyAttachDataStream = await docker.attachContainer(containerId, {
stdout: false,
})
const startMessage = await docker.startContainer(containerId)
assert.equal(typeof startMessage, 'string')
const body = await Readable.Buffer(attachDataStream)
assert.equal(body.constructor, Buffer)
assert.strictEqual(body[0], 1) // stdout
assert.strictEqual(body[1], 0) // empty
assert.strictEqual(body[2], 0) // empty
assert.strictEqual(body[3], 0) // empty
assert.strictEqual(body[4], 0) // SIZE1
assert.strictEqual(body[5], 0) // SIZE2
assert.strictEqual(body[6], 0) // SIZE3
assert.strictEqual(body[7], 6) // SIZE4
assert.strictEqual(body.slice(8).toString('utf8'), 'test0\n')
this.removedContainerId = containerId
const emptyBody = await Readable.Buffer(emptyAttachDataStream)
assert.equal(Buffer.byteLength(emptyBody, 'utf8'), 0)
assert.equal(emptyBody.slice(8).toString('utf8'), '')
}
{
const data = await docker.createContainer({
name: `test-alpine-30-${Date.now()}`,
image: 'node:15-alpine',
cmd: ['node', '-e', 'console.error(\'test0\')'],
rm: true,
})
assert.equal(typeof data.Id, 'string')
assert(Array.isArray(data.Warnings))
const containerId = data.Id
const inspectData = await docker.inspectContainer(containerId)
assert.notEqual(inspectData.HostConfig.NanoCpus, 1e9)
assert.notEqual(inspectData.HostConfig.Memory, 256e6)
const attachDataStream = await docker.attachContainer(containerId)
const emptyAttachDataStream = await docker.attachContainer(containerId, {
stderr: false,
})
const startMessage = await docker.startContainer(containerId)
assert.equal(typeof startMessage, 'string')
const body = await Readable.Buffer(attachDataStream)
assert.equal(body.constructor, Buffer)
assert.strictEqual(body[0], 2) // stderr
assert.strictEqual(body[1], 0) // empty
assert.strictEqual(body[2], 0) // empty
assert.strictEqual(body[3], 0) // empty
assert.strictEqual(body[4], 0) // SIZE1
assert.strictEqual(body[5], 0) // SIZE2
assert.strictEqual(body[6], 0) // SIZE3
assert.strictEqual(body[7], 6) // SIZE4
assert.strictEqual(body.slice(8).toString('utf8'), 'test0\n')
this.removedContainerId2 = containerId
const emptyBody = await Readable.Buffer(emptyAttachDataStream)
assert.equal(Buffer.byteLength(emptyBody, 'utf8'), 0)
assert.equal(emptyBody.slice(8).toString('utf8'), '')
}
{
const data = await docker.createContainer({
name: `test-alpine-1-${Date.now()}`,
image: 'node:15-alpine',
cmd: ['node', '-e', 'console.log(\'test\')'],
workdir: '/opt/test0',
expose: [22, 8888, '8889/udp'],
env: { TEST: 'test' },
volume: ['/opt/my-volume'],
mounts: [{
source: 'other-volume',
target: '/opt/other-volume',
readonly: true,
}],
memory: 256e6, // bytes
cpus: 0.1,
restart: 'on-failure:5',
healthCmd: ['echo', 'ok'],
healthInterval: 1e9,
healthTimeout: 30e9,
healthRetries: 10,
healthStartPeriod: 5e9,
publish: {
23: 22, // hostPort -> containerPort[/protocol]
8888: '8000/tcp',
},
logDriver: 'json-file',
logDriverOptions: {
'max-file': '10',
'max-size': '100m',
},
})
const containerId = data.Id
const inspectData = await docker.inspectContainer(containerId)
assert.equal(inspectData.HostConfig.NanoCpus, 1e8)
assert.equal(inspectData.HostConfig.Memory, 256e6)
const attachDataStream = await docker.attachContainer(containerId)
const startMessage = await docker.startContainer(containerId)
assert.equal(typeof startMessage, 'string')
const body = await Readable.Buffer(attachDataStream)
assert.equal(body.constructor, Buffer)
assert.strictEqual(body[0], 1) // stdout
assert.strictEqual(body[1], 0) // empty
assert.strictEqual(body[2], 0) // empty
assert.strictEqual(body[3], 0) // empty
assert.strictEqual(body[4], 0) // SIZE1
assert.strictEqual(body[5], 0) // SIZE2
assert.strictEqual(body[6], 0) // SIZE3
assert.strictEqual(body[7], 5) // SIZE4
assert.strictEqual(body.slice(8).toString('utf8'), 'test\n')
this.containerId = containerId
}
{
const data = await docker.createContainer({
name: `test-alpine-2-${Date.now()}`,
image: 'node:15-alpine',
cmd: ['node', '-e', 'http.createServer((request, response) => response.end(\'x0\')).listen(2999)'],
rm: true,
})
const containerId = data.Id
const startMessage = await docker.startContainer(containerId)
assert.equal(typeof startMessage, 'string')
const execDataStream = await docker.execContainer(containerId, ['node', '-e', 'console.log(\'test123\')'])
const body = await Readable.Buffer(execDataStream)
assert.equal(body.constructor, Buffer)
assert.strictEqual(body[0], 1) // stdout
assert.strictEqual(body[1], 0) // empty
assert.strictEqual(body[2], 0) // empty
assert.strictEqual(body[3], 0) // empty
assert.strictEqual(body[4], 0) // SIZE1
assert.strictEqual(body[5], 0) // SIZE2
assert.strictEqual(body[6], 0) // SIZE3
assert.strictEqual(body[7], 8) // SIZE4
assert.strictEqual(body.slice(8).toString('utf8'), 'test123\n')
await docker.stopContainer(containerId, { time: 1 })
}
{
const runStream = await docker.runContainer({
name: `test-alpine-4-${Date.now()}`,
image: 'node:15-alpine',
cmd: ['node', '-e', 'console.log(\'x0\')'],
rm: true,
})
const body = await Readable.Buffer(runStream)
assert.equal(body.constructor, Buffer)
assert.strictEqual(body[0], 1) // stdout
assert.strictEqual(body[1], 0) // empty
assert.strictEqual(body[2], 0) // empty
assert.strictEqual(body[3], 0) // empty
assert.strictEqual(body[4], 0) // SIZE1
assert.strictEqual(body[5], 0) // SIZE2
assert.strictEqual(body[6], 0) // SIZE3
assert.strictEqual(body[7], 3) // SIZE4
assert.strictEqual(body.slice(8).toString('utf8'), 'x0\n')
}
await assert.rejects(
docker.removeImage('node:15-alpine'),
error => {
assert(error.message.includes('conflict'))
assert.equal(error.code, 409)
return true
},
)
{
const data = await docker.inspectContainer(this.containerId)
assert.equal(data.Config.Image, 'node:15-alpine')
assert.deepEqual(data.Config.Volumes, { '/opt/my-volume': {} })
assert.equal(data.Config.WorkingDir, '/opt/test0')
assert.deepEqual(data.Config.ExposedPorts, { '22/tcp': {}, '8888/tcp': {}, '8889/udp': {} })
assert.equal(data.HostConfig.Memory, 256e6)
assert.deepEqual(data.HostConfig.PortBindings, {
'22/tcp': [{ HostIp: '', HostPort: '23' }],
'8000/tcp': [{ HostIp: '', HostPort: '8888' }]
})
assert.deepEqual(data.HostConfig.LogConfig, {
Type: 'json-file',
Config: {
'max-file': '10',
'max-size': '100m',
},
})
assert.deepEqual(data.Config.Healthcheck, {
Test: ['CMD', 'echo', 'ok'],
Interval: 1000000000,
Timeout: 30000000000,
StartPeriod: 5000000000,
Retries: 10,
})
assert.deepEqual(data.HostConfig.Mounts, [{
Type: 'volume',
Source: 'other-volume',
Target: '/opt/other-volume',
ReadOnly: true
}])
}
{
const data = await docker.listContainers()
assert(data.length > 0)
assert(!data.map(get('Id')).includes(this.removedContainerId))
assert(!data.map(get('Id')).includes(this.removedContainerId2))
for (item of data) {
assert.equal(typeof item.Id, 'string')
assert.equal(typeof item.Image, 'string')
assert.equal(typeof item.State, 'string')
}
}
}).case()
const test5 = new Test('Docker - swarm', async function integration() {
const docker = new Docker({ apiVersion: '1.44' })
await docker.leaveSwarm({ force: true }).catch(() => {})
{ // initSwarm
const nodeId = await docker.initSwarm('[::1]:2377')
assert.equal(typeof nodeId, 'string')
}
{ // listNodes
const nodes = await docker.listNodes()
assert.equal(nodes.length, 1) // just this computer
this.nodeId = nodes[0].ID
}
// attempt deleteNode
await assert.rejects(
docker.deleteNode(this.nodeId),
error => {
assert(error instanceof Error)
assert.equal(error.code, 400)
return true
},
)
{ // inspectSwarm
const data = await docker.inspectSwarm()
assert.equal(typeof data.JoinTokens.Worker, 'string')
assert.equal(typeof data.JoinTokens.Manager, 'string')
this.managerJoinToken = data.JoinTokens.Manager
this.workerJoinToken = data.JoinTokens.Worker
}
{ // createNetwork
const data1 = await docker.createNetwork({
name: 'my-network',
driver: 'overlay',
subnet: '10.11.0.0/20',
gateway: '10.11.0.1',
})
assert.equal(typeof data1.Id, 'string')
const data2 = await docker.createNetwork({
name: 'my-other-network',
driver: 'overlay',
})
assert.equal(typeof data2.Id, 'string')
}
{ // inspect my-network
const data = await docker.inspectNetwork('my-network')
assert.equal(typeof data.Id, 'string')
assert.equal(data.Scope, 'swarm')
}
{ // create a service
const data1 = await docker.createService('service1', {
image: 'node:15-alpine',
labels: { foo: 'bar' },
replicas: 2,
cmd: ['node', '-e', 'http.createServer((request, response) => response.end(\'service1\')).listen(3000)'],
workdir: '/opt/test0',
env: { TEST: 'test' },
mounts: [{
source: 'other-volume',
target: '/opt/other-volume',
readonly: true,
}],
memory: 256e6, // bytes
cpus: 0.1,
gpus: 'all',
restart: 'on-failure:5',
healthCmd: ['wget', '--no-verbose', '--tries=1', '--spider', 'localhost:3000'],
healthInterval: 1e9,
healthTimeout: 30e9,
healthRetries: 10,
healthStartPeriod: 5e9,
publish: {
// 23: 22, // hostPort -> containerPort[/protocol]
8080: 3000,
},
logDriver: 'json-file',
logDriverOptions: {
'max-file': '10',
'max-size': '100m',
},
network: 'my-network',
})
const serviceId = data1.ID
const data2 = await docker.inspectService(serviceId)
assert.equal(data2.Spec.Labels.foo, 'bar')
assert.equal(data2.Spec.TaskTemplate.Resources.Reservations.NanoCPUs, 100000000)
assert.equal(data2.Spec.TaskTemplate.Resources.Reservations.MemoryBytes, 256000000)
this.serviceId1 = serviceId
}
{ // create the same service
const p = docker.createService('service1', {
image: 'node:15-alpine',
labels: { foo: 'bar' },
replicas: 2,
cmd: ['node', '-e', 'http.createServer((request, response) => response.end(\'service1\')).listen(3000)'],
workdir: '/opt/test0',
env: { TEST: 'test' },
mounts: [{
source: 'other-volume',
target: '/opt/other-volume',
readonly: true,
}],
memory: 256e6, // bytes
cpus: 0.1,
gpus: 'all',
restart: 'on-failure:5',
healthCmd: ['wget', '--no-verbose', '--tries=1', '--spider', 'localhost:3000'],
healthInterval: 1e9,
healthTimeout: 30e9,
healthRetries: 10,
healthStartPeriod: 5e9,
publish: {
// 23: 22, // hostPort -> containerPort[/protocol]
8080: 3000,
},
logDriver: 'json-file',
logDriverOptions: {
'max-file': '10',
'max-size': '100m',
},
network: 'my-network',
})
await assert.rejects(p, error => {
return true
})
}
{ // create another service
const data1 = await docker.createService('service2', {
image: 'node:15-alpine',
replicas: 'global',
cmd: ['node', '-e', 'http.createServer((request, response) => response.end(\'service2\')).listen(3001)'],
workdir: '/opt/test0',
env: { TEST: 'test' },
mounts: [{
source: 'other-volume',
target: '/opt/other-volume',
readonly: true,
}],
restart: 'on-failure',
publish: { 8081: 3001 },
healthCmd: ['wget', '--no-verbose', '--tries=1', '--spider', 'localhost:3001'],
})
const serviceId = data1.ID
const data2 = await docker.inspectService(serviceId)
assert.equal(Object.keys(data2.Spec.TaskTemplate.Resources.Reservations), 0)
this.serviceId2 = serviceId
}
await sleep(1000)
{ // updateService
const data1 = await docker.updateService('service2', {
image: 'node:17-alpine',
memory: 256e6, // bytes
cpus: 0.1,
})
assert.equal(data1.Warnings, null)
const data2 = await docker.inspectService(this.serviceId2)
assert.equal(data2.Spec.TaskTemplate.Resources.Reservations.NanoCPUs, 100000000)
assert.equal(data2.Spec.TaskTemplate.Resources.Reservations.MemoryBytes, 256000000)
}
{ // listServices
let data = await docker.listServices()
while (data.length < 2) {
await sleep(1000)
data = await docker.listServices()
}
assert.equal(data.length, 2)
const serviceIds = data.map(get('ID'))
assert(serviceIds.includes(this.serviceId1))
assert(serviceIds.includes(this.serviceId2))
}
{ // listTasks
let data = await docker.listTasks()
while (data.length < 4) {
await sleep(1000)
data = await docker.listTasks()
}
assert.equal(data.length, 4, data) // 2 for service1, 1 for service2 (global), 1 for service2 (update)
for (const item of data) {
assert.equal(typeof item.ID, 'string')
assert.equal(typeof item.Version, 'object')
assert.equal(typeof item.Spec, 'object')
assert.equal(typeof item.ServiceID, 'string')
assert.equal(typeof item.Status, 'object')
assert.equal(typeof item.DesiredState, 'string')
}
}
{ // listTasks filter
const data = await docker.listTasks({ desiredState: 'accepted' })
assert.equal(data.length, 0)
}
{ // listTasks filter
const data = await docker.listTasks({ service: 'service2' })
assert.equal(data.length, 2)
assert.equal(data[0].ServiceID, this.serviceId2)
assert.equal(data[1].ServiceID, this.serviceId2)
}
{ // deleteService
const message = await docker.deleteService(this.serviceId1)
assert.equal(typeof message, 'string')
}
{ // listServices after deleteService
const data = await docker.listServices()
assert.equal(data.length, 1)
}
// join own swarm
await assert.rejects(
docker.joinSwarm('[::1]:2377', {
RemoteAddrs: ['[::1]:2377'],
JoinToken: this.managerJoinToken,
}),
error => {
assert(error instanceof Error)
assert.equal(error.code, 503)
return true
},
)
// leaveSwarm without forcing
await assert.rejects(
docker.leaveSwarm(),
error => {
assert(error instanceof Error)
assert.equal(error.code, 503)
return true
}
)
{ // delete networks
const message1 = await docker.deleteNetwork('my-network')
assert.equal(typeof message1, 'string')
const message2 = await docker.deleteNetwork('my-other-network')
assert.equal(typeof message2, 'string')
}
{ // force leaveSwarm
const message = await docker.leaveSwarm({ force: true })
assert.equal(typeof message, 'string')
}
}).case()
const test = Test.all([
test1,
test2,
test3,
test4,
test5,
])
if (process.argv[1] == __filename) {
test()
}
module.exports = test