Skip to content

Commit

Permalink
fixed test
Browse files Browse the repository at this point in the history
  • Loading branch information
YulNaumenko committed Mar 15, 2021
1 parent 97eaaf8 commit e595e85
Show file tree
Hide file tree
Showing 54 changed files with 374 additions and 315 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/alerting/server/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const createAlertServicesMock = <
.fn<jest.Mocked<AlertInstance<InstanceState, InstanceContext>>, [string]>()
.mockReturnValue(alertInstanceFactoryMock),
savedObjectsClient: savedObjectsClientMock.create(),
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient().asCurrentUser,
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient(),
};
};
export type AlertServicesMock = ReturnType<typeof createAlertServicesMock>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ describe('Error count alert', () => {
expect(alertExecutor).toBeDefined();

const services = {
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient()
.asCurrentUser,
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient(),
alertInstanceFactory: jest.fn(),
};
const params = { threshold: 1 };

services.scopedClusterClient.search.mockReturnValue(
services.scopedClusterClient.asCurrentUser.search.mockReturnValue(
elasticsearchClientMock.createSuccessTransportRequestPromise({
hits: {
total: {
Expand Down Expand Up @@ -82,13 +81,12 @@ describe('Error count alert', () => {

const scheduleActions = jest.fn();
const services = {
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient()
.asCurrentUser,
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient(),
alertInstanceFactory: jest.fn(() => ({ scheduleActions })),
};
const params = { threshold: 1, windowSize: 5, windowUnit: 'm' };

services.scopedClusterClient.search.mockReturnValue(
services.scopedClusterClient.asCurrentUser.search.mockReturnValue(
elasticsearchClientMock.createSuccessTransportRequestPromise({
hits: {
total: {
Expand Down Expand Up @@ -171,13 +169,12 @@ describe('Error count alert', () => {

const scheduleActions = jest.fn();
const services = {
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient()
.asCurrentUser,
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient(),
alertInstanceFactory: jest.fn(() => ({ scheduleActions })),
};
const params = { threshold: 1, windowSize: 5, windowUnit: 'm' };

services.scopedClusterClient.search.mockReturnValue(
services.scopedClusterClient.asCurrentUser.search.mockReturnValue(
elasticsearchClientMock.createSuccessTransportRequestPromise({
hits: {
total: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ describe('Transaction error rate alert', () => {
expect(alertExecutor).toBeDefined();

const services = {
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient()
.asCurrentUser,
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient(),
alertInstanceFactory: jest.fn(),
};
const params = { threshold: 1 };

services.scopedClusterClient.search.mockReturnValue(
services.scopedClusterClient.asCurrentUser.search.mockReturnValue(
elasticsearchClientMock.createSuccessTransportRequestPromise({
hits: {
total: {
Expand Down Expand Up @@ -80,13 +79,12 @@ describe('Transaction error rate alert', () => {

const scheduleActions = jest.fn();
const services = {
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient()
.asCurrentUser,
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient(),
alertInstanceFactory: jest.fn(() => ({ scheduleActions })),
};
const params = { threshold: 10, windowSize: 5, windowUnit: 'm' };

services.scopedClusterClient.search.mockReturnValue(
services.scopedClusterClient.asCurrentUser.search.mockReturnValue(
elasticsearchClientMock.createSuccessTransportRequestPromise({
hits: {
total: {
Expand Down Expand Up @@ -190,13 +188,12 @@ describe('Transaction error rate alert', () => {

const scheduleActions = jest.fn();
const services = {
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient()
.asCurrentUser,
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient(),
alertInstanceFactory: jest.fn(() => ({ scheduleActions })),
};
const params = { threshold: 10, windowSize: 5, windowUnit: 'm' };

services.scopedClusterClient.search.mockReturnValue(
services.scopedClusterClient.asCurrentUser.search.mockReturnValue(
elasticsearchClientMock.createSuccessTransportRequestPromise({
hits: {
total: {
Expand Down Expand Up @@ -269,13 +266,12 @@ describe('Transaction error rate alert', () => {

const scheduleActions = jest.fn();
const services = {
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient()
.asCurrentUser,
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient(),
alertInstanceFactory: jest.fn(() => ({ scheduleActions })),
};
const params = { threshold: 10, windowSize: 5, windowUnit: 'm' };

services.scopedClusterClient.search.mockReturnValue(
services.scopedClusterClient.asCurrentUser.search.mockReturnValue(
elasticsearchClientMock.createSuccessTransportRequestPromise({
hits: {
total: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ const mockLibs: any = {
const executor = createMetricThresholdExecutor(mockLibs);

const services: AlertServicesMock = alertsMock.createAlertServices();
services.scopedClusterClient.search.mockImplementation((params?: any): any => {
services.scopedClusterClient.asCurrentUser.search.mockImplementation((params?: any): any => {
if (params.index === 'alternatebeat-*') return mocks.changedSourceIdResponse;
const metric = params?.body.query.bool.filter[1]?.exists.field;
if (params?.body.aggs.groupings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe('Previewing the metric threshold alert type', () => {

const services: AlertServicesMock = alertsMock.createAlertServices();

services.scopedClusterClient.search.mockImplementation((params?: any): any => {
services.scopedClusterClient.asCurrentUser.search.mockImplementation((params?: any): any => {
const metric = params?.body.query.bool.filter[1]?.exists.field;
if (params?.body.aggs.groupings) {
if (params?.body.aggs.groupings.composite.after) {
Expand Down Expand Up @@ -210,7 +210,7 @@ const config = {
} as any;

const baseParams = {
esClient: services.scopedClusterClient,
esClient: services.scopedClusterClient.asCurrentUser,
params: {
criteria: [baseCriterion],
groupBy: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { i18n } from '@kbn/i18n';
import { ElasticsearchClient } from 'kibana/server';
import { BaseAlert } from './base_alert';
import {
AlertData,
Expand Down Expand Up @@ -70,7 +71,7 @@ export class CCRReadExceptionsAlert extends BaseAlert {

protected async fetchData(
params: CommonAlertParams,
callCluster: any,
esClient: ElasticsearchClient,
clusters: AlertCluster[],
availableCcs: string[]
): Promise<AlertData[]> {
Expand All @@ -83,7 +84,7 @@ export class CCRReadExceptionsAlert extends BaseAlert {
const endMs = +new Date();
const startMs = endMs - duration;
const stats = await fetchCCRReadExceptions(
callCluster,
esClient,
esIndexPattern,
startMs,
endMs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { AlertClusterHealthType, AlertSeverity } from '../../common/enums';
import { fetchClusterHealth } from '../lib/alerts/fetch_cluster_health';
import { fetchClusters } from '../lib/alerts/fetch_clusters';
import { elasticsearchServiceMock } from 'src/core/server/mocks';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { elasticsearchClientMock } from 'src/core/server/elasticsearch/client/mocks';

const RealDate = Date;

Expand Down Expand Up @@ -81,7 +83,7 @@ describe('ClusterHealthAlert', () => {
const getState = jest.fn();
const executorOptions = {
services: {
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient().asCurrentUser,
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient(),
alertInstanceFactory: jest.fn().mockImplementation(() => {
return {
replaceState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { i18n } from '@kbn/i18n';
import { ElasticsearchClient } from 'kibana/server';
import { BaseAlert } from './base_alert';
import {
AlertData,
Expand Down Expand Up @@ -64,15 +65,15 @@ export class ClusterHealthAlert extends BaseAlert {

protected async fetchData(
params: CommonAlertParams,
callCluster: any,
esClient: ElasticsearchClient,
clusters: AlertCluster[],
availableCcs: string[]
): Promise<AlertData[]> {
let esIndexPattern = appendMetricbeatIndex(Globals.app.config, INDEX_PATTERN_ELASTICSEARCH);
if (availableCcs) {
esIndexPattern = getCcsIndexPattern(esIndexPattern, availableCcs);
}
const healths = await fetchClusterHealth(callCluster, clusters, esIndexPattern);
const healths = await fetchClusterHealth(esClient, clusters, esIndexPattern);
return healths.map((clusterHealth) => {
const shouldFire = clusterHealth.health !== AlertClusterHealthType.Green;
const severity =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('CpuUsageAlert', () => {
const getState = jest.fn();
const executorOptions = {
services: {
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient().asCurrentUser,
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient(),
alertInstanceFactory: jest.fn().mockImplementation(() => {
return {
replaceState,
Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugins/monitoring/server/alerts/cpu_usage_alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { i18n } from '@kbn/i18n';
import numeral from '@elastic/numeral';
import { ElasticsearchClient } from 'kibana/server';
import { BaseAlert } from './base_alert';
import {
AlertData,
Expand Down Expand Up @@ -68,7 +69,7 @@ export class CpuUsageAlert extends BaseAlert {

protected async fetchData(
params: CommonAlertParams,
callCluster: any,
esClient: ElasticsearchClient,
clusters: AlertCluster[],
availableCcs: string[]
): Promise<AlertData[]> {
Expand All @@ -80,7 +81,7 @@ export class CpuUsageAlert extends BaseAlert {
const endMs = +new Date();
const startMs = endMs - duration;
const stats = await fetchCpuUsageNodeStats(
callCluster,
esClient,
clusters,
esIndexPattern,
startMs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('DiskUsageAlert', () => {
const getState = jest.fn();
const executorOptions = {
services: {
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient().asCurrentUser,
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient(),
alertInstanceFactory: jest.fn().mockImplementation(() => {
return {
replaceState,
Expand Down
5 changes: 3 additions & 2 deletions x-pack/plugins/monitoring/server/alerts/disk_usage_alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { i18n } from '@kbn/i18n';
import numeral from '@elastic/numeral';
import { ElasticsearchClient } from 'kibana/server';
import { BaseAlert } from './base_alert';
import {
AlertData,
Expand Down Expand Up @@ -67,7 +68,7 @@ export class DiskUsageAlert extends BaseAlert {

protected async fetchData(
params: CommonAlertParams,
callCluster: any,
esClient: ElasticsearchClient,
clusters: AlertCluster[],
availableCcs: string[]
): Promise<AlertData[]> {
Expand All @@ -77,7 +78,7 @@ export class DiskUsageAlert extends BaseAlert {
}
const { duration, threshold } = params;
const stats = await fetchDiskUsageNodeStats(
callCluster,
esClient,
clusters,
esIndexPattern,
duration as string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ describe('ElasticsearchVersionMismatchAlert', () => {
const getState = jest.fn();
const executorOptions = {
services: {
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient().asCurrentUser,
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient(),
alertInstanceFactory: jest.fn().mockImplementation(() => {
return {
replaceState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { i18n } from '@kbn/i18n';
import { ElasticsearchClient } from 'kibana/server';
import { BaseAlert } from './base_alert';
import {
AlertData,
Expand Down Expand Up @@ -53,7 +54,7 @@ export class ElasticsearchVersionMismatchAlert extends BaseAlert {

protected async fetchData(
params: CommonAlertParams,
callCluster: any,
esClient: ElasticsearchClient,
clusters: AlertCluster[],
availableCcs: string[]
): Promise<AlertData[]> {
Expand All @@ -62,7 +63,7 @@ export class ElasticsearchVersionMismatchAlert extends BaseAlert {
esIndexPattern = getCcsIndexPattern(esIndexPattern, availableCcs);
}
const elasticsearchVersions = await fetchElasticsearchVersions(
callCluster,
esClient,
clusters,
esIndexPattern,
Globals.app.config.ui.max_bucket_size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('KibanaVersionMismatchAlert', () => {
const getState = jest.fn();
const executorOptions = {
services: {
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient().asCurrentUser,
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient(),
alertInstanceFactory: jest.fn().mockImplementation(() => {
return {
replaceState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { i18n } from '@kbn/i18n';
import { ElasticsearchClient } from 'kibana/server';
import { BaseAlert } from './base_alert';
import {
AlertData,
Expand Down Expand Up @@ -66,7 +67,7 @@ export class KibanaVersionMismatchAlert extends BaseAlert {

protected async fetchData(
params: CommonAlertParams,
callCluster: any,
esClient: ElasticsearchClient,
clusters: AlertCluster[],
availableCcs: string[]
): Promise<AlertData[]> {
Expand All @@ -75,7 +76,7 @@ export class KibanaVersionMismatchAlert extends BaseAlert {
kibanaIndexPattern = getCcsIndexPattern(kibanaIndexPattern, availableCcs);
}
const kibanaVersions = await fetchKibanaVersions(
callCluster,
esClient,
clusters,
kibanaIndexPattern,
Globals.app.config.ui.max_bucket_size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

import { i18n } from '@kbn/i18n';
import { ElasticsearchClient } from 'kibana/server';
import { BaseAlert } from './base_alert';
import {
AlertData,
Expand Down Expand Up @@ -59,7 +60,7 @@ export class LargeShardSizeAlert extends BaseAlert {

protected async fetchData(
params: CommonAlertParams & { indexPattern: string },
callCluster: any,
esClient: ElasticsearchClient,
clusters: AlertCluster[],
availableCcs: string[]
): Promise<AlertData[]> {
Expand All @@ -70,7 +71,7 @@ export class LargeShardSizeAlert extends BaseAlert {
const { threshold, indexPattern: shardIndexPatterns } = params;

const stats = await fetchIndexShardSize(
callCluster,
esClient,
clusters,
esIndexPattern,
threshold!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('LicenseExpirationAlert', () => {
const getState = jest.fn();
const executorOptions = {
services: {
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient().asCurrentUser,
scopedClusterClient: elasticsearchServiceMock.createScopedClusterClient(),
alertInstanceFactory: jest.fn().mockImplementation(() => {
return {
replaceState,
Expand Down
Loading

0 comments on commit e595e85

Please sign in to comment.