Skip to content

Commit d748f4a

Browse files
committed
"Delete project" warns about deleting project contents
- Add some integration tests for the `oc` commands we call in the file Closes #2023 Signed-off-by: David Thompson <davthomp@redhat.com>
1 parent 4d121d6 commit d748f4a

File tree

3 files changed

+88
-5
lines changed

3 files changed

+88
-5
lines changed

src/openshift/project.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ import { VsCommandError, vsCommand } from '../vscommand';
1414
import OpenShiftItem from './openshiftItem';
1515

1616
export class Command {
17-
static listProjects(): CommandText {
18-
return new CommandText('oc', 'get projects', [new CommandOption('-o', 'jsonpath="{range .items[*]}{.metadata.name}{\'\\n\'}{end}')]);
19-
}
20-
2117
static setActiveProject(name: string) {
2218
return new CommandText('oc', `project ${name}`);
2319
}
2420

2521
static deleteProject(name: string) {
2622
return new CommandText('oc delete project', name, [new CommandOption('--wait=true')])
2723
}
24+
25+
static getAll(namespace: string) {
26+
return new CommandText('oc', 'get all', [new CommandOption('--namespace', namespace), new CommandOption('-o', 'json')]);
27+
}
2828
}
2929

3030
export class Project extends OpenShiftItem {
@@ -73,7 +73,9 @@ export class Project extends OpenShiftItem {
7373
static async del(project: KubernetesObject): Promise<string> {
7474
let result: Promise<string> = null;
7575

76-
const value = await window.showWarningMessage(`Do you want to delete Project '${project.metadata.name}'?`, 'Yes', 'Cancel');
76+
const isProjectEmpty = JSON.parse((await getOdoInstance().execute(Command.getAll(project.metadata.name))).stdout).items.length === 0;
77+
78+
const value = await window.showWarningMessage(`Do you want to delete Project '${project.metadata.name}'${!isProjectEmpty ? ' and all its contents' : ''}?`, 'Yes', 'Cancel');
7779
if (value === 'Yes') {
7880
result = Progress.execFunctionWithProgress(
7981
`Deleting Project '${project.metadata.name}'`,

test/integration/project.test.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*-----------------------------------------------------------------------------------------------
2+
* Copyright (c) Red Hat, Inc. All rights reserved.
3+
* Licensed under the MIT License. See LICENSE file in the project root for license information.
4+
*-----------------------------------------------------------------------------------------------*/
5+
6+
import { expect } from 'chai';
7+
import { getInstance } from '../../src/odo';
8+
import { Command } from '../../src/odo/command';
9+
import { Command as ProjectCommand } from '../../src/openshift/project';
10+
11+
suite('openshift/project.ts', function () {
12+
13+
const clusterUrl = process.env.CLUSTER_URL || 'https://api.crc.testing:6443';
14+
const username = process.env.CLUSTER_USER || 'developer';
15+
const password = process.env.CLUSTER_PASSWORD || 'developer';
16+
17+
const TEST_PROJECT_1 = 'test-project-1';
18+
const TEST_PROJECT_2 = 'test-project-2';
19+
20+
const ODO = getInstance();
21+
22+
suiteSetup(async function () {
23+
if (await ODO.requireLogin()) {
24+
await ODO.execute(Command.odoLoginWithUsernamePassword(clusterUrl, username, password));
25+
}
26+
try {
27+
await ODO.deleteProject(TEST_PROJECT_1);
28+
} catch (e) {
29+
// do nothing
30+
}
31+
try {
32+
await ODO.deleteProject(TEST_PROJECT_2);
33+
} catch (e) {
34+
// do nothing
35+
}
36+
await ODO.createProject(TEST_PROJECT_1);
37+
await ODO.createProject(TEST_PROJECT_2);
38+
});
39+
40+
suiteTeardown(async function () {
41+
try {
42+
await ODO.deleteProject(TEST_PROJECT_1);
43+
} catch (e) {
44+
// do nothing
45+
}
46+
try {
47+
await ODO.deleteProject(TEST_PROJECT_2);
48+
} catch (e) {
49+
// do nothing
50+
}
51+
});
52+
53+
test('Command.setActiveProject()', async function () {
54+
await ODO.execute(ProjectCommand.setActiveProject(TEST_PROJECT_2));
55+
let activeProject = await ODO.getActiveProject();
56+
expect(activeProject).to.equal(TEST_PROJECT_2);
57+
58+
await ODO.execute(ProjectCommand.setActiveProject(TEST_PROJECT_1));
59+
activeProject = await ODO.getActiveProject();
60+
expect(activeProject).to.equal(TEST_PROJECT_1);
61+
});
62+
63+
test('Command.getAll()', async function() {
64+
const res = await ODO.execute(ProjectCommand.getAll(TEST_PROJECT_1));
65+
expect(JSON.parse(res.stdout).items).to.have.length(0);
66+
});
67+
68+
test('Command.deleteProject()', async function() {
69+
await ODO.execute(ProjectCommand.deleteProject(TEST_PROJECT_1));
70+
const projects = await ODO.getProjects();
71+
expect(projects).not.to.contain(TEST_PROJECT_1);
72+
});
73+
74+
});

test/unit/extension.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ suite('openshift toolkit Extension', () => {
8686
"devfileComponents": []
8787
}`, stderr: ''}
8888
}
89+
if (`${cmd}`.includes('all')) {
90+
return {
91+
error: undefined,
92+
stdout: '{ "items": [] }',
93+
stderr: ''
94+
};
95+
}
8996
return { error: undefined, stdout: '', stderr: ''};
9097
});
9198
sandbox.stub(OdoImpl.prototype, 'getActiveCluster').resolves('cluster');

0 commit comments

Comments
 (0)