Skip to content

Commit

Permalink
[Fleet] refactored input package tests (#184410)
Browse files Browse the repository at this point in the history
## Summary

Closes #175563

Refactored input package tests not to depend on each other.
  • Loading branch information
juliaElastic committed May 30, 2024
1 parent 0a8b651 commit 7fef12b
Showing 1 changed file with 33 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,6 @@ export default function (providerContext: FtrProviderContext) {
return res.body.item;
};

const deletePackagePolicy = (id: string) => {
return supertest
.delete(`/api/fleet/package_policies/${id}`)
.set('kbn-xsrf', 'xxxx')
.expect(200);
};

const createAgentPolicy = async (name = 'Input Package Test 3') => {
const res = await supertest
.post(`/api/fleet/agent_policies`)
Expand Down Expand Up @@ -180,20 +173,20 @@ export default function (providerContext: FtrProviderContext) {
await es.indices.deleteIndexTemplate({ name: templateName });
};

// Tests are order-dependent and share state so can fail.
describe.skip('Package Policy - input package behavior', async function () {
describe('Package Policy - input package behavior', async function () {
skipIfNoDockerRegistry(providerContext);

let agentPolicyId: string;
const packagePolicyIds: string[] = [];
before(async () => {
installPackage(PACKAGE_NAME, START_VERSION);
beforeEach(async () => {
await installPackage(PACKAGE_NAME, START_VERSION);
const agentPolicy = await createAgentPolicy();
agentPolicyId = agentPolicy.id;
});

after(async () => {
afterEach(async () => {
await deleteAgentPolicy(agentPolicyId);

await uninstallPackage(PACKAGE_NAME, START_VERSION);
});
setupFleetAndAgents(providerContext);

Expand All @@ -203,8 +196,7 @@ export default function (providerContext: FtrProviderContext) {
});

it('should create index templates and update installed_es on package policy creation', async () => {
const packagePolicy = await createPackagePolicyWithDataset(agentPolicyId, 'dataset1');
packagePolicyIds.push(packagePolicy.id);
await createPackagePolicyWithDataset(agentPolicyId, 'dataset1');
const installation = await getInstallationSavedObject(PACKAGE_NAME, START_VERSION);
expectIdArraysEqual(installation.installed_es, [
{ id: 'logs-dataset1-1.0.0', type: 'ingest_pipeline' },
Expand All @@ -224,7 +216,7 @@ export default function (providerContext: FtrProviderContext) {
lifecycle: { name: 'logs' },
default_pipeline: 'logs-dataset1-1.0.0',
mapping: {
total_fields: { limit: '10000' },
total_fields: { limit: '1000' },
},
},
},
Expand All @@ -251,37 +243,39 @@ export default function (providerContext: FtrProviderContext) {
});

it('should create index templates and update installed_es on second package policy creation', async () => {
const packagePolicy = await createPackagePolicyWithDataset(agentPolicyId, 'dataset2');
packagePolicyIds.push(packagePolicy.id);
await createPackagePolicyWithDataset(agentPolicyId, 'dataset2');
const installation = await getInstallationSavedObject(PACKAGE_NAME, START_VERSION);
expectIdArraysEqual(installation.installed_es, [
{ id: 'logs-dataset1-1.0.0', type: 'ingest_pipeline' },
{ id: 'logs-dataset1', type: 'index_template' },
{ id: 'logs-dataset1@package', type: 'component_template' },
{ id: 'logs-dataset1@custom', type: 'component_template' },
let found = 0;
[
{ id: 'logs-dataset2-1.0.0', type: 'ingest_pipeline' },
{ id: 'logs-dataset2', type: 'index_template' },
{ id: 'logs-dataset2@package', type: 'component_template' },
{ id: 'logs-dataset2@custom', type: 'component_template' },
]);
].forEach((obj) => {
if (installation.installed_es.find((installed: any) => installed.id === obj.id)) {
found++;
}
});
expect(found).to.eql(4);
});

it('should allow data to be sent to existing stream if owned by package and should not create templates', async () => {
await createFakeFleetDataStream('dataset3');

const packagePolicy = await createPackagePolicyWithDataset(agentPolicyId, 'dataset3');
packagePolicyIds.push(packagePolicy.id);
await createPackagePolicyWithDataset(agentPolicyId, 'dataset3');
const installation = await getInstallationSavedObject(PACKAGE_NAME, START_VERSION);
expectIdArraysEqual(installation.installed_es, [
{ id: 'logs-dataset1-1.0.0', type: 'ingest_pipeline' },
{ id: 'logs-dataset1', type: 'index_template' },
{ id: 'logs-dataset1@package', type: 'component_template' },
{ id: 'logs-dataset1@custom', type: 'component_template' },
{ id: 'logs-dataset2-1.0.0', type: 'ingest_pipeline' },
{ id: 'logs-dataset2', type: 'index_template' },
{ id: 'logs-dataset2@package', type: 'component_template' },
{ id: 'logs-dataset2@custom', type: 'component_template' },
]);
let found = 0;
[
{ id: 'logs-dataset3-1.0.0', type: 'ingest_pipeline' },
{ id: 'logs-dataset3', type: 'index_template' },
{ id: 'logs-dataset3@package', type: 'component_template' },
{ id: 'logs-dataset3@custom', type: 'component_template' },
].forEach((obj) => {
if (installation.installed_es.find((installed: any) => installed.id === obj.id)) {
found++;
}
});
expect(found).to.eql(0);

const dataset3PkgComponentTemplate = await getComponentTemplate('logs-dataset3@package');
expect(dataset3PkgComponentTemplate).eql(null);
Expand Down Expand Up @@ -346,6 +340,7 @@ export default function (providerContext: FtrProviderContext) {
});

it('should update all index templates created by package policies when the package is upgraded', async () => {
await createPackagePolicyWithDataset(agentPolicyId, 'dataset1');
// version 1.1.0 of the test package introduces elasticsearch mappings to the index
// templates, upgrading the package should add this field to both package component templates
await installPackage(PACKAGE_NAME, UPGRADE_VERSION);
Expand All @@ -367,24 +362,15 @@ export default function (providerContext: FtrProviderContext) {
mappingsWithTimestamp
);

const dataset2PkgComponentTemplate = await getComponentTemplate('logs-dataset2@package');
expect(dataset2PkgComponentTemplate).not.eql(null);
expect(dataset2PkgComponentTemplate!.component_template.template?.mappings?.properties).eql(
mappingsWithTimestamp
);
await uninstallPackage(PACKAGE_NAME, UPGRADE_VERSION);
});
it('should delete all index templates created by package policies when the package is uninstalled', async () => {
for (const packagePolicyId of packagePolicyIds) {
await deletePackagePolicy(packagePolicyId);
}
await createPackagePolicyWithDataset(agentPolicyId, 'dataset1');
await deleteAgentPolicy(agentPolicyId);
await uninstallPackage(PACKAGE_NAME, UPGRADE_VERSION);

const dataset1PkgComponentTemplate = await getComponentTemplate('logs-dataset1@package');
expect(dataset1PkgComponentTemplate).eql(null);

const dataset2PkgComponentTemplate = await getComponentTemplate('logs-dataset2@package');
expect(dataset2PkgComponentTemplate).eql(null);
});
});
}

0 comments on commit 7fef12b

Please sign in to comment.