Skip to content

Commit

Permalink
Convert templates to use websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
ppadti committed Jun 3, 2024
1 parent 073cb0e commit 3445807
Show file tree
Hide file tree
Showing 28 changed files with 416 additions and 219 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { mockK8sResourceList } from '~/__mocks__/mockK8sResourceList';
import { mockServingRuntimeTemplateK8sResource } from '~/__mocks__/mockServingRuntimeTemplateK8sResource';
import { servingRuntimes } from '~/__tests__/cypress/cypress/pages/servingRuntimes';
import { ServingRuntimeAPIProtocol, ServingRuntimePlatform } from '~/types';
import { deleteModal } from '~/__tests__/cypress/cypress/pages/components/DeleteModal';
import { mockServingRuntimeK8sResource } from '~/__mocks__/mockServingRuntimeK8sResource';
import { asProductAdminUser, asProjectAdminUser } from '~/__tests__/cypress/cypress/utils/users';
import { pageNotfound } from '~/__tests__/cypress/cypress/pages/pageNotFound';
import {
customServingRuntimesInitialMock,
customServingRuntimesIntercept,
} from '~/__tests__/cypress/cypress/e2e/customServingRuntimes/customServingRuntimesUtils';
import { customServingRuntimesIntercept } from '~/__tests__/cypress/cypress/e2e/customServingRuntimes/customServingRuntimesUtils';
import { TemplateModel } from '~/__tests__/cypress/cypress/utils/models';

const addfilePath = '../../__mocks__/mock-custom-serving-runtime-add.yaml';
const editfilePath = '../../__mocks__/mock-custom-serving-runtime-edit.yaml';
Expand Down Expand Up @@ -81,7 +78,6 @@ describe('Custom serving runtimes', () => {

servingRuntimes.findSubmitButton().should('be.enabled');
servingRuntimes.findSubmitButton().click();

cy.wait('@createSingleModelServingRuntime').then((interception) => {
expect(interception.request.url).to.include('?dryRun=All');
expect(interception.request.body).to.containSubset({
Expand Down Expand Up @@ -112,6 +108,18 @@ describe('Custom serving runtimes', () => {
],
});
});
cy.wsK8s(
'ADDED',
TemplateModel,
mockServingRuntimeTemplateK8sResource({
name: 'template-new',
displayName: 'New OVMS Server',
platforms: [ServingRuntimePlatform.SINGLE],
apiProtocol: ServingRuntimeAPIProtocol.REST,
}),
);

servingRuntimes.getRowById('template-new').shouldBeSingleModel(true);
});

it('should add a new multi model serving runtime', () => {
Expand Down Expand Up @@ -172,6 +180,18 @@ describe('Custom serving runtimes', () => {
],
});
});

cy.wsK8s(
'ADDED',
TemplateModel,
mockServingRuntimeTemplateK8sResource({
name: 'template-new',
displayName: 'New OVMS Server',
platforms: [ServingRuntimePlatform.MULTI],
}),
);

servingRuntimes.getRowById('template-new').shouldBeMultiModel(true);
});

it('should duplicate a serving runtime', () => {
Expand All @@ -185,19 +205,6 @@ describe('Custom serving runtimes', () => {
'duplicateTemplate',
);

const ServingRuntimeTemplateMock = mockServingRuntimeTemplateK8sResource({
name: 'serving-runtime-template-1',
displayName: 'Multi platform',
platforms: [ServingRuntimePlatform.SINGLE],
apiProtocol: ServingRuntimeAPIProtocol.GRPC,
});

cy.interceptOdh(
'GET /api/templates/:namespace',
{ path: { namespace: 'opendatahub' } },
mockK8sResourceList([...customServingRuntimesInitialMock, ServingRuntimeTemplateMock]),
).as('refreshServingRuntime');

servingRuntimes.getRowById('template-1').find().findKebabAction('Duplicate').click();
servingRuntimes.findAppTitle().should('have.text', 'Duplicate serving runtime');
cy.url().should('include', '/addServingRuntime');
Expand Down Expand Up @@ -236,10 +243,20 @@ describe('Custom serving runtimes', () => {
],
});
});
cy.wait('@refreshServingRuntime');

cy.wsK8s(
'ADDED',
TemplateModel,
mockServingRuntimeTemplateK8sResource({
name: 'template-1-copy',
displayName: 'Copy of Multi platform',
platforms: [ServingRuntimePlatform.SINGLE],
apiProtocol: ServingRuntimeAPIProtocol.GRPC,
}),
);

