Skip to content

Commit

Permalink
add reportingstore test
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Jun 23, 2020
1 parent 82a8525 commit 6cc3f38
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 6 deletions.
38 changes: 38 additions & 0 deletions x-pack/plugins/reporting/server/lib/store/report.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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 { Report } from './report';

describe('Class Report', () => {
it('constructs Report instance', () => {
const opts = {
index: '.reporting-test-index-12345',
jobtype: 'test-report',
created_by: 'created_by_test_string',
browser_type: 'browser_type_test_string',
max_attempts: 50,
payload: { payload_test_field: 1 },
timeout: 30000,
priority: 1,
};
const report = new Report(opts);
expect(report).toMatchObject({
_primary_term: undefined,
_seq_no: undefined,
browser_type: 'browser_type_test_string',
created_by: 'created_by_test_string',
jobtype: 'test-report',
max_attempts: 50,
payload: {
payload_test_field: 1,
},
priority: 1,
timeout: 30000,
});

expect(report.id).toBeDefined();
});
});
8 changes: 4 additions & 4 deletions x-pack/plugins/reporting/server/lib/store/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface Payload {
payload: unknown;
browser_type: string;
priority: number;
maxAttempts: number;
max_attempts: number;
timeout: number;
}

Expand All @@ -30,7 +30,7 @@ export class Report {

public readonly priority: number;
// queue stuff, to be removed with Task Manager integration
public readonly maxAttempts: number;
public readonly max_attempts: number;
public readonly timeout: number;

public _index: string;
Expand All @@ -47,7 +47,7 @@ export class Report {
this.payload = opts.payload;
this.browser_type = opts.browser_type;
this.priority = opts.priority;
this.maxAttempts = opts.maxAttempts;
this.max_attempts = opts.max_attempts;
this.timeout = opts.timeout;
this.id = puid.generate();

Expand Down Expand Up @@ -77,7 +77,7 @@ export class Report {
created_by: this.created_by,
payload: this.payload,
timeout: this.timeout,
max_attempts: this.maxAttempts,
max_attempts: this.max_attempts,
priority: this.priority,
browser_type: this.browser_type,
};
Expand Down
145 changes: 145 additions & 0 deletions x-pack/plugins/reporting/server/lib/store/store.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* 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 sinon from 'sinon';
import { ReportingConfig, ReportingCore } from '../..';
import { createMockReportingCore } from '../../test_helpers';
import { createMockLevelLogger } from '../../test_helpers/create_mock_levellogger';
import { ReportingStore } from './store';
import { ElasticsearchServiceSetup } from 'src/core/server';

const getMockConfig = (mockConfigGet: sinon.SinonStub) => ({
get: mockConfigGet,
kbnConfig: { get: mockConfigGet },
});

describe('ReportingStore', () => {
const mockLogger = createMockLevelLogger();
let mockConfig: ReportingConfig;
let mockCore: ReportingCore;

const callClusterStub = sinon.stub();
const mockElasticsearch = {
legacy: {
client: {
callAsInternalUser: callClusterStub,
},
},
};

beforeEach(async () => {
const mockConfigGet = sinon.stub();
mockConfigGet.withArgs('index').returns('.reporting-test');
mockConfigGet.withArgs('queue', 'indexInterval').returns('week');
mockConfig = getMockConfig(mockConfigGet);
mockCore = await createMockReportingCore(mockConfig);

callClusterStub.withArgs('indices.exists').resolves({});
callClusterStub.withArgs('indices.create').resolves({});
callClusterStub.withArgs('index').resolves({});
callClusterStub.withArgs('indices.refresh').resolves({});
callClusterStub.withArgs('update').resolves({});

mockCore.getElasticsearchService = () =>
(mockElasticsearch as unknown) as ElasticsearchServiceSetup;
});

describe('addReport', () => {
it('returns Report object', async () => {
const store = new ReportingStore(mockCore, mockLogger);
const reportType = 'unknowntype';
const reportPayload = {};
const reportOptions = {
timeout: 10000,
created_by: 'created_by_string',
browser_type: 'browser_type_string',
max_attempts: 1,
};
await expect(
store.addReport(reportType, reportPayload, reportOptions)
).resolves.toMatchObject({
_primary_term: undefined,
_seq_no: undefined,
browser_type: 'browser_type_string',
created_by: 'created_by_string',
jobtype: 'unknowntype',
max_attempts: 1,
payload: {},
priority: 10,
timeout: 10000,
});
});

it('throws if options has invalid indexInterval', async () => {
const mockConfigGet = sinon.stub();
mockConfigGet.withArgs('index').returns('.reporting-test');
mockConfigGet.withArgs('queue', 'indexInterval').returns('centurially');
mockConfig = getMockConfig(mockConfigGet);
mockCore = await createMockReportingCore(mockConfig);

const store = new ReportingStore(mockCore, mockLogger);
const reportType = 'unknowntype';
const reportPayload = {};
const reportOptions = {
timeout: 10000,
created_by: 'created_by_string',
browser_type: 'browser_type_string',
max_attempts: 1,
};
expect(
store.addReport(reportType, reportPayload, reportOptions)
).rejects.toMatchInlineSnapshot(`[Error: Invalid index interval: centurially]`);
});

it('handles error creating the index', async () => {
// setup
callClusterStub.withArgs('indices.exists').resolves(false);
callClusterStub.withArgs('indices.create').rejects(new Error('error'));

const store = new ReportingStore(mockCore, mockLogger);
const reportType = 'unknowntype';
const reportPayload = {};
const reportOptions = {
timeout: 10000,
created_by: 'created_by_string',
browser_type: 'browser_type_string',
max_attempts: 1,
};
await expect(
store.addReport(reportType, reportPayload, reportOptions)
).rejects.toMatchInlineSnapshot(`[Error: error]`);
});

it('skips creating the index if already exists', async () => {
// setup
callClusterStub.withArgs('indices.exists').resolves(true);
callClusterStub.withArgs('indices.create').rejects(new Error('error')); // will not be triggered

const store = new ReportingStore(mockCore, mockLogger);
const reportType = 'unknowntype';
const reportPayload = {};
const reportOptions = {
timeout: 10000,
created_by: 'created_by_string',
browser_type: 'browser_type_string',
max_attempts: 1,
};
await expect(
store.addReport(reportType, reportPayload, reportOptions)
).resolves.toMatchObject({
_primary_term: undefined,
_seq_no: undefined,
browser_type: 'browser_type_string',
created_by: 'created_by_string',
jobtype: 'unknowntype',
max_attempts: 1,
payload: {},
priority: 10,
timeout: 10000,
});
});
});
});
4 changes: 2 additions & 2 deletions x-pack/plugins/reporting/server/lib/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class ReportingStore {
process_expiration: new Date(0), // use epoch so the job query works
created_at: new Date(),
attempts: 0,
max_attempts: report.maxAttempts,
max_attempts: report.max_attempts,
status: statuses.JOB_STATUS_PENDING,
browser_type: report.browser_type,
},
Expand All @@ -151,7 +151,7 @@ export class ReportingStore {
jobtype: type,
created_by: options.created_by,
browser_type: options.browser_type,
maxAttempts: options.max_attempts,
max_attempts: options.max_attempts,
timeout: options.timeout,
priority: 10, // unused
});
Expand Down

0 comments on commit 6cc3f38

Please sign in to comment.