Skip to content

Commit

Permalink
feat(OCPADVISOR-88): Hide upgrade risks for managed clusters (#560)
Browse files Browse the repository at this point in the history
* Hide upgrade risks for managed clusters

* Assume that all clusters are managed while fetching info

* Create hook for upgrade risks enable/disable; improve tests

* Add extra tests for active_tab
  • Loading branch information
gkarat authored Apr 25, 2023
1 parent 7f864a2 commit 084cccf
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"cluster": {
"cluster_id": "dcb95bbf-8673-4f3a-a63c-12d4a530aa6f",
"display_name": "Cluster With Issues",
"managed": false,
"status": "Stale"
},
"status": "ok"
}
39 changes: 35 additions & 4 deletions cypress/utils/interceptors.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import singleClusterPageReport from '../fixtures/api/insights-results-aggregator/v2/cluster/dcb95bbf-8673-4f3a-a63c-12d4a530aa6f/reports-disabled-false.json';
import upgradeRisksFixtures from '../fixtures/api/insights-results-aggregator/v1/clusters/41c30565-b4c9-49f2-a4ce-3277ad22b258/upgrade-risks-prediction.json';
import clusterInfoFixtures from '../fixtures/api/insights-results-aggregator/v2/cluster/dcb95bbf-8673-4f3a-a63c-12d4a530aa6f/info.json';
import _ from 'lodash';

export const clusterReportsInterceptors = {
successful: () =>
Expand Down Expand Up @@ -27,7 +29,7 @@ export const clusterReportsInterceptors = {
}
),
'successful, cluster name is null': () => {
const report = singleClusterPageReport;
const report = _.cloneDeep(singleClusterPageReport);
report.report.meta.cluster_name = '';

cy.intercept(
Expand All @@ -39,7 +41,7 @@ export const clusterReportsInterceptors = {
);
},
'successful, no rules': () => {
const report = singleClusterPageReport;
const report = _.cloneDeep(singleClusterPageReport);
report.report.data = [];

cy.intercept(
Expand Down Expand Up @@ -70,7 +72,7 @@ export const upgradeRisksInterceptors = {
}
),
'successful, alerts empty': () => {
const fixtures = upgradeRisksFixtures;
const fixtures = _.cloneDeep(upgradeRisksFixtures);
fixtures.upgrade_recommendation.upgrade_risks_predictors.alerts = [];
cy.intercept(
'GET',
Expand All @@ -82,7 +84,7 @@ export const upgradeRisksInterceptors = {
);
},
'successful, empty': () => {
const fixtures = upgradeRisksFixtures;
const fixtures = _.cloneDeep(upgradeRisksFixtures);
fixtures.upgrade_recommendation.upgrade_risks_predictors = {
alerts: [],
operator_conditions: [],
Expand Down Expand Up @@ -129,3 +131,32 @@ export const upgradeRisksInterceptors = {
}
),
};

export const clusterInfoInterceptors = {
successful: () =>
cy
.intercept(
'GET',
/\/api\/insights-results-aggregator\/v2\/cluster\/.*\/info/,
{
statusCode: 200,
body: clusterInfoFixtures,
}
)
.as('clusterInfo'),
'successful, managed': () => {
const fixtures = _.cloneDeep(clusterInfoFixtures);
fixtures.cluster.managed = true;

return cy
.intercept(
'GET',
/\/api\/insights-results-aggregator\/v2\/cluster\/.*\/info/,
{
statusCode: 200,
body: fixtures,
}
)
.as('clusterInfo');
},
};
71 changes: 56 additions & 15 deletions src/Components/Cluster/Cluster.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
checkRowCounts,
} from '../../../cypress/utils/table';
import {
clusterInfoInterceptors,
clusterReportsInterceptors as interceptors,
upgradeRisksInterceptors,
} from '../../../cypress/utils/interceptors';
Expand All @@ -15,8 +16,7 @@ const CLUSTER_HEADER = '#cluster-header';
const BREADCRUMBS = 'nav[class=pf-c-breadcrumb]';
const RULES_TABLE = '#cluster-recs-list-table';
const FILTER_CHIPS = 'li[class=pf-c-chip-group__list-item]';
const ALERT = '[data-ouia-component-type="PF4/Alert"]';
const TAB_BUTTON = '[data-ouia-component-type="PF4/TabButton"]';
const TAB_BUTTON = '[data-ouia-component-type="PF4/TabButton"]'; // TODO: move to utils

const CLUSTER_ID = '123';
const CLUSTER_NAME = 'Cluster With Issues';
Expand Down Expand Up @@ -136,6 +136,8 @@ describe('cluster page', () => {

describe('upgrade risks', () => {
beforeEach(() => {
upgradeRisksInterceptors.successful();
clusterInfoInterceptors.successful();
cy.intercept('/feature_flags*', {
statusCode: 200,
body: {
Expand All @@ -161,22 +163,40 @@ describe('upgrade risks', () => {
.and('have.text', 'RecommendationsUpgrade risks');
});

describe('managed clusters', () => {
it('does not render banner and tab', () => {
clusterInfoInterceptors['successful, managed'](); // override interceptor
mount();

cy.ouiaId('recommendations').should(
'have.attr',
'data-ouia-safe',
'true'
);
cy.wait('@clusterInfo');

cy.ouiaId('upgrade-risks-alert').should('not.exist');
cy.get(TAB_BUTTON)
.should('have.length', 1)
.and('have.text', 'Recommendations');
});
});

it('has some upgrade risks', () => {
upgradeRisksInterceptors.successful();
mount();

cy.get(ALERT).should('have.class', 'pf-m-warning');
cy.get(ALERT).within(() => {
cy.ouiaId('upgrade-risks-alert').should('have.class', 'pf-m-warning');
cy.ouiaId('upgrade-risks-alert').within(() => {
cy.get('h4').should('contain.text', 'Resolve upgrade risks');
});
});

it('has no upgrade risks', () => {
upgradeRisksInterceptors['successful, empty']();
upgradeRisksInterceptors['successful, empty'](); // override interceptor
mount();

cy.get(ALERT).should('have.class', 'pf-m-success');
cy.get(ALERT).within(() => {
cy.ouiaId('upgrade-risks-alert').should('have.class', 'pf-m-success');
cy.ouiaId('upgrade-risks-alert').within(() => {
cy.get('h4').should(
'contain.text',
'No known upgrade risks identified for this cluster.'
Expand All @@ -185,11 +205,11 @@ describe('upgrade risks', () => {
});

it('upgrade risks not found', () => {
upgradeRisksInterceptors['error, not found']();
upgradeRisksInterceptors['error, not found'](); // override interceptor
mount();

cy.get(ALERT).should('have.class', 'pf-m-warning');
cy.get(ALERT).within(() => {
cy.ouiaId('upgrade-risks-alert').should('have.class', 'pf-m-warning');
cy.ouiaId('upgrade-risks-alert').within(() => {
cy.get('h4').should(
'contain.text',
'Upgrade risks are not currently available.'
Expand All @@ -198,22 +218,43 @@ describe('upgrade risks', () => {
});

it('should not render alert in other error', () => {
upgradeRisksInterceptors['error, other']();
upgradeRisksInterceptors['error, other'](); // override interceptor
mount();

cy.get(ALERT).should('not.exist');
cy.ouiaId('upgrade-risks-alert').should('not.exist');
});

it('should not render alert in the loading state', () => {
upgradeRisksInterceptors['long responding']();
upgradeRisksInterceptors['long responding'](); // override interceptor
mount();

cy.get(ALERT).should('not.exist');
cy.ouiaId('upgrade-risks-alert').should('not.exist');
});

describe('active_tab search parameter', () => {
it('opens upgrade risks tab', () => {
mount(['/clusters/123?active_tab=upgrade_risks']);
cy.ouiaId('upgrade-risks-tab').should(
'have.attr',
'aria-selected',
'true'
);
});

it('opens recommendations tab', () => {
mount(['/clusters/123?active_tab=recommendations']);
cy.ouiaId('recommendations-tab').should(
'have.attr',
'aria-selected',
'true'
);
});
});

describe('analytics tracking', () => {
beforeEach(() => {
cy.intercept('/analytics/track').as('track');
clusterInfoInterceptors.successful(); // override interceptor
});

it('should track click on upgrade risks tab', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/Components/Cluster/Cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import Breadcrumbs from '../Breadcrumbs';
import ClusterTabs from '../ClusterTabs/ClusterTabs';
import { Flex, FlexItem, PageSection } from '@patternfly/react-core';
import { UpgradeRisksAlert } from '../UpgradeRisksAlert';
import { useUpgradeRisksFeatureFlag } from '../../Utilities/useFeatureFlag';
import useUpgradeRisksFeature from '../UpgradeRisksTable/useUpgradeRisksFeature';

export const Cluster = ({ cluster, clusterId }) => {
const upgradeRisksEnabled = useUpgradeRisksFeatureFlag();
const areUpgradeRisksEnabled = useUpgradeRisksFeature(clusterId);

// TODO: make breadcrumbs take display name from GET /cluster/id/info
return (
Expand All @@ -25,7 +25,7 @@ export const Cluster = ({ cluster, clusterId }) => {
/>
<ClusterHeader />
</FlexItem>
{upgradeRisksEnabled && <UpgradeRisksAlert />}
{areUpgradeRisksEnabled && <UpgradeRisksAlert />}
</Flex>
</PageHeader>
<PageSection>
Expand Down
2 changes: 1 addition & 1 deletion src/Components/ClusterHeader/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const ClusterHeaderWrapper = () => {
});
const clusterInfo = useGetClusterInfoQuery({
id: clusterId,
});
}); // TODO: improve cache handling: do network request only when cache is empty

return (
<ClusterHeader
Expand Down
13 changes: 9 additions & 4 deletions src/Components/ClusterTabs/ClusterTabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Card, CardBody, Tab, Tabs } from '@patternfly/react-core';
import React, { useEffect, useState } from 'react';

import { useIntl } from 'react-intl';
import { useSearchParams } from 'react-router-dom';
import { useParams, useSearchParams } from 'react-router-dom';
import messages from '../../Messages';
import { setSearchParameter } from '../../Utilities/Helpers';
import { useUpgradeRisksFeatureFlag } from '../../Utilities/useFeatureFlag';
import ClusterRules from '../ClusterRules/ClusterRules';
import { UpgradeRisksTable } from '../UpgradeRisksTable';
import { UpgradeRisksTracker } from '../UpgradeRisksTracker';
import useUpgradeRisksFeature from '../UpgradeRisksTable/useUpgradeRisksFeature';

const CLUSTER_TABS = ['recommendations', 'upgrade_risks'];

Expand All @@ -17,9 +18,12 @@ const ClusterTabs = () => {
const upgradeRisksEnabled = useUpgradeRisksFeatureFlag();
const [searchParams] = useSearchParams();

const { clusterId } = useParams();
const areUpgradeRisksEnabled = useUpgradeRisksFeature(clusterId);

const [activeKey, setActiveKey] = useState(() => {
const activeTab = searchParams.get('active_tab');
return upgradeRisksEnabled
return areUpgradeRisksEnabled
? CLUSTER_TABS.includes(activeTab)
? activeTab
: 'recommendations'
Expand All @@ -28,7 +32,7 @@ const ClusterTabs = () => {

useEffect(() => {
if (
upgradeRisksEnabled &&
areUpgradeRisksEnabled &&
searchParams.get('active_tab') === 'upgrade_risks'
) {
setActiveKey('upgrade_risks');
Expand All @@ -49,10 +53,11 @@ const ClusterTabs = () => {
<Tab
eventKey="recommendations"
title={intl.formatMessage(messages.recommendations)}
ouiaId="recommendations-tab"
>
{activeKey === 'recommendations' && <ClusterRules />}
</Tab>
{upgradeRisksEnabled && (
{areUpgradeRisksEnabled && (
<Tab
eventKey="upgrade_risks"
title={intl.formatMessage(messages.upgradeRisks)}
Expand Down
3 changes: 3 additions & 0 deletions src/Components/UpgradeRisksAlert/UpgradeRisksAlert.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const UpgradeRisksAlert = () => {
variant="warning"
isInline
title={intl.formatMessage(messages.resolveUpgradeRisks)}
ouiaId="upgrade-risks-alert"
>
{intl.formatMessage(messages.resolveUpgradeRisksDesc, { strong })}
</Alert>
Expand All @@ -32,12 +33,14 @@ const UpgradeRisksAlert = () => {
variant="success"
isInline
title={intl.formatMessage(messages.noKnownUpgradeRisks)}
ouiaId="upgrade-risks-alert"
/>
) : isError && error.status === 404 ? (
<Alert
variant="warning"
isInline
title={intl.formatMessage(messages.upgradeRisksNotCurrentlyAvailable)}
ouiaId="upgrade-risks-alert"
>
{intl.formatMessage(messages.upgradeRisksNotAvailableDesc)}
</Alert>
Expand Down
16 changes: 16 additions & 0 deletions src/Components/UpgradeRisksTable/useUpgradeRisksFeature.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import get from 'lodash/get';
import { useParams } from 'react-router-dom';

import { useUpgradeRisksFeatureFlag } from '../../Utilities/useFeatureFlag';
import { useGetClusterInfoState } from '../../Services/SmartProxy';

const useUpgradeRisksFeature = (clusterId) => {
const upgradeRisksEnabled = useUpgradeRisksFeatureFlag();
const id = clusterId || useParams().clusterId;
const clusterInfo = useGetClusterInfoState({ id }); // doesn't request new data, uses cache
const isManaged = get(clusterInfo, 'data.managed', true);

return upgradeRisksEnabled && !isManaged;
};

export default useUpgradeRisksFeature;
1 change: 1 addition & 0 deletions src/Services/SmartProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const SmartProxyApi = createApi({
export const {
endpoints: {
getUpgradeRisks: { useQueryState: useGetUpgradeRisksState },
getClusterInfo: { useQueryState: useGetClusterInfoState },
},
useGetClusterByIdQuery,
useLazyGetClusterByIdQuery,
Expand Down

0 comments on commit 084cccf

Please sign in to comment.