Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fe96d6d

Browse files
committedMar 7, 2025
[Test] Added 'BuildContainerAPI' to API tests (eclipse-che#23377)
Signed-off-by: Martin Szuc <mszuc@redhat.com>
1 parent 45d4d3d commit fe96d6d

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
/** *******************************************************************
2+
* copyright (c) 2025 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 { expect } from 'chai';
11+
import { e2eContainer } from '../../configs/inversify.config';
12+
import { CLASSES, TYPES } from '../../configs/inversify.types';
13+
import { BASE_TEST_CONSTANTS } from '../../constants/BASE_TEST_CONSTANTS';
14+
import { WorkspaceHandlingTests } from '../../tests-library/WorkspaceHandlingTests';
15+
import { ProjectAndFileTests } from '../../tests-library/ProjectAndFileTests';
16+
import { LoginTests } from '../../tests-library/LoginTests';
17+
import { registerRunningWorkspace } from '../MochaHooks';
18+
import { BrowserTabsUtil } from '../../utils/BrowserTabsUtil';
19+
import { KubernetesCommandLineToolsExecutor } from '../../utils/KubernetesCommandLineToolsExecutor';
20+
import { ShellString } from 'shelljs';
21+
import { ITestWorkspaceUtil } from '../../utils/workspace/ITestWorkspaceUtil';
22+
import { Dashboard } from '../../pageobjects/dashboard/Dashboard';
23+
import { ShellExecutor } from '../../utils/ShellExecutor';
24+
25+
suite(`Test podman build container functionality ${BASE_TEST_CONSTANTS.TEST_ENVIRONMENT}`, function (): void {
26+
const projectAndFileTests: ProjectAndFileTests = e2eContainer.get(CLASSES.ProjectAndFileTests);
27+
const workspaceHandlingTests: WorkspaceHandlingTests = e2eContainer.get(CLASSES.WorkspaceHandlingTests);
28+
const loginTests: LoginTests = e2eContainer.get(CLASSES.LoginTests);
29+
const browserTabsUtil: BrowserTabsUtil = e2eContainer.get(CLASSES.BrowserTabsUtil);
30+
const testWorkspaceUtil: ITestWorkspaceUtil = e2eContainer.get(TYPES.WorkspaceUtil);
31+
const dashboard: Dashboard = e2eContainer.get(CLASSES.Dashboard);
32+
const shellExecutor: ShellExecutor = e2eContainer.get(CLASSES.ShellExecutor);
33+
34+
let kubernetesCommandLineToolsExecutor: KubernetesCommandLineToolsExecutor;
35+
let workspaceName: string = '';
36+
let originalBuildCapabilitiesSetting: string = '';
37+
let devSpacesNamespace: string = '';
38+
let cheClusterName: string = '';
39+
40+
const buildPushScript: string = `
41+
export ARCH=$(uname -m)
42+
export DATE=$(date +"%m%d%y")
43+
export USER=$(oc whoami)
44+
export TKN=$(oc whoami -t)
45+
export REG="image-registry.openshift-image-registry.svc:5000"
46+
export PROJECT=$(oc project -q)
47+
export IMG="\${REG}/\${PROJECT}/hello:\${DATE}"
48+
49+
# Create test directory and Dockerfile
50+
mkdir -p /projects/dockerfile-test
51+
cd /projects/dockerfile-test
52+
53+
cat > Dockerfile << EOF
54+
FROM registry.access.redhat.com/ubi8/ubi-minimal:latest
55+
RUN echo "Hello from Kubedock!" > /hello.txt
56+
CMD ["cat", "/hello.txt"]
57+
EOF
58+
59+
podman login --tls-verify=false --username "\${USER}" --password "\${TKN}" "\${REG}"
60+
podman build -t "\${IMG}" .
61+
podman push --tls-verify=false "\${IMG}"
62+
`;
63+
64+
const runTestScript: string = `
65+
export DATE=$(date +"%m%d%y")
66+
export REG="image-registry.openshift-image-registry.svc:5000"
67+
export PROJECT=$(oc project -q)
68+
export IMG="\${REG}/\${PROJECT}/hello:\${DATE}"
69+
70+
oc delete pod test-hello-pod --ignore-not-found
71+
oc run test-hello-pod --restart=Never --image="\${IMG}"
72+
73+
for i in {1..10}; do
74+
PHASE=$(oc get pod test-hello-pod -o jsonpath='{.status.phase}')
75+
if [[ "$PHASE" == "Succeeded" ]]; then
76+
break
77+
elif [[ "$PHASE" == "Failed" ]]; then
78+
oc describe pod test-hello-pod
79+
exit 1
80+
fi
81+
sleep 6
82+
done
83+
84+
oc logs test-hello-pod
85+
`;
86+
87+
suiteSetup('Setup DevSpaces with container build capabilities enabled', function (): void {
88+
kubernetesCommandLineToolsExecutor = e2eContainer.get(CLASSES.KubernetesCommandLineToolsExecutor);
89+
kubernetesCommandLineToolsExecutor.loginToOcp();
90+
91+
// get the namespace where DevSpaces is installed
92+
const getDevSpacesNamespaceCommand: string = 'oc get checluster --all-namespaces -o jsonpath="{.items[0].metadata.namespace}"';
93+
devSpacesNamespace = shellExecutor.executeCommand(getDevSpacesNamespaceCommand).stdout.trim();
94+
95+
// get the name of the CheCluster
96+
const getCheClusterNameCommand: string = `oc get checluster -n ${devSpacesNamespace} -o jsonpath='{.items[0].metadata.name}'`;
97+
cheClusterName = shellExecutor.executeCommand(getCheClusterNameCommand).stdout.trim();
98+
99+
// get the original value of disableContainerBuildCapabilities
100+
const getOriginalSettingCommand: string = `oc get checluster/${cheClusterName} -n ${devSpacesNamespace} -o jsonpath='{.spec.devEnvironments.disableContainerBuildCapabilities}'`;
101+
originalBuildCapabilitiesSetting = shellExecutor.executeCommand(getOriginalSettingCommand).stdout.trim();
102+
103+
// patch the CheCluster to enable container build capabilities
104+
const patchCommand: string = `oc patch checluster/${cheClusterName} -n ${devSpacesNamespace} --type=merge -p '{"spec":{"devEnvironments":{"disableContainerBuildCapabilities":false}}}'`;
105+
const patchResult: ShellString = shellExecutor.executeCommand(patchCommand);
106+
107+
expect(patchResult.code).to.equal(0, 'Failed to patch CheCluster to enable container build capabilities');
108+
});
109+
110+
suiteSetup('Login into DevSpaces', async function (): Promise<void> {
111+
await loginTests.loginIntoChe();
112+
});
113+
114+
test('Create and open new Empty Workspace', async function (): Promise<void> {
115+
await dashboard.waitPage();
116+
await workspaceHandlingTests.createAndOpenWorkspace('Empty Workspace');
117+
await workspaceHandlingTests.obtainWorkspaceNameFromStartingPage();
118+
workspaceName = WorkspaceHandlingTests.getWorkspaceName();
119+
expect(workspaceName, 'Workspace name was not detected').not.empty;
120+
registerRunningWorkspace(workspaceName);
121+
});
122+
123+
test('Wait for workspace readiness', async function (): Promise<void> {
124+
await projectAndFileTests.waitWorkspaceReadinessForCheCodeEditor();
125+
});
126+
127+
test('Build and push container image from workspace', function (): void {
128+
kubernetesCommandLineToolsExecutor = e2eContainer.get(CLASSES.KubernetesCommandLineToolsExecutor);
129+
kubernetesCommandLineToolsExecutor.workspaceName = workspaceName;
130+
kubernetesCommandLineToolsExecutor.loginToOcp();
131+
kubernetesCommandLineToolsExecutor.getPodAndContainerNames();
132+
kubernetesCommandLineToolsExecutor.execInContainerCommand(buildPushScript);
133+
});
134+
135+
test('Verify container image can be used in a pod', function (): void {
136+
const runTestScriptOutput: ShellString = kubernetesCommandLineToolsExecutor.execInContainerCommand(runTestScript);
137+
expect(runTestScriptOutput.stdout).to.include('Hello from Kubedock!', 'Expected "Hello from Kubedock!" message not found in logs');
138+
});
139+
140+
suiteTeardown('Open dashboard and close all other tabs', async function (): Promise<void> {
141+
await dashboard.openDashboard();
142+
await browserTabsUtil.closeAllTabsExceptCurrent();
143+
});
144+
145+
suiteTeardown('Stop and delete the workspace by API', async function (): Promise<void> {
146+
await testWorkspaceUtil.stopAndDeleteWorkspaceByName(workspaceName);
147+
});
148+
149+
suiteTeardown('Unregister running workspace', function (): void {
150+
registerRunningWorkspace('');
151+
});
152+
153+
suiteTeardown('Restore DevSpaces container build capabilities setting', function (): void {
154+
kubernetesCommandLineToolsExecutor = e2eContainer.get(CLASSES.KubernetesCommandLineToolsExecutor);
155+
kubernetesCommandLineToolsExecutor.loginToOcp();
156+
157+
// Default to false if value wasn't found
158+
const validSetting = ["true", "false"].includes(originalBuildCapabilitiesSetting)
159+
? originalBuildCapabilitiesSetting
160+
: "false";
161+
162+
const restorePatchCommand: string = `oc patch checluster/${cheClusterName} -n ${devSpacesNamespace} --type=merge -p '{"spec":{"devEnvironments":{"disableContainerBuildCapabilities":${validSetting}}}}'`;
163+
const restorePatchResult: ShellString = shellExecutor.executeCommand(restorePatchCommand);
164+
165+
expect(restorePatchResult.code).to.equal(0, 'Failed to restore CheCluster container build capabilities setting');
166+
});
167+
});

0 commit comments

Comments
 (0)
Please sign in to comment.