Skip to content

Commit

Permalink
add tests and types for incluster tektonhub resource
Browse files Browse the repository at this point in the history
  • Loading branch information
karthikjeeyar committed Apr 14, 2022
1 parent a58e998 commit 2ce56d0
Show file tree
Hide file tree
Showing 4 changed files with 210 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { act } from 'react-dom/test-utils';
import { setUtilsConfig } from '@console/dynamic-plugin-sdk/src/app/configSetup';
import { appInternalFetch } from '@console/internal/co-fetch';
import { useK8sGet } from '@console/internal/components/utils/k8s-get-hook';
import { testHook } from '../../../../../../../__tests__/utils/hooks-utils';
import { sampleTektonHubCatalogItem } from '../../../../test-data/catalog-item-data';
import { sampleTektonHubCR } from '../../../../test-data/tektonhub-data';
import {
TektonHubTaskVersion,
getApiResponse,
getHubUIPath,
TEKTON_HUB_ENDPOINT,
useInclusterTektonHubURLs,
TEKTON_HUB_API_ENDPOINT,
} from '../tektonHub';

jest.mock('@console/internal/components/utils/k8s-get-hook', () => ({
useK8sGet: jest.fn(),
}));

const emptyHeaders = new Headers();
const apiResults = ({
ok,
Expand Down Expand Up @@ -94,3 +104,85 @@ describe('getHubUIPath', () => {
expect(getHubUIPath('test-path', 'https://hub-ui.com')).toBe(`https://hub-ui.com/test-path`);
});
});

