2021-12-11 22:18:48 +05:30
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2023-06-20 00:43:36 +05:30
|
|
|
import { CoreV1Api } from '@gitlab/cluster-client';
|
2022-03-02 08:16:31 +05:30
|
|
|
import { s__ } from '~/locale';
|
2021-12-11 22:18:48 +05:30
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2023-04-23 21:23:45 +05:30
|
|
|
import { HTTP_STATUS_INTERNAL_SERVER_ERROR, HTTP_STATUS_OK } from '~/lib/utils/http_status';
|
2021-12-11 22:18:48 +05:30
|
|
|
import { resolvers } from '~/environments/graphql/resolvers';
|
2022-01-26 12:08:38 +05:30
|
|
|
import environmentToRollback from '~/environments/graphql/queries/environment_to_rollback.query.graphql';
|
|
|
|
import environmentToDelete from '~/environments/graphql/queries/environment_to_delete.query.graphql';
|
2022-03-02 08:16:31 +05:30
|
|
|
import environmentToStopQuery from '~/environments/graphql/queries/environment_to_stop.query.graphql';
|
2022-01-26 12:08:38 +05:30
|
|
|
import createMockApollo from 'helpers/mock_apollo_helper';
|
|
|
|
import pollIntervalQuery from '~/environments/graphql/queries/poll_interval.query.graphql';
|
2022-05-07 20:08:51 +05:30
|
|
|
import isEnvironmentStoppingQuery from '~/environments/graphql/queries/is_environment_stopping.query.graphql';
|
2022-01-26 12:08:38 +05:30
|
|
|
import pageInfoQuery from '~/environments/graphql/queries/page_info.query.graphql';
|
2021-12-11 22:18:48 +05:30
|
|
|
import { TEST_HOST } from 'helpers/test_constants';
|
2022-01-26 12:08:38 +05:30
|
|
|
import {
|
|
|
|
environmentsApp,
|
|
|
|
resolvedEnvironmentsApp,
|
|
|
|
resolvedEnvironment,
|
|
|
|
folder,
|
|
|
|
resolvedFolder,
|
2023-06-20 00:43:36 +05:30
|
|
|
k8sPodsMock,
|
2022-01-26 12:08:38 +05:30
|
|
|
} from './mock_data';
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
const ENDPOINT = `${TEST_HOST}/environments`;
|
|
|
|
|
|
|
|
describe('~/frontend/environments/graphql/resolvers', () => {
|
|
|
|
let mockResolvers;
|
|
|
|
let mock;
|
2022-01-26 12:08:38 +05:30
|
|
|
let mockApollo;
|
|
|
|
let localState;
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mockResolvers = resolvers(ENDPOINT);
|
|
|
|
mock = new MockAdapter(axios);
|
2022-01-26 12:08:38 +05:30
|
|
|
mockApollo = createMockApollo();
|
|
|
|
localState = mockApollo.defaultClient.localState;
|
2021-12-11 22:18:48 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('environmentApp', () => {
|
|
|
|
it('should fetch environments and map them to frontend data', async () => {
|
2022-01-26 12:08:38 +05:30
|
|
|
const cache = { writeQuery: jest.fn() };
|
|
|
|
const scope = 'available';
|
2022-11-25 23:54:43 +05:30
|
|
|
const search = '';
|
2022-01-26 12:08:38 +05:30
|
|
|
mock
|
2022-11-25 23:54:43 +05:30
|
|
|
.onGet(ENDPOINT, { params: { nested: true, scope, page: 1, search } })
|
2023-04-23 21:23:45 +05:30
|
|
|
.reply(HTTP_STATUS_OK, environmentsApp, {});
|
2021-12-11 22:18:48 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
const app = await mockResolvers.Query.environmentApp(
|
|
|
|
null,
|
|
|
|
{ scope, page: 1, search },
|
|
|
|
{ cache },
|
|
|
|
);
|
2021-12-11 22:18:48 +05:30
|
|
|
expect(app).toEqual(resolvedEnvironmentsApp);
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(cache.writeQuery).toHaveBeenCalledWith({
|
|
|
|
query: pollIntervalQuery,
|
|
|
|
data: { interval: undefined },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should set the poll interval when there is one', async () => {
|
|
|
|
const cache = { writeQuery: jest.fn() };
|
|
|
|
const scope = 'stopped';
|
|
|
|
const interval = 3000;
|
|
|
|
mock
|
2022-11-25 23:54:43 +05:30
|
|
|
.onGet(ENDPOINT, { params: { nested: true, scope, page: 1, search: '' } })
|
2023-04-23 21:23:45 +05:30
|
|
|
.reply(HTTP_STATUS_OK, environmentsApp, {
|
2022-01-26 12:08:38 +05:30
|
|
|
'poll-interval': interval,
|
|
|
|
});
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
await mockResolvers.Query.environmentApp(null, { scope, page: 1, search: '' }, { cache });
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(cache.writeQuery).toHaveBeenCalledWith({
|
|
|
|
query: pollIntervalQuery,
|
|
|
|
data: { interval },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should set page info if there is any', async () => {
|
|
|
|
const cache = { writeQuery: jest.fn() };
|
|
|
|
const scope = 'stopped';
|
|
|
|
mock
|
2022-11-25 23:54:43 +05:30
|
|
|
.onGet(ENDPOINT, { params: { nested: true, scope, page: 1, search: '' } })
|
2023-04-23 21:23:45 +05:30
|
|
|
.reply(HTTP_STATUS_OK, environmentsApp, {
|
2022-01-26 12:08:38 +05:30
|
|
|
'x-next-page': '2',
|
|
|
|
'x-page': '1',
|
|
|
|
'X-Per-Page': '2',
|
|
|
|
'X-Prev-Page': '',
|
|
|
|
'X-TOTAL': '37',
|
|
|
|
'X-Total-Pages': '5',
|
|
|
|
});
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
await mockResolvers.Query.environmentApp(null, { scope, page: 1, search: '' }, { cache });
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(cache.writeQuery).toHaveBeenCalledWith({
|
|
|
|
query: pageInfoQuery,
|
|
|
|
data: {
|
|
|
|
pageInfo: {
|
|
|
|
total: 37,
|
|
|
|
perPage: 2,
|
|
|
|
previousPage: NaN,
|
|
|
|
totalPages: 5,
|
|
|
|
nextPage: 2,
|
|
|
|
page: 1,
|
|
|
|
__typename: 'LocalPageInfo',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should not set page info if there is none', async () => {
|
|
|
|
const cache = { writeQuery: jest.fn() };
|
|
|
|
const scope = 'stopped';
|
|
|
|
mock
|
2022-11-25 23:54:43 +05:30
|
|
|
.onGet(ENDPOINT, { params: { nested: true, scope, page: 1, search: '' } })
|
2023-04-23 21:23:45 +05:30
|
|
|
.reply(HTTP_STATUS_OK, environmentsApp, {});
|
2022-01-26 12:08:38 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
await mockResolvers.Query.environmentApp(null, { scope, page: 1, search: '' }, { cache });
|
2022-01-26 12:08:38 +05:30
|
|
|
expect(cache.writeQuery).toHaveBeenCalledWith({
|
|
|
|
query: pageInfoQuery,
|
|
|
|
data: {
|
|
|
|
pageInfo: {
|
|
|
|
__typename: 'LocalPageInfo',
|
|
|
|
nextPage: NaN,
|
|
|
|
page: NaN,
|
|
|
|
perPage: NaN,
|
|
|
|
previousPage: NaN,
|
|
|
|
total: NaN,
|
|
|
|
totalPages: NaN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2021-12-11 22:18:48 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('folder', () => {
|
|
|
|
it('should fetch the folder url passed to it', async () => {
|
2022-11-25 23:54:43 +05:30
|
|
|
mock
|
|
|
|
.onGet(ENDPOINT, { params: { per_page: 3, scope: 'available', search: '' } })
|
2023-04-23 21:23:45 +05:30
|
|
|
.reply(HTTP_STATUS_OK, folder);
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
const environmentFolder = await mockResolvers.Query.folder(null, {
|
|
|
|
environment: { folderPath: ENDPOINT },
|
2022-05-07 20:08:51 +05:30
|
|
|
scope: 'available',
|
2022-11-25 23:54:43 +05:30
|
|
|
search: '',
|
2021-12-11 22:18:48 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
expect(environmentFolder).toEqual(resolvedFolder);
|
|
|
|
});
|
|
|
|
});
|
2023-06-20 00:43:36 +05:30
|
|
|
describe('k8sPods', () => {
|
|
|
|
const namespace = 'default';
|
|
|
|
const configuration = {
|
|
|
|
basePath: 'kas-proxy/',
|
|
|
|
baseOptions: {
|
|
|
|
headers: { 'GitLab-Agent-Id': '1' },
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockPodsListFn = jest.fn().mockImplementation(() => {
|
|
|
|
return Promise.resolve({
|
|
|
|
data: {
|
|
|
|
items: k8sPodsMock,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const mockNamespacedPodsListFn = jest.fn().mockImplementation(mockPodsListFn);
|
|
|
|
const mockAllPodsListFn = jest.fn().mockImplementation(mockPodsListFn);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
jest
|
|
|
|
.spyOn(CoreV1Api.prototype, 'listCoreV1NamespacedPod')
|
|
|
|
.mockImplementation(mockNamespacedPodsListFn);
|
|
|
|
jest
|
|
|
|
.spyOn(CoreV1Api.prototype, 'listCoreV1PodForAllNamespaces')
|
|
|
|
.mockImplementation(mockAllPodsListFn);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should request namespaced pods from the cluster_client library if namespace is specified', async () => {
|
|
|
|
const pods = await mockResolvers.Query.k8sPods(null, { configuration, namespace });
|
|
|
|
|
|
|
|
expect(mockNamespacedPodsListFn).toHaveBeenCalledWith(namespace);
|
|
|
|
expect(mockAllPodsListFn).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
expect(pods).toEqual(k8sPodsMock);
|
|
|
|
});
|
|
|
|
it('should request all pods from the cluster_client library if namespace is not specified', async () => {
|
|
|
|
const pods = await mockResolvers.Query.k8sPods(null, { configuration, namespace: '' });
|
|
|
|
|
|
|
|
expect(mockAllPodsListFn).toHaveBeenCalled();
|
|
|
|
expect(mockNamespacedPodsListFn).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
expect(pods).toEqual(k8sPodsMock);
|
|
|
|
});
|
|
|
|
it('should throw an error if the API call fails', async () => {
|
|
|
|
jest
|
|
|
|
.spyOn(CoreV1Api.prototype, 'listCoreV1PodForAllNamespaces')
|
|
|
|
.mockRejectedValue(new Error('API error'));
|
|
|
|
|
|
|
|
await expect(mockResolvers.Query.k8sPods(null, { configuration })).rejects.toThrow(
|
|
|
|
'API error',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2021-12-11 22:18:48 +05:30
|
|
|
describe('stopEnvironment', () => {
|
|
|
|
it('should post to the stop environment path', async () => {
|
2023-04-23 21:23:45 +05:30
|
|
|
mock.onPost(ENDPOINT).reply(HTTP_STATUS_OK);
|
2021-12-11 22:18:48 +05:30
|
|
|
|
2022-05-07 20:08:51 +05:30
|
|
|
const client = { writeQuery: jest.fn() };
|
|
|
|
const environment = { stopPath: ENDPOINT };
|
|
|
|
await mockResolvers.Mutation.stopEnvironment(null, { environment }, { client });
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
expect(mock.history.post).toContainEqual(
|
|
|
|
expect.objectContaining({ url: ENDPOINT, method: 'post' }),
|
|
|
|
);
|
2022-05-07 20:08:51 +05:30
|
|
|
|
|
|
|
expect(client.writeQuery).toHaveBeenCalledWith({
|
|
|
|
query: isEnvironmentStoppingQuery,
|
|
|
|
variables: { environment },
|
|
|
|
data: { isEnvironmentStopping: true },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should set is stopping to false if stop fails', async () => {
|
2023-04-23 21:23:45 +05:30
|
|
|
mock.onPost(ENDPOINT).reply(HTTP_STATUS_INTERNAL_SERVER_ERROR);
|
2022-05-07 20:08:51 +05:30
|
|
|
|
|
|
|
const client = { writeQuery: jest.fn() };
|
|
|
|
const environment = { stopPath: ENDPOINT };
|
|
|
|
await mockResolvers.Mutation.stopEnvironment(null, { environment }, { client });
|
|
|
|
|
|
|
|
expect(mock.history.post).toContainEqual(
|
|
|
|
expect.objectContaining({ url: ENDPOINT, method: 'post' }),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(client.writeQuery).toHaveBeenCalledWith({
|
|
|
|
query: isEnvironmentStoppingQuery,
|
|
|
|
variables: { environment },
|
|
|
|
data: { isEnvironmentStopping: false },
|
|
|
|
});
|
2021-12-11 22:18:48 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('rollbackEnvironment', () => {
|
|
|
|
it('should post to the retry environment path', async () => {
|
2023-04-23 21:23:45 +05:30
|
|
|
mock.onPost(ENDPOINT).reply(HTTP_STATUS_OK);
|
2021-12-11 22:18:48 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
await mockResolvers.Mutation.rollbackEnvironment(null, {
|
2021-12-11 22:18:48 +05:30
|
|
|
environment: { retryUrl: ENDPOINT },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(mock.history.post).toContainEqual(
|
|
|
|
expect.objectContaining({ url: ENDPOINT, method: 'post' }),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('deleteEnvironment', () => {
|
|
|
|
it('should DELETE to the delete environment path', async () => {
|
2023-04-23 21:23:45 +05:30
|
|
|
mock.onDelete(ENDPOINT).reply(HTTP_STATUS_OK);
|
2021-12-11 22:18:48 +05:30
|
|
|
|
2022-01-26 12:08:38 +05:30
|
|
|
await mockResolvers.Mutation.deleteEnvironment(null, {
|
2021-12-11 22:18:48 +05:30
|
|
|
environment: { deletePath: ENDPOINT },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(mock.history.delete).toContainEqual(
|
|
|
|
expect.objectContaining({ url: ENDPOINT, method: 'delete' }),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('cancelAutoStop', () => {
|
|
|
|
it('should post to the auto stop path', async () => {
|
2023-04-23 21:23:45 +05:30
|
|
|
mock.onPost(ENDPOINT).reply(HTTP_STATUS_OK);
|
2021-12-11 22:18:48 +05:30
|
|
|
|
2022-04-04 11:22:00 +05:30
|
|
|
await mockResolvers.Mutation.cancelAutoStop(null, { autoStopUrl: ENDPOINT });
|
2021-12-11 22:18:48 +05:30
|
|
|
|
|
|
|
expect(mock.history.post).toContainEqual(
|
|
|
|
expect.objectContaining({ url: ENDPOINT, method: 'post' }),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2022-01-26 12:08:38 +05:30
|
|
|
describe('setEnvironmentToRollback', () => {
|
|
|
|
it('should write the given environment to the cache', () => {
|
|
|
|
localState.client.writeQuery = jest.fn();
|
|
|
|
mockResolvers.Mutation.setEnvironmentToRollback(
|
|
|
|
null,
|
|
|
|
{ environment: resolvedEnvironment },
|
|
|
|
localState,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(localState.client.writeQuery).toHaveBeenCalledWith({
|
|
|
|
query: environmentToRollback,
|
|
|
|
data: { environmentToRollback: resolvedEnvironment },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('setEnvironmentToDelete', () => {
|
|
|
|
it('should write the given environment to the cache', () => {
|
|
|
|
localState.client.writeQuery = jest.fn();
|
|
|
|
mockResolvers.Mutation.setEnvironmentToDelete(
|
|
|
|
null,
|
|
|
|
{ environment: resolvedEnvironment },
|
|
|
|
localState,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(localState.client.writeQuery).toHaveBeenCalledWith({
|
|
|
|
query: environmentToDelete,
|
|
|
|
data: { environmentToDelete: resolvedEnvironment },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-03-02 08:16:31 +05:30
|
|
|
describe('setEnvironmentToStop', () => {
|
|
|
|
it('should write the given environment to the cache', () => {
|
|
|
|
localState.client.writeQuery = jest.fn();
|
|
|
|
mockResolvers.Mutation.setEnvironmentToStop(
|
|
|
|
null,
|
|
|
|
{ environment: resolvedEnvironment },
|
|
|
|
localState,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(localState.client.writeQuery).toHaveBeenCalledWith({
|
|
|
|
query: environmentToStopQuery,
|
|
|
|
data: { environmentToStop: resolvedEnvironment },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe('action', () => {
|
|
|
|
it('should POST to the given path', async () => {
|
2023-04-23 21:23:45 +05:30
|
|
|
mock.onPost(ENDPOINT).reply(HTTP_STATUS_OK);
|
2022-03-02 08:16:31 +05:30
|
|
|
const errors = await mockResolvers.Mutation.action(null, { action: { playPath: ENDPOINT } });
|
|
|
|
|
|
|
|
expect(errors).toEqual({ __typename: 'LocalEnvironmentErrors', errors: [] });
|
|
|
|
});
|
|
|
|
it('should return a nice error message on fail', async () => {
|
2023-04-23 21:23:45 +05:30
|
|
|
mock.onPost(ENDPOINT).reply(HTTP_STATUS_INTERNAL_SERVER_ERROR);
|
2022-03-02 08:16:31 +05:30
|
|
|
const errors = await mockResolvers.Mutation.action(null, { action: { playPath: ENDPOINT } });
|
|
|
|
|
|
|
|
expect(errors).toEqual({
|
|
|
|
__typename: 'LocalEnvironmentErrors',
|
|
|
|
errors: [s__('Environments|An error occurred while making the request.')],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-12-11 22:18:48 +05:30
|
|
|
});
|