-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e9b2e30
commit 0c4792e
Showing
6 changed files
with
275 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
x-pack/test/api_integration/apis/management/snapshot_restore/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* 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 { FtrProviderContext } from '../../../ftr_provider_context'; | ||
|
||
export default function ({ loadTestFile }: FtrProviderContext) { | ||
describe('Snapshot and Restore', () => { | ||
loadTestFile(require.resolve('./snapshot_restore')); | ||
}); | ||
} |
81 changes: 81 additions & 0 deletions
81
x-pack/test/api_integration/apis/management/snapshot_restore/lib/elasticsearch.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* 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 { FtrProviderContext } from '../../../../ftr_provider_context'; | ||
|
||
interface SlmPolicy { | ||
name: string; | ||
snapshotName: string; | ||
schedule: string; | ||
repository: string; | ||
isManagedPolicy: boolean; | ||
config?: { | ||
indices?: string | string[]; | ||
ignoreUnavailable?: boolean; | ||
includeGlobalState?: boolean; | ||
partial?: boolean; | ||
metadata?: Record<string, string>; | ||
}; | ||
retention?: { | ||
expireAfterValue?: number | ''; | ||
expireAfterUnit?: string; | ||
maxCount?: number | ''; | ||
minCount?: number | ''; | ||
}; | ||
} | ||
|
||
/** | ||
* Helpers to create and delete SLM policies on the Elasticsearch instance | ||
* during our tests. | ||
* @param {ElasticsearchClient} es The Elasticsearch client instance | ||
*/ | ||
export const registerEsHelpers = (getService: FtrProviderContext['getService']) => { | ||
let policiesCreated: string[] = []; | ||
|
||
const es = getService('legacyEs'); | ||
|
||
const createRepository = (repoName: string) => { | ||
return es.snapshot.createRepository({ | ||
repository: repoName, | ||
body: { | ||
type: 'fs', | ||
settings: { | ||
location: '/tmp/', | ||
}, | ||
}, | ||
verify: false, | ||
}); | ||
}; | ||
|
||
const createPolicy = (policy: SlmPolicy, cachePolicy?: boolean) => { | ||
if (cachePolicy) { | ||
policiesCreated.push(policy.name); | ||
} | ||
|
||
return es.sr.updatePolicy({ | ||
name: policy.name, | ||
body: policy, | ||
}); | ||
}; | ||
|
||
const deletePolicy = (policyName: string) => es.sr.deletePolicy({ name: policyName }); | ||
|
||
const cleanupPolicies = () => | ||
Promise.all(policiesCreated.map(deletePolicy)) | ||
.then(() => { | ||
policiesCreated = []; | ||
}) | ||
.catch((err) => { | ||
// eslint-disable-next-line no-console | ||
console.log(`[Cleanup error] Error deleting ES resources: ${err.message}`); | ||
}); | ||
|
||
return { | ||
createRepository, | ||
createPolicy, | ||
deletePolicy, | ||
cleanupPolicies, | ||
}; | ||
}; |
7 changes: 7 additions & 0 deletions
7
x-pack/test/api_integration/apis/management/snapshot_restore/lib/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { registerEsHelpers } from './elasticsearch'; |
172 changes: 172 additions & 0 deletions
172
x-pack/test/api_integration/apis/management/snapshot_restore/snapshot_restore.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
/* | ||
* 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 expect from '@kbn/expect'; | ||
|
||
import { FtrProviderContext } from '../../../ftr_provider_context'; | ||
import { registerEsHelpers } from './lib'; | ||
|
||
const API_BASE_PATH = '/api/snapshot_restore'; | ||
const REPO_NAME = 'test_repo'; | ||
|
||
export default function ({ getService }: FtrProviderContext) { | ||
const supertest = getService('supertest'); | ||
|
||
const { createRepository, createPolicy, deletePolicy, cleanupPolicies } = registerEsHelpers( | ||
getService | ||
); | ||
|
||
describe('Snapshot Lifecycle Management', function () { | ||
before(async () => { | ||
try { | ||
await createRepository(REPO_NAME); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.log('[Setup error] Error creating repository'); | ||
throw err; | ||
} | ||
}); | ||
|
||
after(async () => { | ||
await cleanupPolicies(); | ||
}); | ||
|
||
describe('Create', () => { | ||
const POLICY_NAME = 'test_create_policy'; | ||
const REQUIRED_FIELDS_POLICY_NAME = 'test_create_required_fields_policy'; | ||
|
||
after(async () => { | ||
// Clean up any policies created in test cases | ||
await Promise.all([POLICY_NAME, REQUIRED_FIELDS_POLICY_NAME].map(deletePolicy)).catch( | ||
(err) => { | ||
// eslint-disable-next-line no-console | ||
console.log(`[Cleanup error] Error deleting policies: ${err.message}`); | ||
throw err; | ||
} | ||
); | ||
}); | ||
|
||
it('should create a SLM policy', async () => { | ||
const { body } = await supertest | ||
.post(`${API_BASE_PATH}/policies`) | ||
.set('kbn-xsrf', 'xxx') | ||
.send({ | ||
name: POLICY_NAME, | ||
snapshotName: 'my_snapshot', | ||
schedule: '0 30 1 * * ?', | ||
repository: REPO_NAME, | ||
config: { | ||
indices: ['my_index'], | ||
ignoreUnavailable: true, | ||
partial: false, | ||
metadata: { | ||
meta: 'my_meta', | ||
}, | ||
}, | ||
retention: { | ||
expireAfterValue: 1, | ||
expireAfterUnit: 'd', | ||
maxCount: 10, | ||
minCount: 5, | ||
}, | ||
isManagedPolicy: false, | ||
}) | ||
.expect(200); | ||
|
||
expect(body).to.eql({ | ||
acknowledged: true, | ||
}); | ||
}); | ||
|
||
it('should create a policy with only required fields', async () => { | ||
const { body } = await supertest | ||
.post(`${API_BASE_PATH}/policies`) | ||
.set('kbn-xsrf', 'xxx') | ||
// Exclude config and retention | ||
.send({ | ||
name: REQUIRED_FIELDS_POLICY_NAME, | ||
snapshotName: 'my_snapshot', | ||
repository: REPO_NAME, | ||
schedule: '0 30 1 * * ?', | ||
isManagedPolicy: false, | ||
}) | ||
.expect(200); | ||
|
||
expect(body).to.eql({ | ||
acknowledged: true, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Update', () => { | ||
const POLICY_NAME = 'test_update_policy'; | ||
const POLICY = { | ||
name: POLICY_NAME, | ||
snapshotName: 'my_snapshot', | ||
schedule: '0 30 1 * * ?', | ||
repository: REPO_NAME, | ||
config: { | ||
indices: ['my_index'], | ||
ignoreUnavailable: true, | ||
partial: false, | ||
metadata: { | ||
meta: 'my_meta', | ||
}, | ||
}, | ||
retention: { | ||
expireAfterValue: 1, | ||
expireAfterUnit: 'd', | ||
maxCount: 10, | ||
minCount: 5, | ||
}, | ||
isManagedPolicy: false, | ||
}; | ||
|
||
before(async () => { | ||
// Create SLM policy that can be used to test PUT request | ||
try { | ||
await createPolicy(POLICY, true); | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.log('[Setup error] Error creating policy'); | ||
throw err; | ||
} | ||
}); | ||
|
||
it('should allow an existing policy to be updated', async () => { | ||
const uri = `${API_BASE_PATH}/policies/${POLICY_NAME}`; | ||
|
||
const { body } = await supertest | ||
.put(uri) | ||
.set('kbn-xsrf', 'xxx') | ||
.send({ | ||
...POLICY, | ||
schedule: '0 0 0 ? * 7', | ||
}) | ||
.expect(200); | ||
|
||
expect(body).to.eql({ | ||
acknowledged: true, | ||
}); | ||
}); | ||
|
||
it('should allow optional fields to be removed', async () => { | ||
const uri = `${API_BASE_PATH}/policies/${POLICY_NAME}`; | ||
const { retention, config, ...requiredFields } = POLICY; | ||
|
||
const { body } = await supertest | ||
.put(uri) | ||
.set('kbn-xsrf', 'xxx') | ||
.send(requiredFields) | ||
.expect(200); | ||
|
||
expect(body).to.eql({ | ||
acknowledged: true, | ||
}); | ||
}); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters