Testcontainers for ZIO

main

Provides idiomatic, easy-to-use ZLayers for Testcontainers-scala.

Testcontainers Best-Practices

SBT Settings

  • Make sure your test configuration has the following settings Test / fork := true. Without this the Docker container created by the test will NOT be cleaned up until you exit SBT/the JVM process. This could quickly run your machine out of resources as you will end up with a ton of orphaned containers running.
  • Use provideLayerShared({container}.live) on your suite so that each test case isn't spinning up and down the container.

Logging

The testcontainer runtime can get pretty noisy when it comes to logging. To reduce this noise in your test cases, I recommend creating (or updating) the src/test/resources/logback-test.xml file in your project based on the following sample:

<?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" />
  <!-- Only relevant if you are using the db migration aspect -->
  <logger name="org.flywaydb" level="WARN" />
</configuration>

Cassandra

Release Artifacts

Provides a managed ZLayer that starts and stops a com.dimafeng.testcontainers.CassandraContainer as well as also providing a managed com.datastax.oss.driver.api.core.CqlSession.

ZIO 1.x

libraryDependencies += "io.github.scottweaver" %% "zio-testcontainers-cassandra" % "0.10.0"

ZIO 2.x

libraryDependencies += "io.github.scottweaver" %% "zio-2-0-testcontainers-cassandra" % "0.10.0"

See test cases for example usage.

MySQL

Release Artifacts

Provides a managed ZLayer that starts and stops a com.dimafeng.testcontainers.MySQLTestContainer as well as also providing a managed java.sql.Connection and a javax.sql.DataSource.

ZIO 1.x

libraryDependencies += "io.github.scottweaver" %% "zio-testcontainers-mysql" % "0.10.0"

ZIO 2.x

libraryDependencies += "io.github.scottweaver" %% "zio-2-0-testcontainers-mysql" % "0.10.0"

See test cases for example usage.

PostgreSQL

Release Artifacts

Provides a managed ZLayer that starts and stops a com.dimafeng.testcontainers.PostgreSQLContainer as well as also providing a managed java.sql.Connection and a javax.sql.DataSource.

ZIO 1.x

libraryDependencies += "io.github.scottweaver" %% "zio-testcontainers-postgresql" % "0.10.0"

ZIO 2.x

libraryDependencies += "io.github.scottweaver" %% "zio-2-0-testcontainers-postgresql" % "0.10.0"

See test cases for example usage.

Kafka

Release Artifacts

Provides a managed ZLayer that starts and stops a com.dimafeng.testcontainers.KafkaContainer.

You also have easy access to:

  • zio.kafka.consumer.ConsumerSettings via ZKafkaContainer.defaultConsumerSettings.
  • zio.kafka.consumer.ProducerSettings via ZKafkaContainer.defaultProducerSettings.

You can use these to create a zio.kafka.consumer.Consumer and/or zio.kafka.producer.Producer that can be used to interact with the Kafka container instance.

ZIO 1.x

libraryDependencies += "io.github.scottweaver" %% "zio-testcontainers-kafka" % "0.10.0"

ZIO 2.x

libraryDependencies += "io.github.scottweaver" %% "zio-2-0-testcontainers-kafka" % "0.10.0"

See test cases for example usage.

Database Migrations Aspect

Release Artifacts

Not really a test container, useful none the less.

The io.github.scottweaver.zio.aspect.DatabaseMigrationsAspect provides a ZIO TestAspect for running database migrations via Flyway. It seamlessly integrates with the ZMySQLContainer or ZPostrgeSQLContainer by using the io.github.scottweaver.zio.models.JdbcInfo provided by ZMySQLContainer.live or ZPostrgeSQLContainer.live to run your migrations.

If you are not using ZMySQLContainer or ZPostrgeSQLContainer you can just manually provide an appropriate JdbcInfo as a ZLayer to your tests that are using the DbMigrationAspect.

ZIO 1.X

libraryDependencies += "io.github.scottweaver" %% "zio-db-migration-aspect" % "0.10.0"

ZIO 2.X

libraryDependencies += "io.github.scottweaver" %% "zio-2-0-db-migration-aspect" % "0.10.0"

Liquibase Aspect

Release Artifacts

An alternative to the Flyway-based DbMigrationsAspect that uses Liquibase instead.

The io.github.scottweaver.zio.aspect.LiquibaseAspect provides a ZIO TestAspect for running database migrations via Liquibase. It seamlessly integrates with the ZMySQLContainer or ZPostrgeSQLContainer by using the io.github.scottweaver.zio.models.JdbcInfo provided by ZMySQLContainer.live or ZPostrgeSQLContainer.live to run your Changelog migrations.

If you are not using ZMySQLContainer or ZPostgreSQLContainer you can just manually provide an appropriate JdbcInfo as a ZLayer to your tests that are using the LiquibaseAspect.

ZIO 2.X

libraryDependencies += "io.github.scottweaver" %% "zio-2.0-liquibase-aspect" % "0.10.0"

Cassandra Migrations Aspect

Release Artifacts

Not really a test container, useful none the less.

The io.github.scottweaver.zio.aspect.CassandraMigrationsAspect provides a ZIO TestAspect for running database migrations via Cassandra Schema Migration. It seemlessly integrates with the ZCassandraContainer by using the com.datastax.oss.driver.api.core.CqlSession provided by ZCassandraContainer.live to run your migrations.

If you are not using ZCassandraContainer you can just manually provide an appropriate com.datastax.oss.driver.api.core.CqlSession as a ZLayer to your tests that are using the CassandraMigrationAspect.

ZIO 1.X

libraryDependencies += "io.github.scottweaver" %% "zio-cassandra-migration-aspect" % "0.10.0"

ZIO 2.X

libraryDependencies += "io.github.scottweaver" %% "zio-2-0-cassandra-migration-aspect" % "0.10.0"

Solr

Release Artifacts

Provides a managed ZLayer that starts and stops a com.dimafeng.testcontainers.SolrContainer.

You can use the container host, solrPort to access the web interface. The settings can be customized with schema/solrconfig.xml urls to create a collection.

ZIO 2.x

libraryDependencies += "io.github.scottweaver" %% "zio-2-0-testcontainers-solr" % "0.10.0"

References