Search code examples
javaspringamazon-web-servicestestingtestcontainers

LocalStack TestContainer + AWS SQS + Spring Boot -> Unable to execute HTTP request


I have this interesting problem, and would be glad to receive any help

When I try to create queue, AmazonSQS client creates queue, but when i try to send messages it throws this exception Unable to execute HTTP request

CODE:

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@Testcontainers
public class AwsSqsTest {
private static final Network network = Network.newNetwork();
@Container
static final LocalStackContainer localStack = new LocalStackContainer(
DockerImageName.parse("localstack/localstack:latest"))
.withNetwork(network)
.withNetworkAliases("notthis", "localstack")
.withServices(Service.SQS);

    static AmazonSQS amazonSQSClient;
    final String SqsURL = "http://sqs.us-east-1.localstack:4566/000000000000/";
    
    @BeforeAll
    static void beforeAll() {
        String localStackEndpoint = localStack.getEndpoint().toString();
        amazonSQSClient = amazonSQS(
                localStack.getAccessKey(),
                localStack.getSecretKey(),
                localStack.getRegion(),
                localStackEndpoint
        );
    }
    
    static AmazonSQS amazonSQS(String accessKey, String secretKey, String region, String stackEndpoint) {
        var basicAWSCredentials = new BasicAWSCredentials(accessKey, secretKey);
        var endpointConfiguration = new AwsClientBuilder.EndpointConfiguration(stackEndpoint, region);
        var credentialsProvider = new AWSStaticCredentialsProvider(basicAWSCredentials);
        return AmazonSQSClientBuilder.standard()
                .withEndpointConfiguration(endpointConfiguration)
                .withCredentials(credentialsProvider)
                .build();
    }
    
    @Test
    @Order(1)
    void testSQSClientInitialization() {
        assertNotNull(amazonSQSClient);
    }
    
    @ParameterizedTest
    @ValueSource(strings = {"news", "trends"})
    @Order(2)
    void createQueueTest(String queueName) {
        amazonSQSClient.createQueue(queueName);
    
        var queues = amazonSQSClient.listQueues().getQueueUrls();
    
        assertTrue(queues.contains(SqsURL + queueName));
    }
    
    
    @ParameterizedTest
    @MethodSource("saveToQueue")
    @Order(3)
    void saveToQueue(String queueName, String data) {
        String url = SqsURL + queueName;
        amazonSQSClient.sendMessage(url, data);
    
        var messages = amazonSQSClient.receiveMessage(url);
    
        assertEquals(data, messages.getMessages().getFirst().getBody());
    }
    
    Stream<Arguments> saveToQueue() {
        return Stream.of(
                arguments("trends", "{}"),
                arguments("news", "{}")
        );
    }

}

LOGS:


Unable to execute HTTP request: sqs.us-east-1.localstack
com.amazonaws.SdkClientException: Unable to execute HTTP request: sqs.us-east-1.localstack
...
Caused by: java.net.UnknownHostException: sqs.us-east-1.localstack
...

sqs.us-east-1.localstack
java.net.UnknownHostException: sqs.us-east-1.localstack
...

enter image description here

I tried to use other implementations of AmazonSQS and use docs, and nothing helped;(


Solution

  • Add the following env vars to LocalStackContainer

    .withEnv("LOCALSTACK_HOST", "localhost.localstack.cloud")
    .withEnv("SQS_ENDPOINT_STRATEGY", "dynamic")
    

    The LOCALSTACK_HOST keeps the host for LocalStack internal communication and SQS_ENDPOINT_STRATEGY resolves the host from the client.