describe('useInClusterTektonHubURLs:', () => {
it('should return public tekton hub endpoint incase of hub CR not found', async () => {
(useK8sGet as jest.Mock).mockReturnValue([
null,
true,
'error fetching tektonhubs.operator.tekton.dev "hub" not found',
]);
const { result } = testHook(() => useInclusterTektonHubURLs());
expect(result.current).toEqual({
loaded: true,
apiURL: TEKTON_HUB_API_ENDPOINT,
uiURL: TEKTON_HUB_ENDPOINT,
});
});

it('should return public tekton hub endpoint when hub is installed and if status field missing', async () => {
const sampleHubWithoutApiURL = {
...sampleTektonHubCR,
status: null,
};
(useK8sGet as jest.Mock).mockReturnValue([sampleHubWithoutApiURL, true, '']);
const { result } = testHook(() => useInclusterTektonHubURLs());
expect(result.current).toEqual({
loaded: true,
apiURL: TEKTON_HUB_API_ENDPOINT,
uiURL: TEKTON_HUB_ENDPOINT,
});
});

it('should return public tekton hub endpoint when hub is installed but urls are missing', async () => {
const sampleHubWithoutApiURL = {
...sampleTektonHubCR,
status: {
...sampleTektonHubCR.status,
apiUrl: '',
uiUrl: '',
},
};
(useK8sGet as jest.Mock).mockReturnValue([sampleHubWithoutApiURL, true, '']);
const { result } = testHook(() => useInclusterTektonHubURLs());
expect(result.current).toEqual({
loaded: true,
apiURL: TEKTON_HUB_API_ENDPOINT,
uiURL: TEKTON_HUB_ENDPOINT,
});
});

it('should return incluster tekton hub endpoint when hub is installed', async () => {
(useK8sGet as jest.Mock).mockReturnValue([sampleTektonHubCR, true, '']);
const { result } = testHook(() => useInclusterTektonHubURLs());
expect(result.current).toEqual({
loaded: true,
apiURL: sampleTektonHubCR.status.apiUrl,
uiURL: sampleTektonHubCR.status.uiUrl,
});
});

it('should return the apiUrl and uiUrl when the api call the hub has urls in the status', async () => {
const sampleHubWithoutApiURL = {
...sampleTektonHubCR,
status: null,
};
(useK8sGet as jest.Mock).mockReturnValue([sampleHubWithoutApiURL, true, '']);
const { result, rerender } = testHook(() => useInclusterTektonHubURLs());
expect(result.current).toEqual({
loaded: true,
apiURL: TEKTON_HUB_API_ENDPOINT,
uiURL: TEKTON_HUB_ENDPOINT,
});

(useK8sGet as jest.Mock).mockReturnValue([sampleTektonHubCR, true, '']);
act(() => {
rerender();
});
expect(result.current).toEqual({
loaded: true,
apiURL: sampleTektonHubCR.status.apiUrl,
uiURL: sampleTektonHubCR.status.uiUrl,
});
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { coFetch } from '@console/internal/co-fetch';
import { useK8sGet } from '@console/internal/components/utils/k8s-get-hook';
import { K8sResourceKind } from '@console/internal/module/k8s';
import { TektonHubModel } from '../../../models';
import { TektonHub } from '../../../types/hub';
import useApiResponse, { ApiResult } from '../hooks/useApiResponse';

export type TektonHubItem = {
Expand Down Expand Up @@ -60,7 +60,7 @@ export const getHubUIPath = (path: string = '', baseURL: string = TEKTON_HUB_END
export const getApiResponse = async (url: string) => (await coFetch(url)).json();

export const useInclusterTektonHubURLs = () => {
const [hub, loaded] = useK8sGet<K8sResourceKind>(TektonHubModel, 'hub');
const [hub, loaded] = useK8sGet<TektonHub>(TektonHubModel, 'hub');
// check in-cluster hub exists, if yes use incluster hub instance api url and ui url
return {
loaded,
Expand Down
87 changes: 87 additions & 0 deletions frontend/packages/pipelines-plugin/src/test-data/tektonhub-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { TektonHub } from '../types/hub';

export const sampleTektonHubCR: TektonHub = {
apiVersion: 'operator.tekton.dev/v1alpha1',
kind: 'TektonHub',
metadata: {
creationTimestamp: '2022-04-14T09:56:29Z',
name: 'hub',
resourceVersion: '91182',
uid: '247a09d7-3de3-4e21-92ec-238b99d97c2b',
},
spec: {
api: {
hubConfigUrl: 'https://raw.githubusercontent.com/karthikjeeyar/hub/main/config.yaml',
secret: 'tekton-hub-api',
},
db: {
secret: 'tekton-hub-db',
},
targetNamespace: 'karthik',
},
status: {
apiUrl: 'https://tekton-hub-api.devcluster.openshift.com',
authUrl: 'https://tekton-hub-auth.devcluster.openshift.com',
conditions: [
{
lastTransitionTime: '2022-04-14T10:00:00Z',
status: 'True',
type: 'ApiDependenciesInstalled',
},
{
lastTransitionTime: '2022-04-14T09:59:45Z',
status: 'True',
type: 'ApiInstallSetAvailable',
},
{
lastTransitionTime: '2022-04-14T09:59:04Z',
status: 'True',
type: 'DatabasebMigrationDone',
},
{
lastTransitionTime: '2022-04-14T09:57:52Z',
status: 'True',
type: 'DbDependenciesInstalled',
},
{
lastTransitionTime: '2022-04-14T09:58:43Z',
status: 'True',
type: 'DbInstallSetAvailable',
},
{
lastTransitionTime: '2022-04-14T10:00:00Z',
status: 'True',
type: 'PostReconciler',
},
{
lastTransitionTime: '2022-04-14T09:57:52Z',
status: 'True',
type: 'PreReconciler',
},
{
lastTransitionTime: '2022-04-14T10:00:00Z',
status: 'True',
type: 'Ready',
},
{
lastTransitionTime: '2022-04-14T09:59:45Z',
status: 'True',
type: 'UiDependenciesInstalled',
},
{
lastTransitionTime: '2022-04-14T10:00:00Z',
status: 'True',
type: 'UiInstallSetAvailable',
},
],
hubInstallerSets: {
ApiInstallerSet: 'tekton-hub-api-w6m9z',
DbInstallerSet: 'tekton-hub-db-8mzw8',
DbMigrationInstallerSet: 'tekton-hub-db-migration-zkn9p',
UiInstallerSet: 'tekton-hub-ui-fjbbb',
},
observedGeneration: 1,
uiUrl: 'https://tekton-hub-ui.devcluster.openshift.com',
version: 'v1.7.1',
},
};
29 changes: 29 additions & 0 deletions frontend/packages/pipelines-plugin/src/types/hub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { K8sResourceCommon } from 'packages/console-dynamic-plugin-sdk/src';
import { Condition } from './pipelineRun';

export type TektonHub = K8sResourceCommon & {
spec: {
api: {
hubConfigUrl: string;
secret: string;
};
db: {
secret: string;
};
targetNamespace: string;
};
status: {
apiUrl: string;
uiUrl: string;
authUrl: string;
conditions: Condition[];
hubInstallerSets: {
ApiInstallerSet: string;
DbInstallerSet: string;
DbMigrationInstallerSet: string;
UiInstallerSet: string;
};
observedGeneration: number;
version: string;
};
};

0 comments on commit 2ce56d0

Please sign in to comment.