Skip to content

Commit

Permalink
[Index management] Api integration tests (#36343) (#36428)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebelga authored May 10, 2019
1 parent b9799b5 commit e8baa99
Show file tree
Hide file tree
Showing 16 changed files with 666 additions and 36 deletions.

This file was deleted.

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 @@ -10,6 +10,7 @@ export default function ({ loadTestFile }) {
loadTestFile(require.resolve('./cross_cluster_replication'));
loadTestFile(require.resolve('./remote_clusters'));
loadTestFile(require.resolve('./rollup'));
loadTestFile(require.resolve('./index_management'));
loadTestFile(require.resolve('./index_lifecycle_management'));
});
}
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 const API_BASE_PATH = '/api/index_management';
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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 default function ({ loadTestFile }) {
describe('index management', () => {
loadTestFile(require.resolve('./indices'));
loadTestFile(require.resolve('./mapping'));
loadTestFile(require.resolve('./settings'));
loadTestFile(require.resolve('./stats'));
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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 { API_BASE_PATH } from './constants';

export const registerHelpers = ({ supertest }) => {
const executeActionOnIndices = (index, urlParam, args) => {
const indices = Array.isArray(index) ? index : [index];

return supertest.post(`${API_BASE_PATH}/indices/${urlParam}`)
.set('kbn-xsrf', 'xxx')
.send({ indices, ...args });
};

const closeIndex = (index) => executeActionOnIndices(index, 'close');

const openIndex = (index) => executeActionOnIndices(index, 'open');

const deleteIndex = (index) => executeActionOnIndices(index, 'delete');

const flushIndex = (index) => executeActionOnIndices(index, 'flush');

const refreshIndex = (index) => executeActionOnIndices(index, 'refresh');

const forceMerge = (index, args) => executeActionOnIndices(index, 'forcemerge', args);

const freeze = (index) => executeActionOnIndices(index, 'freeze');

const unfreeze = (index) => executeActionOnIndices(index, 'unfreeze');

const clearCache = (index) => executeActionOnIndices(index, 'clear_cache');

const list = () => supertest.get(`${API_BASE_PATH}/indices`);

const reload = (indexNames) => (
supertest.post(`${API_BASE_PATH}/indices/reload`)
.set('kbn-xsrf', 'xxx')
.send({ indexNames })
);

return {
closeIndex,
openIndex,
deleteIndex,
flushIndex,
refreshIndex,
forceMerge,
freeze,
unfreeze,
list,
reload,
clearCache,
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
/*
* 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 { initElasticsearchHelpers } from './lib';
import { registerHelpers } from './indices.helpers';

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

const {
createIndex,
catIndex,
indexStats,
cleanUp: cleanUpEsResources
} = initElasticsearchHelpers(es);

const {
closeIndex,
openIndex,
deleteIndex,
flushIndex,
refreshIndex,
forceMerge,
freeze,
unfreeze,
list,
reload,
clearCache,
} = registerHelpers({ supertest });

describe('indices', () => {
after(() => Promise.all([cleanUpEsResources()]));

describe('clear cache', () => {
it('should clear the cache on all indices', async () => {
await clearCache('*').expect(200);
});

it('should clear the cache on a single index', async () => {
const index = await createIndex();
await clearCache(index).expect(200);
});
});

describe('close', () => {
it('should close an index', async () => {
const index = await createIndex();

// Make sure the index is open
const [cat1] = await catIndex(index);
expect(cat1.status).to.be('open');

await closeIndex(index).expect(200);

// Make sure the index has been closed
const [cat2] = await catIndex(index);
expect(cat2.status).to.be('close');
});
});

describe('open', () => {
it('should open an index', async () => {
const index = await createIndex();

await closeIndex(index);

// Make sure the index is closed
const [cat1] = await catIndex(index);
expect(cat1.status).to.be('close');

await openIndex(index).expect(200);

// Make sure the index is opened
const [cat2] = await catIndex(index);
expect(cat2.status).to.be('open');
});
});

describe('delete', () => {
it('should delete an index', async () => {
const index = await createIndex();

const indices1 = await catIndex(undefined, 'i');
expect(indices1.map(index => index.i)).to.contain(index);

await deleteIndex([index]).expect(200);

const indices2 = await catIndex(undefined, 'i');
expect(indices2.map(index => index.i)).not.to.contain(index);
});

it('should require index or indices to be provided', async () => {
const { body } = await deleteIndex().expect(400);
expect(body.message).to.contain('index / indices is missing');
});
});

describe('flush', () => {
it('should flush an index', async () => {
const index = await createIndex();

const { indices: indices1 } = await indexStats(index, 'flush');
expect(indices1[index].total.flush.total).to.be(0);

await flushIndex(index).expect(200);

const { indices: indices2 } = await indexStats(index, 'flush');
expect(indices2[index].total.flush.total).to.be(1);
});
});

describe('refresh', () => {
it('should refresh an index', async () => {
const index = await createIndex();

const { indices: indices1 } = await indexStats(index, 'refresh');
const previousRefreshes = indices1[index].total.refresh.total;

await refreshIndex(index).expect(200);

const { indices: indices2 } = await indexStats(index, 'refresh');
expect(indices2[index].total.refresh.total).to.be(previousRefreshes + 1);
});
});

describe('forcemerge', () => {
it('should force merge an index', async () => {
const index = await createIndex();
await forceMerge(index).expect(200);
});

it('should allow to define the number of segments', async () => {
const index = await createIndex();
await forceMerge(index, { max_num_segments: 1 }).expect(200);
});
});

describe('freeze', () => {
it('should freeze an index', async () => {
const index = await createIndex();
// "sth" correspond to search throttling. Frozen indices are normal indices
// with search throttling turned on.
const [cat1] = await catIndex(index, 'sth');
expect(cat1.sth).to.be('false');

await freeze(index).expect(200);

const [cat2] = await catIndex(index, 'sth');
expect(cat2.sth).to.be('true');
});
});

describe('unfreeze', () => {
it('should unfreeze an index', async () => {
const index = await createIndex();

await freeze(index).expect(200);
const [cat1] = await catIndex(index, 'sth');
expect(cat1.sth).to.be('true');

await unfreeze(index).expect(200);
const [cat2] = await catIndex(index, 'sth');
expect(cat2.sth).to.be('false');
});
});

describe('list', () => {
it('should list all the indices with the expected properties and data enrichers', async () => {
const { body } = await list().expect(200);
const expectedKeys = [
'health',
'status',
'name',
'uuid',
'primary',
'replica',
'documents',
'size',
'isFrozen',
'aliases',
'ilm', // data enricher
'isRollupIndex', // data enricher
'isFollowerIndex' // data enricher
];
expect(Object.keys(body[0])).to.eql(expectedKeys);
});
});

describe('reload', () => {
it('should list all the indices with the expected properties and data enrichers', async () => {
const { body } = await reload().expect(200);
const expectedKeys = [
'health',
'status',
'name',
'uuid',
'primary',
'replica',
'documents',
'size',
'isFrozen',
'aliases',
'ilm', // data enricher
'isRollupIndex', // data enricher
'isFollowerIndex' // data enricher
];
expect(Object.keys(body[0])).to.eql(expectedKeys);
expect(body.length > 1).to.be(true); // to contrast it with the next test
});

it('should allow reloading only certain indices', async () => {
const index = await createIndex();
const { body } = await reload([index]);

expect(body.length === 1).to.be(true);
expect(body[0].name).to.be(index);
});
});
});
}
Loading

0 comments on commit e8baa99

Please sign in to comment.