Skip to content

test(ws): Add workspaces tests #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { WorkspaceState } from '~/shared/api/backendApiTypes';
import type { Workspace, WorkspaceKindInfo } from '~/shared/api/backendApiTypes';

const generateMockWorkspace = (
name: string,
namespace: string,
state: WorkspaceState,
paused: boolean,
imageConfigId: string,
imageConfigDisplayName: string,
podConfigId: string,
podConfigDisplayName: string,
pvcName: string,
): Workspace => {
const currentTime = Date.now();
const lastActivityTime = currentTime - Math.floor(Math.random() * 1000000);
const lastUpdateTime = currentTime - Math.floor(Math.random() * 100000);

return {
name,
namespace,
workspaceKind: { name: 'jupyterlab' } as WorkspaceKindInfo,
deferUpdates: paused,
paused,
pausedTime: paused ? currentTime - Math.floor(Math.random() * 1000000) : 0,
pendingRestart: Math.random() < 0.5, //to generate randomly True/False value
state,
stateMessage:
state === WorkspaceState.WorkspaceStateRunning
? 'Workspace is running smoothly.'
: state === WorkspaceState.WorkspaceStatePaused
? 'Workspace is paused.'
: 'Workspace is operational.',
podTemplate: {
podMetadata: {
labels: {},
annotations: {},
},
volumes: {
home: {
pvcName: `${pvcName}-home`,
mountPath: '/home/jovyan',
readOnly: false,
},
data: [
{
pvcName,
mountPath: '/data/my-data',
readOnly: paused,
},
],
},
options: {
imageConfig: {
current: {
id: imageConfigId,
displayName: imageConfigDisplayName,
description: 'JupyterLab environment',
labels: [{ key: 'python_version', value: '3.11' }],
},
},
podConfig: {
current: {
id: podConfigId,
displayName: podConfigDisplayName,
description: 'Pod configuration with resource limits',
labels: [
{ key: 'cpu', value: '100m' },
{ key: 'memory', value: '128Mi' },
],
},
},
},
},
activity: {
lastActivity: lastActivityTime,
lastUpdate: lastUpdateTime,
},
services: [
{
httpService: {
displayName: 'Jupyter-lab',
httpPath: `/workspace/${namespace}/${name}/Jupyter-lab/`,
},
},
],
};
};

const generateMockWorkspaces = (numWorkspaces: number, byNamespace = false) => {
const mockWorkspaces = [];
const podConfigs = [
{ id: 'small-cpu', displayName: 'Small CPU' },
{ id: 'medium-cpu', displayName: 'Medium CPU' },
{ id: 'large-cpu', displayName: 'Large CPU' },
];
const imageConfigs = [
{ id: 'jupyterlab_scipy_180', displayName: 'JupyterLab SciPy 1.8.0' },
{ id: 'jupyterlab_tensorflow_230', displayName: 'JupyterLab TensorFlow 2.3.0' },
{ id: 'jupyterlab_pytorch_120', displayName: 'JupyterLab PyTorch 1.2.0' },
];
const namespaces = byNamespace ? ['kubeflow'] : ['kubeflow', 'system', 'user-example', 'default'];

for (let i = 1; i <= numWorkspaces; i++) {
const state =
i % 3 === 0
? WorkspaceState.WorkspaceStateError
: i % 2 === 0
? WorkspaceState.WorkspaceStatePaused
: WorkspaceState.WorkspaceStateRunning;
const paused = state === WorkspaceState.WorkspaceStatePaused;
const name = `workspace-${i}`;
const namespace = namespaces[i % namespaces.length];
const pvcName = `data-pvc-${i}`;

const imageConfig = imageConfigs[i % imageConfigs.length];
const podConfig = podConfigs[i % podConfigs.length];

mockWorkspaces.push(
generateMockWorkspace(
name,
namespace,
state,
paused,
imageConfig.id,
imageConfig.displayName,
podConfig.id,
podConfig.displayName,
pvcName,
),
);
}

return mockWorkspaces;
};

// Example usage
export const mockWorkspaces = generateMockWorkspaces(5);
export const mockWorkspacesByNS = generateMockWorkspaces(10, true);
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { mockBFFResponse } from '~/__mocks__/utils';
import { mockWorkspaces } from '~/__tests__/cypress/cypress/tests/mocked/workspace.mock';
import { formatTimestamp } from '~/shared/utilities/WorkspaceUtils';

describe('WorkspaceDetailsActivity Component', () => {
beforeEach(() => {
cy.intercept('GET', 'api/v1/workspaces', {
body: mockBFFResponse(mockWorkspaces),
}).as('getWorkspaces');
cy.visit('/');
});

// This tests depends on the mocked workspaces data at home page, needs revisit once workspace data fetched from BE
it('open workspace details, open activity tab, check all fields match', () => {
cy.findAllByTestId('table-body').first().findByTestId('action-column').click();
// Extract first workspace from mock data
cy.wait('@getWorkspaces').then((interception) => {
if (!interception.response || !interception.response.body) {
throw new Error('Intercepted response is undefined or empty');
}
const workspace = interception.response.body.data[0];
cy.findByTestId('action-view-details').click();
cy.findByTestId('activityTab').click();
cy.findByTestId('lastActivity')
.invoke('text')
.then((text) => {
console.log('Rendered lastActivity:', text);
});
cy.findByTestId('lastActivity').should(
'have.text',
formatTimestamp(workspace.activity.lastActivity),
);
cy.findByTestId('lastUpdate').should(
'have.text',
formatTimestamp(workspace.activity.lastUpdate),
);
cy.findByTestId('pauseTime').should('have.text', formatTimestamp(workspace.pausedTime));
cy.findByTestId('pendingRestart').should(
'have.text',
workspace.pendingRestart ? 'Yes' : 'No',
);
});
});
});
Loading