Skip to content

Commit

Permalink
adapt tests to poll status API
Browse files Browse the repository at this point in the history
  • Loading branch information
hop-dev committed Oct 16, 2024
1 parent 8d77cd4 commit 1148af8
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export default ({ getService }: FtrProviderContext) => {
const supertest = getService('supertest');

const utils = EntityStoreUtils(getService);
// Failing: See https://github.com/elastic/kibana/issues/196526
describe.skip('@ess @skipInServerlessMKI Entity Store Engine APIs', () => {
describe('@ess @skipInServerlessMKI Entity Store Engine APIs', () => {
const dataView = dataViewRouteHelpersFactory(supertest);

before(async () => {
Expand All @@ -33,21 +32,21 @@ export default ({ getService }: FtrProviderContext) => {
});

it('should have installed the expected user resources', async () => {
await utils.initEntityEngineForEntityType('user');
await utils.initEntityEngineForEntityTypeAndWait('user');
await utils.expectEngineAssetsExist('user');
});

it('should have installed the expected host resources', async () => {
await utils.initEntityEngineForEntityType('host');
await utils.initEntityEngineForEntityTypeAndWait('host');
await utils.expectEngineAssetsExist('host');
});
});

describe('get and list', () => {
before(async () => {
await Promise.all([
utils.initEntityEngineForEntityType('host'),
utils.initEntityEngineForEntityType('user'),
utils.initEntityEngineForEntityTypeAndWait('host'),
utils.initEntityEngineForEntityTypeAndWait('user'),
]);
});

Expand Down Expand Up @@ -118,7 +117,7 @@ export default ({ getService }: FtrProviderContext) => {

describe('start and stop', () => {
before(async () => {
await utils.initEntityEngineForEntityType('host');
await utils.initEntityEngineForEntityTypeAndWait('host');
});

after(async () => {
Expand Down Expand Up @@ -160,7 +159,7 @@ export default ({ getService }: FtrProviderContext) => {

describe('delete', () => {
it('should delete the host entity engine', async () => {
await utils.initEntityEngineForEntityType('host');
await utils.initEntityEngineForEntityTypeAndWait('host');

await api
.deleteEntityEngine({
Expand All @@ -173,7 +172,7 @@ export default ({ getService }: FtrProviderContext) => {
});

it('should delete the user entity engine', async () => {
await utils.initEntityEngineForEntityType('user');
await utils.initEntityEngineForEntityTypeAndWait('user');

await api
.deleteEntityEngine({
Expand All @@ -188,7 +187,7 @@ export default ({ getService }: FtrProviderContext) => {

describe('apply_dataview_indices', () => {
before(async () => {
await utils.initEntityEngineForEntityType('host');
await utils.initEntityEngineForEntityTypeAndWait('host');
});

after(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ export default ({ getService }: FtrProviderContextWithSpaces) => {
});

it('should have installed the expected user resources', async () => {
await utils.initEntityEngineForEntityType('user');
await utils.initEntityEngineForEntityTypeAndWait('user');
await utils.expectEngineAssetsExist('user');
});

it('should have installed the expected host resources', async () => {
await utils.initEntityEngineForEntityType('host');
await utils.initEntityEngineForEntityTypeAndWait('host');
await utils.expectEngineAssetsExist('host');
});
});

describe('get and list', () => {
before(async () => {
await Promise.all([
utils.initEntityEngineForEntityType('host'),
utils.initEntityEngineForEntityType('user'),
utils.initEntityEngineForEntityTypeAndWait('host'),
utils.initEntityEngineForEntityTypeAndWait('user'),
]);
});

Expand Down Expand Up @@ -133,7 +133,7 @@ export default ({ getService }: FtrProviderContextWithSpaces) => {

describe('start and stop', () => {
before(async () => {
await utils.initEntityEngineForEntityType('host');
await utils.initEntityEngineForEntityTypeAndWait('host');
});

after(async () => {
Expand Down Expand Up @@ -187,7 +187,7 @@ export default ({ getService }: FtrProviderContextWithSpaces) => {

describe('delete', () => {
it('should delete the host entity engine', async () => {
await utils.initEntityEngineForEntityType('host');
await utils.initEntityEngineForEntityTypeAndWait('host');

await api
.deleteEntityEngine(
Expand All @@ -203,7 +203,7 @@ export default ({ getService }: FtrProviderContextWithSpaces) => {
});

it('should delete the user entity engine', async () => {
await utils.initEntityEngineForEntityType('user');
await utils.initEntityEngineForEntityTypeAndWait('user');

await api
.deleteEntityEngine(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,30 @@ export const elasticAssetCheckerFactory = (getService: FtrProviderContext['getSe
}
};

const expectEnrichPolicyStatus = async (policyId: string, exists: boolean) => {
try {
await es.enrich.getPolicy({ name: policyId });
if (!exists) {
throw new Error(`Expected enrich policy ${policyId} to not exist, but it does`);
}
} catch (e) {
if (exists) {
throw new Error(`Expected enrich policy ${policyId} to exist, but it does not: ${e}`);
const expectEnrichPolicyStatus = async (
policyId: string,
exists: boolean,
attempts: number = 5,
delayMs: number = 2000
) => {
let currentAttempt = 1;
while (currentAttempt <= attempts) {
try {
await es.enrich.getPolicy({ name: policyId });
if (!exists) {
throw new Error(`Expected enrich policy ${policyId} to not exist, but it does`);
}
return; // Enrich policy exists, exit the loop
} catch (e) {
if (currentAttempt === attempts) {
if (exists) {
throw new Error(`Expected enrich policy ${policyId} to exist, but it does not: ${e}`);
} else {
return; // Enrich policy does not exist, exit the loop
}
}
await new Promise((resolve) => setTimeout(resolve, delayMs));
currentAttempt++;
}
}
};
Expand All @@ -65,17 +80,32 @@ export const elasticAssetCheckerFactory = (getService: FtrProviderContext['getSe
const expectEnrichPolicyNotFound = async (policyId: string, attempts: number = 5) =>
expectEnrichPolicyStatus(policyId, false);

const expectComponentTemplatStatus = async (templateName: string, exists: boolean) => {
try {
await es.cluster.getComponentTemplate({ name: templateName });
if (!exists) {
throw new Error(`Expected component template ${templateName} to not exist, but it does`);
}
} catch (e) {
if (exists) {
throw new Error(
`Expected component template ${templateName} to exist, but it does not: ${e}`
);
const expectComponentTemplatStatus = async (
templateName: string,
exists: boolean,
attempts: number = 5,
delayMs: number = 2000
) => {
let currentAttempt = 1;
while (currentAttempt <= attempts) {
try {
await es.cluster.getComponentTemplate({ name: templateName });
if (!exists) {
throw new Error(`Expected component template ${templateName} to not exist, but it does`);
}
return; // Component template exists, exit the loop
} catch (e) {
if (currentAttempt === attempts) {
if (exists) {
throw new Error(
`Expected component template ${templateName} to exist, but it does not: ${e}`
);
} else {
return; // Component template does not exist, exit the loop
}
}
await new Promise((resolve) => setTimeout(resolve, delayMs));
currentAttempt++;
}
}
};
Expand All @@ -86,24 +116,60 @@ export const elasticAssetCheckerFactory = (getService: FtrProviderContext['getSe
const expectComponentTemplateNotFound = async (templateName: string) =>
expectComponentTemplatStatus(templateName, false);

const expectIngestPipelineStatus = async (pipelineId: string, exists: boolean) => {
const expectIngestPipelineStatus = async (
pipelineId: string,
exists: boolean,
attempts: number = 5,
delayMs: number = 2000
) => {
let currentAttempt = 1;
while (currentAttempt <= attempts) {
try {
await es.ingest.getPipeline({ id: pipelineId });
if (!exists) {
throw new Error(`Expected ingest pipeline ${pipelineId} to not exist, but it does`);
}
return; // Ingest pipeline exists, exit the loop
} catch (e) {
if (currentAttempt === attempts) {
if (exists) {
throw new Error(
`Expected ingest pipeline ${pipelineId} to exist, but it does not: ${e}`
);
} else {
return; // Ingest pipeline does not exist, exit the loop
}
}
await new Promise((resolve) => setTimeout(resolve, delayMs));
currentAttempt++;
}
}
};

const expectIngestPipelineExists = async (pipelineId: string) =>
expectIngestPipelineStatus(pipelineId, true);

const expectIngestPipelineNotFound = async (pipelineId: string) =>
expectIngestPipelineStatus(pipelineId, false);

const expectIndexStatus = async (indexName: string, exists: boolean) => {
try {
await es.ingest.getPipeline({ id: pipelineId });
await es.indices.get({ index: indexName });
if (!exists) {
throw new Error(`Expected ingest pipeline ${pipelineId} to not exist, but it does`);
throw new Error(`Expected index ${indexName} to not exist, but it does`);
}
} catch (e) {
if (exists) {
throw new Error(`Expected ingest pipeline ${pipelineId} to exist, but it does not: ${e}`);
throw new Error(`Expected index ${indexName} to exist, but it does not: ${e}`);
}
}
};

const expectIngestPipelineExists = async (pipelineId: string) =>
expectIngestPipelineStatus(pipelineId, true);
const expectEntitiesIndexExists = async (entityType: string, namespace: string) =>
expectIndexStatus(`.entities.v1.latest.security_${entityType}_${namespace}`, true);

const expectIngestPipelineNotFound = async (pipelineId: string) =>
expectIngestPipelineStatus(pipelineId, false);
const expectEntitiesIndexNotFound = async (entityType: string, namespace: string) =>
expectIndexStatus(`.entities.v1.latest.security_${entityType}_${namespace}`, false);

return {
expectComponentTemplateExists,
Expand All @@ -112,6 +178,8 @@ export const elasticAssetCheckerFactory = (getService: FtrProviderContext['getSe
expectEnrichPolicyNotFound,
expectIngestPipelineExists,
expectIngestPipelineNotFound,
expectEntitiesIndexExists,
expectEntitiesIndexNotFound,
expectTransformExists,
expectTransformNotFound,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export const EntityStoreUtils = (
expectComponentTemplateNotFound,
expectIngestPipelineExists,
expectIngestPipelineNotFound,
expectEntitiesIndexExists,
expectEntitiesIndexNotFound,
} = elasticAssetCheckerFactory(getService);

log.debug(`EntityStoreUtils namespace: ${namespace}`);
Expand Down Expand Up @@ -68,6 +70,31 @@ export const EntityStoreUtils = (
expect(res.status).to.eql(200);
};

const initEntityEngineForEntityTypeAndWait = async (entityType: EntityType) => {
await initEntityEngineForEntityType(entityType);

let attempts = 10;
let lastBody: any = null;
const delayMs = 2000;

while (attempts > 0) {
const { body } = await api.getEntityEngine({ params: { entityType } }, namespace).expect(200);
lastBody = body;
if (body.status === 'started') {
return;
}

await new Promise((resolve) => setTimeout(resolve, delayMs));
attempts--;
}

throw new Error(
`Engine for entity type ${entityType} did not start after multiple attempts, last status: ${JSON.stringify(
lastBody
)}`
);
};

const expectTransformStatus = async (
transformId: string,
exists: boolean,
Expand Down Expand Up @@ -101,18 +128,21 @@ export const EntityStoreUtils = (
await expectEnrichPolicyExists(`entity_store_field_retention_${entityType}_${namespace}_v1`);
await expectComponentTemplateExists(`security_${entityType}_${namespace}-latest@platform`);
await expectIngestPipelineExists(`security_${entityType}_${namespace}-latest@platform`);
await expectEntitiesIndexExists(entityType, namespace);
};

const expectEngineAssetsDoNotExist = async (entityType: EntityType) => {
await expectTransformNotFound(`entities-v1-latest-security_${entityType}_${namespace}`);
await expectEnrichPolicyNotFound(`entity_store_field_retention_${entityType}_${namespace}_v1`);
await expectComponentTemplateNotFound(`security_${entityType}_${namespace}-latest@platform`);
await expectIngestPipelineNotFound(`security_${entityType}_${namespace}-latest@platform`);
await expectEntitiesIndexNotFound(entityType, namespace);
};

return {
cleanEngines,
initEntityEngineForEntityType,
initEntityEngineForEntityTypeAndWait,
expectTransformStatus,
expectEngineAssetsExist,
expectEngineAssetsDoNotExist,
Expand Down

0 comments on commit 1148af8

Please sign in to comment.