From 2bc441cad7a473d9ca0c0bdfd0a8c2a2f6db2bbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20Fern=C3=A1ndez=20Haro?= Date: Thu, 19 Dec 2024 12:06:00 +0100 Subject: [PATCH] Fix tests --- .../server/services/agents/action.mock.ts | 2 +- .../server/services/agents/actions.test.ts | 12 +++---- .../fleet/server/services/agents/actions.ts | 6 ++-- .../fleet/server/services/agents/crud.test.ts | 6 ++-- .../fleet/server/services/agents/crud.ts | 32 ++++++++----------- .../server/services/agents/reassign.test.ts | 2 +- .../agents/request_diagnostics.test.ts | 6 ++-- .../server/services/agents/unenroll.test.ts | 4 +-- .../services/agents/update_agent_tags.test.ts | 20 ++++++------ 9 files changed, 42 insertions(+), 48 deletions(-) diff --git a/x-pack/plugins/fleet/server/services/agents/action.mock.ts b/x-pack/plugins/fleet/server/services/agents/action.mock.ts index 77dc46b972532..50c2f0d6b0fbf 100644 --- a/x-pack/plugins/fleet/server/services/agents/action.mock.ts +++ b/x-pack/plugins/fleet/server/services/agents/action.mock.ts @@ -152,7 +152,7 @@ export function createClientMock() { esClientMock.mget.mockResponseImplementation((params) => { // @ts-expect-error - const docs = params?.body.docs.map(({ _id }) => { + const docs = params?.docs.map(({ _id }) => { let result; switch (_id) { case agentInHostedDoc._id: diff --git a/x-pack/plugins/fleet/server/services/agents/actions.test.ts b/x-pack/plugins/fleet/server/services/agents/actions.test.ts index b8cb2ce8c8d6a..594d553bff941 100644 --- a/x-pack/plugins/fleet/server/services/agents/actions.test.ts +++ b/x-pack/plugins/fleet/server/services/agents/actions.test.ts @@ -135,7 +135,7 @@ describe('Agent actions', () => { expect(esClient.create).toBeCalledWith( expect.objectContaining({ - body: expect.objectContaining({ + document: expect.objectContaining({ signed: { data: expect.any(String), signature: expect.any(String), @@ -180,7 +180,7 @@ describe('Agent actions', () => { expect(esClient.create).toBeCalledWith( expect.objectContaining({ - body: expect.not.objectContaining({ + document: expect.not.objectContaining({ signed: expect.any(Object), }), }) @@ -235,7 +235,7 @@ describe('Agent actions', () => { await bulkCreateAgentActions(esClient, newActions); expect(esClient.bulk).toHaveBeenCalledWith( expect.objectContaining({ - body: expect.arrayContaining([ + operations: expect.arrayContaining([ expect.arrayContaining([ expect.objectContaining({ signed: { @@ -274,7 +274,7 @@ describe('Agent actions', () => { await bulkCreateAgentActions(esClient, newActions); expect(esClient.bulk).toHaveBeenCalledWith( expect.objectContaining({ - body: expect.arrayContaining([ + operations: expect.arrayContaining([ expect.arrayContaining([ expect.not.objectContaining({ signed: { @@ -350,7 +350,7 @@ describe('Agent actions', () => { expect(esClient.create).toBeCalledTimes(2); expect(esClient.create).toBeCalledWith( expect.objectContaining({ - body: expect.objectContaining({ + document: expect.objectContaining({ type: 'CANCEL', data: { target_id: 'action1' }, agents: ['agent1', 'agent2'], @@ -359,7 +359,7 @@ describe('Agent actions', () => { ); expect(esClient.create).toBeCalledWith( expect.objectContaining({ - body: expect.objectContaining({ + document: expect.objectContaining({ type: 'CANCEL', data: { target_id: 'action1' }, agents: ['agent3', 'agent4'], diff --git a/x-pack/plugins/fleet/server/services/agents/actions.ts b/x-pack/plugins/fleet/server/services/agents/actions.ts index f2faf26fd96af..e3e291eb1ae18 100644 --- a/x-pack/plugins/fleet/server/services/agents/actions.ts +++ b/x-pack/plugins/fleet/server/services/agents/actions.ts @@ -81,7 +81,7 @@ export async function createAgentAction( await esClient.create({ index: AGENT_ACTIONS_INDEX, id: uuidv4(), - body, + document: body, refresh: 'wait_for', }); @@ -153,7 +153,7 @@ export async function bulkCreateAgentActions( await esClient.bulk({ index: AGENT_ACTIONS_INDEX, - body: fleetServerAgentActions, + operations: fleetServerAgentActions, }); for (const action of actions) { @@ -231,7 +231,7 @@ export async function bulkCreateAgentActionResults( await esClient.bulk({ index: AGENT_ACTIONS_RESULTS_INDEX, - body: bulkBody, + operations: bulkBody, refresh: 'wait_for', }); } diff --git a/x-pack/plugins/fleet/server/services/agents/crud.test.ts b/x-pack/plugins/fleet/server/services/agents/crud.test.ts index 00119e5bc44fb..6ccf5653bfc8f 100644 --- a/x-pack/plugins/fleet/server/services/agents/crud.test.ts +++ b/x-pack/plugins/fleet/server/services/agents/crud.test.ts @@ -107,9 +107,7 @@ describe('Agents CRUD test', () => { expect(searchMock).toHaveBeenCalledWith( expect.objectContaining({ aggs: { tags: { terms: { field: 'tags', size: 10000 } } }, - body: { - query: expect.any(Object), - }, + query: expect.any(Object), index: '.fleet-agents', size: 0, fields: ['status'], @@ -164,7 +162,7 @@ describe('Agents CRUD test', () => { }) ); - expect(searchMock.mock.calls.at(-1)[0].body.query).toEqual( + expect(searchMock.mock.calls.at(-1)[0].query).toEqual( toElasticsearchQuery( _joinFilters(['fleet-agents.policy_id: 123', 'NOT status:unenrolled'])! ) diff --git a/x-pack/plugins/fleet/server/services/agents/crud.ts b/x-pack/plugins/fleet/server/services/agents/crud.ts index 879280e3e35ba..bc3a9cb6028ff 100644 --- a/x-pack/plugins/fleet/server/services/agents/crud.ts +++ b/x-pack/plugins/fleet/server/services/agents/crud.ts @@ -169,13 +169,13 @@ export async function getAgentTags( } const kueryNode = _joinFilters(filters); - const body = kueryNode ? { query: toElasticsearchQuery(kueryNode) } : {}; + const query = kueryNode ? { query: toElasticsearchQuery(kueryNode) } : {}; const runtimeFields = await buildAgentStatusRuntimeField(soClient); try { const result = await esClient.search<{}, { tags: { buckets: Array<{ key: string }> } }>({ index: AGENTS_INDEX, size: 0, - body, + ...query, fields: Object.keys(runtimeFields), runtime_mappings: runtimeFields, aggs: { @@ -546,17 +546,15 @@ export async function getAgentVersionsForAgentPolicyIds( FleetServerAgent, Record<'agent_versions', { buckets: Array<{ key: string; doc_count: number }> }> >({ - body: { - query: { - bool: { - filter: [ - { - terms: { - policy_id: agentPolicyIds, - }, + query: { + bool: { + filter: [ + { + terms: { + policy_id: agentPolicyIds, }, - ], - }, + }, + ], }, }, index: AGENTS_INDEX, @@ -628,7 +626,7 @@ export async function updateAgent( await esClient.update({ id: agentId, index: AGENTS_INDEX, - body: { doc: agentSOAttributesToFleetServerAgentDoc(data) }, + doc: agentSOAttributesToFleetServerAgentDoc(data), refresh: 'wait_for', }); } @@ -645,7 +643,7 @@ export async function bulkUpdateAgents( return; } - const body = updateData.flatMap(({ agentId, data }) => [ + const operations = updateData.flatMap(({ agentId, data }) => [ { update: { _id: agentId, @@ -658,7 +656,7 @@ export async function bulkUpdateAgents( ]); const res = await esClient.bulk({ - body, + operations, index: AGENTS_INDEX, refresh: 'wait_for', }); @@ -676,9 +674,7 @@ export async function deleteAgent(esClient: ElasticsearchClient, agentId: string await esClient.update({ id: agentId, index: AGENTS_INDEX, - body: { - doc: { active: false }, - }, + doc: { active: false }, }); } catch (err) { if (isESClientError(err) && err.meta.statusCode === 404) { diff --git a/x-pack/plugins/fleet/server/services/agents/reassign.test.ts b/x-pack/plugins/fleet/server/services/agents/reassign.test.ts index 4699368c72416..cf48ee20159f7 100644 --- a/x-pack/plugins/fleet/server/services/agents/reassign.test.ts +++ b/x-pack/plugins/fleet/server/services/agents/reassign.test.ts @@ -103,7 +103,7 @@ describe('reassignAgent', () => { // only 1 are regular and bulk write two line per update expect((calledWith as estypes.BulkRequest).operations?.length).toBe(2); // @ts-expect-error - expect(calledWith.body[0].update._id).toEqual(agentInRegularDoc._id); + expect(calledWith.operations[0].update._id).toEqual(agentInRegularDoc._id); // hosted policy is updated in action results with error const calledWithActionResults = esClient.bulk.mock.calls[1][0] as estypes.BulkRequest; diff --git a/x-pack/plugins/fleet/server/services/agents/request_diagnostics.test.ts b/x-pack/plugins/fleet/server/services/agents/request_diagnostics.test.ts index c99cd17ecfc01..c6c3fc1622f20 100644 --- a/x-pack/plugins/fleet/server/services/agents/request_diagnostics.test.ts +++ b/x-pack/plugins/fleet/server/services/agents/request_diagnostics.test.ts @@ -33,7 +33,7 @@ describe('requestDiagnostics', () => { expect(esClient.create).toHaveBeenCalledWith( expect.objectContaining({ - body: expect.objectContaining({ + document: expect.objectContaining({ agents: ['agent-in-regular-policy'], type: 'REQUEST_DIAGNOSTICS', expiration: expect.anything(), @@ -53,7 +53,7 @@ describe('requestDiagnostics', () => { expect(esClient.create).toHaveBeenCalledWith( expect.objectContaining({ - body: expect.objectContaining({ + document: expect.objectContaining({ agents: ['agent-in-regular-policy-newer', 'agent-in-regular-policy-newer2'], type: 'REQUEST_DIAGNOSTICS', expiration: expect.anything(), @@ -70,7 +70,7 @@ describe('requestDiagnostics', () => { expect(esClient.create).toHaveBeenCalledWith( expect.objectContaining({ - body: expect.objectContaining({ + document: expect.objectContaining({ agents: ['agent-in-regular-policy-newer', 'agent-in-regular-policy'], type: 'REQUEST_DIAGNOSTICS', expiration: expect.anything(), diff --git a/x-pack/plugins/fleet/server/services/agents/unenroll.test.ts b/x-pack/plugins/fleet/server/services/agents/unenroll.test.ts index edd4adab038d9..222d49ae44eed 100644 --- a/x-pack/plugins/fleet/server/services/agents/unenroll.test.ts +++ b/x-pack/plugins/fleet/server/services/agents/unenroll.test.ts @@ -269,7 +269,7 @@ describe('unenroll', () => { expect(resultIds).toEqual(onlyRegular); const action = esClient.create.mock.calls[0][0] as any; - expect(action.body.type).toEqual('FORCE_UNENROLL'); + expect(action.document.type).toEqual('FORCE_UNENROLL'); }); it('can unenroll from hosted agent policy with force=true', async () => { @@ -326,7 +326,7 @@ describe('unenroll', () => { expect(resultIds).toEqual(idsToUnenroll); const action = esClient.create.mock.calls[0][0] as any; - expect(action.body.type).toEqual('FORCE_UNENROLL'); + expect(action.document.type).toEqual('FORCE_UNENROLL'); }); }); diff --git a/x-pack/plugins/fleet/server/services/agents/update_agent_tags.test.ts b/x-pack/plugins/fleet/server/services/agents/update_agent_tags.test.ts index cd408d953e31a..6a2410dfa3c16 100644 --- a/x-pack/plugins/fleet/server/services/agents/update_agent_tags.test.ts +++ b/x-pack/plugins/fleet/server/services/agents/update_agent_tags.test.ts @@ -112,7 +112,7 @@ describe('update_agent_tags', () => { await updateAgentTags(soClient, esClient, { agentIds: ['agent1'] }, ['one'], []); const agentAction = esClient.create.mock.calls[0][0] as any; - expect(agentAction?.body).toEqual( + expect(agentAction?.document).toEqual( expect.objectContaining({ action_id: expect.anything(), agents: [expect.any(String)], @@ -122,11 +122,11 @@ describe('update_agent_tags', () => { ); const actionResults = esClient.bulk.mock.calls[0][0] as any; - const agentIds = actionResults?.body + const agentIds = actionResults?.operations ?.filter((i: any) => i.agent_id) .map((i: any) => i.agent_id); expect(agentIds.length).toEqual(1); - expect(actionResults.body[1].error).not.toBeDefined(); + expect(actionResults.operations[1].error).not.toBeDefined(); }); it('should skip hosted agent from total when agentIds are passed', async () => { @@ -144,7 +144,7 @@ describe('update_agent_tags', () => { ); const agentAction = esClientMock.create.mock.calls[0][0] as any; - expect(agentAction?.body).toEqual( + expect(agentAction?.document).toEqual( expect.objectContaining({ action_id: expect.anything(), agents: [expect.any(String)], @@ -165,7 +165,7 @@ describe('update_agent_tags', () => { await updateAgentTags(soClient, esClient, { agentIds: ['agent1'] }, ['one'], []); const agentAction = esClient.create.mock.calls[0][0] as any; - expect(agentAction?.body).toEqual( + expect(agentAction?.document).toEqual( expect.objectContaining({ action_id: expect.anything(), agents: ['failure1'], @@ -175,7 +175,7 @@ describe('update_agent_tags', () => { ); const errorResults = esClient.bulk.mock.calls[0][0] as any; - expect(errorResults.body[1].error).toEqual('error reason'); + expect(errorResults.operations[1].error).toEqual('error reason'); }); it('should throw error on version conflicts', async () => { @@ -217,10 +217,10 @@ describe('update_agent_tags', () => { ).rejects.toThrowError('Version conflict of 100 agents'); const agentAction = esClient.create.mock.calls[0][0] as any; - expect(agentAction?.body.agents.length).toEqual(100); + expect(agentAction?.document.agents.length).toEqual(100); const errorResults = esClient.bulk.mock.calls[0][0] as any; - expect(errorResults.body[1].error).toEqual('version conflict on last retry'); + expect(errorResults.operations[1].error).toEqual('version conflict on last retry'); }); it('should combine action agents from updated, failures and version conflicts on last retry', async () => { @@ -249,7 +249,7 @@ describe('update_agent_tags', () => { ).rejects.toThrowError('Version conflict of 1 agents'); const agentAction = esClient.create.mock.calls[0][0] as any; - expect(agentAction?.body.agents.length).toEqual(3); + expect(agentAction?.document.agents.length).toEqual(3); }); it('should run add tags async when actioning more agents than batch size', async () => { @@ -367,7 +367,7 @@ describe('update_agent_tags', () => { ); const agentAction = esClient.create.mock.calls[0][0] as any; - expect(agentAction?.body).toEqual( + expect(agentAction?.document).toEqual( expect.objectContaining({ action_id: expect.anything(), agents: [expect.any(String)],