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

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
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -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
@@ -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"
}
@@ -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",
)

This file was deleted.

@@ -5,7 +5,7 @@ logger {

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

This comment has been minimized.

Copy link
@bsideup

bsideup Dec 1, 2019

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

This comment has been minimized.

Copy link
@bsideup

bsideup Dec 1, 2019

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

This comment has been minimized.

Copy link
@neko-kai

neko-kai Dec 1, 2019

Author Member

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

user = "postgres"
password = "postgres"
}
@@ -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] = {
@@ -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
@@ -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,
@@ -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}")
}
}

@@ -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
@@ -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 {
@@ -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]]

@@ -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,
)
}
})
}
@@ -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}
@@ -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],
),
)
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.