From 7f3f35c8a8f2f8c6d25b522a90623a019c3ac7c8 Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Wed, 7 Jul 2021 19:41:44 +0200 Subject: [PATCH] add integration test for isWriteBlockException --- .../integration_tests/es_errors.test.ts | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 src/core/server/saved_objects/migrationsv2/actions/integration_tests/es_errors.test.ts diff --git a/src/core/server/saved_objects/migrationsv2/actions/integration_tests/es_errors.test.ts b/src/core/server/saved_objects/migrationsv2/actions/integration_tests/es_errors.test.ts new file mode 100644 index 0000000000000..baeef6b9d9f56 --- /dev/null +++ b/src/core/server/saved_objects/migrationsv2/actions/integration_tests/es_errors.test.ts @@ -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); + }); + }); +});