Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core usage data #79101

Merged
merged 21 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions src/core/server/core_usage_data/core_usage_data_service.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { PublicMethodsOf } from '@kbn/utility-types';
import { BehaviorSubject } from 'rxjs';
import { CoreUsageDataService } from './core_usage_data_service';
import { CoreUsageData, CoreUsageDataStart } from './types';

const createStartContractMock = () => {
const startContract: jest.Mocked<CoreUsageDataStart> = {
getCoreUsageData: jest.fn().mockReturnValue(
new BehaviorSubject<CoreUsageData>({
config: {
elasticsearch: {
apiVersion: 'master',
customHeadersConfigured: false,
healthCheckDelayMs: 2500,
logQueries: false,
numberOfHostsConfigured: 1,
pingTimeoutMs: 30000,
requestHeadersWhitelistConfigured: false,
requestTimeoutMs: 30000,
shardTimeoutMs: 30000,
sniffIntervalMs: -1,
sniffOnConnectionFault: false,
sniffOnStart: false,
ssl: {
alwaysPresentCertificate: false,
certificateAuthoritiesConfigured: false,
certificateConfigured: false,
keyConfigured: false,
verificationMode: 'full',
keystoreConfigured: false,
truststoreConfigured: false,
},
},
http: {
basePathConfigured: false,
compression: {
enabled: true,
referrerWhitelistConfigured: false,
},
keepaliveTimeout: 120000,
maxPayloadInBytes: 1048576,
requestId: {
allowFromAnyIp: false,
ipAllowlistConfigured: false,
},
rewriteBasePath: false,
socketTimeout: 120000,
ssl: {
certificateAuthoritiesConfigured: false,
certificateConfigured: false,
cipherSuites: [
'ECDHE-RSA-AES128-GCM-SHA256',
'ECDHE-ECDSA-AES128-GCM-SHA256',
'ECDHE-RSA-AES256-GCM-SHA384',
'ECDHE-ECDSA-AES256-GCM-SHA384',
'DHE-RSA-AES128-GCM-SHA256',
'ECDHE-RSA-AES128-SHA256',
'DHE-RSA-AES128-SHA256',
'ECDHE-RSA-AES256-SHA384',
'DHE-RSA-AES256-SHA384',
'ECDHE-RSA-AES256-SHA256',
'DHE-RSA-AES256-SHA256',
'HIGH',
'!aNULL',
'!eNULL',
'!EXPORT',
'!DES',
'!RC4',
'!MD5',
'!PSK',
'!SRP',
'!CAMELLIA',
],
clientAuthentication: 'none',
keyConfigured: false,
keystoreConfigured: false,
redirectHttpFromPortConfigured: false,
supportedProtocols: ['TLSv1.1', 'TLSv1.2'],
truststoreConfigured: false,
},
xsrf: {
disableProtection: false,
whitelistConfigured: false,
},
},
logging: {
appendersTypesUsed: [],
loggersConfiguredCount: 0,
},
savedObjects: {
maxImportExportSizeBytes: 10000,
maxImportPayloadBytes: 10485760,
},
},
environment: {
memory: {
heapSizeLimit: 1,
heapTotalBytes: 1,
heapUsedBytes: 1,
},
os: {
platform: 'darwin',
platformRelease: 'test',
},
},
})
),
};

return startContract;
};

const createMock = () => {
const mocked: jest.Mocked<PublicMethodsOf<CoreUsageDataService>> = {
setup: jest.fn(),
start: jest.fn().mockReturnValue(createStartContractMock()),
stop: jest.fn(),
};
return mocked;
};

export const coreUsageDataServiceMock = {
create: createMock,
createStartContract: createStartContractMock,
};
206 changes: 206 additions & 0 deletions src/core/server/core_usage_data/core_usage_data_service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { BehaviorSubject, Observable } from 'rxjs';
import { HotObservable } from 'rxjs/internal/testing/HotObservable';
import { TestScheduler } from 'rxjs/testing';

import { configServiceMock } from '../config/mocks';

import { mockCoreContext } from '../core_context.mock';
import { config as RawElasticsearchConfig } from '../elasticsearch/elasticsearch_config';
import { config as RawHttpConfig } from '../http/http_config';
import { config as RawLoggingConfig } from '../logging/logging_config';
import { savedObjectsConfig as RawSavedObjectsConfig } from '../saved_objects/saved_objects_config';
import { metricsServiceMock } from '../metrics/metrics_service.mock';

import { CoreUsageDataService } from './core_usage_data_service';

