|
| 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 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.oldcodecs; |
| 10 | + |
| 11 | +import org.apache.http.HttpHost; |
| 12 | +import org.apache.http.util.EntityUtils; |
| 13 | +import org.elasticsearch.client.Request; |
| 14 | +import org.elasticsearch.client.Response; |
| 15 | +import org.elasticsearch.client.RestClient; |
| 16 | +import org.elasticsearch.common.io.Streams; |
| 17 | +import org.elasticsearch.core.Booleans; |
| 18 | +import org.elasticsearch.test.rest.ESRestTestCase; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | + |
| 22 | +import static org.hamcrest.Matchers.containsString; |
| 23 | + |
| 24 | +public class BWCCodecIT extends ESRestTestCase { |
| 25 | + private static final int DOCS = 5; |
| 26 | + |
| 27 | + private void oldEsTestCase(String portPropertyName, String requestsPerSecond) throws IOException { |
| 28 | + boolean enabled = Booleans.parseBoolean(System.getProperty("tests.fromOld")); |
| 29 | + assumeTrue("test is disabled, probably because this is windows", enabled); |
| 30 | + String repoLocation = System.getProperty("tests.repoLocation"); |
| 31 | + repoLocation = repoLocation + "/" + randomAlphaOfLength(10); |
| 32 | + |
| 33 | + int oldEsPort = Integer.parseInt(System.getProperty(portPropertyName)); |
| 34 | + try (RestClient oldEs = RestClient.builder(new HttpHost("127.0.0.1", oldEsPort)).build()) { |
| 35 | + try { |
| 36 | + Request createIndex = new Request("PUT", "/test"); |
| 37 | + createIndex.setJsonEntity("{\"settings\":{\"number_of_shards\": 1}}"); |
| 38 | + oldEs.performRequest(createIndex); |
| 39 | + |
| 40 | + for (int i = 0; i < DOCS; i++) { |
| 41 | + Request doc = new Request("PUT", "/test/doc/testdoc" + i); |
| 42 | + doc.addParameter("refresh", "true"); |
| 43 | + doc.setJsonEntity("{\"test\":\"test" + i + "\"}"); |
| 44 | + oldEs.performRequest(doc); |
| 45 | + } |
| 46 | + |
| 47 | + // register repo on old ES and take snapshot |
| 48 | + Request createRepoRequest = new Request("PUT", "/_snapshot/testrepo"); |
| 49 | + createRepoRequest.setJsonEntity("{\"type\":\"fs\",\"settings\":{\"location\":\"" + repoLocation + "\"}}"); |
| 50 | + oldEs.performRequest(createRepoRequest); |
| 51 | + |
| 52 | + Request createSnapshotRequest = new Request("PUT", "/_snapshot/testrepo/snap1"); |
| 53 | + createSnapshotRequest.addParameter("wait_for_completion", "true"); |
| 54 | + oldEs.performRequest(createSnapshotRequest); |
| 55 | + |
| 56 | + // register repo on new ES |
| 57 | + Request createReadRepoRequest = new Request("PUT", "/_snapshot/testrepo"); |
| 58 | + createReadRepoRequest.setJsonEntity( |
| 59 | + "{\"type\":\"lucene5\",\"settings\":{\"delegate_type\":\"fs\",\"location\":\"" + repoLocation + |
| 60 | + "\",\"readonly\":true}}"); |
| 61 | + client().performRequest(createReadRepoRequest); |
| 62 | + |
| 63 | + // list snapshots on new ES |
| 64 | + Request listSnapshotsRequest = new Request("GET", "/_snapshot/testrepo/snap1"); |
| 65 | + listSnapshotsRequest.addParameter("error_trace", "true"); |
| 66 | + Response listSnapshotsResponse = client().performRequest(listSnapshotsRequest); |
| 67 | + logger.info(Streams.readFully(listSnapshotsResponse.getEntity().getContent()).utf8ToString()); |
| 68 | + assertEquals(200, listSnapshotsResponse.getStatusLine().getStatusCode()); |
| 69 | + |
| 70 | + // list advanced snapshot info on new ES |
| 71 | + Request listSnapshotStatusRequest = new Request("GET", "/_snapshot/testrepo/snap1/_status"); |
| 72 | + listSnapshotStatusRequest.addParameter("error_trace", "true"); |
| 73 | + Response listSnapshotStatusResponse = client().performRequest(listSnapshotStatusRequest); |
| 74 | + logger.info(Streams.readFully(listSnapshotStatusResponse.getEntity().getContent()).utf8ToString()); |
| 75 | + assertEquals(200, listSnapshotStatusResponse.getStatusLine().getStatusCode()); |
| 76 | + |
| 77 | + // restore snapshot on new ES |
| 78 | + Request restoreSnapshotRequest = new Request("POST", "/_snapshot/testrepo/snap1/_restore"); |
| 79 | + restoreSnapshotRequest.addParameter("error_trace", "true"); |
| 80 | + restoreSnapshotRequest.addParameter("wait_for_completion", "true"); |
| 81 | + restoreSnapshotRequest.setJsonEntity("{\"indices\":\"test\"}"); |
| 82 | + Response restoreSnapshotResponse = client().performRequest(restoreSnapshotRequest); |
| 83 | + logger.info(Streams.readFully(restoreSnapshotResponse.getEntity().getContent()).utf8ToString()); |
| 84 | + assertEquals(200, restoreSnapshotResponse.getStatusLine().getStatusCode()); |
| 85 | + |
| 86 | + // run a search against the restored index |
| 87 | + Request search = new Request("POST", "/test/_search"); |
| 88 | + search.addParameter("pretty", "true"); |
| 89 | + //search.setJsonEntity("{\"stored_fields\": [\"_uid\", \"_source\"]}"); |
| 90 | + Response response = client().performRequest(search); |
| 91 | + String result = EntityUtils.toString(response.getEntity()); |
| 92 | + for (int i = 0; i < DOCS; i++) { |
| 93 | + // check that source_ is present |
| 94 | + assertThat(result, containsString("\"test\" : \"test" + i + "\"")); |
| 95 | + // check that _id is present |
| 96 | + // TODO: _id is stored as _uid in older ES versions, not _id (can extract _type and _id from that) |
| 97 | + // assertThat(result, containsString("\"_id\" : \"testdoc" + i + "\"")); |
| 98 | + } |
| 99 | + |
| 100 | + } finally { |
| 101 | + oldEs.performRequest(new Request("DELETE", "/test")); |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public void testEs2() throws IOException { |
| 107 | + oldEsTestCase("es2.port", null); |
| 108 | + } |
| 109 | + |
| 110 | + public void testEs1() throws IOException { |
| 111 | + oldEsTestCase("es1.port", null); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments