Skip to content

Commit

Permalink
fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
juliaElastic committed Sep 21, 2022
1 parent 278e6df commit 4dfeea5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/fleet/server/services/agents/action.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ export function createClientMock() {
};
});

esClientMock.search.mockResolvedValue({ hits: { hits: [] } } as any);

return {
soClient: soClientMock,
esClient: esClientMock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function getActionStatuses(
size: 0,
aggs: {
ack_counts: {
terms: { field: 'action_id', size: actions.length },
terms: { field: 'action_id', size: actions.length || 10 },
aggs: {
max_timestamp: { max: { field: '@timestamp' } },
},
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/fleet/server/services/agents/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,5 +268,4 @@ export interface ActionsService {
) => Promise<AgentAction>;

getAgentActions: (esClient: ElasticsearchClient, actionId: string) => Promise<any[]>;
getUnenrollAgentActions: (esClient: ElasticsearchClient) => Promise<any[]>;
}
22 changes: 20 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 @@ -165,7 +165,7 @@ describe('unenrollAgents (plural)', () => {

// calls ES update with correct values
const onlyRegular = [agentInRegularDoc._id, agentInRegularDoc2._id];
const calledWith = esClient.bulk.mock.calls[0][0];
const calledWith = esClient.bulk.mock.calls[1][0];
const ids = (calledWith as estypes.BulkRequest)?.body
?.filter((i: any) => i.update !== undefined)
.map((i: any) => i.update._id);
Expand All @@ -176,6 +176,15 @@ describe('unenrollAgents (plural)', () => {
for (const doc of docs!) {
expect(doc).toHaveProperty('unenrolled_at');
}

const actionResults = esClient.bulk.mock.calls[0][0];
const resultIds = (actionResults as estypes.BulkRequest)?.body
?.filter((i: any) => i.agent_id)
.map((i: any) => i.agent_id);
expect(resultIds).toEqual(onlyRegular);

const action = esClient.create.mock.calls[0][0] as any;
expect(action.body.type).toEqual('FORCE_UNENROLL');
});

it('can unenroll from hosted agent policy with force=true', async () => {
Expand Down Expand Up @@ -226,7 +235,7 @@ describe('unenrollAgents (plural)', () => {
]);

// calls ES update with correct values
const calledWith = esClient.bulk.mock.calls[0][0];
const calledWith = esClient.bulk.mock.calls[1][0];
const ids = (calledWith as estypes.BulkRequest)?.body
?.filter((i: any) => i.update !== undefined)
.map((i: any) => i.update._id);
Expand All @@ -237,6 +246,15 @@ describe('unenrollAgents (plural)', () => {
for (const doc of docs!) {
expect(doc).toHaveProperty('unenrolled_at');
}

const actionResults = esClient.bulk.mock.calls[0][0];
const resultIds = (actionResults as estypes.BulkRequest)?.body
?.filter((i: any) => i.agent_id)
.map((i: any) => i.agent_id);
expect(resultIds).toEqual(idsToUnenroll);

const action = esClient.create.mock.calls[0][0] as any;
expect(action.body.type).toEqual('FORCE_UNENROLL');
});
});

Expand Down

0 comments on commit 4dfeea5

Please sign in to comment.