Skip to content

Commit 658fb58

Browse files
committed
reduce duplication
1 parent 1bbbba4 commit 658fb58

File tree

9 files changed

+639
-1339
lines changed

9 files changed

+639
-1339
lines changed

instrumentation/elasticsearch/elasticsearch-transport-5.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/elasticsearch/transport/v5_0/Elasticsearch5NodeClientTest.java

Lines changed: 5 additions & 231 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,27 @@
55

66
package io.opentelemetry.javaagent.instrumentation.elasticsearch.transport.v5_0;
77

8-
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
9-
import static org.assertj.core.api.Assertions.assertThat;
10-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
118
import static org.elasticsearch.cluster.ClusterName.CLUSTER_NAME_SETTING;
12-
import static org.junit.jupiter.api.Named.named;
139

14-
import io.opentelemetry.api.common.AttributeKey;
15-
import io.opentelemetry.api.trace.SpanKind;
16-
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
17-
import io.opentelemetry.instrumentation.testing.util.ThrowingSupplier;
18-
import io.opentelemetry.javaagent.instrumentation.elasticsearch.transport.AbstractElasticsearchClientTest;
19-
import io.opentelemetry.sdk.trace.data.StatusData;
20-
import io.opentelemetry.semconv.incubating.DbIncubatingAttributes;
10+
import io.opentelemetry.javaagent.instrumentation.elasticsearch.transport.AbstractElasticsearchNodeClientTest;
2111
import java.io.File;
2212
import java.util.Collections;
2313
import java.util.UUID;
24-
import java.util.stream.Stream;
25-
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
26-
import org.elasticsearch.action.get.GetResponse;
27-
import org.elasticsearch.action.index.IndexResponse;
2814
import org.elasticsearch.client.Client;
29-
import org.elasticsearch.cluster.health.ClusterHealthStatus;
3015
import org.elasticsearch.common.settings.Settings;
3116
import org.elasticsearch.env.Environment;
32-
import org.elasticsearch.index.IndexNotFoundException;
3317
import org.elasticsearch.node.Node;
3418
import org.elasticsearch.node.internal.InternalSettingsPreparer;
3519
import org.elasticsearch.transport.Netty3Plugin;
3620
import org.junit.jupiter.api.AfterAll;
3721
import org.junit.jupiter.api.BeforeAll;
38-
import org.junit.jupiter.api.Test;
39-
import org.junit.jupiter.api.TestInstance;
40-
import org.junit.jupiter.api.extension.RegisterExtension;
4122
import org.junit.jupiter.api.io.TempDir;
42-
import org.junit.jupiter.params.ParameterizedTest;
43-
import org.junit.jupiter.params.provider.Arguments;
44-
import org.junit.jupiter.params.provider.MethodSource;
4523
import org.slf4j.Logger;
4624
import org.slf4j.LoggerFactory;
4725

