/
dev.test.ts
965 lines (903 loc) · 35.9 KB
/
dev.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
import { expect, test } from '@oclif/test';
import yaml from 'js-yaml';
import path from 'path';
import sinon from 'sinon';
import { buildSpecFromYml, ComponentConfig, resourceRefToNodeRef } from '../../src';
import AppService from '../../src/app-config/service';
import Dev from '../../src/commands/dev';
import DockerComposeTemplate from '../../src/common/docker-compose/template';
import DeployUtils from '../../src/common/utils/deploy.utils';
import * as Docker from '../../src/common/utils/docker';
import * as ComponentBuilder from '../../src/dependency-manager/spec/utils/component-builder';
import { MOCK_API_HOST } from '../utils/mocks';
// set to true while working on tests for easier debugging; otherwise oclif/test eats the stdout/stderr
const print = false;
const account = {
id: 'test-account-id',
name: 'examples'
}
describe('local dev environment', function () {
function getHelloComponentConfig(): any {
return `
name: hello-world
secrets:
hello_ingress: hello
services:
api:
image: heroku/nodejs-hello-world
interfaces:
main:
port: 3000
environment: {}
liveness_probe:
command: curl --fail localhost:3000
interfaces:
hello:
ingress:
subdomain: \${{ secrets.hello_ingress }}
url: \${{ services.api.interfaces.main.url }}
`
}
function getDeprecatedHelloComponentConfig(): any { // TODO: 404: remove
return `
name: hello-world
parameters:
hello_ingress: hello
services:
api:
image: heroku/nodejs-hello-world
interfaces:
main:
port: 3000
environment: {}
liveness_probe:
command: curl --fail localhost:3000
interfaces:
hello:
ingress:
subdomain: \${{ parameters.hello_ingress }}
url: \${{ services.api.interfaces.main.url }}
`
}
const local_component_config_with_secrets = `
name: hello-world
secrets:
a_required_key:
required: true
another_required_key:
required: true
one_more_required_secret:
required: true
compose_escaped_variable:
required: false
api_port:
services:
api:
image: heroku/nodejs-hello-world
interfaces:
main: \${{ secrets.api_port }}
environment:
a_required_key: \${{ secrets.a_required_key }}
another_required_key: \${{ secrets.another_required_key }}
one_more_required_secret: \${{ secrets.one_more_required_secret }}
compose_escaped_variable: \${{ secrets.compose_escaped_variable }}
interfaces:
hello:
url: \${{ services.api.interfaces.main.url }}
`;
// TODO: 404: remove
const deprecated_local_component_config_with_parameters = `
name: hello-world
parameters:
a_required_key:
required: true
another_required_key:
required: true
one_more_required_secret:
required: true
compose_escaped_variable:
required: false
api_port:
services:
api:
image: heroku/nodejs-hello-world
interfaces:
main: \${{ parameters.api_port }}
environment:
a_required_key: \${{ parameters.a_required_key }}
another_required_key: \${{ parameters.another_required_key }}
one_more_required_secret: \${{ parameters.one_more_required_secret }}
compose_escaped_variable: \${{ parameters.compose_escaped_variable }}
interfaces:
hello:
url: \${{ services.api.interfaces.main.url }}
`;
const basic_secrets = {
'hello-world': {
'a_required_key': 'some_value',
'another_required_key': 'required_value',
'one_more_required_secret': 'one_more_value',
'compose_escaped_variable': 'variable_split_$_with_dollar$signs',
'api_port': 3000
},
}
const wildcard_secrets = {
'hello-world': {
'a_required_key': 'some_value',
'api_port': 3000,
'one_more_required_secret': 'one_more_value'
},
'*': {
'another_required_key': 'required_value'
}
}
const stacked_secrets = {
'hello-world': {
'a_required_key': 'some_value',
'another_required_key': 'required_value',
'one_more_required_secret': 'one_more_value',
'api_port': 3000
},
'*': {
'a_required_key': 'a_value_which_will_be_overwritten',
'another_required_key': 'another_value_which_will_be_overwritten'
}
}
const local_component_config_with_dependency = {
"name": "hello-world",
"services": {
"api": {
"image": "heroku/nodejs-hello-world",
"interfaces": {
"main": 3000
},
"environment": {
"a_required_key": "${{ secrets.a_required_key }}",
"another_required_key": "${{ secrets.another_required_key }}",
"one_more_required_secret": "${{ secrets.one_more_required_secret }}"
}
}
},
"interfaces": {
"hello": {
"url": "${{ services.api.interfaces.main.url }}"
}
},
"secrets": {
'a_required_key': {
'required': true
},
'another_required_key': {
'required': true
},
'one_more_required_secret': {
'required': true
}
},
"dependencies": {
"react-app": "latest"
}
}
const local_component_config_dependency = {
'config': {
'name': 'react-app',
'interfaces': {
'app': '\${{ services.app.interfaces.main.url }}'
},
'secrets': {
'world_text': {
'default': 'world'
}
},
'services': {
'app': {
'build': {
'context': './frontend'
},
'interfaces': {
'main': 8080
},
'environment': {
'PORT': '\${{ services.app.interfaces.main.port }}',
'WORLD_TEXT': '\${{ secrets.world_text }}'
}
}
}
},
'tag': 'latest'
}
const component_and_dependency_secrets = {
'hello-world': {
'a_required_key': 'some_value',
'another_required_key': 'required_value',
'one_more_required_secret': 'one_more_value'
},
'*': {
'a_required_key': 'a_value_which_will_be_overwritten',
'another_required_key': 'another_value_which_will_be_overwritten',
'world_text': 'some other name',
'unused_secret': 'value_not_used_by_any_component'
}
}
const local_database_seeding_component_config = {
"name": "database-seeding",
"secrets": {
"AUTO_DDL": {
"default": "none"
},
"DB_USER": {
"default": "postgres"
},
"DB_PASS": {
"default": "architect"
},
"DB_NAME": {
"default": "seeding_demo"
}
},
"services": {
"app": {
"build": {
"context": "./",
"dockerfile": "Dockerfile",
"target": "production",
},
"interfaces": {
"main": 3000,
},
"depends_on": ["my-demo-db"],
"environment": {
"DATABASE_HOST": "${{ services.my-demo-db.interfaces.postgres.host }}",
"DATABASE_PORT": "${{ services.my-demo-db.interfaces.postgres.port }}",
"DATABASE_USER": "${{ services.my-demo-db.environment.POSTGRES_USER }}",
"DATABASE_PASSWORD": "${{ services.my-demo-db.environment.POSTGRES_PASSWORD }}",
"DATABASE_SCHEMA": "${{ services.my-demo-db.environment.POSTGRES_DB }}",
"AUTO_DDL": "${{ secrets.AUTO_DDL }}"
}
},
"my-demo-db": {
"image": "postgres:11",
"interfaces": {
"postgres": 5432,
},
"environment": {
"POSTGRES_DB": "${{ secrets.DB_NAME }}",
"POSTGRES_USER": "${{ secrets.DB_USER }}",
"POSTGRES_PASSWORD": "${{ secrets.DB_PASS }}"
}
}
},
"interfaces": {
"main": {
"url": "${{ services.app.interfaces.main.url }}"
}
}
};
const deprecated_local_database_seeding_component_config = { // TODO: 404: remove
"name": "database-seeding",
"parameters": {
"AUTO_DDL": {
"default": "none"
},
"DB_USER": {
"default": "postgres"
},
"DB_PASS": {
"default": "architect"
},
"DB_NAME": {
"default": "seeding_demo"
}
},
"services": {
"app": {
"build": {
"context": "./",
"dockerfile": "Dockerfile",
"target": "production",
},
"interfaces": {
"main": 3000,
},
"depends_on": ["my-demo-db"],
"environment": {
"DATABASE_HOST": "${{ services.my-demo-db.interfaces.postgres.host }}",
"DATABASE_PORT": "${{ services.my-demo-db.interfaces.postgres.port }}",
"DATABASE_USER": "${{ services.my-demo-db.environment.POSTGRES_USER }}",
"DATABASE_PASSWORD": "${{ services.my-demo-db.environment.POSTGRES_PASSWORD }}",
"DATABASE_SCHEMA": "${{ services.my-demo-db.environment.POSTGRES_DB }}",
"AUTO_DDL": "${{ parameters.AUTO_DDL }}"
}
},
"my-demo-db": {
"image": "postgres:11",
"interfaces": {
"postgres": 5432,
},
"environment": {
"POSTGRES_DB": "${{ parameters.DB_NAME }}",
"POSTGRES_USER": "${{ parameters.DB_USER }}",
"POSTGRES_PASSWORD": "${{ parameters.DB_PASS }}"
}
}
},
"interfaces": {
"main": {
"url": "${{ services.app.interfaces.main.url }}"
}
}
};
const seed_app_resource_ref = 'database-seeding.services.app'
const seed_app_ref = resourceRefToNodeRef(seed_app_resource_ref);
const seed_db_ref = resourceRefToNodeRef('database-seeding.services.my-demo-db');
const seeding_component_expected_compose: DockerComposeTemplate = {
"version": "3",
"services": {
[seed_app_ref]: {
"ports": [
"50000:3000"
],
"depends_on": [
seed_db_ref
],
"environment": {
"DATABASE_HOST": seed_db_ref,
"DATABASE_PORT": "5432",
"DATABASE_USER": "postgres",
"DATABASE_PASSWORD": "architect",
"DATABASE_SCHEMA": "test-db",
"AUTO_DDL": "seed"
},
"labels": [
`architect.ref=${seed_app_resource_ref}`,
"traefik.enable=true",
"traefik.port=80",
`traefik.http.routers.${seed_app_ref}-main.rule=Host(\`app.arc.localhost\`)`,
`traefik.http.routers.${seed_app_ref}-main.service=${seed_app_ref}-main-service`,
`traefik.http.services.${seed_app_ref}-main-service.loadbalancer.server.port=3000`,
],
"build": {
"context": path.resolve('./examples/database-seeding'),
"dockerfile": "Dockerfile",
"target": "production",
},
"external_links": [
"gateway:app.arc.localhost"
]
},
[seed_db_ref]: {
"ports": [
"50001:5432"
],
"environment": {
"POSTGRES_DB": "test-db",
"POSTGRES_USER": "postgres",
"POSTGRES_PASSWORD": "architect"
},
"image": "postgres:11",
"external_links": [
"gateway:app.arc.localhost"
],
labels: ['architect.ref=database-seeding.services.my-demo-db']
},
"gateway": {
"image": "traefik:v2.6.2",
"command": [
"--api.insecure=true",
"--pilot.dashboard=false",
"--accesslog=true",
"--accesslog.filters.statusCodes=400-599",
"--entryPoints.web.address=:80",
"--providers.docker=true",
"--providers.docker.exposedByDefault=false",
"--providers.docker.constraints=Label(`traefik.port`,`80`)"
],
"ports": [
"80:80",
"8080:8080"
],
"volumes": [
"/var/run/docker.sock:/var/run/docker.sock:ro"
]
}
},
"volumes": {}
}
const resource_ref = 'hello-world.services.api'
const hello_api_ref = resourceRefToNodeRef(resource_ref);
const component_expected_compose: DockerComposeTemplate = {
"version": "3",
"services": {
[hello_api_ref]: {
"ports": [
"50000:3000",
],
"environment": {},
"labels": [
`architect.ref=${resource_ref}`,
"traefik.enable=true",
"traefik.port=80",
`traefik.http.routers.${hello_api_ref}-hello.rule=Host(\`hello.arc.localhost\`)`,
`traefik.http.routers.${hello_api_ref}-hello.service=${hello_api_ref}-hello-service`,
`traefik.http.services.${hello_api_ref}-hello-service.loadbalancer.server.port=3000`,
],
"external_links": [
"gateway:hello.arc.localhost"
],
"image": "heroku/nodejs-hello-world",
"healthcheck": {
"test": [
"CMD", "curl", "--fail", "localhost:3000"
],
"interval": "30s",
"timeout": "5s",
"retries": 3,
"start_period": "0s"
},
},
"gateway": {
"image": "traefik:v2.6.2",
"command": [
"--api.insecure=true",
"--pilot.dashboard=false",
"--accesslog=true",
"--accesslog.filters.statusCodes=400-599",
"--entryPoints.web.address=:80",
"--providers.docker=true",
"--providers.docker.exposedByDefault=false",
"--providers.docker.constraints=Label(`traefik.port`,`80`)"
],
"ports": [
"80:80",
"8080:8080"
],
"volumes": [
"/var/run/docker.sock:/var/run/docker.sock:ro"
]
}
},
"volumes": {}
}
test
.timeout(20000)
.stub(ComponentBuilder, 'loadFile', () => {
return getHelloComponentConfig();
})
.stub(Docker, 'verify', sinon.stub().returns(Promise.resolve()))
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.stdout({ print })
.stderr({ print })
.command(['dev', './examples/hello-world/architect.yml', '-i', 'hello'])
.it('Create a local dev with a component and an interface', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
expect(runCompose.calledOnce).to.be.true
expect(runCompose.firstCall.args[0]).to.deep.equal(component_expected_compose)
})
test // TODO: 404: remove
.timeout(20000)
.stub(ComponentBuilder, 'loadFile', () => {
return getDeprecatedHelloComponentConfig();
})
.stub(Docker, 'verify', sinon.stub().returns(Promise.resolve()))
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.stdout({ print })
.stderr({ print })
.command(['dev', './examples/hello-world/architect.yml', '-i', 'hello'])
.it('Create a local dev with a component and an interface (deprecated parameters block)', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
expect(runCompose.calledOnce).to.be.true
expect(runCompose.firstCall.args[0]).to.deep.equal(component_expected_compose)
})
test
.timeout(20000)
.stub(ComponentBuilder, 'loadFile', () => {
const hello_json = yaml.load(getHelloComponentConfig()) as any;
hello_json.services.api.interfaces.main.sticky = true;
return yaml.dump(hello_json);
})
.stub(Docker, 'verify', sinon.stub().returns(Promise.resolve()))
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.stdout({ print })
.stderr({ print })
.command(['dev', './examples/hello-world/architect.yml', '-i', 'hello'])
.it('Sticky label added for sticky interfaces', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
expect(runCompose.calledOnce).to.be.true;
const hello_api_ref = resourceRefToNodeRef('hello-world.services.api');
expect(runCompose.firstCall.args[0].services[hello_api_ref].labels).to.contain(`traefik.http.services.${hello_api_ref}-hello-service.loadBalancer.sticky.cookie=true`);
})
test
.timeout(20000)
.stub(ComponentBuilder, 'buildSpecFromPath', () => {
const spec = buildSpecFromYml(yaml.dump(local_database_seeding_component_config));
spec.metadata.file = { path: './examples/database-seeding/architect.yml', contents: '' }
return spec;
})
.stub(Docker, 'verify', sinon.stub().returns(Promise.resolve()))
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.stdout({ print })
.stderr({ print })
.command(['dev', './examples/database-seeding/architect.yml', '-s', 'AUTO_DDL=seed', '--secret', 'DB_NAME=test-db', '-i', 'app:main'])
.it('Create a local dev with a component, secrets, and an interface', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
expect(runCompose.calledOnce).to.be.true;
expect(runCompose.firstCall.args[0]).to.deep.equal(seeding_component_expected_compose);
})
test // TODO: 404: remove
.timeout(20000)
.stub(ComponentBuilder, 'buildSpecFromPath', () => {
const spec = buildSpecFromYml(yaml.dump(deprecated_local_database_seeding_component_config));
spec.metadata.file = { path: './examples/database-seeding/architect.yml', contents: '' }
return spec;
})
.stub(Docker, 'verify', sinon.stub().returns(Promise.resolve()))
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.stdout({ print })
.stderr({ print })
.command(['dev', './examples/database-seeding/architect.yml', '--parameter', 'AUTO_DDL=seed', '--parameter', 'DB_NAME=test-db', '-i', 'app:main'])
.it('Create a local dev with a component, deprecated parameters, and an interface', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
expect(runCompose.calledOnce).to.be.true;
expect(runCompose.firstCall.args[0]).to.deep.equal(seeding_component_expected_compose);
})
test
.timeout(20000)
.stub(ComponentBuilder, 'buildSpecFromPath', () => {
return buildSpecFromYml(local_component_config_with_secrets)
})
.stub(DeployUtils, 'readSecretsFile', () => {
return basic_secrets;
})
.stub(Docker, 'verify', sinon.stub().returns(Promise.resolve()))
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.stdout({ print })
.stderr({ print })
.command(['dev', './examples/hello-world/architect.yml', '-i', 'test:hello', '--secret-file', './examples/hello-world/secrets.yml'])
.it('Create a local dev with a basic component and a basic secrets file', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
expect(runCompose.calledOnce).to.be.true;
const hello_world_service = runCompose.firstCall.args[0].services[hello_api_ref] as any;
expect(hello_world_service.external_links).to.contain('gateway:test.arc.localhost');
expect(hello_world_service.environment.a_required_key).to.equal('some_value');
expect(hello_world_service.environment.another_required_key).to.equal('required_value');
expect(hello_world_service.environment.one_more_required_secret).to.equal('one_more_value');
})
test // TODO: 404: remove
.timeout(20000)
.stub(ComponentBuilder, 'buildSpecFromPath', () => {
return buildSpecFromYml(deprecated_local_component_config_with_parameters)
})
.stub(DeployUtils, 'readSecretsFile', () => {
return basic_secrets;
})
.stub(Docker, 'verify', sinon.stub().returns(Promise.resolve()))
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.stdout({ print })
.stderr({ print })
.command(['dev', './examples/hello-world/architect.yml', '-i', 'test:hello', '--secret-file', './examples/hello-world/secrets.yml'])
.it('Create a local dev with a basic component using deprecated parameters and a basic secrets file', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
expect(runCompose.calledOnce).to.be.true;
const hello_world_service = runCompose.firstCall.args[0].services[hello_api_ref] as any;
expect(hello_world_service.external_links).to.contain('gateway:test.arc.localhost');
expect(hello_world_service.environment.a_required_key).to.equal('some_value');
expect(hello_world_service.environment.another_required_key).to.equal('required_value');
expect(hello_world_service.environment.one_more_required_secret).to.equal('one_more_value');
})
// This test will be removed when the deprecated 'values' flag is removed
test
.timeout(20000)
.stub(ComponentBuilder, 'buildSpecFromPath', () => {
return buildSpecFromYml(local_component_config_with_secrets)
})
.stub(DeployUtils, 'readSecretsFile', () => {
return basic_secrets;
})
.stub(Docker, 'verify', sinon.stub().returns(Promise.resolve()))
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.stdout({ print })
.stderr({ print })
.command(['dev', './examples/hello-world/architect.yml', '-i', 'test:hello', '-v', './examples/hello-world/secrets.yml'])
.it('Create a local dev with a basic component and a basic secrets file using deprecated values flag', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
expect(runCompose.calledOnce).to.be.true;
const hello_world_service = runCompose.firstCall.args[0].services[hello_api_ref] as any;
expect(hello_world_service.external_links).to.contain('gateway:test.arc.localhost');
expect(hello_world_service.environment.a_required_key).to.equal('some_value');
expect(hello_world_service.environment.another_required_key).to.equal('required_value');
expect(hello_world_service.environment.one_more_required_secret).to.equal('one_more_value');
})
test
.timeout(20000)
.stub(ComponentBuilder, 'buildSpecFromPath', () => {
return buildSpecFromYml(local_component_config_with_secrets)
})
.stub(DeployUtils, 'readSecretsFile', () => {
return wildcard_secrets;
})
.stub(Docker, 'verify', sinon.stub().returns(Promise.resolve()))
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.stdout({ print })
.stderr({ print })
.command(['dev', './examples/hello-world/architect.yml', '-i', 'test:hello', '--secret-file', './examples/hello-world/secrets.yml'])
.it('Create a local dev with a basic component and a wildcard secrets file', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
const hello_world_environment = (runCompose.firstCall.args[0].services[hello_api_ref] as any).environment;
expect(hello_world_environment.a_required_key).to.equal('some_value');
expect(hello_world_environment.another_required_key).to.equal('required_value');
expect(hello_world_environment.one_more_required_secret).to.equal('one_more_value');
})
test
.timeout(20000)
.stub(ComponentBuilder, 'buildSpecFromPath', () => {
return buildSpecFromYml(local_component_config_with_secrets)
})
.stub(Docker, 'verify', sinon.stub().returns(Promise.resolve()))
.stub(DeployUtils, 'readSecretsFile', () => {
return stacked_secrets;
})
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.stdout({ print })
.stderr({ print })
.command(['dev', './examples/hello-world/architect.yml', '-i', 'test:hello', '--secret-file', './examples/hello-world/secrets.yml'])
.it('Create a local dev with a basic component and a stacked secrets file', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
const hello_world_environment = (runCompose.firstCall.args[0].services[hello_api_ref] as any).environment;
expect(hello_world_environment.a_required_key).to.equal('some_value');
expect(hello_world_environment.another_required_key).to.equal('required_value');
expect(hello_world_environment.one_more_required_secret).to.equal('one_more_value');
})
test
.timeout(20000)
.stub(ComponentBuilder, 'buildSpecFromPath', () => {
return buildSpecFromYml(yaml.dump(local_component_config_with_dependency))
})
.stub(Docker, 'verify', sinon.stub().returns(Promise.resolve()))
.stub(DeployUtils, 'readSecretsFile', () => {
return component_and_dependency_secrets;
})
.nock(MOCK_API_HOST, api => api
.get(`/accounts/${account.name}`)
.reply(200, account))
.nock(MOCK_API_HOST, api => api
.get(`/accounts/${account.name}/components/react-app/versions/latest`)
.reply(200, local_component_config_dependency))
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.stdout({ print })
.stderr({ print })
.command(['dev', './examples/hello-world/architect.yml', '-i', 'test:hello', '--secret-file', './examples/hello-world/secrets.yml', '-a', 'examples'])
.it('Create a local dev with a basic component, a dependency, and a values file', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
const hello_world_environment = (runCompose.firstCall.args[0].services[hello_api_ref] as any).environment;
expect(hello_world_environment.a_required_key).to.equal('some_value');
expect(hello_world_environment.another_required_key).to.equal('required_value');
expect(hello_world_environment.one_more_required_secret).to.equal('one_more_value');
})
test
.timeout(20000)
.stub(ComponentBuilder, 'buildSpecFromPath', () => {
return buildSpecFromYml(yaml.dump(local_component_config_with_dependency))
})
.stub(Docker, 'verify', sinon.stub().returns(Promise.resolve()))
.stub(DeployUtils, 'readSecretsFile', () => {
return component_and_dependency_secrets;
})
.nock(MOCK_API_HOST, api => api
.get(`/accounts/${account.name}`)
.reply(200, account))
.nock(MOCK_API_HOST, api => api
.get(`/accounts/examples/components/react-app/versions/latest`)
.reply(200, local_component_config_dependency))
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.stdout({ print })
.stderr({ print })
.command(['dev', './examples/hello-world/architect.yml', '-i', 'test:hello', '--secret-file', './examples/hello-world/secrets.yml', '-r', '-a', 'examples'])
.it('Create a local recursive dev with a basic component, a dependency, and a secrets file', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
const hello_world_environment = (runCompose.firstCall.args[0].services[hello_api_ref] as any).environment;
const react_app_ref = resourceRefToNodeRef('react-app.services.app');
const react_app_environment = (runCompose.firstCall.args[0].services[react_app_ref] as any).environment;
expect(hello_world_environment.a_required_key).to.equal('some_value');
expect(hello_world_environment.another_required_key).to.equal('required_value');
expect(hello_world_environment.one_more_required_secret).to.equal('one_more_value');
expect(react_app_environment.WORLD_TEXT).to.equal('some other name');
})
test
.timeout(20000)
.stub(ComponentBuilder, 'buildSpecFromPath', () => {
return buildSpecFromYml(local_component_config_with_secrets)
})
.stub(DeployUtils, 'readSecretsFile', () => {
return basic_secrets;
})
.stub(Docker, 'verify', sinon.stub().returns(Promise.resolve()))
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.stdout({ print })
.stderr({ print })
.command(['dev', './examples/hello-world/architect.yml', '--secret-file', './examples/hello-world/secrets.yml'])
.it('Dollar signs are escaped for environment variables in local compose devments', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
expect(runCompose.calledOnce).to.be.true;
const hello_world_service = runCompose.firstCall.args[0].services[hello_api_ref] as any;
expect(hello_world_service.environment.compose_escaped_variable).to.equal('variable_split_$$_with_dollar$$signs');
})
describe('linked dev', function () {
test
.timeout(20000)
.stub(ComponentBuilder, 'buildSpecFromPath', () => {
return buildSpecFromYml(getHelloComponentConfig())
})
.stub(Docker, 'verify', sinon.stub().returns(Promise.resolve()))
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.stub(AppService.prototype, 'loadLinkedComponents', sinon.stub().returns({ 'hello-world': './examples/hello-world/architect.yml' }))
.stdout({ print })
.stderr({ print })
.command(['dev', 'hello-world:latest', '-i', 'hello'])
.it('Create a local dev with a component and an interface', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
expect(runCompose.calledOnce).to.be.true
expect(runCompose.firstCall.args[0]).to.deep.equal(component_expected_compose)
})
});
describe('instance devs', function () {
const hello_api_instance_ref = resourceRefToNodeRef('hello-world.services.api@tenant-1');
const expected_instance_compose = JSON.parse(JSON.stringify(component_expected_compose).replace(new RegExp(hello_api_ref, 'g'), hello_api_instance_ref).replace(new RegExp('hello-world.services.api', 'g'), 'hello-world.services.api@tenant-1'));
const local_dev = test
.timeout(20000)
.stub(ComponentBuilder, 'buildSpecFromPath', () => {
return buildSpecFromYml(getHelloComponentConfig())
})
.stub(Docker, 'verify', sinon.stub().returns(Promise.resolve()))
.stub(AppService.prototype, 'loadLinkedComponents', sinon.stub().returns({ 'hello-world': './examples/hello-world/architect.yml' }))
.stdout({ print })
.stderr({ print })
local_dev
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.command(['dev', 'hello-world@tenant-1', '-i', 'hello'])
.it('Create a local dev with instance id and no tag', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
expect(runCompose.calledOnce).to.be.true
expect(runCompose.firstCall.args[0]).to.deep.equal(expected_instance_compose)
})
local_dev
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.command(['dev', 'hello-world:latest@tenant-1', '-i', 'hello'])
.it('Create a local dev with instance name and tag', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
expect(runCompose.calledOnce).to.be.true
expect(runCompose.firstCall.args[0]).to.deep.equal(expected_instance_compose)
})
local_dev
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.stub(ComponentBuilder, 'buildSpecFromPath', () => {
const hello_json = yaml.load(getHelloComponentConfig()) as any;
hello_json.services.api.environment.SELF_URL = `\${{ ingresses['hello'].url }}`
return buildSpecFromYml(yaml.dump(hello_json));
})
.stub(DeployUtils, 'readSecretsFile', () => {
return {
'hello-world@tenant-1': {
'hello_ingress': 'hello-1'
},
'hello-world@tenant-2': {
'hello_ingress': 'hello-2'
}
};
})
.command(['dev', '--secret-file', './examples/hello-world/secrets.yml', 'hello-world@tenant-1', 'hello-world@tenant-2'])
.it('Create a local dev with multiple instances of the same component', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
expect(runCompose.calledOnce).to.be.true;
const tenant_1_ref = resourceRefToNodeRef('hello-world.services.api@tenant-1');
const tenant_2_ref = resourceRefToNodeRef('hello-world.services.api@tenant-2');
const compose = runCompose.firstCall.args[0];
expect(Object.keys(compose.services)).includes(tenant_1_ref, tenant_2_ref)
expect(compose.services[tenant_1_ref].labels || []).includes(`traefik.http.routers.${tenant_1_ref}-hello.rule=Host(\`hello-1.arc.localhost\`)`)
expect(compose.services[tenant_2_ref].labels || []).includes(`traefik.http.routers.${tenant_2_ref}-hello.rule=Host(\`hello-2.arc.localhost\`)`)
})
});
describe('ingresses devs', function () {
test
.timeout(20000)
// @ts-ignore
.stub(ComponentBuilder, 'buildSpecFromPath', (path: string) => {
let config: string;
if (path === './examples/react-app/architect.yml') {
config = `
name: auth
services:
auth:
interfaces:
main: 8080
environment:
SELF_URL: \${{ ingresses.auth.url }} # is not auto-exposed
OLD_SELF_URL: \${{ environment.ingresses['auth'].auth.url }} # is not auto-exposed
interfaces:
auth: \${{ services.auth.interfaces.main.url }}
`
} else {
config = `
name: app
dependencies:
auth: latest
services:
app:
interfaces:
main: 8080
environment:
SELF_URL: \${{ ingresses.app.url }} # successfully auto-exposed as an ingress
OLD_SELF_URL: \${{ environment.ingresses['app'].app.url }} # successfully auto-exposed as an ingress
DEPENDENCY_URL: \${{ dependencies['auth'].ingresses.auth.url }} # is not auto-exposed
interfaces:
app: \${{ services.app.interfaces.main.url }}
`
}
return buildSpecFromYml(config)
})
.stub(Docker, 'verify', sinon.stub().returns(Promise.resolve()))
.stub(AppService.prototype, 'loadLinkedComponents', sinon.stub().returns({
'app': './examples/hello-world/architect.yml',
'auth': './examples/react-app/architect.yml'
}))
.stdout({ print })
.stderr({ print })
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.command(['dev', 'app'])
.it('Dev component with dependency with ingresses', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
expect(runCompose.calledOnce).to.be.true
const compose = runCompose.firstCall.args[0];
const app_ref = resourceRefToNodeRef('app.services.app');
expect(compose.services[app_ref].labels).includes('traefik.enable=true');
const auth_ref = resourceRefToNodeRef('auth.services.auth');
expect(compose.services[auth_ref].labels).includes('traefik.enable=true');
})
});
test
.timeout(20000)
.stub(ComponentBuilder, 'loadFile', () => {
return getHelloComponentConfig();
})
.stub(Docker, 'verify', sinon.stub().returns(Promise.resolve()))
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.stdout({ print })
.stderr({ print })
.command(['dev', './examples/hello-world/architect.yml', '-i', 'hello'])
.it('Command with an operator is converted correctly to the docker compose file', ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
const compose = runCompose.firstCall.args[0];
expect(runCompose.calledOnce).to.be.true;
expect(compose.services['hello-world--api'].healthcheck).to.deep.equal({
"test": [
"CMD", "curl", "--fail", "localhost:3000"
],
"interval": "30s",
"timeout": "5s",
"retries": 3,
"start_period": "0s"
});
});
test
.timeout(20000)
.stub(ComponentBuilder, 'loadFile', () => {
const config = yaml.load(getHelloComponentConfig()) as ComponentConfig;
delete config.services.api.image;
config.services.api.build = {
context: 'non_existent_path',
};
return yaml.dump(config);
})
.stub(Docker, 'verify', sinon.stub().returns(Promise.resolve()))
.stub(Dev.prototype, 'runCompose', sinon.stub().returns(undefined))
.stdout({ print })
.stderr({ print })
.command(['dev', './examples/hello-world/architect.yml'])
.catch(err => {
expect(err.message).to.include('non_existent_path');
expect(err.message).to.include('used for the build context of service api does not exist.');
})
.it(`Throws error if a path is given that doesn't exist`, ctx => {
const runCompose = Dev.prototype.runCompose as sinon.SinonStub;
expect(runCompose.calledOnce).to.be.false;
});
});