Skip to content
This repository has been archived by the owner on Jul 16, 2020. It is now read-only.

Update to Izumi 0.9.16, use distage-testkit docker support instead of docker-compose #2

Merged
merged 1 commit into from
Nov 30, 2019
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Final state of a project livecoded at ScalaWAW, Video: https://www.youtube.com/w

Features [distage](https://izumi.7mind.io/latest/release/doc/distage/), [Bifunctor Tagless Final](https://github.com/7mind/izumi/blob/c027770d35a4a0e99f7694f8afe519f1e7a820f9/fundamentals/fundamentals-bio/src/main/scala/izumi/functional/bio/BIO.scala) and [ZIO](https://zio.dev) Environment for composing test fixtures

To run tests that require postgres, launch postgres docker in this repo via `docker-compose up`
To launch tests that require postgres you need to have a running `docker` daemon
8 changes: 4 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
val V = new {
val distage = "0.9.12"
val distage = "0.9.16"
val scalatest = "3.0.8"
val scalacheck = "1.14.1"
val http4s = "0.21.0-M4"
val doobie = "0.8.6"
val zio = "1.0.0-RC16"
val zioCats = "2.0.0.0-RC7"
val zio = "1.0.0-RC17"
val zioCats = "2.0.0.0-RC10"
val kindProjector = "0.11.0"
val circeDerivation = "0.12.0-M7"
}
Expand Down Expand Up @@ -38,7 +38,7 @@ val Deps = new {

inThisBuild(
Seq(
scalaVersion := "2.13.0",
scalaVersion := "2.13.1",
version := "1.0.0-SNAPSHOT",
organization := "io.7mind",
)
Expand Down
17 changes: 0 additions & 17 deletions docker-compose.yml

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/resources/common-reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ logger {

postgres {
jdbcDriver = "org.postgresql.Driver"
url = "jdbc:postgresql://localhost/postgres"
url = "jdbc:postgresql://localhost:${port}/postgres"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI not every environment has "localhost" as the host where ports are exposed.
This is why in Testcontainers we have this:
https://www.testcontainers.org/features/networking/#getting-the-container-ip-address

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, btw, if you would use Testcontainers, you could launch containers just by specifying a JDBC URL 😊
https://www.testcontainers.org/modules/databases/#database-containers-launched-via-jdbc-url-scheme

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bsideup Ah, that's covered too, I just forgot to add it in the PR - d15721b

user = "postgres"
password = "postgres"
}
18 changes: 10 additions & 8 deletions src/main/scala/livecode/Main.scala
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package livecode

import izumi.distage.plugins.load.PluginLoader.PluginConfig
import izumi.distage.roles.{BootstrapConfig, RoleAppLauncher, RoleAppMain}
import izumi.distage.roles.services.PluginSource
import izumi.distage.roles.{RoleAppLauncher, RoleAppMain}
import izumi.fundamentals.platform.cli.model.raw.RawRoleParams
import livecode.code.LivecodeRole
import zio.Task
import zio.interop.catz._

object Main
extends RoleAppMain.Default[Task](
extends RoleAppMain.Default(
launcher = new RoleAppLauncher.LauncherF[Task] {
override val bootstrapConfig = BootstrapConfig(
PluginConfig(
debug = false,
packagesEnabled = Seq("livecode.plugins"),
packagesDisabled = Nil,
override val pluginSource =
PluginSource(
PluginConfig(
debug = false,
packagesEnabled = Seq("livecode.plugins"),
packagesDisabled = Nil,
)
)
)
}
) {
override val requiredRoles: Vector[RawRoleParams] = {
Expand Down
20 changes: 12 additions & 8 deletions src/main/scala/livecode/code.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import org.http4s.HttpRoutes
import org.http4s.dsl.Http4sDsl
import org.http4s.server.blaze.BlazeServerBuilder

import scala.annotation.unused

object code {
type UserId = UUID
type Score = Long
Expand Down Expand Up @@ -145,14 +147,13 @@ object code {
blocker: Blocker,
async: Async[F],
shift: ContextShift[F],
pgIntegrationCheck: PgIntegrationCheck,
@unused pgIntegrationCheck: PgIntegrationCheck,
portCfg: PostgresPortCfg,
): Resource[F, HikariTransactor[F]] = {
val _ = pgIntegrationCheck

HikariTransactor
.newHikariTransactor(
driverClassName = cfg.jdbcDriver,
url = cfg.url,
url = cfg.url.replace("${port}", portCfg.postgresPort.toString),
user = cfg.user,
pass = cfg.password,
connectEC = blocker.blockingContext,
Expand All @@ -167,17 +168,20 @@ object code {
password: String,
)

final case class PostgresPortCfg(
postgresPort: Int,
)

final class PgIntegrationCheck(
portCheck: PortCheck,
cfg: PostgresCfg @ConfPath("postgres"),
portCfg: PostgresPortCfg,
) extends IntegrationCheck {
override def resourcesAvailable(): ResourceCheck = {
val str = cfg.url.stripPrefix("jdbc:")
val str = cfg.url.stripPrefix("jdbc:").replace("${port}", "")
val uri = URI.create(str)

val postgresDefaultPort = 5432

portCheck.checkUri(uri, postgresDefaultPort, s"Couldn't connect to postgres at uri=$uri defaultPort=$postgresDefaultPort")
portCheck.checkUri(uri, portCfg.postgresPort, s"Couldn't connect to postgres at uri=$uri defaultPort=${portCfg.postgresPort}")
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/livecode/plugins/LivecodePlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import doobie.util.transactor.Transactor
import izumi.distage.model.definition.ModuleDef
import izumi.distage.model.definition.StandardAxis.Repo
import izumi.fundamentals.platform.integration.PortCheck
import livecode.code.Postgres.PgIntegrationCheck
import livecode.code.Postgres.{PgIntegrationCheck, PostgresPortCfg}
import livecode.code._
import org.http4s.dsl.Http4sDsl
import zio.IO
Expand Down Expand Up @@ -39,6 +39,7 @@ object LivecodePlugin extends PluginDef {
make[Transactor[F[Throwable, ?]]].fromResource(Postgres.resource[F[Throwable, ?]] _)
make[PgIntegrationCheck]
make[PortCheck].from(new PortCheck(3))
make[PostgresPortCfg].from(PostgresPortCfg(5432))
}

def repoDummy[F[+_, +_]: TagKK](implicit ev: TagK[F[Throwable, ?]]): ModuleDef = new ModuleDef {
Expand Down
5 changes: 4 additions & 1 deletion src/main/scala/livecode/plugins/ZIOPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ object ZIOPlugin extends ZIODIEffectModule with PluginDef {
addImplicit[Async2[IO]]
addImplicit[ContextShift2[IO]]
addImplicit[Timer2[IO]]
make[ConcurrentEffect2[IO]].from((runtime: zio.Runtime[Any]) => taskEffectInstance(runtime))
make[ConcurrentEffect2[IO]].from {
runtime: zio.Runtime[Any] =>
taskEffectInstance(runtime)
}

addImplicit[BIOPrimitives[IO]]

Expand Down
41 changes: 41 additions & 0 deletions src/test/scala/livecode/PostgresDockerModule.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package livecode

import distage.ModuleDef
import izumi.distage.testkit.integration.docker.Docker
import izumi.distage.testkit.integration.docker.Docker.DockerPort
import izumi.distage.testkit.integration.docker.examples.PostgresDocker
import izumi.distage.testkit.integration.docker.modules.DockerContainerModule
import livecode.code.Postgres.PostgresPortCfg
import zio.Task

object PostgresDockerModule extends ModuleDef {
// launch postgres docker for tests
make[PostgresDocker.Container]
.fromResource(PostgresDocker.make[Task])

// spawned docker container port is dynamic
// to prevent conflicts. Make PostgresPortCfg
// point to the new port. This will also
// cause the container to start before
// integration check is performed
make[PostgresPortCfg].from {
docker: PostgresDocker.Container =>
println(docker)
PostgresPortCfg(docker.availablePorts(DockerPort.TCP(5432)).head.port)
}

// add docker dependencies and override default configuration
include(new DockerContainerModule[Task] overridenBy new ModuleDef {
make[Docker.ClientConfig].from {
Docker.ClientConfig(
readTimeoutMs = 500,
connectTimeoutMs = 500,
allowReuse = true,
useRemote = false,
useRegistry = true,
remote = None,
registry = None,
)
}
})
}
5 changes: 5 additions & 0 deletions src/test/scala/livecode/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package livecode

import distage.{DIKey, ModuleDef}
import doobie.util.transactor.Transactor
import izumi.distage.constructors.ConcreteConstructor
import izumi.distage.model.definition.StandardAxis
import izumi.distage.testkit.integration.docker.examples.{PostgresDocker, PostgresDockerModule}
import izumi.distage.testkit.services.DISyntaxZIOEnv
import izumi.distage.testkit.services.st.dtest.TestConfig
import izumi.distage.testkit.st.specs.DistageBIOSpecScalatest
import livecode.code.Postgres.PgIntegrationCheck
import livecode.code._
import livecode.zioenv._
import zio.{IO, Task, ZIO}
Expand All @@ -16,11 +19,13 @@ abstract class LivecodeTest extends DistageBIOSpecScalatest[IO] with DISyntaxZIO
activation = StandardAxis.testProdActivation,
moduleOverrides = new ModuleDef {
make[Rnd[IO]].from[Rnd.Impl[IO]]
include(PostgresDockerModule)
},
memoizedKeys = Set(
DIKey.get[Transactor[Task]],
DIKey.get[Ladder[IO]],
DIKey.get[Profiles[IO]],
DIKey.get[PostgresDocker.Container],
),
)
}
Expand Down