Skip to content

Commit

Permalink
feat(tkn): fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinandan13jan committed Nov 7, 2024
1 parent a0f63f2 commit 7f19945
Show file tree
Hide file tree
Showing 25 changed files with 335 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ const getResultsSummary = (ECs, ecLoaded) => {
};

export const SecurityEnterpriseContractTab: React.FC<
React.PropsWithChildren<{ pipelineRun: string }>
> = ({ pipelineRun }) => {
const [ecResult, ecResultLoaded] = useEnterpriseContractResults(pipelineRun);
React.PropsWithChildren<{
pipelineRunName: string;
pipelineRunUID: string;
}>
> = ({ pipelineRunName, pipelineRunUID }) => {
const [ecResult, ecResultLoaded] = useEnterpriseContractResults(pipelineRunName, pipelineRunUID);

const [nameFilter, setNameFilter] = useSearchParam('name', '');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,23 @@ describe('SecurityEnterpriseContractTab', () => {
it('should render empty state for security tab when pods are missing', () => {
mockUseEnterpriseContractResults.mockReturnValue([undefined, true]);

routerRenderer(<SecurityEnterpriseContractTab pipelineRun="dummy" />);
routerRenderer(
<SecurityEnterpriseContractTab pipelineRunName="dummy" pipelineRunUID="pipelinerun-test" />,
);
screen.getByTestId('security-tab-empty-state');
});

it('should render component security tab', () => {
routerRenderer(<SecurityEnterpriseContractTab pipelineRun="dummy" />);
routerRenderer(
<SecurityEnterpriseContractTab pipelineRunName="dummy" pipelineRunUID="pipelinerun-test" />,
);
screen.getByText('Missing CVE scan results');
});

it('should filter out results based on the name input field', () => {
routerRenderer(<SecurityEnterpriseContractTab pipelineRun="dummy" />);
routerRenderer(
<SecurityEnterpriseContractTab pipelineRunName="dummy" pipelineRunUID="pipelinerun-test" />,
);
screen.getByText('Missing CVE scan results');
fireEvent.input(screen.getByPlaceholderText('Filter by rule...'), {
target: { value: 'No tasks' },
Expand All @@ -43,7 +49,9 @@ describe('SecurityEnterpriseContractTab', () => {
});

it('should filter out based on the status dropdown', async () => {
routerRenderer(<SecurityEnterpriseContractTab pipelineRun="dummy-1" />);
routerRenderer(
<SecurityEnterpriseContractTab pipelineRunName="dummy" pipelineRunUID="pipelinerun-test" />,
);
screen.getByText('Missing CVE scan results');
fireEvent.click(screen.getByRole('button', { name: 'Status filter menu' }));
fireEvent.click(screen.getByLabelText('Success'));
Expand All @@ -53,7 +61,9 @@ describe('SecurityEnterpriseContractTab', () => {
});

it('should show empty state when no search result found', () => {
routerRenderer(<SecurityEnterpriseContractTab pipelineRun="dummy-1" />);
routerRenderer(
<SecurityEnterpriseContractTab pipelineRunName="dummy" pipelineRunUID="pipelinerun-test" />,
);
screen.getByText('Missing CVE scan results');
fireEvent.click(screen.getByRole('button', { name: 'Status filter menu' }));
fireEvent.click(screen.getByLabelText('Failed'));
Expand All @@ -68,7 +78,9 @@ describe('SecurityEnterpriseContractTab', () => {
});

it('should sort by Status', () => {
routerRenderer(<SecurityEnterpriseContractTab pipelineRun="dummy-1" />);
routerRenderer(
<SecurityEnterpriseContractTab pipelineRunName="dummy" pipelineRunUID="pipelinerun-test" />,
);
const status = screen.getAllByTestId('rule-status');
expect(status[0].textContent.trim()).toEqual('Failed');
fireEvent.click(screen.getAllByText('Status')[1]);
Expand All @@ -77,7 +89,9 @@ describe('SecurityEnterpriseContractTab', () => {
});

it('should render result summary', () => {
routerRenderer(<SecurityEnterpriseContractTab pipelineRun="dummy-1" />);
routerRenderer(
<SecurityEnterpriseContractTab pipelineRunName="dummy" pipelineRunUID="pipelinerun-test" />,
);
const resultSummary = screen.getByTestId('result-summary');
const status = resultSummary.getElementsByTagName('span');
expect(status[0].textContent.trim()).toBe('Failed');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('useEnterpriseContractResultFromLogs', () => {

it('should parse valid rules to json', async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useEnterpriseContractResultFromLogs('dummy-abcd'),
useEnterpriseContractResultFromLogs('dummy-abcd', 'pipelinerun-test'),
);
await waitForNextUpdate();
expect(mockCommmonFetchJSON).toHaveBeenCalled();
Expand All @@ -69,7 +69,9 @@ describe('useEnterpriseContractResultFromLogs', () => {
mockGetTaskRunLogs.mockReturnValue(`asdfcdfadsf
[report-json] { "components": [] }
`);
const { result } = renderHook(() => useEnterpriseContractResultFromLogs('dummy-abcd'));
const { result } = renderHook(() =>
useEnterpriseContractResultFromLogs('dummy-abcd', 'pipelinerun-test'),
);
const [, loaded] = result.current;
expect(mockCommmonFetchJSON).toHaveBeenCalled();
expect(loaded).toBe(true);
Expand All @@ -80,7 +82,7 @@ describe('useEnterpriseContractResultFromLogs', () => {

it('should filter out all 404 image url components from EC results', async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useEnterpriseContractResultFromLogs('dummy-abcd'),
useEnterpriseContractResultFromLogs('dummy-abcd', 'pipelinerun-test'),
);
await waitForNextUpdate();
const [ecResult, loaded] = result.current;
Expand All @@ -93,7 +95,7 @@ describe('useEnterpriseContractResultFromLogs', () => {
mockCommmonFetchJSON.mockRejectedValue(new Error('Api error'));

const { result, waitForNextUpdate } = renderHook(() =>
useEnterpriseContractResultFromLogs('dummy-abcd'),
useEnterpriseContractResultFromLogs('dummy-abcd', 'pipelinerun-test'),
);
await waitForNextUpdate();
const [ecResult, loaded] = result.current;
Expand Down Expand Up @@ -124,7 +126,7 @@ describe('useEnterpriseContractResultFromLogs', () => {
`);

const { result, waitForNextUpdate } = renderHook(() =>
useEnterpriseContractResultFromLogs('dummy-abcd'),
useEnterpriseContractResultFromLogs('dummy-abcd', 'pipelinerun-test'),
);
const [, loaded] = result.current;
expect(mockCommmonFetchJSON).toHaveBeenCalled();
Expand Down Expand Up @@ -177,7 +179,7 @@ describe('useEnterpriseContractResults', () => {

it('should return enterprise contract results', async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useEnterpriseContractResults('dummy-abcd'),
useEnterpriseContractResults('dummy-abcd', 'pipelinerun-test'),
);
expect(result.current[0]).toEqual(undefined);
expect(result.current[1]).toEqual(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { extractEcResultsFromTaskRunLogs } from './utils';

export const useEnterpriseContractResultFromLogs = (
pipelineRunName: string,
pipelineRunUID: string,
): [ComponentEnterpriseContractResult[], boolean] => {
const { namespace, workspace } = useWorkspaceInfo();
const [taskRun, loaded, error] = useTaskRuns(namespace, pipelineRunName, 'verify');
Expand Down Expand Up @@ -71,7 +72,8 @@ export const useEnterpriseContractResultFromLogs = (
const logs = await getTaskRunLog(
workspace,
taskRun[0].metadata.namespace,
taskRun[0].metadata.name,
pipelineRunUID,
taskRun[0],
);
if (unmount) return;
const json = extractEcResultsFromTaskRunLogs(logs);
Expand All @@ -93,7 +95,7 @@ export const useEnterpriseContractResultFromLogs = (
return () => {
unmount = true;
};
}, [fetchTknLogs, taskRun, workspace]);
}, [fetchTknLogs, taskRun, workspace, pipelineRunUID]);

const ecResult = React.useMemo(() => {
// filter out components for which ec didn't execute because invalid image URL
Expand Down Expand Up @@ -158,8 +160,9 @@ export const mapEnterpriseContractResultData = (

export const useEnterpriseContractResults = (
pipelineRunName: string,
pipelineRunUID: string,
): [UIEnterpriseContractData[], boolean] => {
const [ec, ecLoaded] = useEnterpriseContractResultFromLogs(pipelineRunName);
const [ec, ecLoaded] = useEnterpriseContractResultFromLogs(pipelineRunName, pipelineRunUID);
const ecResult = React.useMemo(() => {
return ecLoaded && ec ? mapEnterpriseContractResultData(ec) : undefined;
}, [ec, ecLoaded]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ export const PipelineRunDetailsView: React.FC<
key: 'security',
label: 'Security',
component: (
<SecurityEnterpriseContractTab pipelineRun={pipelineRun.metadata.name} />
<SecurityEnterpriseContractTab
pipelineRunName={pipelineRun.metadata.name}
pipelineRunUID={pipelineRun.metadata.uid}
/>
),
},
]
Expand Down
12 changes: 10 additions & 2 deletions src/components/PipelineRunDetailsView/PipelineRunSidePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import { isTaskNode } from './visualization/utils/pipelinerun-graph-utils';

type Props = {
scrollIntoView?: (node: Node) => void;
pipelineRunUID: string;
};

const PipelineRunSidePanel: React.FC<React.PropsWithChildren<Props>> = ({ scrollIntoView }) => {
const PipelineRunSidePanel: React.FC<React.PropsWithChildren<Props>> = ({
scrollIntoView,
pipelineRunUID,
}) => {
const [[selectedId], setSelectedIds] = useVisualizationState<string[]>(SELECTION_STATE, []);
const controller = useVisualizationController();

Expand All @@ -28,7 +32,11 @@ const PipelineRunSidePanel: React.FC<React.PropsWithChildren<Props>> = ({ scroll
}, [controller, selectedId]);

const panel = taskNode ? (
<TaskRunPanel onClose={() => setSelectedIds([])} taskNode={taskNode} />
<TaskRunPanel
onClose={() => setSelectedIds([])}

Check warning on line 36 in src/components/PipelineRunDetailsView/PipelineRunSidePanel.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/PipelineRunDetailsView/PipelineRunSidePanel.tsx#L36

Added line #L36 was not covered by tests
taskNode={taskNode}
pipelineRunUID={pipelineRunUID}
/>
) : null;

const isExpanded = !!panel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ const PipelineRunVisualization = ({ pipelineRun, error, taskRuns }) => {
layoutFactory={layoutFactory}
model={model}
>
<PipelineRunSidePanel scrollIntoView={scrollIntoView} />
<PipelineRunSidePanel
scrollIntoView={scrollIntoView}
pipelineRunUID={pipelineRun?.metadata?.uid}
/>
</VisualizationFactory>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('PipelineRunSidePanel', () => {
it('should render nothing by default', () => {
const setPropsFn = jest.fn();

render(<PipelineRunSidePanel />, {
render(<PipelineRunSidePanel pipelineRunUID="pipelinerun-test" />, {
wrapper: ({ children }) => (
<SidePanelContext.Provider value={{ setProps: setPropsFn, close: () => {} }}>
{children}
Expand All @@ -42,7 +42,7 @@ describe('PipelineRunSidePanel', () => {

const setPropsFn = jest.fn();

render(<PipelineRunSidePanel />, {
render(<PipelineRunSidePanel pipelineRunUID="pipelinerun-test" />, {
wrapper: ({ children }) => (
<SidePanelContext.Provider value={{ setProps: setPropsFn, close: () => {} }}>
{children}
Expand All @@ -68,13 +68,16 @@ describe('PipelineRunSidePanel', () => {

const setPropsFn = jest.fn();

render(<PipelineRunSidePanel scrollIntoView={scrollIntoViewFn} />, {
wrapper: ({ children }) => (
<SidePanelContext.Provider value={{ setProps: setPropsFn, close: () => {} }}>
{children}
</SidePanelContext.Provider>
),
});
render(
<PipelineRunSidePanel pipelineRunUID="pipelinerun-test" scrollIntoView={scrollIntoViewFn} />,
{
wrapper: ({ children }) => (
<SidePanelContext.Provider value={{ setProps: setPropsFn, close: () => {} }}>
{children}
</SidePanelContext.Provider>
),
},
);

expect(setPropsFn).toHaveBeenCalledWith(
expect.objectContaining({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ import './TaskRunPanel.scss';
type Props = {
onClose: () => void;
taskNode: GraphElement<ElementModel, PipelineRunNodeData>;
pipelineRunUID: string;
};

const TaskRunPanel: React.FC<React.PropsWithChildren<Props>> = ({ taskNode, onClose }) => {
const TaskRunPanel: React.FC<React.PropsWithChildren<Props>> = ({
taskNode,
onClose,
pipelineRunUID,
}) => {
const task = taskNode.getData().task;
const taskRun = taskNode.getData().taskRun;
const { status } = taskNode.getData();
Expand Down Expand Up @@ -64,6 +69,7 @@ const TaskRunPanel: React.FC<React.PropsWithChildren<Props>> = ({ taskNode, onCl
taskRun={taskRun}
namespace={taskNode.getData().namespace}
status={status}
pipelineRunUID={pipelineRunUID}
/>
</DrawerPanelBody>
</Tab>
Expand Down
32 changes: 28 additions & 4 deletions src/components/TaskRunDetailsView/TaskRunDetailsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { Bullseye, Spinner } from '@patternfly/react-core';
import { PipelineRunLabel } from '../../consts/pipelinerun';
import { useTaskRun } from '../../hooks/usePipelineRuns';
import { usePipelineRun, useTaskRun } from '../../hooks/usePipelineRuns';
import DetailsPage from '../../shared/components/details-page/DetailsPage';
import ErrorEmptyState from '../../shared/components/empty-state/ErrorEmptyState';
import { HttpError } from '../../shared/utils/error/http-error';
Expand All @@ -29,6 +29,12 @@ export const TaskRunDetailsView: React.FC<React.PropsWithChildren<TaskRunDetails
const navigate = useNavigate();
const [taskRun, loaded, error] = useTaskRun(namespace, taskRunName);

const plrName = React.useMemo(
() => loaded && !error && taskRun?.metadata?.labels[TektonResourceLabel.pipelinerun],
[taskRun?.metadata?.labels, loaded, error],
);
const [pipelineRun, plrLoaded, plrError] = usePipelineRun(namespace, plrName);

const trStatus = React.useMemo(
() => loaded && taskRun && taskRunStatus(taskRun),
[loaded, taskRun],
Expand Down Expand Up @@ -64,7 +70,6 @@ export const TaskRunDetailsView: React.FC<React.PropsWithChildren<TaskRunDetails
);
}

const plrName = taskRun.metadata?.labels[TektonResourceLabel.pipelinerun];
const isEnterpriseContract = isResourceEnterpriseContract(taskRun);

return (
Expand Down Expand Up @@ -105,14 +110,33 @@ export const TaskRunDetailsView: React.FC<React.PropsWithChildren<TaskRunDetails
{
key: 'logs',
label: 'Logs',
component: <TaskRunLogsTab taskRun={taskRun} />,
component:
(plrLoaded && plrError) || (plrLoaded && !pipelineRun) ? (
<ErrorEmptyState
title={`Unable to load pipelineRun ${plrName}`}
body={JSON.stringify(plrError)}
/>
) : (
<TaskRunLogsTab pipelineRunUID={pipelineRun?.metadata?.uid} taskRun={taskRun} />
),
},
...(isEnterpriseContract
? [
{
key: 'security',
label: 'Security',
component: <SecurityEnterpriseContractTab pipelineRun={plrName} />,
component:
(plrLoaded && plrError) || (plrLoaded && !pipelineRun) ? (
<ErrorEmptyState

Check warning on line 130 in src/components/TaskRunDetailsView/TaskRunDetailsView.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/TaskRunDetailsView/TaskRunDetailsView.tsx#L130

Added line #L130 was not covered by tests
title={`Unable to load pipelineRun ${plrName}`}
body={JSON.stringify(plrError)}
/>
) : (
<SecurityEnterpriseContractTab

Check warning on line 135 in src/components/TaskRunDetailsView/TaskRunDetailsView.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/TaskRunDetailsView/TaskRunDetailsView.tsx#L135

Added line #L135 was not covered by tests
pipelineRunName={plrName}
pipelineRunUID={pipelineRun?.metadata?.uid}
/>
),
},
]
: []),
Expand Down
Loading

0 comments on commit 7f19945

Please sign in to comment.