Skip to content

Commit

Permalink
add integration test for isWriteBlockException
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Jul 7, 2021
1 parent c1bc869 commit 7f3f35c
Showing 1 changed file with 127 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { ElasticsearchClient } from '../../../../';
import { InternalCoreStart } from '../../../../internal_types';
import * as kbnTestServer from '../../../../../test_helpers/kbn_server';
import { Root } from '../../../../root';
import { isWriteBlockException } from '../es_errors';
import { createIndex } from '../create_index';
import { setWriteBlock } from '../set_write_block';

const { startES } = kbnTestServer.createTestServers({
adjustTimeout: (t: number) => jest.setTimeout(t),
});

describe('Elasticsearch Errors', () => {
let root: Root;
let start: InternalCoreStart;
let client: ElasticsearchClient;
let esServer: kbnTestServer.TestElasticsearchUtils;

beforeAll(async () => {
esServer = await startES();
root = kbnTestServer.createRootWithCorePlugins({
server: {
basePath: '/foo',
},
});

await root.setup();
start = await root.start();
client = start.elasticsearch.client.asInternalUser;

await createIndex({
client,
indexName: 'existing_index_with_write_block',
mappings: { properties: {} },
})();
await setWriteBlock({ client, index: 'existing_index_with_write_block' })();
});

afterAll(async () => {
await esServer.stop();
await root.shutdown();
});

describe('isWriteBlockException', () => {
it('correctly identify errors from index operations', async () => {
const res = await client.index(
{
index: 'existing_index_with_write_block',
id: 'some-id',
op_type: 'index',
body: {
hello: 'dolly',
},
},
{ ignore: [403] }
);

expect(isWriteBlockException(res.body.error!)).toEqual(true);
});

it('correctly identify errors from create operations', async () => {
const res = await client.create(
{
index: 'existing_index_with_write_block',
id: 'some-id',
body: {
hello: 'dolly',
},
},
{ ignore: [403] }
);

expect(isWriteBlockException(res.body.error!)).toEqual(true);
});

it('correctly identify errors from bulk index operations', async () => {
const res = await client.bulk({
refresh: 'wait_for',
body: [
{
index: {
_index: 'existing_index_with_write_block',
_id: 'some-id',
},
},
{
hello: 'dolly',
},
],
});

const cause = res.body.items[0].index!.error!;

expect(isWriteBlockException(cause)).toEqual(true);
});

it('correctly identify errors from bulk create operations', async () => {
const res = await client.bulk({
refresh: 'wait_for',
body: [
{
create: {
_index: 'existing_index_with_write_block',
_id: 'some-id',
op_type: 'index',
},
},
{
hello: 'dolly',
},
],
});

const cause = res.body.items[0].create!.error!;

expect(isWriteBlockException(cause)).toEqual(true);
});
});
});

0 comments on commit 7f3f35c

Please sign in to comment.