convert jedis test from groovy to java (#7731)

related to #7195
This commit is contained in:
xiangtianyu 2023-02-08 00:36:50 +08:00 committed by GitHub
parent 7091719e96
commit c9a04620f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 452 additions and 431 deletions

View File

@ -1,137 +0,0 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
import org.testcontainers.containers.GenericContainer
import redis.clients.jedis.Jedis
import spock.lang.Shared
import static io.opentelemetry.api.trace.SpanKind.CLIENT
class JedisClientTest extends AgentInstrumentationSpecification {
private static GenericContainer redisServer = new GenericContainer<>("redis:6.2.3-alpine").withExposedPorts(6379)
@Shared
int port
@Shared
Jedis jedis
def setupSpec() {
redisServer.start()
port = redisServer.getMappedPort(6379)
jedis = new Jedis("localhost", port)
}
def cleanupSpec() {
redisServer.stop()
// jedis.close() // not available in the early version we're using.
}
def setup() {
jedis.flushAll()
clearExportedData()
}
def "set command"() {
when:
jedis.set("foo", "bar")
then:
assertTraces(1) {
trace(0, 1) {
span(0) {
name "SET"
kind CLIENT
attributes {
"$SemanticAttributes.DB_SYSTEM" "redis"
"$SemanticAttributes.DB_STATEMENT" "SET foo ?"
"$SemanticAttributes.DB_OPERATION" "SET"
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
}
}
}
}
}
def "get command"() {
when:
jedis.set("foo", "bar")
def value = jedis.get("foo")
then:
value == "bar"
assertTraces(2) {
trace(0, 1) {
span(0) {
name "SET"
kind CLIENT
attributes {
"$SemanticAttributes.DB_SYSTEM" "redis"
"$SemanticAttributes.DB_STATEMENT" "SET foo ?"
"$SemanticAttributes.DB_OPERATION" "SET"
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
}
}
}
trace(1, 1) {
span(0) {
name "GET"
kind CLIENT
attributes {
"$SemanticAttributes.DB_SYSTEM" "redis"
"$SemanticAttributes.DB_STATEMENT" "GET foo"
"$SemanticAttributes.DB_OPERATION" "GET"
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
}
}
}
}
}
def "command with no arguments"() {
when:
jedis.set("foo", "bar")
def value = jedis.randomKey()
then:
value == "foo"
assertTraces(2) {
trace(0, 1) {
span(0) {
name "SET"
kind CLIENT
attributes {
"$SemanticAttributes.DB_SYSTEM" "redis"
"$SemanticAttributes.DB_STATEMENT" "SET foo ?"
"$SemanticAttributes.DB_OPERATION" "SET"
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
}
}
}
trace(1, 1) {
span(0) {
name "RANDOMKEY"
kind CLIENT
attributes {
"$SemanticAttributes.DB_SYSTEM" "redis"
"$SemanticAttributes.DB_STATEMENT" "RANDOMKEY"
"$SemanticAttributes.DB_OPERATION" "RANDOMKEY"
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
}
}
}
}
}
}

View File

@ -0,0 +1,133 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.jedis.v1_4;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.testcontainers.containers.GenericContainer;
import redis.clients.jedis.Jedis;
class JedisClientTest {
@RegisterExtension
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
static GenericContainer<?> redisServer =
new GenericContainer<>("redis:6.2.3-alpine").withExposedPorts(6379);
static int port;
static Jedis jedis;
@BeforeAll
static void setupSpec() {
redisServer.start();
port = redisServer.getMappedPort(6379);
jedis = new Jedis("localhost", port);
}
@AfterAll
static void cleanupSpec() {
redisServer.stop();
}
@BeforeEach
void setup() {
jedis.flushAll();
testing.clearData();
}
@Test
void setCommand() {
jedis.set("foo", "bar");
testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("SET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"),
equalTo(SemanticAttributes.DB_OPERATION, "SET"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, port))));
}
@Test
void getCommand() {
jedis.set("foo", "bar");
String value = jedis.get("foo");
assertThat(value).isEqualTo("bar");
testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("SET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"),
equalTo(SemanticAttributes.DB_OPERATION, "SET"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, port))),
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("GET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "GET foo"),
equalTo(SemanticAttributes.DB_OPERATION, "GET"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, port))));
}
@Test
void commandWithNoArguments() {
jedis.set("foo", "bar");
String value = jedis.randomKey();
assertThat(value).isEqualTo("foo");
testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("SET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"),
equalTo(SemanticAttributes.DB_OPERATION, "SET"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, port))),
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("RANDOMKEY")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "RANDOMKEY"),
equalTo(SemanticAttributes.DB_OPERATION, "RANDOMKEY"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, port))));
}
}

