From 0973f335c530fea80de9048eca83998f01e4cebd Mon Sep 17 00:00:00 2001 From: Jay DeLuca Date: Tue, 12 Dec 2023 15:50:50 -0500 Subject: [PATCH] Convert lettuce 5.1 SyncClientAuthTest from groovy to java (#10045) --- .../v5_1/LettuceSyncClientAuthTest.groovy | 17 ---- .../v5_1/LettuceSyncClientAuthTest.java | 27 ++++++ .../v5_1/LettuceSyncClientAuthTest.groovy | 21 ----- .../v5_1/LettuceSyncClientAuthTest.java | 33 +++++++ .../lettuce-5.1/testing/build.gradle.kts | 2 - .../AbstractLettuceSyncClientAuthTest.groovy | 86 ------------------- .../AbstractLettuceSyncClientAuthTest.java | 75 ++++++++++++++++ 7 files changed, 135 insertions(+), 126 deletions(-) delete mode 100644 instrumentation/lettuce/lettuce-5.1/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/lettuce/v5_1/LettuceSyncClientAuthTest.groovy create mode 100644 instrumentation/lettuce/lettuce-5.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/lettuce/v5_1/LettuceSyncClientAuthTest.java delete mode 100644 instrumentation/lettuce/lettuce-5.1/library/src/test/groovy/io/opentelemetry/instrumentation/lettuce/v5_1/LettuceSyncClientAuthTest.groovy create mode 100644 instrumentation/lettuce/lettuce-5.1/library/src/test/java/io/opentelemetry/instrumentation/lettuce/v5_1/LettuceSyncClientAuthTest.java delete mode 100644 instrumentation/lettuce/lettuce-5.1/testing/src/main/groovy/io/opentelemetry/instrumentation/lettuce/v5_1/AbstractLettuceSyncClientAuthTest.groovy create mode 100644 instrumentation/lettuce/lettuce-5.1/testing/src/main/java/io/opentelemetry/instrumentation/lettuce/v5_1/AbstractLettuceSyncClientAuthTest.java diff --git a/instrumentation/lettuce/lettuce-5.1/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/lettuce/v5_1/LettuceSyncClientAuthTest.groovy b/instrumentation/lettuce/lettuce-5.1/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/lettuce/v5_1/LettuceSyncClientAuthTest.groovy deleted file mode 100644 index 664e501841cd..000000000000 --- a/instrumentation/lettuce/lettuce-5.1/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/lettuce/v5_1/LettuceSyncClientAuthTest.groovy +++ /dev/null @@ -1,17 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.javaagent.instrumentation.lettuce.v5_1 - -import io.lettuce.core.RedisClient -import io.opentelemetry.instrumentation.lettuce.v5_1.AbstractLettuceSyncClientAuthTest -import io.opentelemetry.instrumentation.test.AgentTestTrait - -class LettuceSyncClientAuthTest extends AbstractLettuceSyncClientAuthTest implements AgentTestTrait { - @Override - RedisClient createClient(String uri) { - return RedisClient.create(uri) - } -} diff --git a/instrumentation/lettuce/lettuce-5.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/lettuce/v5_1/LettuceSyncClientAuthTest.java b/instrumentation/lettuce/lettuce-5.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/lettuce/v5_1/LettuceSyncClientAuthTest.java new file mode 100644 index 000000000000..7f79509b9eba --- /dev/null +++ b/instrumentation/lettuce/lettuce-5.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/lettuce/v5_1/LettuceSyncClientAuthTest.java @@ -0,0 +1,27 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.lettuce.v5_1; + +import io.lettuce.core.RedisClient; +import io.opentelemetry.instrumentation.lettuce.v5_1.AbstractLettuceSyncClientTest; +import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import org.junit.jupiter.api.extension.RegisterExtension; + +class LettuceSyncClientAuthTest extends AbstractLettuceSyncClientTest { + @RegisterExtension + static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); + + @Override + public InstrumentationExtension getInstrumentationExtension() { + return testing; + } + + @Override + protected RedisClient createClient(String uri) { + return RedisClient.create(uri); + } +} diff --git a/instrumentation/lettuce/lettuce-5.1/library/src/test/groovy/io/opentelemetry/instrumentation/lettuce/v5_1/LettuceSyncClientAuthTest.groovy b/instrumentation/lettuce/lettuce-5.1/library/src/test/groovy/io/opentelemetry/instrumentation/lettuce/v5_1/LettuceSyncClientAuthTest.groovy deleted file mode 100644 index 7cb208b5b2ff..000000000000 --- a/instrumentation/lettuce/lettuce-5.1/library/src/test/groovy/io/opentelemetry/instrumentation/lettuce/v5_1/LettuceSyncClientAuthTest.groovy +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.lettuce.v5_1 - -import io.lettuce.core.RedisClient -import io.lettuce.core.resource.ClientResources -import io.opentelemetry.instrumentation.test.LibraryTestTrait - -class LettuceSyncClientAuthTest extends AbstractLettuceSyncClientAuthTest implements LibraryTestTrait { - @Override - RedisClient createClient(String uri) { - return RedisClient.create( - ClientResources.builder() - .tracing(LettuceTelemetry.create(getOpenTelemetry()).newTracing()) - .build(), - uri) - } -} diff --git a/instrumentation/lettuce/lettuce-5.1/library/src/test/java/io/opentelemetry/instrumentation/lettuce/v5_1/LettuceSyncClientAuthTest.java b/instrumentation/lettuce/lettuce-5.1/library/src/test/java/io/opentelemetry/instrumentation/lettuce/v5_1/LettuceSyncClientAuthTest.java new file mode 100644 index 000000000000..357ae8e09e74 --- /dev/null +++ b/instrumentation/lettuce/lettuce-5.1/library/src/test/java/io/opentelemetry/instrumentation/lettuce/v5_1/LettuceSyncClientAuthTest.java @@ -0,0 +1,33 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.lettuce.v5_1; + +import io.lettuce.core.RedisClient; +import io.lettuce.core.resource.ClientResources; +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; +import org.junit.jupiter.api.extension.RegisterExtension; + +class LettuceSyncClientAuthTest extends AbstractLettuceSyncClientAuthTest { + @RegisterExtension + static InstrumentationExtension testing = LibraryInstrumentationExtension.create(); + + @Override + public InstrumentationExtension getInstrumentationExtension() { + return testing; + } + + @Override + protected RedisClient createClient(String uri) { + return RedisClient.create( + ClientResources.builder() + .tracing( + LettuceTelemetry.create(getInstrumentationExtension().getOpenTelemetry()) + .newTracing()) + .build(), + uri); + } +} diff --git a/instrumentation/lettuce/lettuce-5.1/testing/build.gradle.kts b/instrumentation/lettuce/lettuce-5.1/testing/build.gradle.kts index fae109de6c07..577605b637a4 100644 --- a/instrumentation/lettuce/lettuce-5.1/testing/build.gradle.kts +++ b/instrumentation/lettuce/lettuce-5.1/testing/build.gradle.kts @@ -11,7 +11,5 @@ dependencies { implementation("org.testcontainers:testcontainers") implementation("com.google.guava:guava") - implementation("org.apache.groovy:groovy") implementation("io.opentelemetry:opentelemetry-api") - implementation("org.spockframework:spock-core") } diff --git a/instrumentation/lettuce/lettuce-5.1/testing/src/main/groovy/io/opentelemetry/instrumentation/lettuce/v5_1/AbstractLettuceSyncClientAuthTest.groovy b/instrumentation/lettuce/lettuce-5.1/testing/src/main/groovy/io/opentelemetry/instrumentation/lettuce/v5_1/AbstractLettuceSyncClientAuthTest.groovy deleted file mode 100644 index 2266c112af60..000000000000 --- a/instrumentation/lettuce/lettuce-5.1/testing/src/main/groovy/io/opentelemetry/instrumentation/lettuce/v5_1/AbstractLettuceSyncClientAuthTest.groovy +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.instrumentation.lettuce.v5_1 - -import io.lettuce.core.RedisClient -import io.opentelemetry.instrumentation.api.semconv.network.internal.NetworkAttributes -import io.opentelemetry.instrumentation.test.InstrumentationSpecification -import io.opentelemetry.semconv.SemanticAttributes -import org.testcontainers.containers.GenericContainer -import spock.lang.Shared - -import static io.opentelemetry.api.trace.SpanKind.CLIENT - -abstract class AbstractLettuceSyncClientAuthTest extends InstrumentationSpecification { - public static final int DB_INDEX = 0 - - private static GenericContainer redisServer = new GenericContainer<>("redis:6.2.3-alpine").withExposedPorts(6379) - - abstract RedisClient createClient(String uri) - - @Shared - String expectedHostAttributeValue - @Shared - int port - @Shared - String password - - RedisClient redisClient - - def setupSpec() { - password = "password" - - redisServer = redisServer - .withCommand("redis-server", "--requirepass $password") - } - - def setup() { - redisServer.start() - - port = redisServer.getMappedPort(6379) - String host = redisServer.getHost() - String dbAddr = host + ":" + port + "/" + DB_INDEX - String embeddedDbUri = "redis://" + dbAddr - expectedHostAttributeValue = host == "127.0.0.1" ? null : host - - redisClient = createClient(embeddedDbUri) - redisClient.setOptions(LettuceTestUtil.CLIENT_OPTIONS) - } - - def cleanup() { - redisClient.shutdown() - redisServer.stop() - } - - def "auth command"() { - setup: - def res = redisClient.connect().sync().auth(password) - - expect: - res == "OK" - assertTraces(1) { - trace(0, 1) { - span(0) { - name "AUTH" - kind CLIENT - attributes { - "$SemanticAttributes.NETWORK_TYPE" "ipv4" - "$NetworkAttributes.NETWORK_PEER_ADDRESS" "127.0.0.1" - "$NetworkAttributes.NETWORK_PEER_PORT" port - "$SemanticAttributes.DB_SYSTEM" "redis" - "$SemanticAttributes.DB_STATEMENT" "AUTH ?" - } - event(0) { - eventName "redis.encode.start" - } - event(1) { - eventName "redis.encode.end" - } - } - } - } - } -} diff --git a/instrumentation/lettuce/lettuce-5.1/testing/src/main/java/io/opentelemetry/instrumentation/lettuce/v5_1/AbstractLettuceSyncClientAuthTest.java b/instrumentation/lettuce/lettuce-5.1/testing/src/main/java/io/opentelemetry/instrumentation/lettuce/v5_1/AbstractLettuceSyncClientAuthTest.java new file mode 100644 index 000000000000..5fcc6211bb7d --- /dev/null +++ b/instrumentation/lettuce/lettuce-5.1/testing/src/main/java/io/opentelemetry/instrumentation/lettuce/v5_1/AbstractLettuceSyncClientAuthTest.java @@ -0,0 +1,75 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.lettuce.v5_1; + +import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; +import static org.assertj.core.api.Assertions.assertThat; + +import io.lettuce.core.api.sync.RedisCommands; +import io.opentelemetry.api.trace.SpanKind; +import io.opentelemetry.instrumentation.api.semconv.network.internal.NetworkAttributes; +import io.opentelemetry.semconv.SemanticAttributes; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public abstract class AbstractLettuceSyncClientAuthTest extends AbstractLettuceClientTest { + + @BeforeAll + void setUp() { + redisServer = redisServer.withCommand("redis-server", "--requirepass password"); + redisServer.start(); + + host = redisServer.getHost(); + port = redisServer.getMappedPort(6379); + embeddedDbUri = "redis://" + host + ":" + port + "/" + DB_INDEX; + + redisClient = createClient(embeddedDbUri); + redisClient.setOptions(LettuceTestUtil.CLIENT_OPTIONS); + } + + @AfterAll + static void cleanUp() { + redisClient.shutdown(); + redisServer.stop(); + + // Set back so other tests don't fail due to NOAUTH error + redisServer = redisServer.withCommand("redis-server", "--requirepass \"\""); + } + + @Test + void testAuthCommand() throws Exception { + Class commandsClass = RedisCommands.class; + java.lang.reflect.Method authMethod; + // the auth() argument type changed between 5.x -> 6.x + try { + authMethod = commandsClass.getMethod("auth", String.class); + } catch (NoSuchMethodException unused) { + authMethod = commandsClass.getMethod("auth", CharSequence.class); + } + + String result = (String) authMethod.invoke(redisClient.connect().sync(), "password"); + + assertThat(result).isEqualTo("OK"); + + getInstrumentationExtension() + .waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasName("AUTH") + .hasKind(SpanKind.CLIENT) + .hasAttributesSatisfyingExactly( + equalTo(SemanticAttributes.NETWORK_TYPE, "ipv4"), + equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1"), + equalTo(NetworkAttributes.NETWORK_PEER_PORT, port), + equalTo(SemanticAttributes.DB_SYSTEM, "redis"), + equalTo(SemanticAttributes.DB_STATEMENT, "AUTH ?")) + .hasEventsSatisfyingExactly( + event -> event.hasName("redis.encode.start"), + event -> event.hasName("redis.encode.end")))); + } +}