[8.8] [Fleet] Fix bulk action selection error (#158914) (#158943)

# Backport

This will backport the following commits from `main` to `8.8`:
- [[Fleet] Fix bulk action selection error
(#158914)](https://github.com/elastic/kibana/pull/158914)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Nicolas
Chaulet","email":"nicolas.chaulet@elastic.co"},"sourceCommit":{"committedDate":"2023-06-02T15:18:07Z","message":"[Fleet]
Fix bulk action selection error
(#158914)","sha":"39a67ec337c9ea68a464023dcb9550ff7b0d1747","branchLabelMapping":{"^v8.9.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Fleet","backport:prev-minor","v8.9.0","v8.8.1"],"number":158914,"url":"https://github.com/elastic/kibana/pull/158914","mergeCommit":{"message":"[Fleet]
Fix bulk action selection error
(#158914)","sha":"39a67ec337c9ea68a464023dcb9550ff7b0d1747"}},"sourceBranch":"main","suggestedTargetBranches":["8.8"],"targetPullRequestStates":[{"branch":"main","label":"v8.9.0","labelRegex":"^v8.9.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/158914","number":158914,"mergeCommit":{"message":"[Fleet]
Fix bulk action selection error
(#158914)","sha":"39a67ec337c9ea68a464023dcb9550ff7b0d1747"}},{"branch":"8.8","label":"v8.8.1","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Nicolas Chaulet <nicolas.chaulet@elastic.co>
This commit is contained in:
Kibana Machine 2023-06-02 13:25:10 -04:00 committed by GitHub
parent 21beb7d50e
commit cb3763e935
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 123 additions and 1 deletions

View file

@ -13,6 +13,7 @@ import type { Agent } from '../../../../types';
import { createFleetTestRendererMock } from '../../../../../../mock';
import { ExperimentalFeaturesService } from '../../../../services';
import { AgentReassignAgentPolicyModal } from '../../components/agent_reassign_policy_modal';
import { sendGetAgents, sendGetAgentPolicies } from '../../../../hooks';
@ -27,6 +28,8 @@ jest.mock('../../../../hooks', () => ({
sendGetAgentPolicies: jest.fn(),
}));
jest.mock('../../components/agent_reassign_policy_modal');
const mockedSendGetAgents = sendGetAgents as jest.Mock;
const mockedSendGetAgentPolicies = sendGetAgentPolicies as jest.Mock;
@ -37,6 +40,11 @@ describe('AgentBulkActions', () => {
} as any);
});
beforeEach(() => {
jest.mocked(AgentReassignAgentPolicyModal).mockReset();
jest.mocked(AgentReassignAgentPolicyModal).mockReturnValue(null);
});
function render(props: any) {
const renderer = createFleetTestRendererMock();
@ -248,5 +256,119 @@ describe('AgentBulkActions', () => {
results.getByText('Request diagnostics for 8 agents').closest('button')!
).toBeEnabled();
});
it('should generate a correct kuery to select agents', async () => {
mockedSendGetAgentPolicies.mockResolvedValue({
data: {
items: [
{
name: 'Managed agent policy',
namespace: 'default',
description: '',
monitoring_enabled: ['logs', 'metrics'],
is_managed: true,
id: 'test-managed-policy',
},
],
},
});
mockedSendGetAgents.mockResolvedValueOnce({
data: {
items: [],
total: 0,
totalInactive: 0,
},
});
const selectedAgents: Agent[] = [];
const props = {
totalAgents: 10,
totalInactiveAgents: 0,
selectionMode: 'query',
currentQuery: '(Base query)',
selectedAgents,
visibleAgents: [],
refreshAgents: () => undefined,
allTags: [],
agentPolicies: [],
};
const results = render(props);
const bulkActionsButton = results.getByTestId('agentBulkActionsButton');
await act(async () => {
fireEvent.click(bulkActionsButton);
});
expect(results.getByText('Assign to new policy').closest('button')!).toBeEnabled();
await act(async () => {
fireEvent.click(results.getByText('Assign to new policy').closest('button')!);
});
expect(jest.mocked(AgentReassignAgentPolicyModal)).toHaveBeenCalledWith(
expect.objectContaining({
agents: '(Base query)',
}),
expect.anything()
);
});
it('should generate a correct kuery to select agents with managed agents too', async () => {
const selectedAgents: Agent[] = [];
mockedSendGetAgentPolicies.mockResolvedValue({
data: {
items: [
{
name: 'Managed agent policy',
namespace: 'default',
description: '',
monitoring_enabled: ['logs', 'metrics'],
is_managed: true,
id: 'test-managed-policy',
},
],
},
});
mockedSendGetAgents.mockResolvedValueOnce({
data: {
items: [{ id: 'agentId1' }, { id: 'agentId2' }],
total: 2,
totalInactive: 0,
},
});
const props = {
totalAgents: 10,
totalInactiveAgents: 0,
selectionMode: 'query',
currentQuery: '(Base query)',
selectedAgents,
visibleAgents: [],
refreshAgents: () => undefined,
allTags: [],
agentPolicies: [],
};
const results = render(props);
const bulkActionsButton = results.getByTestId('agentBulkActionsButton');
await act(async () => {
fireEvent.click(bulkActionsButton);
});
expect(results.getByText('Assign to new policy').closest('button')!).toBeEnabled();
await act(async () => {
fireEvent.click(results.getByText('Assign to new policy').closest('button')!);
});
expect(jest.mocked(AgentReassignAgentPolicyModal)).toHaveBeenCalledWith(
expect.objectContaining({
agents: '(Base query) AND NOT (fleet-agents.agent.id : ("agentId1" or "agentId2"))',
}),
expect.anything()
);
});
});
});

View file

@ -126,7 +126,7 @@ export const AgentBulkActions: React.FunctionComponent<Props> = ({
// update the query removing the "managed" agents
const selectionQuery = useMemo(() => {
if (managedAgents) {
if (managedAgents.length) {
const excludedKuery = `${AGENTS_PREFIX}.agent.id : (${managedAgents
.map((id) => `"${id}"`)
.join(' or ')})`;