Skip to content

Commit 371229a

Browse files
committed
applied test feedback
1 parent 29df0bc commit 371229a

File tree

1 file changed

+54
-22
lines changed

1 file changed

+54
-22
lines changed

x-pack/plugin/logsdb/src/javaRestTest/java/org/elasticsearch/xpack/logsdb/LogsdbSnapshotRestoreIT.java

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,18 @@
1010
import org.apache.http.client.methods.HttpPut;
1111
import org.elasticsearch.client.Request;
1212
import org.elasticsearch.client.Response;
13-
import org.elasticsearch.client.RestClient;
1413
import org.elasticsearch.common.network.InetAddresses;
1514
import org.elasticsearch.common.settings.Settings;
1615
import org.elasticsearch.common.time.DateFormatter;
1716
import org.elasticsearch.common.time.FormatNames;
17+
import org.elasticsearch.common.xcontent.XContentHelper;
1818
import org.elasticsearch.core.SuppressForbidden;
1919
import org.elasticsearch.repositories.fs.FsRepository;
2020
import org.elasticsearch.test.cluster.ElasticsearchCluster;
2121
import org.elasticsearch.test.cluster.local.distribution.DistributionType;
2222
import org.elasticsearch.test.rest.ESRestTestCase;
2323
import org.elasticsearch.test.rest.ObjectPath;
24+
import org.elasticsearch.xcontent.XContentType;
2425
import org.junit.After;
2526
import org.junit.ClassRule;
2627
import org.junit.rules.RuleChain;
@@ -34,6 +35,8 @@
3435
import java.util.Locale;
3536
import java.util.Map;
3637

