Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { getRepository } from '../../../test/fixtures';
import { getRepository, getPolicy } from '../../../test/fixtures';

export const REPOSITORY_NAME = 'my-test-repository';

export const REPOSITORY_EDIT = getRepository({ name: REPOSITORY_NAME });

export const POLICY_NAME = 'my-test-policy';

export const POLICY_EDIT = getPolicy({ name: POLICY_NAME });
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,47 @@ const registerHttpRequestMockHelpers = (server: SinonFakeServer) => {
);
};

const setLoadIndicesResponse = (response: HttpResponse = {}) => {
const defaultResponse = { indices: [] };

server.respondWith(
'GET',
`${API_BASE_PATH}policies/indices`,
response
? mockResponse(defaultResponse, response)
: [200, { 'Content-Type': 'application/json' }, '']
);
};

const setAddPolicyResponse = (response?: HttpResponse, error?: any) => {
const status = error ? error.status || 400 : 200;
const body = error ? JSON.stringify(error.body) : JSON.stringify(response);

server.respondWith('PUT', `${API_BASE_PATH}policies`, [
status,
{ 'Content-Type': 'application/json' },
body,
]);
};

const setGetPolicyResponse = (response?: HttpResponse) => {
server.respondWith('GET', `${API_BASE_PATH}policy/:name`, [
200,
{ 'Content-Type': 'application/json' },
JSON.stringify(response),
]);
};

