|
| 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 | +package org.elasticsearch.bwcompat; |
| 20 | + |
| 21 | +import org.apache.lucene.util.LuceneTestCase.Slow; |
| 22 | +import org.elasticsearch.Version; |
| 23 | +import org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse; |
| 24 | +import org.elasticsearch.action.search.SearchResponse; |
| 25 | +import org.elasticsearch.cluster.ClusterState; |
| 26 | +import org.elasticsearch.cluster.metadata.IndexMetaData; |
| 27 | +import org.elasticsearch.cluster.metadata.IndexTemplateMetaData; |
| 28 | +import org.elasticsearch.cluster.routing.allocation.decider.FilterAllocationDecider; |
| 29 | +import org.elasticsearch.common.settings.ImmutableSettings; |
| 30 | +import org.elasticsearch.rest.RestStatus; |
| 31 | +import org.elasticsearch.snapshots.AbstractSnapshotTests; |
| 32 | +import org.elasticsearch.snapshots.RestoreInfo; |
| 33 | +import org.elasticsearch.test.ElasticsearchIntegrationTest.ClusterScope; |
| 34 | +import org.elasticsearch.test.ElasticsearchIntegrationTest.Scope; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +import java.io.IOException; |
| 38 | +import java.lang.reflect.Modifier; |
| 39 | +import java.net.URI; |
| 40 | +import java.nio.file.DirectoryStream; |
| 41 | +import java.nio.file.Files; |
| 42 | +import java.nio.file.Path; |
| 43 | +import java.nio.file.Paths; |
| 44 | +import java.util.List; |
| 45 | +import java.util.Locale; |
| 46 | +import java.util.SortedSet; |
| 47 | +import java.util.TreeSet; |
| 48 | + |
| 49 | +import static com.google.common.collect.Lists.newArrayList; |
| 50 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 51 | +import static org.hamcrest.Matchers.*; |
| 52 | + |
| 53 | +@Slow |
| 54 | +@ClusterScope(scope = Scope.TEST) |
| 55 | +public class RestoreBackwardsCompatTests extends AbstractSnapshotTests { |
| 56 | + |
| 57 | + |
| 58 | + @Test |
| 59 | + public void restoreOldSnapshots() throws Exception { |
| 60 | + String repo = "test_repo"; |
| 61 | + String snapshot = "test_1"; |
| 62 | + List<String> repoVersions = repoVersions(); |
| 63 | + assertThat(repoVersions.size(), greaterThan(0)); |
| 64 | + for (String version : repoVersions) { |
| 65 | + createRepo(version, repo); |
| 66 | + testOldSnapshot(version, repo, snapshot); |
| 67 | + } |
| 68 | + |
| 69 | + SortedSet<String> expectedVersions = new TreeSet<>(); |
| 70 | + for (java.lang.reflect.Field field : Version.class.getDeclaredFields()) { |
| 71 | + if (Modifier.isStatic(field.getModifiers()) && field.getType() == Version.class) { |
| 72 | + Version v = (Version) field.get(Version.class); |
| 73 | + if (v.snapshot()) continue; |
| 74 | + if (v.onOrBefore(Version.V_1_0_0_Beta1)) continue; |
| 75 | + |
| 76 | + expectedVersions.add(v.toString()); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + for (String repoVersion : repoVersions) { |
| 81 | + if (expectedVersions.remove(repoVersion) == false) { |
| 82 | + logger.warn("Old repositories tests contain extra repo: " + repoVersion); |
| 83 | + } |
| 84 | + } |
| 85 | + if (expectedVersions.isEmpty() == false) { |
| 86 | + StringBuilder msg = new StringBuilder("Old repositories tests are missing versions:"); |
| 87 | + for (String expected : expectedVersions) { |
| 88 | + msg.append("\n" + expected); |
| 89 | + } |
| 90 | + fail(msg.toString()); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public static List<String> repoVersions() throws Exception { |
| 95 | + List<String> repoVersions = newArrayList(); |
| 96 | + Path repoFiles = Paths.get(RestoreBackwardsCompatTests.class.getResource(".").toURI()); |
| 97 | + try (DirectoryStream<Path> stream = Files.newDirectoryStream(repoFiles, "repo-*.zip")) { |
| 98 | + for (Path entry : stream) { |
| 99 | + String fileName = entry.getFileName().toString(); |
| 100 | + String version = fileName.substring("repo-".length()); |
| 101 | + version = version.substring(0, version.length() - ".zip".length()); |
| 102 | + repoVersions.add(version); |
| 103 | + } |
| 104 | + } |
| 105 | + return repoVersions; |
| 106 | + } |
| 107 | + |
| 108 | + private void createRepo(String version, String repo) throws Exception { |
| 109 | + String repoFile = "repo-" + version + ".zip"; |
| 110 | + URI repoFileUri = getClass().getResource(repoFile).toURI(); |
| 111 | + URI repoJarUri = new URI("jar:" + repoFileUri.toString() + "!/repo/"); |
| 112 | + logger.info("--> creating repository [{}] for version [{}]", repo, version); |
| 113 | + assertAcked(client().admin().cluster().preparePutRepository(repo) |
| 114 | + .setType("url").setSettings(ImmutableSettings.settingsBuilder() |
| 115 | + .put("url", repoJarUri.toString()))); |
| 116 | + } |
| 117 | + |
| 118 | + private void testOldSnapshot(String version, String repo, String snapshot) throws IOException { |
| 119 | + logger.info("--> restoring snapshot"); |
| 120 | + RestoreSnapshotResponse response = client().admin().cluster().prepareRestoreSnapshot(repo, snapshot).setRestoreGlobalState(true).setWaitForCompletion(true).get(); |
| 121 | + assertThat(response.status(), equalTo(RestStatus.OK)); |
| 122 | + RestoreInfo restoreInfo = response.getRestoreInfo(); |
| 123 | + assertThat(restoreInfo.successfulShards(), greaterThan(0)); |
| 124 | + assertThat(restoreInfo.successfulShards(), equalTo(restoreInfo.totalShards())); |
| 125 | + assertThat(restoreInfo.failedShards(), equalTo(0)); |
| 126 | + String index = restoreInfo.indices().get(0); |
| 127 | + |
| 128 | + logger.info("--> check search"); |
| 129 | + SearchResponse searchResponse = client().prepareSearch(index).get(); |
| 130 | + assertThat(searchResponse.getHits().totalHits(), greaterThan(1L)); |
| 131 | + |
| 132 | + logger.info("--> check settings"); |
| 133 | + ClusterState clusterState = client().admin().cluster().prepareState().get().getState(); |
| 134 | + assertThat(clusterState.metaData().persistentSettings().get(FilterAllocationDecider.CLUSTER_ROUTING_EXCLUDE_GROUP + "version_attr"), equalTo(version)); |
| 135 | + |
| 136 | + logger.info("--> check templates"); |
| 137 | + IndexTemplateMetaData template = clusterState.getMetaData().templates().get("template_" + version.toLowerCase(Locale.ROOT)); |
| 138 | + assertThat(template, notNullValue()); |
| 139 | + assertThat(template.template(), equalTo("te*")); |
| 140 | + assertThat(template.settings().getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, -1), equalTo(1)); |
| 141 | + assertThat(template.mappings().size(), equalTo(1)); |
| 142 | + assertThat(template.mappings().get("type1").string(), equalTo("{\"type1\":{\"_source\":{\"enabled\":false}}}")); |
| 143 | + if (Version.fromString(version).onOrAfter(Version.V_1_1_0)) { |
| 144 | + // Support for aliases in templates was added in v1.1.0 |
| 145 | + assertThat(template.aliases().size(), equalTo(3)); |
| 146 | + assertThat(template.aliases().get("alias1"), notNullValue()); |
| 147 | + assertThat(template.aliases().get("alias2").filter().string(), containsString(version)); |
| 148 | + assertThat(template.aliases().get("alias2").indexRouting(), equalTo("kimchy")); |
| 149 | + assertThat(template.aliases().get("{index}-alias"), notNullValue()); |
| 150 | + } |
| 151 | + |
| 152 | + logger.info("--> cleanup"); |
| 153 | + cluster().wipeIndices(restoreInfo.indices().toArray(new String[restoreInfo.indices().size()])); |
| 154 | + cluster().wipeTemplates(); |
| 155 | + |
| 156 | + } |
| 157 | +} |
| 158 | + |
0 commit comments