[Fleet] Sort correctly name before incrementing package policy name (#121965) (#121971)

Co-authored-by: Nicolas Chaulet <nicolas.chaulet@elastic.co>
This commit is contained in:
Kibana Machine 2021-12-23 15:30:48 -05:00 committed by GitHub
parent 7e8eed447a
commit 0846cde660
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 1 deletions

View file

@ -1386,7 +1386,7 @@ export async function incrementPackageName(
? packagePolicyData.items
.filter((ds) => Boolean(ds.name.match(pkgPoliciesNamePattern)))
.map((ds) => parseInt(ds.name.match(pkgPoliciesNamePattern)![1], 10))
.sort()
.sort((a, b) => a - b)
: [];
return `${packageName}-${

View file

@ -133,6 +133,45 @@ export default function (providerContext: FtrProviderContext) {
.expect(409);
});
it('should allow to create policy with the system integration policy and increment correctly the name if there is more than 10 package policy', async () => {
// load a bunch of fake system integration policy
for (let i = 0; i < 10; i++) {
await kibanaServer.savedObjects.create({
id: `package-policy-test-${i}`,
type: PACKAGE_POLICY_SAVED_OBJECT_TYPE,
overwrite: true,
attributes: {
name: `system-${i + 1}`,
package: {
name: 'system',
},
},
});
packagePoliciesToDeleteIds.push(`package-policy-test-${i}`);
}
// first one succeeds
const res = await supertest
.post(`/api/fleet/agent_policies`)
.query({
sys_monitoring: true,
})
.set('kbn-xsrf', 'xxxx')
.send({
name: `Policy with system monitoring ${Date.now()}`,
namespace: 'default',
})
.expect(200);
const {
body: { items: policies },
} = await supertest.get(`/api/fleet/agent_policies?full=true`).expect(200);
const policy = policies.find((p: any) => (p.id = res.body.item.id));
expect(policy.package_policies[0].name).be('system-11');
});
it('should allow to create policy with the system integration policy and increment correctly the name', async () => {
// load a bunch of fake system integration policy
await kibanaServer.savedObjects.create({