forked from redhat-developer/vscode-openshift-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoc.test.ts
More file actions
138 lines (124 loc) · 5.49 KB
/
oc.test.ts
File metadata and controls
138 lines (124 loc) · 5.49 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
/*-----------------------------------------------------------------------------------------------
* 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 chai from 'chai';
import * as sinon from 'sinon';
import * as sinonChai from 'sinon-chai';
import { window } from 'vscode';
import { Oc } from '../../src/oc/ocWrapper';
import { Project } from '../../src/oc/project';
import { Odo } from '../../src/odo/odoWrapper';
import { ToolsConfig } from '../../src/tools';
import { ChildProcessUtil } from '../../src/util/childProcessUtil';
import { YamlFileCommands } from '../../src/yamlFileCommands';
const {expect} = chai;
chai.use(sinonChai);
suite('Oc', function() {
let sandbox: sinon.SinonSandbox;
let detectOrDownloadStub: sinon.SinonStub<[string], Promise<string>>;
let warnStub: sinon.SinonStub<[string, import('vscode').MessageOptions, ...import('vscode').MessageItem[]], Thenable<import('vscode').MessageItem>>;
let execStub: sinon.SinonStub;
let getActiveProjectStub: sinon.SinonStub;
const projectItem: Project = { name: 'my-project', active: true };
const sampleYaml = `
# manifests.yaml
apiVersion: image.openshift.io/v1
kind: ImageStream
metadata:
labels:
app: spring-petclinic
name: spring-petclinic
`;
const TextEditorMock = {
document: {
fileName: 'manifests.yaml',
getText: sinon.stub().returns(sampleYaml),
},
};
setup(function() {
sandbox = sinon.createSandbox();
warnStub = sandbox.stub(window, 'showWarningMessage');
execStub = sandbox.stub(ChildProcessUtil.prototype, 'execute');
getActiveProjectStub = sandbox.stub(Oc.Instance, 'getActiveProject').resolves('my-project');
detectOrDownloadStub = sandbox.stub(ToolsConfig, 'detect').resolves('path');
sandbox.stub(Odo.Instance, 'getActiveCluster').resolves('cluster');
sandbox.stub(Oc.Instance, 'getProjects').resolves([projectItem]);
sandbox.stub(Oc.Instance, 'canCreatePod').resolves(true);
});
teardown(function() {
sandbox.restore();
});
test('show warning message if file is not json or yaml', async function() {
await YamlFileCommands.create();
expect(warnStub).is.calledOnce;
});
test('show warning message if file is untitled', async function() {
sandbox.stub(window, 'activeTextEditor').value({
document: {
fileName: 'manifests.yaml',
isUntitled: true,
},
});
detectOrDownloadStub.onFirstCall().resolves(undefined);
await YamlFileCommands.create();
expect(warnStub).is.calledOnce;
});
test('Save the file if user click on Save button', async function() {
execStub.resolves({
error: undefined,
stderr: '',
stdout: 'imagestream.image.openshift.io/spring-petclinic created\ndeploymentconfig.apps.openshift.io/spring-petclinic created'
});
sandbox.stub<any, any>(window, 'showInformationMessage').resolves('Save');
sandbox.stub(window, 'activeTextEditor').value({
document: {
fileName: 'manifests.yaml',
isDirty: true,
save: sinon.stub().returns(true)
},
});
const result = await YamlFileCommands.create();
expect(result).equals('Resources were successfully created.');
});
test('show warning message if file content is changed', async function() {
const infoMsg = sandbox.stub(window, 'showInformationMessage').resolves(undefined);
sandbox.stub(window, 'activeTextEditor').value({
document: {
fileName: 'manifests.yaml',
isDirty: true,
},
});
await YamlFileCommands.create();
expect(warnStub).is.calledOnce;
expect(infoMsg).is.calledOnce;
});
test('Creates an OpenShift resource using `.json` or `.yaml` file location from an active editor', async function() {
execStub.resolves({
error: undefined,
stderr: '',
stdout: 'imagestream.image.openshift.io/spring-petclinic created\ndeploymentconfig.apps.openshift.io/spring-petclinic created'
});
sandbox.stub(window, 'activeTextEditor').value(TextEditorMock);
const result = await YamlFileCommands.create();
expect(result).equals('Resources were successfully created.');
});
test('errors when fail to create resource', async function() {
let savedErr: any;
execStub.rejects('error');
sandbox.stub(window, 'activeTextEditor').value(TextEditorMock);
try {
await YamlFileCommands.create();
} catch (err) {
savedErr = err;
}
expect(savedErr === 'error');
});
test('shows warning message when there is no active project', async function() {
getActiveProjectStub.resetBehavior();
getActiveProjectStub.resolves(undefined);
sandbox.stub(window, 'activeTextEditor').value(TextEditorMock);
expect(await YamlFileCommands.create());
expect(warnStub).to.be.calledOnceWithExactly('The current project doesn\'t exist. Please select an existing project to work with or create a new project', 'Select or Create Project', 'Cancel');
});
});