Skip to content

Commit

Permalink
Fix places calling callEs still
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed Feb 12, 2020
1 parent ee4393c commit 88b525c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { IClusterClientAdapter } from './cluster_client_adapter';

const createClusterClientMock = () => {
const mock: jest.Mocked<IClusterClientAdapter> = {
indexDocument: jest.fn(),
doesIlmPolicyExist: jest.fn(),
createIlmPolicy: jest.fn(),
doesIndexTemplateExist: jest.fn(),
Expand Down
16 changes: 16 additions & 0 deletions x-pack/plugins/event_log/server/es/cluster_client_adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ beforeEach(() => {
});
});

describe('indexDocument', () => {
test('should call cluster client with given doc', async () => {
await clusterClientAdapter.indexDocument({ args: true });
expect(clusterClient.callAsInternalUser).toHaveBeenCalledWith('index', {
args: true,
});
});

test('should throw error when cluster client throws an error', async () => {
clusterClient.callAsInternalUser.mockRejectedValue(new Error('Fail'));
await expect(
clusterClientAdapter.indexDocument({ args: true })
).rejects.toThrowErrorMatchingInlineSnapshot(`"Fail"`);
});
});

describe('doesIlmPolicyExist', () => {
const notFoundError = new Error('Not found') as any;
notFoundError.statusCode = 404;
Expand Down
4 changes: 4 additions & 0 deletions x-pack/plugins/event_log/server/es/cluster_client_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export class ClusterClientAdapter {
this.clusterClient = opts.clusterClient;
}

public async indexDocument(doc: any): Promise<void> {
await this.callEs('index', doc);
}

public async doesIlmPolicyExist(policyName: string): Promise<boolean> {
const request = {
method: 'GET',
Expand Down
8 changes: 2 additions & 6 deletions x-pack/plugins/event_log/server/event_log_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@

import { IEventLogConfig } from './types';
import { EventLogService } from './event_log_service';
import { getEsNames } from './es/names';
import { createMockEsContext } from './es/context.mock';
import { contextMock } from './es/context.mock';
import { loggingServiceMock } from '../../../../src/core/server/logging/logging_service.mock';

const loggingService = loggingServiceMock.create();
const systemLogger = loggingService.get();

describe('EventLogService', () => {
const esContext = createMockEsContext({
esNames: getEsNames('ABC'),
logger: systemLogger,
});
const esContext = contextMock.create();

function getService(config: IEventLogConfig) {
const { enabled, logEntries, indexEntries } = config;
Expand Down
5 changes: 2 additions & 3 deletions x-pack/plugins/event_log/server/event_logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import { IEvent, IEventLogger, IEventLogService } from './index';
import { ECS_VERSION } from './types';
import { EventLogService } from './event_log_service';
import { getEsNames } from './es/names';
import { EsContext } from './es/context';
import { createMockEsContext } from './es/context.mock';
import { contextMock } from './es/context.mock';
import { loggerMock, MockedLogger } from '../../../../src/core/server/logging/logger.mock';
import { delay } from './lib/delay';
import { EVENT_LOGGED_PREFIX } from './event_logger';
Expand All @@ -24,7 +23,7 @@ describe('EventLogger', () => {

beforeEach(() => {
systemLogger = loggerMock.create();
esContext = createMockEsContext({ esNames: getEsNames('ABC'), logger: systemLogger });
esContext = contextMock.create();
service = new EventLogService({
esContext,
systemLogger,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/event_log/server/event_logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function indexEventDoc(esContext: EsContext, doc: Doc): void {
async function indexLogEventDoc(esContext: EsContext, doc: any) {
esContext.logger.debug(`writing to event log: ${JSON.stringify(doc)}`);
await esContext.waitTillReady();
await esContext.callEs('index', doc);
await esContext.esAdapter.indexDocument(doc);
esContext.logger.debug(`writing to event log complete`);
}

Expand Down

0 comments on commit 88b525c

Please sign in to comment.