-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathcompose.test.ts
1188 lines (996 loc) · 38 KB
/
compose.test.ts
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import {
describe,
it,
expect,
beforeEach,
afterEach,
vi,
beforeAll
} from 'vitest'
import Docker, { ContainerInfo } from 'dockerode'
import * as compose from '../src'
import * as path from 'path'
import { readFile } from 'fs'
import { mapPsOutput, mapImListOutput } from '../src'
const docker = new Docker()
const isContainerRunning = async (name: string): Promise<boolean> =>
new Promise((resolve, reject): void => {
docker.listContainers((err, containers): void => {
if (err) {
reject(err)
}
const running = (containers || []).filter((container): boolean =>
container.Names.includes(name)
)
console.log('running containers', running)
resolve(running.length > 0)
})
})
const getAllContainers = async (): Promise<string[]> => {
return new Promise((resolve, reject) => {
let all = new Array<string>()
docker.listContainers({ all: true }, (err, containers) => {
if (err) {
return reject(err)
}
console.log('containers', containers?.length)
containers?.forEach((container: ContainerInfo) => {
console.log(container.Id)
return (all = [...all, container.Id])
})
return resolve(all)
})
})
}
const getRunningContainers = async (): Promise<string[]> => {
return new Promise((resolve, reject) => {
let all = new Array<string>()
docker.listContainers((err, containers) => {
if (err) {
return reject(err)
}
console.log('containers', containers?.length)
containers?.forEach((container: ContainerInfo) => {
console.log(container.Id)
return (all = [...all, container.Id])
})
return resolve(all)
})
})
}
const removeContainers = async (containerIds: string[]) => {
for (const id of containerIds) {
const container = docker.getContainer(id)
await container.remove()
}
}
const repoTags = (imageInfo): string[] => imageInfo.RepoTags || []
const imageExists = async (name: string): Promise<boolean> => {
const images = await docker.listImages()
const foundImage = images.findIndex((imageInfo): boolean =>
repoTags(imageInfo).includes(name)
)
return foundImage > -1
}
const removeImagesStartingWith = async (
searchString: string
): Promise<void> => {
const images = await docker.listImages()
for (const image of images) {
for (const repoTag of repoTags(image)) {
if (repoTag.startsWith(searchString)) {
const dockerImage = docker.getImage(repoTag)
if (logOutput) {
process.stdout.write(
`removing image ${repoTag} ${dockerImage.id || ''}`
)
}
await dockerImage.remove()
}
}
}
}
const logOutput = true
describe('when upAll is called', () => {
it('container get started', async () => {
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
await compose.upAll({ cwd: path.join(__dirname), log: logOutput })
expect(await isContainerRunning('/compose_test_web')).toBeTruthy()
expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy()
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
})
})
describe('when downOne is called', () => {
it('only one container should stop', async () => {
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
await compose.upAll({ cwd: path.join(__dirname), log: logOutput })
await compose.downOne('web', { cwd: path.join(__dirname), log: logOutput })
expect(await isContainerRunning('/compose_test_web')).toBeFalsy()
expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy()
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
})
})
describe('when downMany is called', () => {
it('only specified container(s) should stop', async () => {
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
await compose.upAll({ cwd: path.join(__dirname), log: logOutput })
await compose.downMany('web', { cwd: path.join(__dirname), log: logOutput })
expect(await isContainerRunning('/compose_test_web')).toBeFalsy()
expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy()
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
})
})
describe('when running a compose command', () => {
it('should return correct status code', async () => {
let result = await compose.downAll({
cwd: path.join(__dirname),
log: logOutput
})
expect(result).toMatchObject({
exitCode: 0
})
result = await compose.upAll({ cwd: path.join(__dirname), log: logOutput })
expect(result).toMatchObject({
exitCode: 0
})
let failedResult = 0
try {
await compose.logs('non_existent_service', {
cwd: path.join(__dirname)
})
} catch (error: any) {
failedResult = error.exitCode
}
expect(failedResult).toBe(1)
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
})
})
describe('when starting container with --build option', () => {
describe('starts containers properly', (): void => {
beforeEach(
async (): Promise<void> => {
await compose.downAll({
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose-build.yml'
})
}
)
afterEach(
async (): Promise<void> => {
await compose.downAll({
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose-build.yml'
})
}
)
it('container gets started with --build option from array', async (): Promise<void> => {
await compose.upAll({
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose-build.yml',
commandOptions: [['--build']]
})
expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy()
await compose.downAll({
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose-build.yml'
})
})
it('ensure container gets started with --build option from string', async (): Promise<void> => {
await compose.upAll({
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose-build.yml',
commandOptions: ['--build']
})
expect(await isContainerRunning('/compose_test_nginx')).toBeTruthy()
await compose.downAll({
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose-build.yml'
})
})
})
})
describe('when container command executed with --workdir command option', () => {
it('should work', async () => {
await compose.downAll({
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose-42.yml'
})
const result = await compose.run('some-service', 'pwd', {
cwd: path.join(__dirname),
log: true,
config: 'docker-compose-42.yml',
composeOptions: ['--verbose'],
// Alpine has "/" as default
commandOptions: ['--workdir', '/home/root']
})
expect(result.out).toBe('/home/root\n')
const all = await getAllContainers()
console.log('running containers', all)
await removeContainers(all)
})
})
describe('when starting a single container', () => {
it('container gets started', async (): Promise<void> => {
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
await compose.upOne('web', { cwd: path.join(__dirname), log: logOutput })
expect(await isContainerRunning('/compose_test_web')).toBeTruthy()
expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy()
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
})
})
describe('when starting multiple containers', () => {
it('all containers get started', async (): Promise<void> => {
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
await compose.upMany(['web'], { cwd: path.join(__dirname), log: logOutput })
expect(await isContainerRunning('/compose_test_web')).toBeTruthy()
expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy()
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
})
})
describe('when calling down on compose file', () => {
it('should stop and remove container', async (): Promise<void> => {
await compose.upAll({ cwd: path.join(__dirname), log: logOutput })
expect(await isContainerRunning('/compose_test_web')).toBeTruthy()
expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy()
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
expect(await isContainerRunning('/compose_test_web')).toBeFalsy()
expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy()
const all = await getAllContainers()
expect(all.length).toBe(0)
})
})
describe('when calling stop on compose file', () => {
it('ensure container gets stopped', async (): Promise<void> => {
await compose.upAll({ cwd: path.join(__dirname), log: logOutput })
expect(await isContainerRunning('/compose_test_web')).toBeTruthy()
expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy()
await compose.stop({ cwd: path.join(__dirname), log: logOutput })
expect(await isContainerRunning('/compose_test_web')).toBeFalsy()
expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy()
const containers = await getAllContainers()
expect(containers.length).toBe(3)
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
})
})
describe('when stopping only one container', () => {
it('only this container gets stopped', async (): Promise<void> => {
await compose.upAll({ cwd: path.join(__dirname), log: logOutput })
expect(await isContainerRunning('/compose_test_web')).toBeTruthy()
expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy()
await compose.stopOne('proxy', {
cwd: path.join(__dirname),
log: logOutput
})
expect(await isContainerRunning('/compose_test_web')).toBeTruthy()
expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy()
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
})
})
describe('when stopping multiple containers', () => {
it('these containers get stopped', async (): Promise<void> => {
await compose.upAll({ cwd: path.join(__dirname), log: logOutput })
expect(await isContainerRunning('/compose_test_web')).toBeTruthy()
expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy()
await compose.stopMany(
{ cwd: path.join(__dirname), log: logOutput },
'proxy',
'web'
)
expect(await isContainerRunning('/compose_test_web')).toBeFalsy()
expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy()
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
})
})
describe('when pausing and resuming a single container', () => {
it('only single container gets paused then resumed', async (): Promise<void> => {
const opts = { cwd: path.join(__dirname), log: logOutput }
await compose.upAll(opts)
expect(await isContainerRunning('/compose_test_web')).toBeTruthy()
expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy()
await compose.pauseOne('proxy', opts)
expect(await isContainerRunning('/compose_test_web')).toBeTruthy()
expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy()
let errMsg
try {
await compose.exec('proxy', 'cat /etc/os-release', opts)
} catch (err: any) {
errMsg = err.err
}
expect(errMsg).toContain('is paused')
await compose.unpauseOne('proxy', opts)
expect(await isContainerRunning('/compose_test_web')).toBeTruthy()
expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy()
const std = await compose.exec('proxy', 'cat /etc/os-release', opts)
expect(std.out).toContain('Alpine Linux')
await compose.downAll(opts)
})
})
describe('when container gets started with --abort-on-container-exit option', () => {
it('should start', async (): Promise<void> => {
const result = await compose.upAll({
cwd: path.join(__dirname),
log: logOutput,
commandOptions: ['--abort-on-container-exit']
})
expect(result).toMatchObject({
exitCode: 0
})
expect(await isContainerRunning('/compose_test_web')).toBeFalsy()
expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy()
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
})
})
describe('when container gets started with --abort-on-container-exit option', () => {
it('should abort all services when a container exits', async (): Promise<void> => {
const result = await compose.upAll({
cwd: path.join(__dirname),
log: logOutput,
commandOptions: ['--abort-on-container-exit']
})
expect(result.err).toContain('Aborting on container exit')
expect(await isContainerRunning('/compose_test_web')).toBeFalsy()
expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy()
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
})
})
describe('when killing a container', () => {
it('should not run', async (): Promise<void> => {
await compose.upAll({ cwd: path.join(__dirname), log: logOutput })
console.log('up')
expect(await isContainerRunning('/compose_test_web')).toBeTruthy()
expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy()
await compose.kill({ cwd: path.join(__dirname), log: logOutput })
expect(await isContainerRunning('/compose_test_web')).toBeFalsy()
expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy()
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
})
})
describe('when using custom yml file name', () => {
it('should be used to start, kill and down containers', async (): Promise<void> => {
const config = './docker-compose-2.yml'
const cwd = path.join(__dirname)
await compose.upAll({ cwd, log: logOutput, config })
expect(await isContainerRunning('/compose_test_web_2')).toBeTruthy()
// config & [config] are the same thing, ensures that multiple configs are handled properly
await compose.kill({ cwd, log: logOutput, config: config })
expect(await isContainerRunning('/compose_test_web_2')).toBeFalsy()
await compose.downAll({ cwd, log: logOutput, config })
})
})
describe('when using run and exec', () => {
it('containers should run and exec commands', async (): Promise<void> => {
const checkOSID = (out, id): void => {
// parse /etc/os-release contents
const re = /([\w,_]+)=(.*)/g
let match
const os: { ID?: string } = {}
while ((match = re.exec(out)) !== null) {
// eslint-disable-line no-cond-assign
os[match[1]] = match[2]
}
expect(os.ID).toBe(id)
}
const opts = { cwd: path.join(__dirname), log: logOutput }
await compose.upAll(opts)
expect(await isContainerRunning('/compose_test_web')).toBeTruthy()
let std = await compose.exec('web', 'cat /etc/os-release', opts)
checkOSID(std.out, 'debian')
std = await compose.run('proxy', 'cat /etc/os-release', opts)
checkOSID(std.out, 'alpine')
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
const ids = await getAllContainers()
await removeContainers(ids)
})
})
describe('when using and exec using in an array', (): void => {
it('containers should run and exec commands', async (): Promise<void> => {
const checkOSID = (out, id): void => {
// parse /etc/os-release contents
const re = /([\w,_]+)=(.*)/g
let match
const os: { ID?: string } = {}
while ((match = re.exec(out)) !== null) {
// eslint-disable-line no-cond-assign
os[match[1]] = match[2]
}
expect(os.ID).toBe(id)
}
const opts = { cwd: path.join(__dirname), log: false }
await compose.upAll(opts)
expect(await isContainerRunning('/compose_test_web')).toBe(true)
let std = await compose.exec(
'web',
['/bin/sh', '-c', 'cat /etc/os-release'],
opts
)
checkOSID(std.out, 'debian')
std = await compose.run(
'proxy',
['/bin/sh', '-c', 'cat /etc/os-release'],
opts
)
checkOSID(std.out, 'alpine')
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
const ids = await getAllContainers()
await removeContainers(ids)
})
})
describe('when using build config as string', (): void => {
it('build should use config', async (): Promise<void> => {
const configuration = await new Promise<string>(function (
resolve,
reject
): void {
readFile(
path.join(__dirname, 'docker-compose-2.yml'),
function (err, content) {
if (err) {
reject(err)
return
}
resolve(content.toString())
}
)
})
const config = {
configAsString: configuration,
log: logOutput
}
await compose.upAll(config)
const result = await compose.port('web', 8888, config)
expect(result.data.address).toBe('0.0.0.0')
expect(result.data.port).toBe(8888)
await compose.downAll(config)
})
})
describe('when building single service', (): void => {
it('should only build this service', async (): Promise<void> => {
const opts = {
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose-build.yml'
}
await removeImagesStartingWith('compose-test-build-image')
await compose.buildOne('build_test_1', opts)
expect(await imageExists('compose-test-build-image-1:test')).toBeTruthy()
expect(await imageExists('compose-test-build-image-2:test')).toBeFalsy()
expect(await imageExists('compose-test-build-image-3:test')).toBeFalsy()
expect(await imageExists('compose-test-build-image-4:test')).toBeFalsy()
await removeImagesStartingWith('compose-test-build-image')
})
})
describe('when building multiple services', (): void => {
it('should build these services', async (): Promise<void> => {
const opts = {
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose-build.yml'
}
await compose.buildMany(['build_test_2', 'build_test_3'], opts)
expect(await imageExists('compose-test-build-image-1:test')).toBeFalsy()
expect(await imageExists('compose-test-build-image-2:test')).toBeTruthy()
expect(await imageExists('compose-test-build-image-3:test')).toBeTruthy()
expect(await imageExists('compose-test-build-image-4:test')).toBeFalsy()
await removeImagesStartingWith('compose-test-build-image')
})
})
describe('when building all services', (): void => {
it('should build all services', async (): Promise<void> => {
const opts = {
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose-build.yml'
}
await compose.buildAll(opts)
expect(await imageExists('compose-test-build-image-1:test')).toBeTruthy()
expect(await imageExists('compose-test-build-image-2:test')).toBeTruthy()
expect(await imageExists('compose-test-build-image-3:test')).toBeTruthy()
expect(await imageExists('compose-test-build-image-4:test')).toBeTruthy()
await removeImagesStartingWith('compose-test-build-image')
})
})
describe('when pulling a single service', (): void => {
it('only this service gets pulled', async (): Promise<void> => {
const opts = {
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose.yml'
}
await removeImagesStartingWith('nginx:1.19.9-alpine')
expect(await imageExists('nginx:1.19.9-alpine')).toBeFalsy()
await compose.pullOne('proxy', opts)
expect(await imageExists('nginx:1.19.9-alpine')).toBeTruthy()
})
})
describe('when pulling multiple services', (): void => {
it('pulls multiple services', async (): Promise<void> => {
const opts = {
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose.yml'
}
await removeImagesStartingWith('nginx:1.16.0')
await removeImagesStartingWith('nginx:1.19.9-alpine')
expect(await imageExists('nginx:1.16.0')).toBeFalsy()
expect(await imageExists('nginx:1.19.9-alpine')).toBeFalsy()
await compose.pullMany(['web', 'proxy'], opts)
expect(await imageExists('nginx:1.16.0')).toBeTruthy()
expect(await imageExists('nginx:1.19.9-alpine')).toBeTruthy()
})
})
describe('when pulling all services', (): void => {
it('pulls all services', async (): Promise<void> => {
const opts = {
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose.yml'
}
await removeImagesStartingWith('nginx:1.16.0')
await removeImagesStartingWith('nginx:1.19.9-alpine')
expect(await imageExists('nginx:1.16.0')).toBeFalsy()
expect(await imageExists('nginx:1.19.9-alpine')).toBeFalsy()
await compose.pullAll(opts)
expect(await imageExists('nginx:1.16.0')).toBeTruthy()
expect(await imageExists('nginx:1.19.9-alpine')).toBeTruthy()
})
})
describe('when calling config command', (): void => {
it('shows data for docker-compose files', async (): Promise<void> => {
const std = await compose.config({
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose-42.yml'
})
// expect(std.data.config.version).toBe('3') // output doesn't include this any longer
expect(std.data.config.services['some-service']['image']).toBe(
'nginx:1.19.9-alpine'
)
expect(std.data.config.volumes['db-data']).toEqual({ name: 'test_db-data' })
})
})
describe('when calling config command for services', (): void => {
it('shows data services', async (): Promise<void> => {
const std = await compose.configServices({
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose-build.yml'
})
expect(std.data.services.length).toBe(5)
expect(std.data.services).toContain('build-nginx')
expect(std.exitCode).toBe(0)
})
})
describe('when calling config command for volumes', (): void => {
it('show data for volumes', async (): Promise<void> => {
const std = await compose.configVolumes({
cwd: path.join(__dirname),
log: logOutput,
config: 'docker-compose-42.yml'
})
expect(std.data.volumes.length).toBe(1)
expect(std.data.volumes[0]).toContain('db-data')
expect(std.exitCode).toBe(0)
})
})
describe('when calling ps command', (): void => {
it('ps shows status data for started containers', async (): Promise<void> => {
await compose.upAll({ cwd: path.join(__dirname), log: logOutput })
// await new Promise((resolve) => setTimeout(resolve, 2000))
const std = await compose.ps({ cwd: path.join(__dirname), log: logOutput })
const running = await getRunningContainers()
expect(std.exitCode).toBe(0)
expect(std.data.services.length).toBe(2)
const web = std.data.services.find(
(service) => service.name === 'compose_test_web'
)
expect(web?.command).toContain('nginx') // Note: actually it contains "nginx -g 'daemon off;'"
expect(web?.state).toContain('Up')
expect(web?.ports.length).toBe(2)
expect(web?.ports[1].exposed.port).toBe(443)
expect(web?.ports[1].exposed.protocol).toBe('tcp')
expect(web?.ports[1].mapped?.port).toBe(443)
expect(web?.ports[1].mapped?.address).toBe('0.0.0.0')
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
})
it('ps shows status data for started containers using json format', async (): Promise<void> => {
await compose.upAll({ cwd: path.join(__dirname), log: logOutput })
// await new Promise((resolve) => setTimeout(resolve, 2000))
const std = await compose.ps({
cwd: path.join(__dirname),
log: logOutput,
commandOptions: [['--format', 'json']]
})
const running = await getRunningContainers()
expect(std.exitCode).toBe(0)
expect(std.data.services.length).toBe(2)
const web = std.data.services.find(
(service) => service.name === 'compose_test_web'
)
expect(web?.command).toContain('nginx') // Note: actually it contains "nginx -g 'daemon off;'"
expect(web?.state).toBe('running')
expect(web?.ports.length).toBe(2)
expect(web?.ports[1].exposed.port).toBe(443)
expect(web?.ports[1].exposed.protocol).toBe('tcp')
expect(web?.ports[1].mapped?.port).toBe(443)
expect(web?.ports[1].mapped?.address).toBe('0.0.0.0')
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
})
it('ps does not show status data for stopped containers', async (): Promise<void> => {
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
// await new Promise((resolve) => setTimeout(resolve, 1000))
await compose.upOne('web', { cwd: path.join(__dirname), log: logOutput })
await new Promise((resolve) => setTimeout(resolve, 2000))
const std = await compose.ps({ cwd: path.join(__dirname), log: logOutput })
console.log('data', std.data.services)
expect(std.data.services.length).toBe(1)
expect(std.exitCode).toBe(0)
const web = std.data.services.find(
(service) => service.name === 'compose_test_web'
)
const proxy = std.data.services.find(
(service) => service.name === 'compose_test_proxy'
)
expect(web?.name).toBe('compose_test_web')
expect(proxy).toBeFalsy()
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
}, 30000)
})
describe('when calling image list command', (): void => {
it('image list shows image data', async (): Promise<void> => {
await compose.createAll({ cwd: path.join(__dirname), log: logOutput })
const std = await compose.images({
cwd: path.join(__dirname),
log: logOutput
})
console.log(std.out)
expect(std.exitCode).toBe(0)
expect(std.data.services.length).toBe(3)
const web = std.data.services.find(
(service) => service.container === 'compose_test_web'
)
expect(web).toBeDefined()
expect(web?.repository).toBe('nginx')
expect(web?.tag).toBe('1.16.0')
expect(web?.id).toBeTruthy()
expect(web?.id).toMatch(/^\w{12}$/)
const hello = std.data.services.find(
(service) => service.container === 'compose_test_hello'
)
expect(hello).toBeDefined()
expect(hello?.repository).toBe('hello-world')
expect(hello?.tag).toBe('latest')
expect(hello?.id).toMatch(/^\w{12}$/)
})
it('image list shows image data using json format', async (): Promise<void> => {
await compose.createAll({ cwd: path.join(__dirname), log: logOutput })
const std = await compose.images({
cwd: path.join(__dirname),
log: logOutput,
commandOptions: [['--format', 'json']]
})
expect(std.exitCode).toBe(0)
expect(std.data.services.length).toBe(3)
const web = std.data.services.find(
(service) => service.container === 'compose_test_web'
)
expect(web).toBeDefined()
expect(web?.repository).toBe('nginx')
expect(web?.tag).toBe('1.16.0')
expect(web?.id).toMatch(/^\w{12}$/)
const hello = std.data.services.find(
(service) => service.container === 'compose_test_hello'
)
expect(hello).toBeDefined()
expect(hello?.repository).toBe('hello-world')
expect(hello?.tag).toBe('latest')
expect(hello?.id).toMatch(/^\w{12}$/)
})
})
describe('when restarting all containers', (): void => {
it('all containers restart', async (): Promise<void> => {
await compose.upAll({ cwd: path.join(__dirname), log: logOutput })
await compose.restartAll({ cwd: path.join(__dirname), log: logOutput })
expect(await isContainerRunning('/compose_test_web')).toBeTruthy()
expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy()
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
})
})
describe('when restarting many containers', (): void => {
it('restarts selected containers', async (): Promise<void> => {
await compose.upAll({ cwd: path.join(__dirname), log: logOutput })
await compose.restartMany(['web', 'proxy'], {
cwd: path.join(__dirname),
log: logOutput
})
expect(await isContainerRunning('/compose_test_web')).toBeTruthy()
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
})
})
describe('when restarting one container', (): void => {
it('does restart one container', async (): Promise<void> => {
await compose.upAll({ cwd: path.join(__dirname), log: logOutput })
await compose.restartOne('proxy', {
cwd: path.join(__dirname),
log: logOutput
})
expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy()
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
})
})
describe('logs command', (): void => {
it('does follow service logs', async (): Promise<void> => {
await compose.upAll({ cwd: path.join(__dirname), log: logOutput })
const std = await compose.logs('proxy', {
cwd: path.join(__dirname),
log: logOutput
})
expect(std.out.includes('compose_test_proxy')).toBeTruthy()
await compose.downAll({ cwd: path.join(__dirname), log: logOutput })
})
})
describe('port command', (): void => {
it('returns the port for a started service', async (): Promise<void> => {
const config = {
cwd: path.join(__dirname),
config: './docker-compose-2.yml',
log: logOutput
}
await compose.upAll(config)
const port = await compose.port('web', 8888, config)
expect(port.out).toMatch(/.*:[0-9]{1,5}/)
await compose.downAll(config)
})
})
describe('rm command', (): void => {
it('removes container', async (): Promise<void> => {
const config = {
cwd: path.join(__dirname),
config: './docker-compose.yml',
log: logOutput
}
await compose.upAll(config)
expect(await isContainerRunning('/compose_test_web')).toBeTruthy()
expect(await isContainerRunning('/compose_test_proxy')).toBeTruthy()
await compose.rm({ ...config, commandOptions: ['-s'] }, 'proxy')
expect(await isContainerRunning('/compose_test_web')).toBeTruthy()
expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy()
await compose.rm({ ...config, commandOptions: ['-s'] }, 'proxy', 'web')
expect(await isContainerRunning('/compose_test_web')).toBeFalsy()
expect(await isContainerRunning('/compose_test_proxy')).toBeFalsy()
})
})
describe('version command', (): void => {
it('returns version information', async (): Promise<void> => {
const version = (await compose.version()).data.version
expect(version).toMatch(/^(\d+\.)?(\d+\.)?(\*|\d+)?(\+.*)*(-\w+(\.\d+))?$/)
})
})
describe('parsePsOutput', (): void => {
it('parses ps output', () => {
// eslint-disable-next-line no-useless-escape
const output = `NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS\ncompose_test_hello hello-world \"/hello\" hello 2 seconds ago Exited (0) Less than a second ago \ncompose_test_proxy nginx:1.19.9-alpine \"/docker-entrypoint.…\" proxy 2 seconds ago Up Less than a second 80/tcp\ncompose_test_web nginx:1.16.0 \"nginx -g 'daemon of…\" web 2 seconds ago Up 1 second 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp\n`
const psOut = mapPsOutput(output)
// prettier-ignore
expect(psOut.services[0]).toEqual({
command: '"/hello"',
name: 'compose_test_hello',
state: 'Exited (0) Less than a second ago',
ports: []
})
// prettier-ignore
expect(psOut.services[1]).toEqual({
command: '"/docker-entrypoint.…"',
name: 'compose_test_proxy',
state: 'Up Less than a second',
ports: [{ exposed: { port: 80, protocol: 'tcp' } }]
})
expect(psOut.services[2]).toEqual({
command: '"nginx -g \'daemon of…"',
name: 'compose_test_web',
state: 'Up 1 second',
ports: [
{
exposed: { port: 80, protocol: 'tcp' },
mapped: { port: 80, address: '0.0.0.0' }
},
{
exposed: { port: 443, protocol: 'tcp' },
mapped: { port: 443, address: '0.0.0.0' }
}
]
})
})
})
describe('ps command in quiet mode', (): void => {
it('ps returns container ids when quiet', () => {
const output = `64848fc721dfeff435edc7d4bb42e2f0e0a10d0c7602b73729a7fd7b09b7586f
aed60ce17575e69c56cc4cb07eeba89b5d7b7b2b307c8b87f3363db6af850719
f49548fa0b1f88846b78c65c6ea7f802bcbdfb2cf10204497eb89ba622d7715b
`
const psOut = mapPsOutput(output, { commandOptions: ['-q'] })
expect(psOut.services[0]).toEqual(
expect.objectContaining({
name: '64848fc721dfeff435edc7d4bb42e2f0e0a10d0c7602b73729a7fd7b09b7586f'
})
)
expect(psOut.services[1]).toEqual(
expect.objectContaining({
name: 'aed60ce17575e69c56cc4cb07eeba89b5d7b7b2b307c8b87f3363db6af850719'
})
)