使用Arquillian和LocalStack脱机测试AWS云堆栈
在AWS云堆棧 (例如DynamoDB,S3等)上構(gòu)建應(yīng)用程序時(shí),需要針對這些組件編寫測試。 您可能首先想到的是擁有一個(gè)用于生產(chǎn)的環(huán)境和一個(gè)用于測試的環(huán)境,然后針對該環(huán)境運(yùn)行測試。
這對于集成測試,部署測試,端到端測試或性能測試是很好的,但是對于組件測試,如果可以在本地和脫機(jī)運(yùn)行AWS云堆棧 ,它將更快。
Localstack提供了此功能。 它提供了功能齊全的本地AWS云堆棧,因此您可以脫機(jī)開發(fā)和測試云應(yīng)用程序。
Localstack提供了啟動(dòng)所有堆棧的不同方法,但是最簡單的方法是使用Docker映像。 所以如果你跑
atlassianlabs / localstack,然后您就可以啟動(dòng)堆棧并使用下一個(gè)配置運(yùn)行它:
- 位于http:// localhost:4567的API網(wǎng)關(guān)
- Kinesis位于http:// localhost:4568
- DynamoDB位于http:// localhost:4569
- DynamoDB流位于http:// localhost:4570
- 位于http:// localhost:4571的Elasticsearch
- S3位于http:// localhost:4572
- Firehose位于http:// localhost:4573
- Lambda位于http:// localhost:4574
- SNS位于http:// localhost:4575
- SQS位于http:// localhost:4576
- http:// localhost:4577上的Redshift
- 位于http:// localhost:4578的ES(Elasticsearch Service)
- SES位于http:// localhost:4579
- 位于http:// localhost:4580的Route53
- http:// localhost:4581上的CloudFormation
- 位于http:// localhost:4582的CloudWatch
因此,下一個(gè)問題是如何使啟動(dòng)容器,運(yùn)行測試以及最終停止所有操作并使它可移植的所有過程自動(dòng)化,因此您無需擔(dān)心在Linux或MacOS中使用Docker的情況? 答案是使用Arquillian Cube 。
Arquillian Cube是Arquillian擴(kuò)展,可用于在測試中管理Docker容器。 要使用它,您需要在計(jì)算機(jī)上運(yùn)行的Docker守護(hù)程序(它可以是本地的,也可以不是本地的),但可能在本地。
Arquillian Cube提供了三種定義容器的方法:
- 定義docker-compose文件。
- 定義容器對象。
- 使用容器對象DSL。
在此示例中,我將向您展示“容器對象DSL”方法,但其他方法也都可以使用。
您需要做的第一件事是在構(gòu)建工具上添加Arquillian和Arquillian Cube依賴項(xiàng)。
<dependencyManagement><dependencies><dependency><groupId>org.arquillian.cube</groupId><artifactId>arquillian-cube-docker</artifactId><version>1.6.0</version></dependency><dependency><groupId>org.jboss.arquillian</groupId><artifactId>arquillian-bom</artifactId><version>1.1.13.Final</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>com.amazonaws</groupId><artifactId>aws-java-sdk</artifactId><version>1.11.86</version></dependency><dependency><groupId>org.jboss.arquillian.junit</groupId><artifactId>arquillian-junit-standalone</artifactId><scope>test</scope></dependency><dependency><groupId>org.arquillian.cube</groupId><artifactId>arquillian-cube-docker</artifactId><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><dependency><groupId>org.assertj</groupId><artifactId>assertj-core</artifactId><version>3.6.2</version><scope>test</scope></dependency></dependencies>然后,您可以編寫測試,在這種情況下,該測試將測試您可以使用在Docker主機(jī)中啟動(dòng)的S3實(shí)例創(chuàng)建存儲(chǔ)桶并添加一些內(nèi)容:
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()));}}要考慮的重要事項(xiàng):
沒有其他要求。 當(dāng)您運(yùn)行此測試時(shí), Arquillian Cube將連接到已安裝的Docker(Machine)主機(jī)并啟動(dòng)localstack容器。 當(dāng)它啟動(dòng)并運(yùn)行并且服務(wù)能夠接收請求時(shí),將執(zhí)行測試。 之后,將容器停止并銷毀。
提示1 :如果不能使用Arquillian Runner,也可以使用JUnit類規(guī)則來定義容器,如下所述: http : //arquillian.org/arquillian-cube/#_junit_rule
提示2:如果您打算在整個(gè)組織中使用localstack ,建議您使用Container Object方法而不是DSL,因?yàn)檫@樣您可以將localstack Container Object打包到j(luò)ar文件中,并導(dǎo)入所有需要使用它的項(xiàng)目。 您可以在http://arquillian.org/arquillian-cube/#_arquillian_cube_and_container_object中閱讀
因此,現(xiàn)在您只需使用本地環(huán)境即可為在AWS云上運(yùn)行的應(yīng)用程序編寫測試,而不必連接到遠(yuǎn)程主機(jī)。
我們不斷學(xué)習(xí),
亞歷克斯
翻譯自: https://www.javacodegeeks.com/2017/06/test-aws-cloud-stack-offline-arquillian-localstack.html
總結(jié)
以上是生活随笔為你收集整理的使用Arquillian和LocalStack脱机测试AWS云堆栈的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设置资源限制防ddos攻击怎么解决(设置
- 下一篇: 安卓试题功能(安卓试题)