2018-02-02 05:47:58 -05:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2022-02-10 13:18:16 -05:00
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2018-02-02 05:47:58 -05:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2017-06-01 11:54:56 -04:00
|
|
|
import PANEL_STATE from '~/prometheus_metrics/constants';
|
2021-02-14 13:09:20 -05:00
|
|
|
import PrometheusMetrics from '~/prometheus_metrics/prometheus_metrics';
|
2020-05-19 20:08:20 -04:00
|
|
|
import { metrics2 as metrics, missingVarMetrics } from './mock_data';
|
2017-06-01 05:35:27 -04:00
|
|
|
|
|
|
|
describe('PrometheusMetrics', () => {
|
2019-03-26 12:03:28 -04:00
|
|
|
const FIXTURE = 'services/prometheus/prometheus_service.html';
|
2017-06-01 05:35:27 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
loadFixtures(FIXTURE);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('constructor', () => {
|
|
|
|
let prometheusMetrics;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
prometheusMetrics = new PrometheusMetrics('.js-prometheus-metrics-monitoring');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should initialize wrapper element refs on class object', () => {
|
|
|
|
expect(prometheusMetrics.$wrapper).toBeDefined();
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsPanel).toBeDefined();
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsCount).toBeDefined();
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsLoading).toBeDefined();
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsEmpty).toBeDefined();
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsList).toBeDefined();
|
|
|
|
expect(prometheusMetrics.$missingEnvVarPanel).toBeDefined();
|
2020-11-17 13:09:20 -05:00
|
|
|
expect(prometheusMetrics.$panelToggleRight).toBeDefined();
|
|
|
|
expect(prometheusMetrics.$panelToggleDown).toBeDefined();
|
2017-06-01 05:35:27 -04:00
|
|
|
expect(prometheusMetrics.$missingEnvVarMetricCount).toBeDefined();
|
|
|
|
expect(prometheusMetrics.$missingEnvVarMetricsList).toBeDefined();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should initialize metadata on class object', () => {
|
|
|
|
expect(prometheusMetrics.backOffRequestCounter).toEqual(0);
|
|
|
|
expect(prometheusMetrics.activeMetricsEndpoint).toContain('/test');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-01 11:54:56 -04:00
|
|
|
describe('showMonitoringMetricsPanelState', () => {
|
|
|
|
let prometheusMetrics;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
prometheusMetrics = new PrometheusMetrics('.js-prometheus-metrics-monitoring');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show loading state when called with `loading`', () => {
|
|
|
|
prometheusMetrics.showMonitoringMetricsPanelState(PANEL_STATE.LOADING);
|
|
|
|
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsLoading.hasClass('hidden')).toBeFalsy();
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsEmpty.hasClass('hidden')).toBeTruthy();
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsList.hasClass('hidden')).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show metrics list when called with `list`', () => {
|
|
|
|
prometheusMetrics.showMonitoringMetricsPanelState(PANEL_STATE.LIST);
|
|
|
|
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsLoading.hasClass('hidden')).toBeTruthy();
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsEmpty.hasClass('hidden')).toBeTruthy();
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsList.hasClass('hidden')).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show empty state when called with `empty`', () => {
|
|
|
|
prometheusMetrics.showMonitoringMetricsPanelState(PANEL_STATE.EMPTY);
|
|
|
|
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsLoading.hasClass('hidden')).toBeTruthy();
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsEmpty.hasClass('hidden')).toBeFalsy();
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsList.hasClass('hidden')).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-01 05:35:27 -04:00
|
|
|
describe('populateActiveMetrics', () => {
|
|
|
|
let prometheusMetrics;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
prometheusMetrics = new PrometheusMetrics('.js-prometheus-metrics-monitoring');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show monitored metrics list', () => {
|
|
|
|
prometheusMetrics.populateActiveMetrics(metrics);
|
|
|
|
|
|
|
|
const $metricsListLi = prometheusMetrics.$monitoredMetricsList.find('li');
|
|
|
|
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsLoading.hasClass('hidden')).toBeTruthy();
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsList.hasClass('hidden')).toBeFalsy();
|
|
|
|
|
2018-10-17 03:13:26 -04:00
|
|
|
expect(prometheusMetrics.$monitoredMetricsCount.text()).toEqual(
|
|
|
|
'3 exporters with 12 metrics were found',
|
|
|
|
);
|
2018-10-17 03:21:28 -04:00
|
|
|
|
2017-06-01 05:35:27 -04:00
|
|
|
expect($metricsListLi.length).toEqual(metrics.length);
|
2020-12-23 07:10:26 -05:00
|
|
|
expect($metricsListLi.first().find('.badge').text()).toEqual(`${metrics[0].active_metrics}`);
|
2017-06-01 05:35:27 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should show missing environment variables list', () => {
|
|
|
|
prometheusMetrics.populateActiveMetrics(missingVarMetrics);
|
|
|
|
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsLoading.hasClass('hidden')).toBeTruthy();
|
|
|
|
expect(prometheusMetrics.$missingEnvVarPanel.hasClass('hidden')).toBeFalsy();
|
|
|
|
|
|
|
|
expect(prometheusMetrics.$missingEnvVarMetricCount.text()).toEqual('2');
|
|
|
|
expect(prometheusMetrics.$missingEnvVarPanel.find('li').length).toEqual(2);
|
|
|
|
expect(prometheusMetrics.$missingEnvVarPanel.find('.flash-container')).toBeDefined();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('loadActiveMetrics', () => {
|
|
|
|
let prometheusMetrics;
|
2018-02-02 05:47:58 -05:00
|
|
|
let mock;
|
|
|
|
|
|
|
|
function mockSuccess() {
|
|
|
|
mock.onGet(prometheusMetrics.activeMetricsEndpoint).reply(200, {
|
|
|
|
data: metrics,
|
|
|
|
success: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function mockError() {
|
|
|
|
mock.onGet(prometheusMetrics.activeMetricsEndpoint).networkError();
|
|
|
|
}
|
2017-06-01 05:35:27 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2020-05-19 20:08:20 -04:00
|
|
|
jest.spyOn(axios, 'get');
|
2018-02-02 05:47:58 -05:00
|
|
|
|
2017-06-01 05:35:27 -04:00
|
|
|
prometheusMetrics = new PrometheusMetrics('.js-prometheus-metrics-monitoring');
|
2018-02-02 05:47:58 -05:00
|
|
|
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
2017-06-01 05:35:27 -04:00
|
|
|
});
|
|
|
|
|
2022-02-10 13:18:16 -05:00
|
|
|
it('should show loader animation while response is being loaded and hide it when request is complete', async () => {
|
2018-02-02 05:47:58 -05:00
|
|
|
mockSuccess();
|
2017-06-01 05:35:27 -04:00
|
|
|
|
|
|
|
prometheusMetrics.loadActiveMetrics();
|
|
|
|
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsLoading.hasClass('hidden')).toBeFalsy();
|
2018-02-02 05:47:58 -05:00
|
|
|
expect(axios.get).toHaveBeenCalledWith(prometheusMetrics.activeMetricsEndpoint);
|
2017-06-01 05:35:27 -04:00
|
|
|
|
2022-02-10 13:18:16 -05:00
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsLoading.hasClass('hidden')).toBeTruthy();
|
2017-06-01 05:35:27 -04:00
|
|
|
});
|
|
|
|
|
2022-02-10 13:18:16 -05:00
|
|
|
it('should show empty state if response failed to load', async () => {
|
2018-02-02 05:47:58 -05:00
|
|
|
mockError();
|
2017-06-01 05:35:27 -04:00
|
|
|
|
|
|
|
prometheusMetrics.loadActiveMetrics();
|
|
|
|
|
2022-02-10 13:18:16 -05:00
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsLoading.hasClass('hidden')).toBeTruthy();
|
|
|
|
expect(prometheusMetrics.$monitoredMetricsEmpty.hasClass('hidden')).toBeFalsy();
|
2017-06-01 05:35:27 -04:00
|
|
|
});
|
|
|
|
|
2022-02-10 13:18:16 -05:00
|
|
|
it('should populate metrics list once response is loaded', async () => {
|
2020-05-19 20:08:20 -04:00
|
|
|
jest.spyOn(prometheusMetrics, 'populateActiveMetrics').mockImplementation();
|
2018-02-02 05:47:58 -05:00
|
|
|
mockSuccess();
|
2017-06-01 05:35:27 -04:00
|
|
|
|
|
|
|
prometheusMetrics.loadActiveMetrics();
|
|
|
|
|
2022-02-10 13:18:16 -05:00
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(prometheusMetrics.populateActiveMetrics).toHaveBeenCalledWith(metrics);
|
2017-06-01 05:35:27 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|