|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.repositories.url; |
| 21 | + |
| 22 | +import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse; |
| 23 | +import org.elasticsearch.action.admin.cluster.snapshots.delete.DeleteSnapshotResponse; |
| 24 | +import org.elasticsearch.action.admin.cluster.snapshots.get.GetSnapshotsResponse; |
| 25 | +import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse; |
| 26 | +import org.elasticsearch.client.Client; |
| 27 | +import org.elasticsearch.common.settings.Settings; |
| 28 | +import org.elasticsearch.common.unit.ByteSizeUnit; |
| 29 | +import org.elasticsearch.plugin.repository.url.URLRepositoryPlugin; |
| 30 | +import org.elasticsearch.plugins.Plugin; |
| 31 | +import org.elasticsearch.repositories.fs.FsRepository; |
| 32 | +import org.elasticsearch.snapshots.SnapshotState; |
| 33 | +import org.elasticsearch.test.ESIntegTestCase; |
| 34 | + |
| 35 | +import java.nio.file.Path; |
| 36 | +import java.util.Collection; |
| 37 | +import java.util.Collections; |
| 38 | + |
| 39 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 40 | +import static org.hamcrest.Matchers.equalTo; |
| 41 | +import static org.hamcrest.Matchers.greaterThan; |
| 42 | +import static org.hamcrest.Matchers.notNullValue; |
| 43 | + |
| 44 | +@ESIntegTestCase.ClusterScope(scope = ESIntegTestCase.Scope.TEST) |
| 45 | +public class URLSnapshotRestoreTests extends ESIntegTestCase { |
| 46 | + |
| 47 | + @Override |
| 48 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 49 | + return Collections.singletonList(URLRepositoryPlugin.class); |
| 50 | + } |
| 51 | + |
| 52 | + public void testUrlRepository() throws Exception { |
| 53 | + Client client = client(); |
| 54 | + |
| 55 | + logger.info("--> creating repository"); |
| 56 | + Path repositoryLocation = randomRepoPath(); |
| 57 | + assertAcked(client.admin().cluster().preparePutRepository("test-repo") |
| 58 | + .setType(FsRepository.TYPE).setSettings(Settings.builder() |
| 59 | + .put(FsRepository.LOCATION_SETTING.getKey(), repositoryLocation) |
| 60 | + .put(FsRepository.COMPRESS_SETTING.getKey(), randomBoolean()) |
| 61 | + .put(FsRepository.CHUNK_SIZE_SETTING.getKey(), randomIntBetween(100, 1000), ByteSizeUnit.BYTES))); |
| 62 | + |
| 63 | + createIndex("test-idx"); |
| 64 | + ensureGreen(); |
| 65 | + |
| 66 | + logger.info("--> indexing some data"); |
| 67 | + for (int i = 0; i < 100; i++) { |
| 68 | + index("test-idx", "doc", Integer.toString(i), "foo", "bar" + i); |
| 69 | + } |
| 70 | + refresh(); |
| 71 | + assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().totalHits(), equalTo(100L)); |
| 72 | + |
| 73 | + logger.info("--> snapshot"); |
| 74 | + CreateSnapshotResponse createSnapshotResponse = client |
| 75 | + .admin() |
| 76 | + .cluster() |
| 77 | + .prepareCreateSnapshot("test-repo", "test-snap") |
| 78 | + .setWaitForCompletion(true) |
| 79 | + .setIndices("test-idx") |
| 80 | + .get(); |
| 81 | + assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0)); |
| 82 | + int actualTotalShards = createSnapshotResponse.getSnapshotInfo().totalShards(); |
| 83 | + assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(actualTotalShards)); |
| 84 | + |
| 85 | + SnapshotState state = client |
| 86 | + .admin() |
| 87 | + .cluster() |
| 88 | + .prepareGetSnapshots("test-repo") |
| 89 | + .setSnapshots("test-snap") |
| 90 | + .get() |
| 91 | + .getSnapshots() |
| 92 | + .get(0) |
| 93 | + .state(); |
| 94 | + assertThat(state, equalTo(SnapshotState.SUCCESS)); |
| 95 | + |
| 96 | + logger.info("--> delete index"); |
| 97 | + cluster().wipeIndices("test-idx"); |
| 98 | + |
| 99 | + logger.info("--> create read-only URL repository"); |
| 100 | + assertAcked(client.admin().cluster().preparePutRepository("url-repo") |
| 101 | + .setType(URLRepository.TYPE).setSettings(Settings.builder() |
| 102 | + .put(URLRepository.URL_SETTING.getKey(), repositoryLocation.toUri().toURL()) |
| 103 | + .put("list_directories", randomBoolean()))); |
| 104 | + logger.info("--> restore index after deletion"); |
| 105 | + RestoreSnapshotResponse restoreSnapshotResponse = client |
| 106 | + .admin() |
| 107 | + .cluster() |
| 108 | + .prepareRestoreSnapshot("url-repo", "test-snap") |
| 109 | + .setWaitForCompletion(true) |
| 110 | + .setIndices("test-idx") |
| 111 | + .execute() |
| 112 | + .actionGet(); |
| 113 | + assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0)); |
| 114 | + |
| 115 | + assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().totalHits(), equalTo(100L)); |
| 116 | + |
| 117 | + logger.info("--> list available shapshots"); |
| 118 | + GetSnapshotsResponse getSnapshotsResponse = client.admin().cluster().prepareGetSnapshots("url-repo").get(); |
| 119 | + assertThat(getSnapshotsResponse.getSnapshots(), notNullValue()); |
| 120 | + assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(1)); |
| 121 | + |
| 122 | + logger.info("--> delete snapshot"); |
| 123 | + DeleteSnapshotResponse deleteSnapshotResponse = client.admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap").get(); |
| 124 | + assertAcked(deleteSnapshotResponse); |
| 125 | + |
| 126 | + logger.info("--> list available shapshot again, no snapshots should be returned"); |
| 127 | + getSnapshotsResponse = client.admin().cluster().prepareGetSnapshots("url-repo").get(); |
| 128 | + assertThat(getSnapshotsResponse.getSnapshots(), notNullValue()); |
| 129 | + assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(0)); |
| 130 | + } |
| 131 | +} |
0 commit comments