View File

@ -1,147 +0,0 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
import org.testcontainers.containers.GenericContainer
import redis.clients.jedis.Jedis
import spock.lang.Shared
import static io.opentelemetry.api.trace.SpanKind.CLIENT
class Jedis30ClientTest extends AgentInstrumentationSpecification {
private static GenericContainer redisServer = new GenericContainer<>("redis:6.2.3-alpine").withExposedPorts(6379)
@Shared
int port
@Shared
Jedis jedis
def setupSpec() {
redisServer.start()
port = redisServer.getMappedPort(6379)
jedis = new Jedis("localhost", port)
}
def cleanupSpec() {
redisServer.stop()
jedis.close()
}
def setup() {
jedis.flushAll()
clearExportedData()
}
def "set command"() {
when:
jedis.set("foo", "bar")
then:
assertTraces(1) {
trace(0, 1) {
span(0) {
name "SET"
kind CLIENT
attributes {
"$SemanticAttributes.DB_SYSTEM" "redis"
"$SemanticAttributes.DB_STATEMENT" "SET foo ?"
"$SemanticAttributes.DB_OPERATION" "SET"
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
"$SemanticAttributes.NET_SOCK_PEER_ADDR" "127.0.0.1"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
}
}
}
}
}
def "get command"() {
when:
jedis.set("foo", "bar")
def value = jedis.get("foo")
then:
value == "bar"
assertTraces(2) {
trace(0, 1) {
span(0) {
name "SET"
kind CLIENT
attributes {
"$SemanticAttributes.DB_SYSTEM" "redis"
"$SemanticAttributes.DB_STATEMENT" "SET foo ?"
"$SemanticAttributes.DB_OPERATION" "SET"
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
"$SemanticAttributes.NET_SOCK_PEER_ADDR" "127.0.0.1"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
}
}
}
trace(1, 1) {
span(0) {
name "GET"
kind CLIENT
attributes {
"$SemanticAttributes.DB_SYSTEM" "redis"
"$SemanticAttributes.DB_STATEMENT" "GET foo"
"$SemanticAttributes.DB_OPERATION" "GET"
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
"$SemanticAttributes.NET_SOCK_PEER_ADDR" "127.0.0.1"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
}
}
}
}
}
def "command with no arguments"() {
when:
jedis.set("foo", "bar")
def value = jedis.randomKey()
then:
value == "foo"
assertTraces(2) {
trace(0, 1) {
span(0) {
name "SET"
kind CLIENT
attributes {
"$SemanticAttributes.DB_SYSTEM" "redis"
"$SemanticAttributes.DB_STATEMENT" "SET foo ?"
"$SemanticAttributes.DB_OPERATION" "SET"
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
"$SemanticAttributes.NET_SOCK_PEER_ADDR" "127.0.0.1"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
}
}
}
trace(1, 1) {
span(0) {
name "RANDOMKEY"
kind CLIENT
attributes {
"$SemanticAttributes.DB_SYSTEM" "redis"
"$SemanticAttributes.DB_STATEMENT" "RANDOMKEY"
"$SemanticAttributes.DB_OPERATION" "RANDOMKEY"
"$SemanticAttributes.NET_PEER_NAME" "localhost"
"$SemanticAttributes.NET_PEER_PORT" port
"$SemanticAttributes.NET_SOCK_PEER_ADDR" "127.0.0.1"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
}
}
}
}
}
}

View File

