Skip to content

Commit 04d0b55

Browse files
authored
Add JBossEapDevFileAPI test (#23305)
* Add JBossEapDevFileAPI test Signed-off-by: Dmytro Nochevnov <dnochevn@redhat.com> * Get devfile configuration from file; add "-t" to oc exec command Signed-off-by: Dmytro Nochevnov <dnochevn@redhat.com> --------- Signed-off-by: Dmytro Nochevnov <dnochevn@redhat.com>
1 parent 7ba4b8c commit 04d0b55

File tree

2 files changed

+161
-2
lines changed

2 files changed

+161
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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('JBoss EAP 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 = 'java-jboss-eap';
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 "package" 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. Init Java Truststore...');
68+
const initJavaTruststoreCommand: string =
69+
'cp /home/user/init-java-truststore.sh /tmp && chmod +x /tmp/init-java-truststore.sh && /tmp/init-java-truststore.sh';
70+
const output: ShellString = containerTerminal.execInContainerCommand(initJavaTruststoreCommand, containerName);
71+
expect(output.code).eqls(0);
72+
}
73+
74+
const workdir: string = YAML.parse(devfileContent).commands[0].exec.workingDir;
75+
const commandLine: string = YAML.parse(devfileContent).commands[0].exec.commandLine;
76+
Logger.info(`workdir from exec section of DevWorkspace file: ${workdir}`);
77+
Logger.info(`commandLine from exec section of DevWorkspace file: ${commandLine}`);
78+
79+
let runCommandInBash: string = commandLine.replaceAll('$', '\\$'); // don't wipe out env. vars like "${PROJECTS_ROOT}"
80+
if (workdir !== undefined && workdir !== '') {
81+
runCommandInBash = `cd ${workdir} && ` + runCommandInBash;
82+
}
83+
84+
const output: ShellString = containerTerminal.execInContainerCommand(runCommandInBash, containerName);
85+
expect(output.code).eqls(0);
86+
87+
const outputText: string = output.stdout.trim();
88+
expect(outputText).contains('BUILD SUCCESS');
89+
});
90+
91+
test('Check "run" command', function (): void {
92+
const workdir: string = YAML.parse(devfileContent).commands[1].exec.workingDir;
93+
const commandLine: string = YAML.parse(devfileContent).commands[1].exec.commandLine;
94+
const containerName: string = YAML.parse(devfileContent).commands[1].exec.component;
95+
Logger.info(`workdir from exec section of DevWorkspace file: ${workdir}`);
96+
Logger.info(`commandLine from exec section of DevWorkspace file: ${commandLine}`);
97+
98+
let runCommandInBash: string = commandLine.replaceAll('$', '\\$'); // don't wipe out env. vars like "${PROJECTS_ROOT}"
99+
if (workdir !== undefined && workdir !== '') {
100+
runCommandInBash = `cd ${workdir} && sh -c "(` + runCommandInBash + ' > command.log 2>&1 &) && sleep 15s && exit"';
101+
}
102+
103+
let commandOutput: ShellString = containerTerminal.execInContainerCommand(runCommandInBash, containerName);
104+
expect(commandOutput.code).eqls(0);
105+
106+
commandOutput = containerTerminal.execInContainerCommand(`cat ${workdir}/command.log`, containerName);
107+
const commandLog: string = commandOutput.stdout.trim();
108+
109+
expect(commandLog).contains('Server configuration file in use: standalone.xml');
110+
});
111+
112+
test('Check "shutdown" command', function (): void {
113+
const workdir: string = YAML.parse(devfileContent).commands[3].exec.workingDir;
114+
const commandLine: string = YAML.parse(devfileContent).commands[3].exec.commandLine;
115+
const containerName: string = YAML.parse(devfileContent).commands[3].exec.component;
116+
Logger.info(`workdir from exec section of DevWorkspace file: ${workdir}`);
117+
Logger.info(`commandLine from exec section of DevWorkspace file: ${commandLine}`);
118+
119+
let runCommandInBash: string = commandLine.replaceAll('$', '\\$'); // don't wipe out env. vars like "${PROJECTS_ROOT}"
120+
if (workdir !== undefined && workdir !== '') {
121+
runCommandInBash = `cd ${workdir} && ` + runCommandInBash;
122+
}
123+
124+
const output: ShellString = containerTerminal.execInContainerCommand(runCommandInBash, containerName);
125+
expect(output.code).eqls(0);
126+
});
127+
128+
test('Check "debug" command', function (): void {
129+
const workdir: string = YAML.parse(devfileContent).commands[2].exec.workingDir;
130+
const commandLine: string = YAML.parse(devfileContent).commands[2].exec.commandLine;
131+
const containerName: string = YAML.parse(devfileContent).commands[2].exec.component;
132+
Logger.info(`workdir from exec section of DevWorkspace file: ${workdir}`);
133+
Logger.info(`commandLine from exec section of DevWorkspace file: ${commandLine}`);
134+
135+
let runCommandInBash: string = commandLine.replaceAll('$', '\\$'); // don't wipe out env. vars like "${PROJECTS_ROOT}"
136+
if (workdir !== undefined && workdir !== '') {
137+
runCommandInBash = `cd ${workdir} && sh -c "(` + runCommandInBash + ' > command.log 2>&1 &) && sleep 15s && exit"';
138+
}
139+
140+
let commandOutput: ShellString = containerTerminal.execInContainerCommand(runCommandInBash, containerName);
141+
expect(commandOutput.code).eqls(0);
142+
143+
commandOutput = containerTerminal.execInContainerCommand(`cat ${workdir}/command.log`, containerName);
144+
const commandLog: string = commandOutput.stdout.trim();
145+
146+
expect(commandLog).contains('Listening for transport dt_socket at address: 5005');
147+
expect(commandLog).contains('Server configuration file in use: standalone.xml');
148+
});
149+
150+
suiteTeardown('Delete workspace', function (): void {
151+
kubernetesCommandLineToolsExecutor.deleteDevWorkspace(devfileName);
152+
});
153+
});

