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';
|
2023-03-17 16:20:25 +05:30
|
|
|
import {
|
|
|
|
HTTP_STATUS_BAD_REQUEST,
|
2023-04-23 21:23:45 +05:30
|
|
|
HTTP_STATUS_INTERNAL_SERVER_ERROR,
|
2023-03-04 22:38:38 +05:30
|
|
|
HTTP_STATUS_NO_CONTENT,
|
2023-03-17 16:20:25 +05:30
|
|
|
HTTP_STATUS_OK,
|
|
|
|
HTTP_STATUS_SERVICE_UNAVAILABLE,
|
|
|
|
HTTP_STATUS_UNAUTHORIZED,
|
2023-03-04 22:38:38 +05:30
|
|
|
HTTP_STATUS_UNPROCESSABLE_ENTITY,
|
|
|
|
} 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', () => {
|
2023-03-17 16:20:25 +05:30
|
|
|
mock.onGet(dashboardEndpoint).reply(HTTP_STATUS_OK, response);
|
2020-10-24 23:57:45 +05:30
|
|
|
|
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', () => {
|
2023-03-04 22:38:38 +05:30
|
|
|
mock.onGet(dashboardEndpoint).replyOnce(HTTP_STATUS_NO_CONTENT);
|
|
|
|
mock.onGet(dashboardEndpoint).replyOnce(HTTP_STATUS_NO_CONTENT);
|
2023-03-17 16:20:25 +05:30
|
|
|
mock.onGet(dashboardEndpoint).reply(HTTP_STATUS_OK, response);
|
2020-10-24 23:57:45 +05:30
|
|
|
|
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', () => {
|
2023-04-23 21:23:45 +05:30
|
|
|
mock.onGet(dashboardEndpoint).reply(HTTP_STATUS_INTERNAL_SERVER_ERROR);
|
2020-10-24 23:57:45 +05:30
|
|
|
|
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', () => {
|
2023-03-17 16:20:25 +05:30
|
|
|
mock.onGet(prometheusEndpoint).reply(HTTP_STATUS_OK, response);
|
2020-10-24 23:57:45 +05:30
|
|
|
|
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
|
2023-03-04 22:38:38 +05:30
|
|
|
mock.onGet(prometheusEndpoint).replyOnce(HTTP_STATUS_NO_CONTENT);
|
|
|
|
mock.onGet(prometheusEndpoint).replyOnce(HTTP_STATUS_NO_CONTENT);
|
2023-03-17 16:20:25 +05:30
|
|
|
mock.onGet(prometheusEndpoint).reply(HTTP_STATUS_OK, response); // 3rd attempt
|
2020-10-24 23:57:45 +05:30
|
|
|
|
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', () => {
|
2023-04-23 21:23:45 +05:30
|
|
|
mock.onGet(prometheusEndpoint).reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, {
|
2020-10-24 23:57:45 +05:30
|
|
|
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
|
2023-03-17 16:20:25 +05:30
|
|
|
mock.onGet(prometheusEndpoint).reply(HTTP_STATUS_UNAUTHORIZED, {
|
2020-10-24 23:57:45 +05:30
|
|
|
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
|
2023-03-04 22:38:38 +05:30
|
|
|
mock.onGet(prometheusEndpoint).replyOnce(HTTP_STATUS_NO_CONTENT);
|
|
|
|
mock.onGet(prometheusEndpoint).replyOnce(HTTP_STATUS_NO_CONTENT);
|
2023-04-23 21:23:45 +05:30
|
|
|
mock.onGet(prometheusEndpoint).reply(HTTP_STATUS_INTERNAL_SERVER_ERROR, {
|
2020-10-24 23:57:45 +05:30
|
|
|
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
|
2023-03-17 16:20:25 +05:30
|
|
|
${HTTP_STATUS_BAD_REQUEST} | ${'Parameters are missing or incorrect'}
|
2023-03-04 22:38:38 +05:30
|
|
|
${HTTP_STATUS_UNPROCESSABLE_ENTITY} | ${"Expression can't be executed"}
|
2023-03-17 16:20:25 +05:30
|
|
|
${HTTP_STATUS_SERVICE_UNAVAILABLE} | ${'Query timed out or aborted'}
|
2020-10-24 23:57:45 +05:30
|
|
|
`('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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|