Browse Source

Add IT that tries to rename a DS backing index (#59006)

Andrei Dan 5 years ago
parent
commit
91db0db4b8

+ 77 - 2
server/src/internalClusterTest/java/org/elasticsearch/snapshots/DataStreamsSnapshotsIT.java

@@ -44,11 +44,10 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutionException;
 
+import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
 import static org.hamcrest.Matchers.empty;
 import static org.hamcrest.Matchers.is;
 
-import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
-
 public class DataStreamsSnapshotsIT extends AbstractSnapshotIntegTestCase {
 
     private static final String DS_BACKING_INDEX_NAME = DataStream.getDefaultBackingIndexName("ds", 1);
@@ -194,6 +193,82 @@ public class DataStreamsSnapshotsIT extends AbstractSnapshotIntegTestCase {
         assertEquals(DOCUMENT_SOURCE, client.prepareGet(DS2_BACKING_INDEX_NAME, id).get().getSourceAsMap());
     }
 
+    public void testBackingIndexIsNotRenamedWhenRestoringDataStream() {
+        CreateSnapshotResponse createSnapshotResponse = client.admin().cluster()
+            .prepareCreateSnapshot(REPO, SNAPSHOT)
+            .setWaitForCompletion(true)
+            .setIndices("ds")
+            .setIncludeGlobalState(false)
+            .get();
+
+        RestStatus status = createSnapshotResponse.getSnapshotInfo().status();
+        assertEquals(RestStatus.OK, status);
+
+        expectThrows(SnapshotRestoreException.class, () -> client.admin().cluster()
+            .prepareRestoreSnapshot(REPO, SNAPSHOT)
+            .setWaitForCompletion(true)
+            .setIndices("ds")
+            .get());
+
+        // delete data stream
+        client.admin().indices().deleteDataStream(new DeleteDataStreamAction.Request("ds")).actionGet();
+
+        // restore data stream attempting to rename the backing index
+        RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster()
+            .prepareRestoreSnapshot(REPO, SNAPSHOT)
+            .setWaitForCompletion(true)
+            .setIndices("ds")
+            .setRenamePattern(DS_BACKING_INDEX_NAME)
+            .setRenameReplacement("new_index_name")
+            .get();
+
+        assertThat(restoreSnapshotResponse.status(), is(RestStatus.OK));
+
+        GetDataStreamAction.Request getDSRequest = new GetDataStreamAction.Request("ds");
+        GetDataStreamAction.Response response = client.admin().indices().getDataStreams(getDSRequest).actionGet();
+        assertThat(response.getDataStreams().get(0).getIndices().get(0).getName(), is(DS_BACKING_INDEX_NAME));
+    }
+
+    public void testDataStreamAndBackingIndidcesAreRenamedUsingRegex() {
+        CreateSnapshotResponse createSnapshotResponse = client.admin().cluster()
+            .prepareCreateSnapshot(REPO, SNAPSHOT)
+            .setWaitForCompletion(true)
+            .setIndices("ds")
+            .setIncludeGlobalState(false)
+            .get();
+
+        RestStatus status = createSnapshotResponse.getSnapshotInfo().status();
+        assertEquals(RestStatus.OK, status);
+
+        expectThrows(SnapshotRestoreException.class, () -> client.admin().cluster()
+            .prepareRestoreSnapshot(REPO, SNAPSHOT)
+            .setWaitForCompletion(true)
+            .setIndices("ds")
+            .get());
+
+        // restore data stream attempting to rename the backing index
+        RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster()
+            .prepareRestoreSnapshot(REPO, SNAPSHOT)
+            .setWaitForCompletion(true)
+            .setIndices("ds")
+            .setRenamePattern("(.+)")
+            .setRenameReplacement("test-$1")
+            .get();
+
+        assertThat(restoreSnapshotResponse.status(), is(RestStatus.OK));
+
+        // assert "ds" was restored as "test-ds" and the backing index has a valid name
+        GetDataStreamAction.Request getRenamedDS = new GetDataStreamAction.Request("test-ds");
+        GetDataStreamAction.Response response = client.admin().indices().getDataStreams(getRenamedDS).actionGet();
+        assertThat(response.getDataStreams().get(0).getIndices().get(0).getName(),
+            is(DataStream.getDefaultBackingIndexName("test-ds", 1L)));
+
+        // data stream "ds" should still exist in the system
+        GetDataStreamAction.Request getDSRequest = new GetDataStreamAction.Request("ds");
+        response = client.admin().indices().getDataStreams(getDSRequest).actionGet();
+        assertThat(response.getDataStreams().get(0).getIndices().get(0).getName(), is(DS_BACKING_INDEX_NAME));
+    }
+
     public void testWildcards() throws Exception {
         CreateSnapshotResponse createSnapshotResponse = client.admin().cluster()
             .prepareCreateSnapshot(REPO, "snap2")