@ -0,0 +1,154 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.jedis.v3_0;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.testcontainers.containers.GenericContainer;
import redis.clients.jedis.Jedis;
class Jedis30ClientTest {
@RegisterExtension
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
static GenericContainer<?> redisServer =
new GenericContainer<>("redis:6.2.3-alpine").withExposedPorts(6379);
static int port;
static Jedis jedis;
@BeforeAll
static void setupSpec() {
redisServer.start();
port = redisServer.getMappedPort(6379);
jedis = new Jedis("localhost", port);
}
@AfterAll
static void cleanupSpec() {
redisServer.stop();
jedis.close();
}
@BeforeEach
void setup() {
jedis.flushAll();
testing.clearData();
}
@Test
void setCommand() {
jedis.set("foo", "bar");
testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("SET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"),
equalTo(SemanticAttributes.DB_OPERATION, "SET"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"),
equalTo(
SemanticAttributes.NET_TRANSPORT,
SemanticAttributes.NetTransportValues.IP_TCP))));
}
@Test
void getCommand() {
jedis.set("foo", "bar");
String value = jedis.get("foo");
assertThat(value).isEqualTo("bar");
testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("SET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"),
equalTo(SemanticAttributes.DB_OPERATION, "SET"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"),
equalTo(
SemanticAttributes.NET_TRANSPORT,
SemanticAttributes.NetTransportValues.IP_TCP))),
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("GET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "GET foo"),
equalTo(SemanticAttributes.DB_OPERATION, "GET"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"),
equalTo(
SemanticAttributes.NET_TRANSPORT,
SemanticAttributes.NetTransportValues.IP_TCP))));
}
@Test
void commandWithNoArguments() {
jedis.set("foo", "bar");
String value = jedis.randomKey();
assertThat(value).isEqualTo("foo");
testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("SET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"),
equalTo(SemanticAttributes.DB_OPERATION, "SET"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"),
equalTo(
SemanticAttributes.NET_TRANSPORT,
SemanticAttributes.NetTransportValues.IP_TCP))),
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("RANDOMKEY")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "RANDOMKEY"),
equalTo(SemanticAttributes.DB_OPERATION, "RANDOMKEY"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"),
equalTo(
SemanticAttributes.NET_TRANSPORT,
SemanticAttributes.NetTransportValues.IP_TCP))));
}
}

View File

@ -1,147 +0,0 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
import org.testcontainers.containers.GenericContainer
import redis.clients.jedis.Jedis
import spock.lang.Shared
import static io.opentelemetry.api.trace.SpanKind.CLIENT
class Jedis40ClientTest extends AgentInstrumentationSpecification {
private static GenericContainer redisServer = new GenericContainer<>("redis:6.2.3-alpine").withExposedPorts(6379)
@Shared
int port
@Shared
Jedis jedis
def setupSpec() {
redisServer.start()
port = redisServer.getMappedPort(6379)
jedis = new Jedis("localhost", port)
}
def cleanupSpec() {
redisServer.stop()
jedis.close()
}
def setup() {
jedis.flushAll()
clearExportedData()
}
def "set command"() {
when:
jedis.set("foo", "bar")
then:
assertTraces(1) {
trace(0, 1) {
span(0) {
name "SET"
kind CLIENT
attributes {
"$SemanticAttributes.DB_SYSTEM" "redis"
"$SemanticAttributes.DB_STATEMENT" "SET foo ?"
"$SemanticAttributes.DB_OPERATION" "SET"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.NET_SOCK_PEER_ADDR" "127.0.0.1"
"$SemanticAttributes.NET_SOCK_PEER_NAME" { it == "localhost" || it == "127.0.0.1" }
"$SemanticAttributes.NET_SOCK_PEER_PORT" port
}
}
}
}
}
def "get command"() {
when:
jedis.set("foo", "bar")
def value = jedis.get("foo")
then:
value == "bar"
assertTraces(2) {
trace(0, 1) {
span(0) {
name "SET"
kind CLIENT
attributes {
"$SemanticAttributes.DB_SYSTEM" "redis"
"$SemanticAttributes.DB_STATEMENT" "SET foo ?"
"$SemanticAttributes.DB_OPERATION" "SET"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.NET_SOCK_PEER_ADDR" "127.0.0.1"
"$SemanticAttributes.NET_SOCK_PEER_NAME" { it == "localhost" || it == "127.0.0.1" }
"$SemanticAttributes.NET_SOCK_PEER_PORT" port
}
}
}
trace(1, 1) {
span(0) {
name "GET"
kind CLIENT
attributes {
"$SemanticAttributes.DB_SYSTEM" "redis"
"$SemanticAttributes.DB_STATEMENT" "GET foo"
"$SemanticAttributes.DB_OPERATION" "GET"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.NET_SOCK_PEER_ADDR" "127.0.0.1"
"$SemanticAttributes.NET_SOCK_PEER_NAME" { it == "localhost" || it == "127.0.0.1" }
"$SemanticAttributes.NET_SOCK_PEER_PORT" port
}
}
}
}
}
def "command with no arguments"() {
when:
jedis.set("foo", "bar")
def value = jedis.randomKey()
then:
value == "foo"
assertTraces(2) {
trace(0, 1) {
span(0) {
name "SET"
kind CLIENT
attributes {
"$SemanticAttributes.DB_SYSTEM" "redis"
"$SemanticAttributes.DB_STATEMENT" "SET foo ?"
"$SemanticAttributes.DB_OPERATION" "SET"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.NET_SOCK_PEER_ADDR" "127.0.0.1"
"$SemanticAttributes.NET_SOCK_PEER_NAME" { it == "localhost" || it == "127.0.0.1" }
"$SemanticAttributes.NET_SOCK_PEER_PORT" port
}
}
}
trace(1, 1) {
span(0) {
name "RANDOMKEY"
kind CLIENT
attributes {
"$SemanticAttributes.DB_SYSTEM" "redis"
"$SemanticAttributes.DB_STATEMENT" "RANDOMKEY"
"$SemanticAttributes.DB_OPERATION" "RANDOMKEY"
"$SemanticAttributes.NET_TRANSPORT" SemanticAttributes.NetTransportValues.IP_TCP
"$SemanticAttributes.NET_SOCK_PEER_ADDR" "127.0.0.1"
"$SemanticAttributes.NET_SOCK_PEER_NAME" { it == "localhost" || it == "127.0.0.1" }
"$SemanticAttributes.NET_SOCK_PEER_PORT" port
}
}
}
}
}
}

View File

@ -0,0 +1,165 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.jedis.v4_0;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.testcontainers.containers.GenericContainer;
import redis.clients.jedis.Jedis;
class Jedis40ClientTest {
@RegisterExtension
static final InstrumentationExtension testing = AgentInstrumentationExtension.create();
static GenericContainer<?> redisServer =
new GenericContainer<>("redis:6.2.3-alpine").withExposedPorts(6379);
static int port;
static Jedis jedis;
@BeforeAll
static void setupSpec() {
redisServer.start();
port = redisServer.getMappedPort(6379);
jedis = new Jedis("localhost", port);
}
@AfterAll
static void cleanupSpec() {
redisServer.stop();
jedis.close();
}
@BeforeEach
void setup() {
jedis.flushAll();
testing.clearData();
}
@Test
void setCommand() {
jedis.set("foo", "bar");
testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("SET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"),
equalTo(SemanticAttributes.DB_OPERATION, "SET"),
equalTo(
SemanticAttributes.NET_TRANSPORT,
SemanticAttributes.NetTransportValues.IP_TCP),
equalTo(SemanticAttributes.NET_SOCK_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"),
satisfies(
SemanticAttributes.NET_SOCK_PEER_NAME,
val -> val.isIn("localhost", "127.0.0.1")))));
}
@Test
void getCommand() {
jedis.set("foo", "bar");
String value = jedis.get("foo");
assertThat(value).isEqualTo("bar");
testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("SET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"),
equalTo(SemanticAttributes.DB_OPERATION, "SET"),
equalTo(
SemanticAttributes.NET_TRANSPORT,
SemanticAttributes.NetTransportValues.IP_TCP),
equalTo(SemanticAttributes.NET_SOCK_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"),
satisfies(
SemanticAttributes.NET_SOCK_PEER_NAME,
val -> val.isIn("localhost", "127.0.0.1")))),
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("GET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "GET foo"),
equalTo(SemanticAttributes.DB_OPERATION, "GET"),
equalTo(
SemanticAttributes.NET_TRANSPORT,
SemanticAttributes.NetTransportValues.IP_TCP),
equalTo(SemanticAttributes.NET_SOCK_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"),
satisfies(
SemanticAttributes.NET_SOCK_PEER_NAME,
val -> val.isIn("localhost", "127.0.0.1")))));
}
@Test
void commandWithNoArguments() {
jedis.set("foo", "bar");
String value = jedis.randomKey();
assertThat(value).isEqualTo("foo");
testing.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("SET")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "SET foo ?"),
equalTo(SemanticAttributes.DB_OPERATION, "SET"),
equalTo(
SemanticAttributes.NET_TRANSPORT,
SemanticAttributes.NetTransportValues.IP_TCP),
equalTo(SemanticAttributes.NET_SOCK_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"),
satisfies(
SemanticAttributes.NET_SOCK_PEER_NAME,
val -> val.isIn("localhost", "127.0.0.1")))),
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("RANDOMKEY")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.DB_SYSTEM, "redis"),
equalTo(SemanticAttributes.DB_STATEMENT, "RANDOMKEY"),
equalTo(SemanticAttributes.DB_OPERATION, "RANDOMKEY"),
equalTo(
SemanticAttributes.NET_TRANSPORT,
SemanticAttributes.NetTransportValues.IP_TCP),
equalTo(SemanticAttributes.NET_SOCK_PEER_PORT, port),
equalTo(SemanticAttributes.NET_SOCK_PEER_ADDR, "127.0.0.1"),
satisfies(
SemanticAttributes.NET_SOCK_PEER_NAME,
val -> val.isIn("localhost", "127.0.0.1")))));
}
}