5

我正在尝试为 Spring 引导服务提出一些集成测试。由于该服务使用 AWS SQS 和 DynamoDB,我倾向于利用测试容器的 Localstack 模块进行集成测试。但是虽然我认为我已经包含了所有必要的代码,但 LocalStackContainer 似乎没有运行,并且引发了以下错误:

com.amazonaws.SdkClientException: Unable to execute HTTP request: Connect to localhost:4576
  [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused (Connection refused)

顺便说一句,如果我使用独立的 localstack(即,在终端“$localstack start”中手动运行),集成测试将通过。

谁能帮我弄清楚我错过了什么?

在 build.gradle 我有

testCompile("org.testcontainers:testcontainers:1.10.6")
testCompile("org.testcontainers:localstack:1.10.6")

在一个超类中,我设置了一些像这样的共享测试上下文,包括 LocalStackContainer 作为@ClassRule

@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("local")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class)
@Slf4j
public class BaseIntegrationTest extends CamelTestSupport {

    @Value("${isLocal:false}")
    protected boolean isLocal;

    @Value("${isLocalStack:true}")
    protected boolean isLocalStack;

    @Value("${instanceUrl}")
    protected String instanceUrl;

    @LocalServerPort
    private int serverPort;

    @EndpointInject(uri = "mock:endPoint")
    protected MockEndpoint mockEndpoint;

    protected ApplicationContext appContext;
    protected AmazonDynamoDB amazonDynamoDB;
    protected AmazonSQS amazonSQS;
    protected CamelContext camelContext;
    protected Exchange exchange;
    protected ProducerTemplate producerTemplate;

    @ClassRule
    public static LocalStackContainer localstack = new LocalStackContainer()
            .withEnv("HOSTNAME_EXTERNAL", DockerClientFactory.instance().dockerHostIpAddress())
            .withExposedPorts(4569, 4576) // mock DynamoDB (http port 4569), mock SQS (http port 4576)
            .withStartupAttempts(3)
            .withStartupTimeout(Duration.ofMinutes(3))
            .withServices(DYNAMODB, SQS);

    @BeforeClass
    public static void init() {
        localstack.start();
    }

    @AfterClass
    public static void teardown() {
        localstack.stop();
    }

    @Before
    public void setup() throws Exception {
        RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
        RestAssured.port = serverPort;

        if (instanceUrl != null) {
            RestAssured.baseURI = instanceUrl;
        } else {
            RestAssured.requestSpecification = new RequestSpecBuilder()
                    .setBaseUri(System.getProperty("APP_HOST", "http://localhost:" + serverPort))
                    .build();
        }

        String dynamoDBEndpoint = "http://" + localstack.getContainerIpAddress() 
                                            + ":" + localstack.getMappedPort(4569);
        String sqsEndpoint = "http://" + localstack.getContainerIpAddress() 
                                       + ":" + localstack.getMappedPort(4576);

        log.info("The integration test is using localstack dynamoDBEndpoint={} and sqsEndpoint={}",
                dynamoDBEndpoint, sqsEndpoint);

        appContext = new SpringApplicationBuilder(Application.class)
                .profiles("local")
                .properties("localstackDynamoDBEndpoint=" + dynamoDBEndpoint,
                        "localstackSQSEndpoint=" + sqsEndpoint)
                .run();

        amazonDynamoDB = appContext.getBean(AmazonDynamoDB.class);
        amazonSQS = appContext.getBean(AmazonSQS.class);
        camelContext = appContext.getBean(CamelContext.class);
        producerTemplate = camelContext.createProducerTemplate();
    }

    @After
    public void cleanup() {
        ((ConfigurableApplicationContext) appContext).close();
    }

}

在扩展超类的测试类中,只是一些这样放心的代码

@Test
public void test400Response() {

    given()
            .body("")
            .contentType("application/json")
            .post("/root/my_service/v1")
            .then()
            .statusCode(HttpStatus.SC_BAD_REQUEST)
            .body("message", Matchers.equalTo("Missing expected content"))
            .log()
            .all();
}

如果你想看看,这里是成功测试的日志

https://justpaste.it/success-with-running-localstack

以及失败测试的日志

https://justpaste.it/failed-with-localstackContainer

4

1 回答 1

0

我离开了测试容器,转而使用 Dockercompose。这就是我现在用 Gradle 做的事情。

在 build.gradle 中,添加

apply from: file('gradle/integration.gradle')

然后在 gradle 文件夹的 integration.gradle 文件中,添加

apply plugin: 'docker-compose'

dockerCompose {
    useComposeFiles = ["${rootDir}/localstack/docker-compose.yml"]
}

并定义一个 integrationTest 任务

task integrationTest (type: Test) { 
 //....
}

使用适当的 docker-compose.yml 包括 Localstack docker 映像和 gradle 文件中的其他内容,我可以成功运行它

./gradlew clean integrationTest
于 2021-01-31T18:51:15.557 回答