Skip to content

Commit

Permalink
add api integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alisonelizabeth committed Nov 30, 2020
1 parent e9b2e30 commit 0c4792e
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 1 deletion.
1 change: 1 addition & 0 deletions x-pack/test/api_integration/apis/management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export default function ({ loadTestFile }) {
loadTestFile(require.resolve('./index_management'));
loadTestFile(require.resolve('./index_lifecycle_management'));
loadTestFile(require.resolve('./ingest_pipelines'));
loadTestFile(require.resolve('./snapshot_restore'));
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { FtrProviderContext } from '../../../ftr_provider_context';

export default function ({ loadTestFile }: FtrProviderContext) {
describe('Snapshot and Restore', () => {
loadTestFile(require.resolve('./snapshot_restore'));
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { FtrProviderContext } from '../../../../ftr_provider_context';

interface SlmPolicy {
name: string;
snapshotName: string;
schedule: string;
repository: string;
isManagedPolicy: boolean;
config?: {
indices?: string | string[];
ignoreUnavailable?: boolean;
includeGlobalState?: boolean;
partial?: boolean;
metadata?: Record<string, string>;
};
retention?: {
expireAfterValue?: number | '';
expireAfterUnit?: string;
maxCount?: number | '';
minCount?: number | '';
};
}

/**
* Helpers to create and delete SLM policies on the Elasticsearch instance
* during our tests.
* @param {ElasticsearchClient} es The Elasticsearch client instance
*/
export const registerEsHelpers = (getService: FtrProviderContext['getService']) => {
let policiesCreated: string[] = [];

const es = getService('legacyEs');

const createRepository = (repoName: string) => {
return es.snapshot.createRepository({
repository: repoName,
body: {
type: 'fs',
settings: {
location: '/tmp/',
},
},
verify: false,
});
};

const createPolicy = (policy: SlmPolicy, cachePolicy?: boolean) => {
if (cachePolicy) {
policiesCreated.push(policy.name);
}

return es.sr.updatePolicy({
name: policy.name,
body: policy,
});
};

const deletePolicy = (policyName: string) => es.sr.deletePolicy({ name: policyName });

const cleanupPolicies = () =>
Promise.all(policiesCreated.map(deletePolicy))
.then(() => {
policiesCreated = [];
})
.catch((err) => {
// eslint-disable-next-line no-console
console.log(`[Cleanup error] Error deleting ES resources: ${err.message}`);
});

return {
createRepository,
createPolicy,
deletePolicy,
cleanupPolicies,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

export { registerEsHelpers } from './elasticsearch';
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import expect from '@kbn/expect';

import { FtrProviderContext } from '../../../ftr_provider_context';
import { registerEsHelpers } from './lib';

const API_BASE_PATH = '/api/snapshot_restore';
const REPO_NAME = 'test_repo';

export default function ({ getService }: FtrProviderContext) {
const supertest = getService('supertest');

const { createRepository, createPolicy, deletePolicy, cleanupPolicies } = registerEsHelpers(
getService
);

describe('Snapshot Lifecycle Management', function () {
before(async () => {
try {
await createRepository(REPO_NAME);
} catch (err) {
// eslint-disable-next-line no-console
console.log('[Setup error] Error creating repository');
throw err;
}
});

after(async () => {
await cleanupPolicies();
});

describe('Create', () => {
const POLICY_NAME = 'test_create_policy';
const REQUIRED_FIELDS_POLICY_NAME = 'test_create_required_fields_policy';

after(async () => {
// Clean up any policies created in test cases
await Promise.all([POLICY_NAME, REQUIRED_FIELDS_POLICY_NAME].map(deletePolicy)).catch(
(err) => {
// eslint-disable-next-line no-console
console.log(`[Cleanup error] Error deleting policies: ${err.message}`);
throw err;
}
);
});

it('should create a SLM policy', async () => {
const { body } = await supertest
.post(`${API_BASE_PATH}/policies`)
.set('kbn-xsrf', 'xxx')
.send({
name: POLICY_NAME,
snapshotName: 'my_snapshot',
schedule: '0 30 1 * * ?',
repository: REPO_NAME,
config: {
indices: ['my_index'],
ignoreUnavailable: true,
partial: false,
metadata: {
meta: 'my_meta',
},
},
retention: {
expireAfterValue: 1,
expireAfterUnit: 'd',
maxCount: 10,
minCount: 5,
},
isManagedPolicy: false,
})
.expect(200);

expect(body).to.eql({
acknowledged: true,
});
});

it('should create a policy with only required fields', async () => {
const { body } = await supertest
.post(`${API_BASE_PATH}/policies`)
.set('kbn-xsrf', 'xxx')
// Exclude config and retention
.send({
name: REQUIRED_FIELDS_POLICY_NAME,
snapshotName: 'my_snapshot',
repository: REPO_NAME,
schedule: '0 30 1 * * ?',
isManagedPolicy: false,
})
.expect(200);

expect(body).to.eql({
acknowledged: true,
});
});
});

describe('Update', () => {
const POLICY_NAME = 'test_update_policy';
const POLICY = {
name: POLICY_NAME,
snapshotName: 'my_snapshot',
schedule: '0 30 1 * * ?',
repository: REPO_NAME,
config: {
indices: ['my_index'],
ignoreUnavailable: true,
partial: false,
metadata: {
meta: 'my_meta',
},
},
retention: {
expireAfterValue: 1,
expireAfterUnit: 'd',
maxCount: 10,
minCount: 5,
},
isManagedPolicy: false,
};

before(async () => {
// Create SLM policy that can be used to test PUT request
try {
await createPolicy(POLICY, true);
} catch (err) {
// eslint-disable-next-line no-console
console.log('[Setup error] Error creating policy');
throw err;
}
});

it('should allow an existing policy to be updated', async () => {
const uri = `${API_BASE_PATH}/policies/${POLICY_NAME}`;

const { body } = await supertest
.put(uri)
.set('kbn-xsrf', 'xxx')
.send({
...POLICY,
schedule: '0 0 0 ? * 7',
})
.expect(200);

expect(body).to.eql({
acknowledged: true,
});
});

it('should allow optional fields to be removed', async () => {
const uri = `${API_BASE_PATH}/policies/${POLICY_NAME}`;
const { retention, config, ...requiredFields } = POLICY;

const { body } = await supertest
.put(uri)
.set('kbn-xsrf', 'xxx')
.send(requiredFields)
.expect(200);

expect(body).to.eql({
acknowledged: true,
});
});
});
});
}
3 changes: 2 additions & 1 deletion x-pack/test/api_integration/services/legacy_es.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as legacyElasticsearch from 'elasticsearch';

import { elasticsearchClientPlugin as securityEsClientPlugin } from '../../../plugins/security/server/elasticsearch/elasticsearch_client_plugin';
import { elasticsearchJsPlugin as indexManagementEsClientPlugin } from '../../../plugins/index_management/server/client/elasticsearch';
import { elasticsearchJsPlugin as snapshotRestoreEsClientPlugin } from '../../../plugins/snapshot_restore/server/client/elasticsearch_sr';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { DEFAULT_API_VERSION } from '../../../../src/core/server/elasticsearch/elasticsearch_config';

Expand All @@ -20,6 +21,6 @@ export function LegacyEsProvider({ getService }) {
apiVersion: DEFAULT_API_VERSION,
host: formatUrl(config.get('servers.elasticsearch')),
requestTimeout: config.get('timeouts.esRequestTimeout'),
plugins: [securityEsClientPlugin, indexManagementEsClientPlugin],
plugins: [securityEsClientPlugin, indexManagementEsClientPlugin, snapshotRestoreEsClientPlugin],
});
}

0 comments on commit 0c4792e

Please sign in to comment.