From 92e7167c99e1b07cd3833da2f18c094d367ecace Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 22 Sep 2020 12:55:30 +0100 Subject: [PATCH] Add test containers integration test to another sample --- .../function-sample-aws-custom/pom.xml | 11 ++++ .../src/main/resources/application.properties | 3 + .../test/java/com/example/ContainerTests.java | 64 +++++++++++++++++++ .../src/test/resources/testBootstrap | 6 ++ 4 files changed, 84 insertions(+) create mode 100644 spring-cloud-function-samples/function-sample-aws-custom/src/test/java/com/example/ContainerTests.java create mode 100755 spring-cloud-function-samples/function-sample-aws-custom/src/test/resources/testBootstrap diff --git a/spring-cloud-function-samples/function-sample-aws-custom/pom.xml b/spring-cloud-function-samples/function-sample-aws-custom/pom.xml index 2df2ec87e..9747b9398 100644 --- a/spring-cloud-function-samples/function-sample-aws-custom/pom.xml +++ b/spring-cloud-function-samples/function-sample-aws-custom/pom.xml @@ -66,6 +66,17 @@ reactor-test test + + org.awaitility + awaitility + test + + + org.testcontainers + testcontainers + 1.14.3 + test + diff --git a/spring-cloud-function-samples/function-sample-aws-custom/src/main/resources/application.properties b/spring-cloud-function-samples/function-sample-aws-custom/src/main/resources/application.properties index e847cb38a..9e8695a2c 100644 --- a/spring-cloud-function-samples/function-sample-aws-custom/src/main/resources/application.properties +++ b/spring-cloud-function-samples/function-sample-aws-custom/src/main/resources/application.properties @@ -1 +1,4 @@ spring.cloud.function.web.export.enabled=true +spring.cloud.function.web.export.debug=true +spring.main.web-application-type=none +logging.level.org.springframework.cloud=DEBUG \ No newline at end of file diff --git a/spring-cloud-function-samples/function-sample-aws-custom/src/test/java/com/example/ContainerTests.java b/spring-cloud-function-samples/function-sample-aws-custom/src/test/java/com/example/ContainerTests.java new file mode 100644 index 000000000..410b92559 --- /dev/null +++ b/spring-cloud-function-samples/function-sample-aws-custom/src/test/java/com/example/ContainerTests.java @@ -0,0 +1,64 @@ +/* + * Copyright 2019-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.example; + +import java.util.concurrent.TimeUnit; + +import org.awaitility.Awaitility; +import org.junit.jupiter.api.Test; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.output.ToStringConsumer; +import org.testcontainers.utility.MountableFile; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Dave Syer + * + */ +public class ContainerTests { + + @Test + void test() throws Exception { + ToStringConsumer consumer = new ToStringConsumer(); + try (@SuppressWarnings("resource") + GenericContainer container = new GenericContainer<>("lambci/lambda:provided").withLogConsumer(consumer) + .withCopyFileToContainer(MountableFile.forClasspathResource("testBootstrap"), "/var/task/bootstrap") + .withEnv("DOCKER_LAMBDA_STAY_OPEN", "1").withExposedPorts(9001)) { + container.start(); + int port = container.getFirstMappedPort(); + String host = container.getHost(); + LambdaApplication.main(new String[] { "--AWS_LAMBDA_RUNTIME_API=" + host + ":" + port, + "--_HANDLER=uppercase", "--logging.level.org.springframework=DEBUG" }); + ResponseEntity response = Awaitility.waitAtMost(5, TimeUnit.SECONDS).until(() -> { + ResponseEntity result = new RestTemplate().postForEntity( + "http://" + host + ":" + port + "/2015-03-31/functions/foobar/invocations", "foo", + String.class); + return result; + }, result -> result != null); + assertThat(response.getBody()).contains("hi foo!"); + assertThat(response.getHeaders()).containsKey("X-Amzn-Requestid"); + } + String output = consumer.toUtf8String(); + assertThat(output).contains("Lambda API listening on port 9001"); + assertThat(output).contains("START RequestId:"); + assertThat(output).contains("END RequestId:"); + } + +} diff --git a/spring-cloud-function-samples/function-sample-aws-custom/src/test/resources/testBootstrap b/spring-cloud-function-samples/function-sample-aws-custom/src/test/resources/testBootstrap new file mode 100755 index 000000000..142d4caad --- /dev/null +++ b/spring-cloud-function-samples/function-sample-aws-custom/src/test/resources/testBootstrap @@ -0,0 +1,6 @@ +#!/bin/sh + +while true +do + sleep 1 +done \ No newline at end of file