Skip to content

Commit

Permalink
"Delete project" warns about deleting project contents
Browse files Browse the repository at this point in the history
- Add some integration tests for the `oc` commands we call in the file

Closes redhat-developer#2023

Signed-off-by: David Thompson <davthomp@redhat.com>
  • Loading branch information
datho7561 committed Jul 13, 2023
1 parent 4d121d6 commit 05ed7f6
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/openshift/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ import { VsCommandError, vsCommand } from '../vscommand';
import OpenShiftItem from './openshiftItem';

export class Command {
static listProjects(): CommandText {
return new CommandText('oc', 'get projects', [new CommandOption('-o', 'jsonpath="{range .items[*]}{.metadata.name}{\'\\n\'}{end}')]);
}

static setActiveProject(name: string) {
return new CommandText('oc', `project ${name}`);
}

static deleteProject(name: string) {
return new CommandText('oc delete project', name, [new CommandOption('--wait=true')])
}

static getAll(namespace: string) {
return new CommandText('oc', 'get all', [new CommandOption('--namespace', namespace), new CommandOption('-o', 'json')]);
}
}

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

const value = await window.showWarningMessage(`Do you want to delete Project '${project.metadata.name}'?`, 'Yes', 'Cancel');
const isProjectEmpty = JSON.parse((await getOdoInstance().execute(Command.getAll(project.metadata.name))).stdout).items.length === 0;

const value = await window.showWarningMessage(`Do you want to delete Project '${project.metadata.name}'${!isProjectEmpty ? ' and all its contents' : ''}?`, 'Yes', 'Cancel');
if (value === 'Yes') {
result = Progress.execFunctionWithProgress(
`Deleting Project '${project.metadata.name}'`,
Expand Down
74 changes: 74 additions & 0 deletions test/integration/project.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*-----------------------------------------------------------------------------------------------
* Copyright (c) Red Hat, Inc. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*-----------------------------------------------------------------------------------------------*/

import { expect } from 'chai';
import { getInstance } from '../../src/odo';
import { Command } from '../../src/odo/command';
import { Command as ProjectCommand } from '../../src/openshift/project';

suite('openshift/project.ts', function () {

const clusterUrl = process.env.CLUSTER_URL || 'https://api.crc.testing:6443';
const username = process.env.CLUSTER_USER || 'developer';
const password = process.env.CLUSTER_PASSWORD || 'developer';

const TEST_PROJECT_1 = 'test-project-1';
const TEST_PROJECT_2 = 'test-project-2';

const ODO = getInstance();

suiteSetup(async function () {
if (await ODO.requireLogin()) {
await ODO.execute(Command.odoLoginWithUsernamePassword(clusterUrl, username, password));
}
try {
await ODO.deleteProject(TEST_PROJECT_1);
} catch (e) {
// do nothing
}
try {
await ODO.deleteProject(TEST_PROJECT_2);
} catch (e) {
// do nothing
}
await ODO.createProject(TEST_PROJECT_1);
await ODO.createProject(TEST_PROJECT_2);
});

suiteTeardown(async function () {
try {
await ODO.deleteProject(TEST_PROJECT_1);
} catch (e) {
// do nothing
}
try {
await ODO.deleteProject(TEST_PROJECT_2);
} catch (e) {
// do nothing
}
});

test('Command.setActiveProject()', async function () {
await ODO.execute(ProjectCommand.setActiveProject(TEST_PROJECT_2));
let activeProject = await ODO.getActiveProject();
expect(activeProject).to.equal(TEST_PROJECT_2);

await ODO.execute(ProjectCommand.setActiveProject(TEST_PROJECT_1));
activeProject = await ODO.getActiveProject();
expect(activeProject).to.equal(TEST_PROJECT_1);
});

test('Command.getAll()', async function() {
const res = await ODO.execute(ProjectCommand.getAll(TEST_PROJECT_1));
expect(JSON.parse(res.stdout).items).to.have.length(0);
});

test('Command.deleteProject()', async function() {
await ODO.execute(ProjectCommand.deleteProject(TEST_PROJECT_1));
const projects = await ODO.getProjects();
expect(projects).not.to.contain(TEST_PROJECT_1);
});

});
7 changes: 7 additions & 0 deletions test/unit/extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ suite('openshift toolkit Extension', () => {
"devfileComponents": []
}`, stderr: ''}
}
if (`${cmd}`.includes('all')) {
return {
error: undefined,
stdout: '{ "items": [] }',
stderr: ''
};
}
return { error: undefined, stdout: '', stderr: ''};
});
sandbox.stub(OdoImpl.prototype, 'getActiveCluster').resolves('cluster');
Expand Down

0 comments on commit 05ed7f6

Please sign in to comment.