Skip to content
Open
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 frontend/packages/console-app/locales/en/console-app.json
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,6 @@
"Inventory": "Inventory",
"Images": "Images",
"Image": "Image",
"Virtual machines": "Virtual machines",
"This count reflects your access permissions and might not include all virtual machines.": "This count reflects your access permissions and might not include all virtual machines.",
"Virtual machine": "Virtual machine",
"Health checks": "Health checks",
"See details": "See details",
"{{ cpuMessage }}": "{{ cpuMessage }}",
Expand All @@ -412,6 +409,9 @@
"Utilization": "Utilization",
"Network transfer": "Network transfer",
"Pod count": "Pod count",
"Virtual machines": "Virtual machines",
"The total shown is based on your access permissions and might not include all virtual machines.": "The total shown is based on your access permissions and might not include all virtual machines.",
"Virtual machine": "Virtual machine",
"Node conditions": "Node conditions",
"Reason": "Reason",
"Updated": "Updated",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import {
DescriptionListTerm,
} from '@patternfly/react-core';
import { useTranslation } from 'react-i18next';
import { Link } from 'react-router';
import BareMetalInventoryItems from '@console/app/src/components/nodes/node-dashboard/BareMetalInventoryItems';
import VirtualMachinesInventoryItems from '@console/app/src/components/nodes/node-dashboard/VirtualMachinesInventoryItems';
import { FLAG_NODE_MGMT_V1 } from '@console/app/src/consts';
import { useK8sWatchResource } from '@console/internal/components/utils/k8s-watch-hook';
import { resourcePathFromModel } from '@console/internal/components/utils/resource-link';
import { PodModel, NodeModel } from '@console/internal/models';
Expand All @@ -24,9 +25,7 @@ import {
ResourceInventoryItem,
} from '@console/shared/src/components/dashboard/inventory-card/InventoryItem';
import { getPodStatusGroups } from '@console/shared/src/components/dashboard/inventory-card/utils';
import { DescriptionListTermHelp } from '@console/shared/src/components/description-list/DescriptionListTermHelp';
import { useIsKubevirtPluginActive } from '../../../utils/kubevirt';
import { useWatchVirtualMachineInstances, VirtualMachineModel } from '../NodeVmUtils';
import { useFlag } from '@console/shared/src/hooks/useFlag';
import { NodeDashboardContext } from './NodeDashboardContext';

