debian-mirror-gitlab/spec/frontend/monitoring/store/mutations_spec.js

264 lines
8.3 KiB
JavaScript
Raw Normal View History

2020-01-01 13:55:28 +05:30
import httpStatusCodes from '~/lib/utils/http_status';
2019-09-04 21:01:54 +05:30
import mutations from '~/monitoring/stores/mutations';
import * as types from '~/monitoring/stores/mutation_types';
import state from '~/monitoring/stores/state';
2020-01-01 13:55:28 +05:30
import { metricStates } from '~/monitoring/constants';
2019-09-30 21:07:59 +05:30
import {
metricsGroupsAPIResponse,
deploymentData,
metricsDashboardResponse,
dashboardGitResponse,
} from '../mock_data';
2019-09-04 21:01:54 +05:30
describe('Monitoring mutations', () => {
let stateCopy;
2020-01-01 13:55:28 +05:30
2019-09-04 21:01:54 +05:30
beforeEach(() => {
stateCopy = state();
});
2019-12-26 22:10:19 +05:30
describe('RECEIVE_METRICS_DATA_SUCCESS', () => {
2020-01-01 13:55:28 +05:30
let payload;
const getGroups = () => stateCopy.dashboard.panel_groups;
2019-09-04 21:01:54 +05:30
beforeEach(() => {
2019-12-26 22:10:19 +05:30
stateCopy.dashboard.panel_groups = [];
2020-01-01 13:55:28 +05:30
payload = metricsGroupsAPIResponse;
2019-09-04 21:01:54 +05:30
});
2019-12-26 22:10:19 +05:30
it('adds a key to the group', () => {
2020-01-01 13:55:28 +05:30
mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, payload);
const groups = getGroups();
expect(groups[0].key).toBe('response-metrics-nginx-ingress-vts--0');
expect(groups[1].key).toBe('system-metrics-kubernetes--1');
2019-09-04 21:01:54 +05:30
});
2019-12-26 22:10:19 +05:30
it('normalizes values', () => {
2020-01-01 13:55:28 +05:30
mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, payload);
2019-12-26 22:10:19 +05:30
const expectedLabel = 'Pod average';
2020-01-01 13:55:28 +05:30
const { label, query_range } = getGroups()[1].panels[0].metrics[0];
2019-12-26 22:10:19 +05:30
expect(label).toEqual(expectedLabel);
expect(query_range.length).toBeGreaterThan(0);
2019-09-04 21:01:54 +05:30
});
2020-01-01 13:55:28 +05:30
it('contains two groups, with panels with a metric each', () => {
mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, payload);
const groups = getGroups();
expect(groups).toBeDefined();
expect(groups).toHaveLength(2);
expect(groups[0].panels).toHaveLength(1);
expect(groups[0].panels[0].metrics).toHaveLength(1);
expect(groups[1].panels).toHaveLength(2);
expect(groups[1].panels[0].metrics).toHaveLength(1);
expect(groups[1].panels[1].metrics).toHaveLength(1);
2019-09-04 21:01:54 +05:30
});
2020-01-01 13:55:28 +05:30
it('assigns metrics a metric id', () => {
mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, payload);
const groups = getGroups();
expect(groups[0].panels[0].metrics[0].metricId).toEqual(
'1_response_metrics_nginx_ingress_throughput_status_code',
);
expect(groups[1].panels[0].metrics[0].metricId).toEqual(
2019-12-26 22:10:19 +05:30
'17_system_metrics_kubernetes_container_memory_average',
);
2019-09-04 21:01:54 +05:30
});
});
2019-12-26 22:10:19 +05:30
describe('RECEIVE_DEPLOYMENTS_DATA_SUCCESS', () => {
2019-09-04 21:01:54 +05:30
it('stores the deployment data', () => {
stateCopy.deploymentData = [];
mutations[types.RECEIVE_DEPLOYMENTS_DATA_SUCCESS](stateCopy, deploymentData);
expect(stateCopy.deploymentData).toBeDefined();
2020-01-01 13:55:28 +05:30
expect(stateCopy.deploymentData).toHaveLength(3);
2019-09-04 21:01:54 +05:30
expect(typeof stateCopy.deploymentData[0]).toEqual('object');
});
});
describe('SET_ENDPOINTS', () => {
it('should set all the endpoints', () => {
mutations[types.SET_ENDPOINTS](stateCopy, {
metricsEndpoint: 'additional_metrics.json',
environmentsEndpoint: 'environments.json',
deploymentsEndpoint: 'deployments.json',
dashboardEndpoint: 'dashboard.json',
2019-12-04 20:38:33 +05:30
projectPath: '/gitlab-org/gitlab-foss',
2019-09-04 21:01:54 +05:30
});
expect(stateCopy.metricsEndpoint).toEqual('additional_metrics.json');
expect(stateCopy.environmentsEndpoint).toEqual('environments.json');
expect(stateCopy.deploymentsEndpoint).toEqual('deployments.json');
expect(stateCopy.dashboardEndpoint).toEqual('dashboard.json');
2019-12-04 20:38:33 +05:30
expect(stateCopy.projectPath).toEqual('/gitlab-org/gitlab-foss');
2019-09-04 21:01:54 +05:30
});
});
2020-01-01 13:55:28 +05:30
describe('Individual panel/metric results', () => {
const metricId = '12_system_metrics_kubernetes_container_memory_total';
2019-12-26 22:10:19 +05:30
const result = [
{
values: [[0, 1], [1, 1], [1, 3]],
},
];
2020-01-01 13:55:28 +05:30
const dashboardGroups = metricsDashboardResponse.dashboard.panel_groups;
const getMetric = () => stateCopy.dashboard.panel_groups[0].panels[0].metrics[0];
describe('REQUEST_METRIC_RESULT', () => {
beforeEach(() => {
mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, dashboardGroups);
});
it('stores a loading state on a metric', () => {
expect(stateCopy.showEmptyState).toBe(true);
mutations[types.REQUEST_METRIC_RESULT](stateCopy, {
metricId,
result,
});
expect(stateCopy.showEmptyState).toBe(true);
expect(getMetric()).toEqual(
expect.objectContaining({
loading: true,
result: null,
state: metricStates.LOADING,
}),
);
2019-09-04 21:01:54 +05:30
});
});
2020-01-01 13:55:28 +05:30
describe('RECEIVE_METRIC_RESULT_SUCCESS', () => {
beforeEach(() => {
mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, dashboardGroups);
});
it('clears empty state', () => {
expect(stateCopy.showEmptyState).toBe(true);
mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](stateCopy, {
metricId,
result,
});
expect(stateCopy.showEmptyState).toBe(false);
2019-12-26 22:10:19 +05:30
});
2020-01-01 13:55:28 +05:30
it('adds results to the store', () => {
expect(getMetric().result).toBe(undefined);
mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](stateCopy, {
metricId,
result,
});
expect(getMetric().result).toHaveLength(result.length);
expect(getMetric()).toEqual(
expect.objectContaining({
loading: false,
state: metricStates.OK,
}),
);
2019-09-04 21:01:54 +05:30
});
});
2020-01-01 13:55:28 +05:30
describe('RECEIVE_METRIC_RESULT_FAILURE', () => {
beforeEach(() => {
mutations[types.RECEIVE_METRICS_DATA_SUCCESS](stateCopy, dashboardGroups);
});
it('maintains the loading state when a metric fails', () => {
expect(stateCopy.showEmptyState).toBe(true);
mutations[types.RECEIVE_METRIC_RESULT_FAILURE](stateCopy, {
metricId,
error: 'an error',
});
expect(stateCopy.showEmptyState).toBe(true);
});
it('stores a timeout error in a metric', () => {
mutations[types.RECEIVE_METRIC_RESULT_FAILURE](stateCopy, {
metricId,
error: { message: 'BACKOFF_TIMEOUT' },
});
expect(getMetric()).toEqual(
expect.objectContaining({
loading: false,
result: null,
state: metricStates.TIMEOUT,
}),
);
});
it('stores a connection failed error in a metric', () => {
mutations[types.RECEIVE_METRIC_RESULT_FAILURE](stateCopy, {
metricId,
error: {
response: {
status: httpStatusCodes.SERVICE_UNAVAILABLE,
},
},
});
expect(getMetric()).toEqual(
expect.objectContaining({
loading: false,
result: null,
state: metricStates.CONNECTION_FAILED,
}),
);
});
it('stores a bad data error in a metric', () => {
mutations[types.RECEIVE_METRIC_RESULT_FAILURE](stateCopy, {
metricId,
error: {
response: {
status: httpStatusCodes.BAD_REQUEST,
},
},
});
expect(getMetric()).toEqual(
expect.objectContaining({
loading: false,
result: null,
state: metricStates.BAD_QUERY,
}),
);
});
it('stores an unknown error in a metric', () => {
mutations[types.RECEIVE_METRIC_RESULT_FAILURE](stateCopy, {
metricId,
error: null, // no reason in response
});
expect(getMetric()).toEqual(
expect.objectContaining({
loading: false,
result: null,
state: metricStates.UNKNOWN_ERROR,
}),
);
2019-09-04 21:01:54 +05:30
});
});
});
2019-09-30 21:07:59 +05:30
describe('SET_ALL_DASHBOARDS', () => {
2019-12-26 22:10:19 +05:30
it('stores `undefined` dashboards as an empty array', () => {
mutations[types.SET_ALL_DASHBOARDS](stateCopy, undefined);
2019-09-30 21:07:59 +05:30
2019-12-26 22:10:19 +05:30
expect(stateCopy.allDashboards).toEqual([]);
});
it('stores `null` dashboards as an empty array', () => {
mutations[types.SET_ALL_DASHBOARDS](stateCopy, null);
expect(stateCopy.allDashboards).toEqual([]);
});
it('stores dashboards loaded from the git repository', () => {
mutations[types.SET_ALL_DASHBOARDS](stateCopy, dashboardGitResponse);
2019-09-30 21:07:59 +05:30
expect(stateCopy.allDashboards).toEqual(dashboardGitResponse);
});
});
2019-09-04 21:01:54 +05:30
});