无法使用localstacktestrunner在gitlab runner上启动localstack

0yycz8jy  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(256)

我对运行java集成测试有一个问题 LocalstackTestRunner 在gitlab探员身上。我从官方的localstack网站上举了一个例子:

import cloud.localstack.LocalstackTestRunner;
import cloud.localstack.TestUtils;
import cloud.localstack.docker.annotation.LocalstackDockerProperties;

@RunWith(LocalstackTestRunner.class)
@LocalstackDockerProperties(services = { "s3", "sqs", "kinesis:77077" })
public class MyCloudAppTest {

  @Test
  public void testLocalS3API() {
    AmazonS3 s3 = TestUtils.getClientS3();
    List<Bucket> buckets = s3.listBuckets();
  }
}

在gradle的帮助下运行它 gradle clean test . 如果我在mac book上本地运行它—一切正常,但如果在gitlab代理上运行—则存在一个问题:

com.amazonaws.SdkClientException: Unable to execute HTTP request: Connect to localhost.localstack.cloud:4566 [localhost.localstack.cloud/127.0.0.1] failed: Connection refused (Connection refused)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.handleRetryableException(AmazonHttpClient.java:1207)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1153)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:802)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:770)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:744)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access$500(AmazonHttpClient.java:704)
    at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:686)

我的gitlab ci工作如下所示:

Localstack_test:
  stage: test
  services:
    - docker:dind
  when: always
  script:
    - ./gradlew clean test --stacktrace

s3客户端无法连接到 localhost.localstack.cloud:4566 因为docker容器是由 LocalstackTestRunner 在父级中启动 docker:dind 容器和amazons3客户端无法访问它。我尝试过其他aws服务-结果是一样的-aws客户端无法访问localstack端点。
我发现一些解决方法如下:
在gitlab ci中添加localstack作为服务
添加别名
expose env variable hostname\u external=别名
实施 IHostNameResolver 以gitlab ci中指定的外部主机名形式返回我的别名。
类似于:
gitlab ci:

Localstack_test:
  stage: test
  services:
    - docker:dind
    - name: localstack/localstack
      alias: localstack-it
  variables:
    HOSTNAME_EXTERNAL: "localstack-it"
  when: always
  script:
    - ./gradlew clean test --stacktrace |& tee -a ./gradle.log

java it测试:

@RunWith(LocalstackTestRunner.class)
@LocalstackDockerProperties(
        services = { "s3", "sqs", "kinesis:77077" },
        hostNameResolver = SystemEnvHostNameResolver.class
)
public class MyCloudAppTest {

    @Test
    public void testLocalS3API() {
        AmazonS3 s3 = TestUtils.getClientS3();
        List<Bucket> buckets = s3.listBuckets();
    }
}

public class SystemEnvHostNameResolver implements IHostNameResolver {
    private static final String HOSTNAME_EXTERNAL = "HOSTNAME_EXTERNAL";

    @Override
    public String getHostName() {
        String external = System.getenv(HOSTNAME_EXTERNAL);
        return !Strings.isNullOrEmpty(external) ?
                external :
                new LocalHostNameResolver().getHostName();
    }
}

它可以工作,但结果是运行了2个localstack docker容器,而内部docker容器仍然不可用。也许有人知道更好的解决办法?
街道:
梯度-6.7
云,我ocalstack:localstack-utils:0.2.5

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题