Skip to content

Commit

Permalink
Refactor test code
Browse files Browse the repository at this point in the history
  • Loading branch information
jfaltermeier committed Dec 19, 2024
1 parent d86d7d6 commit 27e2119
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 75 deletions.
3 changes: 2 additions & 1 deletion node/configs/license-check-exclusions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"npm/npmjs/-/landing-page/0.1.0": "Theia Cloud Internal Package",
"npm/npmjs/-/testing-page/0.1.0": "Theia Cloud Internal Package"
"npm/npmjs/-/testing-page/0.1.0": "Theia Cloud Internal Package",
"npm/npmjs/-/e2e-tests/0.12.0-next": "Theia Cloud Internal Package"
}
6 changes: 4 additions & 2 deletions node/e2e-tests/src/constats.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const namespace = 'theiacloud';
export const resourceGroup = 'theia.cloud';
export const resourceVersion = 'v1beta8';
export const resourcePlural = 'sessions';
export const sessionVersion = 'v1beta8';
export const sessionPlural = 'sessions';
export const workspaceVersion = 'v1beta5';
export const workspacePlural = 'workspaces';
44 changes: 44 additions & 0 deletions node/e2e-tests/src/k8s.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { KubeConfig, CustomObjectsApi } from '@kubernetes/client-node';
import { namespace, resourceGroup, sessionPlural, sessionVersion, workspacePlural, workspaceVersion } from './constats';

const kc = new KubeConfig();
kc.loadFromDefault();
export const k8sApi = kc.makeApiClient(CustomObjectsApi);

export async function deleteAllSessions(): Promise<void> {
const sessions: any = await k8sApi.listNamespacedCustomObject(
resourceGroup,
sessionVersion,
namespace,
sessionPlural
);

for (const resource of sessions.body.items) {
await k8sApi.deleteNamespacedCustomObject(
resourceGroup,
sessionVersion,
namespace,
sessionPlural,
resource.metadata.name
);
}
}

export async function deleteAllWorkspaces(): Promise<void> {
const sessions: any = await k8sApi.listNamespacedCustomObject(
resourceGroup,
workspaceVersion,
namespace,
workspacePlural
);

for (const resource of sessions.body.items) {
await k8sApi.deleteNamespacedCustomObject(
resourceGroup,
workspaceVersion,
namespace,
workspacePlural,
resource.metadata.name
);
}
}
26 changes: 3 additions & 23 deletions node/e2e-tests/src/tests/login.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
import { expect, test } from '@playwright/test';
import { KubeConfig, CustomObjectsApi } from '@kubernetes/client-node';
import { namespace, resourceGroup, resourcePlural, resourceVersion } from '../constats';

const kc = new KubeConfig();
kc.loadFromDefault();
const k8sApi = kc.makeApiClient(CustomObjectsApi);
import { deleteAllSessions, deleteAllWorkspaces } from '../k8s';

test.describe('Login', () => {
test.beforeEach(async () => {
/* delete all sessions */
const resources: any = await k8sApi.listNamespacedCustomObject(
resourceGroup,
resourceVersion,
namespace,
resourcePlural
);

for (const resource of resources.body.items) {
await k8sApi.deleteNamespacedCustomObject(
resourceGroup,
resourceVersion,
namespace,
resourcePlural,
resource.metadata.name
);
}
deleteAllSessions();
deleteAllWorkspaces();
});

test('should work', async ({ page, baseURL }) => {
Expand Down
26 changes: 3 additions & 23 deletions node/e2e-tests/src/tests/logout.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,10 @@
import { expect, test } from '@playwright/test';
import { KubeConfig, CustomObjectsApi } from '@kubernetes/client-node';
import { namespace, resourceGroup, resourcePlural, resourceVersion } from '../constats';

const kc = new KubeConfig();
kc.loadFromDefault();
const k8sApi = kc.makeApiClient(CustomObjectsApi);
import { deleteAllSessions, deleteAllWorkspaces } from '../k8s';

test.describe('Logout', () => {
test.beforeEach(async () => {
/* delete all sessions */
const resources: any = await k8sApi.listNamespacedCustomObject(
resourceGroup,
resourceVersion,
namespace,
resourcePlural
);

for (const resource of resources.body.items) {
await k8sApi.deleteNamespacedCustomObject(
resourceGroup,
resourceVersion,
namespace,
resourcePlural,
resource.metadata.name
);
}
deleteAllSessions();
deleteAllWorkspaces();
});

test('should work', async ({ page, baseURL }) => {
Expand Down
41 changes: 15 additions & 26 deletions node/e2e-tests/src/tests/start.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
import { expect, test } from '@playwright/test';
import { KubeConfig, CustomObjectsApi } from '@kubernetes/client-node';
import { namespace, resourceGroup, resourcePlural, resourceVersion } from '../constats';

const kc = new KubeConfig();
kc.loadFromDefault();
const k8sApi = kc.makeApiClient(CustomObjectsApi);
import { namespace, resourceGroup, sessionPlural, sessionVersion } from '../constats';
import { deleteAllSessions, deleteAllWorkspaces, k8sApi } from '../k8s';

test.describe('Start Session', () => {
test.beforeEach(async () => {
/* delete all sessions */
const resources: any = await k8sApi.listNamespacedCustomObject(
resourceGroup,
resourceVersion,
namespace,
resourcePlural
);

for (const resource of resources.body.items) {
await k8sApi.deleteNamespacedCustomObject(
resourceGroup,
resourceVersion,
namespace,
resourcePlural,
resource.metadata.name
);
}
deleteAllSessions();
deleteAllWorkspaces();
});

test('should work', async ({ page, baseURL }) => {
Expand Down Expand Up @@ -56,15 +37,23 @@ test.describe('Start Session', () => {
await expect(loadingAnimation).toBeHidden({ timeout: 180000 });

/* check redirect url */
expect(page.url()).toContain(baseURL!.replace('trynow', 'instances'));
const browserUrl = page.url();
expect(browserUrl).toContain(baseURL!.replace('trynow', 'instances'));

/* check created session */
const resources: any = await k8sApi.listNamespacedCustomObject(
resourceGroup,
resourceVersion,
sessionVersion,
namespace,
resourcePlural
sessionPlural
);
expect(resources.body.items).toHaveLength(1);

const sessionUrl = resources.body.items[0].status.url;
expect(sessionUrl).toBeDefined();
const normalizedBrowserUrl = new URL(browserUrl);
const normalizedSessionUrl = new URL(sessionUrl.startsWith('http') ? sessionUrl : `https://${sessionUrl}`);
expect(normalizedBrowserUrl.hostname).toBe(normalizedSessionUrl.hostname);
expect(normalizedBrowserUrl.pathname).toBe(normalizedSessionUrl.pathname);
});
});

0 comments on commit 27e2119

Please sign in to comment.