tests/e2e/utils/KubernetesCommandLineToolsExecutor.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { OAUTH_CONSTANTS } from '../constants/OAUTH_CONSTANTS';
1717
import { IKubernetesCommandLineToolsExecutor } from './IKubernetesCommandLineToolsExecutor';
1818
import { inject, injectable } from 'inversify';
1919
import { CLASSES } from '../configs/inversify.types';
20+
import * as fs from 'fs';
2021

2122
@injectable()
2223
export class KubernetesCommandLineToolsExecutor implements IKubernetesCommandLineToolsExecutor {
@@ -128,7 +129,7 @@ export class KubernetesCommandLineToolsExecutor implements IKubernetesCommandLin
128129
Logger.debug(`${this.kubernetesCommandLineTool}`);
129130

130131
return this.shellExecutor.executeCommand(
131-
`${this.kubernetesCommandLineTool} exec -i ${KubernetesCommandLineToolsExecutor.pod} -n ${this.namespace} -c ${container} -- sh -c '${commandToExecute}'`
132+
`${this.kubernetesCommandLineTool} exec -i -t ${KubernetesCommandLineToolsExecutor.pod} -n ${this.namespace} -c ${container} -- sh -c '${commandToExecute}'`
132133
);
133134
}
134135

@@ -147,8 +148,13 @@ export class KubernetesCommandLineToolsExecutor implements IKubernetesCommandLin
147148
applyYamlConfigurationAsStringOutput(yamlConfiguration: string): ShellString {
148149
Logger.debug(`${this.kubernetesCommandLineTool}`);
149150

151+
// write yaml configuration to the devfile file
152+
const randomSuffix: number = Math.floor(Math.random() * 10000);
153+
const devfileYamlPath: string = `/tmp/devfile-${randomSuffix}.yaml`;
154+
fs.writeFileSync(devfileYamlPath, yamlConfiguration);
155+
150156
return this.shellExecutor.executeCommand(
151-
`cat <<EOF | ${this.kubernetesCommandLineTool} apply -n ${this.namespace} -f - \n` + yamlConfiguration + '\n' + 'EOF'
157+
`${this.kubernetesCommandLineTool} apply -n ${this.namespace} -f ${devfileYamlPath} && rm -f ${devfileYamlPath}`
152158
);
153159
}
154160

0 commit comments

Comments
 (0)