export const NodeInventoryItem: FC<NodeInventoryItemProps> = ({ nodeName, model, mapper }) => {
Expand Down Expand Up @@ -56,9 +55,7 @@ export const NodeInventoryItem: FC<NodeInventoryItemProps> = ({ nodeName, model,
const InventoryCard: FC = () => {
const { obj } = useContext(NodeDashboardContext);
const { t } = useTranslation();

const showVms = useIsKubevirtPluginActive();
const [vms, vmsLoaded, vmsLoadError] = useWatchVirtualMachineInstances(obj.metadata.name);
const nodeMgmtV1Enabled = useFlag(FLAG_NODE_MGMT_V1);

return (
<Card data-test-id="inventory-card">
Expand Down Expand Up @@ -89,32 +86,12 @@ const InventoryCard: FC = () => {
/>
</DescriptionListDescription>
</DescriptionListGroup>
<BareMetalInventoryItems />
{showVms ? (
<DescriptionListGroup>
<DescriptionListTermHelp
text={t('console-app~Virtual machines')}
textHelp={t(
'console-app~This count reflects your access permissions and might not include all virtual machines.',
)}
/>
<DescriptionListDescription>
<Link
to={`${resourcePathFromModel(VirtualMachineModel)}/search?rowFilter-node=${
obj.metadata.name
}`}
>
<InventoryItem
isLoading={!vmsLoaded}
title={t('console-app~Virtual machine')}
titlePlural={t('console-app~Virtual machines')}
count={vms.length}
error={!!vmsLoadError}
/>
</Link>
</DescriptionListDescription>
</DescriptionListGroup>
) : null}
{nodeMgmtV1Enabled && (
<>
<BareMetalInventoryItems />
<VirtualMachinesInventoryItems />
</>
)}
</DescriptionList>
</CardBody>
</Card>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import type { FC } from 'react';
import { useContext } from 'react';
import { DescriptionListDescription, DescriptionListGroup } from '@patternfly/react-core';
import { useTranslation } from 'react-i18next';
import { Link } from 'react-router';
import {
useWatchVirtualMachineInstances,
VirtualMachineModel,
} from '@console/app/src/components/nodes/NodeVmUtils';
import { useIsKubevirtPluginActive } from '@console/app/src/utils/kubevirt';
import { resourcePathFromModel } from '@console/internal/components/utils/resource-link';
import { InventoryItem } from '@console/shared/src/components/dashboard/inventory-card/InventoryItem';
import { DescriptionListTermHelp } from '@console/shared/src/components/description-list/DescriptionListTermHelp';
import { NodeDashboardContext } from './NodeDashboardContext';

const VirtualMachinesInventoryItems: FC = () => {
const { obj } = useContext(NodeDashboardContext);
const { t } = useTranslation();
const showVms = useIsKubevirtPluginActive();

const [vms, vmsLoaded, vmsLoadError] = useWatchVirtualMachineInstances(obj.metadata.name);

if (!showVms) {
return null;
}

return (
<DescriptionListGroup>
<DescriptionListTermHelp
text={t('console-app~Virtual machines')}
textHelp={t(
'console-app~The total shown is based on your access permissions and might not include all virtual machines.',
)}
/>
<DescriptionListDescription>
<Link
to={`${resourcePathFromModel(VirtualMachineModel)}/search?rowFilter-node=${
obj.metadata.name
}`}
>
<InventoryItem
isLoading={!vmsLoaded}
title={t('console-app~Virtual machine')}
titlePlural={t('console-app~Virtual machines')}
count={vms.length}
error={!!vmsLoadError}
/>
</Link>
</DescriptionListDescription>
</DescriptionListGroup>
);
};

export default VirtualMachinesInventoryItems;
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { render, screen } from '@testing-library/react';
import { useK8sWatchResource } from '@console/internal/components/utils/k8s-watch-hook';
import type { NodeKind } from '@console/internal/module/k8s';
import { useFlag } from '@console/shared/src/hooks/useFlag';
import BareMetalInventoryItems from '../BareMetalInventoryItems';
import InventoryCard from '../InventoryCard';
import { NodeDashboardContext } from '../NodeDashboardContext';
import VirtualMachinesInventoryItems from '../VirtualMachinesInventoryItems';

jest.mock('@console/shared/src/hooks/useFlag', () => ({
useFlag: jest.fn(),
}));

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

// Mock child components using jest.fn
jest.mock('@console/app/src/components/nodes/node-dashboard/BareMetalInventoryItems', () => ({
__esModule: true,
default: jest.fn(() => null),
}));

jest.mock('@console/app/src/components/nodes/node-dashboard/VirtualMachinesInventoryItems', () => ({
__esModule: true,
default: jest.fn(() => null),
}));

// Mock InventoryItem components
jest.mock('@console/shared/src/components/dashboard/inventory-card/InventoryItem', () => ({
InventoryItem: ({ count }) => `Images: ${count || 0}`,
ResourceInventoryItem: () => 'Pod Inventory',
}));

const useFlagMock = useFlag as jest.Mock;
const useK8sWatchResourceMock = useK8sWatchResource as jest.Mock;
const BareMetalInventoryItemsMock = BareMetalInventoryItems as jest.Mock;
const VirtualMachinesInventoryItemsMock = VirtualMachinesInventoryItems as jest.Mock;

describe('InventoryCard', () => {
const mockNode: NodeKind = {
apiVersion: 'v1',
kind: 'Node',
metadata: {
name: 'test-node',
uid: 'node-uid',
},
spec: {},
status: {
images: [{ names: ['image1'] }, { names: ['image2'] }],
},
};

const renderWithContext = (node: NodeKind = mockNode) => {
return render(
<NodeDashboardContext.Provider
value={{
obj: node,
setCPULimit: () => {},
setMemoryLimit: () => {},
setHealthCheck: () => {},
}}
>
<InventoryCard />
</NodeDashboardContext.Provider>,
);
};

beforeEach(() => {
jest.clearAllMocks();
useK8sWatchResourceMock.mockReturnValue([[], true, undefined]);
});

it('should render the inventory card with title', () => {
useFlagMock.mockReturnValue(false);
renderWithContext();

expect(screen.getByText('Inventory')).toBeVisible();
});

it('should render Pod and Image inventory items when NODE_MGMT_V1 flag state is false', () => {
useFlagMock.mockReturnValue(false);
renderWithContext();

expect(screen.getByText('Pods')).toBeVisible();
expect(screen.getByText('Pod Inventory')).toBeVisible();
expect(screen.getByText('Images')).toBeVisible();
expect(screen.getByText('Images: 2')).toBeVisible();
});

it('should render Pod and Image inventory items when NODE_MGMT_V1 flag state is true', () => {
useFlagMock.mockReturnValue(true);
renderWithContext();

expect(screen.getByText('Pods')).toBeVisible();
expect(screen.getByText('Pod Inventory')).toBeVisible();
expect(screen.getByText('Images')).toBeVisible();
expect(screen.getByText('Images: 2')).toBeVisible();
});

it('should not render BareMetalInventoryItems when NODE_MGMT_V1 flag is off', () => {
useFlagMock.mockReturnValue(false);
renderWithContext();

expect(BareMetalInventoryItemsMock).not.toHaveBeenCalled();
});

it('should not render VirtualMachinesInventoryItems when NODE_MGMT_V1 flag is off', () => {
useFlagMock.mockReturnValue(false);
renderWithContext();

expect(VirtualMachinesInventoryItemsMock).not.toHaveBeenCalled();
});

it('should render BareMetalInventoryItems when NODE_MGMT_V1 flag is on', () => {
useFlagMock.mockReturnValue(true);
renderWithContext();

expect(BareMetalInventoryItemsMock).toHaveBeenCalled();
});

it('should render VirtualMachinesInventoryItems when NODE_MGMT_V1 flag is on', () => {
useFlagMock.mockReturnValue(true);
renderWithContext();

expect(VirtualMachinesInventoryItemsMock).toHaveBeenCalled();
});
});
Loading