|
| 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('Cpp 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 = 'cpp'; |
| 31 | + const containerTerminal: ContainerTerminal = e2eContainer.get(CLASSES.ContainerTerminal); |
| 32 | + let devWorkspaceConfigurationHelper: DevWorkspaceConfigurationHelper; |
| 33 | + let devfileContext: DevfileContext; |
| 34 | + let devfileContent: string = ''; |
| 35 | + let dwtName: string = ''; |
| 36 | + const workDirPath: string = 'c-plus-plus/strings'; |
| 37 | + const fileName: string = 'knuth_morris_pratt.cpp'; |
| 38 | + const tasksJsonPath: string = 'c-plus-plus/.vscode/tasks.json'; |
| 39 | + |
| 40 | + suiteSetup(`Prepare login ${BASE_TEST_CONSTANTS.TEST_ENVIRONMENT}`, function (): void { |
| 41 | + kubernetesCommandLineToolsExecutor.loginToOcp(); |
| 42 | + }); |
| 43 | + |
| 44 | + test(`Create ${devfileID} workspace`, async function (): Promise<void> { |
| 45 | + const randomPref: string = crypto.randomBytes(4).toString('hex'); |
| 46 | + kubernetesCommandLineToolsExecutor.namespace = API_TEST_CONSTANTS.TS_API_TEST_NAMESPACE || 'admin-devspaces'; |
| 47 | + devfileContent = devfilesRegistryHelper.getDevfileContent(devfileID); |
| 48 | + const editorDevfileContent: string = devfilesRegistryHelper.obtainCheDevFileEditorFromCheConfigMap('editors-definitions'); |
| 49 | + dwtName = YAML.parse(devfileContent).metadata.name; |
| 50 | + const uniqName: string = YAML.parse(devfileContent).metadata.name + randomPref; |
| 51 | + kubernetesCommandLineToolsExecutor.workspaceName = uniqName; |
| 52 | + |
| 53 | + devWorkspaceConfigurationHelper = new DevWorkspaceConfigurationHelper({ |
| 54 | + editorContent: editorDevfileContent, |
| 55 | + devfileContent: devfileContent |
| 56 | + }); |
| 57 | + devfileContext = await devWorkspaceConfigurationHelper.generateDevfileContext(); |
| 58 | + if (devfileContext.devWorkspace.metadata) { |
| 59 | + devfileContext.devWorkspace.metadata.name = uniqName; |
| 60 | + } |
| 61 | + const devWorkspaceConfigurationYamlString: string = |
| 62 | + devWorkspaceConfigurationHelper.getDevWorkspaceConfigurationYamlAsString(devfileContext); |
| 63 | + const output: ShellString = kubernetesCommandLineToolsExecutor.applyAndWaitDevWorkspace(devWorkspaceConfigurationYamlString); |
| 64 | + expect(output.stdout).contains('condition met'); |
| 65 | + }); |
| 66 | + |
| 67 | + test('Check commands', function (): void { |
| 68 | + let output: ShellString; |
| 69 | + const toolsComponent: any = YAML.parse(devfileContent).components.find((component: any): boolean => component.name === 'tools'); |
| 70 | + const containerName: string = toolsComponent ? toolsComponent.name : 'Component not found'; |
| 71 | + Logger.info(`container from components section of Devfile:: ${containerName}`); |
| 72 | + const tasksJsonContent: string = containerTerminal.execInContainerCommand(`cat ${tasksJsonPath}`, containerName).stdout; |
| 73 | + const parsedJson: any = YAML.parse(tasksJsonContent); |
| 74 | + |
| 75 | + Logger.info('"Check \'build current algorithm\' command"'); |
| 76 | + const buildTaskCommand: string = parsedJson.tasks[0].command; |
| 77 | + const buildCommand: string = buildTaskCommand.replaceAll('${file}', fileName).replaceAll(/'/g, '"'); |
| 78 | + Logger.info(`commandLine from tasks.json file of project: ${buildCommand}`); |
| 79 | + const runCommandInBash: string = `cd ${workDirPath} && ${buildCommand}`; |
| 80 | + output = containerTerminal.execInContainerCommand(runCommandInBash, containerName); |
| 81 | + expect(output.code).eqls(0); |
| 82 | + expect(output.stdout.trim()).contains('Build complete'); |
| 83 | + |
| 84 | + Logger.info('"Check \'run current algorithm\' command"'); |
| 85 | + const runTaskCommand: string = parsedJson.tasks[1].command; |
| 86 | + const runCommand: string = runTaskCommand.replaceAll('${file}', fileName).replaceAll(/'/g, '"'); |
| 87 | + Logger.info(`commandLine from tasks.json file of project: ${runCommand}`); |
| 88 | + const runCommandInBash2: string = `cd ${workDirPath} && ${runCommand}`; |
| 89 | + output = containerTerminal.execInContainerCommand(runCommandInBash2, containerName); |
| 90 | + expect(output.code).eqls(0); |
| 91 | + expect(output.stdout.trim()).contains('Found'); |
| 92 | + }); |
| 93 | + |
| 94 | + suiteTeardown('Delete workspace', function (): void { |
| 95 | + kubernetesCommandLineToolsExecutor.deleteDevWorkspace(dwtName); |
| 96 | + }); |
| 97 | +}); |
0 commit comments