|
| 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('Dotnet 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 = 'dotnet'; |
| 30 | + const containerTerminal: ContainerTerminal = e2eContainer.get(CLASSES.ContainerTerminal); |
| 31 | + let devWorkspaceConfigurationHelper: DevWorkspaceConfigurationHelper; |
| 32 | + let devfileContext: DevfileContext; |
| 33 | + let devfileContent: string = ''; |
| 34 | + let dwtName: 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 | + dwtName = YAML.parse(devfileContent).metadata.name; |
| 46 | + const uniqName: string = YAML.parse(devfileContent).metadata.name + randomPref; |
| 47 | + kubernetesCommandLineToolsExecutor.workspaceName = uniqName; |
| 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 = uniqName; |
| 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 devfile commands', function (): void { |
| 64 | + const workdir: string = YAML.parse(devfileContent).commands[0].exec.workingDir; |
| 65 | + const containerName: string = YAML.parse(devfileContent).commands[0].exec.component; |
| 66 | + const updateCommandLine: string = YAML.parse(devfileContent).commands[0].exec.commandLine; |
| 67 | + Logger.info(`workdir from exec section of DevFile: ${workdir}`); |
| 68 | + Logger.info(`containerName from exec section of DevFile: ${containerName}`); |
| 69 | + |
| 70 | + Logger.info('"Test \'update-dependencies\' command execution"'); |
| 71 | + Logger.info(`commandLine from exec section of DevFile file: ${updateCommandLine}`); |
| 72 | + const runCommandInBash: string = `cd ${workdir} && ${updateCommandLine}`; |
| 73 | + const output: ShellString = containerTerminal.execInContainerCommand(runCommandInBash, containerName); |
| 74 | + expect(output.code).eqls(0); |
| 75 | + expect(output.stdout.trim()).contains('Restored /projects/dotnet-web-simple/web.csproj'); |
| 76 | + |
| 77 | + Logger.info('"Test \'build\' command execution"'); |
| 78 | + const buildCommandLine: string = YAML.parse(devfileContent).commands[1].exec.commandLine; |
| 79 | + Logger.info(`commandLine from exec section of DevFile: ${buildCommandLine}`); |
| 80 | + const runCommandInBash2: string = `cd ${workdir} && ${buildCommandLine}`; |
| 81 | + const output2: ShellString = containerTerminal.execInContainerCommand(runCommandInBash2, containerName); |
| 82 | + expect(output2.code).eqls(0); |
| 83 | + expect(output2.stdout.trim()).contains('Build succeeded'); |
| 84 | + |
| 85 | + Logger.info('"Test \'run\' command execution"'); |
| 86 | + const runCommandLine: string = YAML.parse(devfileContent).commands[2].exec.commandLine; |
| 87 | + Logger.info(`commandLine from exec section of DevFile: ${runCommandLine}`); |
| 88 | + const runCommandInBash3: string = `cd ${workdir} && sh -c "(${runCommandLine} > server.log 2>&1 &) && sleep 20s && exit"`; |
| 89 | + const output3: ShellString = containerTerminal.execInContainerCommand(runCommandInBash3, containerName); |
| 90 | + expect(output3.code).eqls(0); |
| 91 | + const logOutput: ShellString = containerTerminal.execInContainerCommand(`cat ${workdir}/server.log`, containerName); |
| 92 | + Logger.info(`Log output: ${logOutput.stdout}`); |
| 93 | + expect(logOutput.stdout.trim()).contains('Content root path: /projects/dotnet-web-simple'); |
| 94 | + }); |
| 95 | + |
| 96 | + suiteTeardown('Delete DevWorkspace', function (): void { |
| 97 | + kubernetesCommandLineToolsExecutor.deleteDevWorkspace(dwtName); |
| 98 | + }); |
| 99 | +}); |
0 commit comments