[Enterprise Search] Create connectors index if none is present (#139092)

This commit is contained in:
Sander Philipse 2022-08-18 14:42:01 +02:00 committed by GitHub
parent f5352432cb
commit f35ccba41d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 61 deletions

View file

@ -40,13 +40,20 @@ describe('addConnector lib function', () => {
asInternalUser: {},
};
const createConnectorsIndexExistsFn =
(connectorsIndexExists: boolean, defaultValue: boolean) =>
({ index }: { index: string }) =>
index === CONNECTORS_INDEX ? connectorsIndexExists : defaultValue;
beforeEach(() => {
jest.clearAllMocks();
});
it('should add connector', async () => {
mockClient.asCurrentUser.index.mockImplementation(() => ({ _id: 'fakeId' }));
mockClient.asCurrentUser.indices.exists.mockImplementation(() => false);
mockClient.asCurrentUser.indices.exists.mockImplementation(
createConnectorsIndexExistsFn(true, false)
);
(fetchConnectorByIndexName as jest.Mock).mockImplementation(() => undefined);
(fetchCrawlerByIndexName as jest.Mock).mockImplementation(() => undefined);
@ -82,7 +89,9 @@ describe('addConnector lib function', () => {
it('should reject if index already exists', async () => {
mockClient.asCurrentUser.index.mockImplementation(() => ({ _id: 'fakeId' }));
mockClient.asCurrentUser.indices.exists.mockImplementation(() => true);
mockClient.asCurrentUser.indices.exists.mockImplementation(
createConnectorsIndexExistsFn(true, true)
);
(fetchConnectorByIndexName as jest.Mock).mockImplementation(() => undefined);
(fetchCrawlerByIndexName as jest.Mock).mockImplementation(() => undefined);
@ -97,7 +106,9 @@ describe('addConnector lib function', () => {
it('should reject if connector already exists', async () => {
mockClient.asCurrentUser.index.mockImplementation(() => ({ _id: 'fakeId' }));
mockClient.asCurrentUser.indices.exists.mockImplementation(() => false);
mockClient.asCurrentUser.indices.exists.mockImplementation(
createConnectorsIndexExistsFn(true, false)
);
(fetchConnectorByIndexName as jest.Mock).mockImplementation(() => true);
(fetchCrawlerByIndexName as jest.Mock).mockImplementation(() => undefined);
@ -110,9 +121,28 @@ describe('addConnector lib function', () => {
expect(mockClient.asCurrentUser.indices.create).not.toHaveBeenCalled();
});
it('should reject if crawler already exists', async () => {
mockClient.asCurrentUser.index.mockImplementation(() => ({ _id: 'fakeId' }));
mockClient.asCurrentUser.indices.exists.mockImplementation(
createConnectorsIndexExistsFn(true, false)
);
(fetchConnectorByIndexName as jest.Mock).mockImplementation(() => undefined);
(fetchCrawlerByIndexName as jest.Mock).mockImplementation(() => true);
await expect(
addConnector(mockClient as unknown as IScopedClusterClient, {
index_name: 'index_name',
language: 'en',
})
).rejects.toEqual(new Error(ErrorCode.CRAWLER_ALREADY_EXISTS));
expect(mockClient.asCurrentUser.indices.create).not.toHaveBeenCalled();
});
it('should reject with index already exists if connector and index already exist', async () => {
mockClient.asCurrentUser.index.mockImplementation(() => ({ _id: 'fakeId' }));
mockClient.asCurrentUser.indices.exists.mockImplementation(() => true);
mockClient.asCurrentUser.indices.exists.mockImplementation(
createConnectorsIndexExistsFn(true, true)
);
(fetchConnectorByIndexName as jest.Mock).mockImplementation(() => true);
(fetchCrawlerByIndexName as jest.Mock).mockImplementation(() => undefined);
@ -127,7 +157,9 @@ describe('addConnector lib function', () => {
it('should replace connector if deleteExistingConnector flag is true', async () => {
mockClient.asCurrentUser.index.mockImplementation(() => ({ _id: 'fakeId' }));
mockClient.asCurrentUser.indices.exists.mockImplementation(() => false);
mockClient.asCurrentUser.indices.exists.mockImplementation(
createConnectorsIndexExistsFn(true, false)
);
(fetchConnectorByIndexName as jest.Mock).mockImplementation(() => ({ id: 'connectorId' }));
(fetchCrawlerByIndexName as jest.Mock).mockImplementation(() => undefined);
@ -167,13 +199,9 @@ describe('addConnector lib function', () => {
});
it('should create index if no connectors index exists', async () => {
mockClient.asCurrentUser.index.mockImplementationOnce(() => {
return Promise.reject({
meta: { body: { error: { type: 'index_not_found_exception' } } },
statusCode: 404,
});
});
mockClient.asCurrentUser.indices.exists.mockImplementation(() => false);
mockClient.asCurrentUser.indices.exists.mockImplementation(
createConnectorsIndexExistsFn(false, false)
);
(fetchConnectorByIndexName as jest.Mock).mockImplementation(() => false);
(fetchCrawlerByIndexName as jest.Mock).mockImplementation(() => undefined);
await expect(
@ -206,36 +234,4 @@ describe('addConnector lib function', () => {
settings: textAnalysisSettings('en'),
});
});
it('should not create index if status code is not 404', async () => {
mockClient.asCurrentUser.index.mockImplementationOnce(() => {
return Promise.reject({ statusCode: 500 });
});
mockClient.asCurrentUser.indices.exists.mockImplementation(() => false);
(fetchConnectorByIndexName as jest.Mock).mockImplementation(() => false);
(fetchCrawlerByIndexName as jest.Mock).mockImplementation(() => undefined);
await expect(
addConnector(mockClient as unknown as IScopedClusterClient, {
index_name: 'index_name',
language: 'en',
})
).rejects.toEqual({ statusCode: 500 });
expect(setupConnectorsIndices).not.toHaveBeenCalled();
expect(mockClient.asCurrentUser.index).toHaveBeenCalledTimes(1);
});
it('should not create index if crawler exists', async () => {
mockClient.asCurrentUser.index.mockImplementationOnce(() => {
return 'connector ';
});
mockClient.asCurrentUser.indices.exists.mockImplementation(() => false);
(fetchConnectorByIndexName as jest.Mock).mockImplementation(() => false);
(fetchCrawlerByIndexName as jest.Mock).mockImplementation(() => 'crawler');
await expect(
addConnector(mockClient as unknown as IScopedClusterClient, {
index_name: 'index_name',
language: 'en',
})
).rejects.toEqual(new Error(ErrorCode.CRAWLER_ALREADY_EXISTS));
expect(setupConnectorsIndices).not.toHaveBeenCalled();
expect(mockClient.asCurrentUser.index).not.toHaveBeenCalled();
});
});

View file

@ -11,7 +11,6 @@ import { CONNECTORS_INDEX } from '../..';
import { ConnectorDocument, ConnectorStatus } from '../../../common/types/connectors';
import { ErrorCode } from '../../../common/types/error_codes';
import { setupConnectorsIndices } from '../../index_management/setup_indices';
import { isIndexNotFoundException } from '../../utils/identify_exceptions';
import { fetchCrawlerByIndexName } from '../crawler/fetch_crawlers';
import { textAnalysisSettings } from '../indices/text_analysis';
@ -80,21 +79,11 @@ export const addConnector = async (
status: ConnectorStatus.CREATED,
sync_now: false,
};
try {
return await createConnector(
document,
client,
input.language,
!!input.delete_existing_connector
);
} catch (error) {
if (isIndexNotFoundException(error)) {
// This means .ent-search-connectors index doesn't exist yet
// So we first have to create it, and then try inserting the document again
await setupConnectorsIndices(client.asCurrentUser);
return await createConnector(document, client, input.language, false);
} else {
throw error;
}
const connectorsIndexExists = await client.asCurrentUser.indices.exists({
index: CONNECTORS_INDEX,
});
if (!connectorsIndexExists) {
await setupConnectorsIndices(client.asCurrentUser);
}
return await createConnector(document, client, input.language, !!input.delete_existing_connector);
};