Skip to content

Commit 7ba4b8c

Browse files
authored
[Test] Rework NodeJS MongoDB devfile API test (#23307)
* add NodeJSMongoDBAPI e2e test
1 parent 5eb56bf commit 7ba4b8c

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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('NodeJS MongoDB 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 = 'nodejs-mongodb';
30+
const containerTerminal: ContainerTerminal = e2eContainer.get(CLASSES.ContainerTerminal);
31+
let devWorkspaceConfigurationHelper: DevWorkspaceConfigurationHelper;
32+
let devfileContext: DevfileContext;
33+
let devfileContent: string = '';
34+
let devfileName: 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+
devfileName = YAML.parse(devfileContent).metadata.name;
46+
const uniqueName: string = YAML.parse(devfileContent).metadata.name + randomPref;
47+
kubernetesCommandLineToolsExecutor.workspaceName = uniqueName;
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 = uniqueName;
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 "Run the application" command', function (): void {
64+
const containerName: string = YAML.parse(devfileContent).commands[0].exec.component;
65+
66+
if (BASE_TEST_CONSTANTS.IS_CLUSTER_DISCONNECTED()) {
67+
Logger.info('Test cluster is disconnected. Ignore Self-Signed Certificate error.');
68+
const ignoreSelfSignedCertificateErrorCommand: string = 'npm config set strict-ssl false';
69+
const output: ShellString = containerTerminal.execInContainerCommand(ignoreSelfSignedCertificateErrorCommand, containerName);
70+
expect(output.code).eqls(0);
71+
}
72+
73+
const workdir: string = YAML.parse(devfileContent).commands[0].exec.workingDir;
74+
const commandLine: string = YAML.parse(devfileContent).commands[0].exec.commandLine;
75+
Logger.info(`workdir from exec section of DevWorkspace file: ${workdir}`);
76+
Logger.info(`commandLine from exec section of DevWorkspace file: ${commandLine}`);
77+
78+
let runCommandInBash: string = commandLine.replaceAll('$', '\\$'); // don't wipe out env. vars like "${PROJECTS_ROOT}"
79+
if (workdir !== undefined && workdir !== '') {
80+
runCommandInBash = `cd ${workdir} && ` + runCommandInBash;
81+
}
82+
83+
const output: ShellString = containerTerminal.execInContainerCommandWithTimeout(runCommandInBash, containerName);
84+
expect(output.code).eqls(124);
85+
86+
const outputText: string = output.stdout.trim();
87+
expect(outputText).contains('App listening on port 8080');
88+
});
89+
90+
suiteTeardown('Delete workspace', function (): void {
91+
kubernetesCommandLineToolsExecutor.deleteDevWorkspace(devfileName);
92+
});
93+
});

tests/e2e/utils/KubernetesCommandLineToolsExecutor.ts

+12
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ export class KubernetesCommandLineToolsExecutor implements IKubernetesCommandLin
132132
);
133133
}
134134

135+
execInContainerCommandWithTimeout(
136+
commandToExecute: string,
137+
container: string = KubernetesCommandLineToolsExecutor.container,
138+
timeout: string = '20'
139+
): ShellString {
140+
Logger.debug(`${this.kubernetesCommandLineTool}`);
141+
142+
return this.shellExecutor.executeCommand(
143+
` timeout ${timeout} ${this.kubernetesCommandLineTool} exec -i ${KubernetesCommandLineToolsExecutor.pod} -n ${this.namespace} -c ${container} -- sh -c '${commandToExecute}'`
144+
);
145+
}
146+
135147
applyYamlConfigurationAsStringOutput(yamlConfiguration: string): ShellString {
136148
Logger.debug(`${this.kubernetesCommandLineTool}`);
137149

0 commit comments

Comments
 (0)