-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
189 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
x-pack/plugins/reporting/server/lib/store/store.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters