|
12 | 12 | import org.elasticsearch.cluster.metadata.IndexMetadata; |
13 | 13 | import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; |
14 | 14 | import org.elasticsearch.cluster.metadata.Metadata; |
| 15 | +import org.elasticsearch.cluster.metadata.RepositoriesMetadata; |
| 16 | +import org.elasticsearch.cluster.metadata.RepositoryMetadata; |
15 | 17 | import org.elasticsearch.common.Strings; |
| 18 | +import org.elasticsearch.common.settings.Settings; |
16 | 19 |
|
| 20 | +import java.util.Collections; |
17 | 21 | import java.util.Locale; |
18 | 22 |
|
19 | 23 | import static org.elasticsearch.xpack.core.ilm.GenerateSnapshotNameStep.generateSnapshotName; |
20 | 24 | import static org.elasticsearch.xpack.core.ilm.GenerateSnapshotNameStep.validateGeneratedSnapshotName; |
| 25 | +import static org.elasticsearch.xpack.core.ilm.LifecycleExecutionState.ILM_CUSTOM_METADATA_KEY; |
21 | 26 | import static org.hamcrest.Matchers.containsInAnyOrder; |
22 | 27 | import static org.hamcrest.Matchers.containsString; |
23 | 28 | import static org.hamcrest.Matchers.greaterThan; |
@@ -63,23 +68,98 @@ protected GenerateSnapshotNameStep copyInstance(GenerateSnapshotNameStep instanc |
63 | 68 | public void testPerformAction() { |
64 | 69 | String indexName = randomAlphaOfLength(10); |
65 | 70 | String policyName = "test-ilm-policy"; |
66 | | - IndexMetadata.Builder indexMetadataBuilder = |
67 | | - IndexMetadata.builder(indexName).settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) |
68 | | - .numberOfShards(randomIntBetween(1, 5)).numberOfReplicas(randomIntBetween(0, 5)); |
| 71 | + final IndexMetadata indexMetadata = IndexMetadata.builder(indexName) |
| 72 | + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) |
| 73 | + .numberOfShards(randomIntBetween(1, 5)) |
| 74 | + .numberOfReplicas(randomIntBetween(0, 5)) |
| 75 | + .build(); |
| 76 | + |
| 77 | + GenerateSnapshotNameStep generateSnapshotNameStep = createRandomInstance(); |
| 78 | + |
| 79 | + // generate a snapshot repository with the expected name |
| 80 | + RepositoryMetadata repo = new RepositoryMetadata(generateSnapshotNameStep.getSnapshotRepository(), "fs", Settings.EMPTY); |
69 | 81 |
|
70 | | - final IndexMetadata indexMetadata = indexMetadataBuilder.build(); |
71 | 82 | ClusterState clusterState = ClusterState.builder(emptyClusterState()) |
72 | | - .metadata(Metadata.builder().put(indexMetadata, false).build()).build(); |
| 83 | + .metadata(Metadata.builder() |
| 84 | + .put(indexMetadata, false) |
| 85 | + .putCustom(RepositoriesMetadata.TYPE, new RepositoriesMetadata(Collections.singletonList(repo))) |
| 86 | + .build()).build(); |
73 | 87 |
|
74 | | - GenerateSnapshotNameStep generateSnapshotNameStep = createRandomInstance(); |
75 | | - ClusterState newClusterState = generateSnapshotNameStep.performAction(indexMetadata.getIndex(), clusterState); |
| 88 | + ClusterState newClusterState; |
76 | 89 |
|
| 90 | + // the snapshot index name, snapshot repository, and snapshot name are generated as expected |
| 91 | + newClusterState = generateSnapshotNameStep.performAction(indexMetadata.getIndex(), clusterState); |
77 | 92 | LifecycleExecutionState executionState = LifecycleExecutionState.fromIndexMetadata(newClusterState.metadata().index(indexName)); |
| 93 | + assertThat(executionState.getSnapshotIndexName(), is(indexName)); |
78 | 94 | assertThat("the " + GenerateSnapshotNameStep.NAME + " step must generate a snapshot name", executionState.getSnapshotName(), |
79 | 95 | notNullValue()); |
80 | 96 | assertThat(executionState.getSnapshotRepository(), is(generateSnapshotNameStep.getSnapshotRepository())); |
81 | 97 | assertThat(executionState.getSnapshotName(), containsString(indexName.toLowerCase(Locale.ROOT))); |
82 | 98 | assertThat(executionState.getSnapshotName(), containsString(policyName.toLowerCase(Locale.ROOT))); |
| 99 | + |
| 100 | + // re-running this step results in no change to the important outputs |
| 101 | + newClusterState = generateSnapshotNameStep.performAction(indexMetadata.getIndex(), newClusterState); |
| 102 | + LifecycleExecutionState repeatedState = LifecycleExecutionState.fromIndexMetadata(newClusterState.metadata().index(indexName)); |
| 103 | + assertThat(repeatedState.getSnapshotIndexName(), is(executionState.getSnapshotIndexName())); |
| 104 | + assertThat(repeatedState.getSnapshotRepository(), is(executionState.getSnapshotRepository())); |
| 105 | + assertThat(repeatedState.getSnapshotName(), is(executionState.getSnapshotName())); |
| 106 | + } |
| 107 | + |
| 108 | + public void testPerformActionRejectsNonexistentRepository() { |
| 109 | + String indexName = randomAlphaOfLength(10); |
| 110 | + String policyName = "test-ilm-policy"; |
| 111 | + final IndexMetadata indexMetadata = IndexMetadata.builder(indexName) |
| 112 | + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) |
| 113 | + .numberOfShards(randomIntBetween(1, 5)) |
| 114 | + .numberOfReplicas(randomIntBetween(0, 5)) |
| 115 | + .build(); |
| 116 | + |
| 117 | + GenerateSnapshotNameStep generateSnapshotNameStep = createRandomInstance(); |
| 118 | + |
| 119 | + ClusterState clusterState = ClusterState.builder(emptyClusterState()) |
| 120 | + .metadata(Metadata.builder() |
| 121 | + .put(indexMetadata, false) |
| 122 | + .putCustom(RepositoriesMetadata.TYPE, RepositoriesMetadata.EMPTY) |
| 123 | + .build()).build(); |
| 124 | + |
| 125 | + IllegalStateException illegalStateException = expectThrows(IllegalStateException.class, |
| 126 | + () -> generateSnapshotNameStep.performAction(indexMetadata.getIndex(), clusterState)); |
| 127 | + assertThat(illegalStateException.getMessage(), is("repository [" + generateSnapshotNameStep.getSnapshotRepository() + "] " + |
| 128 | + "is missing. [test-ilm-policy] policy for index [" + indexName + "] cannot continue until the repository " + |
| 129 | + "is created or the policy is changed")); |
| 130 | + } |
| 131 | + |
| 132 | + public void testPerformActionWillOverwriteCachedRepository() { |
| 133 | + String indexName = randomAlphaOfLength(10); |
| 134 | + String policyName = "test-ilm-policy"; |
| 135 | + |
| 136 | + LifecycleExecutionState.Builder newCustomData = LifecycleExecutionState.builder(); |
| 137 | + newCustomData.setSnapshotName("snapshot-name-is-not-touched"); |
| 138 | + newCustomData.setSnapshotRepository("snapshot-repository-will-be-reset"); |
| 139 | + |
| 140 | + final IndexMetadata indexMetadata = IndexMetadata.builder(indexName) |
| 141 | + .settings(settings(Version.CURRENT).put(LifecycleSettings.LIFECYCLE_NAME, policyName)) |
| 142 | + .numberOfShards(randomIntBetween(1, 5)) |
| 143 | + .numberOfReplicas(randomIntBetween(0, 5)) |
| 144 | + .putCustom(ILM_CUSTOM_METADATA_KEY, newCustomData.build().asMap()) |
| 145 | + .build(); |
| 146 | + |
| 147 | + GenerateSnapshotNameStep generateSnapshotNameStep = createRandomInstance(); |
| 148 | + |
| 149 | + // generate a snapshot repository with the expected name |
| 150 | + RepositoryMetadata repo = new RepositoryMetadata(generateSnapshotNameStep.getSnapshotRepository(), "fs", Settings.EMPTY); |
| 151 | + |
| 152 | + ClusterState clusterState = ClusterState.builder(emptyClusterState()) |
| 153 | + .metadata(Metadata.builder() |
| 154 | + .put(indexMetadata, false) |
| 155 | + .putCustom(RepositoriesMetadata.TYPE, new RepositoriesMetadata(Collections.singletonList(repo))) |
| 156 | + .build()).build(); |
| 157 | + |
| 158 | + ClusterState newClusterState = generateSnapshotNameStep.performAction(indexMetadata.getIndex(), clusterState); |
| 159 | + |
| 160 | + LifecycleExecutionState executionState = LifecycleExecutionState.fromIndexMetadata(newClusterState.metadata().index(indexName)); |
| 161 | + assertThat(executionState.getSnapshotName(), is("snapshot-name-is-not-touched")); |
| 162 | + assertThat(executionState.getSnapshotRepository(), is(generateSnapshotNameStep.getSnapshotRepository())); |
83 | 163 | } |
84 | 164 |
|
85 | 165 | public void testNameGeneration() { |
|
0 commit comments