Skip to content

Commit 76b99e7

Browse files
authored
Add 'GoDevFileAPI' E2E test (#23293)
1 parent b219dd1 commit 76b99e7

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
11+
import { BASE_TEST_CONSTANTS } from '../../constants/BASE_TEST_CONSTANTS';
12+
import { e2eContainer } from '../../configs/inversify.config';
13+
import { CLASSES } from '../../configs/inversify.types';
14+
import { DevfilesHelper } from '../../utils/DevfilesHelper';
15+
import { ContainerTerminal, KubernetesCommandLineToolsExecutor } from '../../utils/KubernetesCommandLineToolsExecutor';
16+
import { DevWorkspaceConfigurationHelper } from '../../utils/DevWorkspaceConfigurationHelper';
17+
import { DevfileContext } from '@eclipse-che/che-devworkspace-generator/lib/api/devfile-context';
18+
import { ShellString } from 'shelljs';
19+
import { expect } from 'chai';
20+
import { API_TEST_CONSTANTS } from '../../constants/API_TEST_CONSTANTS';
21+
import YAML from 'yaml';
22+
import { Logger } from '../../utils/Logger';
23+
import crypto from 'crypto';
24+
25+
suite('Go devfile API test', function (): void {
26+
const devfilesRegistryHelper: DevfilesHelper = e2eContainer.get(CLASSES.DevfilesRegistryHelper);
27+
const kubernetesCommandLineToolsExecutor: KubernetesCommandLineToolsExecutor = e2eContainer.get(
28+
CLASSES.KubernetesCommandLineToolsExecutor
29+
);
30+
const devfileID: string = 'go';
31+
const containerTerminal: ContainerTerminal = e2eContainer.get(CLASSES.ContainerTerminal);
32+
let devWorkspaceConfigurationHelper: DevWorkspaceConfigurationHelper;
33+
let devfileContext: DevfileContext;
34+
let devfileContent: 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+
const uniqName: string = YAML.parse(devfileContent).metadata.name + randomPref;
46+
kubernetesCommandLineToolsExecutor.workspaceName = uniqName;
47+
48+
devWorkspaceConfigurationHelper = new DevWorkspaceConfigurationHelper({
49+
editorContent: editorDevfileContent,
50+
devfileContent: devfileContent
51+
});
52+
devfileContext = await devWorkspaceConfigurationHelper.generateDevfileContext();
53+
if (devfileContext.devWorkspace.metadata) {
54+
devfileContext.devWorkspace.metadata.name = uniqName;
55+
}
56+
const devWorkspaceConfigurationYamlString: string =
57+
devWorkspaceConfigurationHelper.getDevWorkspaceConfigurationYamlAsString(devfileContext);
58+
const output: ShellString = kubernetesCommandLineToolsExecutor.applyAndWaitDevWorkspace(devWorkspaceConfigurationYamlString);
59+
expect(output.stdout).contains('condition met');
60+
});
61+
62+
test('Check devfile commands', function (): void {
63+
const workdir: string = YAML.parse(devfileContent).commands[0].exec.workingDir;
64+
const buildCommandLine: string = YAML.parse(devfileContent).commands[0].exec.commandLine;
65+
const containerName: string = YAML.parse(devfileContent).commands[0].exec.component;
66+
67+
Logger.info('"Test \'build\' command execution"');
68+
Logger.info(`workdir from exec section of DevFile: ${workdir}`);
69+
Logger.info(`commandLine from exec section of DevFile: ${buildCommandLine}`);
70+
const runCommandInBash: string = `cd ${workdir} && ${buildCommandLine}`;
71+
const output: ShellString = containerTerminal.execInContainerCommand(runCommandInBash, containerName);
72+
Logger.info(`Output stderr: ${output.stderr}`);
73+
expect(output.code).eqls(0);
74+
expect(output.stderr.trim()).contains('go: downloading');
75+
76+
Logger.info('"Test \'run\' command execution"');
77+
const runCommandLine: string = YAML.parse(devfileContent).commands[1].exec.commandLine;
78+
Logger.info(`commandLine from exec section of DevFile: ${runCommandLine}`);
79+
const runCommandInBash2: string = `cd ${workdir} && sh -c "(${runCommandLine} > server.log 2>&1 &) && exit"`;
80+
const output2: ShellString = containerTerminal.execInContainerCommand(runCommandInBash2, containerName);
81+
expect(output2.code).eqls(0);
82+
const logOutput: ShellString = containerTerminal.execInContainerCommand(`cat ${workdir}/server.log`, containerName);
83+
Logger.info(`Log output: ${logOutput.stdout}`);
84+
expect(logOutput.stdout.trim()).contains('Web server running on port 8080');
85+
});
86+
87+
suiteTeardown('Delete workspace', function (): void {
88+
kubernetesCommandLineToolsExecutor.deleteDevWorkspace();
89+
});
90+
});

0 commit comments

Comments
 (0)