Skip to content

Commit 34e2649

Browse files
authored
Add 'DotnetDevFileAPI' E2E test (#23298)
* Add 'DotnetDevFileAPI' test * Update to overload 'deleteDevWorkspace' method
1 parent 76b99e7 commit 34e2649

5 files changed

+133
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/** *******************************************************************
2+
* copyright (c) 2024 Red Hat, Inc.
3+
*
4+
* This program and the accompanying materials are made
5+
* available under the terms of the Eclipse Public License 2.0
6+
* which is available at https://www.eclipse.org/legal/epl-2.0/
7+
*
8+
* SPDX-License-Identifier: EPL-2.0
9+
**********************************************************************/
10+
import { BASE_TEST_CONSTANTS } from '../../constants/BASE_TEST_CONSTANTS';
11+
import { e2eContainer } from '../../configs/inversify.config';
12+
import { CLASSES } from '../../configs/inversify.types';
13+
import { DevfilesHelper } from '../../utils/DevfilesHelper';
14+
import { ContainerTerminal, KubernetesCommandLineToolsExecutor } from '../../utils/KubernetesCommandLineToolsExecutor';
15+
import { DevWorkspaceConfigurationHelper } from '../../utils/DevWorkspaceConfigurationHelper';
16+
import { DevfileContext } from '@eclipse-che/che-devworkspace-generator/lib/api/devfile-context';
17+
import { ShellString } from 'shelljs';
18+
import { expect } from 'chai';
19+
import { API_TEST_CONSTANTS } from '../../constants/API_TEST_CONSTANTS';
20+
import YAML from 'yaml';
21+
import { Logger } from '../../utils/Logger';
22+
import crypto from 'crypto';
23+
24+
suite('Dotnet devfile API test', function (): void {
25+
const devfilesRegistryHelper: DevfilesHelper = e2eContainer.get(CLASSES.DevfilesRegistryHelper);
26+
const kubernetesCommandLineToolsExecutor: KubernetesCommandLineToolsExecutor = e2eContainer.get(
27+
CLASSES.KubernetesCommandLineToolsExecutor
28+
);
29+
const devfileID: string = 'dotnet';
30+
const containerTerminal: ContainerTerminal = e2eContainer.get(CLASSES.ContainerTerminal);
31+
let devWorkspaceConfigurationHelper: DevWorkspaceConfigurationHelper;
32+
let devfileContext: DevfileContext;
33+
let devfileContent: string = '';
34+
let dwtName: string = '';
35+
36+
suiteSetup(`Prepare login ${BASE_TEST_CONSTANTS.TEST_ENVIRONMENT}`, function (): void {
37+
kubernetesCommandLineToolsExecutor.loginToOcp();
38+
});
39+
40+
test(`Create ${devfileID} workspace`, async function (): Promise<void> {
41+
const randomPref: string = crypto.randomBytes(4).toString('hex');
42+
kubernetesCommandLineToolsExecutor.namespace = API_TEST_CONSTANTS.TS_API_TEST_NAMESPACE || 'admin-devspaces';
43+
devfileContent = devfilesRegistryHelper.getDevfileContent(devfileID);
44+
const editorDevfileContent: string = devfilesRegistryHelper.obtainCheDevFileEditorFromCheConfigMap('editors-definitions');
45+
dwtName = YAML.parse(devfileContent).metadata.name;
46+
const uniqName: string = YAML.parse(devfileContent).metadata.name + randomPref;
47+
kubernetesCommandLineToolsExecutor.workspaceName = uniqName;
48+
49+
devWorkspaceConfigurationHelper = new DevWorkspaceConfigurationHelper({
50+
editorContent: editorDevfileContent,
51+
devfileContent: devfileContent
52+
});
53+
devfileContext = await devWorkspaceConfigurationHelper.generateDevfileContext();
54+
if (devfileContext.devWorkspace.metadata) {
55+
devfileContext.devWorkspace.metadata.name = uniqName;
56+
}
57+
const devWorkspaceConfigurationYamlString: string =
58+
devWorkspaceConfigurationHelper.getDevWorkspaceConfigurationYamlAsString(devfileContext);
59+
const output: ShellString = kubernetesCommandLineToolsExecutor.applyAndWaitDevWorkspace(devWorkspaceConfigurationYamlString);
60+
expect(output.stdout).contains('condition met');
61+
});
62+
63+
test('Check devfile commands', function (): void {
64+
const workdir: string = YAML.parse(devfileContent).commands[0].exec.workingDir;
65+
const containerName: string = YAML.parse(devfileContent).commands[0].exec.component;
66+
const updateCommandLine: string = YAML.parse(devfileContent).commands[0].exec.commandLine;
67+
Logger.info(`workdir from exec section of DevFile: ${workdir}`);
68+
Logger.info(`containerName from exec section of DevFile: ${containerName}`);
69+
70+
Logger.info('"Test \'update-dependencies\' command execution"');
71+
Logger.info(`commandLine from exec section of DevFile file: ${updateCommandLine}`);
72+
const runCommandInBash: string = `cd ${workdir} && ${updateCommandLine}`;
73+
const output: ShellString = containerTerminal.execInContainerCommand(runCommandInBash, containerName);
74+
expect(output.code).eqls(0);
75+
expect(output.stdout.trim()).contains('Restored /projects/dotnet-web-simple/web.csproj');
76+
77+
Logger.info('"Test \'build\' command execution"');
78+
const buildCommandLine: string = YAML.parse(devfileContent).commands[1].exec.commandLine;
79+
Logger.info(`commandLine from exec section of DevFile: ${buildCommandLine}`);
80+
const runCommandInBash2: string = `cd ${workdir} && ${buildCommandLine}`;
81+
const output2: ShellString = containerTerminal.execInContainerCommand(runCommandInBash2, containerName);
82+
expect(output2.code).eqls(0);
83+
expect(output2.stdout.trim()).contains('Build succeeded');
84+
85+
Logger.info('"Test \'run\' command execution"');
86+
const runCommandLine: string = YAML.parse(devfileContent).commands[2].exec.commandLine;
87+
Logger.info(`commandLine from exec section of DevFile: ${runCommandLine}`);
88+
const runCommandInBash3: string = `cd ${workdir} && sh -c "(${runCommandLine} > server.log 2>&1 &) && sleep 20s && exit"`;
89+
const output3: ShellString = containerTerminal.execInContainerCommand(runCommandInBash3, containerName);
90+
expect(output3.code).eqls(0);
91+
const logOutput: ShellString = containerTerminal.execInContainerCommand(`cat ${workdir}/server.log`, containerName);
92+
Logger.info(`Log output: ${logOutput.stdout}`);
93+
expect(logOutput.stdout.trim()).contains('Content root path: /projects/dotnet-web-simple');
94+
});
95+
96+
suiteTeardown('Delete DevWorkspace', function (): void {
97+
kubernetesCommandLineToolsExecutor.deleteDevWorkspace(dwtName);
98+
});
99+
});

tests/e2e/specs/api/GoDevFileAPI.spec.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ suite('Go devfile API test', function (): void {
3232
let devWorkspaceConfigurationHelper: DevWorkspaceConfigurationHelper;
3333
let devfileContext: DevfileContext;
3434
let devfileContent: string = '';
35+
let dwtName: string = '';
3536

3637
suiteSetup(`Prepare login ${BASE_TEST_CONSTANTS.TEST_ENVIRONMENT}`, function (): void {
3738
kubernetesCommandLineToolsExecutor.loginToOcp();
@@ -42,6 +43,7 @@ suite('Go devfile API test', function (): void {
4243
kubernetesCommandLineToolsExecutor.namespace = API_TEST_CONSTANTS.TS_API_TEST_NAMESPACE || 'admin-devspaces';
4344
devfileContent = devfilesRegistryHelper.getDevfileContent(devfileID);
4445
const editorDevfileContent: string = devfilesRegistryHelper.obtainCheDevFileEditorFromCheConfigMap('editors-definitions');
46+
dwtName = YAML.parse(devfileContent).metadata.name;
4547
const uniqName: string = YAML.parse(devfileContent).metadata.name + randomPref;
4648
kubernetesCommandLineToolsExecutor.workspaceName = uniqName;
4749

@@ -84,7 +86,7 @@ suite('Go devfile API test', function (): void {
8486
expect(logOutput.stdout.trim()).contains('Web server running on port 8080');
8587
});
8688

87-
suiteTeardown('Delete workspace', function (): void {
88-
kubernetesCommandLineToolsExecutor.deleteDevWorkspace();
89+
suiteTeardown('Delete DevWorkspace', function (): void {
90+
kubernetesCommandLineToolsExecutor.deleteDevWorkspace(dwtName);
8991
});
9092
});

tests/e2e/specs/api/PhpDevFileAPI.spec.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ suite('PHP devfile API test', function (): void {
3131
let devWorkspaceConfigurationHelper: DevWorkspaceConfigurationHelper;
3232
let devfileContext: DevfileContext;
3333
let devfileContent: string = '';
34+
let dwtName: string = '';
3435

3536
suiteSetup(`Prepare login ${BASE_TEST_CONSTANTS.TEST_ENVIRONMENT}`, function (): void {
3637
kubernetesCommandLineToolsExecutor.loginToOcp();
@@ -41,6 +42,7 @@ suite('PHP devfile API test', function (): void {
4142
kubernetesCommandLineToolsExecutor.namespace = API_TEST_CONSTANTS.TS_API_TEST_NAMESPACE || 'admin-devspaces';
4243
devfileContent = devfilesRegistryHelper.getDevfileContent(devfileID);
4344
const editorDevfileContent: string = devfilesRegistryHelper.obtainCheDevFileEditorFromCheConfigMap('editors-definitions');
45+
dwtName = YAML.parse(devfileContent).metadata.name;
4446
const uniqName: string = YAML.parse(devfileContent).metadata.name + randomPref;
4547
kubernetesCommandLineToolsExecutor.workspaceName = uniqName;
4648

@@ -70,7 +72,7 @@ suite('PHP devfile API test', function (): void {
7072
expect(output.stdout.trim()).contains('Hello, world!');
7173
});
7274

73-
suiteTeardown('Delete workspace', function (): void {
74-
kubernetesCommandLineToolsExecutor.deleteDevWorkspace();
75+
suiteTeardown('Delete DevWorkspace', function (): void {
76+
kubernetesCommandLineToolsExecutor.deleteDevWorkspace(dwtName);
7577
});
7678
});

tests/e2e/specs/api/PythonDevFileAPI.spec.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ suite('Python devfile API test', function (): void {
3232
let devWorkspaceConfigurationHelper: DevWorkspaceConfigurationHelper;
3333
let devfileContext: DevfileContext;
3434
let devfileContent: string = '';
35+
let dwtName: string = '';
3536

3637
suiteSetup(`Prepare login ${BASE_TEST_CONSTANTS.TEST_ENVIRONMENT}`, function (): void {
3738
kubernetesCommandLineToolsExecutor.loginToOcp();
@@ -42,6 +43,7 @@ suite('Python devfile API test', function (): void {
4243
kubernetesCommandLineToolsExecutor.namespace = API_TEST_CONSTANTS.TS_API_TEST_NAMESPACE || 'admin-devspaces';
4344
devfileContent = devfilesRegistryHelper.getDevfileContent(devfileID);
4445
const editorDevfileContent: string = devfilesRegistryHelper.obtainCheDevFileEditorFromCheConfigMap('editors-definitions');
46+
dwtName = YAML.parse(devfileContent).metadata.name;
4547
const uniqName: string = YAML.parse(devfileContent).metadata.name + randomPref;
4648
kubernetesCommandLineToolsExecutor.workspaceName = uniqName;
4749

@@ -71,7 +73,7 @@ suite('Python devfile API test', function (): void {
7173
expect(output.stdout.trim()).contains('Hello, world!');
7274
});
7375

74-
suiteTeardown('Delete workspace', function (): void {
75-
kubernetesCommandLineToolsExecutor.deleteDevWorkspace();
76+
suiteTeardown('Delete DevWorkspace', function (): void {
77+
kubernetesCommandLineToolsExecutor.deleteDevWorkspace(dwtName);
7678
});
7779
});

tests/e2e/utils/KubernetesCommandLineToolsExecutor.ts

+22-5
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,33 @@ export class KubernetesCommandLineToolsExecutor implements IKubernetesCommandLin
9494
return output.stderr ? output.stderr : output.stdout.replace('\n', '');
9595
}
9696

97-
deleteDevWorkspace(): void {
98-
Logger.debug(`${this.kubernetesCommandLineTool} - delete '${this.workspaceName}' workspace`);
97+
// used to delete when devWorkspace and devWorkspaceTemplate have the same names
98+
deleteDevWorkspace(): void;
99+
100+
// used to delete when devWorkspace and devWorkspaceTemplate have different names
101+
deleteDevWorkspace(dwTemplateName: string): void;
102+
103+
deleteDevWorkspace(dwTemplateName?: string): void {
104+
Logger.debug(`${this.kubernetesCommandLineTool} - delete '${this.workspaceName}' devWorkspace`);
99105

100106
this.shellExecutor.executeCommand(
101107
`${this.kubernetesCommandLineTool} patch dw ${this.workspaceName} -n ${this.namespace} -p '{ "metadata": { "finalizers": null }}' --type merge || true`
102108
);
103109
this.shellExecutor.executeCommand(`${this.kubernetesCommandLineTool} delete dw ${this.workspaceName} -n ${this.namespace} || true`);
104-
this.shellExecutor.executeCommand(
105-
`${this.kubernetesCommandLineTool} delete dwt ${BASE_TEST_CONSTANTS.TS_SELENIUM_EDITOR}-${this.workspaceName} -n ${this.namespace} || true`
106-
);
110+
111+
if (dwTemplateName === undefined) {
112+
Logger.debug(`${this.kubernetesCommandLineTool} - delete '${this.workspaceName}' devWorkspaceTemplate`);
113+
114+
this.shellExecutor.executeCommand(
115+
`${this.kubernetesCommandLineTool} delete dwt ${BASE_TEST_CONSTANTS.TS_SELENIUM_EDITOR}-${this.workspaceName} -n ${this.namespace} || true`
116+
);
117+
} else {
118+
Logger.debug(`${this.kubernetesCommandLineTool} - delete '${dwTemplateName}' devWorkspaceTemplate`);
119+
120+
this.shellExecutor.executeCommand(
121+
`${this.kubernetesCommandLineTool} delete dwt ${BASE_TEST_CONSTANTS.TS_SELENIUM_EDITOR}-${dwTemplateName} -n ${this.namespace} || true`
122+
);
123+
}
107124
}
108125

109126
applyAndWaitDevWorkspace(yamlConfiguration: string): ShellString {

0 commit comments

Comments
 (0)