servingRuntimes
.getRowById('serving-runtime-template-1')
.getRowById('template-1-copy')
.shouldHaveAPIProtocol(ServingRuntimeAPIProtocol.GRPC);
});

Expand Down Expand Up @@ -310,5 +327,16 @@ describe('Custom serving runtimes', () => {
deleteModal.findSubmitButton().should('be.enabled').click();

cy.wait('@deleteServingRuntime');
cy.wsK8s(
'DELETED',
TemplateModel,
mockServingRuntimeTemplateK8sResource({
name: 'template-1',
displayName: 'Multi platform',
platforms: [ServingRuntimePlatform.SINGLE],
apiProtocol: ServingRuntimeAPIProtocol.REST,
}),
);
servingRuntimes.getRowById('template-1').find().should('not.exist');
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mockK8sResourceList } from '~/__mocks__/mockK8sResourceList';
import { mockServingRuntimeTemplateK8sResource } from '~/__mocks__/mockServingRuntimeTemplateK8sResource';
import { ServingRuntimeAPIProtocol, ServingRuntimePlatform } from '~/types';
import { ProjectModel } from '~/__tests__/cypress/cypress/utils/models';
import { ProjectModel, TemplateModel } from '~/__tests__/cypress/cypress/utils/models';
import { mockProjectK8sResource } from '~/__mocks__';

export const customServingRuntimesInitialMock = [
Expand All @@ -28,10 +28,11 @@ export const customServingRuntimesInitialMock = [
];

export const customServingRuntimesIntercept = (): void => {
cy.interceptK8sList(TemplateModel, mockK8sResourceList(customServingRuntimesInitialMock));
cy.interceptK8sList(ProjectModel, mockK8sResourceList([mockProjectK8sResource({})]));
cy.interceptOdh(
'GET /api/templates/:namespace',
{ path: { namespace: 'opendatahub' } },
mockK8sResourceList(customServingRuntimesInitialMock),
);
cy.interceptK8sList(ProjectModel, mockK8sResourceList([mockProjectK8sResource({})]));
};
35 changes: 20 additions & 15 deletions frontend/src/api/k8s/__tests__/groups.spec.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
import { useK8sWatchResource } from '@openshift/dynamic-plugin-sdk-utils';
import { groupVersionKind, useAccessReview, useGroups } from '~/api';
import { testHook } from '~/__tests__/unit/testUtils/hooks';
import { GroupModel } from '~/api/models';
import { mockGroup } from '~/__mocks__/mockGroup';
import useCustomK8sWatchResource from '~/utilities/useCustomK8sWatchResource';

jest.mock('@openshift/dynamic-plugin-sdk-utils', () => ({
useK8sWatchResource: jest.fn(),
jest.mock('~/utilities/useCustomK8sWatchResource', () => ({
__esModule: true,
default: jest.fn(),
}));

jest.mock('~/api/useAccessReview', () => ({
useAccessReview: jest.fn(),
}));

const useAccessReviewMock = jest.mocked(useAccessReview);
const useK8sWatchResourceMock = useK8sWatchResource as jest.Mock;
const useCustomK8sWatchResourceMock = useCustomK8sWatchResource as jest.Mock;

describe('useGroups', () => {
it('should wrap useK8sWatchResource to watch groups', async () => {
const mockReturnValue: ReturnType<typeof useK8sWatchResourceMock> = [[], false, undefined];
const mockReturnValue: ReturnType<typeof useCustomK8sWatchResourceMock> = [
[],
false,
undefined,
];
useAccessReviewMock.mockReturnValue([true, true]);
useK8sWatchResourceMock.mockReturnValue(mockReturnValue);
useCustomK8sWatchResourceMock.mockReturnValue(mockReturnValue);
const { result } = testHook(useGroups)();

expect(useK8sWatchResourceMock).toHaveBeenCalledTimes(1);
expect(useK8sWatchResourceMock).toHaveBeenCalledWith(
expect(useCustomK8sWatchResourceMock).toHaveBeenCalledTimes(1);
expect(useCustomK8sWatchResourceMock).toHaveBeenCalledWith(
{
isList: true,
groupVersionKind: groupVersionKind(GroupModel),
Expand All @@ -34,16 +39,16 @@ describe('useGroups', () => {
});

it('should render list of groups', () => {
const mockReturnValue: ReturnType<typeof useK8sWatchResourceMock> = [
const mockReturnValue: ReturnType<typeof useCustomK8sWatchResourceMock> = [
[mockGroup({})],
true,
undefined,
];
useAccessReviewMock.mockReturnValue([true, true]);
useK8sWatchResourceMock.mockReturnValue(mockReturnValue);
useCustomK8sWatchResourceMock.mockReturnValue(mockReturnValue);
const { result } = testHook(useGroups)();
expect(useK8sWatchResourceMock).toHaveBeenCalledTimes(1);
expect(useK8sWatchResourceMock).toHaveBeenCalledWith(
expect(useCustomK8sWatchResourceMock).toHaveBeenCalledTimes(1);
expect(useCustomK8sWatchResourceMock).toHaveBeenCalledWith(
{
isList: true,
groupVersionKind: groupVersionKind(GroupModel),
Expand All @@ -55,10 +60,10 @@ describe('useGroups', () => {

it('should handle 403 error', () => {
useAccessReviewMock.mockReturnValue([false, true]);
useK8sWatchResourceMock.mockReturnValue([undefined, true, undefined]);
useCustomK8sWatchResourceMock.mockReturnValue([undefined, true, undefined]);
const { result } = testHook(useGroups)();
expect(useK8sWatchResourceMock).toHaveBeenCalledTimes(1);
expect(useK8sWatchResourceMock).toHaveBeenCalledWith(null, GroupModel);
expect(useCustomK8sWatchResourceMock).toHaveBeenCalledTimes(1);
expect(useCustomK8sWatchResourceMock).toHaveBeenCalledWith(null, GroupModel);
expect(result.current).toStrictEqual([[], true, undefined]);
});
});
20 changes: 14 additions & 6 deletions frontend/src/api/k8s/__tests__/projects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
k8sCreateResource,
k8sUpdateResource,
k8sDeleteResource,
useK8sWatchResource,
} from '@openshift/dynamic-plugin-sdk-utils';
import axios from 'axios';
import { mockProjectK8sResource } from '~/__mocks__/mockProjectK8sResource';
Expand All @@ -26,13 +25,18 @@ import { listServingRuntimes } from '~/api/k8s/servingRuntimes';
import { NamespaceApplicationCase } from '~/pages/projects/types';
import { ProjectKind } from '~/k8sTypes';
import { groupVersionKind } from '~/api/k8sUtils';
import useCustomK8sWatchResource from '~/utilities/useCustomK8sWatchResource';

jest.mock('@openshift/dynamic-plugin-sdk-utils', () => ({
k8sListResource: jest.fn(),
k8sCreateResource: jest.fn(),
k8sUpdateResource: jest.fn(),
k8sDeleteResource: jest.fn(),
useK8sWatchResource: jest.fn(),
}));

jest.mock('~/utilities/useCustomK8sWatchResource', () => ({
__esModule: true,
default: jest.fn(),
}));

jest.mock('~/api/k8s/servingRuntimes.ts', () => ({
Expand All @@ -47,14 +51,18 @@ const k8sListResourceMock = jest.mocked(k8sListResource<ProjectKind>);
const k8sCreateResourceMock = jest.mocked(k8sCreateResource<ProjectKind>);
const k8sUpdateResourceMock = jest.mocked(k8sUpdateResource<ProjectKind>);
const k8sDeleteResourceMock = jest.mocked(k8sDeleteResource<ProjectKind>);
const useK8sWatchResourceMock = jest.mocked(useK8sWatchResource<ProjectKind[]>);
const useCustomK8sWatchResourceMock = jest.mocked(useCustomK8sWatchResource<ProjectKind[]>);

describe('useProjects', () => {
it('should wrap useK8sWatchResource to watch projects', async () => {
const mockReturnValue: ReturnType<typeof useK8sWatchResourceMock> = [[], false, false];
useK8sWatchResourceMock.mockReturnValue(mockReturnValue);
const mockReturnValue: ReturnType<typeof useCustomK8sWatchResourceMock> = [
[],
false,
undefined,
];
useCustomK8sWatchResourceMock.mockReturnValue(mockReturnValue);
expect(useProjects()).toBe(mockReturnValue);
expect(useK8sWatchResourceMock).toHaveBeenCalledWith(
expect(useCustomK8sWatchResourceMock).toHaveBeenCalledWith(
{
isList: true,
groupVersionKind: groupVersionKind(ProjectModel),
Expand Down
Loading

0 comments on commit 3445807

Please sign in to comment.