Restructure and simplify AWS Custom Runtime code

Add Custom Runtime emulator to simplify integration testing
This commit is contained in:
Oleg Zhurakousky
2021-10-20 17:52:52 +02:00
parent f5707e88c4
commit 12cf0d5763
8 changed files with 316 additions and 274 deletions

View File

@@ -1,66 +0,0 @@
/*
* 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.Disabled;
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
@Disabled
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<String> response = Awaitility.waitAtMost(5, TimeUnit.SECONDS).until(() -> {
ResponseEntity<String> result = new RestTemplate().postForEntity(
"http://" + host + ":" + port + "/2015-03-31/functions/foobar/invocations", "foo",
String.class);
return result;
}, result -> result != null);
assertThat(response.getBody()).isEqualTo("\"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:");
}
}

View File

@@ -0,0 +1,45 @@
/*
* 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 static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.cloud.function.adapter.test.aws.AWSCustomRuntime;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
/**
* @author Oleg Zhurakousky
*
*/
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {"spring.main.web-application-type=servlet"})
@ContextConfiguration(classes = {AWSCustomRuntime.class}, initializers = LambdaApplication.class)
@TestPropertySource(properties = {"_HANDLER=uppercase"})
public class LambdaApplicationTests {
@Autowired
private AWSCustomRuntime aws;
@Test
void testWithCustomRuntime() throws Exception {
assertThat(aws.exchange("\"oleg\"").getPayload()).isEqualTo("\"OLEG\"");
assertThat(aws.exchange("\"dave\"").getPayload()).isEqualTo("\"DAVE\"");
}
}