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

[Fleet] fixed bug where fleet server instructions were incorrectly shown #125357

Merged
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
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:/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