|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.knn.index; |
| 7 | + |
| 8 | +import org.opensearch.client.Request; |
| 9 | +import org.opensearch.client.Response; |
| 10 | +import org.opensearch.client.ResponseException; |
| 11 | +import org.opensearch.common.xcontent.json.JsonXContent; |
| 12 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 13 | +import org.opensearch.knn.KNNRestTestCase; |
| 14 | +import org.junit.Before; |
| 15 | +import org.junit.Test; |
| 16 | +import lombok.SneakyThrows; |
| 17 | +import static org.hamcrest.Matchers.containsString; |
| 18 | + |
| 19 | +public class RestoreSnapshotIT extends KNNRestTestCase { |
| 20 | + |
| 21 | + private String index = "test-index";; |
| 22 | + private String snapshot = "snapshot-" + index; |
| 23 | + private String repository = "repo"; |
| 24 | + |
| 25 | + @Before |
| 26 | + @SneakyThrows |
| 27 | + public void setUp() { |
| 28 | + super.setUp(); |
| 29 | + setupSnapshotRestore(index, snapshot, repository); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + @SneakyThrows |
| 34 | + public void testKnnSettingIsModifiable_whenRestore_thenSuccess() { |
| 35 | + // valid restore |
| 36 | + XContentBuilder restoreCommand = JsonXContent.contentBuilder().startObject(); |
| 37 | + restoreCommand.field("indices", index); |
| 38 | + restoreCommand.field("rename_pattern", index); |
| 39 | + restoreCommand.field("rename_replacement", "restored-" + index); |
| 40 | + restoreCommand.startObject("index_settings"); |
| 41 | + { |
| 42 | + restoreCommand.field("knn.model.index.number_of_shards", 1); |
| 43 | + } |
| 44 | + restoreCommand.endObject(); |
| 45 | + restoreCommand.endObject(); |
| 46 | + Request restoreRequest = new Request("POST", "/_snapshot/" + repository + "/" + snapshot + "/_restore"); |
| 47 | + restoreRequest.addParameter("wait_for_completion", "true"); |
| 48 | + restoreRequest.setJsonEntity(restoreCommand.toString()); |
| 49 | + |
| 50 | + final Response restoreResponse = client().performRequest(restoreRequest); |
| 51 | + assertEquals(200, restoreResponse.getStatusLine().getStatusCode()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + @SneakyThrows |
| 56 | + public void testKnnSettingIsUnmodifiable_whenRestore_thenFailure() { |
| 57 | + // invalid restore |
| 58 | + XContentBuilder restoreCommand = JsonXContent.contentBuilder().startObject(); |
| 59 | + restoreCommand.field("indices", index); |
| 60 | + restoreCommand.field("rename_pattern", index); |
| 61 | + restoreCommand.field("rename_replacement", "restored-" + index); |
| 62 | + restoreCommand.startObject("index_settings"); |
| 63 | + { |
| 64 | + restoreCommand.field("index.knn", false); |
| 65 | + } |
| 66 | + restoreCommand.endObject(); |
| 67 | + restoreCommand.endObject(); |
| 68 | + Request restoreRequest = new Request("POST", "/_snapshot/" + repository + "/" + snapshot + "/_restore"); |
| 69 | + restoreRequest.addParameter("wait_for_completion", "true"); |
| 70 | + restoreRequest.setJsonEntity(restoreCommand.toString()); |
| 71 | + final ResponseException error = expectThrows(ResponseException.class, () -> client().performRequest(restoreRequest)); |
| 72 | + assertThat(error.getMessage(), containsString("cannot modify UnmodifiableOnRestore setting [index.knn]" + " on restore")); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + @SneakyThrows |
| 77 | + public void testKnnSettingCanBeIgnored_whenRestore_thenSuccess() { |
| 78 | + // valid restore |
| 79 | + XContentBuilder restoreCommand = JsonXContent.contentBuilder().startObject(); |
| 80 | + restoreCommand.field("indices", index); |
| 81 | + restoreCommand.field("rename_pattern", index); |
| 82 | + restoreCommand.field("rename_replacement", "restored-" + index); |
| 83 | + restoreCommand.field("ignore_index_settings", "knn.model.index.number_of_shards"); |
| 84 | + restoreCommand.endObject(); |
| 85 | + Request restoreRequest = new Request("POST", "/_snapshot/" + repository + "/" + snapshot + "/_restore"); |
| 86 | + restoreRequest.addParameter("wait_for_completion", "true"); |
| 87 | + restoreRequest.setJsonEntity(restoreCommand.toString()); |
| 88 | + final Response restoreResponse = client().performRequest(restoreRequest); |
| 89 | + assertEquals(200, restoreResponse.getStatusLine().getStatusCode()); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + @SneakyThrows |
| 94 | + public void testKnnSettingCannotBeIgnored_whenRestore_thenFailure() { |
| 95 | + // invalid restore |
| 96 | + XContentBuilder restoreCommand = JsonXContent.contentBuilder().startObject(); |
| 97 | + restoreCommand.field("indices", index); |
| 98 | + restoreCommand.field("rename_pattern", index); |
| 99 | + restoreCommand.field("rename_replacement", "restored-" + index); |
| 100 | + restoreCommand.field("ignore_index_settings", "index.knn"); |
| 101 | + restoreCommand.endObject(); |
| 102 | + Request restoreRequest = new Request("POST", "/_snapshot/" + repository + "/" + snapshot + "/_restore"); |
| 103 | + restoreRequest.addParameter("wait_for_completion", "true"); |
| 104 | + restoreRequest.setJsonEntity(restoreCommand.toString()); |
| 105 | + final ResponseException error = expectThrows(ResponseException.class, () -> client().performRequest(restoreRequest)); |
| 106 | + assertThat(error.getMessage(), containsString("cannot remove UnmodifiableOnRestore setting [index.knn] on restore")); |
| 107 | + } |
| 108 | +} |
0 commit comments