From 3074f2b373d86ae962b9095eb9c96fbc823f177b Mon Sep 17 00:00:00 2001 From: Sergey Skorik Date: Wed, 23 Oct 2024 12:38:17 +0300 Subject: [PATCH] [Test] Fix Dev Spaces "User secrets in workspace" test to avoid using an absolute devfile registry (#23203) * add new CheckUserEnvsTest e2e test --- .../UserSecretsInWorkspace.spec.ts | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 tests/e2e/specs/miscellaneous/UserSecretsInWorkspace.spec.ts diff --git a/tests/e2e/specs/miscellaneous/UserSecretsInWorkspace.spec.ts b/tests/e2e/specs/miscellaneous/UserSecretsInWorkspace.spec.ts new file mode 100644 index 00000000000..3ec0158aaff --- /dev/null +++ b/tests/e2e/specs/miscellaneous/UserSecretsInWorkspace.spec.ts @@ -0,0 +1,97 @@ +/** ******************************************************************* + * copyright (c) 2020-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 { e2eContainer } from '../../configs/inversify.config'; +import { CLASSES, TYPES } from '../../configs/inversify.types'; +import { WorkspaceHandlingTests } from '../../tests-library/WorkspaceHandlingTests'; +import { ProjectAndFileTests } from '../../tests-library/ProjectAndFileTests'; +import { LoginTests } from '../../tests-library/LoginTests'; +import { registerRunningWorkspace } from '../MochaHooks'; +import { KubernetesCommandLineToolsExecutor } from '../../utils/KubernetesCommandLineToolsExecutor'; +import { ShellExecutor } from '../../utils/ShellExecutor'; +import { ITestWorkspaceUtil } from '../../utils/workspace/ITestWorkspaceUtil'; +import { ShellString } from 'shelljs'; +import { expect } from 'chai'; +import { Dashboard } from '../../pageobjects/dashboard/Dashboard'; +import { BrowserTabsUtil } from '../../utils/BrowserTabsUtil'; +import { OAUTH_CONSTANTS } from '../../constants/OAUTH_CONSTANTS'; + +suite('"User secrets in workspace" test', function (): void { + const workspaceHandlingTests: WorkspaceHandlingTests = e2eContainer.get(CLASSES.WorkspaceHandlingTests); + const projectAndFileTests: ProjectAndFileTests = e2eContainer.get(CLASSES.ProjectAndFileTests); + const loginTests: LoginTests = e2eContainer.get(CLASSES.LoginTests); + const dashboard: Dashboard = e2eContainer.get(CLASSES.Dashboard); + const browserTabsUtil: BrowserTabsUtil = e2eContainer.get(CLASSES.BrowserTabsUtil); + + let kubernetesCommandLineToolsExecutor: KubernetesCommandLineToolsExecutor = e2eContainer.get( + CLASSES.KubernetesCommandLineToolsExecutor + ); + const shellExecutor: ShellExecutor = e2eContainer.get(CLASSES.ShellExecutor); + const testWorkspaceUtil: ITestWorkspaceUtil = e2eContainer.get(TYPES.WorkspaceUtil); + const userProject: string = OAUTH_CONSTANTS.TS_SELENIUM_OCP_USERNAME + `-devspaces`; + const stackName: string = 'Empty Workspace'; + + const mountEnvCommand: string = `echo ' +apiVersion: v1 +kind: Secret +metadata: + name: mount + labels: + controller.devfile.io/mount-to-devworkspace: "true" + controller.devfile.io/watch-secret: "true" + annotations: + controller.devfile.io/mount-as: env +data: + mykey: bXl2YWx1ZQ== +type: Opaque +' | oc apply -f -`; + + suiteSetup('Mount kubernetes secret to the DevWorkspace containers', function (): void { + kubernetesCommandLineToolsExecutor.loginToOcp(); + shellExecutor.executeCommand('oc project ' + userProject); + shellExecutor.executeCommand('oc delete secret mount || true'); + + ShellString(shellExecutor.executeCommand(mountEnvCommand)); + }); + + suiteSetup('Login', async function (): Promise { + await loginTests.loginIntoChe(); + }); + + test(`Create and open new workspace, stack:${stackName} and wait workspace readiness`, async function (): Promise { + await workspaceHandlingTests.createAndOpenWorkspace(stackName); + await workspaceHandlingTests.obtainWorkspaceNameFromStartingPage(); + + registerRunningWorkspace(WorkspaceHandlingTests.getWorkspaceName()); + await projectAndFileTests.waitWorkspaceReadinessForCheCodeEditor(); + }); + + test('Check that mounted secret exists in envs list', async function (): Promise { + kubernetesCommandLineToolsExecutor = e2eContainer.get(CLASSES.KubernetesCommandLineToolsExecutor); + kubernetesCommandLineToolsExecutor.workspaceName = WorkspaceHandlingTests.getWorkspaceName(); + kubernetesCommandLineToolsExecutor.loginToOcp(); + kubernetesCommandLineToolsExecutor.getPodAndContainerNames(); + + const output: ShellString = kubernetesCommandLineToolsExecutor.execInContainerCommand('env | grep mykey'); + expect(output, 'Environment variable "mykey" from the user secret is not found').contains('mykey=myvalue'); + }); + + suiteTeardown('Open dashboard and close all other tabs', async function (): Promise { + await dashboard.openDashboard(); + await browserTabsUtil.closeAllTabsExceptCurrent(); + }); + + suiteTeardown('Stop and delete the workspace by API', async function (): Promise { + await testWorkspaceUtil.deleteWorkspaceByName(WorkspaceHandlingTests.getWorkspaceName()); + }); + + suiteTeardown('Unregister running workspace', function (): void { + registerRunningWorkspace(''); + }); +});