Skip to content

Commit

Permalink
fixed bug where fleet server instructions were incorrectly shown (ela…
Browse files Browse the repository at this point in the history
…stic#125357) (elastic#125374)

(cherry picked from commit 2d93bcb)

Co-authored-by: Julia Bardi <[email protected]>
  • Loading branch information
kibanamachine and juliaElastic authored Feb 11, 2022
1 parent 8d7068e commit a5d761b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jest.mock('../../hooks/use_request', () => {
sendGetFleetStatus: jest.fn(),
sendGetOneAgentPolicy: jest.fn(),
useGetAgents: jest.fn(),
useGetAgentPolicies: jest.fn(),
};
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
sendGetFleetStatus,
sendGetOneAgentPolicy,
useGetAgents,
useGetAgentPolicies,
} from '../../hooks/use_request';
import { FleetStatusProvider, ConfigContext } from '../../hooks';

Expand Down Expand Up @@ -101,6 +102,10 @@ describe('<AgentEnrollmentFlyout />', () => {
data: { items: [{ policy_id: 'fleet-server-policy' }] },
});

(useGetAgentPolicies as jest.Mock).mockReturnValue?.({
data: { items: [{ id: 'fleet-server-policy' }] },
});

await act(async () => {
testBed = await setup({
agentPolicies: [{ id: 'fleet-server-policy' } as AgentPolicy],
Expand Down Expand Up @@ -143,6 +148,25 @@ describe('<AgentEnrollmentFlyout />', () => {
});
});

describe('with a specific policy when no agentPolicies set', () => {
beforeEach(async () => {
jest.clearAllMocks();
await act(async () => {
testBed = await setup({
agentPolicy: testAgentPolicy,
onClose: jest.fn(),
});
testBed.component.update();
});
});

it('should not show fleet server instructions', () => {
const { exists } = testBed;
expect(exists('agentEnrollmentFlyout')).toBe(true);
expect(AgentEnrollmentKeySelectionStep).toHaveBeenCalled();
});
});

// Skipped due to implementation details in the step components. See https://github.com/elastic/kibana/issues/103894
describe.skip('"View data" extension point', () => {
it('shows the "View data" step when UI extension is provided', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ import type { EuiContainedStepProps } from '@elastic/eui/src/components/steps/st
import { i18n } from '@kbn/i18n';
import { FormattedMessage } from '@kbn/i18n-react';

import { useGetOneEnrollmentAPIKey, useLink, useFleetStatus, useGetAgents } from '../../hooks';
import {
useGetOneEnrollmentAPIKey,
useLink,
useFleetStatus,
useGetAgents,
useGetAgentPolicies,
} from '../../hooks';

import { ManualInstructions } from '../../components/enrollment_instructions';
import {
Expand Down Expand Up @@ -81,14 +87,24 @@ export const ManagedInstructions = React.memo<Props>(
showInactive: false,
});

const { data: agentPoliciesData, isLoading: isLoadingAgentPolicies } = useGetAgentPolicies({
page: 1,
perPage: 1000,
full: true,
});

const fleetServers = useMemo(() => {
const fleetServerAgentPolicies: string[] = (agentPolicies ?? [])
let policies = agentPolicies;
if (!agentPolicies && !isLoadingAgentPolicies) {
policies = agentPoliciesData?.items;
}
const fleetServerAgentPolicies: string[] = (policies ?? [])
.filter((pol) => policyHasFleetServer(pol))
.map((pol) => pol.id);
return (agents?.items ?? []).filter((agent) =>
fleetServerAgentPolicies.includes(agent.policy_id ?? '')
);
}, [agents, agentPolicies]);
}, [agents, agentPolicies, agentPoliciesData, isLoadingAgentPolicies]);

const fleetServerSteps = useMemo(() => {
const {
Expand Down

0 comments on commit a5d761b

Please sign in to comment.