Ver código fonte

converted couchbase-3.2 test from groovy to java (#8837)

Anthony Quinones 1 ano atrás
pai
commit
93fc34ebfb

+ 0 - 80
instrumentation/couchbase/couchbase-3.2/javaagent/src/test/groovy/CouchbaseClient32Test.groovy

@@ -1,80 +0,0 @@
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-import com.couchbase.client.core.error.DocumentNotFoundException
-import com.couchbase.client.java.Cluster
-import com.couchbase.client.java.Collection
-import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
-import org.slf4j.Logger
-import org.slf4j.LoggerFactory
-import org.testcontainers.containers.output.Slf4jLogConsumer
-import org.testcontainers.couchbase.BucketDefinition
-import org.testcontainers.couchbase.CouchbaseContainer
-import org.testcontainers.couchbase.CouchbaseService
-import spock.lang.Shared
-
-import java.time.Duration
-
-import static io.opentelemetry.api.trace.StatusCode.ERROR
-
-// Couchbase instrumentation is owned upstream so we don't assert on the contents of the spans, only
-// that the instrumentation is properly registered by the agent, meaning some spans were generated.
-class CouchbaseClient32Test extends AgentInstrumentationSpecification {
-  private static final Logger logger = LoggerFactory.getLogger("couchbase-container")
-
-  @Shared
-  CouchbaseContainer couchbase
-  @Shared
-  Cluster cluster
-  @Shared
-  Collection collection
-
-  def setupSpec() {
-    couchbase = new CouchbaseContainer()
-      .withExposedPorts(8091)
-      .withEnabledServices(CouchbaseService.KV)
-      .withBucket(new BucketDefinition("test"))
-      .withLogConsumer(new Slf4jLogConsumer(logger))
-      .withStartupTimeout(Duration.ofSeconds(120))
-    couchbase.start()
-
-    cluster = Cluster.connect(couchbase.connectionString, couchbase.username, couchbase.password)
-    def bucket = cluster.bucket("test")
-    collection = bucket.defaultCollection()
-    bucket.waitUntilReady(Duration.ofSeconds(30))
-  }
-
-  def cleanupSpec() {
-    couchbase.stop()
-  }
-
-  def "emits spans"() {
-    when:
-    try {
-      collection.get("id")
-    } catch (DocumentNotFoundException e) {
-      // Expected
-    }
-
-    then:
-    assertTracesWithoutScopeVersionVerification(1) {
-      trace(0, 2) {
-        span(0) {
-          name(~/.*get/)
-          if (Boolean.getBoolean("testLatestDeps")) {
-            // this is the correct behavior
-            status ERROR
-          }
-        }
-        span(1) {
-          name(~/.*dispatch_to_server/)
-        }
-      }
-    }
-
-    cleanup:
-    cluster.disconnect()
-  }
-}

+ 85 - 0
instrumentation/couchbase/couchbase-3.2/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/couchbase/v3_2/CouchbaseClient32Test.java

@@ -0,0 +1,85 @@
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.couchbase.v3_2;
+
+import com.couchbase.client.core.error.DocumentNotFoundException;
+import com.couchbase.client.java.Bucket;
+import com.couchbase.client.java.Cluster;
+import com.couchbase.client.java.Collection;
+import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
+import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
+import io.opentelemetry.sdk.trace.data.StatusData;
+import java.time.Duration;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.RegisterExtension;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.testcontainers.containers.output.Slf4jLogConsumer;
+import org.testcontainers.couchbase.BucketDefinition;
+import org.testcontainers.couchbase.CouchbaseContainer;
+import org.testcontainers.couchbase.CouchbaseService;
+
+// Couchbase instrumentation is owned upstream so we don't assert on the contents of the spans, only
+// that the instrumentation is properly registered by the agent, meaning some spans were generated.
+class CouchbaseClient32Test {
+  @RegisterExtension
+  private static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
+
+  private static final Logger logger = LoggerFactory.getLogger("couchbase-container");
+
+  static CouchbaseContainer couchbase;
+  static Cluster cluster;
+  static Collection collection;
+
+  @BeforeAll
+  static void setup() {
+    couchbase =
+        new CouchbaseContainer("couchbase/server:6.5.1")
+            .withExposedPorts(8091)
+            .withEnabledServices(CouchbaseService.KV)
+            .withBucket(new BucketDefinition("test"))
+            .withLogConsumer(new Slf4jLogConsumer(logger))
+            .withStartupTimeout(Duration.ofMinutes(2));
+    couchbase.start();
+
+    cluster =
+        Cluster.connect(
+            couchbase.getConnectionString(), couchbase.getUsername(), couchbase.getPassword());
+    Bucket bucket = cluster.bucket("test");
+    collection = bucket.defaultCollection();
+    bucket.waitUntilReady(Duration.ofSeconds(30));
+  }
+
+  @AfterAll
+  static void cleanup() {
+    cluster.disconnect();
+    couchbase.stop();
+  }
+
+  @Test
+  void testEmitsSpans() {
+    try {
+      collection.get("id");
+    } catch (DocumentNotFoundException e) {
+      // Expected
+    }
+
+    testing.waitAndAssertTracesWithoutScopeVersionVerification(
+        trace ->
+            trace
+                .hasSize(2)
+                .hasSpansSatisfyingExactly(
+                    span -> {
+                      span.hasName("get");
+                      if (Boolean.getBoolean("testLatestDeps")) {
+                        span.hasStatus(StatusData.error());
+                      }
+                    },
+                    span -> span.hasName("dispatch_to_server")));
+  }
+}