From b3348b4576e2aa53e20cc24f3a82dc8032ef0055 Mon Sep 17 00:00:00 2001 From: Peter Nied Date: Fri, 16 Feb 2024 18:40:42 +0000 Subject: [PATCH] Create an index with invalid setting Signed-off-by: Peter Nied --- .../RestoreSnapshotInvalidStateIT.java | 95 +++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 server/src/internalClusterTest/java/org/opensearch/snapshots/RestoreSnapshotInvalidStateIT.java diff --git a/server/src/internalClusterTest/java/org/opensearch/snapshots/RestoreSnapshotInvalidStateIT.java b/server/src/internalClusterTest/java/org/opensearch/snapshots/RestoreSnapshotInvalidStateIT.java new file mode 100644 index 0000000000000..8a8f2bf2479cf --- /dev/null +++ b/server/src/internalClusterTest/java/org/opensearch/snapshots/RestoreSnapshotInvalidStateIT.java @@ -0,0 +1,95 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ + +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* + * Modifications Copyright OpenSearch Contributors. See + * GitHub history for details. + */ + +package org.opensearch.snapshots; + +import org.hamcrest.MatcherAssert; +import org.opensearch.action.UnavailableShardsException; +import org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; +import org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse; +import org.opensearch.action.admin.indices.get.GetIndexResponse; +import org.opensearch.common.settings.Settings; + +import java.util.Map; + +import static org.hamcrest.Matchers.*; +import static org.opensearch.index.mapper.MapperService.INDEX_MAPPER_DYNAMIC_SETTING; +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; + +public class RestoreSnapshotInvalidStateIT extends AbstractSnapshotIntegTestCase { + /** + * Reproduces https://github.com/opensearch-project/OpenSearch/issues/3879 + */ + public void testRestoreIndexWithInvalidSettings() throws Exception { + //noinspection deprecation + final String disallowedSetting = INDEX_MAPPER_DYNAMIC_SETTING.getKey(); + + final String repoName = "test-repo"; + logger.info("--> creating repository: {}", repoName); + createRepository(repoName, "fs"); + + final String indexName = "test-idx"; + logger.info("--> create index: {}", indexName); + assertAcked(prepareCreate(indexName, 2).setSettings(Map.of("number_of_shards", 1))); + ensureGreen(); + + final String snapshotName = "test-snap"; + logger.info("--> creating snapshot: {} of indexes {}", snapshotName, indexName); + final CreateSnapshotResponse createSnapshotResponse = clusterAdmin().prepareCreateSnapshot(repoName, snapshotName) + .setWaitForCompletion(true) + .setIndices(indexName) + .get(); + + logger.info("--> delete index: {} ", indexName); + cluster().wipeIndices(indexName); + + logger.info("--> restore snapshot {}, for indexes {} with invalid setting {}", snapshotName, indexName, disallowedSetting); + final RestoreSnapshotResponse restoreSnapshotResponse = clusterAdmin().prepareRestoreSnapshot(repoName, snapshotName) + .setWaitForCompletion(true) + .setIndexSettings(Settings.builder() + .put(disallowedSetting, true) + .build()) + .execute() + .actionGet(); + + final RestoreInfo restoreInfo = restoreSnapshotResponse.getRestoreInfo(); + logger.info("--> restore details {}", restoreInfo); + MatcherAssert.assertThat(restoreInfo.status().getStatus(), is(200)); + MatcherAssert.assertThat(restoreInfo.indices(), contains(indexName)); + + final GetIndexResponse getIndexResponse = client().admin().indices().prepareGetIndex().addIndices(indexName).execute().actionGet(); + MatcherAssert.assertThat(getIndexResponse.settings().get(indexName).keySet(), hasItem(disallowedSetting)); + logger.info("--> index {}'s restored settings {}", indexName, getIndexResponse); + + assertThrows(UnavailableShardsException.class, () -> client().prepareIndex().setIndex(indexName).setId("1").setSource(Map.of("hello", "world")).execute().actionGet()); + } +}