Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some initial integration tests #25

Merged
merged 7 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,9 @@ object Main extends App {
applicationName <- appDescription.applicationName
_ <- console.putStrLn(s"Got application description for $applicationName")

envsResult <- elasticbeanstalk.describeEnvironments(DescribeEnvironmentsRequest(applicationName = Some(applicationName)))
envs <- envsResult.environments
envStream = elasticbeanstalk.describeEnvironments(DescribeEnvironmentsRequest(applicationName = Some(applicationName)))

_ <- ZIO.foreach(envs) { env =>
_ <- envStream.run(Sink.foreach { env =>
env.environmentName.flatMap { environmentName =>
(for {
environmentId <- env.environmentId
Expand All @@ -184,7 +183,7 @@ object Main extends App {
instanceIds <- ZIO.foreach(instances)(_.id)
_ <- console.putStrLn(s"Instance IDs are ${instanceIds.mkString(", ")}")

reservationsStream <- ec2.describeInstancesStream(DescribeInstancesRequest(instanceIds = Some(instanceIds)))
reservationsStream = ec2.describeInstances(DescribeInstancesRequest(instanceIds = Some(instanceIds)))
_ <- reservationsStream.run(Sink.foreach {
reservation =>
reservation.instances.flatMap { instances =>
Expand All @@ -204,7 +203,7 @@ object Main extends App {
console.putStrLnErr(s"Failed to get info for $environmentName: $error")
}
}
}
})
} yield ()
case None =>
ZIO.unit
Expand Down
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ val awsSubVersion = awsVersion.drop(awsVersion.indexOf('.') + 1)
val http4sVersion = "0.21.7"
val fs2Version = "2.2.2"

val majorVersion = "1"
val minorVersion = "1"
val majorVersion = "2"
val minorVersion = "0"
val zioAwsVersion = s"$majorVersion.$awsSubVersion.$minorVersion"

val generateAll = taskKey[Unit]("Generates all AWS client libraries")
Expand Down
4 changes: 2 additions & 2 deletions examples/build.sbt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

val commonSettings = Seq(
scalaVersion := "2.13.2"
scalaVersion := "2.13.3"
)

val zioAwsVersion = "1.13.69.1"
val zioAwsVersion = "2.14.2.0"

lazy val example1 = Project("example1", file("example1")).settings(commonSettings).settings(
libraryDependencies ++= Seq(
Expand Down
29 changes: 11 additions & 18 deletions examples/example1/src/main/scala/Main.scala
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import scala.jdk.CollectionConverters._
import zio._
import zio.console
import zio.console._
import zio.stream._
import io.github.vigoo.zioaws.core
import io.github.vigoo.zioaws.http4s
import io.github.vigoo.zioaws.netty
import io.github.vigoo.zioaws.ec2
import io.github.vigoo.zioaws.ec2.model._
import io.github.vigoo.zioaws.core.AwsError
import io.github.vigoo.zioaws.ec2.Ec2
import io.github.vigoo.zioaws.elasticbeanstalk
import io.github.vigoo.zioaws.elasticbeanstalk.model._
import io.github.vigoo.zioaws.ec2.model._
import io.github.vigoo.zioaws.elasticbeanstalk.ElasticBeanstalk
import io.github.vigoo.zioaws.core.AwsError
import io.github.vigoo.zioaws.elasticbeanstalk.model._
import io.github.vigoo.zioaws.{core, ec2, elasticbeanstalk, http4s}
import zio.{console, _}
import zio.console._
import zio.stream._

object Main extends App {
val program: ZIO[Console with Ec2 with ElasticBeanstalk, AwsError, Unit] =
Expand All @@ -25,10 +19,9 @@ object Main extends App {
applicationName <- appDescription.applicationName
_ <- console.putStrLn(s"Got application description for $applicationName")

envsResult <- elasticbeanstalk.describeEnvironments(DescribeEnvironmentsRequest(applicationName = Some(applicationName)))
envs <- envsResult.environments
envStream = elasticbeanstalk.describeEnvironments(DescribeEnvironmentsRequest(applicationName = Some(applicationName)))

_ <- ZIO.foreach(envs) { env =>
_ <- envStream.run(Sink.foreach { env =>
env.environmentName.flatMap { environmentName =>
(for {
environmentId <- env.environmentId
Expand All @@ -41,7 +34,7 @@ object Main extends App {
instanceIds <- ZIO.foreach(instances)(_.id)
_ <- console.putStrLn(s"Instance IDs are ${instanceIds.mkString(", ")}")

reservationsStream <- ec2.describeInstancesStream(DescribeInstancesRequest(instanceIds = Some(instanceIds)))
reservationsStream = ec2.describeInstances(DescribeInstancesRequest(instanceIds = Some(instanceIds)))
_ <- reservationsStream.run(Sink.foreach {
reservation =>
reservation.instances.flatMap { instances =>
Expand All @@ -61,7 +54,7 @@ object Main extends App {
console.putStrLnErr(s"Failed to get info for $environmentName: $error")
}
}
}
})
} yield ()
case None =>
ZIO.unit
Expand Down
23 changes: 23 additions & 0 deletions integtests/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

scalaVersion := "2.13.3"

val zioAwsVersion = "2.14.2.1"

libraryDependencies ++= Seq(
"io.github.vigoo" %% "zio-aws-core" % "2.14.2.2",
"io.github.vigoo" %% "zio-aws-http4s" % "2.14.2.2",
"io.github.vigoo" %% "zio-aws-netty" % zioAwsVersion,
"io.github.vigoo" %% "zio-aws-s3" % zioAwsVersion,
"io.github.vigoo" %% "zio-aws-dynamodb" % zioAwsVersion,

"dev.zio" %% "zio" % "1.0.1",
"dev.zio" %% "zio-test" % "1.0.1",
"dev.zio" %% "zio-test-sbt" % "1.0.1",

"org.apache.logging.log4j" % "log4j-1.2-api" % "2.13.3",
"org.apache.logging.log4j" % "log4j-core" % "2.13.3",
"org.apache.logging.log4j" % "log4j-api" % "2.13.3",
"org.apache.logging.log4j" % "log4j-slf4j-impl" % "2.13.3",
)

testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")
18 changes: 18 additions & 0 deletions integtests/src/test/resources/log4j2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Configuration status="WARN">
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c:%L - %m%n" />
</Console>
</Appenders>

<Loggers>
<Root level="WARN">
<AppenderRef ref="ConsoleAppender"/>
</Root>
<Logger name="software.amazon.awssdk" level="WARN" />
<Logger name="software.amazon.awssdk.request" level="WARN" />
<Logger name="org.apache.http.wire" level="WARN" />
<logger name="org.http4s.client.middleware" level="DEBUG"/>
<logger name="org.http4s.server.middleware" level="DEBUG"/>
</Loggers>
</Configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package io.github.vigoo.zioaws.integtests

import java.net.URI

import io.github.vigoo.zioaws.core._
import io.github.vigoo.zioaws.{dynamodb, _}
import io.github.vigoo.zioaws.dynamodb.model._
import software.amazon.awssdk.auth.credentials.{AwsBasicCredentials, StaticCredentialsProvider}
import zio._
import zio.test.Assertion._
import zio.test._
import zio.test.environment.TestRandom

object DynamoDbTests extends DefaultRunnableSpec {

val nettyClient = netty.client()
val http4sClient = http4s.client()
val awsConfig = config.default
val dynamoDb = dynamodb.customized(
_.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("dummy", "key")))
.endpointOverride(new URI("http://localhost:4566"))
)

private def testTable = {
for {
_ <- TestRandom.setSeed(scala.util.Random.nextLong())
tableName <- generateName
env <- ZIO.environment[dynamodb.DynamoDb]
} yield ZManaged.make(
for {
tableData <- dynamodb.createTable(CreateTableRequest(
tableName = tableName,
attributeDefinitions = List(
AttributeDefinition("key", ScalarAttributeType.S)
),
keySchema = List(
KeySchemaElement("key", KeyType.HASH)
),
provisionedThroughput = Some(ProvisionedThroughput(
readCapacityUnits = 16L,
writeCapacityUnits = 16L
))
))
tableDesc <- tableData.tableDescription
} yield tableDesc
)(tableDescription =>
tableDescription.tableName.flatMap { tableName =>
dynamodb.deleteTable(DeleteTableRequest(tableName))
}.provide(env)
.catchAll(error => ZIO.die(error.toThrowable))
.unit)
}

def tests = Seq(
testM("can create and delete a table") {
// simple request/response calls
val steps = for {
table <- testTable
_ <- table.use { _ =>
ZIO.unit
}
} yield ()

assertM(steps.run)(succeeds(isUnit))
},
testM("scan") {
// java paginator based streaming

val N = 100
val steps = for {
table <- testTable
result <- table.use { tableDescription =>
val put =
for {
tableName <- tableDescription.tableName
randomKey <- random.nextString(10)
randomValue <- random.nextInt
_ <- dynamodb.putItem(PutItemRequest(
tableName = tableName,
item = Map(
"key" -> AttributeValue(s = Some(randomKey)),
"value" -> AttributeValue(n = Some(randomValue.toString))
)
))
} yield ()

for {
tableName <- tableDescription.tableName
_ <- put.repeatN(N - 1)
stream = dynamodb.scan(ScanRequest(
tableName = tableName,
limit = Some(10)
))
streamResult <- stream.runCollect
} yield streamResult
}
} yield result.length

assertM(steps)(equalTo(N))
},
testM("listTagsOfResource") {
// simple paginated streaming
val N = 1000
val steps = for {
table <- testTable
result <- table.use { tableDescription =>
for {
arn <- tableDescription.tableArn
_ <- dynamodb.tagResource(TagResourceRequest(
resourceArn = arn,
tags = (0 until N).map(i => dynamodb.model.Tag(s"tag$i", i.toString)).toList
))

tagStream = dynamodb.listTagsOfResource(ListTagsOfResourceRequest(
resourceArn = arn
))
tags <- tagStream.runCollect
} yield tags
}
} yield result.length

assertM(steps)(equalTo(N))
}
)

private def generateName =
ZIO.foreach((0 to 8).toList) { _ =>
random.nextIntBetween('a'.toInt, 'z'.toInt).map(_.toChar)
}.map(_.mkString)


override def spec = {
suite("DynamoDB")(
suite("with Netty")(
tests: _*
).provideCustomLayer((nettyClient >>> awsConfig >>> dynamoDb).mapError(TestFailure.die)),
suite("with http4s")(
tests: _*
).provideCustomLayer((http4sClient >>> awsConfig >>> dynamoDb).mapError(TestFailure.die)),
)
}
}
Loading