describe('CoreUsageDataService', () => {
const getTestScheduler = () =>
new TestScheduler((actual, expected) => {
expect(actual).toEqual(expected);
});

let service: CoreUsageDataService;
const configService = configServiceMock.create();
configService.atPath.mockImplementation((path) => {
if (path === 'elasticsearch') {
return new BehaviorSubject(RawElasticsearchConfig.schema.validate({}));
} else if (path === 'server') {
return new BehaviorSubject(RawHttpConfig.schema.validate({}));
} else if (path === 'logging') {
return new BehaviorSubject(RawLoggingConfig.schema.validate({}));
} else if (path === 'savedObjects') {
return new BehaviorSubject(RawSavedObjectsConfig.schema.validate({}));
}
return new BehaviorSubject({});
});
const coreContext = mockCoreContext.create({ configService });

beforeEach(() => {
service = new CoreUsageDataService(coreContext);
});

describe('start', () => {
describe('getCoreUsageData', () => {
it('returns core metrics for default config', () => {
const metrics = metricsServiceMock.createInternalSetupContract();
service.setup({ metrics });
const { getCoreUsageData } = service.start();
expect(getCoreUsageData()).toMatchInlineSnapshot(`
Object {
"config": Object {
"elasticsearch": Object {
"apiVersion": "master",
"customHeadersConfigured": false,
"healthCheckDelayMs": 2500,
"logQueries": false,
"numberOfHostsConfigured": 1,
"pingTimeoutMs": 30000,
"requestHeadersWhitelistConfigured": false,
"requestTimeoutMs": 30000,
"shardTimeoutMs": 30000,
"sniffIntervalMs": -1,
"sniffOnConnectionFault": false,
"sniffOnStart": false,
"ssl": Object {
"alwaysPresentCertificate": false,
"certificateAuthoritiesConfigured": false,
"certificateConfigured": false,
"keyConfigured": false,
"keystoreConfigured": false,
"truststoreConfigured": false,
"verificationMode": "full",
},
},
"http": Object {
"basePathConfigured": false,
"compression": Object {
"enabled": true,
"referrerWhitelistConfigured": false,
},
"keepaliveTimeout": 120000,
"maxPayloadInBytes": 1048576,
"requestId": Object {
"allowFromAnyIp": false,
"ipAllowlistConfigured": false,
},
"rewriteBasePath": false,
"socketTimeout": 120000,
"ssl": Object {
"certificateAuthoritiesConfigured": false,
"certificateConfigured": false,
"cipherSuites": Array [
"ECDHE-RSA-AES128-GCM-SHA256",
"ECDHE-ECDSA-AES128-GCM-SHA256",
"ECDHE-RSA-AES256-GCM-SHA384",
"ECDHE-ECDSA-AES256-GCM-SHA384",
"DHE-RSA-AES128-GCM-SHA256",
"ECDHE-RSA-AES128-SHA256",
"DHE-RSA-AES128-SHA256",
"ECDHE-RSA-AES256-SHA384",
"DHE-RSA-AES256-SHA384",
"ECDHE-RSA-AES256-SHA256",
"DHE-RSA-AES256-SHA256",
"HIGH",
"!aNULL",
"!eNULL",
"!EXPORT",
"!DES",
"!RC4",
"!MD5",
"!PSK",
"!SRP",
"!CAMELLIA",
],
"clientAuthentication": "none",
"keyConfigured": false,
"keystoreConfigured": false,
"redirectHttpFromPortConfigured": false,
"supportedProtocols": Array [
"TLSv1.1",
"TLSv1.2",
],
"truststoreConfigured": false,
},
"xsrf": Object {
"disableProtection": false,
"whitelistConfigured": false,
},
},
"logging": Object {
"appendersTypesUsed": Array [],
"loggersConfiguredCount": 0,
},
"savedObjects": Object {
"maxImportExportSizeBytes": 10000,
"maxImportPayloadBytes": 10485760,
},
},
"environment": Object {
"memory": Object {
"heapSizeLimit": 1,
"heapTotalBytes": 1,
"heapUsedBytes": 1,
},
"os": Object {
"platform": "darwin",
"platformRelease": "test",
},
},
}
`);
});
});
});

describe('setup and stop', () => {
it('subscribes and unsubscribes from all config paths and metrics', () => {
getTestScheduler().run(({ cold, hot, expectSubscriptions }) => {
const observables: Array<HotObservable<string>> = [];
configService.atPath.mockImplementation(() => {
const newObservable = hot('-a-------');
observables.push(newObservable);
return newObservable;
});
const metrics = metricsServiceMock.createInternalSetupContract();
metrics.getOpsMetrics$.mockImplementation(() => {
const newObservable = hot('-a-------');
observables.push(newObservable);
return newObservable as Observable<any>;
});

service.setup({ metrics });

// Use the stopTimer$ to delay calling stop() until the third frame
const stopTimer$ = cold('---a|');
stopTimer$.subscribe(() => {
service.stop();
});

const subs = '^--!';

observables.forEach((o) => {
expectSubscriptions(o.subscriptions).toBe(subs);
});
});
});
});
});
Loading