Skip to content

Commit

Permalink
Create an index with invalid setting
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Nied <[email protected]>
  • Loading branch information
peternied committed Feb 16, 2024
1 parent 76ae14a commit b3348b4
Showing 1 changed file with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit b3348b4

Please sign in to comment.