|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +/* |
| 10 | + * Licensed to Elasticsearch under one or more contributor |
| 11 | + * license agreements. See the NOTICE file distributed with |
| 12 | + * this work for additional information regarding copyright |
| 13 | + * ownership. Elasticsearch licenses this file to you under |
| 14 | + * the Apache License, Version 2.0 (the "License"); you may |
| 15 | + * not use this file except in compliance with the License. |
| 16 | + * You may obtain a copy of the License at |
| 17 | + * |
| 18 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | + * |
| 20 | + * Unless required by applicable law or agreed to in writing, |
| 21 | + * software distributed under the License is distributed on an |
| 22 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 23 | + * KIND, either express or implied. See the License for the |
| 24 | + * specific language governing permissions and limitations |
| 25 | + * under the License. |
| 26 | + */ |
| 27 | + |
| 28 | +/* |
| 29 | + * Modifications Copyright OpenSearch Contributors. See |
| 30 | + * GitHub history for details. |
| 31 | + */ |
| 32 | + |
| 33 | +package org.opensearch.snapshots; |
| 34 | + |
| 35 | +import org.hamcrest.MatcherAssert; |
| 36 | +import org.opensearch.action.UnavailableShardsException; |
| 37 | +import org.opensearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; |
| 38 | +import org.opensearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse; |
| 39 | +import org.opensearch.action.admin.indices.get.GetIndexResponse; |
| 40 | +import org.opensearch.common.settings.Settings; |
| 41 | + |
| 42 | +import java.util.Map; |
| 43 | + |
| 44 | +import static org.hamcrest.Matchers.*; |
| 45 | +import static org.opensearch.index.mapper.MapperService.INDEX_MAPPER_DYNAMIC_SETTING; |
| 46 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; |
| 47 | + |
| 48 | +public class RestoreSnapshotInvalidStateIT extends AbstractSnapshotIntegTestCase { |
| 49 | + /** |
| 50 | + * Reproduces https://github.com/opensearch-project/OpenSearch/issues/3879 |
| 51 | + */ |
| 52 | + public void testRestoreIndexWithInvalidSettings() throws Exception { |
| 53 | + //noinspection deprecation |
| 54 | + final String disallowedSetting = INDEX_MAPPER_DYNAMIC_SETTING.getKey(); |
| 55 | + |
| 56 | + final String repoName = "test-repo"; |
| 57 | + logger.info("--> creating repository: {}", repoName); |
| 58 | + createRepository(repoName, "fs"); |
| 59 | + |
| 60 | + final String indexName = "test-idx"; |
| 61 | + logger.info("--> create index: {}", indexName); |
| 62 | + assertAcked(prepareCreate(indexName, 2).setSettings(Map.of("number_of_shards", 1))); |
| 63 | + ensureGreen(); |
| 64 | + |
| 65 | + final String snapshotName = "test-snap"; |
| 66 | + logger.info("--> creating snapshot: {} of indexes {}", snapshotName, indexName); |
| 67 | + final CreateSnapshotResponse createSnapshotResponse = clusterAdmin().prepareCreateSnapshot(repoName, snapshotName) |
| 68 | + .setWaitForCompletion(true) |
| 69 | + .setIndices(indexName) |
| 70 | + .get(); |
| 71 | + |
| 72 | + logger.info("--> delete index: {} ", indexName); |
| 73 | + cluster().wipeIndices(indexName); |
| 74 | + |
| 75 | + logger.info("--> restore snapshot {}, for indexes {} with invalid setting {}", snapshotName, indexName, disallowedSetting); |
| 76 | + final RestoreSnapshotResponse restoreSnapshotResponse = clusterAdmin().prepareRestoreSnapshot(repoName, snapshotName) |
| 77 | + .setWaitForCompletion(true) |
| 78 | + .setIndexSettings(Settings.builder() |
| 79 | + .put(disallowedSetting, true) |
| 80 | + .build()) |
| 81 | + .execute() |
| 82 | + .actionGet(); |
| 83 | + |
| 84 | + final RestoreInfo restoreInfo = restoreSnapshotResponse.getRestoreInfo(); |
| 85 | + logger.info("--> restore details {}", restoreInfo); |
| 86 | + MatcherAssert.assertThat(restoreInfo.status().getStatus(), is(200)); |
| 87 | + MatcherAssert.assertThat(restoreInfo.indices(), contains(indexName)); |
| 88 | + |
| 89 | + final GetIndexResponse getIndexResponse = client().admin().indices().prepareGetIndex().addIndices(indexName).execute().actionGet(); |
| 90 | + MatcherAssert.assertThat(getIndexResponse.settings().get(indexName).keySet(), hasItem(disallowedSetting)); |
| 91 | + logger.info("--> index {}'s restored settings {}", indexName, getIndexResponse); |
| 92 | + |
| 93 | + assertThrows(UnavailableShardsException.class, () -> client().prepareIndex().setIndex(indexName).setId("1").setSource(Map.of("hello", "world")).execute().actionGet()); |
| 94 | + } |
| 95 | +} |
0 commit comments