forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathodo.test.ts
More file actions
750 lines (681 loc) · 27.9 KB
/
odo.test.ts
File metadata and controls
750 lines (681 loc) · 27.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
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/
import * as assert from 'assert';
import * as sinon from 'sinon';
import * as chai from 'chai';
import * as sinonChai from 'sinon-chai';
import { window, Terminal, workspace } from 'vscode';
import { ExecException } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { TestItem } from './openshift/testOSItem';
import { WindowUtil } from '../../src/util/windowUtils';
import { ToolsConfig } from '../../src/tools';
import { CliExitData, CliChannel } from '../../src/cli';
import * as odo from '../../src/odo';
import { ContextType } from '../../src/odo';
import jsYaml = require('js-yaml');
const {expect} = chai;
chai.use(sinonChai);
suite("odo", () => {
const odoCli: odo.Odo = odo.OdoImpl.Instance;
let sandbox: sinon.SinonSandbox;
const errorMessage = 'Error';
setup(() => {
sandbox = sinon.createSandbox();
sandbox.stub(workspace, 'getConfiguration').returns({
get<T>(): Promise<T|undefined> {
return Promise.resolve(undefined);
},
update(): Promise<void> {
return Promise.resolve();
},
inspect(): {
key: string;
} {
return undefined;
},
has(): boolean {
return true;
},
disableCheckForMigration: false,
outputVerbosityLevel: 0,
showChannelOnOutput: false
});
sandbox.stub(ToolsConfig, 'getVersion').resolves('0.0.15');
odoCli.clearCache();
});
teardown(() => {
sandbox.restore();
});
suite('command execution', () => {
let execStub: sinon.SinonStub; let toolsStub: sinon.SinonStub;
const command = 'odo do whatever you do';
setup(() => {
execStub = sandbox.stub(CliChannel.prototype, 'execute');
toolsStub = sandbox.stub(ToolsConfig, 'detectOrDownload').resolves();
});
test('execute calls the given command in shell', async () => {
const data = { stdout: 'done', stderr: '', error: null };
execStub.resolves(data);
const result = await odoCli.execute(command);
expect(execStub).calledOnceWith(command);
expect(result).deep.equals(data);
});
test('execute calls command with its detected location', async () => {
const toolPath = 'path/to/tool/tool';
execStub.resolves({ stdout: 'done', stderr: '', error: null });
toolsStub.resolves(toolPath);
await odoCli.execute(command);
expect(execStub).calledOnceWith(command.replace('odo', `"${toolPath}"`));
});
test('execute allows to set its working directory', async () => {
execStub.resolves({ stdout: 'done', stderr: '', error: null });
const cwd = 'path/to/some/dir';
await odoCli.execute(command, cwd);
expect(execStub).calledOnceWith(command, { cwd });
});
test('execute rejects if an error occurs in the shell command', async () => {
const err: ExecException = { message: 'ERROR', name: 'err' };
execStub.resolves({ error: err, stdout: '', stderr: '' });
try {
await odoCli.execute(command);
expect.fail();
} catch (error) {
expect(error).equals(err);
}
});
test('execute can be set to pass errors through exit data', async () => {
const err: ExecException = { message: 'ERROR', name: 'err' };
execStub.resolves({ error: err, stdout: '', stderr: '' });
const result = await odoCli.execute(command, null, false);
expect(result).deep.equals({ error: err, stdout: '', stderr: '' });
});
test('executeInTerminal send command to terminal and shows it', async () => {
const termFake: Terminal = {
name: "name",
processId: Promise.resolve(1),
sendText: sinon.stub(),
show: sinon.stub(),
hide: sinon.stub(),
dispose: sinon.stub()
};
toolsStub.restore();
toolsStub = sandbox.stub(ToolsConfig, 'detectOrDownload').resolves(path.join('segment1', 'segment2'));
const ctStub = sandbox.stub(WindowUtil, 'createTerminal').returns(termFake);
await odoCli.executeInTerminal('cmd');
expect(termFake.sendText).calledOnce;
expect(termFake.show).calledOnce;
expect(ctStub).calledWith('OpenShift', process.cwd());
});
});
suite('item listings', () => {
let execStub: sinon.SinonStub; let yamlStub: sinon.SinonStub;
const project = new TestItem(null, 'project', ContextType.PROJECT);
const app = new TestItem(project, 'app', ContextType.APPLICATION);
setup(() => {
execStub = sandbox.stub(odoCli, 'execute');
sandbox.stub(odo.OdoImpl.prototype, 'convertObjectsFromPreviousOdoReleases');
yamlStub = sandbox.stub(jsYaml, 'safeLoad');
sandbox.stub(fs, 'readFileSync');
});
test('getProjects returns projects under a given cluster', async () => {
const activeProjs = [{ name: 'project1' }, { name: 'project2'}];
yamlStub.returns({ ActiveApplications: activeProjs });
execStub.onFirstCall().resolves({
error: undefined,
stdout: 'Server https://172.17.185.52:8443',
stderr: ''
});
execStub.onSecondCall().resolves({
error: undefined,
stdout: 'Server https://172.17.185.52:8443',
stderr: ''
});
execStub.onThirdCall().resolves({
error: undefined,
stdout: JSON.stringify({
items: [
{
metadata: {
name: 'project1'
}
}
]
}
),
stderr: ''
});
const result = await odoCli.getProjects();
expect(result.length).equals(1);
expect(result[0].getName()).equals('project1');
});
test('getProjects returns empty list if no projects present', async () => {
execStub.onFirstCall().resolves({
error: undefined,
stdout: 'Server https://172.17.185.52:8443',
stderr: ''
});
execStub.onSecondCall().resolves({
error: undefined,
stdout: 'Server https://172.17.185.52:8443',
stderr: ''
});
execStub.onThirdCall().resolves({ stdout: '', stderr: '', error: null });
const result = await odoCli.getProjects();
expect(result).empty;
});
test('getProjects returns empty list if an error occurs', async () => {
const errorStub = sandbox.stub(window, 'showErrorMessage');
sandbox.stub(odoCli, 'getClusters').resolves([new TestItem(undefined, 'cluster', ContextType.CLUSTER)]);
execStub.rejects(errorMessage);
const result = await odoCli.getProjects();
expect(result).empty;
expect(errorStub).calledOnceWith(`Cannot retrieve projects for current cluster. Error: ${errorMessage}`);
});
test('getApplications returns applications for a project', async () => {
const activeApps = [{ name: 'app1', project: 'project1' }, { name: 'app2', project: 'project1'}];
yamlStub.returns({ ActiveApplications: activeApps });
execStub.returns({
error: undefined,
stdout: JSON.stringify({
items: [
{
metadata: {
name: 'app1',
namespace: 'project'
}
}
]
}
),
stderr: ''
});
const result = await odoCli.getApplications(project);
expect(result.length).equals(1);
expect(result[0].getName()).equals('app1');
});
test('getApplications returns empty list if no odo apps are present', async () => {
const activeApps = [{ name: 'app1', project: 'project1' }, { name: 'app2', project: 'project1'}];
yamlStub.returns({ ActiveApplications: activeApps });
execStub.returns({
error: undefined,
stdout: JSON.stringify({
items: []
}
),
stderr: ''
});
const result = await odoCli.getApplications(project);
expect(result).empty;
});
test('getComponents returns components list for an application', async () => {
const activeApps = [{ name: 'app1', project: 'project1' }, { name: 'app2', project: 'project1'}];
yamlStub.returns({ ActiveApplications: activeApps });
execStub.returns({
error: undefined,
stdout: JSON.stringify({
items: [
{
metadata: {
name: 'component1',
namespace: 'project'
}
}
]
}
),
stderr: ''
});
const result = await odoCli.getApplications(app);
expect(result.length).equals(1);
expect(result[0].getName()).equals('component1');
});
test('getServices returns services for an application', async () => {
const services = ['service1', 'service2', 'service3'];
execStub.resolves({ error: null, stderr: '', stdout: JSON.stringify({
kind: "ServiceList",
items: [{
metadata: {
name: "service1"
}, spec: {
}
}, {
metadata: {
name: "service2"
}, spec: {
}
}, {
metadata: {
name: "service3"
}, spec: {
}
}]
})
});
const result = await odoCli.getServices(app);
expect(execStub).calledWith(odo.Command.listServiceInstances(project.getName(), app.getName()));
expect(result.length).equals(3);
for (let i = 0; i < result.length; i++) {
expect(result[i].getName()).equals(services[i]);
}
});
test('getServices returns an empty list if an error occurs', async () => {
execStub.onFirstCall().resolves({error: undefined, stdout: '', stderr: ''});
execStub.onSecondCall().rejects(errorMessage);
const result = await odoCli.getServices(app);
expect(result).empty;
});
test('getServiceTemplates throws exception if service catalog is not enabled', async () => {
execStub.resolves({error: null, stdout: '', stderr:
JSON.stringify({
kind: "Error",
apiVersion: "odo.openshift.io/v1alpha1",
message: "unable to list services because Service Catalog is not enabled in your cluster"
})
});
let e: Error;
try {
await odoCli.getServiceTemplates();
} catch (err) {
e = err;
}
expect(e.message).equals("unable to list services because Service Catalog is not enabled in your cluster");
});
test('getApplicationChildren returns both components and services for an application', async () => {
execStub.onFirstCall().resolves({error: undefined, stdout: JSON.stringify({
items: [
{
metadata: {
name: 'component1',
namespace: 'project'
},
spec: {
source: 'https://'
}
}
]
}), stderr: ''});
const stdout = JSON.stringify({
kind: "ServiceList",
items: [{
metadata: {
name: "service1"
}
}]
});
execStub.onSecondCall().resolves({error: undefined, stdout, stderr: ''});
const result = await odoCli.getApplicationChildren(app);
expect(result[0].getName()).deep.equals('component1');
expect(result[1].getName()).deep.equals('service1');
});
test('getStorageNames returns storage items for a component', async () => {
const component = new TestItem(app, 'comp', ContextType.COMPONENT);
execStub.returns({
error: undefined,
stdout: JSON.stringify({
items: [
{
metadata: {
name: "storage1"
}
},
{
metadata: {
name: "storage2"
}
}
]
}
),
stderr: ''
});
const result = await odoCli.getStorageNames(component);
expect(result.length).equals(2);
});
test('getRoutes returns URL list items for a component', async () => {
const component = new TestItem(app, 'comp', ContextType.COMPONENT);
execStub.returns({
error: undefined,
stdout: JSON.stringify({
items: [
{
metadata: {
name: "route1"
}
},
{
metadata: {
name: "route2"
}
}
]
}
),
stderr: ''
});
const result = await odoCli.getRoutes(component);
expect(result.length).equals(2);
});
test('getComponentChildren returns both routes and storage for a component', async () => {
const component = new TestItem(app, 'comp', ContextType.COMPONENT);
execStub.onFirstCall().resolves({error: undefined, stdout: JSON.stringify({
items: [
{
metadata: {
name: "route1"
}
},
{
metadata: {
name: "route2"
}
}
]
}), stderr: ''});
execStub.onSecondCall().resolves({error: undefined, stdout: JSON.stringify({
items: [
{
metadata: {
name: "storage1"
}
},
{
metadata: {
name: "storage2"
}
}
]
}), stderr: ''});
const result = await odoCli.getComponentChildren(component);
expect(result[2].getName()).deep.equals('route1');
expect(result[0].getName()).deep.equals('storage1');
});
});
suite("catalog integration", () => {
const nodejs = 'nodejs';
const odoCatalog = JSON.stringify({
kind : "ComponentTypeList",
apiVersion : "odo.openshift.io/v1alpha1",
items: [
{
kind : "ComponentType",
apiVersion : "odo.openshift.io/v1alpha1",
metadata: {
name : "nodejs",
namespace : "openshift",
creationTimestamp : null
},
spec: {
allTags: [
"10",
"8",
"8-RHOAR",
"latest",
]
}
}
]
});
const componentCatalog: CliExitData = {
error: null,
stderr: '',
stdout: odoCatalog
};
setup(async () => {
sandbox.stub(odo.OdoImpl.prototype, 'execute').resolves(componentCatalog);
});
test("getComponentTypes returns correct component type names", async () => {
const result = await odoCli.getComponentTypes();
expect(result.length).equals(1);
expect(result[0]).equals('nodejs');
});
test("getComponentTypeVersions returns correct number of tags for component type", async () => {
const result = await odoCli.getComponentTypeVersions(nodejs);
expect(result.length).equals(4);
expect(result[0]).equals("10");
expect(result[3]).equals("latest");
});
});
suite("service integration", () => {
const data: CliExitData = { error: undefined, stderr: null, stdout:
JSON.stringify({
kind: "ServiceTypeList",
apiVersion: "odo.openshift.io/v1alpha1",
metadata: {
creationTimestamp: null
},
items: [
{
kind: "ServiceType",
apiVersion: "odo.openshift.io/v1alpha1",
metadata: {
name: "cakephp-mysql-persistent",
creationTimestamp: null
},
spec: {
hidden: false,
planList: [
"default"
]
}
},
]
})
};
setup(() => {
sandbox.stub(odo.OdoImpl.prototype, 'execute').resolves(data);
});
test("getServiceTemplates returns correct number of services", async () => {
const result: string[] = await odoCli.getServiceTemplates();
expect(result.length).equals(1);
expect(result[0]).equals("cakephp-mysql-persistent");
});
test("getServiceTemplatePlans returns correct number of plans for service", async () => {
const result: string[] = await odoCli.getServiceTemplatePlans("cakephp-mysql-persistent");
assert.equal(result.length, 1);
assert.equal(result[0], 'default');
});
});
suite('odo and oc current cluster detection integration', () => {
const clusterUrl = 'https://localhost:8443';
const odoVersionOutLoggedIn = [
'odo v0.0.15 (2f7ed497)',
'',
`Server: ${clusterUrl}`,
'Kubernetes: v1.11.0+d4cacc0'
];
const odoVersionOutLoggedOut = [
'odo v0.0.15 (2f7ed497)',
'',
'Kubernetes: v1.11.0+d4cacc0'
];
const oc = [
'oc v3.9.0+191fece',
'kubernetes v1.9.1+a0ce1bc657',
'features: Basic-Auth',
'',
`Server ${clusterUrl}`,
'kubernetes v1.11.0+d4cacc0'
];
test('extension first uses odo version to get cluster url', async () => {
sandbox.stub(odo.OdoImpl.prototype, 'execute').resolves({
error: undefined,
stdout: odoVersionOutLoggedIn.join('\n'),
stderr: ''
});
const cluster: odo.OpenShiftObject[] = await odo.getInstance().getClusters();
assert.equal(cluster[0].getName(), clusterUrl);
});
test('show error message if cluster is not login', async () => {
sandbox.stub(odo.OdoImpl.prototype, 'execute').onFirstCall().resolves({
error: undefined,
stdout: '',
stderr: 'Please log in to the cluster'
});
const cluster: odo.OpenShiftObject[] = await odo.getInstance().getClusters();
assert.equal(cluster[0].getName(), "Please log in to the cluster");
});
test('show message if cluster is down', async () => {
sandbox.stub(odo.OdoImpl.prototype, 'execute').onFirstCall().resolves({
error: undefined,
stdout: '',
stderr: 'Unable to connect to OpenShift cluster, is it down?'
});
const cluster: odo.OpenShiftObject[] = await odo.getInstance().getClusters();
assert.equal(cluster[0].getName(), "Cannot connect to the OpenShift cluster");
});
test('extension uses oc version to get cluster url as a backup plan', async () => {
sandbox.stub(odo.OdoImpl.prototype, 'execute').onFirstCall().resolves({
error: undefined,
stdout: odoVersionOutLoggedOut.join('\n'),
stderr: ''
}).onSecondCall().resolves({
error: undefined,
stdout: oc.join('\n'),
stderr: ''
});
const cluster: odo.OpenShiftObject[] = await odo.getInstance().getClusters();
assert.equal(cluster[0].getName(), clusterUrl);
});
test('extension uses odo version to determine if login is required', async () => {
const stub = sandbox.stub(odoCli, 'execute').resolves({ error: null, stdout: 'logged in', stderr: ''});
const result = await odoCli.requireLogin();
expect(stub).calledOnceWith(odo.Command.printOdoVersionAndProjects());
expect(result).false;
});
test('requireLogin returns true if odo is not logged in to the cluster', async () => {
sandbox.stub(odoCli, 'execute').resolves({ error: null, stdout: '', stderr: 'Please log in to the cluster'});
const result = await odoCli.requireLogin();
expect(result).true;
});
});
suite('convertObjectsFromPreviousOdoReleases', () => {
let execStub: sinon.SinonStub;
let showWarningMessageStub: sinon.SinonStub<[string, import("vscode").MessageOptions, ...import("vscode").MessageItem[]], Thenable<import("vscode").MessageItem>>;
setup(() => {
execStub = sandbox.stub(odoCli, 'execute');
execStub.onFirstCall().resolves({
error: undefined,
stdout: 'odo v1.0.0 (4e3175e6f)\n\nServer: https://192.168.64.177:8443\nKubernetes: v1.11.0+d4cacc0\nACTIVE NAME\n* project1',
stderr: ''
});
execStub.onSecondCall().resolves({
error: null,
stderr: "",
stdout: "project1"
});
execStub.onThirdCall().resolves({
error: null,
stderr: "",
stdout: "comp"
});
execStub.onCall(3).resolves({
error: null,
stderr: "",
stdout: "service"
});
showWarningMessageStub = sandbox.stub(window, 'showWarningMessage');
});
test('Disable the check for migration if user select Don\'t check again button', async () => {
showWarningMessageStub.onFirstCall().resolves('Don\'t check again');
execStub.onCall(4).resolves({
error: undefined,
stdout: JSON.stringify({
items: [
{
metadata: {
name: 'project1'
}
}
]
}
),
stderr: ''
});
const result = await odoCli.getProjects();
expect(result.length).equals(1);
expect(result[0].getName()).equals('project1');
});
test('Update the cluster resource when user select on update button', async () => {
sandbox.stub(odoCli.subject, 'next').resolves();
sandbox.stub(window, 'showInformationMessage').resolves();
showWarningMessageStub.onFirstCall().resolves('Update');
execStub.onCall(4).resolves({
error: undefined,
stdout: JSON.stringify({
items: [
{
metadata: {
name: 'project1'
}
}
]
}
),
stderr: ''
});
execStub.onCall(5).resolves({
error: null,
stderr: "",
stdout: "comp"
});
execStub.onCall(6).resolves({
error: null,
stderr: "",
stdout: JSON.stringify({
metadata: {
labels: {
app: "app",
"app.kubernetes.io/component-name": "comp",
"app.kubernetes.io/component-type": "nodejs",
"app.kubernetes.io/component-version": "latest",
"app.kubernetes.io/name": "app",
"app.kubernetes.io/url-name": "URL"
}
}
})
});
execStub.onCall(10).resolves({
error: null,
stderr: "",
stdout: ""
});
execStub.onCall(11).resolves({
error: null,
stderr: "",
stdout: ""
});
execStub.onCall(12).resolves({
error: null,
stderr: "",
stdout: ""
});
execStub.onCall(13).resolves({
error: null,
stderr: "",
stdout: ""
});
execStub.onCall(14).resolves({
error: null,
stderr: "",
stdout: ""
});
execStub.onCall(15).resolves({
error: null,
stderr: "",
stdout: ""
});
execStub.onCall(16).resolves({
error: null,
stderr: "",
stdout: ""
});
const result = await odoCli.getProjects();
expect(result.length).equals(1);
expect(result[0].getName()).equals('project1');
});
});
});