Skip to content

Commit 6545e3d

Browse files
sudhirvermadgolovin
authored andcommitted
Increasing unit test for cluster.ts file (#1206)
1 parent e9b71bf commit 6545e3d

File tree

2 files changed

+98
-23
lines changed

2 files changed

+98
-23
lines changed

src/openshift/cluster.ts

+2-11
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,13 @@ import { KubeConfigUtils } from '../util/kubeUtils';
1313
import { Filters } from "../util/filters";
1414

1515
class CreateUrlItem implements QuickPickItem {
16-
17-
constructor() { }
18-
1916
get label(): string { return `$(plus) Provide new URL...`; }
20-
get description(): string { return ''; }
21-
2217
}
2318

2419
class CreateUserItem implements QuickPickItem {
25-
26-
constructor() { }
27-
2820
get label(): string { return `$(plus) Add new user...`; }
29-
get description(): string { return ''; }
30-
3121
}
22+
3223
export class Cluster extends OpenShiftItem {
3324
public static extensionContext: ExtensionContext;
3425
static async logout(): Promise<string> {
@@ -88,7 +79,7 @@ export class Cluster extends OpenShiftItem {
8879
if (!choice) return null;
8980
return Promise.resolve()
9081
.then(() => Cluster.odo.execute(Command.setOpenshiftContext(choice.label)))
91-
.then(() => window.showInformationMessage(`Cluster context is changed to: ${choice.label}`));
82+
.then(() => `Cluster context is changed to: ${choice.label}`);
9283
}
9384

9485
static async getUrl(): Promise<string | null> {

test/unit/openshift/cluster.test.ts

+96-12
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { CliExitData } from '../../../src/cli';
1616
import { TestItem } from './testOSItem';
1717
import { OpenShiftItem } from '../../../src/openshift/openshiftItem';
1818
import pq = require('proxyquire');
19-
import { getVscodeModule } from '../../../src/util/credentialManager';
19+
import { getVscodeModule, TokenStore } from '../../../src/util/credentialManager';
2020

2121
const expect = chai.expect;
2222
chai.use(sinonChai);
@@ -44,6 +44,27 @@ suite('Openshift/Cluster', () => {
4444
stderr: error,
4545
stdout: 'output'
4646
};
47+
const routeObj = `{
48+
"apiVersion": "v1",
49+
"items": [
50+
{
51+
"apiVersion": "route.openshift.io/v1",
52+
"kind": "Route",
53+
"spec": {
54+
"host": "console-openshift-console.apps-crc.testing",
55+
"port": {
56+
"targetPort": "https"
57+
},
58+
"wildcardPolicy": "None"
59+
}
60+
}
61+
],
62+
"kind": "List",
63+
"metadata": {
64+
"resourceVersion": "",
65+
"selfLink": ""
66+
}
67+
}`;
4768
const testUrl = 'https://162.165.64.43:8443';
4869
const testUser = 'user';
4970
const password = 'password';
@@ -83,13 +104,21 @@ suite('Openshift/Cluster', () => {
83104
expect(status).null;
84105
});
85106

107+
test('returns null if loginActions is not selected', async () => {
108+
infoStub.resolves('Yes');
109+
quickPickStub.onFirstCall().resolves(null);
110+
const status = await Cluster.login();
111+
112+
expect(status).null;
113+
});
114+
86115
test('wraps incoming errors', async () => {
87116
quickPickStub.resolves('Credentials');
88117
quickPickStub.onSecondCall().resolves({description: "Current Context", label: testUrl});
89118
quickPickStub.onThirdCall().resolves({description: "Current Context", label: testUser});
90119
inputStub.resolves(password);
91120
commandStub.rejects(err);
92-
let expectedErr;
121+
let expectedErr: { message: any; };
93122
try {
94123
await Cluster.login();
95124
} catch (error) {
@@ -125,6 +154,27 @@ suite('Openshift/Cluster', () => {
125154
expect(result).equals(`Successfully logged in to '${testUrl}'`);
126155
});
127156

157+
test('returns null if cluster url is not provided', async () => {
158+
infoStub.resolves('Yes');
159+
quickPickStub.onFirstCall().resolves(null);
160+
const result = await Cluster.credentialsLogin();
161+
expect(result).null;
162+
});
163+
164+
test('returns null if username is not provided', async () => {
165+
infoStub.resolves('Yes');
166+
quickPickStub.onSecondCall().resolves(null);
167+
const result = await Cluster.credentialsLogin();
168+
expect(result).null;
169+
});
170+
171+
test("doesn't ask to save password if old and new passwords are the same", async () => {
172+
infoStub.resolves('Yes');
173+
sandbox.stub(TokenStore, 'getItem').resolves(password);
174+
const result = await Cluster.credentialsLogin();
175+
expect(result).equals(`Successfully logged in to '${testUrl}'`);
176+
});
177+
128178
test('exits if the user cancels url input box', async () => {
129179
loginStub.resolves(false);
130180
inputStub.onFirstCall().resolves(null);
@@ -163,7 +213,7 @@ suite('Openshift/Cluster', () => {
163213

164214
test('errors if there is output on odo stderr', async () => {
165215
execStub.resolves(errorData);
166-
let expectedErr;
216+
let expectedErr: { message: any; };
167217
try {
168218
await Cluster.credentialsLogin();
169219
} catch (err) {
@@ -248,13 +298,12 @@ suite('Openshift/Cluster', () => {
248298

249299
test('handles incoming errors the same way as credentials login', async () => {
250300
execStub.rejects(err);
251-
let expectedErr;
301+
let expectedErr: { message: any; };
252302
try {
253303
await Cluster.tokenLogin();
254304
} catch (error) {
255305
expectedErr = error;
256306
}
257-
console.log(expectedErr.message);
258307
expect(expectedErr.message).equals(`Failed to login to cluster '${testUrl}' with '${err.message}'!`);
259308
});
260309
});
@@ -294,7 +343,7 @@ suite('Openshift/Cluster', () => {
294343

295344
test('handles errors from odo', async () => {
296345
execStub.rejects(error);
297-
let expectedErr;
346+
let expectedErr: any;
298347
try {
299348
await Cluster.logout();
300349

@@ -306,7 +355,7 @@ suite('Openshift/Cluster', () => {
306355

307356
test('handles errors from odo stderr', async () => {
308357
execStub.resolves(errorData);
309-
let expectedErr;
358+
let expectedErr: any;
310359
try {
311360
await Cluster.logout();
312361
} catch (err) {
@@ -322,7 +371,7 @@ suite('Openshift/Cluster', () => {
322371
quickPickStub.onSecondCall().resolves({description: "Current Context", label: testUrl});
323372
quickPickStub.onThirdCall().resolves({description: "Current Context", label: testUrl});
324373
inputStub.resolves(password);
325-
let expectedErr;
374+
let expectedErr: { message: any; };
326375
try {
327376
await Cluster.logout();
328377
} catch (err) {
@@ -350,35 +399,70 @@ suite('Openshift/Cluster', () => {
350399
});
351400
});
352401

402+
suite('switchContext', () => {
403+
const choice = {
404+
label: 'minishift'
405+
};
406+
setup(() => {
407+
execStub.onFirstCall().resolves();
408+
});
409+
410+
test('changes cluster\'s context', async () => {
411+
quickPickStub.onFirstCall().resolves(choice);
412+
const result = await Cluster.switchContext();
413+
expect(result).equals(`Cluster context is changed to: ${choice.label}`);
414+
});
415+
416+
test('returns null if OpenShift context is not selected', async () => {
417+
quickPickStub.onFirstCall().resolves(null);
418+
const result = await Cluster.switchContext();
419+
expect(result).null;
420+
});
421+
});
422+
353423
suite('open console', () => {
354424
const openStub: sinon.SinonStub = sinon.stub();
355-
let clusterMock;
425+
let clusterMock: { openshiftConsole: { (arg0: TestItem): void; (): void; (): void; }; };
426+
let cluster: TestItem;
427+
356428
setup(() => {
357429
clusterMock = pq('../../../src/openshift/cluster', {
358430
open: openStub
359431
}).Cluster;
432+
cluster = new TestItem(null, 'http://localhost', ContextType.CLUSTER);
360433
});
361434

362435
test('opens URL from cluster\'s tree item label if called from cluster\'s context menu', () => {
363-
const cluster = new TestItem(null, 'http://localhost', ContextType.CLUSTER);
364436
clusterMock.openshiftConsole(cluster);
365437
openStub.calledOnceWith('http://localhost');
366438
});
367439

368440
test('opens URL from first cluster label', () => {
369-
const cluster = new TestItem(null, 'http://localhost', ContextType.CLUSTER);
370441
sandbox.stub(OdoImpl.prototype, 'getClusters').resolves([cluster]);
371442
clusterMock.openshiftConsole();
372443
openStub.calledOnceWith('http://localhost');
373444
});
374445

375446
test('shows error message if node label is not URL', () => {
376-
const cluster = new TestItem(null, 'localhost', ContextType.CLUSTER);
377447
sandbox.stub(OdoImpl.prototype, 'getClusters').resolves([cluster]);
378448
const errMsgStub = sandbox.stub(vscode.window, 'showErrorMessage');
379449
clusterMock.openshiftConsole();
380450
errMsgStub.calledOnceWith('localhost', undefined);
381451
});
382452

453+
test('opens cluster\'s URL from context menu', () => {
454+
execStub.onFirstCall().resolves({
455+
error: null,
456+
stderr: error,
457+
stdout: 'output'
458+
});
459+
execStub.onSecondCall().resolves({
460+
error: null,
461+
stderr: error,
462+
stdout: routeObj
463+
});
464+
clusterMock.openshiftConsole(cluster);
465+
openStub.calledOnceWith('http://localhost');
466+
});
383467
});
384468
});

0 commit comments

Comments
 (0)