Skip to content

Commit

Permalink
fetch agentless policy only in serverless
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcold committed Dec 6, 2023
1 parent add2b37 commit 48b5d37
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { renderHook, act } from '@testing-library/react-hooks';

import { SetupTechnology } from '../../../../../../../../common/types';
import { ExperimentalFeaturesService } from '../../../../../services';
import { sendGetOneAgentPolicy } from '../../../../../hooks';
import { sendGetOneAgentPolicy, useStartServices } from '../../../../../hooks';
import { SelectedPolicyTab } from '../../components';

import { useSetupTechnology } from './setup_technology';
Expand All @@ -18,6 +18,7 @@ jest.mock('../../../../../services');
jest.mock('../../../../../hooks', () => ({
...jest.requireActual('../../../../../hooks'),
sendGetOneAgentPolicy: jest.fn(),
useStartServices: jest.fn(),
}));

type MockFn = jest.MockedFunction<any>;
Expand All @@ -41,6 +42,11 @@ describe('useSetupTechnology', () => {
item: { id: 'agentless-policy-id' },
},
});
(useStartServices as MockFn).mockReturnValue({
cloud: {
isServerlessEnabled: true,
},
});
jest.clearAllMocks();
});

Expand Down Expand Up @@ -78,6 +84,27 @@ describe('useSetupTechnology', () => {
expect(result.current.agentlessPolicy).toEqual({ id: 'agentless-policy-id' });
});

it('should not fetch agentless policy if agentless is enabled but serverless is disabled', async () => {
(useStartServices as MockFn).mockReturnValue({
cloud: {
isServerlessEnabled: false,
},
});

const { result } = renderHook(() =>
useSetupTechnology({
updateNewAgentPolicy: updateNewAgentPolicyMock,
newAgentPolicy: newAgentPolicyMock,
updateAgentPolicy: updateAgentPolicyMock,
setSelectedPolicyTab: setSelectedPolicyTabMock,
})
);

expect(sendGetOneAgentPolicy).not.toHaveBeenCalled();
expect(result.current.selectedSetupTechnology).toBe(SetupTechnology.AGENT_BASED);
expect(result.current.agentlessPolicy).toBeUndefined();
});

it('should update agent policy and selected policy tab when setup technology is agentless', async () => {
const { result, waitForNextUpdate } = renderHook(() =>
useSetupTechnology({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useCallback, useEffect, useState } from 'react';
import { ExperimentalFeaturesService } from '../../../../../services';
import type { AgentPolicy, NewAgentPolicy } from '../../../../../types';
import { SetupTechnology } from '../../../../../types';
import { sendGetOneAgentPolicy } from '../../../../../hooks';
import { sendGetOneAgentPolicy, useStartServices } from '../../../../../hooks';
import { SelectedPolicyTab } from '../../components';

const AGENTLESS_POLICY_ID = 'agentless';
Expand All @@ -27,6 +27,8 @@ export function useSetupTechnology({
setSelectedPolicyTab: (tab: SelectedPolicyTab) => void;
}) {
const { agentless: isAgentlessEnabled } = ExperimentalFeaturesService.get();
const { cloud } = useStartServices();
const isServerless = cloud?.isServerlessEnabled ?? false;
const [selectedSetupTechnology, setSelectedSetupTechnology] = useState<SetupTechnology>(
SetupTechnology.AGENT_BASED
);
Expand All @@ -42,10 +44,10 @@ export function useSetupTechnology({
}
};

if (isAgentlessEnabled) {
if (isAgentlessEnabled && isServerless) {
fetchAgentlessPolicy();
}
}, [isAgentlessEnabled]);
}, [isAgentlessEnabled, isServerless]);

const handleSetupTechnologyChange = useCallback(
(setupTechnology) => {
Expand Down

0 comments on commit 48b5d37

Please sign in to comment.