|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.oldrepos7x; |
| 9 | + |
| 10 | +import org.apache.http.HttpHost; |
| 11 | +import org.elasticsearch.client.Request; |
| 12 | +import org.elasticsearch.client.RequestOptions; |
| 13 | +import org.elasticsearch.client.RestClient; |
| 14 | +import org.elasticsearch.client.WarningsHandler; |
| 15 | +import org.elasticsearch.common.Strings; |
| 16 | +import org.elasticsearch.common.xcontent.support.XContentMapValues; |
| 17 | +import org.elasticsearch.test.cluster.ElasticsearchCluster; |
| 18 | +import org.elasticsearch.test.cluster.local.distribution.DistributionType; |
| 19 | +import org.elasticsearch.test.cluster.util.Version; |
| 20 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 21 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 22 | +import org.elasticsearch.xcontent.XContentFactory; |
| 23 | +import org.elasticsearch.xcontent.XContentType; |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.ClassRule; |
| 26 | +import org.junit.rules.RuleChain; |
| 27 | +import org.junit.rules.TemporaryFolder; |
| 28 | +import org.junit.rules.TestRule; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.List; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.stream.Collectors; |
| 35 | + |
| 36 | +public class OldMappingsIT extends ESRestTestCase { |
| 37 | + |
| 38 | + public static TemporaryFolder repoDirectory = new TemporaryFolder(); |
| 39 | + |
| 40 | + public static ElasticsearchCluster currentCluster = ElasticsearchCluster.local() |
| 41 | + .distribution(DistributionType.DEFAULT) |
| 42 | + .distribution(DistributionType.DEFAULT) |
| 43 | + .setting("xpack.security.enabled", "false") |
| 44 | + .setting("xpack.license.self_generated.type", "trial") |
| 45 | + .setting("xpack.ml.enabled", "false") |
| 46 | + .setting("path.repo", () -> repoDirectory.getRoot().getPath()) |
| 47 | + .build(); |
| 48 | + |
| 49 | + public static ElasticsearchCluster oldCluster = ElasticsearchCluster.local() |
| 50 | + .version(Version.fromString("7.17.25")) |
| 51 | + .distribution(DistributionType.DEFAULT) |
| 52 | + .setting("xpack.security.enabled", "false") |
| 53 | + .setting("xpack.license.self_generated.type", "trial") |
| 54 | + .setting("xpack.ml.enabled", "false") |
| 55 | + .setting("path.repo", () -> repoDirectory.getRoot().getPath()) |
| 56 | + .build(); |
| 57 | + |
| 58 | + @ClassRule |
| 59 | + public static TestRule ruleChain = RuleChain.outerRule(repoDirectory).around(oldCluster).around(currentCluster); |
| 60 | + |
| 61 | + @Override |
| 62 | + protected String getTestRestCluster() { |
| 63 | + return oldCluster.getHttpAddresses(); |
| 64 | + } |
| 65 | + |
| 66 | + private Request createIndex(String indexName, String file) throws IOException { |
| 67 | + Request createIndex = new Request("PUT", "/" + indexName); |
| 68 | + int numberOfShards = randomIntBetween(1, 3); |
| 69 | + |
| 70 | + XContentBuilder builder = XContentFactory.jsonBuilder() |
| 71 | + .startObject() |
| 72 | + .startObject("settings") |
| 73 | + .field("index.number_of_shards", numberOfShards) |
| 74 | + .endObject() |
| 75 | + .startObject("mappings"); |
| 76 | + builder.rawValue(getClass().getResourceAsStream(file), XContentType.JSON); |
| 77 | + builder.endObject().endObject(); |
| 78 | + |
| 79 | + createIndex.setJsonEntity(Strings.toString(builder)); |
| 80 | + return createIndex; |
| 81 | + } |
| 82 | + |
| 83 | + @Before |
| 84 | + public void setupIndex() throws IOException { |
| 85 | + // String repoLocation = PathUtils.get(System.getProperty("tests.repo.location")) |
| 86 | + // .resolve(RandomizedTest.getContext().getTargetClass().getName()) |
| 87 | + // .toString(); |
| 88 | + |
| 89 | + String repoLocation = repoDirectory.getRoot().getPath(); |
| 90 | + |
| 91 | + String repoName = "old_mappings_repo"; |
| 92 | + String snapshotName = "snap"; |
| 93 | + List<String> indices; |
| 94 | + indices = Arrays.asList("filebeat", "custom", "nested"); |
| 95 | + |
| 96 | + List<HttpHost> oldClusterHosts = parseClusterHosts(oldCluster.getHttpAddresses()); |
| 97 | + List<HttpHost> clusterHosts = parseClusterHosts(currentCluster.getHttpAddresses()); |
| 98 | + // try (RestClient oldEsClient = client()) { |
| 99 | + try ( |
| 100 | + RestClient oldEsClient = RestClient.builder(oldClusterHosts.toArray(new HttpHost[oldClusterHosts.size()])).build(); |
| 101 | + RestClient esClient = RestClient.builder(clusterHosts.toArray(new HttpHost[clusterHosts.size()])).build(); |
| 102 | + ) { |
| 103 | + assertOK(oldEsClient.performRequest(createIndex("filebeat", "filebeat.json"))); |
| 104 | + |
| 105 | + assertOK(oldEsClient.performRequest(createIndex("custom", "custom.json"))); |
| 106 | + assertOK(oldEsClient.performRequest(createIndex("nested", "nested.json"))); |
| 107 | + |
| 108 | + Request doc1 = new Request("PUT", "/" + "custom" + "/" + "_doc" + "/" + "1"); |
| 109 | + doc1.addParameter("refresh", "true"); |
| 110 | + XContentBuilder bodyDoc1 = XContentFactory.jsonBuilder() |
| 111 | + .startObject() |
| 112 | + .startObject("apache2") |
| 113 | + .startObject("access") |
| 114 | + .field("url", "myurl1") |
| 115 | + .field("agent", "agent1") |
| 116 | + .endObject() |
| 117 | + .endObject() |
| 118 | + .endObject(); |
| 119 | + doc1.setJsonEntity(Strings.toString(bodyDoc1)); |
| 120 | + assertOK(oldEsClient.performRequest(doc1)); |
| 121 | + |
| 122 | + Request doc2 = new Request("PUT", "/" + "custom" + "/" + "_doc" + "/" + "2"); |
| 123 | + doc2.addParameter("refresh", "true"); |
| 124 | + XContentBuilder bodyDoc2 = XContentFactory.jsonBuilder() |
| 125 | + .startObject() |
| 126 | + .startObject("apache2") |
| 127 | + .startObject("access") |
| 128 | + .field("url", "myurl2") |
| 129 | + .field("agent", "agent2 agent2") |
| 130 | + .endObject() |
| 131 | + .endObject() |
| 132 | + .field("completion", "some_value") |
| 133 | + .endObject(); |
| 134 | + doc2.setJsonEntity(Strings.toString(bodyDoc2)); |
| 135 | + assertOK(oldEsClient.performRequest(doc2)); |
| 136 | + |
| 137 | + Request doc3 = new Request("PUT", "/" + "nested" + "/" + "_doc" + "/" + "1"); |
| 138 | + doc3.addParameter("refresh", "true"); |
| 139 | + XContentBuilder bodyDoc3 = XContentFactory.jsonBuilder() |
| 140 | + .startObject() |
| 141 | + .field("group", "fans") |
| 142 | + .startArray("user") |
| 143 | + .startObject() |
| 144 | + .field("first", "John") |
| 145 | + .field("last", "Smith") |
| 146 | + .endObject() |
| 147 | + .startObject() |
| 148 | + .field("first", "Alice") |
| 149 | + .field("last", "White") |
| 150 | + .endObject() |
| 151 | + .endArray() |
| 152 | + .endObject(); |
| 153 | + doc3.setJsonEntity(Strings.toString(bodyDoc3)); |
| 154 | + assertOK(oldEsClient.performRequest(doc3)); |
| 155 | + |
| 156 | + Request getSettingsRequest = new Request("GET", "/_cluster/settings?include_defaults=true"); |
| 157 | + Map<String, Object> response = entityAsMap(oldEsClient.performRequest(getSettingsRequest)); |
| 158 | + assertEquals(repoLocation, ((List<?>) (XContentMapValues.extractValue("defaults.path.repo", response))).get(0)); |
| 159 | + |
| 160 | + // register repo on old ES and take snapshot |
| 161 | + Request createRepoRequest = new Request("PUT", "/_snapshot/" + repoName); |
| 162 | + createRepoRequest.setJsonEntity(Strings.format(""" |
| 163 | + {"type":"fs","settings":{"location":"%s"}} |
| 164 | + """, repoLocation)); |
| 165 | + assertOK(oldEsClient.performRequest(createRepoRequest)); |
| 166 | + |
| 167 | + Request createSnapshotRequest = new Request("PUT", "/_snapshot/" + repoName + "/" + snapshotName); |
| 168 | + createSnapshotRequest.addParameter("wait_for_completion", "true"); |
| 169 | + createSnapshotRequest.setJsonEntity("{\"indices\":\"" + indices.stream().collect(Collectors.joining(",")) + "\"}"); |
| 170 | + assertOK(oldEsClient.performRequest(createSnapshotRequest)); |
| 171 | + // } |
| 172 | + |
| 173 | + // register repo on new ES and restore snapshot |
| 174 | + Request createRepoRequest2 = new Request("PUT", "/_snapshot/" + repoName); |
| 175 | + createRepoRequest2.setJsonEntity(Strings.format(""" |
| 176 | + {"type":"fs","settings":{"location":"%s"}} |
| 177 | + """, repoLocation)); |
| 178 | + assertOK(esClient.performRequest(createRepoRequest2)); |
| 179 | + |
| 180 | + final Request createRestoreRequest = new Request("POST", "/_snapshot/" + repoName + "/" + snapshotName + "/_restore"); |
| 181 | + createRestoreRequest.addParameter("wait_for_completion", "true"); |
| 182 | + createRestoreRequest.setJsonEntity("{\"indices\":\"" + indices.stream().collect(Collectors.joining(",")) + "\"}"); |
| 183 | + createRestoreRequest.setOptions(RequestOptions.DEFAULT.toBuilder().setWarningsHandler(WarningsHandler.PERMISSIVE)); |
| 184 | + assertOK(esClient.performRequest(createRestoreRequest)); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + public void testMappingOk() throws IOException { |
| 189 | + // Request mappingRequest = new Request("GET", "/" + "filebeat" + "/_mapping"); |
| 190 | + // Map<String, Object> mapping = entityAsMap(client().performRequest(mappingRequest)); |
| 191 | + // assertNotNull(XContentMapValues.extractValue(mapping, "filebeat", "mappings", "properties", "apache2")); |
| 192 | + } |
| 193 | + |
| 194 | +} |
0 commit comments