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

[FIX] when e2e test docker-compose fails to start, capture logs from the containers and print #7497

Merged
merged 7 commits into from
Jan 24, 2025
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
4 changes: 4 additions & 0 deletions utils/test-utils/src/main/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
<logger name="skuber" level="${SKUBER_LOG_LEVEL:-WARN}"/>
<logger name="com.azure" level="INFO"/>
<logger name="org.testcontainers" level="INFO"/>
<!-- The following logger can be used for containers logs since 1.18.0 -->
<logger name="tc" level="INFO"/>
<logger name="com.github.dockerjava" level="WARN"/>
<logger name="com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http" level="OFF"/>

<logger name="scenario-activity-audit" level="INFO" additivity="false">
<appender-ref ref="STDOUT_FOR_SCENARIO_ACTIVITY_AUDIT"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import cats.effect.IO
import cats.effect.kernel.Resource
import cats.effect.unsafe.implicits.global
import com.dimafeng.testcontainers.{DockerComposeContainer, ServiceLogConsumer, WaitingForService}
import com.github.dockerjava.api.DockerClient
import com.github.dockerjava.api.model.Container
import com.typesafe.scalalogging.LazyLogging
import org.slf4j.Logger
import org.slf4j.MarkerFactory.getIMarkerFactory
import org.testcontainers.DockerClientFactory
import org.testcontainers.containers.output.Slf4jLogConsumer
import org.testcontainers.containers.wait.strategy.DockerHealthcheckWaitStrategy
import org.testcontainers.utility.LogUtils
import pl.touk.nussknacker.test.MiscUtils._
import pl.touk.nussknacker.test.WithTestHttpClientCreator
import pl.touk.nussknacker.test.containers.ContainerExt.toContainerExt
Expand All @@ -18,7 +23,9 @@ import ujson.Value

import java.io.{File => JFile}
import java.time.Duration
import scala.util.Try
import java.util
import scala.jdk.CollectionConverters._
import scala.util.{Failure, Success, Try}

class DockerBasedInstallationExampleNuEnvironment(
nussknackerImageVersion: String,
Expand Down Expand Up @@ -46,7 +53,15 @@ class DockerBasedInstallationExampleNuEnvironment(
tailChildContainers = false
) {

start()
Try(start()) match {
case Failure(ex) =>
// There is no way currently to automatically capture logs from containers before all services from the docker
// compose started. When one of the services is not healthy, there won't be any logs captured. That's why we do
// the capture manually.
captureAllContainerLogs()
throw ex
case Success(()) =>
}

private val (dockerBasedInstallationExampleClient, closeHandler) =
DockerBasedInstallationExampleClient.create(this).allocated.unsafeRunSync()
Expand All @@ -58,6 +73,31 @@ class DockerBasedInstallationExampleNuEnvironment(
super.stop()
}

private def captureAllContainerLogs() = {
val dockerClient = DockerClientFactory.lazyClient()
getNuDockerComposeContainers(dockerClient).foreach { container =>
val logs = LogUtils.getOutput(dockerClient, container.getId)
slf4jLogger.info(getIMarkerFactory.getMarker(container.getNames.mkString(",")), logs)
}
}

private def getNuDockerComposeContainers(dockerClient: DockerClient) = {
dockerClient
.listContainersCmd()
.exec()
.asInstanceOf[util.ArrayList[Container]]
.asScala
.filter { container =>
// dummy method of how to distinguish if the container is Nu docker-compose related container
container.labels.asScala.get("com.docker.compose.project.working_dir") match {
case Some(value) =>
value.contains("nussknacker")
case None => false
}
}
.toList
}

}

object DockerBasedInstallationExampleNuEnvironment extends LazyLogging {
Expand Down Expand Up @@ -87,7 +127,7 @@ class DockerBasedInstallationExampleClient private (

def deployAndWaitForRunningState(scenarioName: String): Unit = {
bootstrapSetupService.executeBash(
s"""/app/utils/nu/deploy-scenario-and-wait-for-running-state.sh "$scenarioName" """
s"""/app/utils/nu/deploy-scenario-and-wait-for-deployed-state.sh "$scenarioName" """
)
}

Expand Down
Loading