2020-03-11 11:09:37 -04:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
|
|
|
|
import testAction from 'helpers/vuex_action_helper';
|
|
|
|
import * as types from '~/logs/stores/mutation_types';
|
|
|
|
import { convertToFixedRange } from '~/lib/utils/datetime_range';
|
|
|
|
import logsPageState from '~/logs/stores/state';
|
|
|
|
import {
|
|
|
|
setInitData,
|
|
|
|
setSearch,
|
|
|
|
showPodLogs,
|
|
|
|
fetchEnvironments,
|
|
|
|
fetchLogs,
|
2020-03-13 08:09:22 -04:00
|
|
|
fetchMoreLogsPrepend,
|
2020-03-11 11:09:37 -04:00
|
|
|
} from '~/logs/stores/actions';
|
|
|
|
|
|
|
|
import { defaultTimeRange } from '~/monitoring/constants';
|
|
|
|
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import flash from '~/flash';
|
|
|
|
|
|
|
|
import {
|
|
|
|
mockPodName,
|
|
|
|
mockEnvironmentsEndpoint,
|
|
|
|
mockEnvironments,
|
|
|
|
mockPods,
|
|
|
|
mockLogsResult,
|
|
|
|
mockEnvName,
|
|
|
|
mockSearch,
|
2020-03-13 08:09:22 -04:00
|
|
|
mockLogsEndpoint,
|
|
|
|
mockResponse,
|
|
|
|
mockCursor,
|
|
|
|
mockNextCursor,
|
2020-03-11 11:09:37 -04:00
|
|
|
} from '../mock_data';
|
|
|
|
|
|
|
|
jest.mock('~/flash');
|
|
|
|
jest.mock('~/lib/utils/datetime_range');
|
|
|
|
jest.mock('~/logs/utils');
|
|
|
|
|
|
|
|
const mockDefaultRange = {
|
|
|
|
start: '2020-01-10T18:00:00.000Z',
|
|
|
|
end: '2020-01-10T10:00:00.000Z',
|
|
|
|
};
|
|
|
|
const mockFixedRange = {
|
|
|
|
start: '2020-01-09T18:06:20.000Z',
|
|
|
|
end: '2020-01-09T18:36:20.000Z',
|
|
|
|
};
|
|
|
|
const mockRollingRange = {
|
|
|
|
duration: 120,
|
|
|
|
};
|
|
|
|
const mockRollingRangeAsFixed = {
|
|
|
|
start: '2020-01-10T18:00:00.000Z',
|
|
|
|
end: '2020-01-10T17:58:00.000Z',
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('Logs Store actions', () => {
|
|
|
|
let state;
|
|
|
|
let mock;
|
|
|
|
|
2020-03-13 08:09:22 -04:00
|
|
|
const latestGetParams = () => mock.history.get[mock.history.get.length - 1].params;
|
|
|
|
|
2020-03-11 11:09:37 -04:00
|
|
|
convertToFixedRange.mockImplementation(range => {
|
|
|
|
if (range === defaultTimeRange) {
|
|
|
|
return { ...mockDefaultRange };
|
|
|
|
}
|
|
|
|
if (range === mockFixedRange) {
|
|
|
|
return { ...mockFixedRange };
|
|
|
|
}
|
|
|
|
if (range === mockRollingRange) {
|
|
|
|
return { ...mockRollingRangeAsFixed };
|
|
|
|
}
|
|
|
|
throw new Error('Invalid time range');
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
state = logsPageState();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
flash.mockClear();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('setInitData', () => {
|
|
|
|
it('should commit environment and pod name mutation', () =>
|
2020-03-13 08:09:22 -04:00
|
|
|
testAction(
|
|
|
|
setInitData,
|
|
|
|
{ timeRange: mockFixedRange, environmentName: mockEnvName, podName: mockPodName },
|
|
|
|
state,
|
|
|
|
[
|
|
|
|
{ type: types.SET_TIME_RANGE, payload: mockFixedRange },
|
|
|
|
{ type: types.SET_PROJECT_ENVIRONMENT, payload: mockEnvName },
|
|
|
|
{ type: types.SET_CURRENT_POD_NAME, payload: mockPodName },
|
|
|
|
],
|
|
|
|
));
|
2020-03-11 11:09:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('setSearch', () => {
|
|
|
|
it('should commit search mutation', () =>
|
|
|
|
testAction(
|
|
|
|
setSearch,
|
|
|
|
mockSearch,
|
|
|
|
state,
|
|
|
|
[{ type: types.SET_SEARCH, payload: mockSearch }],
|
|
|
|
[{ type: 'fetchLogs' }],
|
|
|
|
));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('showPodLogs', () => {
|
|
|
|
it('should commit pod name', () =>
|
|
|
|
testAction(
|
|
|
|
showPodLogs,
|
|
|
|
mockPodName,
|
|
|
|
state,
|
|
|
|
[{ type: types.SET_CURRENT_POD_NAME, payload: mockPodName }],
|
|
|
|
[{ type: 'fetchLogs' }],
|
|
|
|
));
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('fetchEnvironments', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should commit RECEIVE_ENVIRONMENTS_DATA_SUCCESS mutation on correct data', () => {
|
|
|
|
mock.onGet(mockEnvironmentsEndpoint).replyOnce(200, { environments: mockEnvironments });
|
|
|
|
return testAction(
|
|
|
|
fetchEnvironments,
|
|
|
|
mockEnvironmentsEndpoint,
|
|
|
|
state,
|
|
|
|
[
|
|
|
|
{ type: types.REQUEST_ENVIRONMENTS_DATA },
|
|
|
|
{ type: types.RECEIVE_ENVIRONMENTS_DATA_SUCCESS, payload: mockEnvironments },
|
|
|
|
],
|
|
|
|
[{ type: 'fetchLogs' }],
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should commit RECEIVE_ENVIRONMENTS_DATA_ERROR on wrong data', () => {
|
|
|
|
mock.onGet(mockEnvironmentsEndpoint).replyOnce(500);
|
|
|
|
return testAction(
|
|
|
|
fetchEnvironments,
|
|
|
|
mockEnvironmentsEndpoint,
|
|
|
|
state,
|
|
|
|
[
|
|
|
|
{ type: types.REQUEST_ENVIRONMENTS_DATA },
|
|
|
|
{ type: types.RECEIVE_ENVIRONMENTS_DATA_ERROR },
|
|
|
|
],
|
|
|
|
[],
|
|
|
|
() => {
|
|
|
|
expect(flash).toHaveBeenCalledTimes(1);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-03-13 08:09:22 -04:00
|
|
|
describe('when the backend responds succesfully', () => {
|
|
|
|
let expectedMutations;
|
|
|
|
let expectedActions;
|
|
|
|
|
2020-03-11 11:09:37 -04:00
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
2020-03-13 08:09:22 -04:00
|
|
|
mock.onGet(mockLogsEndpoint).reply(200, mockResponse);
|
|
|
|
mock.onGet(mockLogsEndpoint).replyOnce(202); // mock reactive cache
|
|
|
|
|
|
|
|
state.environments.options = mockEnvironments;
|
|
|
|
state.environments.current = mockEnvName;
|
2020-03-11 11:09:37 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.reset();
|
|
|
|
});
|
|
|
|
|
2020-03-13 08:09:22 -04:00
|
|
|
describe('fetchLogs', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
expectedMutations = [
|
2020-03-11 11:09:37 -04:00
|
|
|
{ type: types.REQUEST_PODS_DATA },
|
|
|
|
{ type: types.REQUEST_LOGS_DATA },
|
|
|
|
{ type: types.SET_CURRENT_POD_NAME, payload: mockPodName },
|
|
|
|
{ type: types.RECEIVE_PODS_DATA_SUCCESS, payload: mockPods },
|
2020-03-13 08:09:22 -04:00
|
|
|
{
|
|
|
|
type: types.RECEIVE_LOGS_DATA_SUCCESS,
|
|
|
|
payload: { logs: mockLogsResult, cursor: mockNextCursor },
|
|
|
|
},
|
|
|
|
];
|
2020-03-11 11:09:37 -04:00
|
|
|
|
2020-03-13 08:09:22 -04:00
|
|
|
expectedActions = [];
|
|
|
|
});
|
2020-03-11 11:09:37 -04:00
|
|
|
|
2020-03-13 08:09:22 -04:00
|
|
|
it('should commit logs and pod data when there is pod name defined', () => {
|
|
|
|
state.pods.current = mockPodName;
|
2020-03-11 11:09:37 -04:00
|
|
|
|
2020-03-13 08:09:22 -04:00
|
|
|
return testAction(fetchLogs, null, state, expectedMutations, expectedActions, () => {
|
|
|
|
expect(latestGetParams()).toMatchObject({
|
2020-03-11 11:09:37 -04:00
|
|
|
pod_name: mockPodName,
|
2020-03-13 08:09:22 -04:00
|
|
|
});
|
2020-03-11 11:09:37 -04:00
|
|
|
});
|
2020-03-13 08:09:22 -04:00
|
|
|
});
|
2020-03-11 11:09:37 -04:00
|
|
|
|
2020-03-13 08:09:22 -04:00
|
|
|
it('should commit logs and pod data when there is pod name defined and a non-default date range', () => {
|
|
|
|
state.pods.current = mockPodName;
|
|
|
|
state.timeRange.current = mockFixedRange;
|
|
|
|
state.logs.cursor = mockCursor;
|
2020-03-11 11:09:37 -04:00
|
|
|
|
2020-03-13 08:09:22 -04:00
|
|
|
return testAction(fetchLogs, null, state, expectedMutations, expectedActions, () => {
|
|
|
|
expect(latestGetParams()).toEqual({
|
2020-03-11 11:09:37 -04:00
|
|
|
pod_name: mockPodName,
|
2020-03-13 08:09:22 -04:00
|
|
|
start: mockFixedRange.start,
|
|
|
|
end: mockFixedRange.end,
|
|
|
|
cursor: mockCursor,
|
|
|
|
});
|
2020-03-11 11:09:37 -04:00
|
|
|
});
|
2020-03-13 08:09:22 -04:00
|
|
|
});
|
2020-03-11 11:09:37 -04:00
|
|
|
|
2020-03-13 08:09:22 -04:00
|
|
|
it('should commit logs and pod data when there is pod name and search and a faulty date range', () => {
|
|
|
|
state.pods.current = mockPodName;
|
|
|
|
state.search = mockSearch;
|
|
|
|
state.timeRange.current = 'INVALID_TIME_RANGE';
|
2020-03-11 11:09:37 -04:00
|
|
|
|
2020-03-13 08:09:22 -04:00
|
|
|
return testAction(fetchLogs, null, state, expectedMutations, expectedActions, () => {
|
|
|
|
expect(latestGetParams()).toEqual({
|
|
|
|
pod_name: mockPodName,
|
|
|
|
search: mockSearch,
|
|
|
|
});
|
2020-03-11 11:09:37 -04:00
|
|
|
// Warning about time ranges was issued
|
|
|
|
expect(flash).toHaveBeenCalledTimes(1);
|
|
|
|
expect(flash).toHaveBeenCalledWith(expect.any(String), 'warning');
|
2020-03-13 08:09:22 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should commit logs and pod data when no pod name defined', () => {
|
|
|
|
state.timeRange.current = mockDefaultRange;
|
|
|
|
|
|
|
|
return testAction(fetchLogs, null, state, expectedMutations, expectedActions, () => {
|
|
|
|
expect(latestGetParams()).toEqual({});
|
|
|
|
});
|
|
|
|
});
|
2020-03-11 11:09:37 -04:00
|
|
|
});
|
|
|
|
|
2020-03-13 08:09:22 -04:00
|
|
|
describe('fetchMoreLogsPrepend', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
expectedMutations = [
|
|
|
|
{ type: types.REQUEST_LOGS_DATA_PREPEND },
|
|
|
|
{
|
|
|
|
type: types.RECEIVE_LOGS_DATA_PREPEND_SUCCESS,
|
|
|
|
payload: { logs: mockLogsResult, cursor: mockNextCursor },
|
|
|
|
},
|
|
|
|
];
|
2020-03-11 11:09:37 -04:00
|
|
|
|
2020-03-13 08:09:22 -04:00
|
|
|
expectedActions = [];
|
|
|
|
});
|
2020-03-11 11:09:37 -04:00
|
|
|
|
2020-03-13 08:09:22 -04:00
|
|
|
it('should commit logs and pod data when there is pod name defined', () => {
|
|
|
|
state.pods.current = mockPodName;
|
|
|
|
|
|
|
|
expectedActions = [];
|
|
|
|
|
|
|
|
return testAction(
|
|
|
|
fetchMoreLogsPrepend,
|
|
|
|
null,
|
|
|
|
state,
|
|
|
|
expectedMutations,
|
|
|
|
expectedActions,
|
|
|
|
() => {
|
|
|
|
expect(latestGetParams()).toMatchObject({
|
|
|
|
pod_name: mockPodName,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
2020-03-11 11:09:37 -04:00
|
|
|
});
|
|
|
|
|
2020-03-13 08:09:22 -04:00
|
|
|
it('should commit logs and pod data when there is pod name defined and a non-default date range', () => {
|
|
|
|
state.pods.current = mockPodName;
|
|
|
|
state.timeRange.current = mockFixedRange;
|
|
|
|
state.logs.cursor = mockCursor;
|
|
|
|
|
|
|
|
return testAction(
|
|
|
|
fetchMoreLogsPrepend,
|
|
|
|
null,
|
|
|
|
state,
|
|
|
|
expectedMutations,
|
|
|
|
expectedActions,
|
|
|
|
() => {
|
|
|
|
expect(latestGetParams()).toEqual({
|
|
|
|
pod_name: mockPodName,
|
|
|
|
start: mockFixedRange.start,
|
|
|
|
end: mockFixedRange.end,
|
|
|
|
cursor: mockCursor,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should commit logs and pod data when there is pod name and search and a faulty date range', () => {
|
|
|
|
state.pods.current = mockPodName;
|
|
|
|
state.search = mockSearch;
|
|
|
|
state.timeRange.current = 'INVALID_TIME_RANGE';
|
|
|
|
|
|
|
|
return testAction(
|
|
|
|
fetchMoreLogsPrepend,
|
|
|
|
null,
|
|
|
|
state,
|
|
|
|
expectedMutations,
|
|
|
|
expectedActions,
|
|
|
|
() => {
|
|
|
|
expect(latestGetParams()).toEqual({
|
|
|
|
pod_name: mockPodName,
|
|
|
|
search: mockSearch,
|
|
|
|
});
|
|
|
|
// Warning about time ranges was issued
|
|
|
|
expect(flash).toHaveBeenCalledTimes(1);
|
|
|
|
expect(flash).toHaveBeenCalledWith(expect.any(String), 'warning');
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should commit logs and pod data when no pod name defined', () => {
|
|
|
|
state.timeRange.current = mockDefaultRange;
|
|
|
|
|
|
|
|
return testAction(
|
|
|
|
fetchMoreLogsPrepend,
|
|
|
|
null,
|
|
|
|
state,
|
|
|
|
expectedMutations,
|
|
|
|
expectedActions,
|
|
|
|
() => {
|
|
|
|
expect(latestGetParams()).toEqual({});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not commit logs or pod data when it has reached the end', () => {
|
|
|
|
state.logs.isComplete = true;
|
|
|
|
state.logs.cursor = null;
|
|
|
|
|
|
|
|
return testAction(
|
|
|
|
fetchMoreLogsPrepend,
|
|
|
|
null,
|
|
|
|
state,
|
|
|
|
[], // no mutations done
|
|
|
|
[], // no actions dispatched
|
|
|
|
() => {
|
|
|
|
expect(mock.history.get).toHaveLength(0);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when the backend responds with an error', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
mock.onGet(mockLogsEndpoint).reply(500);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fetchLogs should commit logs and pod errors', () => {
|
|
|
|
state.environments.options = mockEnvironments;
|
|
|
|
state.environments.current = mockEnvName;
|
|
|
|
|
|
|
|
return testAction(
|
2020-03-11 11:09:37 -04:00
|
|
|
fetchLogs,
|
|
|
|
null,
|
|
|
|
state,
|
|
|
|
[
|
|
|
|
{ type: types.REQUEST_PODS_DATA },
|
|
|
|
{ type: types.REQUEST_LOGS_DATA },
|
2020-03-13 08:09:22 -04:00
|
|
|
{ type: types.RECEIVE_PODS_DATA_ERROR },
|
|
|
|
{ type: types.RECEIVE_LOGS_DATA_ERROR },
|
2020-03-11 11:09:37 -04:00
|
|
|
],
|
|
|
|
[],
|
2020-03-13 08:09:22 -04:00
|
|
|
() => {
|
|
|
|
expect(mock.history.get[0].url).toBe(mockLogsEndpoint);
|
|
|
|
},
|
2020-03-11 11:09:37 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-03-13 08:09:22 -04:00
|
|
|
it('fetchMoreLogsPrepend should commit logs and pod errors', () => {
|
2020-03-11 11:09:37 -04:00
|
|
|
state.environments.options = mockEnvironments;
|
|
|
|
state.environments.current = mockEnvName;
|
|
|
|
|
|
|
|
return testAction(
|
2020-03-13 08:09:22 -04:00
|
|
|
fetchMoreLogsPrepend,
|
2020-03-11 11:09:37 -04:00
|
|
|
null,
|
|
|
|
state,
|
|
|
|
[
|
2020-03-13 08:09:22 -04:00
|
|
|
{ type: types.REQUEST_LOGS_DATA_PREPEND },
|
|
|
|
{ type: types.RECEIVE_LOGS_DATA_PREPEND_ERROR },
|
2020-03-11 11:09:37 -04:00
|
|
|
],
|
|
|
|
[],
|
|
|
|
() => {
|
2020-03-13 08:09:22 -04:00
|
|
|
expect(mock.history.get[0].url).toBe(mockLogsEndpoint);
|
2020-03-11 11:09:37 -04:00
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|