Skip to content

Commit

Permalink
added minor fixes (handling 404 from ES) and added API integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jloleysens committed Jun 30, 2021
1 parent 7f4b518 commit 508c947
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
4 changes: 4 additions & 0 deletions x-pack/plugins/reporting/server/routes/deprecations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ export const registerDeprecationsRoutes = (reporting: ReportingCore, logger: Log
logger.error(err);

if (err instanceof errors.ResponseError) {
// If there were no reporting indices to update, that's OK because then there is nothing to migrate
if (err.statusCode === 404) {
return res.ok();
}
return res.customError({
statusCode: err.statusCode ?? 500,
body: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import expect from '@kbn/expect';
import { JOB_PARAMS_RISON_CSV_DEPRECATED } from '../services/fixtures';
import { FtrProviderContext } from '../ftr_provider_context';

import { ILM_POLICY_NAME } from '../../../plugins/reporting/common/constants';

// eslint-disable-next-line import/no-default-export
export default function ({ getService }: FtrProviderContext) {
const esArchiver = getService('esArchiver');
const es = getService('es');
const supertestNoAuth = getService('supertestWithoutAuth');
const reportingAPI = getService('reportingAPI');

describe('ILM policy migration APIs', () => {
before(async () => {
await esArchiver.load('x-pack/test/functional/es_archives/reporting/logs');
await esArchiver.load('x-pack/test/functional/es_archives/logstash_functional');
});

after(async () => {
await esArchiver.unload('x-pack/test/functional/es_archives/reporting/logs');
await esArchiver.unload('x-pack/test/functional/es_archives/logstash_functional');
});

afterEach(async () => {
await reportingAPI.deleteAllReports();
await reportingAPI.migrateReportingIndices(); // ensure that the ILM policy exists
});

it('detects when no migration is needed', async () => {
expect(await reportingAPI.checkIlmMigrationStatus()).to.eql('ok');

// try creating a report
await supertestNoAuth
.post(`/api/reporting/generate/csv`)
.set('kbn-xsrf', 'xxx')
.send({ jobParams: JOB_PARAMS_RISON_CSV_DEPRECATED });

expect(await reportingAPI.checkIlmMigrationStatus()).to.eql('ok');
});

it('detects when reporting indices should be migrated due to missing ILM policy', async () => {
await reportingAPI.makeAllReportingPoliciesUnmanaged();
// TODO: Remove "any" when no longer through type issue "policy_id" missing
await es.ilm.deleteLifecycle({ policy: ILM_POLICY_NAME } as any);

await supertestNoAuth
.post(`/api/reporting/generate/csv`)
.set('kbn-xsrf', 'xxx')
.send({ jobParams: JOB_PARAMS_RISON_CSV_DEPRECATED });

expect(await reportingAPI.checkIlmMigrationStatus()).to.eql('policy-not-found');
// assert that migration fixes this
await reportingAPI.migrateReportingIndices();
expect(await reportingAPI.checkIlmMigrationStatus()).to.eql('ok');
});

it('detects when reporting indices should be migrated due to unmanaged indices', async () => {
await reportingAPI.makeAllReportingPoliciesUnmanaged();
await supertestNoAuth
.post(`/api/reporting/generate/csv`)
.set('kbn-xsrf', 'xxx')
.send({ jobParams: JOB_PARAMS_RISON_CSV_DEPRECATED });

expect(await reportingAPI.checkIlmMigrationStatus()).to.eql('indices-not-managed-by-policy');
// assert that migration fixes this
await reportingAPI.migrateReportingIndices();
expect(await reportingAPI.checkIlmMigrationStatus()).to.eql('ok');
});

it('does not override an existing ILM policy', async () => {
const customLifecycle = {
policy: {
phases: {
hot: {
min_age: '0ms',
actions: {},
},
delete: {
min_age: '0ms',
actions: {
delete: {
delete_searchable_snapshot: true,
},
},
},
},
},
};

// customize the lifecycle policy
await es.ilm.putLifecycle({
policy: ILM_POLICY_NAME,
body: customLifecycle,
});

await reportingAPI.migrateReportingIndices();

const {
body: {
[ILM_POLICY_NAME]: { policy },
},
} = await es.ilm.getLifecycle({ policy: ILM_POLICY_NAME });

expect(policy).to.eql(customLifecycle.policy);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ export default function ({ loadTestFile }: FtrProviderContext) {
describe('Reporting API Integration Tests with Security disabled', function () {
this.tags('ciGroup13');
loadTestFile(require.resolve('./job_apis'));
loadTestFile(require.resolve('./ilm_migration_apis'));
});
}
33 changes: 33 additions & 0 deletions x-pack/test/reporting_api_integration/services/scenarios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,36 @@ export function createScenarios({ getService }: Pick<FtrProviderContext, 'getSer
});
};

const checkIlmMigrationStatus = async () => {
log.debug('ReportingAPI.checkIlmMigrationStatus');
const { body } = await supertestWithoutAuth
.get('/api/reporting/ilm_policy_status')
.set('kbn-xsrf', 'xxx')
.expect(200);
return body.status;
};

const migrateReportingIndices = async () => {
log.debug('ReportingAPI.migrateReportingIndices');
await supertestWithoutAuth
.put('/api/reporting/deprecations/migrate_ilm_policy')
.set('kbn-xsrf', 'xxx')
.expect(200);
};

const makeAllReportingPoliciesUnmanaged = async () => {
log.debug('ReportingAPI.makeAllReportingPoliciesUnmanaged');
const settings: any = {
'index.lifecycle.name': null,
};
await esSupertest
.put('/.reporting*/_settings')
.send({
settings,
})
.expect(200);
};

return {
initEcommerce,
teardownEcommerce,
Expand All @@ -182,5 +212,8 @@ export function createScenarios({ getService }: Pick<FtrProviderContext, 'getSer
postJob,
postJobJSON,
deleteAllReports,
checkIlmMigrationStatus,
migrateReportingIndices,
makeAllReportingPoliciesUnmanaged,
};
}

0 comments on commit 508c947

Please sign in to comment.