Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
afharo committed Dec 19, 2024
1 parent c810264 commit 2bc441c
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 48 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/fleet/server/services/agents/action.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
12 changes: 6 additions & 6 deletions x-pack/plugins/fleet/server/services/agents/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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),
}),
})
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down Expand Up @@ -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'],
Expand All @@ -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'],
Expand Down
6 changes: 3 additions & 3 deletions x-pack/plugins/fleet/server/services/agents/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export async function createAgentAction(
await esClient.create({
index: AGENT_ACTIONS_INDEX,
id: uuidv4(),
body,
document: body,
refresh: 'wait_for',
});

Expand Down Expand Up @@ -153,7 +153,7 @@ export async function bulkCreateAgentActions(

await esClient.bulk({
index: AGENT_ACTIONS_INDEX,
body: fleetServerAgentActions,
operations: fleetServerAgentActions,
});

for (const action of actions) {
Expand Down Expand Up @@ -231,7 +231,7 @@ export async function bulkCreateAgentActionResults(

await esClient.bulk({
index: AGENT_ACTIONS_RESULTS_INDEX,
body: bulkBody,
operations: bulkBody,
refresh: 'wait_for',
});
}
Expand Down
6 changes: 2 additions & 4 deletions x-pack/plugins/fleet/server/services/agents/crud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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'])!
)
Expand Down
32 changes: 14 additions & 18 deletions x-pack/plugins/fleet/server/services/agents/crud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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',
});
}
Expand All @@ -645,7 +643,7 @@ export async function bulkUpdateAgents(
return;
}

const body = updateData.flatMap(({ agentId, data }) => [
const operations = updateData.flatMap(({ agentId, data }) => [
{
update: {
_id: agentId,
Expand All @@ -658,7 +656,7 @@ export async function bulkUpdateAgents(
]);

const res = await esClient.bulk({
body,
operations,
index: AGENTS_INDEX,
refresh: 'wait_for',
});
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/fleet/server/services/agents/unenroll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
Expand All @@ -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 () => {
Expand All @@ -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)],
Expand All @@ -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'],
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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)],
Expand Down

0 comments on commit 2bc441c

Please sign in to comment.