38+
import static org.elasticsearch.test.MapMatcher.assertMap;
39+
import static org.elasticsearch.test.MapMatcher.matchesMap;
3740
import static org.hamcrest.Matchers.containsString;
3841
import static org.hamcrest.Matchers.empty;
3942
import static org.hamcrest.Matchers.equalTo;
@@ -78,11 +81,15 @@ public class LogsdbSnapshotRestoreIT extends ESRestTestCase {
7881
"@timestamp" : {
7982
"type": "date"
8083
},
81-
"host.name": {
82-
"type": "keyword"
84+
"host": {
85+
"properties": {
86+
"name": {
87+
"type": "keyword"
88+
}
89+
}
8390
},
8491
"pid": {
85-
"type": "long"
92+
"type": "integer"
8693
},
8794
"method": {
8895
"type": "keyword"
@@ -104,8 +111,8 @@ public class LogsdbSnapshotRestoreIT extends ESRestTestCase {
104111
static final String DOC_TEMPLATE = """
105112
{
106113
"@timestamp": "%s",
107-
"host.name": "%s",
108-
"pid": "%d",
114+
"host": { "name": "%s"},
115+
"pid": %d,
109116
"method": "%s",
110117
"message": "%s",
111118
"ip_address": "%s",
@@ -179,22 +186,22 @@ static void snapshotAndRestore(String sourceMode, String arrayType, boolean sour
179186
}
180187

181188
putTemplate("my-template", LOGS_TEMPLATE.replace("{{source_mode}}", sourceMode).replace("{{array_type}}", arrayType));
189+
String[] docs = new String[100];
182190
for (int i = 0; i < 100; i++) {
183-
indexDocument(
184-
dataStreamName,
185-
document(
186-
Instant.now(),
187-
randomAlphaOfLength(10),
188-
randomNonNegativeLong(),
189-
randomFrom("PUT", "POST", "GET"),
190-
randomAlphaOfLength(32),
191-
randomIp(randomBoolean()),
192-
randomLongBetween(1_000_000L, 2_000_000L)
193-
)
191+
docs[i] = document(
192+
Instant.now(),
193+
String.format(Locale.ROOT, "host-%03d", i),
194+
randomNonNegativeInt(),
195+
randomFrom("PUT", "POST", "GET"),
196+
randomAlphaOfLength(32),
197+
randomIp(randomBoolean()),
198+
randomLongBetween(1_000_000L, 2_000_000L)
194199
);
200+
indexDocument(dataStreamName, docs[i]);
195201
}
196202
refresh(dataStreamName);
197203
assertDocCount(client(), dataStreamName, 100);
204+
assertSource(dataStreamName, docs);
198205
assertDataStream(dataStreamName, sourceMode);
199206

200207
String snapshotName = "my-snapshot";
@@ -205,11 +212,12 @@ static void snapshotAndRestore(String sourceMode, String arrayType, boolean sour
205212
List<?> failures = (List<?>) snapshotItem.get("failures");
206213
assertThat(failures, empty());
207214
deleteDataStream(dataStreamName);
208-
assertDocCount(client(), dataStreamName, 0);
215+
assertDocCount(dataStreamName, 0);
209216

210217
restoreSnapshot(repositoryName, snapshotName, true);
211218
assertDataStream(dataStreamName, sourceMode);
212-
assertDocCount(client(), dataStreamName, 100);
219+
assertDocCount(dataStreamName, 100);
220+
assertSource(dataStreamName, docs);
213221
}
214222

215223
static void snapshotAndFail(String arrayType) throws IOException {
@@ -234,7 +242,7 @@ static void snapshotAndFail(String arrayType) throws IOException {
234242
randomFrom("PUT", "POST", "GET"),
235243
randomAlphaOfLength(32),
236244
randomIp(randomBoolean()),
237-
randomLongBetween(1_000_000L, 2_000_000L)
245+
randomIntBetween(1_000_000, 2_000_000)
238246
)
239247
);
240248
}
@@ -326,17 +334,41 @@ static String getWriteBackingIndex(String dataStreamName, int backingIndex) thro
326334
return (String) ((Map<?, ?>) backingIndices.get(backingIndex)).get("index_name");
327335
}
328336

329-
public static void assertDocCount(RestClient client, String indexName, long docCount) throws IOException {
337+
static void assertDocCount(String indexName, long docCount) throws IOException {
330338
Request countReq = new Request("GET", "/" + indexName + "/_count");
331339
countReq.addParameter("ignore_unavailable", "true");
332-
ObjectPath resp = ObjectPath.createFromResponse(client.performRequest(countReq));
340+
ObjectPath resp = ObjectPath.createFromResponse(client().performRequest(countReq));
333341
assertEquals(
334342
"expected " + docCount + " documents but it was a different number",
335343
docCount,
336344
Long.parseLong(resp.evaluate("count").toString())
337345
);
338346
}
339347

348+
static void assertSource(String indexName, String[] docs) throws IOException {
349+
Request searchReq = new Request("GET", "/" + indexName + "/_search");
350+
searchReq.addParameter("size", String.valueOf(docs.length));
351+
var response = client().performRequest(searchReq);
352+
assertOK(response);
353+
var responseBody = entityAsMap(response);
354+
List<?> hits = (List<?>) ((Map<?, ?>) responseBody.get("hits")).get("hits");
355+
assertThat(hits, hasSize(docs.length));
356+
for (Object hit : hits) {
357+
Map<?, ?> actualSource = (Map<?, ?>) ((Map<?, ?>) hit).get("_source");
358+
String actualHost = (String) ((Map<?, ?>) actualSource.get("host")).get("name");
359+
Map<?, ?> expectedSource = null;
360+
for (String doc : docs) {
361+
expectedSource = XContentHelper.convertToMap(XContentType.JSON.xContent(), doc, false);
362+
String expectedHost = (String) ((Map<?, ?>) expectedSource.get("host")).get("name");
363+
if (expectedHost.equals(actualHost)) {
364+
break;
365+
}
366+
}
367+
368+
assertMap(actualSource, matchesMap(expectedSource));
369+
}
370+
}
371+
340372
@SuppressForbidden(reason = "TemporaryFolder only has io.File methods, not nio.File")
341373
private static String getRepoPath() {
342374
return repoDirectory.getRoot().getPath();

0 commit comments

Comments
 (0)