2019-11-04 07:06:19 -05:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2019-11-18 07:06:03 -05:00
|
|
|
import testAction from 'helpers/vuex_action_helper';
|
2019-12-09 07:07:58 -05:00
|
|
|
import Tracking from '~/tracking';
|
2019-11-18 07:06:03 -05:00
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2019-11-04 07:06:19 -05:00
|
|
|
import statusCodes from '~/lib/utils/http_status';
|
|
|
|
import { backOff } from '~/lib/utils/common_utils';
|
2019-11-26 16:06:38 -05:00
|
|
|
import createFlash from '~/flash';
|
2019-11-04 07:06:19 -05:00
|
|
|
|
2019-11-18 07:06:03 -05:00
|
|
|
import store from '~/monitoring/stores';
|
|
|
|
import * as types from '~/monitoring/stores/mutation_types';
|
|
|
|
import {
|
|
|
|
fetchDashboard,
|
|
|
|
receiveMetricsDashboardSuccess,
|
|
|
|
receiveMetricsDashboardFailure,
|
|
|
|
fetchDeploymentsData,
|
|
|
|
fetchEnvironmentsData,
|
|
|
|
fetchPrometheusMetrics,
|
|
|
|
fetchPrometheusMetric,
|
|
|
|
setEndpoints,
|
2020-01-29 04:08:49 -05:00
|
|
|
filterEnvironments,
|
2019-11-18 07:06:03 -05:00
|
|
|
setGettingStartedEmptyState,
|
2020-01-16 13:08:46 -05:00
|
|
|
duplicateSystemDashboard,
|
2019-11-18 07:06:03 -05:00
|
|
|
} from '~/monitoring/stores/actions';
|
2020-01-24 10:09:00 -05:00
|
|
|
import { gqClient, parseEnvironmentsResponse } from '~/monitoring/stores/utils';
|
2020-01-29 04:08:49 -05:00
|
|
|
import getEnvironments from '~/monitoring/queries/getEnvironments.query.graphql';
|
2019-11-18 07:06:03 -05:00
|
|
|
import storeState from '~/monitoring/stores/state';
|
|
|
|
import {
|
|
|
|
deploymentData,
|
|
|
|
environmentData,
|
|
|
|
metricsDashboardResponse,
|
2020-01-20 22:08:37 -05:00
|
|
|
metricsDashboardPayload,
|
2019-11-18 07:06:03 -05:00
|
|
|
dashboardGitResponse,
|
|
|
|
} from '../mock_data';
|
|
|
|
|
2019-11-04 07:06:19 -05:00
|
|
|
jest.mock('~/lib/utils/common_utils');
|
2019-11-26 16:06:38 -05:00
|
|
|
jest.mock('~/flash');
|
2019-11-04 07:06:19 -05:00
|
|
|
|
2019-11-18 07:06:03 -05:00
|
|
|
const resetStore = str => {
|
|
|
|
str.replaceState({
|
|
|
|
showEmptyState: true,
|
|
|
|
emptyState: 'loading',
|
|
|
|
groups: [],
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2019-11-26 16:06:38 -05:00
|
|
|
describe('Monitoring store actions', () => {
|
2019-11-04 07:06:19 -05:00
|
|
|
let mock;
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
|
2019-11-26 16:06:38 -05:00
|
|
|
// Mock `backOff` function to remove exponential algorithm delay.
|
|
|
|
jest.useFakeTimers();
|
2019-11-04 07:06:19 -05:00
|
|
|
|
2019-11-26 16:06:38 -05:00
|
|
|
backOff.mockImplementation(callback => {
|
|
|
|
const q = new Promise((resolve, reject) => {
|
|
|
|
const stop = arg => (arg instanceof Error ? reject(arg) : resolve(arg));
|
|
|
|
const next = () => callback(next, stop);
|
|
|
|
// Define a timeout based on a mock timer
|
|
|
|
setTimeout(() => {
|
|
|
|
callback(next, stop);
|
2019-11-04 07:06:19 -05:00
|
|
|
});
|
2019-11-26 16:06:38 -05:00
|
|
|
});
|
|
|
|
// Run all resolved promises in chain
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
return q;
|
2019-11-04 07:06:19 -05:00
|
|
|
});
|
|
|
|
});
|
2019-11-18 07:06:03 -05:00
|
|
|
afterEach(() => {
|
|
|
|
resetStore(store);
|
2019-11-26 16:06:38 -05:00
|
|
|
mock.reset();
|
|
|
|
|
|
|
|
backOff.mockReset();
|
|
|
|
createFlash.mockReset();
|
2019-11-18 07:06:03 -05:00
|
|
|
});
|
2019-11-26 16:06:38 -05:00
|
|
|
|
2019-11-18 07:06:03 -05:00
|
|
|
describe('fetchDeploymentsData', () => {
|
|
|
|
it('commits RECEIVE_DEPLOYMENTS_DATA_SUCCESS on error', done => {
|
|
|
|
const dispatch = jest.fn();
|
|
|
|
const { state } = store;
|
|
|
|
state.deploymentsEndpoint = '/success';
|
|
|
|
mock.onGet(state.deploymentsEndpoint).reply(200, {
|
|
|
|
deployments: deploymentData,
|
|
|
|
});
|
|
|
|
fetchDeploymentsData({
|
|
|
|
state,
|
|
|
|
dispatch,
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
expect(dispatch).toHaveBeenCalledWith('receiveDeploymentsDataSuccess', deploymentData);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
|
|
|
it('commits RECEIVE_DEPLOYMENTS_DATA_FAILURE on error', done => {
|
|
|
|
const dispatch = jest.fn();
|
|
|
|
const { state } = store;
|
|
|
|
state.deploymentsEndpoint = '/error';
|
|
|
|
mock.onGet(state.deploymentsEndpoint).reply(500);
|
|
|
|
fetchDeploymentsData({
|
|
|
|
state,
|
|
|
|
dispatch,
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
expect(dispatch).toHaveBeenCalledWith('receiveDeploymentsDataFailure');
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
|
|
|
});
|
2020-01-29 04:08:49 -05:00
|
|
|
|
2019-11-18 07:06:03 -05:00
|
|
|
describe('fetchEnvironmentsData', () => {
|
2020-01-29 04:08:49 -05:00
|
|
|
const dispatch = jest.fn();
|
|
|
|
const { state } = store;
|
|
|
|
state.projectPath = 'gitlab-org/gitlab-test';
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
resetStore(store);
|
|
|
|
jest.restoreAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('setting SET_ENVIRONMENTS_FILTER should dispatch fetchEnvironmentsData', () => {
|
|
|
|
jest.spyOn(gqClient, 'mutate').mockReturnValue(
|
|
|
|
Promise.resolve({
|
|
|
|
data: {
|
|
|
|
project: {
|
|
|
|
data: {
|
|
|
|
environments: [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
return testAction(
|
|
|
|
filterEnvironments,
|
|
|
|
{},
|
|
|
|
state,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'SET_ENVIRONMENTS_FILTER',
|
|
|
|
payload: {},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: 'fetchEnvironmentsData',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
);
|
|
|
|
});
|
2020-01-24 10:09:00 -05:00
|
|
|
|
2020-01-29 04:08:49 -05:00
|
|
|
it('fetch environments data call takes in search param', () => {
|
|
|
|
const mockMutate = jest.spyOn(gqClient, 'mutate');
|
|
|
|
const searchTerm = 'Something';
|
|
|
|
const mutationVariables = {
|
|
|
|
mutation: getEnvironments,
|
|
|
|
variables: {
|
|
|
|
projectPath: state.projectPath,
|
|
|
|
search: searchTerm,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
state.environmentsSearchTerm = searchTerm;
|
|
|
|
mockMutate.mockReturnValue(Promise.resolve());
|
|
|
|
|
|
|
|
return fetchEnvironmentsData({
|
|
|
|
state,
|
|
|
|
dispatch,
|
|
|
|
}).then(() => {
|
|
|
|
expect(mockMutate).toHaveBeenCalledWith(mutationVariables);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('commits RECEIVE_ENVIRONMENTS_DATA_SUCCESS on success', () => {
|
2020-01-24 10:09:00 -05:00
|
|
|
jest.spyOn(gqClient, 'mutate').mockReturnValue(
|
|
|
|
Promise.resolve({
|
|
|
|
data: {
|
|
|
|
project: {
|
|
|
|
data: {
|
|
|
|
environments: environmentData,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
return fetchEnvironmentsData({
|
2019-11-18 07:06:03 -05:00
|
|
|
state,
|
|
|
|
dispatch,
|
2020-01-24 10:09:00 -05:00
|
|
|
}).then(() => {
|
|
|
|
expect(dispatch).toHaveBeenCalledWith(
|
|
|
|
'receiveEnvironmentsDataSuccess',
|
|
|
|
parseEnvironmentsResponse(environmentData, state.projectPath),
|
|
|
|
);
|
|
|
|
});
|
2019-11-18 07:06:03 -05:00
|
|
|
});
|
2020-01-24 10:09:00 -05:00
|
|
|
|
|
|
|
it('commits RECEIVE_ENVIRONMENTS_DATA_FAILURE on error', () => {
|
|
|
|
jest.spyOn(gqClient, 'mutate').mockReturnValue(Promise.reject());
|
|
|
|
|
|
|
|
return fetchEnvironmentsData({
|
2019-11-18 07:06:03 -05:00
|
|
|
state,
|
|
|
|
dispatch,
|
2020-01-24 10:09:00 -05:00
|
|
|
}).then(() => {
|
|
|
|
expect(dispatch).toHaveBeenCalledWith('receiveEnvironmentsDataFailure');
|
|
|
|
});
|
2019-11-18 07:06:03 -05:00
|
|
|
});
|
|
|
|
});
|
2020-01-29 04:08:49 -05:00
|
|
|
|
2019-11-18 07:06:03 -05:00
|
|
|
describe('Set endpoints', () => {
|
|
|
|
let mockedState;
|
|
|
|
beforeEach(() => {
|
|
|
|
mockedState = storeState();
|
|
|
|
});
|
|
|
|
it('should commit SET_ENDPOINTS mutation', done => {
|
|
|
|
testAction(
|
|
|
|
setEndpoints,
|
|
|
|
{
|
|
|
|
metricsEndpoint: 'additional_metrics.json',
|
|
|
|
deploymentsEndpoint: 'deployments.json',
|
|
|
|
},
|
|
|
|
mockedState,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: types.SET_ENDPOINTS,
|
|
|
|
payload: {
|
|
|
|
metricsEndpoint: 'additional_metrics.json',
|
|
|
|
deploymentsEndpoint: 'deployments.json',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
done,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('Set empty states', () => {
|
|
|
|
let mockedState;
|
|
|
|
beforeEach(() => {
|
|
|
|
mockedState = storeState();
|
|
|
|
});
|
|
|
|
it('should commit SET_METRICS_ENDPOINT mutation', done => {
|
|
|
|
testAction(
|
|
|
|
setGettingStartedEmptyState,
|
|
|
|
null,
|
|
|
|
mockedState,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: types.SET_GETTING_STARTED_EMPTY_STATE,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
done,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('fetchDashboard', () => {
|
|
|
|
let dispatch;
|
|
|
|
let state;
|
|
|
|
const response = metricsDashboardResponse;
|
|
|
|
beforeEach(() => {
|
|
|
|
dispatch = jest.fn();
|
|
|
|
state = storeState();
|
|
|
|
state.dashboardEndpoint = '/dashboard';
|
|
|
|
});
|
2019-12-11 04:08:12 -05:00
|
|
|
it('on success, dispatches receive and success actions', done => {
|
2019-11-18 07:06:03 -05:00
|
|
|
const params = {};
|
2019-11-22 04:06:20 -05:00
|
|
|
document.body.dataset.page = 'projects:environments:metrics';
|
2019-11-18 07:06:03 -05:00
|
|
|
mock.onGet(state.dashboardEndpoint).reply(200, response);
|
|
|
|
fetchDashboard(
|
|
|
|
{
|
|
|
|
state,
|
|
|
|
dispatch,
|
|
|
|
},
|
|
|
|
params,
|
|
|
|
)
|
|
|
|
.then(() => {
|
|
|
|
expect(dispatch).toHaveBeenCalledWith('requestMetricsDashboard');
|
|
|
|
expect(dispatch).toHaveBeenCalledWith('receiveMetricsDashboardSuccess', {
|
|
|
|
response,
|
|
|
|
params,
|
|
|
|
});
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
2019-12-11 04:08:12 -05:00
|
|
|
|
|
|
|
describe('on failure', () => {
|
|
|
|
let result;
|
|
|
|
let errorResponse;
|
|
|
|
beforeEach(() => {
|
|
|
|
const params = {};
|
|
|
|
result = () => {
|
|
|
|
mock.onGet(state.dashboardEndpoint).replyOnce(500, errorResponse);
|
|
|
|
return fetchDashboard({ state, dispatch }, params);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
it('dispatches a failure action', done => {
|
|
|
|
errorResponse = {};
|
|
|
|
result()
|
|
|
|
.then(() => {
|
|
|
|
expect(dispatch).toHaveBeenCalledWith(
|
|
|
|
'receiveMetricsDashboardFailure',
|
|
|
|
new Error('Request failed with status code 500'),
|
|
|
|
);
|
|
|
|
expect(createFlash).toHaveBeenCalled();
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('dispatches a failure action when a message is returned', done => {
|
|
|
|
const message = 'Something went wrong with Prometheus!';
|
|
|
|
errorResponse = { message };
|
|
|
|
result()
|
|
|
|
.then(() => {
|
|
|
|
expect(dispatch).toHaveBeenCalledWith(
|
|
|
|
'receiveMetricsDashboardFailure',
|
|
|
|
new Error('Request failed with status code 500'),
|
|
|
|
);
|
|
|
|
expect(createFlash).toHaveBeenCalledWith(expect.stringContaining(message));
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('does not show a flash error when showErrorBanner is disabled', done => {
|
|
|
|
state.showErrorBanner = false;
|
|
|
|
|
|
|
|
result()
|
|
|
|
.then(() => {
|
|
|
|
expect(dispatch).toHaveBeenCalledWith(
|
|
|
|
'receiveMetricsDashboardFailure',
|
|
|
|
new Error('Request failed with status code 500'),
|
|
|
|
);
|
|
|
|
expect(createFlash).not.toHaveBeenCalled();
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
2019-11-18 07:06:03 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('receiveMetricsDashboardSuccess', () => {
|
|
|
|
let commit;
|
|
|
|
let dispatch;
|
|
|
|
let state;
|
|
|
|
beforeEach(() => {
|
|
|
|
commit = jest.fn();
|
|
|
|
dispatch = jest.fn();
|
|
|
|
state = storeState();
|
|
|
|
});
|
|
|
|
it('stores groups ', () => {
|
|
|
|
const params = {};
|
|
|
|
const response = metricsDashboardResponse;
|
|
|
|
receiveMetricsDashboardSuccess(
|
|
|
|
{
|
|
|
|
state,
|
|
|
|
commit,
|
|
|
|
dispatch,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
response,
|
|
|
|
params,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
expect(commit).toHaveBeenCalledWith(
|
|
|
|
types.RECEIVE_METRICS_DATA_SUCCESS,
|
2020-01-14 07:07:41 -05:00
|
|
|
metricsDashboardResponse.dashboard,
|
2019-11-18 07:06:03 -05:00
|
|
|
);
|
|
|
|
expect(dispatch).toHaveBeenCalledWith('fetchPrometheusMetrics', params);
|
|
|
|
});
|
|
|
|
it('sets the dashboards loaded from the repository', () => {
|
|
|
|
const params = {};
|
|
|
|
const response = metricsDashboardResponse;
|
|
|
|
response.all_dashboards = dashboardGitResponse;
|
|
|
|
receiveMetricsDashboardSuccess(
|
|
|
|
{
|
|
|
|
state,
|
|
|
|
commit,
|
|
|
|
dispatch,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
response,
|
|
|
|
params,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
expect(commit).toHaveBeenCalledWith(types.SET_ALL_DASHBOARDS, dashboardGitResponse);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('receiveMetricsDashboardFailure', () => {
|
|
|
|
let commit;
|
|
|
|
beforeEach(() => {
|
|
|
|
commit = jest.fn();
|
|
|
|
});
|
|
|
|
it('commits failure action', () => {
|
|
|
|
receiveMetricsDashboardFailure({
|
|
|
|
commit,
|
|
|
|
});
|
|
|
|
expect(commit).toHaveBeenCalledWith(types.RECEIVE_METRICS_DATA_FAILURE, undefined);
|
|
|
|
});
|
|
|
|
it('commits failure action with error', () => {
|
|
|
|
receiveMetricsDashboardFailure(
|
|
|
|
{
|
|
|
|
commit,
|
|
|
|
},
|
|
|
|
'uh-oh',
|
|
|
|
);
|
|
|
|
expect(commit).toHaveBeenCalledWith(types.RECEIVE_METRICS_DATA_FAILURE, 'uh-oh');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('fetchPrometheusMetrics', () => {
|
2019-12-11 04:08:12 -05:00
|
|
|
const params = {};
|
2019-11-18 07:06:03 -05:00
|
|
|
let commit;
|
|
|
|
let dispatch;
|
2019-12-11 04:08:12 -05:00
|
|
|
let state;
|
|
|
|
|
2019-11-18 07:06:03 -05:00
|
|
|
beforeEach(() => {
|
2019-12-11 04:08:12 -05:00
|
|
|
jest.spyOn(Tracking, 'event');
|
2019-11-18 07:06:03 -05:00
|
|
|
commit = jest.fn();
|
|
|
|
dispatch = jest.fn();
|
2019-12-11 04:08:12 -05:00
|
|
|
state = storeState();
|
2019-11-18 07:06:03 -05:00
|
|
|
});
|
2019-12-11 04:08:12 -05:00
|
|
|
|
2019-11-18 07:06:03 -05:00
|
|
|
it('commits empty state when state.groups is empty', done => {
|
2019-12-11 04:08:12 -05:00
|
|
|
const getters = {
|
|
|
|
metricsWithData: () => [],
|
|
|
|
};
|
|
|
|
fetchPrometheusMetrics({ state, commit, dispatch, getters }, params)
|
2019-11-18 07:06:03 -05:00
|
|
|
.then(() => {
|
2019-12-11 04:08:12 -05:00
|
|
|
expect(Tracking.event).toHaveBeenCalledWith(
|
|
|
|
document.body.dataset.page,
|
|
|
|
'dashboard_fetch',
|
|
|
|
{
|
|
|
|
label: 'custom_metrics_dashboard',
|
|
|
|
property: 'count',
|
|
|
|
value: 0,
|
|
|
|
},
|
|
|
|
);
|
2019-11-18 07:06:03 -05:00
|
|
|
expect(dispatch).not.toHaveBeenCalled();
|
2019-11-26 16:06:38 -05:00
|
|
|
expect(createFlash).not.toHaveBeenCalled();
|
2019-11-18 07:06:03 -05:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
|
|
|
it('dispatches fetchPrometheusMetric for each panel query', done => {
|
|
|
|
state.dashboard.panel_groups = metricsDashboardResponse.dashboard.panel_groups;
|
2019-12-11 04:08:12 -05:00
|
|
|
const [metric] = state.dashboard.panel_groups[0].panels[0].metrics;
|
|
|
|
const getters = {
|
|
|
|
metricsWithData: () => [metric.id],
|
|
|
|
};
|
|
|
|
|
|
|
|
fetchPrometheusMetrics({ state, commit, dispatch, getters }, params)
|
2019-11-26 16:06:38 -05:00
|
|
|
.then(() => {
|
|
|
|
expect(dispatch).toHaveBeenCalledWith('fetchPrometheusMetric', {
|
|
|
|
metric,
|
|
|
|
params,
|
|
|
|
});
|
|
|
|
|
2019-12-11 04:08:12 -05:00
|
|
|
expect(Tracking.event).toHaveBeenCalledWith(
|
|
|
|
document.body.dataset.page,
|
|
|
|
'dashboard_fetch',
|
|
|
|
{
|
|
|
|
label: 'custom_metrics_dashboard',
|
|
|
|
property: 'count',
|
|
|
|
value: 1,
|
|
|
|
},
|
|
|
|
);
|
2019-11-26 16:06:38 -05:00
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('dispatches fetchPrometheusMetric for each panel query, handles an error', done => {
|
|
|
|
state.dashboard.panel_groups = metricsDashboardResponse.dashboard.panel_groups;
|
|
|
|
const metric = state.dashboard.panel_groups[0].panels[0].metrics[0];
|
|
|
|
|
|
|
|
// Mock having one out of three metrics failing
|
|
|
|
dispatch.mockRejectedValueOnce(new Error('Error fetching this metric'));
|
|
|
|
dispatch.mockResolvedValue();
|
|
|
|
|
|
|
|
fetchPrometheusMetrics({ state, commit, dispatch }, params)
|
2019-11-18 07:06:03 -05:00
|
|
|
.then(() => {
|
|
|
|
expect(dispatch).toHaveBeenCalledTimes(3);
|
|
|
|
expect(dispatch).toHaveBeenCalledWith('fetchPrometheusMetric', {
|
|
|
|
metric,
|
|
|
|
params,
|
|
|
|
});
|
2019-11-26 16:06:38 -05:00
|
|
|
|
|
|
|
expect(createFlash).toHaveBeenCalledTimes(1);
|
|
|
|
|
2019-11-18 07:06:03 -05:00
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('fetchPrometheusMetric', () => {
|
2019-11-26 16:06:38 -05:00
|
|
|
const params = {
|
|
|
|
start: '2019-08-06T12:40:02.184Z',
|
|
|
|
end: '2019-08-06T20:40:02.184Z',
|
|
|
|
};
|
|
|
|
let metric;
|
|
|
|
let state;
|
|
|
|
let data;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
state = storeState();
|
|
|
|
[metric] = metricsDashboardResponse.dashboard.panel_groups[0].panels[0].metrics;
|
2020-01-20 22:08:37 -05:00
|
|
|
[data] = metricsDashboardPayload.panel_groups[0].panels[0].metrics;
|
2019-11-26 16:06:38 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('commits result', done => {
|
|
|
|
mock.onGet('http://test').reply(200, { data }); // One attempt
|
|
|
|
|
2019-12-13 07:07:41 -05:00
|
|
|
testAction(
|
|
|
|
fetchPrometheusMetric,
|
|
|
|
{ metric, params },
|
|
|
|
state,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: types.REQUEST_METRIC_RESULT,
|
|
|
|
payload: {
|
|
|
|
metricId: metric.metric_id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: types.RECEIVE_METRIC_RESULT_SUCCESS,
|
|
|
|
payload: {
|
|
|
|
metricId: metric.metric_id,
|
|
|
|
result: data.result,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
() => {
|
2019-11-26 16:06:38 -05:00
|
|
|
expect(mock.history.get).toHaveLength(1);
|
2019-11-18 07:06:03 -05:00
|
|
|
done();
|
2019-12-13 07:07:41 -05:00
|
|
|
},
|
|
|
|
).catch(done.fail);
|
2019-11-18 07:06:03 -05:00
|
|
|
});
|
2019-11-26 16:06:38 -05:00
|
|
|
|
|
|
|
it('commits result, when waiting for results', done => {
|
|
|
|
// Mock multiple attempts while the cache is filling up
|
|
|
|
mock.onGet('http://test').replyOnce(statusCodes.NO_CONTENT);
|
|
|
|
mock.onGet('http://test').replyOnce(statusCodes.NO_CONTENT);
|
|
|
|
mock.onGet('http://test').replyOnce(statusCodes.NO_CONTENT);
|
|
|
|
mock.onGet('http://test').reply(200, { data }); // 4th attempt
|
|
|
|
|
2019-12-13 07:07:41 -05:00
|
|
|
testAction(
|
|
|
|
fetchPrometheusMetric,
|
|
|
|
{ metric, params },
|
|
|
|
state,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: types.REQUEST_METRIC_RESULT,
|
|
|
|
payload: {
|
|
|
|
metricId: metric.metric_id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: types.RECEIVE_METRIC_RESULT_SUCCESS,
|
|
|
|
payload: {
|
|
|
|
metricId: metric.metric_id,
|
|
|
|
result: data.result,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
() => {
|
2019-11-26 16:06:38 -05:00
|
|
|
expect(mock.history.get).toHaveLength(4);
|
|
|
|
done();
|
2019-12-13 07:07:41 -05:00
|
|
|
},
|
|
|
|
).catch(done.fail);
|
2019-11-26 16:06:38 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('commits failure, when waiting for results and getting a server error', done => {
|
|
|
|
// Mock multiple attempts while the cache is filling up and fails
|
|
|
|
mock.onGet('http://test').replyOnce(statusCodes.NO_CONTENT);
|
|
|
|
mock.onGet('http://test').replyOnce(statusCodes.NO_CONTENT);
|
|
|
|
mock.onGet('http://test').replyOnce(statusCodes.NO_CONTENT);
|
|
|
|
mock.onGet('http://test').reply(500); // 4th attempt
|
|
|
|
|
2019-12-13 07:07:41 -05:00
|
|
|
const error = new Error('Request failed with status code 500');
|
|
|
|
|
|
|
|
testAction(
|
|
|
|
fetchPrometheusMetric,
|
|
|
|
{ metric, params },
|
|
|
|
state,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
type: types.REQUEST_METRIC_RESULT,
|
|
|
|
payload: {
|
|
|
|
metricId: metric.metric_id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-12-17 10:08:15 -05:00
|
|
|
type: types.RECEIVE_METRIC_RESULT_FAILURE,
|
2019-12-13 07:07:41 -05:00
|
|
|
payload: {
|
|
|
|
metricId: metric.metric_id,
|
|
|
|
error,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
).catch(e => {
|
|
|
|
expect(mock.history.get).toHaveLength(4);
|
|
|
|
expect(e).toEqual(error);
|
|
|
|
done();
|
|
|
|
});
|
2019-11-26 16:06:38 -05:00
|
|
|
});
|
2019-11-18 07:06:03 -05:00
|
|
|
});
|
2020-01-16 13:08:46 -05:00
|
|
|
|
|
|
|
describe('duplicateSystemDashboard', () => {
|
|
|
|
let state;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
state = storeState();
|
|
|
|
state.dashboardsEndpoint = '/dashboards.json';
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Succesful POST request resolves', done => {
|
|
|
|
mock.onPost(state.dashboardsEndpoint).reply(statusCodes.CREATED, {
|
|
|
|
dashboard: dashboardGitResponse[1],
|
|
|
|
});
|
|
|
|
|
|
|
|
testAction(duplicateSystemDashboard, {}, state, [], [])
|
|
|
|
.then(() => {
|
|
|
|
expect(mock.history.post).toHaveLength(1);
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Succesful POST request resolves to a dashboard', done => {
|
|
|
|
const mockCreatedDashboard = dashboardGitResponse[1];
|
|
|
|
|
|
|
|
const params = {
|
|
|
|
dashboard: 'my-dashboard',
|
|
|
|
fileName: 'file-name.yml',
|
|
|
|
branch: 'my-new-branch',
|
|
|
|
commitMessage: 'A new commit message',
|
|
|
|
};
|
|
|
|
|
|
|
|
const expectedPayload = JSON.stringify({
|
|
|
|
dashboard: 'my-dashboard',
|
|
|
|
file_name: 'file-name.yml',
|
|
|
|
branch: 'my-new-branch',
|
|
|
|
commit_message: 'A new commit message',
|
|
|
|
});
|
|
|
|
|
|
|
|
mock.onPost(state.dashboardsEndpoint).reply(statusCodes.CREATED, {
|
|
|
|
dashboard: mockCreatedDashboard,
|
|
|
|
});
|
|
|
|
|
|
|
|
testAction(duplicateSystemDashboard, params, state, [], [])
|
|
|
|
.then(result => {
|
|
|
|
expect(mock.history.post).toHaveLength(1);
|
|
|
|
expect(mock.history.post[0].data).toEqual(expectedPayload);
|
|
|
|
expect(result).toEqual(mockCreatedDashboard);
|
|
|
|
|
|
|
|
done();
|
|
|
|
})
|
|
|
|
.catch(done.fail);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Failed POST request throws an error', done => {
|
|
|
|
mock.onPost(state.dashboardsEndpoint).reply(statusCodes.BAD_REQUEST);
|
|
|
|
|
|
|
|
testAction(duplicateSystemDashboard, {}, state, [], []).catch(err => {
|
|
|
|
expect(mock.history.post).toHaveLength(1);
|
|
|
|
expect(err).toEqual(expect.any(String));
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Failed POST request throws an error with a description', done => {
|
|
|
|
const backendErrorMsg = 'This file already exists!';
|
|
|
|
|
|
|
|
mock.onPost(state.dashboardsEndpoint).reply(statusCodes.BAD_REQUEST, {
|
|
|
|
error: backendErrorMsg,
|
|
|
|
});
|
|
|
|
|
|
|
|
testAction(duplicateSystemDashboard, {}, state, [], []).catch(err => {
|
|
|
|
expect(mock.history.post).toHaveLength(1);
|
|
|
|
expect(err).toEqual(expect.any(String));
|
|
|
|
expect(err).toEqual(expect.stringContaining(backendErrorMsg));
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2019-11-18 07:06:03 -05:00
|
|
|
});
|