Skip to content

Commit 0c4792e

Browse files
add api integration tests
1 parent e9b2e30 commit 0c4792e

6 files changed

Lines changed: 275 additions & 1 deletion

File tree

x-pack/test/api_integration/apis/management/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ export default function ({ loadTestFile }) {
1313
loadTestFile(require.resolve('./index_management'));
1414
loadTestFile(require.resolve('./index_lifecycle_management'));
1515
loadTestFile(require.resolve('./ingest_pipelines'));
16+
loadTestFile(require.resolve('./snapshot_restore'));
1617
});
1718
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
import { FtrProviderContext } from '../../../ftr_provider_context';
7+
8+
export default function ({ loadTestFile }: FtrProviderContext) {
9+
describe('Snapshot and Restore', () => {
10+
loadTestFile(require.resolve('./snapshot_restore'));
11+
});
12+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
import { FtrProviderContext } from '../../../../ftr_provider_context';
7+
8+
interface SlmPolicy {
9+
name: string;
10+
snapshotName: string;
11+
schedule: string;
12+
repository: string;
13+
isManagedPolicy: boolean;
14+
config?: {
15+
indices?: string | string[];
16+
ignoreUnavailable?: boolean;
17+
includeGlobalState?: boolean;
18+
partial?: boolean;
19+
metadata?: Record<string, string>;
20+
};
21+
retention?: {
22+
expireAfterValue?: number | '';
23+
expireAfterUnit?: string;
24+
maxCount?: number | '';
25+
minCount?: number | '';
26+
};
27+
}
28+
29+
/**
30+
* Helpers to create and delete SLM policies on the Elasticsearch instance
31+
* during our tests.
32+
* @param {ElasticsearchClient} es The Elasticsearch client instance
33+
*/
34+
export const registerEsHelpers = (getService: FtrProviderContext['getService']) => {
35+
let policiesCreated: string[] = [];
36+
37+
const es = getService('legacyEs');
38+
39+
const createRepository = (repoName: string) => {
40+
return es.snapshot.createRepository({
41+
repository: repoName,
42+
body: {
43+
type: 'fs',
44+
settings: {
45+
location: '/tmp/',
46+
},
47+
},
48+
verify: false,
49+
});
50+
};
51+
52+
const createPolicy = (policy: SlmPolicy, cachePolicy?: boolean) => {
53+
if (cachePolicy) {
54+
policiesCreated.push(policy.name);
55+
}
56+
57+
return es.sr.updatePolicy({
58+
name: policy.name,
59+
body: policy,
60+
});
61+
};
62+
63+
const deletePolicy = (policyName: string) => es.sr.deletePolicy({ name: policyName });
64+
65+
const cleanupPolicies = () =>
66+
Promise.all(policiesCreated.map(deletePolicy))
67+
.then(() => {
68+
policiesCreated = [];
69+
})
70+
.catch((err) => {
71+
// eslint-disable-next-line no-console
72+
console.log(`[Cleanup error] Error deleting ES resources: ${err.message}`);
73+
});
74+
75+
return {
76+
createRepository,
77+
createPolicy,
78+
deletePolicy,
79+
cleanupPolicies,
80+
};
81+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
export { registerEsHelpers } from './elasticsearch';
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
7+
import expect from '@kbn/expect';
8+
9+
import { FtrProviderContext } from '../../../ftr_provider_context';
10+
import { registerEsHelpers } from './lib';
11+
12+
const API_BASE_PATH = '/api/snapshot_restore';
13+
const REPO_NAME = 'test_repo';
14+
15+
export default function ({ getService }: FtrProviderContext) {
16+
const supertest = getService('supertest');
17+
18+
const { createRepository, createPolicy, deletePolicy, cleanupPolicies } = registerEsHelpers(
19+
getService
20+
);
21+
22+
describe('Snapshot Lifecycle Management', function () {
23+
before(async () => {
24+
try {
25+
await createRepository(REPO_NAME);
26+
} catch (err) {
27+
// eslint-disable-next-line no-console
28+
console.log('[Setup error] Error creating repository');
29+
throw err;
30+
}
31+
});
32+
33+
after(async () => {
34+
await cleanupPolicies();
35+
});
36+
37+
describe('Create', () => {
38+
const POLICY_NAME = 'test_create_policy';
39+
const REQUIRED_FIELDS_POLICY_NAME = 'test_create_required_fields_policy';
40+
41+
after(async () => {
42+
// Clean up any policies created in test cases
43+
await Promise.all([POLICY_NAME, REQUIRED_FIELDS_POLICY_NAME].map(deletePolicy)).catch(
44+
(err) => {
45+
// eslint-disable-next-line no-console
46+
console.log(`[Cleanup error] Error deleting policies: ${err.message}`);
47+
throw err;
48+
}
49+
);
50+
});
51+
52+
it('should create a SLM policy', async () => {
53+
const { body } = await supertest
54+
.post(`${API_BASE_PATH}/policies`)
55+
.set('kbn-xsrf', 'xxx')
56+
.send({
57+
name: POLICY_NAME,
58+
snapshotName: 'my_snapshot',
59+
schedule: '0 30 1 * * ?',
60+
repository: REPO_NAME,
61+
config: {
62+
indices: ['my_index'],
63+
ignoreUnavailable: true,
64+
partial: false,
65+
metadata: {
66+
meta: 'my_meta',
67+
},
68+
},
69+
retention: {
70+
expireAfterValue: 1,
71+
expireAfterUnit: 'd',
72+
maxCount: 10,
73+
minCount: 5,
74+
},
75+
isManagedPolicy: false,
76+
})
77+
.expect(200);
78+
79+
expect(body).to.eql({
80+
acknowledged: true,
81+
});
82+
});
83+
84+
it('should create a policy with only required fields', async () => {
85+
const { body } = await supertest
86+
.post(`${API_BASE_PATH}/policies`)
87+
.set('kbn-xsrf', 'xxx')
88+
// Exclude config and retention
89+
.send({
90+
name: REQUIRED_FIELDS_POLICY_NAME,
91+
snapshotName: 'my_snapshot',
92+
repository: REPO_NAME,
93+
schedule: '0 30 1 * * ?',
94+
isManagedPolicy: false,
95+
})
96+
.expect(200);
97+
98+
expect(body).to.eql({
99+
acknowledged: true,
100+
});
101+
});
102+
});
103+
104+
describe('Update', () => {
105+
const POLICY_NAME = 'test_update_policy';
106+
const POLICY = {
107+
name: POLICY_NAME,
108+
snapshotName: 'my_snapshot',
109+
schedule: '0 30 1 * * ?',
110+
repository: REPO_NAME,
111+
config: {
112+
indices: ['my_index'],
113+
ignoreUnavailable: true,
114+
partial: false,
115+
metadata: {
116+
meta: 'my_meta',
117+
},
118+
},
119+
retention: {
120+
expireAfterValue: 1,
121+
expireAfterUnit: 'd',
122+
maxCount: 10,
123+
minCount: 5,
124+
},
125+
isManagedPolicy: false,
126+
};
127+
128+
before(async () => {
129+
// Create SLM policy that can be used to test PUT request
130+
try {
131+
await createPolicy(POLICY, true);
132+
} catch (err) {
133+
// eslint-disable-next-line no-console
134+
console.log('[Setup error] Error creating policy');
135+
throw err;
136+
}
137+
});
138+
139+
it('should allow an existing policy to be updated', async () => {
140+
const uri = `${API_BASE_PATH}/policies/${POLICY_NAME}`;
141+
142+
const { body } = await supertest
143+
.put(uri)
144+
.set('kbn-xsrf', 'xxx')
145+
.send({
146+
...POLICY,
147+
schedule: '0 0 0 ? * 7',
148+
})
149+
.expect(200);
150+
151+
expect(body).to.eql({
152+
acknowledged: true,
153+
});
154+
});
155+
156+
it('should allow optional fields to be removed', async () => {
157+
const uri = `${API_BASE_PATH}/policies/${POLICY_NAME}`;
158+
const { retention, config, ...requiredFields } = POLICY;
159+
160+
const { body } = await supertest
161+
.put(uri)
162+
.set('kbn-xsrf', 'xxx')
163+
.send(requiredFields)
164+
.expect(200);
165+
166+
expect(body).to.eql({
167+
acknowledged: true,
168+
});
169+
});
170+
});
171+
});
172+
}

x-pack/test/api_integration/services/legacy_es.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as legacyElasticsearch from 'elasticsearch';
1010

1111
import { elasticsearchClientPlugin as securityEsClientPlugin } from '../../../plugins/security/server/elasticsearch/elasticsearch_client_plugin';
1212
import { elasticsearchJsPlugin as indexManagementEsClientPlugin } from '../../../plugins/index_management/server/client/elasticsearch';
13+
import { elasticsearchJsPlugin as snapshotRestoreEsClientPlugin } from '../../../plugins/snapshot_restore/server/client/elasticsearch_sr';
1314
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
1415
import { DEFAULT_API_VERSION } from '../../../../src/core/server/elasticsearch/elasticsearch_config';
1516

@@ -20,6 +21,6 @@ export function LegacyEsProvider({ getService }) {
2021
apiVersion: DEFAULT_API_VERSION,
2122
host: formatUrl(config.get('servers.elasticsearch')),
2223
requestTimeout: config.get('timeouts.esRequestTimeout'),
23-
plugins: [securityEsClientPlugin, indexManagementEsClientPlugin],
24+
plugins: [securityEsClientPlugin, indexManagementEsClientPlugin, snapshotRestoreEsClientPlugin],
2425
});
2526
}

0 commit comments

Comments
 (0)