-
Notifications
You must be signed in to change notification settings - Fork 21
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
Add LocalStack (V1) container for zio 2.0 #41
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2021 io.github.scottweaver | ||
* | ||
* 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 | ||
* | ||
* http://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 io.github.scottweaver.zio.testcontainers.localstack | ||
|
||
import com.dimafeng.testcontainers.LocalStackContainer | ||
import zio._ | ||
|
||
object ZLocalStackContainer { | ||
|
||
type Settings = LocalStackContainer.Def | ||
|
||
object Settings { | ||
|
||
val default: ULayer[Settings] = | ||
ZLayer.succeed(LocalStackContainer.Def()) | ||
|
||
def withServices(services: LocalStackContainer.Service*): ULayer[Settings] = | ||
ZLayer.succeed(LocalStackContainer.Def(services = services)) | ||
|
||
} | ||
|
||
val live: ZLayer[Settings, Nothing, LocalStackContainer] = | ||
ZLayer.scoped { | ||
for { | ||
settings <- ZIO.service[Settings] | ||
container <- makeContainer(settings) | ||
} yield container | ||
} | ||
|
||
def makeContainer(settings: Settings): ZIO[Scope, Nothing, LocalStackContainer] = | ||
ZIO.acquireRelease(ZIO.attempt(settings.start()).orDie)(container => ZIO.attempt(container.stop()).ignoreLogged) | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration> | ||
|
||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder"> | ||
<layout class="ch.qos.logback.classic.PatternLayout"> | ||
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} %msg%n</Pattern> | ||
</layout> | ||
</encoder> | ||
</appender> | ||
|
||
|
||
<root level="WARN"> | ||
<appender-ref ref="STDOUT" /> | ||
</root> | ||
|
||
<logger name="org.testcontainers" level="WARN" /> | ||
<logger name="com.github.dockerjava" level="WARN" /> | ||
<logger name="ch.qos.logback" level="WARN" /> | ||
</configuration> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2021 io.github.scottweaver | ||
* | ||
* 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 | ||
* | ||
* http://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 io.github.scottweaver.zio.testcontainers.localstack | ||
|
||
import com.dimafeng.testcontainers.LocalStackContainer | ||
import org.testcontainers.containers.localstack.{LocalStackContainer => JavaLocalStackContainer} | ||
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider | ||
import zio._ | ||
import zio.aws.core.config.{AwsConfig, CommonAwsConfig} | ||
import zio.aws.netty.NettyHttpClient | ||
import zio.aws.s3.S3 | ||
import zio.aws.s3.model.CreateBucketRequest | ||
import zio.aws.s3.model.primitives.BucketName | ||
import zio.test.Assertion._ | ||
import zio.test._ | ||
|
||
object ZLocalStackContainerSpec extends ZIOSpecDefault { | ||
|
||
override def spec = | ||
suite("ZLocalStackContainerSpec")( | ||
test("Should start a LocalStack container and create S3 bucket") { | ||
val testCase = | ||
for { | ||
s3 <- ZIO.service[S3] | ||
_ <- s3.createBucket(CreateBucketRequest(bucket = BucketName("foo"))) | ||
listBuckets <- s3.listBuckets().flatMap(_.getBuckets) | ||
listBucketNames <- ZIO.foreach(listBuckets)(_.getName) | ||
} yield assert(listBucketNames)(equalTo(List("foo"))) | ||
|
||
testCase | ||
} | ||
).provideShared( | ||
ZLocalStackContainer.Settings.withServices(JavaLocalStackContainer.Service.S3), | ||
ZLocalStackContainer.live, | ||
s3Layer, | ||
AwsConfig.configured(), | ||
commonAwsConfigLayer, | ||
NettyHttpClient.default | ||
) | ||
|
||
private def s3Layer: RLayer[LocalStackContainer with AwsConfig, S3] = | ||
ZLayer.scoped { | ||
for { | ||
localstack <- ZIO.service[LocalStackContainer] | ||
endpoint <- ZIO.attempt(localstack.container.getEndpointOverride(JavaLocalStackContainer.Service.S3)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This shows an example of overriding endpoints of LocalStack services. |
||
s3 <- S3.scoped(_.endpointOverride(endpoint)) | ||
} yield s3 | ||
} | ||
|
||
private def commonAwsConfigLayer: ULayer[CommonAwsConfig] = | ||
ZLayer.succeed( | ||
CommonAwsConfig( | ||
region = Some(software.amazon.awssdk.regions.Region.US_EAST_1), | ||
credentialsProvider = DefaultCredentialsProvider.builder().build(), | ||
endpointOverride = None, | ||
commonClientConfig = None | ||
) | ||
) | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,9 @@ object V { | |
val zioKafkaVersion = "0.17.1" | ||
val zio2KafkaVersion = "2.0.0" | ||
val zioHttpVersion = "0.0.4" | ||
val testcontainersScalaVersion = "0.40.10" | ||
val zioAwsVersion = "5.20.17.1" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is the latest version of |
||
val awsV1Version = "1.11.479" | ||
val testcontainersScalaVersion = "0.40.15" | ||
val mysqlConnnectorJVersion = "8.0.29" | ||
val postgresqlDriverVersion = "42.3.3" | ||
val slf4jVersion = "1.7.32" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the way
testcontainers-scala
provides these dependencies. I decided to stick to their approach.