From 2e0123d439dfb745ba366ff68a3c8ddfdd1cb387 Mon Sep 17 00:00:00 2001 From: Aleksandr Shmaraiev Date: Mon, 16 Dec 2024 19:16:48 +0200 Subject: [PATCH 1/2] Add 'CppDevFileAPI' E2E test --- tests/e2e/specs/api/CppDevFileAPI.spec.ts | 86 +++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 tests/e2e/specs/api/CppDevFileAPI.spec.ts diff --git a/tests/e2e/specs/api/CppDevFileAPI.spec.ts b/tests/e2e/specs/api/CppDevFileAPI.spec.ts new file mode 100644 index 00000000000..26cf1f0c2f4 --- /dev/null +++ b/tests/e2e/specs/api/CppDevFileAPI.spec.ts @@ -0,0 +1,86 @@ +/** ******************************************************************* + * copyright (c) 2024 Red Hat, Inc. + * + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + **********************************************************************/ + +import { BASE_TEST_CONSTANTS } from '../../constants/BASE_TEST_CONSTANTS'; +import { e2eContainer } from '../../configs/inversify.config'; +import { CLASSES } from '../../configs/inversify.types'; +import { DevfilesHelper } from '../../utils/DevfilesHelper'; +import { ContainerTerminal, KubernetesCommandLineToolsExecutor } from '../../utils/KubernetesCommandLineToolsExecutor'; +import { DevWorkspaceConfigurationHelper } from '../../utils/DevWorkspaceConfigurationHelper'; +import { DevfileContext } from '@eclipse-che/che-devworkspace-generator/lib/api/devfile-context'; +import { ShellString } from 'shelljs'; +import { expect } from 'chai'; +import { API_TEST_CONSTANTS } from '../../constants/API_TEST_CONSTANTS'; +import YAML from 'yaml'; +import { Logger } from '../../utils/Logger'; +import crypto from 'crypto'; + +suite('Cpp devfile API test', function (): void { + const devfilesRegistryHelper: DevfilesHelper = e2eContainer.get(CLASSES.DevfilesRegistryHelper); + const kubernetesCommandLineToolsExecutor: KubernetesCommandLineToolsExecutor = e2eContainer.get( + CLASSES.KubernetesCommandLineToolsExecutor + ); + const devfileID: string = 'cpp'; + const containerTerminal: ContainerTerminal = e2eContainer.get(CLASSES.ContainerTerminal); + let devWorkspaceConfigurationHelper: DevWorkspaceConfigurationHelper; + let devfileContext: DevfileContext; + let devfileContent: string = ''; + const workDirPath: string = 'c-plus-plus/strings'; + const buildCommand: string = 'rm -f bin.out && g++ -g "knuth_morris_pratt.cpp" -o bin.out && echo "Build complete"'; + const runCommand: string = './bin.out'; + + suiteSetup(`Prepare login ${BASE_TEST_CONSTANTS.TEST_ENVIRONMENT}`, function (): void { + kubernetesCommandLineToolsExecutor.loginToOcp(); + }); + + test(`Create ${devfileID} workspace`, async function (): Promise { + const randomPref: string = crypto.randomBytes(4).toString('hex'); + kubernetesCommandLineToolsExecutor.namespace = API_TEST_CONSTANTS.TS_API_TEST_NAMESPACE || 'admin-devspaces'; + devfileContent = devfilesRegistryHelper.getDevfileContent(devfileID); + const editorDevfileContent: string = devfilesRegistryHelper.obtainCheDevFileEditorFromCheConfigMap('editors-definitions'); + const uniqName: string = YAML.parse(devfileContent).metadata.name + randomPref; + kubernetesCommandLineToolsExecutor.workspaceName = uniqName; + + devWorkspaceConfigurationHelper = new DevWorkspaceConfigurationHelper({ + editorContent: editorDevfileContent, + devfileContent: devfileContent + }); + devfileContext = await devWorkspaceConfigurationHelper.generateDevfileContext(); + if (devfileContext.devWorkspace.metadata) { + devfileContext.devWorkspace.metadata.name = uniqName; + } + const devWorkspaceConfigurationYamlString: string = + devWorkspaceConfigurationHelper.getDevWorkspaceConfigurationYamlAsString(devfileContext); + const output: ShellString = kubernetesCommandLineToolsExecutor.applyAndWaitDevWorkspace(devWorkspaceConfigurationYamlString); + expect(output.stdout).contains('condition met'); + }); + + test('Check commands', function (): void { + let runCommandInBash: string; + let output: ShellString; + const toolsComponent: any = YAML.parse(devfileContent).components.find((component: any): boolean => component.name === 'tools'); + const containerName: string = toolsComponent ? toolsComponent.name : 'Component not found'; + Logger.info(`container from components section of Devfile:: ${containerName}`); + runCommandInBash = `cd ${workDirPath} && ${buildCommand}`; + Logger.info('Check build command'); + output = containerTerminal.execInContainerCommand(runCommandInBash, containerName); + expect(output.code).eqls(0); + expect(output.stdout.trim()).contains('Build complete'); + Logger.info('Check run command'); + runCommandInBash = `cd ${workDirPath} && ${runCommand}`; + output = containerTerminal.execInContainerCommand(runCommandInBash, containerName); + expect(output.code).eqls(0); + expect(output.stdout.trim()).contains('Found'); + }); + + suiteTeardown('Delete workspace', function (): void { + kubernetesCommandLineToolsExecutor.deleteDevWorkspace(); + }); +}); From 1de04e956ea5d589ad3f0db51f2abc5834daa0ab Mon Sep 17 00:00:00 2001 From: Aleksandr Shmaraiev Date: Thu, 26 Dec 2024 10:51:18 +0200 Subject: [PATCH 2/2] Implement to extracting the commands from 'tasks.json' file --- tests/e2e/specs/api/CppDevFileAPI.spec.ts | 29 ++++++++++++++++------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/tests/e2e/specs/api/CppDevFileAPI.spec.ts b/tests/e2e/specs/api/CppDevFileAPI.spec.ts index 26cf1f0c2f4..5d747ba3afe 100644 --- a/tests/e2e/specs/api/CppDevFileAPI.spec.ts +++ b/tests/e2e/specs/api/CppDevFileAPI.spec.ts @@ -32,9 +32,10 @@ suite('Cpp devfile API test', function (): void { let devWorkspaceConfigurationHelper: DevWorkspaceConfigurationHelper; let devfileContext: DevfileContext; let devfileContent: string = ''; + let dwtName: string = ''; const workDirPath: string = 'c-plus-plus/strings'; - const buildCommand: string = 'rm -f bin.out && g++ -g "knuth_morris_pratt.cpp" -o bin.out && echo "Build complete"'; - const runCommand: string = './bin.out'; + const fileName: string = 'knuth_morris_pratt.cpp'; + const tasksJsonPath: string = 'c-plus-plus/.vscode/tasks.json'; suiteSetup(`Prepare login ${BASE_TEST_CONSTANTS.TEST_ENVIRONMENT}`, function (): void { kubernetesCommandLineToolsExecutor.loginToOcp(); @@ -45,6 +46,7 @@ suite('Cpp devfile API test', function (): void { kubernetesCommandLineToolsExecutor.namespace = API_TEST_CONSTANTS.TS_API_TEST_NAMESPACE || 'admin-devspaces'; devfileContent = devfilesRegistryHelper.getDevfileContent(devfileID); const editorDevfileContent: string = devfilesRegistryHelper.obtainCheDevFileEditorFromCheConfigMap('editors-definitions'); + dwtName = YAML.parse(devfileContent).metadata.name; const uniqName: string = YAML.parse(devfileContent).metadata.name + randomPref; kubernetesCommandLineToolsExecutor.workspaceName = uniqName; @@ -63,24 +65,33 @@ suite('Cpp devfile API test', function (): void { }); test('Check commands', function (): void { - let runCommandInBash: string; let output: ShellString; const toolsComponent: any = YAML.parse(devfileContent).components.find((component: any): boolean => component.name === 'tools'); const containerName: string = toolsComponent ? toolsComponent.name : 'Component not found'; Logger.info(`container from components section of Devfile:: ${containerName}`); - runCommandInBash = `cd ${workDirPath} && ${buildCommand}`; - Logger.info('Check build command'); + const tasksJsonContent: string = containerTerminal.execInContainerCommand(`cat ${tasksJsonPath}`, containerName).stdout; + const parsedJson: any = YAML.parse(tasksJsonContent); + + Logger.info('"Check \'build current algorithm\' command"'); + const buildTaskCommand: string = parsedJson.tasks[0].command; + const buildCommand: string = buildTaskCommand.replaceAll('${file}', fileName).replaceAll(/'/g, '"'); + Logger.info(`commandLine from tasks.json file of project: ${buildCommand}`); + const runCommandInBash: string = `cd ${workDirPath} && ${buildCommand}`; output = containerTerminal.execInContainerCommand(runCommandInBash, containerName); expect(output.code).eqls(0); expect(output.stdout.trim()).contains('Build complete'); - Logger.info('Check run command'); - runCommandInBash = `cd ${workDirPath} && ${runCommand}`; - output = containerTerminal.execInContainerCommand(runCommandInBash, containerName); + + Logger.info('"Check \'run current algorithm\' command"'); + const runTaskCommand: string = parsedJson.tasks[1].command; + const runCommand: string = runTaskCommand.replaceAll('${file}', fileName).replaceAll(/'/g, '"'); + Logger.info(`commandLine from tasks.json file of project: ${runCommand}`); + const runCommandInBash2: string = `cd ${workDirPath} && ${runCommand}`; + output = containerTerminal.execInContainerCommand(runCommandInBash2, containerName); expect(output.code).eqls(0); expect(output.stdout.trim()).contains('Found'); }); suiteTeardown('Delete workspace', function (): void { - kubernetesCommandLineToolsExecutor.deleteDevWorkspace(); + kubernetesCommandLineToolsExecutor.deleteDevWorkspace(dwtName); }); });