return {
setLoadRepositoriesResponse,
setLoadRepositoryTypesResponse,
setGetRepositoryResponse,
setSaveRepositoryResponse,
setLoadSnapshotsResponse,
setGetSnapshotResponse,
setLoadIndicesResponse,
setAddPolicyResponse,
setGetPolicyResponse,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import { setup as homeSetup } from './home.helpers';
import { setup as repositoryAddSetup } from './repository_add.helpers';
import { setup as repositoryEditSetup } from './repository_edit.helpers';
import { setup as policyAddSetup } from './policy_add.helpers';
import { setup as policyEditSetup } from './policy_edit.helpers';

export { nextTick, getRandomString, findTestSubject, TestBed } from '../../../../../../test_utils';

Expand All @@ -16,4 +18,6 @@ export const pageHelpers = {
home: { setup: homeSetup },
repositoryAdd: { setup: repositoryAddSetup },
repositoryEdit: { setup: repositoryEditSetup },
policyAdd: { setup: policyAddSetup },
policyEdit: { setup: policyEditSetup },
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { registerTestBed, TestBedConfig } from '../../../../../../test_utils';
import { PolicyAdd } from '../../../public/app/sections/policy_add';
import { WithProviders } from './providers';
import { formSetup, PolicyFormTestSubjects } from './policy_form.helpers';

const testBedConfig: TestBedConfig = {
memoryRouter: {
initialEntries: ['/add_policy'],
componentRoutePath: '/add_policy',
},
doMountAsync: true,
};

const initTestBed = registerTestBed<PolicyFormTestSubjects>(
WithProviders(PolicyAdd),
testBedConfig
);

export const setup = formSetup.bind(null, initTestBed);
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { registerTestBed, TestBedConfig } from '../../../../../../test_utils';
import { PolicyEdit } from '../../../public/app/sections/policy_edit';
import { WithProviders } from './providers';
import { POLICY_NAME } from './constant';
import { formSetup, PolicyFormTestSubjects } from './policy_form.helpers';

const testBedConfig: TestBedConfig = {
memoryRouter: {
initialEntries: [`/edit_policy/${POLICY_NAME}`],
componentRoutePath: '/edit_policy/:name',
},
doMountAsync: true,
};

const initTestBed = registerTestBed<PolicyFormTestSubjects>(
WithProviders(PolicyEdit),
testBedConfig
);

export const setup = formSetup.bind(null, initTestBed);
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { TestBed, SetupFunc } from '../../../../../../test_utils';

export interface PolicyFormTestBed extends TestBed<PolicyFormTestSubjects> {
actions: {
clickNextButton: () => void;
clickSubmitButton: () => void;
};
}

export const formSetup = async (
initTestBed: SetupFunc<PolicyFormTestSubjects>
): Promise<PolicyFormTestBed> => {
const testBed = await initTestBed();

// User actions
const clickNextButton = () => {
testBed.find('nextButton').simulate('click');
};

const clickSubmitButton = () => {
testBed.find('submitButton').simulate('click');
};

return {
...testBed,
actions: {
clickNextButton,
clickSubmitButton,
},
};
};

export type PolicyFormTestSubjects =
| 'advancedCronInput'
| 'allIndicesToggle'
| 'backButton'
| 'deselectIndicesLink'
| 'expireAfterValueInput'
| 'expireAfterUnitSelect'
| 'ignoreUnavailableIndicesToggle'
| 'nameInput'
| 'maxCountInput'
| 'minCountInput'
| 'nextButton'
| 'pageTitle'
| 'savePolicyApiError'
| 'selectIndicesLink'
| 'showAdvancedCronLink'
| 'snapshotNameInput'
| 'submitButton';
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,15 @@ import { setAppDependencies } from '../../../public/app/index';

const { core, plugins } = createShim();
const appDependencies = {
core,
core: {
...core,
chrome: {
...core.chrome,
// mock getInjected() to return true
// this is used so the policy tab renders (slmUiEnabled config)
getInjected: () => true,
},
},
plugins,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,25 @@ import axiosXhrAdapter from 'axios/lib/adapters/xhr';

import { i18n } from '@kbn/i18n';

import { docTitle } from 'ui/doc_title/doc_title';
import { httpService } from '../../../public/app/services/http';
import { breadcrumbService } from '../../../public/app/services/navigation';
import { breadcrumbService, docTitleService } from '../../../public/app/services/navigation';
import { textService } from '../../../public/app/services/text';
import { chrome } from '../../../public/test/mocks';
import { init as initHttpRequests } from './http_requests';
import { uiMetricService } from '../../../public/app/services/ui_metric';
import { documentationLinksService } from '../../../public/app/services/documentation';
import { createUiStatsReporter } from '../../../../../../../src/legacy/core_plugins/ui_metric/public';

export const setupEnvironment = () => {
httpService.init(axios.create({ adapter: axiosXhrAdapter }), {
addBasePath: (path: string) => path,
});
breadcrumbService.init(chrome, {});
textService.init(i18n);
uiMetricService.init(createUiStatsReporter);
documentationLinksService.init('', '');
docTitleService.init(docTitle.change);

const { server, httpRequestsMockHelpers } = initHttpRequests();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from './helpers';
import { HomeTestBed } from './helpers/home.helpers';
import { REPOSITORY_NAME } from './helpers/constant';
import moment from 'moment-timezone';

const { setup } = pageHelpers.home;

Expand Down Expand Up @@ -51,7 +52,7 @@ describe.skip('<SnapshotRestoreHome />', () => {
test('should set the correct app title', () => {
const { exists, find } = testBed;
expect(exists('appTitle')).toBe(true);
expect(find('appTitle').text()).toEqual('Snapshot Repositories');
expect(find('appTitle').text()).toEqual('Snapshot and Restore');
});

test('should display a loading while fetching the repositories', () => {
Expand All @@ -63,7 +64,7 @@ describe.skip('<SnapshotRestoreHome />', () => {
test('should have a link to the documentation', () => {
const { exists, find } = testBed;
expect(exists('documentationLink')).toBe(true);
expect(find('documentationLink').text()).toBe('Snapshot docs');
expect(find('documentationLink').text()).toBe('Snapshot and Restore docs');
});

describe('tabs', () => {
Expand All @@ -77,21 +78,32 @@ describe.skip('<SnapshotRestoreHome />', () => {
});
});

test('should have 2 tabs', () => {
test('should have 4 tabs', () => {
const { find } = testBed;

expect(find('tab').length).toBe(2);
expect(find('tab').map(t => t.text())).toEqual(['Snapshots', 'Repositories']);
expect(find('tab').length).toBe(4);
expect(find('tab').map(t => t.text())).toEqual([
'Snapshots',
'Repositories',
'Policies',
'Restore Status',
]);
});

test('should navigate to snapshot list tab', () => {
test('should navigate to snapshot list tab', async () => {
const { exists, actions } = testBed;

expect(exists('repositoryList')).toBe(true);
expect(exists('snapshotList')).toBe(false);

actions.selectTab('snapshots');

// @ts-ignore (remove when react 16.9.0 is released)
await act(async () => {
await nextTick();
testBed.component.update();
});

expect(exists('repositoryList')).toBe(false);
expect(exists('snapshotList')).toBe(true);
});
Expand Down Expand Up @@ -264,6 +276,11 @@ describe.skip('<SnapshotRestoreHome />', () => {
expect(exists('repositoryDetail')).toBe(false);

await actions.clickRepositoryAt(0);
// @ts-ignore (remove when react 16.9.0 is released)
await act(async () => {
await nextTick();
testBed.component.update();
});

expect(exists('repositoryDetail')).toBe(true);
});
Expand Down Expand Up @@ -454,14 +471,19 @@ describe.skip('<SnapshotRestoreHome />', () => {
const { tableCellsValues } = table.getMetaData('snapshotTable');
tableCellsValues.forEach((row, i) => {
const snapshot = snapshots[i];
const startTime = moment(new Date(snapshot.startTimeInMillis));
const timezone = moment.tz.guess();

expect(row).toEqual([
'', // Checkbox
snapshot.snapshot, // Snapshot
REPOSITORY_NAME, // Repository
'foo', // TODO: fix this with FormattedDateTime value
`${Math.ceil(snapshot.durationInMillis / 1000).toString()}s`, // Duration
snapshot.indices.length.toString(), // Indices
snapshot.shards.total.toString(), // Shards
snapshot.shards.failed.toString(), // Failed shards
startTime.tz(timezone).format('MMMM D, YYYY h:mm A z'), // Start time
`${Math.ceil(snapshot.durationInMillis / 1000).toString()}s`, // Duration
'',
]);
});
});
Expand Down Expand Up @@ -590,22 +612,38 @@ describe.skip('<SnapshotRestoreHome />', () => {

describe('summary tab', () => {
test('should set the correct summary values', () => {
const {
version,
versionId,
uuid,
indices,
endTimeInMillis,
startTimeInMillis,
} = snapshot1;

const { find } = testBed;
const startTime = moment(new Date(startTimeInMillis));
const endTime = moment(new Date(endTimeInMillis));
const timezone = moment.tz.guess();

expect(find('snapshotDetail.version.value').text()).toBe(
`${snapshot1.version} / ${snapshot1.versionId}`
`${version} / ${versionId}`
);
expect(find('snapshotDetail.uuid.value').text()).toBe(snapshot1.uuid);
expect(find('snapshotDetail.uuid.value').text()).toBe(uuid);
expect(find('snapshotDetail.state.value').text()).toBe('Snapshot complete');
expect(find('snapshotDetail.includeGlobalState.value').text()).toBe('Yes');
expect(find('snapshotDetail.indices.title').text()).toBe(
`Indices (${snapshot1.indices.length})`
`Indices (${indices.length})`
);
expect(find('snapshotDetail.indices.value').text()).toContain(
indices.splice(0, 10).join('')
);
expect(find('snapshotDetail.startTime.value').text()).toBe(
startTime.tz(timezone).format('MMMM D, YYYY h:mm A z')
);
expect(find('snapshotDetail.indices.value').text()).toBe(
snapshot1.indices.join('')
expect(find('snapshotDetail.endTime.value').text()).toBe(
endTime.tz(timezone).format('MMMM D, YYYY h:mm A z')
);
expect(find('snapshotDetail.startTime.value').text()).toBe('foo'); // TODO: fix this with FormattedDateTime value
expect(find('snapshotDetail.endTime.value').text()).toBe('foo'); // TODO: fix this with FormattedDateTime value
});

test('should indicate the different snapshot states', async () => {
Expand Down Expand Up @@ -647,7 +685,7 @@ describe.skip('<SnapshotRestoreHome />', () => {
[SNAPSHOT_STATE.INCOMPATIBLE]: 'Incompatible version ',
};

// Call sequencially each state and verify that the message is ok
// Call sequentially each state and verify that the message is ok
return Object.entries(mapStateToMessage).reduce((promise, [state, message]) => {
return promise.then(async () => expectMessageForSnapshotState(state, message));
}, Promise.resolve());
Expand Down
Loading