Skip to content
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

Add expect enum usage #2370

Merged
merged 4 commits into from
Mar 4, 2024
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
6 changes: 3 additions & 3 deletions assets/js/lib/api/checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ const baseURL = config.checksServiceBaseUrl;
const defaultConfig = { baseURL };

export const getExecutionResult = (executionID) =>
networkClient.get(`/api/v1/checks/executions/${executionID}`, defaultConfig);
networkClient.get(`/api/v2/checks/executions/${executionID}`, defaultConfig);

export const getLastExecutionByGroupID = (groupID) =>
networkClient.get(
`/api/v1/checks/groups/${groupID}/executions/last`,
`/api/v2/checks/groups/${groupID}/executions/last`,
defaultConfig
);

export const getCatalog = (env) =>
networkClient.get(`/api/v2/checks/catalog`, {
networkClient.get(`/api/v3/checks/catalog`, {
...defaultConfig,
params: env,
});
Expand Down
8 changes: 8 additions & 0 deletions assets/js/lib/model/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
export const EXPECT = 'expect';
export const EXPECT_ENUM = 'expect_enum';
export const EXPECT_SAME = 'expect_same';

const hostExepectations = [EXPECT, EXPECT_ENUM];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a quick question here, we have multiple files in model directory.
Like cluster.js, sapSystem.js and index.js

What kind of data should we always put in index.js?

For now, it is mostly cluster details view related.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, that's subjective, as long as it doesn't go out of control having one file is convenient

export const isHostExpectation = ({ type }) => hostExepectations.includes(type);

export const TARGET_HOST = 'host';
export const TARGET_CLUSTER = 'cluster';

export const targetTypes = [TARGET_HOST, TARGET_CLUSTER];
export const isValidTargetType = (targetType) =>
targetTypes.includes(targetType);

export const PASSING = 'passing';
export const WARNING = 'warning';
export const CRITICAL = 'critical';

export const AWS_PROVIDER = 'aws';
export const AZURE_PROVIDER = 'azure';
export const GCP_PROVIDER = 'gcp';
Expand Down
15 changes: 10 additions & 5 deletions assets/js/lib/test-utils/factories/executions.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,20 @@ const addExpectation = (checkResult, name, expectation, result) => {
};
};

export const addPassingExpectation = (checkResult, type, expectationName) => {
export const addExpectationWithResult = (
checkResult,
type,
expectationName,
result
) => {
const name = expectationName || faker.company.name();
const expectation = executionExpectationEvaluationFactory.build({
name,
type,
return_value: true,
return_value: result,
});

return addExpectation(checkResult, name, expectation, true);
return addExpectation(checkResult, name, expectation, result);
};

export const addExpectationWithError = (checkResult, expectationName) => {
Expand All @@ -237,7 +242,7 @@ export const addExpectationWithError = (checkResult, expectationName) => {
};

export const addPassingExpectExpectation = (checkResult, expectationName) =>
addPassingExpectation(checkResult, 'expect', expectationName);
addExpectationWithResult(checkResult, 'expect', expectationName, true);

export const addCriticalExpectExpectation = (checkResult, expectationName) => {
const name = expectationName || faker.company.name();
Expand All @@ -248,7 +253,7 @@ export const addCriticalExpectExpectation = (checkResult, expectationName) => {
return addExpectation(checkResult, name, expectation, false);
};
export const addPassingExpectSameExpectation = (checkResult, expectationName) =>
addPassingExpectation(checkResult, 'expect_same', expectationName);
addExpectationWithResult(checkResult, 'expect_same', expectationName, true);

export const agentsCheckResultsWithHostname = (
agentsCheckResults,
Expand Down
14 changes: 12 additions & 2 deletions assets/js/lib/test-utils/factories/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { faker } from '@faker-js/faker';
import { Factory } from 'fishery';

import { EXPECT, EXPECT_ENUM, EXPECT_SAME } from '@lib/model';

export * from './executions';
export * from './hosts';
export * from './sapSystems';
Expand Down Expand Up @@ -67,15 +69,23 @@ export const healthSummaryFactory = Factory.define(() => ({
export const catalogExpectExpectationFactory = Factory.define(
({ sequence }) => ({
name: `${faker.lorem.word()}_${sequence}`,
type: 'expect',
type: EXPECT,
expression: faker.lorem.sentence(),
})
);

export const catalogExpectSameExpectationFactory = Factory.define(
({ sequence }) => ({
name: `${faker.lorem.word()}_${sequence}`,
type: 'expect_same',
type: EXPECT_SAME,
expression: faker.lorem.sentence(),
})
);

export const catalogExpectEnumExpectationFactory = Factory.define(
({ sequence }) => ({
name: `${faker.lorem.word()}_${sequence}`,
type: EXPECT_ENUM,
expression: faker.lorem.sentence(),
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

import {
getAgentCheckResultByAgentID,
getExpectStatementsResults,
getHostExpectationStatementsResults,
getExpectSameStatementsResults,
getClusterCheckResults,
isAgentCheckError,
Expand All @@ -18,6 +18,7 @@ function CheckResultDetail({
expectations = [],
targetID,
targetType,
severity,
executionData,
}) {
const targetHost = isTargetHost(targetType);
Expand All @@ -38,7 +39,7 @@ function CheckResultDetail({
const isError = isAgentCheckError(targetResult);

const targetExpectationsResults = targetHost
? getExpectStatementsResults(expectations, expectation_evaluations)
? getHostExpectationStatementsResults(expectations, expectation_evaluations)
: getExpectSameStatementsResults(expectations, expectation_results);

const gatheredFacts = targetHost
Expand All @@ -49,6 +50,7 @@ function CheckResultDetail({
<>
<ExpectationsResults
isTargetHost={targetHost}
severity={severity}
results={targetExpectationsResults}
isError={isError}
errorMessage={message}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
addPassingExpectExpectation,
addPassingExpectSameExpectation,
addExpectationWithResult,
emptyCheckResultFactory,
hostFactory,
checksExecutionCompletedFactory,
Expand All @@ -9,8 +10,11 @@ import {
withOverriddenValues,
catalogExpectExpectationFactory,
catalogExpectSameExpectationFactory,
catalogExpectEnumExpectationFactory,
} from '@lib/test-utils/factories';

import { EXPECT_ENUM, WARNING } from '@lib/model';

import CheckResultDetail from './CheckResultDetail';

const clusterHosts = hostFactory.buildList(2);
Expand All @@ -20,6 +24,7 @@ const targetType = 'host';
const catalogExpectations = [
...catalogExpectExpectationFactory.buildList(5),
catalogExpectSameExpectationFactory.build(),
catalogExpectEnumExpectationFactory.build(),
];

const [
Expand All @@ -29,6 +34,7 @@ const [
{ name: expectationName4 },
{ name: expectationName5 },
{ name: expectationName6 },
{ name: expectationName7 },
] = catalogExpectations;

let checkResult = emptyCheckResultFactory.build({
Expand All @@ -43,6 +49,12 @@ checkResult = addCriticalExpectExpectation(checkResult, expectationName3);
checkResult = addCriticalExpectExpectation(checkResult, expectationName4);
checkResult = addCriticalExpectExpectation(checkResult, expectationName5);
checkResult = addPassingExpectSameExpectation(checkResult, expectationName6);
checkResult = addExpectationWithResult(
checkResult,
EXPECT_ENUM,
expectationName7,
WARNING
);

const checkResultWithoutValues = withOverriddenValues(checkResult, target1, []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe('CheckResultDetail Component', () => {
);

expect(screen.getAllByText('Passing')).toHaveLength(2);
expect(screen.getAllByText('Failing')).toHaveLength(3);
expect(screen.getAllByText('Critical')).toHaveLength(3);

values.forEach(({ name, value }) => {
expect(screen.getByText(name)).toBeVisible();
Expand Down Expand Up @@ -175,7 +175,7 @@ describe('CheckResultDetail Component', () => {
);

expect(screen.getAllByText('Passing')).toHaveLength(2);
expect(screen.queryAllByText('Failing')).toHaveLength(0);
expect(screen.queryAllByText('Critical')).toHaveLength(0);
expect(screen.queryByText('Values')).toBeNull();
expect(screen.getAllByLabelText('property tree')).toHaveLength(2);
});
Expand Down Expand Up @@ -221,7 +221,7 @@ describe('CheckResultDetail Component', () => {
);

expect(screen.queryAllByText('Passing')).toHaveLength(0);
expect(screen.queryAllByText('Failing')).toHaveLength(0);
expect(screen.queryAllByText('Critical')).toHaveLength(0);
expect(screen.getByText(factGatheringErrorMessage)).toBeVisible();

expect(screen.getByText('Expected Values unavailable')).toBeVisible();
Expand Down Expand Up @@ -273,7 +273,7 @@ describe('CheckResultDetail Component', () => {
);

expect(screen.queryAllByText('Passing')).toHaveLength(0);
expect(screen.queryAllByText('Failing')).toHaveLength(0);
expect(screen.queryAllByText('Critical')).toHaveLength(0);
expect(screen.getByText(timeoutMessage)).toBeVisible();

expect(values).toBeUndefined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import ResultsContainer from '@pages/ExecutionResults/ResultsContainer';
import CheckResultDetail from './CheckResultDetail';

import {
getCheckDescription,
getCheckExpectations,
findCheck,
isTargetHost,
isTargetCluster,
getClusterCheckResults,
Expand Down Expand Up @@ -163,7 +162,7 @@ function CheckResultDetailPage({ targetType }) {
);
}

const checkDescription = getCheckDescription(catalog, checkID);
const { description, severity, expectations } = findCheck(catalog, checkID);

const resultTargetID = getResultTargetID(
targetID,
Expand All @@ -181,7 +180,7 @@ function CheckResultDetailPage({ targetType }) {
>
<CheckDetailHeader
checkID={checkID}
checkDescription={checkDescription}
checkDescription={description}
targetID={targetID}
targetType={targetType}
resultTargetType={resultTargetType}
Expand Down Expand Up @@ -220,7 +219,8 @@ function CheckResultDetailPage({ targetType }) {
>
<CheckResultDetail
checkID={checkID}
expectations={getCheckExpectations(catalog, checkID)}
severity={severity}
expectations={expectations}
targetID={resultTargetID}
targetType={resultTargetType}
executionData={executionData}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
import React from 'react';
import { capitalize } from 'lodash';
import classNames from 'classnames';

import { WARNING, CRITICAL } from '@lib/model';
import ListView from '@common/ListView';
import classNames from 'classnames';

import { normalizeExpectationResult } from '../checksUtils';

function ExpectationsResults({
isTargetHost = true,
severity,
results,
isError = false,
errorMessage = 'An error occurred',
}) {
const renderedResults = isTargetHost
? results.map(({ name, return_value, failure_message, message }) => ({
name,
passing: !!return_value,
result: normalizeExpectationResult(return_value, severity),
failureMessage: failure_message,
evaluationErrorMessage: message,
}))
: results.map(({ name, result, failure_message, message }) => ({
name,
passing: !!result,
result: normalizeExpectationResult(result, severity),
failureMessage: failure_message,
evaluationErrorMessage: message,
}));

const expectationsEvaluations = renderedResults.map(
({ name, passing, failureMessage, evaluationErrorMessage }) => ({
({ name, result, failureMessage, evaluationErrorMessage }) => ({
title: name,
content: passing,
render: (isPassing) => (
content: result || CRITICAL,
render: (content) => (
<div
className={classNames({
'text-red-500': !isPassing,
'text-red-500': content === CRITICAL,
'text-yellow-500': content === WARNING,
})}
>
<span>{isPassing ? 'Passing' : 'Failing'}</span>
<span>{capitalize(content)}</span>
{failureMessage && <span className="block">{failureMessage}</span>}
{evaluationErrorMessage && (
<span className="block">{evaluationErrorMessage}</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
failingExpectEvaluationFactory,
} from '@lib/test-utils/factories';

import { WARNING } from '@lib/model';

import ExpectationsResults from './ExpectationsResults';

export default {
Expand All @@ -30,6 +32,13 @@ export const WithFailureMessage = {
},
};

export const WithWarning = {
args: {
...WithFailureMessage.args,
severity: WARNING,
},
};

export const WithEvaluationError = {
args: {
...Default.args,
Expand Down
Loading
Loading