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

458 lines
13 KiB
JavaScript
Raw Normal View History

2020-04-22 19:07:51 +05:30
import _ from 'lodash';
2021-03-11 19:13:27 +05:30
import { metricStates } from '~/monitoring/constants';
2020-01-01 13:55:28 +05:30
import * as getters from '~/monitoring/stores/getters';
import * as types from '~/monitoring/stores/mutation_types';
2021-03-11 19:13:27 +05:30
import mutations from '~/monitoring/stores/mutations';
import { metricsDashboardPayload } from '../fixture_data';
2020-05-24 23:13:21 +05:30
import {
2020-07-28 23:09:34 +05:30
customDashboardBasePath,
2020-05-24 23:13:21 +05:30
environmentData,
metricsResult,
dashboardGitResponse,
2020-07-28 23:09:34 +05:30
storeVariables,
2020-06-23 00:09:42 +05:30
mockLinks,
2020-05-24 23:13:21 +05:30
} from '../mock_data';
2020-01-01 13:55:28 +05:30
describe('Monitoring store Getters', () => {
2020-10-24 23:57:45 +05:30
let state;
const getMetric = ({ group = 0, panel = 0, metric = 0 } = {}) =>
state.dashboard.panelGroups[group].panels[panel].metrics[metric];
const setMetricSuccess = ({ group, panel, metric, result = metricsResult } = {}) => {
const { metricId } = getMetric({ group, panel, metric });
mutations[types.RECEIVE_METRIC_RESULT_SUCCESS](state, {
metricId,
data: {
resultType: 'matrix',
result,
},
});
};
const setMetricFailure = ({ group, panel, metric } = {}) => {
const { metricId } = getMetric({ group, panel, metric });
mutations[types.RECEIVE_METRIC_RESULT_FAILURE](state, {
metricId,
});
};
2020-01-01 13:55:28 +05:30
describe('getMetricStates', () => {
let setupState;
let getMetricStates;
beforeEach(() => {
setupState = (initState = {}) => {
state = initState;
getMetricStates = getters.getMetricStates(state);
};
});
it('has method-style access', () => {
setupState();
expect(getMetricStates).toEqual(expect.any(Function));
});
it('when dashboard has no panel groups, returns empty', () => {
setupState({
dashboard: {
2020-04-08 14:13:33 +05:30
panelGroups: [],
2020-01-01 13:55:28 +05:30
},
});
expect(getMetricStates()).toEqual([]);
});
describe('when the dashboard is set', () => {
let groups;
beforeEach(() => {
setupState({
2020-04-08 14:13:33 +05:30
dashboard: { panelGroups: [] },
2020-01-01 13:55:28 +05:30
});
2020-04-22 19:07:51 +05:30
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload);
2020-04-08 14:13:33 +05:30
groups = state.dashboard.panelGroups;
2020-01-01 13:55:28 +05:30
});
it('no loaded metric returns empty', () => {
expect(getMetricStates()).toEqual([]);
});
it('on an empty metric with no result, returns NO_DATA', () => {
2020-04-22 19:07:51 +05:30
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload);
2020-10-24 23:57:45 +05:30
setMetricSuccess({ group: 2, result: [] });
2020-01-01 13:55:28 +05:30
expect(getMetricStates()).toEqual([metricStates.NO_DATA]);
});
it('on a metric with a result, returns OK', () => {
2020-04-22 19:07:51 +05:30
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload);
setMetricSuccess({ group: 1 });
2020-01-01 13:55:28 +05:30
expect(getMetricStates()).toEqual([metricStates.OK]);
});
it('on a metric with an error, returns an error', () => {
2020-04-22 19:07:51 +05:30
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload);
setMetricFailure({});
2020-01-01 13:55:28 +05:30
expect(getMetricStates()).toEqual([metricStates.UNKNOWN_ERROR]);
});
it('on multiple metrics with results, returns OK', () => {
2020-04-22 19:07:51 +05:30
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload);
setMetricSuccess({ group: 1 });
setMetricSuccess({ group: 1, panel: 1 });
2020-01-01 13:55:28 +05:30
expect(getMetricStates()).toEqual([metricStates.OK]);
// Filtered by groups
2020-04-08 14:13:33 +05:30
expect(getMetricStates(state.dashboard.panelGroups[1].key)).toEqual([metricStates.OK]);
expect(getMetricStates(state.dashboard.panelGroups[2].key)).toEqual([]);
2020-01-01 13:55:28 +05:30
});
it('on multiple metrics errors', () => {
2020-04-22 19:07:51 +05:30
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload);
2020-01-01 13:55:28 +05:30
2020-04-22 19:07:51 +05:30
setMetricFailure({});
setMetricFailure({ group: 1 });
2020-01-01 13:55:28 +05:30
// Entire dashboard fails
expect(getMetricStates()).toEqual([metricStates.UNKNOWN_ERROR]);
expect(getMetricStates(groups[0].key)).toEqual([metricStates.UNKNOWN_ERROR]);
expect(getMetricStates(groups[1].key)).toEqual([metricStates.UNKNOWN_ERROR]);
});
it('on multiple metrics with errors', () => {
2020-04-22 19:07:51 +05:30
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload);
2020-01-01 13:55:28 +05:30
// An success in 1 group
2020-04-22 19:07:51 +05:30
setMetricSuccess({ group: 1 });
2020-01-01 13:55:28 +05:30
// An error in 2 groups
2020-04-22 19:07:51 +05:30
setMetricFailure({ group: 1, panel: 1 });
setMetricFailure({ group: 2, panel: 0 });
2020-01-01 13:55:28 +05:30
expect(getMetricStates()).toEqual([metricStates.OK, metricStates.UNKNOWN_ERROR]);
expect(getMetricStates(groups[1].key)).toEqual([
metricStates.OK,
metricStates.UNKNOWN_ERROR,
]);
2020-04-08 14:13:33 +05:30
expect(getMetricStates(groups[2].key)).toEqual([metricStates.UNKNOWN_ERROR]);
2020-01-01 13:55:28 +05:30
});
});
});
describe('metricsWithData', () => {
let metricsWithData;
let setupState;
beforeEach(() => {
setupState = (initState = {}) => {
state = initState;
metricsWithData = getters.metricsWithData(state);
};
});
afterEach(() => {
state = null;
});
it('has method-style access', () => {
setupState();
expect(metricsWithData).toEqual(expect.any(Function));
});
it('when dashboard has no panel groups, returns empty', () => {
setupState({
dashboard: {
2020-04-08 14:13:33 +05:30
panelGroups: [],
2020-01-01 13:55:28 +05:30
},
});
expect(metricsWithData()).toEqual([]);
});
describe('when the dashboard is set', () => {
beforeEach(() => {
setupState({
2020-04-08 14:13:33 +05:30
dashboard: { panelGroups: [] },
2020-01-01 13:55:28 +05:30
});
});
it('no loaded metric returns empty', () => {
2020-04-22 19:07:51 +05:30
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload);
2020-01-01 13:55:28 +05:30
expect(metricsWithData()).toEqual([]);
});
it('an empty metric, returns empty', () => {
2020-04-22 19:07:51 +05:30
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload);
2020-10-24 23:57:45 +05:30
setMetricSuccess({ result: [] });
2020-01-01 13:55:28 +05:30
expect(metricsWithData()).toEqual([]);
});
it('a metric with results, it returns a metric', () => {
2020-04-22 19:07:51 +05:30
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload);
2020-10-24 23:57:45 +05:30
setMetricSuccess();
2020-01-01 13:55:28 +05:30
2020-10-24 23:57:45 +05:30
expect(metricsWithData()).toEqual([getMetric().metricId]);
2020-01-01 13:55:28 +05:30
});
it('multiple metrics with results, it return multiple metrics', () => {
2020-04-22 19:07:51 +05:30
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload);
2020-10-24 23:57:45 +05:30
setMetricSuccess({ panel: 0 });
setMetricSuccess({ panel: 1 });
2020-01-01 13:55:28 +05:30
2020-10-24 23:57:45 +05:30
expect(metricsWithData()).toEqual([
getMetric({ panel: 0 }).metricId,
getMetric({ panel: 1 }).metricId,
]);
2020-01-01 13:55:28 +05:30
});
it('multiple metrics with results, it returns metrics filtered by group', () => {
2020-04-22 19:07:51 +05:30
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, metricsDashboardPayload);
2020-10-24 23:57:45 +05:30
setMetricSuccess({ group: 1 });
setMetricSuccess({ group: 1, panel: 1 });
2020-01-01 13:55:28 +05:30
2020-04-08 14:13:33 +05:30
// First group has metrics
expect(metricsWithData(state.dashboard.panelGroups[1].key)).toEqual([
2020-10-24 23:57:45 +05:30
getMetric({ group: 1 }).metricId,
getMetric({ group: 1, panel: 1 }).metricId,
2020-01-01 13:55:28 +05:30
]);
2020-04-08 14:13:33 +05:30
// Second group has no metrics
expect(metricsWithData(state.dashboard.panelGroups[2].key)).toEqual([]);
2020-01-01 13:55:28 +05:30
});
});
});
2020-03-13 15:44:24 +05:30
describe('filteredEnvironments', () => {
const setupState = (initState = {}) => {
state = {
...state,
...initState,
};
};
beforeAll(() => {
setupState({
environments: environmentData,
});
});
afterAll(() => {
state = null;
});
[
{
input: '',
output: 17,
},
{
input: ' ',
output: 17,
},
{
input: null,
output: 17,
},
{
input: 'does-not-exist',
output: 0,
},
{
input: 'noop-branch-',
output: 15,
},
{
input: 'noop-branch-9',
output: 1,
},
].forEach(({ input, output }) => {
it(`filteredEnvironments returns ${output} items for ${input}`, () => {
setupState({
environmentsSearchTerm: input,
});
expect(getters.filteredEnvironments(state).length).toBe(output);
});
});
});
2020-04-22 19:07:51 +05:30
describe('metricsSavedToDb', () => {
let metricsSavedToDb;
let mockData;
beforeEach(() => {
mockData = _.cloneDeep(metricsDashboardPayload);
state = {
dashboard: {
panelGroups: [],
},
};
});
it('return no metrics when dashboard is not persisted', () => {
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, mockData);
metricsSavedToDb = getters.metricsSavedToDb(state);
expect(metricsSavedToDb).toEqual([]);
});
it('return a metric id when one metric is persisted', () => {
const id = 99;
const [metric] = mockData.panel_groups[0].panels[0].metrics;
metric.metric_id = id;
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, mockData);
metricsSavedToDb = getters.metricsSavedToDb(state);
expect(metricsSavedToDb).toEqual([`${id}_${metric.id}`]);
});
it('return a metric id when two metrics are persisted', () => {
const id1 = 101;
const id2 = 102;
const [metric1] = mockData.panel_groups[0].panels[0].metrics;
const [metric2] = mockData.panel_groups[0].panels[1].metrics;
// database persisted 2 metrics
metric1.metric_id = id1;
metric2.metric_id = id2;
mutations[types.RECEIVE_METRICS_DASHBOARD_SUCCESS](state, mockData);
metricsSavedToDb = getters.metricsSavedToDb(state);
expect(metricsSavedToDb).toEqual([`${id1}_${metric1.id}`, `${id2}_${metric2.id}`]);
});
});
2020-05-24 23:13:21 +05:30
2020-05-30 21:06:31 +05:30
describe('getCustomVariablesParams', () => {
2020-05-24 23:13:21 +05:30
beforeEach(() => {
state = {
2020-06-23 00:09:42 +05:30
variables: {},
2020-05-24 23:13:21 +05:30
};
});
2020-06-23 00:09:42 +05:30
it('transforms the variables object to an array in the [variable, variable_value] format for all variable types', () => {
2020-07-28 23:09:34 +05:30
state.variables = storeVariables;
2020-05-30 21:06:31 +05:30
const variablesArray = getters.getCustomVariablesParams(state);
expect(variablesArray).toEqual({
2020-07-28 23:09:34 +05:30
'variables[textSimple]': 'My default value',
'variables[textAdvanced]': 'A default value',
'variables[customSimple]': 'value1',
'variables[customAdvanced]': 'value2',
'variables[customAdvancedWithoutLabel]': 'value2',
'variables[customAdvancedWithoutOptText]': 'value2',
2020-05-30 21:06:31 +05:30
});
2020-05-24 23:13:21 +05:30
});
2020-06-23 00:09:42 +05:30
it('transforms the variables object to an empty array when no keys are present', () => {
2020-07-28 23:09:34 +05:30
state.variables = [];
2020-05-30 21:06:31 +05:30
const variablesArray = getters.getCustomVariablesParams(state);
2020-05-24 23:13:21 +05:30
2020-05-30 21:06:31 +05:30
expect(variablesArray).toEqual({});
2020-05-24 23:13:21 +05:30
});
});
describe('selectedDashboard', () => {
const { selectedDashboard } = getters;
2021-03-08 18:12:59 +05:30
const localGetters = (localState) => ({
2020-10-24 23:57:45 +05:30
fullDashboardPath: getters.fullDashboardPath(localState),
2020-07-28 23:09:34 +05:30
});
2020-05-24 23:13:21 +05:30
it('returns a dashboard', () => {
2020-10-24 23:57:45 +05:30
const localState = {
2020-05-24 23:13:21 +05:30
allDashboards: dashboardGitResponse,
currentDashboard: dashboardGitResponse[0].path,
2020-07-28 23:09:34 +05:30
customDashboardBasePath,
2020-05-24 23:13:21 +05:30
};
2020-10-24 23:57:45 +05:30
expect(selectedDashboard(localState, localGetters(localState))).toEqual(
dashboardGitResponse[0],
);
2020-05-24 23:13:21 +05:30
});
2020-10-24 23:57:45 +05:30
it('returns a dashboard different from the overview dashboard', () => {
const localState = {
2020-05-24 23:13:21 +05:30
allDashboards: dashboardGitResponse,
currentDashboard: dashboardGitResponse[1].path,
2020-07-28 23:09:34 +05:30
customDashboardBasePath,
2020-05-24 23:13:21 +05:30
};
2020-10-24 23:57:45 +05:30
expect(selectedDashboard(localState, localGetters(localState))).toEqual(
dashboardGitResponse[1],
);
2020-05-24 23:13:21 +05:30
});
2020-10-24 23:57:45 +05:30
it('returns the overview dashboard when no dashboard is selected', () => {
const localState = {
2020-05-24 23:13:21 +05:30
allDashboards: dashboardGitResponse,
currentDashboard: null,
2020-07-28 23:09:34 +05:30
customDashboardBasePath,
2020-05-24 23:13:21 +05:30
};
2020-10-24 23:57:45 +05:30
expect(selectedDashboard(localState, localGetters(localState))).toEqual(
dashboardGitResponse[0],
);
2020-05-24 23:13:21 +05:30
});
2020-10-24 23:57:45 +05:30
it('returns the overview dashboard when dashboard cannot be found', () => {
const localState = {
2020-05-24 23:13:21 +05:30
allDashboards: dashboardGitResponse,
currentDashboard: 'wrong_path',
2020-07-28 23:09:34 +05:30
customDashboardBasePath,
2020-05-24 23:13:21 +05:30
};
2020-10-24 23:57:45 +05:30
expect(selectedDashboard(localState, localGetters(localState))).toEqual(
dashboardGitResponse[0],
);
2020-05-24 23:13:21 +05:30
});
it('returns null when no dashboards are present', () => {
2020-10-24 23:57:45 +05:30
const localState = {
2020-05-24 23:13:21 +05:30
allDashboards: [],
currentDashboard: dashboardGitResponse[0].path,
2020-07-28 23:09:34 +05:30
customDashboardBasePath,
2020-05-24 23:13:21 +05:30
};
2020-10-24 23:57:45 +05:30
expect(selectedDashboard(localState, localGetters(localState))).toEqual(null);
2020-05-24 23:13:21 +05:30
});
});
2020-06-23 00:09:42 +05:30
describe('linksWithMetadata', () => {
const setupState = (initState = {}) => {
state = {
...state,
...initState,
};
};
beforeAll(() => {
setupState({
links: mockLinks,
});
});
afterAll(() => {
state = null;
});
it.each`
timeRange | output
${{}} | ${''}
${{ start: '2020-01-01T00:00:00.000Z', end: '2020-01-31T23:59:00.000Z' }} | ${'start=2020-01-01T00%3A00%3A00.000Z&end=2020-01-31T23%3A59%3A00.000Z'}
${{ duration: { seconds: 86400 } }} | ${'duration_seconds=86400'}
`('linksWithMetadata returns URLs with time range', ({ timeRange, output }) => {
setupState({ timeRange });
const links = getters.linksWithMetadata(state);
links.forEach(({ url }) => {
expect(url).toMatch(output);
});
});
});
2020-01-01 13:55:28 +05:30
});