Skip to content

Commit 9919159

Browse files
committed
Add DocValueOnlyFieldsIT yaml rest test
1 parent d4be0ac commit 9919159

File tree

2 files changed

+213
-0
lines changed

2 files changed

+213
-0
lines changed
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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 com.carrotsearch.randomizedtesting.annotations.Name;
11+
import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;
12+
13+
import org.apache.http.HttpHost;
14+
import org.elasticsearch.Version;
15+
import org.elasticsearch.client.Request;
16+
import org.elasticsearch.client.RestClient;
17+
import org.elasticsearch.common.Strings;
18+
import org.elasticsearch.test.cluster.ElasticsearchCluster;
19+
import org.elasticsearch.test.cluster.local.distribution.DistributionType;
20+
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
21+
import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;
22+
import org.elasticsearch.xcontent.XContentBuilder;
23+
import org.elasticsearch.xcontent.XContentFactory;
24+
import org.junit.Before;
25+
import org.junit.BeforeClass;
26+
import org.junit.ClassRule;
27+
import org.junit.rules.RuleChain;
28+
import org.junit.rules.TemporaryFolder;
29+
import org.junit.rules.TestRule;
30+
31+
import java.io.IOException;
32+
33+
/**
34+
* Tests doc-value-based searches against indices imported from clusters older than N-1.
35+
* We reuse the YAML tests in search/390_doc_values_search.yml but have to do the setup
36+
* manually here as the setup is done on the old cluster for which we have to use the
37+
* low-level REST client instead of the YAML set up that only knows how to talk to
38+
* newer ES versions.
39+
*
40+
* We mimic the setup in search/390_doc_values_search.yml here, but adapt it to work
41+
* against older version clusters.
42+
*/
43+
public class DocValueOnlyFieldsIT extends ESClientYamlSuiteTestCase {
44+
45+
static final Version oldVersion = Version.fromString(System.getProperty("tests.old_cluster_version"));
46+
static boolean setupDone;
47+
48+
public static TemporaryFolder repoDirectory = new TemporaryFolder();
49+
50+
public static ElasticsearchCluster currentCluster = ElasticsearchCluster.local()
51+
.distribution(DistributionType.DEFAULT)
52+
.nodes(2)
53+
.setting("xpack.security.enabled", "false")
54+
.setting("xpack.license.self_generated.type", "trial")
55+
.setting("xpack.ml.enabled", "false")
56+
.setting("path.repo", () -> repoDirectory.getRoot().getPath())
57+
.setting("xpack.searchable.snapshot.shared_cache.size", "16MB")
58+
.setting("xpack.searchable.snapshot.shared_cache.region_size", "256KB")
59+
.build();
60+
61+
public static ElasticsearchCluster oldCluster = ElasticsearchCluster.local()
62+
.version(org.elasticsearch.test.cluster.util.Version.fromString(System.getProperty("tests.old_cluster_version")))
63+
.distribution(DistributionType.DEFAULT)
64+
.nodes(2)
65+
.setting("xpack.security.enabled", "false")
66+
.setting("xpack.license.self_generated.type", "trial")
67+
.setting("xpack.ml.enabled", "false")
68+
.setting("path.repo", () -> repoDirectory.getRoot().getPath())
69+
.build();
70+
71+
@ClassRule
72+
public static TestRule ruleChain = RuleChain.outerRule(repoDirectory).around(oldCluster).around(currentCluster);
73+
private static final String REPO_NAME = "doc_values_repo";
74+
private static final String INDEX_NAME = "test";
75+
private static final String snapshotName = "snap";
76+
77+
private static String repoLocation;
78+
79+
public DocValueOnlyFieldsIT(@Name("yaml") ClientYamlTestCandidate testCandidate) {
80+
super(testCandidate);
81+
}
82+
83+
@ParametersFactory
84+
public static Iterable<Object[]> parameters() throws Exception {
85+
return ESClientYamlSuiteTestCase.createParameters();
86+
}
87+
88+
@Override
89+
protected boolean preserveClusterUponCompletion() {
90+
return true;
91+
}
92+
93+
@Override
94+
protected boolean skipSetupSections() {
95+
// setup in the YAML file is replaced by the method below
96+
return true;
97+
}
98+
99+
@BeforeClass
100+
public static void setupSnapshot() throws IOException {
101+
repoLocation = repoDirectory.getRoot().getPath();
102+
String[] basicTypes = new String[] {
103+
"byte",
104+
"double",
105+
"float",
106+
"half_float",
107+
"integer",
108+
"long",
109+
"short",
110+
"boolean",
111+
"keyword",
112+
"ip",
113+
"geo_point" }; // date is manually added as it need further configuration
114+
115+
int oldEsPort = Integer.parseInt(System.getProperty("tests.es.port"));
116+
try (RestClient oldEs = RestClient.builder(new HttpHost("127.0.0.1", oldEsPort)).build()) {
117+
Request createIndex = new Request("PUT", "/" + INDEX_NAME);
118+
int numberOfShards = randomIntBetween(1, 3);
119+
120+
XContentBuilder settingsBuilder = XContentFactory.jsonBuilder()
121+
.startObject()
122+
.startObject("settings")
123+
.field("index.number_of_shards", numberOfShards)
124+
.endObject()
125+
.startObject("mappings");
126+
settingsBuilder.field("dynamic", false).startObject("properties");
127+
for (String type : basicTypes) {
128+
settingsBuilder.startObject(type).field("type", type).endObject();
129+
}
130+
settingsBuilder.startObject("date").field("type", "date").field("format", "yyyy/MM/dd").endObject();
131+
settingsBuilder.endObject().endObject().endObject();
132+
133+
createIndex.setJsonEntity(Strings.toString(settingsBuilder));
134+
assertOK(oldEs.performRequest(createIndex));
135+
136+
Request doc1 = new Request("PUT", "/" + INDEX_NAME + "/" + "doc" + "/" + "1");
137+
doc1.addParameter("refresh", "true");
138+
XContentBuilder bodyDoc1 = XContentFactory.jsonBuilder()
139+
.startObject()
140+
.field("byte", 1)
141+
.field("double", 1.0)
142+
.field("float", 1.0)
143+
.field("half_float", 1.0)
144+
.field("integer", 1)
145+
.field("long", 1)
146+
.field("short", 1)
147+
.field("date", "2017/01/01")
148+
.field("keyword", "key1")
149+
.field("boolean", false)
150+
.field("ip", "192.168.0.1")
151+
.array("geo_point", 13.5, 34.89)
152+
.endObject();
153+
doc1.setJsonEntity(Strings.toString(bodyDoc1));
154+
assertOK(oldEs.performRequest(doc1));
155+
156+
Request doc2 = new Request("PUT", "/" + INDEX_NAME + "/" + "doc" + "/" + "2");
157+
doc2.addParameter("refresh", "true");
158+
XContentBuilder bodyDoc2 = XContentFactory.jsonBuilder()
159+
.startObject()
160+
.field("byte", 2)
161+
.field("double", 2.0)
162+
.field("float", 2.0)
163+
.field("half_float", 2.0)
164+
.field("integer", 2)
165+
.field("long", 2)
166+
.field("short", 2)
167+
.field("date", "2017/01/02")
168+
.field("keyword", "key2")
169+
.field("boolean", true)
170+
.field("ip", "192.168.0.2")
171+
.array("geo_point", -63.24, 31.0)
172+
.endObject();
173+
doc2.setJsonEntity(Strings.toString(bodyDoc2));
174+
assertOK(oldEs.performRequest(doc2));
175+
176+
// register repo on old ES and take snapshot
177+
Request createRepoRequest = new Request("PUT", "/_snapshot/" + REPO_NAME);
178+
createRepoRequest.setJsonEntity(Strings.format("""
179+
{"type":"fs","settings":{"location":"%s"}}
180+
""", repoLocation));
181+
assertOK(oldEs.performRequest(createRepoRequest));
182+
183+
Request createSnapshotRequest = new Request("PUT", "/_snapshot/" + REPO_NAME + "/" + snapshotName);
184+
createSnapshotRequest.addParameter("wait_for_completion", "true");
185+
createSnapshotRequest.setJsonEntity("{\"indices\":\"" + INDEX_NAME + "\"}");
186+
assertOK(oldEs.performRequest(createSnapshotRequest));
187+
}
188+
}
189+
190+
@Before
191+
public void registerAndRestoreRepo() throws IOException {
192+
// The following is bit of a hack. While we wish we could make this an @BeforeClass, it does not work because the client() is only
193+
// initialized later, so we do it when running the first test
194+
if (setupDone = false) {
195+
// register repo on new ES and restore snapshot
196+
Request createRepoRequest2 = new Request("PUT", "/_snapshot/" + REPO_NAME);
197+
createRepoRequest2.setJsonEntity(Strings.format("""
198+
{"type":"fs","settings":{"location":"%s"}}
199+
""", repoLocation));
200+
assertOK(client().performRequest(createRepoRequest2));
201+
202+
final Request createRestoreRequest = new Request("POST", "/_snapshot/" + REPO_NAME + "/" + snapshotName + "/_restore");
203+
createRestoreRequest.addParameter("wait_for_completion", "true");
204+
createRestoreRequest.setJsonEntity("{\"indices\":\"" + INDEX_NAME + "\"}");
205+
assertOK(client().performRequest(createRestoreRequest));
206+
207+
setupDone = true;
208+
}
209+
}
210+
}

x-pack/qa/repository-old-versions-7x/src/javaRestTest/java/org/elasticsearch/oldrepos7x/OldMappingsIT.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ public static void setupOldRepo() throws IOException {
179179

180180
@Before
181181
public void registerAndRestoreRepo() throws IOException {
182+
// this would ideally also happen in @BeforeClass and just once, but we don't have the current cluster client()
183+
// there yet. So we do it before tests here and make sure to only restore the repo once.
184+
// Goes together with the empty "wipeSnapshot()" override in this test.
182185
if (repoRestored == false) {
183186
// register repo on new ES and restore snapshot
184187
Request createRepoRequest2 = new Request("PUT", "/_snapshot/" + repoName);

0 commit comments

Comments
 (0)