|
| 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 | +package org.elasticsearch.xpack.autoscaling; |
| 8 | + |
| 9 | +import org.elasticsearch.ResourceNotFoundException; |
| 10 | +import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; |
| 11 | +import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse; |
| 12 | +import org.elasticsearch.client.Client; |
| 13 | +import org.elasticsearch.common.settings.Settings; |
| 14 | +import org.elasticsearch.rest.RestStatus; |
| 15 | +import org.elasticsearch.xpack.autoscaling.action.DeleteAutoscalingPolicyAction; |
| 16 | +import org.elasticsearch.xpack.autoscaling.action.GetAutoscalingPolicyAction; |
| 17 | +import org.elasticsearch.xpack.autoscaling.action.PutAutoscalingPolicyAction; |
| 18 | +import org.elasticsearch.xpack.autoscaling.policy.AutoscalingPolicy; |
| 19 | +import org.junit.Before; |
| 20 | + |
| 21 | +import java.nio.file.Path; |
| 22 | + |
| 23 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 24 | +import static org.elasticsearch.xpack.autoscaling.AutoscalingTestCase.randomAutoscalingPolicy; |
| 25 | +import static org.elasticsearch.xpack.autoscaling.AutoscalingTestCase.randomAutoscalingPolicyOfName; |
| 26 | +import static org.hamcrest.Matchers.containsString; |
| 27 | +import static org.hamcrest.Matchers.equalTo; |
| 28 | + |
| 29 | +public class AutoscalingSnapshotsIT extends AutoscalingIntegTestCase { |
| 30 | + |
| 31 | + public static final String REPO = "repo"; |
| 32 | + public static final String SNAPSHOT = "snap"; |
| 33 | + |
| 34 | + @Before |
| 35 | + public void setup() throws Exception { |
| 36 | + Path location = randomRepoPath(); |
| 37 | + logger.info("--> creating repository [{}] [{}]", REPO, "fs"); |
| 38 | + assertAcked(clusterAdmin().preparePutRepository(REPO).setType("fs").setSettings(Settings.builder().put("location", location))); |
| 39 | + } |
| 40 | + |
| 41 | + public void testAutoscalingPolicyWillNotBeRestored() { |
| 42 | + final Client client = client(); |
| 43 | + |
| 44 | + AutoscalingPolicy policy = randomAutoscalingPolicy(); |
| 45 | + putPolicy(policy); |
| 46 | + |
| 47 | + CreateSnapshotResponse createSnapshotResponse = client.admin() |
| 48 | + .cluster() |
| 49 | + .prepareCreateSnapshot(REPO, SNAPSHOT) |
| 50 | + .setWaitForCompletion(true) |
| 51 | + .setIncludeGlobalState(true) |
| 52 | + .get(); |
| 53 | + assertEquals(RestStatus.OK, createSnapshotResponse.status()); |
| 54 | + |
| 55 | + final boolean deletePolicy = randomBoolean(); |
| 56 | + if (deletePolicy) { |
| 57 | + final DeleteAutoscalingPolicyAction.Request deleteRequest = new DeleteAutoscalingPolicyAction.Request(policy.name()); |
| 58 | + assertAcked(client.execute(DeleteAutoscalingPolicyAction.INSTANCE, deleteRequest).actionGet()); |
| 59 | + } else { |
| 60 | + // Update the policy |
| 61 | + policy = randomAutoscalingPolicyOfName(policy.name()); |
| 62 | + putPolicy(policy); |
| 63 | + } |
| 64 | + final AutoscalingPolicy anotherPolicy = randomAutoscalingPolicy(); |
| 65 | + putPolicy(anotherPolicy); |
| 66 | + |
| 67 | + RestoreSnapshotResponse restoreSnapshotResponse = client.admin() |
| 68 | + .cluster() |
| 69 | + .prepareRestoreSnapshot(REPO, SNAPSHOT) |
| 70 | + .setWaitForCompletion(true) |
| 71 | + .setRestoreGlobalState(true) |
| 72 | + .get(); |
| 73 | + assertEquals(RestStatus.OK, restoreSnapshotResponse.status()); |
| 74 | + |
| 75 | + if (deletePolicy) { |
| 76 | + assertPolicyNotFound(policy); |
| 77 | + } else { |
| 78 | + assertPolicy(policy); |
| 79 | + } |
| 80 | + assertPolicy(anotherPolicy); |
| 81 | + } |
| 82 | + |
| 83 | + private void putPolicy(AutoscalingPolicy policy) { |
| 84 | + final PutAutoscalingPolicyAction.Request request = new PutAutoscalingPolicyAction.Request( |
| 85 | + policy.name(), |
| 86 | + policy.roles(), |
| 87 | + policy.deciders() |
| 88 | + ); |
| 89 | + assertAcked(client().execute(PutAutoscalingPolicyAction.INSTANCE, request).actionGet()); |
| 90 | + } |
| 91 | + |
| 92 | + private void assertPolicy(AutoscalingPolicy policy) { |
| 93 | + final GetAutoscalingPolicyAction.Request getRequest = new GetAutoscalingPolicyAction.Request(policy.name()); |
| 94 | + final AutoscalingPolicy actualPolicy = client().execute(GetAutoscalingPolicyAction.INSTANCE, getRequest).actionGet().policy(); |
| 95 | + assertThat(actualPolicy, equalTo(policy)); |
| 96 | + } |
| 97 | + |
| 98 | + private void assertPolicyNotFound(AutoscalingPolicy policy) { |
| 99 | + final GetAutoscalingPolicyAction.Request getRequest = new GetAutoscalingPolicyAction.Request(policy.name()); |
| 100 | + final ResourceNotFoundException e = expectThrows( |
| 101 | + ResourceNotFoundException.class, |
| 102 | + () -> client().execute(GetAutoscalingPolicyAction.INSTANCE, getRequest).actionGet().policy() |
| 103 | + ); |
| 104 | + assertThat(e.getMessage(), containsString("autoscaling policy with name [" + policy.name() + "] does not exist")); |
| 105 | + } |
| 106 | +} |
0 commit comments