Skip to content

Commit

Permalink
use GenericContainer (fix for CircleCI)
Browse files Browse the repository at this point in the history
  • Loading branch information
joeyzhao2018 committed May 24, 2024
1 parent b2d6915 commit ae1a5b5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import datadog.trace.agent.test.utils.TraceUtils
import datadog.trace.api.DDSpanTypes
import datadog.trace.api.config.GeneralConfig
import datadog.trace.bootstrap.instrumentation.api.Tags
import org.testcontainers.containers.localstack.LocalStackContainer
import org.testcontainers.utility.DockerImageName
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider
Expand All @@ -18,29 +17,36 @@ import software.amazon.awssdk.services.sqs.model.QueueAttributeName
import spock.lang.Shared
import groovy.json.JsonSlurper

import java.time.Duration
import org.testcontainers.containers.GenericContainer
import static datadog.trace.agent.test.utils.TraceUtils.basicSpan
import static org.testcontainers.containers.localstack.LocalStackContainer.Service.SNS
import static org.testcontainers.containers.localstack.LocalStackContainer.Service.SQS

abstract class SnsClientTest extends VersionedNamingTestBase {
static final LOCALSTACK = new LocalStackContainer(DockerImageName.parse("localstack/localstack:latest"))
.withServices( SQS, SNS)

static final LOCALSTACK = new GenericContainer(DockerImageName.parse("localstack/localstack"))
.withExposedPorts(4566) // Default LocalStack port
.withEnv("SERVICES", "sns,sqs") // Enable SNS and SQS service
.withReuse(true)
.withStartupTimeout(Duration.ofSeconds(120))

@Shared AmazonSNSClient snsClient
@Shared SqsClient sqsClient

@Shared String testQueueURL
@Shared String testQueueARN
@Shared String testTopicARN


def setupSpec() {
LOCALSTACK.start()
def endPoint = "http://" + LOCALSTACK.getHost() + ":" + LOCALSTACK.getMappedPort(4566)
snsClient = AmazonSNSClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration( LOCALSTACK.getEndpointOverride(SNS).toString(), LOCALSTACK.getRegion()))
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endPoint, "us-east-1"))
.withCredentials( new AWSStaticCredentialsProvider(new BasicAWSCredentials("test", "test")))
.build()
sqsClient = SqsClient.builder()
.endpointOverride(LOCALSTACK.getEndpointOverride(SQS))
.region(Region.of(LOCALSTACK.getRegion()))
.endpointOverride(URI.create(endPoint))
.region(Region.of("us-east-1"))
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
.build()
testQueueURL = sqsClient.createQueue { it.queueName("testqueue") }.queueUrl()
Expand Down Expand Up @@ -83,6 +89,7 @@ abstract class SnsClientTest extends VersionedNamingTestBase {
def message = sqsClient.receiveMessage {it.queueUrl(testQueueURL).waitTimeSeconds(3)}.messages().get(0)
def jsonSlurper = new JsonSlurper()
def messageBody = jsonSlurper.parseText(message.body())
def endPoint = "http://" + LOCALSTACK.getHost() + ":" + LOCALSTACK.getMappedPort(4566)

then:
def sendSpan
Expand All @@ -100,14 +107,14 @@ abstract class SnsClientTest extends VersionedNamingTestBase {
tags {
"$Tags.COMPONENT" "java-aws-sdk"
"$Tags.SPAN_KIND" Tags.SPAN_KIND_CLIENT
"$Tags.HTTP_URL" LOCALSTACK.getEndpointOverride(SNS).toString()+'/'
"$Tags.HTTP_URL" endPoint+'/'
"$Tags.HTTP_METHOD" "POST"
"$Tags.HTTP_STATUS" 200
"$Tags.PEER_PORT" LOCALSTACK.getEndpointOverride(SNS).port
"$Tags.PEER_HOSTNAME" LOCALSTACK.getEndpointOverride(SNS).host
"$Tags.PEER_PORT" LOCALSTACK.getMappedPort(4566)
"$Tags.PEER_HOSTNAME" LOCALSTACK.getHost()
"aws.service" "AmazonSNS"
"aws_service" "sns"
"aws.endpoint" LOCALSTACK.getEndpointOverride(SNS).toString()
"aws.endpoint" endPoint
"aws.operation" "PublishRequest"
"aws.agent" "java-aws-sdk"
"aws.topic.name" "testtopic"
Expand All @@ -128,11 +135,11 @@ abstract class SnsClientTest extends VersionedNamingTestBase {
tags {
"$Tags.COMPONENT" "apache-httpclient"
"$Tags.SPAN_KIND" Tags.SPAN_KIND_CLIENT
"$Tags.HTTP_URL" LOCALSTACK.getEndpointOverride(SNS).toString()+'/'
"$Tags.HTTP_URL" endPoint+'/'
"$Tags.HTTP_METHOD" "POST"
"$Tags.HTTP_STATUS" 200
"$Tags.PEER_PORT" LOCALSTACK.getEndpointOverride(SNS).port
"$Tags.PEER_HOSTNAME" LOCALSTACK.getEndpointOverride(SNS).host
"$Tags.PEER_PORT" LOCALSTACK.getMappedPort(4566)
"$Tags.PEER_HOSTNAME" LOCALSTACK.getHost()
defaultTags(true)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import datadog.trace.agent.test.utils.TraceUtils
import datadog.trace.api.DDSpanTypes
import datadog.trace.api.config.GeneralConfig
import datadog.trace.bootstrap.instrumentation.api.Tags
import org.testcontainers.containers.localstack.LocalStackContainer
import org.testcontainers.containers.GenericContainer
import org.testcontainers.utility.DockerImageName
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider
Expand All @@ -16,13 +16,17 @@ import software.amazon.awssdk.services.sqs.model.QueueAttributeName
import spock.lang.Shared
import groovy.json.JsonSlurper

import java.time.Duration

import static datadog.trace.agent.test.utils.TraceUtils.basicSpan
import static org.testcontainers.containers.localstack.LocalStackContainer.Service.SNS
import static org.testcontainers.containers.localstack.LocalStackContainer.Service.SQS

abstract class SnsClientTest extends VersionedNamingTestBase {
static final LOCALSTACK = new LocalStackContainer(DockerImageName.parse("localstack/localstack:latest"))
.withServices( SQS, SNS)
static final LOCALSTACK = new GenericContainer(DockerImageName.parse("localstack/localstack"))
.withExposedPorts(4566) // Default LocalStack port
.withEnv("SERVICES", "sns,sqs") // Enable SNS and SQS service
.withReuse(true)
.withStartupTimeout(Duration.ofSeconds(120))

@Shared SnsClient snsClient
@Shared SqsClient sqsClient

Expand All @@ -32,14 +36,15 @@ abstract class SnsClientTest extends VersionedNamingTestBase {

def setupSpec() {
LOCALSTACK.start()
def endPoint = "http://" + LOCALSTACK.getHost() + ":" + LOCALSTACK.getMappedPort(4566)
snsClient = SnsClient.builder()
.endpointOverride(LOCALSTACK.getEndpointOverride(LocalStackContainer.Service.SNS))
.region(Region.of(LOCALSTACK.getRegion()))
.endpointOverride(URI.create(endPoint))
.region(Region.of("us-east-1"))
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
.build()
sqsClient = SqsClient.builder()
.endpointOverride(LOCALSTACK.getEndpointOverride(SQS))
.region(Region.of(LOCALSTACK.getRegion()))
.endpointOverride(URI.create(endPoint))
.region(Region.of("us-east-1"))
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
.build()
testQueueURL = sqsClient.createQueue { it.queueName("testqueue") }.queueUrl()
Expand Down Expand Up @@ -83,6 +88,7 @@ abstract class SnsClientTest extends VersionedNamingTestBase {
def message = sqsClient.receiveMessage {it.queueUrl(testQueueURL).waitTimeSeconds(3)}.messages().get(0)
def jsonSlurper = new JsonSlurper()
def messageBody = jsonSlurper.parseText(message.body())
def endPoint = "http://" + LOCALSTACK.getHost() + ":" + LOCALSTACK.getMappedPort(4566)

then:
def sendSpan
Expand All @@ -100,11 +106,11 @@ abstract class SnsClientTest extends VersionedNamingTestBase {
tags {
"$Tags.COMPONENT" "java-aws-sdk"
"$Tags.SPAN_KIND" Tags.SPAN_KIND_CLIENT
"$Tags.HTTP_URL" LOCALSTACK.getEndpointOverride(SNS).toString()+'/'
"$Tags.HTTP_URL" endPoint+'/'
"$Tags.HTTP_METHOD" "POST"
"$Tags.HTTP_STATUS" 200
"$Tags.PEER_PORT" LOCALSTACK.getEndpointOverride(SNS).port
"$Tags.PEER_HOSTNAME" LOCALSTACK.getEndpointOverride(SNS).host
"$Tags.PEER_PORT" LOCALSTACK.getMappedPort(4566)
"$Tags.PEER_HOSTNAME" LOCALSTACK.getHost()
"aws.service" "Sns"
"aws_service" "Sns"
"aws.operation" "Publish"
Expand Down

0 comments on commit ae1a5b5

Please sign in to comment.