localstack / localstack-java-utils

☕ Java utilities and JUnit integration for LocalStack
Apache License 2.0
75 stars 43 forks source link

UnknownHostException: localhost.localstack.cloud on mac os #68

Open vicmosin opened 3 years ago

vicmosin commented 3 years ago

I am using 0.2.15 version of the utils. The docker starts up without any issue, but the s3 client can not resolve the url.. here is the config I have:

@ExtendWith(LocalstackDockerExtension::class)
@LocalstackDockerProperties(services = [ServiceName.S3])
@SpringBootTest(classes = [Application::class, SomeControllerTest.S3TestConfiguration::class])
@TestPropertySource(
    properties = ["spring.main.allow-bean-definition-overriding=true"]
)
class SomeControllerTest : AbstractTest() {

    @Configuration
    class S3TestConfiguration {

        // override existing s3 client
        @Primary
        @Bean
        fun s3Client(): S3Client =
            S3Client.builder()
                .credentialsProvider(
                    StaticCredentialsProvider.create(
                        AwsBasicCredentials.create(
                            "key", "secret"
                        )))
                .endpointOverride(URI.create(Localstack.INSTANCE.endpointS3))
                .region(Region.AWS_GLOBAL)
                .serviceConfiguration { it.pathStyleAccessEnabled() }
                .build()
    }
   ...
}
Caused by: software.amazon.awssdk.core.exception.SdkClientException: Unable to execute HTTP request: localhost.localstack.cloud
    at software.amazon.awssdk.core.exception.SdkClientException$BuilderImpl.build(SdkClientException.java:98)
...
Caused by: java.net.UnknownHostException: localhost.localstack.cloud
    at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:800)
whummer commented 3 years ago

Thanks for reporting @vicmosin . This hostname should be resolvable, but I guess there's a few ISPs that may be blocking it from getting resolved.

Would it be an option for you to add an entry to the /etc/hosts file?

Alternatively, you should be able to customize the API endpoint address by setting this environment variable in your LocalStack container: LOCALSTACK_HOSTNAME=127.0.0.1. Hope that helps - please keep us posted how it goes. Thanks!

vicmosin commented 3 years ago

@whummer I finally had a time to check proposed solution.. Changing hosts file indeed not an option. And regarding the environment variable - can you please elaborate how exactly it must be set from the test? I tried the following way and it didn't work:

@LocalstackDockerProperties(services = [ServiceName.S3], environmentVariableProvider = DefaultEnvironmentVariableProvider::class)
...
class DefaultEnvironmentVariableProvider : IEnvironmentVariableProvider {

    override fun getEnvironmentVariables(): MutableMap<String, String> =
        mutableMapOf("LOCALSTACK_HOSTNAME" to "127.0.0.1")
}
whyWhale commented 1 year ago

Me too.

The endpoints are entered in a strange combination.

I am currently using mac os, and when connecting localstack and application through docker-compose, it is assembled as a strange endpoint.

The bucket name and the container name are concatenated with ., and the endpoint is passed, resulting in an UnknownHostException.

Especially when creating a bucket by code, an exception is thrown.

code >> amazonS3.deleteBucket(new DeleteBucketRequest(localStackProperty.bucket())); amazonS3.createBucket(new CreateBucketRequest(localStackProperty.bucket(), region));


Caused by: com.amazonaws.SdkClientException: Unable to execute HTTP request:   ${bucket_name}.${containerName}
Caused by: java.net.UnknownHostException:  ${bucket_name}.${containerName}