做网站一单能挣多少,什么样的网站好优化,零基础小程序开发教程,网站建设与设计实训总结aws 堆栈模板在AWS云堆栈 #xff08;例如DynamoDB#xff0c;S3等#xff09;上构建应用程序时#xff0c;需要针对这些组件编写测试。 您可能首先想到的是拥有一个用于生产的环境和另一个用于测试的环境#xff0c;然后针对该环境运行测试。 这对于集成测试#xff0c… aws 堆栈模板 在AWS云堆栈 例如DynamoDBS3等上构建应用程序时需要针对这些组件编写测试。 您可能首先想到的是拥有一个用于生产的环境和另一个用于测试的环境然后针对该环境运行测试。 这对于集成测试部署测试端到端测试或性能测试是很好的但是对于组件测试如果可以在本地和脱机运行AWS云堆栈 它将更快。 Localstack提供了此功能。 它提供了功能齐全的本地AWS云堆栈因此您可以脱机开发和测试云应用程序。 Localstack提供了启动所有堆栈的不同方法但最简单的方法是使用Docker映像。 所以如果你跑 atlassianlabs / localstack然后您将使用以下配置启动堆栈并运行它 位于http// localhost4567的API网关 Kinesis位于http// localhost4568 DynamoDB位于http// localhost4569 DynamoDB流位于http// localhost4570 位于http// localhost4571的Elasticsearch S3位于http// localhost4572 http// localhost4573上的Firehose Lambda位于http// localhost4574 SNS位于http// localhost4575 http// localhost4576上的SQS http// localhost4577上的Redshift 位于http// localhost4578的ESElasticsearch Service SES位于http// localhost4579 Route53位于http// localhost4580 http// localhost4581上的CloudFormation 位于http// localhost4582的CloudWatch 因此下一个问题是如何使启动容器运行测试以及最终停止所有操作并使它可移植的所有过程自动化因此您无需担心在Linux或MacOS中使用Docker的情况 答案是使用Arquillian Cube 。 Arquillian Cube是Arquillian扩展可用于在测试中管理Docker容器。 要使用它您需要在计算机上运行的Docker守护程序它可以是本地的也可以不是本地的但可能在本地。 Arquillian Cube提供了三种定义容器的不同方法 定义docker-compose文件。 定义容器对象。 使用容器对象DSL。 在此示例中我将向您展示“容器对象DSL”方法但其他方法也都可以。 您需要做的第一件事是在构建工具上添加Arquillian和Arquillian Cube依赖项。 dependencyManagementdependenciesdependencygroupIdorg.arquillian.cube/groupIdartifactIdarquillian-cube-docker/artifactIdversion1.6.0/version/dependencydependencygroupIdorg.jboss.arquillian/groupIdartifactIdarquillian-bom/artifactIdversion1.1.13.Final/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementdependenciesdependencygroupIdcom.amazonaws/groupIdartifactIdaws-java-sdk/artifactIdversion1.11.86/version/dependencydependencygroupIdorg.jboss.arquillian.junit/groupIdartifactIdarquillian-junit-standalone/artifactIdscopetest/scope/dependencydependencygroupIdorg.arquillian.cube/groupIdartifactIdarquillian-cube-docker/artifactIdscopetest/scope/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/versionscopetest/scope/dependencydependencygroupIdorg.assertj/groupIdartifactIdassertj-core/artifactIdversion3.6.2/versionscopetest/scope/dependency/dependencies 然后您可以编写测试在这种情况下该测试将测试您可以使用在Docker主机中启动的S3实例创建存储桶并添加一些内容 import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.S3Object;
import java.io.ByteArrayInputStream;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.arquillian.cube.docker.impl.client.containerobject.dsl.Container;
import org.arquillian.cube.docker.impl.client.containerobject.dsl.DockerContainer;
import org.jboss.arquillian.junit.Arquillian;
import org.junit.Test;
import org.junit.runner.RunWith;import static org.assertj.core.api.Assertions.assertThat;RunWith(Arquillian.class)
public class S3Test {DockerContainerContainer localStack Container.withContainerName(localstack).fromImage(atlassianlabs/localstack:0.5.3.1).withPortBinding(IntStream.rangeClosed(4567, 4578).boxed().collect(Collectors.toList()).toArray(new Integer[0])).withPortBinding(8080).build();Testpublic void should_create_bucket_and_add_content() {final AmazonS3Client amazonS3Client new AmazonS3Client();amazonS3Client.setEndpoint(http:// localStack.getIpAddress() :4572/);String bucketName my-first-s3-bucket- UUID.randomUUID();String key MyObjectKey;amazonS3Client.createBucket(bucketName);assertThat(amazonS3Client.listBuckets()).hasSize(1);amazonS3Client.putObject(bucketName, key, abcdef);final S3Object object amazonS3Client.getObject(bucketName, key);assertThat(object.getObjectContent()).hasSameContentAs(new ByteArrayInputStream(abcdef.getBytes()));}} 要考虑的重要事项 您用Arquillian赛跑者注释测试。 使用DockerContainer批注将属性用于定义容器。 容器对象DSL只是允许您配置要使用的容器的DSL。 在这种情况下 本地堆栈容器具有所需的端口绑定信息。 该测试仅连接到Amazon S3并创建一个存储桶并存储一些内容。 没有其他要求。 当您运行此测试时 Arquillian Cube将连接到已安装的DockerMachine主机并启动localstack容器。 当它启动并运行并且服务能够接收请求时将执行测试。 之后该容器停止并销毁。 提示1 如果不能使用Arquillian Runner也可以使用JUnit类规则来定义容器如下所述 http : //arquillian.org/arquillian-cube/#_junit_rule 提示2如果您打算在整个组织中使用localstack 建议您使用Container Object方法而不是DSL因为这样您就可以将localstack Container Object打包到jar文件中并导入所有需要使用它的项目。 您可以在http://arquillian.org/arquillian-cube/#_arquillian_cube_and_container_object中阅读 因此现在您只需使用本地环境即可为在AWS云上运行的应用程序编写测试而无需连接到远程主机。 我们不断学习 亚历克斯 翻译自: https://www.javacodegeeks.com/2017/06/test-aws-cloud-stack-offline-arquillian-localstack.htmlaws 堆栈模板