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] refactored bulk update tags retry #147594

Merged
merged 13 commits into from
Dec 20, 2022
Prev Previous commit
Next Next commit
fixed test
  • Loading branch information
juliaElastic committed Dec 15, 2022
commit c098438016806b3be712d1ac50ec74eb354d2b10
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ export async function updateTagsBatch(
const extraFilters = [];
if (options.tagsToAdd.length === 1 && options.tagsToRemove.length === 0) {
hop-dev marked this conversation as resolved.
Show resolved Hide resolved
extraFilters.push(`NOT (tags:${options.tagsToAdd[0]})`);
} else if (options.tagsToRemove.length === 1) {
} else if (options.tagsToRemove.length === 1 && options.tagsToAdd.length === 0) {
extraFilters.push(`tags:${options.tagsToRemove[0]}`);
}
query = getElasticsearchQuery(options.kuery, false, false, hostedIds, extraFilters);
Original file line number Diff line number Diff line change
@@ -87,29 +87,11 @@ export default function (providerContext: FtrProviderContext) {
});
});

it('should bulk update tags of multiple agents by kuery in batches', async () => {
const { body: actionBody } = await supertest
.post(`/api/fleet/agents/bulk_update_agent_tags`)
.set('kbn-xsrf', 'xxx')
.send({
agents: 'active: true',
tagsToAdd: ['newTag'],
tagsToRemove: ['existingTag'],
batchSize: 3,
})
.expect(200);

const actionId = actionBody.actionId;

const verifyActionResult = async () => {
const { body } = await supertest.get(`/api/fleet/agents`).set('kbn-xsrf', 'xxx');
expect(body.total).to.eql(4);
body.items.forEach((agent: any) => {
expect(agent.tags.includes('newTag')).to.be(true);
expect(agent.tags.includes('existingTag')).to.be(false);
});
};

async function pollResult(
actionId: string,
nbAgentsAck: number,
verifyActionResult: Function
) {
await new Promise((resolve, reject) => {
let attempts = 0;
const intervalId = setInterval(async () => {
@@ -122,7 +104,7 @@ export default function (providerContext: FtrProviderContext) {
body: { items: actionStatuses },
} = await supertest.get(`/api/fleet/agents/action_status`).set('kbn-xsrf', 'xxx');
const action = actionStatuses.find((a: any) => a.actionId === actionId);
if (action && action.nbAgentsAck === 4) {
if (action && action.nbAgentsAck === nbAgentsAck) {
clearInterval(intervalId);
await verifyActionResult();
resolve({});
@@ -131,6 +113,54 @@ export default function (providerContext: FtrProviderContext) {
}).catch((e) => {
throw e;
});
}

it('should bulk update tags of multiple agents by kuery in batches - add', async () => {
const { body: actionBody } = await supertest
.post(`/api/fleet/agents/bulk_update_agent_tags`)
.set('kbn-xsrf', 'xxx')
.send({
agents: 'active: true',
tagsToAdd: ['newTag'],
tagsToRemove: [],
batchSize: 3,
})
.expect(200);

const actionId = actionBody.actionId;

const verifyActionResult = async () => {
const { body } = await supertest
.get(`/api/fleet/agents?kuery=tags:newTag`)
.set('kbn-xsrf', 'xxx');
expect(body.total).to.eql(4);
};

await pollResult(actionId, 4, verifyActionResult);
});

it('should bulk update tags of multiple agents by kuery in batches - remove', async () => {
const { body: actionBody } = await supertest
.post(`/api/fleet/agents/bulk_update_agent_tags`)
.set('kbn-xsrf', 'xxx')
.send({
agents: 'active: true',
tagsToAdd: [],
tagsToRemove: ['existingTag'],
batchSize: 3,
})
.expect(200);

const actionId = actionBody.actionId;

const verifyActionResult = async () => {
const { body } = await supertest
.get(`/api/fleet/agents?kuery=tags:existingTag`)
.set('kbn-xsrf', 'xxx');
expect(body.total).to.eql(0);
};

await pollResult(actionId, 2, verifyActionResult);
});

it('should return a 403 if user lacks fleet all permissions', async () => {