Repository: 7mind/izumi
Pull request #1478 (2021-05-05 15:23:19)

Feature: di-docker bugfixes
…tests; fallback to default localhost for ServiceHost.local


Patch:
diff --git a/distage/distage-framework-api/src/main/scala/izumi/distage/framework/model/exceptions/IntegrationCheckException.scala b/distage/distage-framework-api/src/main/scala/izumi/distage/framework/model/exceptions/IntegrationCheckException.scala
index 85d83a734..1726c5434 100644
--- a/distage/distage-framework-api/src/main/scala/izumi/distage/framework/model/exceptions/IntegrationCheckException.scala
+++ b/distage/distage-framework-api/src/main/scala/izumi/distage/framework/model/exceptions/IntegrationCheckException.scala
@@ -8,4 +8,6 @@ import izumi.fundamentals.platform.strings.IzString._
 class IntegrationCheckException(val failures: NonEmptyList[ResourceCheck.Failure])
   extends DIException(
     s"""Integration check failed, failures were: ${failures.toList.niceList()}""".stripMargin
-  )
+  ) {
+  def this(failure: ResourceCheck.Failure) = this(NonEmptyList(failure))
+}
diff --git a/distage/distage-framework-docker/src/main/scala/izumi/distage/docker/ContainerNetworkDef.scala b/distage/distage-framework-docker/src/main/scala/izumi/distage/docker/ContainerNetworkDef.scala
index 37b2c4470..8cf3052ea 100644
--- a/distage/distage-framework-docker/src/main/scala/izumi/distage/docker/ContainerNetworkDef.scala
+++ b/distage/distage-framework-docker/src/main/scala/izumi/distage/docker/ContainerNetworkDef.scala
@@ -8,7 +8,6 @@ import izumi.distage.framework.model.exceptions.IntegrationCheckException
 import izumi.distage.model.definition.Lifecycle
 import izumi.distage.model.effect.{QuasiAsync, QuasiIO}
 import izumi.distage.model.providers.Functoid
-import izumi.fundamentals.collections.nonempty.NonEmptyList
 import izumi.fundamentals.platform.integration.ResourceCheck
 import izumi.fundamentals.platform.language.Quirks._
 import izumi.fundamentals.platform.strings.IzString._
@@ -123,7 +122,7 @@ object ContainerNetworkDef {
       // FIXME: temporary hack to allow missing containers to skip tests (happens when both DockerWrapper & integration check that depends on Docker.Container are memoized)
       F.definitelyRecover(f) {
         c: Throwable =>
-          F.fail(new IntegrationCheckException(NonEmptyList(ResourceCheck.ResourceUnavailable(c.getMessage, Some(c)))))
+          F.fail(new IntegrationCheckException(ResourceCheck.ResourceUnavailable(c.getMessage, Some(c))))
       }
     }
 
diff --git a/distage/distage-framework-docker/src/main/scala/izumi/distage/docker/ContainerResource.scala b/distage/distage-framework-docker/src/main/scala/izumi/distage/docker/ContainerResource.scala
index ef9786db5..6757e9244 100644
--- a/distage/distage-framework-docker/src/main/scala/izumi/distage/docker/ContainerResource.scala
+++ b/distage/distage-framework-docker/src/main/scala/izumi/distage/docker/ContainerResource.scala
@@ -200,7 +200,7 @@ import scala.util.{Failure, Success, Try}
               .sortBy(_.getId)
           } catch {
             case c: Throwable =>
-              throw new IntegrationCheckException(NonEmptyList(ResourceCheck.ResourceUnavailable(c.getMessage, Some(c))))
+              throw new IntegrationCheckException(ResourceCheck.ResourceUnavailable(c.getMessage, Some(c)))
           }
         }
         val candidate = {
@@ -365,9 +365,9 @@ import scala.util.{Failure, Success, Try}
           case Success(pulled) if pulled => // pulled successfully
             Right(())
           case Success(_) => // timed out
-            Left(new IntegrationCheckException(NonEmptyList(ResourceCheck.ResourceUnavailable(s"Image `${config.image}` pull timeout exception.", None))))
+            Left(new IntegrationCheckException(ResourceCheck.ResourceUnavailable(s"Image `${config.image}` pull timeout exception.", None)))
           case Failure(t) => // failure occurred (e.g. rate limiter failure)
-            Left(new IntegrationCheckException(NonEmptyList(ResourceCheck.ResourceUnavailable(s"Image pulling failed due to: ${t.getMessage}", Some(t)))))
+            Left(new IntegrationCheckException(ResourceCheck.ResourceUnavailable(s"Image pulling failed due to: ${t.getMessage}", Some(t))))
         }
       }
     }
diff --git a/distage/distage-framework-docker/src/main/scala/izumi/distage/docker/Docker.scala b/distage/distage-framework-docker/src/main/scala/izumi/distage/docker/Docker.scala
index ad0387bb3..a512f77e7 100644
--- a/distage/distage-framework-docker/src/main/scala/izumi/distage/docker/Docker.scala
+++ b/distage/distage-framework-docker/src/main/scala/izumi/distage/docker/Docker.scala
@@ -41,9 +41,11 @@ object Docker {
       case _ => None
     }
 
-    def local: ServiceHost = (InetAddress.getLocalHost: @unchecked) match {
-      case address: Inet4Address => IPv4(address)
-      case address: Inet6Address => IPv6(address)
+    // we will try to find local host address or will return default 127.0.0.1
+    def local: ServiceHost = Try(InetAddress.getLocalHost) match {
+      case Success(address: Inet4Address) => IPv4(address)
+      case Success(address: Inet6Address) => IPv6(address)
+      case _ => IPv4(InetAddress.getByName("127.0.0.1").asInstanceOf[Inet4Address])
     }
 
     def zeroAddresses: Set[ServiceHost] = {


Test patch:
diff --git a/distage/distage-testkit-core/src/main/scala/izumi/distage/testkit/services/dstest/DistageTestRunner.scala b/distage/distage-testkit-core/src/main/scala/izumi/distage/testkit/services/dstest/DistageTestRunner.scala
index f6361103e..f34545bfc 100644
--- a/distage/distage-testkit-core/src/main/scala/izumi/distage/testkit/services/dstest/DistageTestRunner.scala
+++ b/distage/distage-testkit-core/src/main/scala/izumi/distage/testkit/services/dstest/DistageTestRunner.scala
@@ -470,6 +470,11 @@ class DistageTestRunner[F[_]: TagK: DefaultModule](
           F.maybeSuspend {
             reporter.testStatus(test.meta, TestStatus.Cancelled(s.getMessage, testDuration(before)))
           }
+        // TODO: workaround to handle integration exceptions thrown by DockerWrapper and ContainerResorce
+        case (ProvisioningIntegrationException(failures), _) =>
+          F.maybeSuspend {
+            reporter.testStatus(test.meta, TestStatus.Ignored(failures))
+          }
         case (_, getTrace) =>
           F.maybeSuspend {
             reporter.testStatus(test.meta, TestStatus.Failed(getTrace(), testDuration(before)))