forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent.test.ts
More file actions
1516 lines (1288 loc) · 60.9 KB
/
component.test.ts
File metadata and controls
1516 lines (1288 loc) · 60.9 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
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
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
'use strict';
import * as path from 'path';
import * as vscode from 'vscode';
import * as chai from 'chai';
import * as sinonChai from 'sinon-chai';
import * as sinon from 'sinon';
import { TestItem } from './testOSItem';
import { OdoImpl, Command, ContextType } from '../../../src/odo';
import { Progress } from '../../../src/util/progress';
import * as Util from '../../../src/util/async';
import { Refs } from '../../../src/util/refs';
import { OpenShiftItem } from '../../../src/openshift/openshiftItem';
import pq = require('proxyquire');
import globby = require('globby');
const expect = chai.expect;
chai.use(sinonChai);
suite('OpenShift/Component', () => {
let quickPickStub: sinon.SinonStub;
let sandbox: sinon.SinonSandbox;
let termStub: sinon.SinonStub, execStub: sinon.SinonStub;
let getComponentsStub: sinon.SinonStub;
const fixtureFolder = path.join(__dirname, '..', '..', '..', 'test', 'fixtures').normalize();
const comp1Uri = vscode.Uri.file(path.join(fixtureFolder, 'components', 'comp1'));
const comp2Uri = vscode.Uri.file(path.join(fixtureFolder, 'components', 'comp2'));
const wsFolder1 = { uri: comp1Uri, index: 0, name: 'comp1' };
const wsFolder2 = { uri: comp2Uri, index: 1, name: 'comp2' };
const clusterItem = new TestItem(null, 'cluster', ContextType.CLUSTER);
const projectItem = new TestItem(clusterItem, 'myproject', ContextType.PROJECT);
const appItem = new TestItem(projectItem, 'app1', ContextType.APPLICATION);
const componentItem = new TestItem(appItem, 'comp1', ContextType.COMPONENT_PUSHED, [], false, comp1Uri);
const serviceItem = new TestItem(appItem, 'service', ContextType.SERVICE);
const errorMessage = 'FATAL ERROR';
let getProjects: sinon.SinonStub;
let getApps: sinon.SinonStub;
let Component: any;
let infoStub: sinon.SinonStub;
let fetchTag: sinon.SinonStub;
let commandStub: sinon.SinonStub;
setup(() => {
sandbox = sinon.createSandbox();
sandbox.stub(vscode.workspace, "updateWorkspaceFolders");
fetchTag = sandbox.stub(Refs, 'fetchTag').resolves (new Map<string, string>([['HEAD', 'shanumb']]));
Component = pq('../../../src/openshift/component', {}).Component;
termStub = sandbox.stub(OdoImpl.prototype, 'executeInTerminal');
execStub = sandbox.stub(OdoImpl.prototype, 'execute').resolves({ stdout: "" });
sandbox.stub(OdoImpl.prototype, 'getServices');
sandbox.stub(OdoImpl.prototype, 'getProjects').resolves([]);
sandbox.stub(OdoImpl.prototype, 'getApplications').resolves([]);
getComponentsStub = sandbox.stub(OdoImpl.prototype, 'getComponents').resolves([]);
sandbox.stub(Util, 'wait').resolves();
getProjects = sandbox.stub(OpenShiftItem, 'getProjectNames').resolves([projectItem]);
getApps = sandbox.stub(OpenShiftItem, 'getApplicationNames').resolves([appItem]);
sandbox.stub(OpenShiftItem, 'getComponentNames').resolves([componentItem]);
sandbox.stub(OpenShiftItem, 'getServiceNames').resolves([serviceItem]);
sandbox.stub(OdoImpl.prototype, 'convertObjectsFromPreviousOdoReleases');
commandStub = sandbox.stub(vscode.commands, 'executeCommand');
});
teardown(() => {
sandbox.restore();
});
suite('create component with no context', () => {
setup(() => {
quickPickStub = sandbox.stub(vscode.window, 'showQuickPick');
quickPickStub.onFirstCall().resolves(projectItem);
quickPickStub.onSecondCall().resolves(undefined);
});
test('asks for context and exits if not provided', async () => {
const result = await Component.create(null);
expect(result).null;
expect(getProjects).calledOnce;
expect(getApps).calledOnce;
});
});
suite('create', () => {
const componentType = 'nodejs';
const version = 'latest';
const ref = 'master';
const folder = { uri: { fsPath: 'folder' } };
let inputStub: sinon.SinonStub,
progressFunctionStub: sinon.SinonStub;
setup(() => {
quickPickStub = sandbox.stub(vscode.window, 'showQuickPick');
quickPickStub.onFirstCall().resolves({
label: 'Workspace Directory',
description: 'Use workspace directory as a source for the Component'
});
quickPickStub.onSecondCall().resolves({label: 'file:///c:/Temp', folder: vscode.Uri.parse('file:///c:/Temp')});
quickPickStub.onThirdCall().resolves(componentType);
quickPickStub.onCall(3).resolves(version);
inputStub = sandbox.stub(vscode.window, 'showInputBox');
sandbox.stub(Progress, 'execWithProgress').resolves();
sandbox.stub(Progress, 'execCmdWithProgress').resolves();
progressFunctionStub = sandbox.stub(Progress, 'execFunctionWithProgress').yields();
sandbox.stub(vscode.workspace, 'workspaceFolders').value([wsFolder1, wsFolder2]);
});
test('returns null when cancelled', async () => {
quickPickStub.onFirstCall().resolves(undefined);
const result = await Component.create(appItem);
expect(result).null;
});
test('errors when a subcommand fails', async () => {
quickPickStub.onSecondCall().rejects(errorMessage);
let expectedError: Error;
try {
await Component.create(appItem);
} catch (error) {
expectedError = error;
}
expect(expectedError).equals(`Failed to create Component with error '${errorMessage}'`);
});
suite('from local workspace', () => {
setup(() => {
inputStub.resolves(componentItem.getName());
quickPickStub.onSecondCall().resolves({label: folder.uri.fsPath, uri: folder.uri});
});
test('happy path works', async () => {
const result = await Component.create(appItem);
expect(result).equals(`Component '${componentItem.getName()}' successfully created. To deploy it on cluster, perform 'Push' action.`);
expect(progressFunctionStub).calledOnceWith(
`Creating new Component '${componentItem.getName()}'`);
expect(execStub).calledWith(Command.createLocalComponent(appItem.getParent().getName(), appItem.getName(), componentType, version, componentItem.getName(), folder.uri.fsPath));
});
test('returns null when no option is selected from quick pick', async () => {
quickPickStub.onFirstCall().resolves(undefined);
const result = await Component.createFromLocal(null);
expect(result).null;
});
test('returns null when no folder selected', async () => {
quickPickStub.onSecondCall().resolves(undefined);
const result = await Component.create(appItem);
expect(result).null;
});
test('returns null when no component name selected', async () => {
inputStub.resolves();
const result = await Component.create(appItem);
expect(result).null;
});
test('returns null when no component type selected', async () => {
quickPickStub.onThirdCall().resolves(undefined);
const result = await Component.create(appItem);
expect(result).null;
});
test('returns null when no component type version selected', async () => {
quickPickStub.onCall(3).resolves(undefined);
const result = await Component.create(appItem);
expect(result).null;
});
});
suite('from git repository', () => {
const uri = 'git uri';
setup(() => {
sandbox.stub(OdoImpl.prototype, 'getComponentTypes').resolves(['nodejs']);
sandbox.stub(OdoImpl.prototype, 'getComponentTypeVersions').resolves(['latest']);
quickPickStub.onFirstCall().resolves({ label: 'Git Repository' });
quickPickStub.onSecondCall().resolves({
description: "Folder which does not have an OpenShift context",
label: "$(plus) Add new context folder."
});
inputStub.onFirstCall().resolves(uri);
quickPickStub.onThirdCall().resolves('master');
quickPickStub.onCall(3).resolves(componentType);
quickPickStub.onCall(4).resolves(version);
inputStub.onSecondCall().resolves(componentItem.getName());
infoStub = sandbox.stub(vscode.window, 'showInformationMessage').resolves();
sandbox.stub(vscode.window, 'showOpenDialog').resolves([vscode.Uri.parse('file:///c%3A/Temp')]);
});
test('returns null when no option is selected from quick pick', async () => {
quickPickStub.onFirstCall().resolves(undefined);
const result = await Component.createFromGit(null);
expect(result).null;
});
test('returns null when no folder selected', async () => {
quickPickStub.onFirstCall().resolves(undefined);
const result = await Component.createFromGit(appItem);
expect(result).null;
});
test('happy path works', async () => {
const result = await Component.create(appItem);
expect(result).equals(`Component '${componentItem.getName()}' successfully created. To deploy it on cluster, perform 'Push' action.`);
expect(execStub).calledWith(Command.createGitComponent(projectItem.getName(), appItem.getName(), componentType, version, componentItem.getName(), uri, ref));
});
test('returns null when no git repo selected', async () => {
inputStub.onFirstCall().resolves();
const result = await Component.create(appItem);
expect(result).null;
});
test('returns null when no component name selected', async () => {
inputStub.onSecondCall().resolves();
const result = await Component.create(appItem);
expect(result).null;
});
test('returns null when no git reference selected', async () => {
quickPickStub.onThirdCall().resolves();
const result = await Component.create(appItem);
expect(result).null;
});
test('returns null when no component type selected', async () => {
quickPickStub.onCall(3).resolves();
const result = await Component.create(appItem);
expect(result).null;
});
test('returns null when no component type version selected', async () => {
quickPickStub.onCall(4).resolves();
const result = await Component.create(appItem);
expect(result).null;
});
test('clones the git repo if selected', async () => {
infoStub.resolves('Yes');
await Component.create(appItem);
expect(commandStub).calledOnceWith('git.clone', uri);
});
test('allows to continue with valid git repository url', async () => {
let result: string | Thenable<string>;
inputStub.onFirstCall().callsFake(async (options?: vscode.InputBoxOptions, token?: vscode.CancellationToken): Promise<string> => {
result = await options.validateInput('https://github.com/redhat-developer/vscode-openshift-tools');
return Promise.resolve('https://github.com/redhat-developer/vscode-openshift-tools');
});
await Component.create(appItem);
expect(result).to.be.undefined;
});
test('shows error message when repo does not exist', async () => {
fetchTag.resolves (new Map<string, string>());
let result: string | Thenable<string>;
inputStub.onFirstCall().callsFake(async (options?: vscode.InputBoxOptions, token?: vscode.CancellationToken): Promise<string> => {
result = await options.validateInput('https://github.com');
return Promise.resolve('https://github.com');
});
await Component.create(appItem);
expect(result).equals('There is no git repository at provided URL.');
});
test('shows error message when invalid URL provided', async () => {
let result: string | Thenable<string>;
inputStub.onFirstCall().callsFake(async (options?: vscode.InputBoxOptions, token?: vscode.CancellationToken): Promise<string> => {
result = await options.validateInput('github');
return Promise.resolve('github');
});
await Component.create(appItem);
expect(result).equals('Invalid URL provided');
});
test('shows error message for empty git repository url', async () => {
let result: string | Thenable<string>;
inputStub.onFirstCall().callsFake(async (options?: vscode.InputBoxOptions, token?: vscode.CancellationToken): Promise<string> => {
result = await (async () => options.validateInput(''))();
return Promise.resolve('');
});
await Component.create(appItem);
expect(result).equals('Empty Git repository URL');
});
});
suite('from binary file', () => {
let fsPath: string, paths: string;
let globbyStub: sinon.SinonStub;
if (process.platform === 'win32') {
fsPath = 'c:\\Users\\Downloads';
paths = 'c:\\Users\\Downloads\\sb.jar';
} else {
fsPath = '/Users/Downloads';
paths = '/Users/Downloads';
}
const files = [{
_formatted: undefined,
_fsPath: undefined,
authority: "",
fragment: "",
fsPath: fsPath,
path: paths,
query: "",
scheme: "file"
}];
setup(() => {
quickPickStub.onFirstCall().resolves({ label: 'Binary File' });
quickPickStub.onSecondCall().resolves({
description: "Folder which does not have an OpenShift context",
label: "$(plus) Add new context folder."
});
quickPickStub.onThirdCall().resolves({
description: paths,
label: `$(file-zip) sb.jar`
});
quickPickStub.onCall(3).resolves(componentType);
quickPickStub.onCall(4).resolves(version);
sandbox.stub(vscode.window, 'showOpenDialog').resolves(files);
globbyStub = sandbox.stub(globby, 'sync').returns([paths]);
inputStub.resolves(componentItem.getName());
});
test('happy path works', async () => {
const result = await Component.create(appItem);
expect(result).equals(`Component '${componentItem.getName()}' successfully created. To deploy it on cluster, perform 'Push' action.`);
expect(execStub).calledWith(Command.createBinaryComponent(projectItem.getName(), appItem.getName(), componentType, version, componentItem.getName(), paths, files[0].fsPath));
});
test('returns null when no option is selected from quick pick', async () => {
quickPickStub.onFirstCall().resolves(undefined);
const result = await Component.createFromBinary(null);
expect(result).null;
});
test('returns information message if no binary file present in the context', async () => {
globbyStub.onFirstCall().returns([]);
const result = await Component.createFromBinary(null);
expect(result).equals('No binary file present in the context folder selected. We currently only support .jar and .war files. If you need support for any other file, please raise an issue.');
});
test('returns null when no work space folder selected', async () => {
quickPickStub.onSecondCall().resolves(undefined);
const result = await Component.create(appItem);
expect(result).null;
});
test('returns null when no binary file selected', async () => {
quickPickStub.onThirdCall().resolves(undefined);
const result = await Component.create(appItem);
expect(result).null;
});
test('returns null when no component name selected', async () => {
inputStub.resolves();
const result = await Component.create(appItem);
expect(result).null;
});
test('returns null when no component type selected', async () => {
quickPickStub.onCall(3).resolves();
const result = await Component.create(appItem);
expect(result).null;
});
test('returns null when no component type version selected', async () => {
quickPickStub.onCall(4).resolves();
const result = await Component.create(appItem);
expect(result).null;
});
});
});
suite('createFromFolder', () => {
let inputStub: sinon.SinonStub;
const pathOne: string = path.join('some', 'path');
const folder: vscode.Uri = vscode.Uri.file(pathOne);
setup(() => {
quickPickStub = sandbox.stub(vscode.window, 'showQuickPick');
quickPickStub.onFirstCall().resolves(projectItem);
quickPickStub.onSecondCall().resolves(appItem);
inputStub = sandbox.stub(vscode.window, 'showInputBox');
});
test('returns null when no option is selected from quick pick', async () => {
quickPickStub.onFirstCall().resolves(undefined);
const result = await Component.createFromFolder(null);
expect(result).null;
});
test('return null when no component type selected', async () => {
inputStub.resolves(componentItem.getName());
const result = await Component.createFromFolder(folder);
expect(result).null;
});
test('return null when no component name is provided', async () => {
inputStub.resolves();
const result = await Component.createFromFolder(folder);
expect(result).null;
});
test('return null when no component version selected', async () => {
inputStub.resolves(componentItem.getName());
quickPickStub.onThirdCall().resolves('nodejs');
const result = await Component.createFromFolder(folder);
expect(result).null;
});
test('happy path works', async () => {
inputStub.resolves(componentItem.getName());
quickPickStub.onThirdCall().resolves('nodejs');
quickPickStub.resolves('latest');
const result = await Component.createFromFolder(folder);
expect(result).equals(`Component '${componentItem.getName()}' successfully created. To deploy it on cluster, perform 'Push' action.`);
});
});
suite('unlinkComponent', () => {
let getLinkDataStub: sinon.SinonStub;
const mockData = `{
"kind": "Component",
"apiVersion": "odo.openshift.io/v1alpha1",
"metadata": {
"name": "comp2",
"creationTimestamp": null
},
"spec": {
"type": "nodejs",
"source": "file:///Users/nodejs-ex"
},
"status": {
"active": false,
"linkedServices": {
"service1": ["8080"]
},
"linkedComponents": {
"comp1": ["8080"]
}
}
}`;
setup(() => {
quickPickStub = sandbox.stub(vscode.window, 'showQuickPick');
quickPickStub.onFirstCall().resolves({
label: 'Component',
description: 'Unlink Component'
});
getLinkDataStub = sandbox.stub(Component, 'getLinkData').resolves({
kind: "Component",
apiVersion: "odo.openshift.io/v1alpha1",
metadata: {
creationTimestamp: null,
name: "comp1",
namespace: "myproject"
},
spec: {
type: "nodejs",
source: "file:///Users/nodejs-ex"
},
status: {
linkedComponents: {
comp2: ["8080"]
},
state: "Pushed"
}
});
execStub.resolves({ error: undefined, stdout: mockData, stderr: '' });
});
test('returns null when no option is selected', async () => {
quickPickStub.onFirstCall().resolves(undefined);
const result = await Component.unlink(componentItem);
expect(result).null;
});
test('returns null when no option is selected from quick pick', async () => {
quickPickStub.onSecondCall().resolves(undefined);
const result = await Component.unlink(null);
expect(result).null;
});
test('works from context menu', async () => {
quickPickStub.resolves("comp2");
const result = await Component.unlink(componentItem);
expect(result).equals(`Component 'comp2' has been successfully unlinked from the Component '${componentItem.getName()}'`);
});
test('returns null when no component selected to unlink', async () => {
quickPickStub.resolves();
const result = await Component.unlink(componentItem);
expect(result).null;
});
test('errors when a command fails', async () => {
quickPickStub.onFirstCall().resolves('comp2');
execStub.onFirstCall().rejects(errorMessage);
let savedErr: any;
try {
await Component.unlinkComponent(componentItem);
} catch (err) {
savedErr = err;
}
expect(savedErr).equals(`Failed to unlink Component with error '${errorMessage}'`);
});
test('calls the appropriate error message when no link component found', async () => {
getLinkDataStub.onFirstCall().resolves({
kind: "Component",
apiVersion: "odo.openshift.io/v1alpha1",
metadata: {
name: "comp2",
creationTimestamp: null
},
spec: {
type: "nodejs",
source: "file:///Users/nodejs-ex"
},
status: {
active: false
}
});
try {
await Component.unlink(componentItem);
} catch (err) {
expect(err.message).equals('No linked Components found');
return;
}
expect.fail();
});
test('Should able to unlink the component', async () => {
await Component.unlinkAllComponents(componentItem);
execStub.calledOnce;
});
});
suite('unlinkService', () => {
const mockData = `{
"kind": "Component",
"apiVersion": "odo.openshift.io/v1alpha1",
"metadata": {
"name": "comp2",
"creationTimestamp": null
},
"spec": {
"type": "nodejs",
"source": "file:///Users/nodejs-ex"
},
"status": {
"active": false,
"linkedServices": {
"service1": ["8080"]
},
"linkedComponents": {
"comp1": ["8080"]
}
}
}`;
setup(() => {
quickPickStub = sandbox.stub(vscode.window, 'showQuickPick');
quickPickStub.onFirstCall().resolves({
label: 'Service',
description: 'Unlink Component'
});
execStub.resolves({ error: undefined, stdout: mockData, stderr: '' });
});
test('works from context menu', async () => {
quickPickStub.resolves("service1");
const result = await Component.unlink(componentItem);
expect(result).equals(`Service 'service1' has been successfully unlinked from the Component '${componentItem.getName()}'`);
});
test('returns null when no option is selected from quick pick', async () => {
quickPickStub.onSecondCall().resolves(undefined);
const result = await Component.unlink(null);
expect(result).null;
});
test('returns null when no service selected to unlink', async () => {
quickPickStub.resolves();
const result = await Component.unlink(componentItem);
expect(result).null;
});
test('errors when a command fails', async () => {
sandbox.stub(Component, 'getLinkData').resolves({
kind: "Component",
apiVersion: "odo.openshift.io/v1alpha1",
metadata: {
name: "comp2",
creationTimestamp: null
},
spec: {
type: "nodejs",
source: "file:///Users/nodejs-ex"
},
status: {
linkedServices: {
service: 'service'
}
}
});
quickPickStub.onFirstCall().resolves('service');
execStub.onFirstCall().rejects(errorMessage);
let savedErr: any;
try {
await Component.unlinkService(componentItem);
} catch (err) {
savedErr = err;
}
expect(savedErr).equals(`Failed to unlink Service with error '${errorMessage}'`);
});
test('calls the appropriate error message when no link component found', async () => {
sandbox.stub(Component, 'getLinkData').resolves({
kind: "Component",
apiVersion: "odo.openshift.io/v1alpha1",
metadata: {
name: "comp2",
creationTimestamp: null
},
spec: {
type: "nodejs",
source: "file:///Users/nodejs-ex"
},
status: {
active: false
}
});
try {
await Component.unlink(componentItem);
} catch (err) {
expect(err.message).equals('No linked Services found');
return;
}
expect.fail();
});
});
suite('del', () => {
const onDidFake = (listener) => {
Promise.resolve().then(() => { listener(undefined); } );
return {
dispose: () => { return; }
};
};
setup(() => {
sandbox.stub(Component, 'unlinkAllComponents');
quickPickStub = sandbox.stub(vscode.window, 'showQuickPick');
quickPickStub.onFirstCall().resolves(projectItem);
quickPickStub.onSecondCall().resolves(appItem);
quickPickStub.onThirdCall().resolves(componentItem);
sandbox.stub(vscode.window, 'showWarningMessage').resolves('Yes');
execStub.resolves({ error: undefined, stdout: '', stderr: '' });
sandbox.stub(vscode.workspace, 'workspaceFolders').value([wsFolder1, wsFolder2]);
sandbox.stub(vscode.workspace, 'getWorkspaceFolder').returns(wsFolder1);
OdoImpl.data.addContexts(vscode.workspace.workspaceFolders);
});
test('works from context menu', async () => {
sandbox.stub(vscode.workspace, 'onDidChangeWorkspaceFolders').callsFake(onDidFake);
const result = await Component.del(componentItem);
expect(result).equals(`Component '${componentItem.getName()}' successfully deleted`);
expect(execStub).calledWith(Command.deleteComponent(projectItem.getName(), appItem.getName(), componentItem.getName()));
});
test('works with no context', async () => {
sandbox.stub(vscode.workspace, 'onDidChangeWorkspaceFolders').callsFake(onDidFake);
const result = await Component.del(null);
expect(result).equals(`Component '${componentItem.getName()}' successfully deleted`);
expect(execStub).calledWith(Command.deleteComponent(projectItem.getName(), appItem.getName(), componentItem.getName()));
});
test('wraps errors in additional info', async () => {
execStub.rejects(errorMessage);
try {
await Component.del(componentItem);
} catch (err) {
expect(err).equals(`Failed to delete Component with error '${errorMessage}'`);
}
});
test('returns null when no project is selected', async () => {
quickPickStub.onFirstCall().resolves();
const result = await Component.del(null);
expect(result).null;
});
test('returns null when no application is selected', async () => {
quickPickStub.onSecondCall().resolves();
const result = await Component.del(null);
expect(result).null;
});
test('returns null when no component is selected', async () => {
quickPickStub.onThirdCall().resolves();
const result = await Component.del(null);
expect(result).null;
});
});
suite('undeploy', () => {
setup(() => {
sandbox.stub(Component, 'unlinkAllComponents');
quickPickStub = sandbox.stub(vscode.window, 'showQuickPick');
quickPickStub.onFirstCall().resolves(projectItem);
quickPickStub.onSecondCall().resolves(appItem);
quickPickStub.onThirdCall().resolves(componentItem);
sandbox.stub(vscode.window, 'showWarningMessage').resolves('Yes');
execStub.resolves({ error: undefined, stdout: '', stderr: '' });
sandbox.stub(vscode.workspace, 'workspaceFolders').value([wsFolder1, wsFolder2]);
sandbox.stub(vscode.workspace, 'getWorkspaceFolder').returns(wsFolder1);
OdoImpl.data.addContexts(vscode.workspace.workspaceFolders);
});
test('works from context menu', async () => {
const result = await Component.undeploy(componentItem);
expect(result).equals(`Component '${componentItem.getName()}' successfully undeployed`);
expect(execStub).calledWith(Command.undeployComponent(projectItem.getName(), appItem.getName(), componentItem.getName()));
});
test('works with no context', async () => {
const result = await Component.undeploy(null);
expect(result).equals(`Component '${componentItem.getName()}' successfully undeployed`);
expect(execStub).calledWith(Command.undeployComponent(projectItem.getName(), appItem.getName(), componentItem.getName()));
});
test('wraps errors in additional info', async () => {
execStub.rejects(errorMessage);
try {
await Component.undeploy(componentItem);
} catch (err) {
expect(err).equals(`Failed to undeploy Component with error '${errorMessage}'`);
}
});
test('returns null when no project is selected', async () => {
quickPickStub.onFirstCall().resolves();
const result = await Component.undeploy(null);
expect(result).null;
});
test('returns null when no application is selected', async () => {
quickPickStub.onSecondCall().resolves();
const result = await Component.undeploy(null);
expect(result).null;
});
test('returns null when no component is selected', async () => {
quickPickStub.onThirdCall().resolves();
const result = await Component.undeploy(null);
expect(result).null;
});
});
suite('linkComponent', () => {
setup(() => {
quickPickStub = sandbox.stub(vscode.window, 'showQuickPick');
});
test('works from context menu', async () => {
quickPickStub.resolves(componentItem);
execStub.resolves({ error: null, stderr: "", stdout: '8080, ' });
const result = await Component.linkComponent(componentItem);
expect(result).equals(`Component '${componentItem.getName()}' successfully linked with Component '${componentItem.getName()}'`);
});
test('works from context menu if more than one ports is available', async () => {
getComponentsStub.resolves([componentItem, componentItem]);
quickPickStub.resolves(componentItem);
execStub.resolves({ error: null, stderr: "", stdout: '8080, 8081, ' });
const result = await Component.linkComponent(componentItem);
expect(result).equals(`Component '${componentItem.getName()}' successfully linked with Component '${componentItem.getName()}'`);
});
test('returns null when no component selected to link', async () => {
quickPickStub.resolves();
const result = await Component.linkComponent(componentItem);
expect(result).null;
});
test('calls the appropriate error message when only one component found', async () => {
quickPickStub.restore();
componentItem.contextValue = ContextType.COMPONENT_PUSHED;
getComponentsStub.resolves([componentItem]);
try {
await Component.linkComponent(componentItem);
} catch (err) {
expect(err.message).equals('You have no Components available to link, please create new OpenShift Component and try again.');
return;
}
expect.fail();
});
test('errors when no ports available', async () => {
quickPickStub.resolves(componentItem);
execStub.resolves({ error: null, stderr: "", stdout: "" });
let savedErr: any;
try {
await Component.linkComponent(componentItem);
} catch (err) {
savedErr = err;
}
expect(savedErr).equals(`Component '${componentItem.getName()}' has no Ports declared.`);
});
test('errors when a subcommand fails', async () => {
quickPickStub.resolves(componentItem);
execStub.onFirstCall().resolves({ error: null, stderr: "", stdout: '8080, ' });
execStub.onSecondCall().rejects(errorMessage);
let savedErr: any;
try {
await Component.linkComponent(componentItem);
} catch (err) {
savedErr = err;
}
expect(savedErr).equals(`Failed to link component with error '${errorMessage}'`);
});
});
suite('linkComponent with no context', () => {
setup(() => {
quickPickStub = sandbox.stub(vscode.window, 'showQuickPick');
quickPickStub.onFirstCall().resolves(projectItem);
quickPickStub.onSecondCall().resolves(appItem);
quickPickStub.onThirdCall().resolves(undefined);
});
test('asks for context and exits if not provided', async () => {
const result = await Component.linkComponent(null);
expect(result).null;
expect(quickPickStub).calledThrice;
});
});
suite('linkService', () => {
setup(() => {
quickPickStub = sandbox.stub(vscode.window, 'showQuickPick');
});
test('returns null when cancelled', async () => {
quickPickStub.resolves();
const result = await Component.linkService(null);
expect(result).null;
});
test('works from context menu', async () => {
quickPickStub.resolves(serviceItem);
const result = await Component.linkService(componentItem);
expect(result).equals(`Service '${serviceItem.getName()}' successfully linked with Component '${componentItem.getName()}'`);
expect(execStub).calledOnceWith(Command.linkServiceTo(projectItem.getName(), appItem.getName(), componentItem.getName(), serviceItem.getName()));
});
test('returns null when no service selected to link', async () => {
quickPickStub.resolves();
const result = await Component.linkService(componentItem);
expect(result).null;
});
test('errors when a subcommand fails', async () => {
quickPickStub.resolves(componentItem);
execStub.rejects(errorMessage);
let savedErr: any;
try {
await Component.linkService(componentItem);
} catch (err) {
savedErr = err;
}
expect(savedErr).equals(`Failed to link Service with error '${errorMessage}'`);
});
});
suite('linkService with no context', () => {
setup(() => {
quickPickStub = sandbox.stub(vscode.window, 'showQuickPick');
quickPickStub.onFirstCall().resolves(projectItem);
quickPickStub.onSecondCall().resolves(appItem);
quickPickStub.onThirdCall().resolves(componentItem);
});
test('works from context menu', async () => {
quickPickStub.resolves(serviceItem);
const result = await Component.linkService(null);
expect(result).equals(`Service '${serviceItem.getName()}' successfully linked with Component '${componentItem.getName()}'`);
expect(execStub).calledOnceWith(Command.linkServiceTo(projectItem.getName(), appItem.getName(), componentItem.getName(), serviceItem.getName()));
});
test('returns null when no service selected to link', async () => {
quickPickStub.resolves();
const result = await Component.linkService(null);
expect(result).null;
});
test('errors when a subcommand fails', async () => {
quickPickStub.resolves(componentItem);
execStub.rejects(errorMessage);
let savedErr: any;
try {
await Component.linkService(null);
} catch (err) {
savedErr = err;
}
expect(savedErr).equals(`Failed to link Service with error '${errorMessage}'`);
});
});
suite('describe', () => {
setup(() => {
quickPickStub = sandbox.stub(vscode.window, 'showQuickPick');
quickPickStub.onFirstCall().resolves(projectItem);
quickPickStub.onSecondCall().resolves(appItem);
quickPickStub.onThirdCall().resolves(componentItem);
});
test('returns null when cancelled', async () => {
quickPickStub.onFirstCall().resolves();
const result = await Component.describe(null);
expect(result).null;
});
test('describe calls the correct odo command in terminal', async () => {
await Component.describe(componentItem);
expect(termStub).calledOnceWith(Command.describeComponent(projectItem.getName(), appItem.getName(), componentItem.getName()));
});
test('works with no context', async () => {