debian-mirror-gitlab/spec/frontend/prometheus_metrics/prometheus_metrics_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

177 lines
6.5 KiB
JavaScript
Raw Normal View History

2018-03-17 18:26:18 +05:30
import MockAdapter from 'axios-mock-adapter';
2023-07-09 08:55:56 +05:30
import prometheusIntegration from 'test_fixtures/integrations/prometheus/prometheus_integration.html';
2023-06-20 00:43:36 +05:30
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
2022-04-04 11:22:00 +05:30
import waitForPromises from 'helpers/wait_for_promises';
2018-03-17 18:26:18 +05:30
import axios from '~/lib/utils/axios_utils';
2023-04-23 21:23:45 +05:30
import { HTTP_STATUS_OK } from '~/lib/utils/http_status';
2017-09-10 17:25:29 +05:30
import PANEL_STATE from '~/prometheus_metrics/constants';
2021-03-11 19:13:27 +05:30
import PrometheusMetrics from '~/prometheus_metrics/prometheus_metrics';
2020-05-24 23:13:21 +05:30
import { metrics2 as metrics, missingVarMetrics } from './mock_data';
2017-09-10 17:25:29 +05:30
describe('PrometheusMetrics', () => {
beforeEach(() => {
2023-06-20 00:43:36 +05:30
setHTMLFixture(prometheusIntegration);
2017-09-10 17:25:29 +05:30
});
describe('constructor', () => {
let prometheusMetrics;
beforeEach(() => {
prometheusMetrics = new PrometheusMetrics('.js-prometheus-metrics-monitoring');
});
2022-07-16 23:28:13 +05:30
afterEach(() => {
resetHTMLFixture();
});
2017-09-10 17:25:29 +05:30
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();
2021-01-29 00:20:46 +05:30
expect(prometheusMetrics.$panelToggleRight).toBeDefined();
expect(prometheusMetrics.$panelToggleDown).toBeDefined();
2017-09-10 17:25:29 +05:30
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');
});
});
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);
2022-08-27 11:52:29 +05:30
expect(prometheusMetrics.$monitoredMetricsLoading.hasClass('hidden')).toBe(false);
expect(prometheusMetrics.$monitoredMetricsEmpty.hasClass('hidden')).toBe(true);
expect(prometheusMetrics.$monitoredMetricsList.hasClass('hidden')).toBe(true);
2017-09-10 17:25:29 +05:30
});
it('should show metrics list when called with `list`', () => {
prometheusMetrics.showMonitoringMetricsPanelState(PANEL_STATE.LIST);
2022-08-27 11:52:29 +05:30
expect(prometheusMetrics.$monitoredMetricsLoading.hasClass('hidden')).toBe(true);
expect(prometheusMetrics.$monitoredMetricsEmpty.hasClass('hidden')).toBe(true);
expect(prometheusMetrics.$monitoredMetricsList.hasClass('hidden')).toBe(false);
2017-09-10 17:25:29 +05:30
});
it('should show empty state when called with `empty`', () => {
prometheusMetrics.showMonitoringMetricsPanelState(PANEL_STATE.EMPTY);
2022-08-27 11:52:29 +05:30
expect(prometheusMetrics.$monitoredMetricsLoading.hasClass('hidden')).toBe(true);
expect(prometheusMetrics.$monitoredMetricsEmpty.hasClass('hidden')).toBe(false);
expect(prometheusMetrics.$monitoredMetricsList.hasClass('hidden')).toBe(true);
2017-09-10 17:25:29 +05:30
});
});
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');
2022-08-27 11:52:29 +05:30
expect(prometheusMetrics.$monitoredMetricsLoading.hasClass('hidden')).toBe(true);
expect(prometheusMetrics.$monitoredMetricsList.hasClass('hidden')).toBe(false);
2017-09-10 17:25:29 +05:30
2018-12-13 13:39:08 +05:30
expect(prometheusMetrics.$monitoredMetricsCount.text()).toEqual(
'3 exporters with 12 metrics were found',
);
2017-09-10 17:25:29 +05:30
expect($metricsListLi.length).toEqual(metrics.length);
2021-03-08 18:12:59 +05:30
expect($metricsListLi.first().find('.badge').text()).toEqual(`${metrics[0].active_metrics}`);
2017-09-10 17:25:29 +05:30
});
it('should show missing environment variables list', () => {
prometheusMetrics.populateActiveMetrics(missingVarMetrics);
2022-08-27 11:52:29 +05:30
expect(prometheusMetrics.$monitoredMetricsLoading.hasClass('hidden')).toBe(true);
expect(prometheusMetrics.$missingEnvVarPanel.hasClass('hidden')).toBe(false);
2017-09-10 17:25:29 +05:30
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-03-17 18:26:18 +05:30
let mock;
function mockSuccess() {
2023-04-23 21:23:45 +05:30
mock.onGet(prometheusMetrics.activeMetricsEndpoint).reply(HTTP_STATUS_OK, {
2018-03-17 18:26:18 +05:30
data: metrics,
success: true,
});
}
function mockError() {
mock.onGet(prometheusMetrics.activeMetricsEndpoint).networkError();
}
2017-09-10 17:25:29 +05:30
beforeEach(() => {
2020-05-24 23:13:21 +05:30
jest.spyOn(axios, 'get');
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
prometheusMetrics = new PrometheusMetrics('.js-prometheus-metrics-monitoring');
2018-03-17 18:26:18 +05:30
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
2017-09-10 17:25:29 +05:30
});
2022-04-04 11:22:00 +05:30
it('should show loader animation while response is being loaded and hide it when request is complete', async () => {
2018-03-17 18:26:18 +05:30
mockSuccess();
2017-09-10 17:25:29 +05:30
prometheusMetrics.loadActiveMetrics();
2022-08-27 11:52:29 +05:30
expect(prometheusMetrics.$monitoredMetricsLoading.hasClass('hidden')).toBe(false);
2018-03-17 18:26:18 +05:30
expect(axios.get).toHaveBeenCalledWith(prometheusMetrics.activeMetricsEndpoint);
2017-09-10 17:25:29 +05:30
2022-04-04 11:22:00 +05:30
await waitForPromises();
2022-08-27 11:52:29 +05:30
expect(prometheusMetrics.$monitoredMetricsLoading.hasClass('hidden')).toBe(true);
2017-09-10 17:25:29 +05:30
});
2022-04-04 11:22:00 +05:30
it('should show empty state if response failed to load', async () => {
2018-03-17 18:26:18 +05:30
mockError();
2017-09-10 17:25:29 +05:30
prometheusMetrics.loadActiveMetrics();
2022-04-04 11:22:00 +05:30
await waitForPromises();
2022-08-27 11:52:29 +05:30
expect(prometheusMetrics.$monitoredMetricsLoading.hasClass('hidden')).toBe(true);
expect(prometheusMetrics.$monitoredMetricsEmpty.hasClass('hidden')).toBe(false);
2017-09-10 17:25:29 +05:30
});
2022-04-04 11:22:00 +05:30
it('should populate metrics list once response is loaded', async () => {
2020-05-24 23:13:21 +05:30
jest.spyOn(prometheusMetrics, 'populateActiveMetrics').mockImplementation();
2018-03-17 18:26:18 +05:30
mockSuccess();
2017-09-10 17:25:29 +05:30
prometheusMetrics.loadActiveMetrics();
2022-04-04 11:22:00 +05:30
await waitForPromises();
expect(prometheusMetrics.populateActiveMetrics).toHaveBeenCalledWith(metrics);
2017-09-10 17:25:29 +05:30
});
});
});