debian-mirror-gitlab/spec/frontend/monitoring/requests/index_spec.js

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

150 lines
5.2 KiB
JavaScript
Raw Normal View History

2020-10-24 23:57:45 +05:30
import MockAdapter from 'axios-mock-adapter';
2021-03-08 18:12:59 +05:30
import { backoffMockImplementation } from 'helpers/backoff_helper';
2020-10-24 23:57:45 +05:30
import axios from '~/lib/utils/axios_utils';
import * as commonUtils from '~/lib/utils/common_utils';
2021-03-11 19:13:27 +05:30
import statusCodes from '~/lib/utils/http_status';
2020-10-24 23:57:45 +05:30
import { getDashboard, getPrometheusQueryData } from '~/monitoring/requests';
2021-03-11 19:13:27 +05:30
import { metricsDashboardResponse } from '../fixture_data';
2020-10-24 23:57:45 +05:30
describe('monitoring metrics_requests', () => {
let mock;
beforeEach(() => {
mock = new MockAdapter(axios);
jest.spyOn(commonUtils, 'backOff').mockImplementation(backoffMockImplementation);
});
afterEach(() => {
mock.reset();
commonUtils.backOff.mockReset();
});
describe('getDashboard', () => {
const response = metricsDashboardResponse;
const dashboardEndpoint = '/dashboard';
const params = {
start_time: 'start_time',
end_time: 'end_time',
};
it('returns a dashboard response', () => {
mock.onGet(dashboardEndpoint).reply(statusCodes.OK, response);
2021-03-08 18:12:59 +05:30
return getDashboard(dashboardEndpoint, params).then((data) => {
2020-10-24 23:57:45 +05:30
expect(data).toEqual(metricsDashboardResponse);
});
});
it('returns a dashboard response after retrying twice', () => {
mock.onGet(dashboardEndpoint).replyOnce(statusCodes.NO_CONTENT);
mock.onGet(dashboardEndpoint).replyOnce(statusCodes.NO_CONTENT);
mock.onGet(dashboardEndpoint).reply(statusCodes.OK, response);
2021-03-08 18:12:59 +05:30
return getDashboard(dashboardEndpoint, params).then((data) => {
2020-10-24 23:57:45 +05:30
expect(data).toEqual(metricsDashboardResponse);
expect(mock.history.get).toHaveLength(3);
});
});
it('rejects after getting an error', () => {
mock.onGet(dashboardEndpoint).reply(500);
2021-03-08 18:12:59 +05:30
return getDashboard(dashboardEndpoint, params).catch((error) => {
2020-10-24 23:57:45 +05:30
expect(error).toEqual(expect.any(Error));
expect(mock.history.get).toHaveLength(1);
});
});
});
describe('getPrometheusQueryData', () => {
const response = {
status: 'success',
data: {
resultType: 'matrix',
result: [],
},
};
const prometheusEndpoint = '/query_range';
const params = {
start_time: 'start_time',
end_time: 'end_time',
};
it('returns a dashboard response', () => {
mock.onGet(prometheusEndpoint).reply(statusCodes.OK, response);
2021-03-08 18:12:59 +05:30
return getPrometheusQueryData(prometheusEndpoint, params).then((data) => {
2020-10-24 23:57:45 +05:30
expect(data).toEqual(response.data);
});
});
it('returns a dashboard response after retrying twice', () => {
// Mock multiple attempts while the cache is filling up
mock.onGet(prometheusEndpoint).replyOnce(statusCodes.NO_CONTENT);
mock.onGet(prometheusEndpoint).replyOnce(statusCodes.NO_CONTENT);
mock.onGet(prometheusEndpoint).reply(statusCodes.OK, response); // 3rd attempt
2021-03-08 18:12:59 +05:30
return getPrometheusQueryData(prometheusEndpoint, params).then((data) => {
2020-10-24 23:57:45 +05:30
expect(data).toEqual(response.data);
expect(mock.history.get).toHaveLength(3);
});
});
it('rejects after getting an HTTP 500 error', () => {
mock.onGet(prometheusEndpoint).reply(500, {
status: 'error',
2021-04-17 20:07:23 +05:30
error: 'An error occurred',
2020-10-24 23:57:45 +05:30
});
2021-03-08 18:12:59 +05:30
return getPrometheusQueryData(prometheusEndpoint, params).catch((error) => {
2020-10-24 23:57:45 +05:30
expect(error).toEqual(new Error('Request failed with status code 500'));
});
});
it('rejects after retrying twice and getting an HTTP 401 error', () => {
// Mock multiple attempts while the cache is filling up and fails
mock.onGet(prometheusEndpoint).reply(statusCodes.UNAUTHORIZED, {
status: 'error',
2021-04-17 20:07:23 +05:30
error: 'An error occurred',
2020-10-24 23:57:45 +05:30
});
2021-03-08 18:12:59 +05:30
return getPrometheusQueryData(prometheusEndpoint, params).catch((error) => {
2020-10-24 23:57:45 +05:30
expect(error).toEqual(new Error('Request failed with status code 401'));
});
});
it('rejects after retrying twice and getting an HTTP 500 error', () => {
// Mock multiple attempts while the cache is filling up and fails
mock.onGet(prometheusEndpoint).replyOnce(statusCodes.NO_CONTENT);
mock.onGet(prometheusEndpoint).replyOnce(statusCodes.NO_CONTENT);
mock.onGet(prometheusEndpoint).reply(500, {
status: 'error',
2021-04-17 20:07:23 +05:30
error: 'An error occurred',
2020-10-24 23:57:45 +05:30
}); // 3rd attempt
2021-03-08 18:12:59 +05:30
return getPrometheusQueryData(prometheusEndpoint, params).catch((error) => {
2020-10-24 23:57:45 +05:30
expect(error).toEqual(new Error('Request failed with status code 500'));
expect(mock.history.get).toHaveLength(3);
});
});
2022-11-25 23:54:43 +05:30
it.each`
2020-10-24 23:57:45 +05:30
code | reason
${statusCodes.BAD_REQUEST} | ${'Parameters are missing or incorrect'}
${statusCodes.UNPROCESSABLE_ENTITY} | ${"Expression can't be executed"}
${statusCodes.SERVICE_UNAVAILABLE} | ${'Query timed out or aborted'}
`('rejects with details: "$reason" after getting an HTTP $code error', ({ code, reason }) => {
mock.onGet(prometheusEndpoint).reply(code, {
status: 'error',
error: reason,
});
2021-03-08 18:12:59 +05:30
return getPrometheusQueryData(prometheusEndpoint, params).catch((error) => {
2020-10-24 23:57:45 +05:30
expect(error).toEqual(new Error(reason));
expect(mock.history.get).toHaveLength(1);
});
});
});
});