48-
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
49-
class Elasticsearch5NodeClientTest extends AbstractElasticsearchClientTest {
26+
class Elasticsearch5NodeClientTest extends AbstractElasticsearchNodeClientTest {
5027
private static final Logger logger = LoggerFactory.getLogger(Elasticsearch5NodeClientTest.class);
5128

52-
@RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create();
53-
5429
private static final String clusterName = UUID.randomUUID().toString();
5530
private static Node testNode;
5631
private static Client client;
@@ -104,209 +79,8 @@ public Client client() {
10479
return client;
10580
}
10681

107-
private Stream<Arguments> healthArguments() {
108-
return Stream.of(
109-
Arguments.of(
110-
named(
111-
"sync",
112-
(ThrowingSupplier<ClusterHealthStatus, Exception>) this::clusterHealthSync)),
113-
Arguments.of(
114-
named(
115-
"async",
116-
(ThrowingSupplier<ClusterHealthStatus, Exception>) this::clusterHealthAsync)));
117-
}
118-
119-
@ParameterizedTest
120-
@MethodSource("healthArguments")
121-
void elasticsearchStatus(ThrowingSupplier<ClusterHealthStatus, Exception> supplier)
122-
throws Exception {
123-
ClusterHealthStatus clusterHealthStatus = testing.runWithSpan("parent", supplier);
124-
125-
assertThat(clusterHealthStatus.name()).isEqualTo("GREEN");
126-
127-
testing.waitAndAssertTraces(
128-
trace ->
129-
trace.hasSpansSatisfyingExactly(
130-
span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(),
131-
span ->
132-
span.hasName("ClusterHealthAction")
133-
.hasKind(SpanKind.CLIENT)
134-
.hasParent(trace.getSpan(0))
135-
.hasAttributesSatisfyingExactly(
136-
equalTo(
137-
DbIncubatingAttributes.DB_SYSTEM,
138-
DbIncubatingAttributes.DbSystemValues.ELASTICSEARCH),
139-
equalTo(DbIncubatingAttributes.DB_OPERATION, "ClusterHealthAction"),
140-
equalTo(ELASTICSEARCH_ACTION, "ClusterHealthAction"),
141-
equalTo(ELASTICSEARCH_REQUEST, "ClusterHealthRequest")),
142-
span ->
143-
span.hasName("callback")
144-
.hasKind(SpanKind.INTERNAL)
145-
.hasParent(trace.getSpan(0))));
146-
}
147-
148-
private Stream<Arguments> errorArguments() {
149-
return Stream.of(
150-
Arguments.of(
151-
named("sync", (Runnable) () -> prepareGetSync("invalid-index", "test-type", "1"))),
152-
Arguments.of(
153-
named("async", (Runnable) () -> prepareGetAsync("invalid-index", "test-type", "1"))));
154-
}
155-
156-
@ParameterizedTest
157-
@MethodSource("errorArguments")
158-
void elasticsearchError(Runnable action) {
159-
assertThatThrownBy(() -> testing.runWithSpan("parent", action::run))
160-
.isInstanceOf(IndexNotFoundException.class)
161-
.hasMessage("no such index");
162-
163-
IndexNotFoundException expectedException = new IndexNotFoundException("no such index");
164-
testing.waitAndAssertTraces(
165-
trace ->
166-
trace.hasSpansSatisfyingExactly(
167-
span ->
168-
span.hasName("parent")
169-
.hasKind(SpanKind.INTERNAL)
170-
.hasNoParent()
171-
.hasStatus(StatusData.error())
172-
.hasException(expectedException),
173-
span ->
174-
span.hasName("GetAction")
175-
.hasKind(SpanKind.CLIENT)
176-
.hasParent(trace.getSpan(0))
177-
.hasStatus(StatusData.error())
178-
.hasException(expectedException)
179-
.hasAttributesSatisfyingExactly(
180-
equalTo(
181-
DbIncubatingAttributes.DB_SYSTEM,
182-
DbIncubatingAttributes.DbSystemValues.ELASTICSEARCH),
183-
equalTo(DbIncubatingAttributes.DB_OPERATION, "GetAction"),
184-
equalTo(ELASTICSEARCH_ACTION, "GetAction"),
185-
equalTo(ELASTICSEARCH_REQUEST, "GetRequest"),
186-
equalTo(ELASTICSEARCH_REQUEST_INDICES, "invalid-index")),
187-
span ->
188-
span.hasName("callback")
189-
.hasKind(SpanKind.INTERNAL)
190-
.hasParent(trace.getSpan(0))));
191-
}
192-
193-
@Test
194-
void elasticsearchGet() {
195-
String indexName = "test-index";
196-
String indexType = "test-type";
197-
String id = "1";
198-
199-
CreateIndexResponse indexResult = client.admin().indices().prepareCreate(indexName).get();
200-
assertThat(indexResult.isAcknowledged()).isTrue();
201-
202-
client.admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet(TIMEOUT);
203-
GetResponse emptyResult = client.prepareGet(indexName, indexType, id).get();
204-
assertThat(emptyResult.isExists()).isFalse();
205-
assertThat(emptyResult.getId()).isEqualTo(id);
206-
assertThat(emptyResult.getType()).isEqualTo(indexType);
207-
assertThat(emptyResult.getIndex()).isEqualTo(indexName);
208-
209-
IndexResponse createResult =
210-
client.prepareIndex(indexName, indexType, id).setSource(Collections.emptyMap()).get();
211-
assertThat(createResult.getId()).isEqualTo(id);
212-
assertThat(createResult.getType()).isEqualTo(indexType);
213-
assertThat(createResult.getIndex()).isEqualTo(indexName);
214-
assertThat(createResult.status().getStatus()).isEqualTo(201);
215-
cleanup.deferCleanup(() -> client.admin().indices().prepareDelete(indexName).get());
216-
217-
GetResponse result = client.prepareGet(indexName, indexType, id).get();
218-
assertThat(result.isExists()).isTrue();
219-
assertThat(result.getId()).isEqualTo(id);
220-
assertThat(result.getType()).isEqualTo(indexType);
221-
assertThat(result.getIndex()).isEqualTo(indexName);
222-
223-
testing.waitAndAssertTraces(
224-
trace ->
225-
trace.hasSpansSatisfyingExactly(
226-
span ->
227-
span.hasName("CreateIndexAction")
228-
.hasKind(SpanKind.CLIENT)
229-
.hasNoParent()
230-
.hasAttributesSatisfyingExactly(
231-
equalTo(
232-
DbIncubatingAttributes.DB_SYSTEM,
233-
DbIncubatingAttributes.DbSystemValues.ELASTICSEARCH),
234-
equalTo(DbIncubatingAttributes.DB_OPERATION, "CreateIndexAction"),
235-
equalTo(ELASTICSEARCH_ACTION, "CreateIndexAction"),
236-
equalTo(ELASTICSEARCH_REQUEST, "CreateIndexRequest"),
237-
equalTo(ELASTICSEARCH_REQUEST_INDICES, indexName))),
238-
trace ->
239-
trace.hasSpansSatisfyingExactly(
240-
span ->
241-
span.hasName("ClusterHealthAction")
242-
.hasKind(SpanKind.CLIENT)
243-
.hasNoParent()
244-
.hasAttributesSatisfyingExactly(
245-
equalTo(
246-
DbIncubatingAttributes.DB_SYSTEM,
247-
DbIncubatingAttributes.DbSystemValues.ELASTICSEARCH),
248-
equalTo(DbIncubatingAttributes.DB_OPERATION, "ClusterHealthAction"),
249-
equalTo(ELASTICSEARCH_ACTION, "ClusterHealthAction"),
250-
equalTo(ELASTICSEARCH_REQUEST, "ClusterHealthRequest"))),
251-
trace ->
252-
trace.hasSpansSatisfyingExactly(
253-
span ->
254-
span.hasName("GetAction")
255-
.hasKind(SpanKind.CLIENT)
256-
.hasNoParent()
257-
.hasAttributesSatisfyingExactly(
258-
equalTo(
259-
DbIncubatingAttributes.DB_SYSTEM,
260-
DbIncubatingAttributes.DbSystemValues.ELASTICSEARCH),
261-
equalTo(DbIncubatingAttributes.DB_OPERATION, "GetAction"),
262-
equalTo(ELASTICSEARCH_ACTION, "GetAction"),
263-
equalTo(ELASTICSEARCH_REQUEST, "GetRequest"),
264-
equalTo(ELASTICSEARCH_REQUEST_INDICES, indexName),
265-
equalTo(ELASTICSEARCH_TYPE, indexType),
266-
equalTo(ELASTICSEARCH_ID, id),
267-
equalTo(ELASTICSEARCH_VERSION, -1))),
268-
trace ->
269-
trace.hasSpansSatisfyingExactly(
270-
span ->
271-
span.hasName("IndexAction")
272-
.hasKind(SpanKind.CLIENT)
273-
.hasNoParent()
274-
.hasAttributesSatisfyingExactly(
275-
equalTo(
276-
DbIncubatingAttributes.DB_SYSTEM,
277-
DbIncubatingAttributes.DbSystemValues.ELASTICSEARCH),
278-
equalTo(DbIncubatingAttributes.DB_OPERATION, "IndexAction"),
279-
equalTo(ELASTICSEARCH_ACTION, "IndexAction"),
280-
equalTo(ELASTICSEARCH_REQUEST, "IndexRequest"),
281-
equalTo(ELASTICSEARCH_REQUEST_INDICES, indexName),
282-
equalTo(
283-
AttributeKey.stringKey("elasticsearch.request.write.type"),
284-
indexType),
285-
equalTo(AttributeKey.longKey("elasticsearch.response.status"), 201),
286-
equalTo(
287-
AttributeKey.longKey("elasticsearch.shard.replication.total"), 2),
288-
equalTo(
289-
AttributeKey.longKey("elasticsearch.shard.replication.successful"),
290-
1),
291-
equalTo(
292-
AttributeKey.longKey("elasticsearch.shard.replication.failed"),
293-
0))),
294-
trace ->
295-
trace.hasSpansSatisfyingExactly(
296-
span ->
297-
span.hasName("GetAction")
298-
.hasKind(SpanKind.CLIENT)
299-
.hasNoParent()
300-
.hasAttributesSatisfyingExactly(
301-
equalTo(
302-
DbIncubatingAttributes.DB_SYSTEM,
303-
DbIncubatingAttributes.DbSystemValues.ELASTICSEARCH),
304-
equalTo(DbIncubatingAttributes.DB_OPERATION, "GetAction"),
305-
equalTo(ELASTICSEARCH_ACTION, "GetAction"),
306-
equalTo(ELASTICSEARCH_REQUEST, "GetRequest"),
307-
equalTo(ELASTICSEARCH_REQUEST_INDICES, indexName),
308-
equalTo(ELASTICSEARCH_TYPE, indexType),
309-
equalTo(ELASTICSEARCH_ID, id),
310-
equalTo(ELASTICSEARCH_VERSION, 1))));
82+
@Override
83+
protected boolean hasWriteVersion() {
84+
return false;
31185
}
31286
}

0 commit comments

Comments
 (0)