Skip to content

Commit

Permalink
fix oss telemetry tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flash1293 committed Nov 20, 2019
1 parent d63a190 commit a6c905b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,11 @@ describe('getVisualizationsCollector#fetch', () => {
// In real life, the CollectorSet calls fetch and handles errors
test('defers the errors', async () => {
const mockTaskFetch = jest.fn(() => {
return Promise.reject(Error('BOOM'));
throw new Error('BOOM');
});

const { fetch } = getUsageCollector(getMockTaskManager(mockTaskFetch));
await expect(async () => {
await fetch();
}).rejects.toEqual({ message: 'BOOM' });
await expect(fetch()).rejects.toThrowErrorMatchingInlineSnapshot(`"BOOM"`);
});
});
});
6 changes: 4 additions & 2 deletions x-pack/legacy/plugins/oss_telemetry/server/lib/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { PluginSetupContract as TaskManagerPluginSetupContract } from '../../../
import { PLUGIN_ID, VIS_TELEMETRY_TASK } from '../../../constants';
import { visualizationsTaskRunner } from './visualizations/task_runner';
import KbnServer from '../../../../../../../src/legacy/server/kbn_server';
import { LegacyConfig } from '../../plugin';
import { TaskInstance } from '../../../../task_manager';

export function registerTasks({
taskManager,
Expand All @@ -19,7 +21,7 @@ export function registerTasks({
taskManager?: TaskManagerPluginSetupContract;
logger: Logger;
elasticsearch: CoreSetup['elasticsearch'];
config: any;
config: LegacyConfig;
}) {
if (!taskManager) {
logger.debug('Task manager is not available');
Expand All @@ -30,7 +32,7 @@ export function registerTasks({
[VIS_TELEMETRY_TASK]: {
title: 'X-Pack telemetry calculator for Visualizations',
type: VIS_TELEMETRY_TASK,
createTaskRunner({ taskInstance }: { taskInstance: any }) {
createTaskRunner({ taskInstance }: { taskInstance: TaskInstance }) {
return {
run: visualizationsTaskRunner(taskInstance, config, elasticsearch),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getNextMidnight } from '../../get_next_midnight';
import { VisState } from '../../../../../../../../src/legacy/core_plugins/visualizations/public';
import { TaskInstance } from '../../../../../task_manager';
import { ESSearchHit } from '../../../../../apm/typings/elasticsearch';
import { LegacyConfig } from '../../../plugin';

interface VisSummary {
type: string;
Expand Down Expand Up @@ -72,7 +73,7 @@ async function getStats(callCluster: APICaller, index: string) {

export function visualizationsTaskRunner(
taskInstance: TaskInstance,
config: any,
config: LegacyConfig,
es: CoreSetup['elasticsearch']
) {
const { callAsInternalUser: callCluster } = es.createClient('data');
Expand Down
6 changes: 5 additions & 1 deletion x-pack/legacy/plugins/oss_telemetry/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ interface UsageCollectorDefinition {
fetch(): Promise<any>;
}

export interface LegacyConfig {
get: (key: string) => string | number | boolean;
}

export interface OssTelemetrySetupDependencies {
__LEGACY: {
telemetry: {
Expand All @@ -25,7 +29,7 @@ export interface OssTelemetrySetupDependencies {
register(collector: UsageCollector): void;
};
};
config: any;
config: LegacyConfig;
xpackMainStatus: { kbnServer: KbnServer };
};
taskManager?: TaskManagerPluginSetupContract;
Expand Down
14 changes: 7 additions & 7 deletions x-pack/legacy/plugins/oss_telemetry/test_utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { CoreSetup } from 'kibana/server';
import { APICaller, CoreSetup } from 'kibana/server';

import { TaskInstance } from '../../task_manager';
import { PluginSetupContract as TaskManagerPluginSetupContract } from '../../task_manager/plugin';
Expand All @@ -27,15 +27,15 @@ const defaultMockSavedObjects = [

const defaultMockTaskDocs = [getMockTaskInstance()];

export const getMockEs = (mockCallWithInternal: any = getMockCallWithInternal()) =>
export const getMockEs = (mockCallWithInternal: APICaller = getMockCallWithInternal()) =>
(({
createClient: () => ({ callAsInternalUser: mockCallWithInternal }),
} as unknown) as CoreSetup['elasticsearch']);

export const getMockCallWithInternal = (hits: any[] = defaultMockSavedObjects) => {
return (): Promise<any> => {
export const getMockCallWithInternal = (hits: unknown[] = defaultMockSavedObjects): APICaller => {
return ((() => {
return Promise.resolve({ hits: { hits } });
};
}) as unknown) as APICaller;
};

export const getMockTaskFetch = (docs: TaskInstance[] = defaultMockTaskDocs) => {
Expand All @@ -50,8 +50,8 @@ export const getMockConfig = () => {

export const getMockTaskManager = (fetch: any = getMockTaskFetch()) =>
(({
registerTaskDefinitions: (opts: any) => undefined,
ensureScheduled: (opts: any) => Promise.resolve(),
registerTaskDefinitions: () => undefined,
ensureScheduled: () => Promise.resolve(),
fetch,
} as unknown) as TaskManagerPluginSetupContract);

Expand Down

0 comments on commit a6c905b

Please sign in to comment.