All Downloads are FREE. Search and download functionalities are using the official Maven repository.

blended.itestsupport.docker.Docker.scala Maven / Gradle / Ivy

Go to download

Define an integration test API for collaborating blended container(s) using docker as a runtime for the container(s) under test and an Akka based Camel framework to perform the integration tests as pure blackbox tests. Container(s) may be prestarted and discovered (for execution speed) or started by the integration test (for reproducability).

The newest version!
package blended.itestsupport.docker

import java.io.File

import akka.event.LoggingAdapter
import com.github.dockerjava.api.DockerClient
import com.github.dockerjava.api.model.{Container, Image}
import com.github.dockerjava.core.{DefaultDockerClientConfig, DockerClientBuilder}
import com.typesafe.config.Config

import scala.collection.convert.Wrappers.JListWrapper

object DockerClientFactory {

  var client : Option[DockerClient] = None
  
  def apply(config : Config)(implicit logger: LoggingAdapter) = client match {
    case Some(dockerClient) => dockerClient
    case _ =>

      val dockerConfig =  DefaultDockerClientConfig.createDefaultConfigBuilder()
        .withDockerHost("tcp://" + config.getString("docker.host") + ":" + config.getString("docker.port"))
        .withRegistryUsername(config.getString("docker.user"))
        .withRegistryPassword(config.getString("docker.password"))
        .withRegistryEmail(config.getString("docker.eMail"))
        .build()

      logger.info(s"Trying to connect to docker at [${dockerConfig.getDockerHost()}]")

      val dockerClient = DockerClientBuilder.getInstance(dockerConfig).build()

      val version = dockerClient.versionCmd().exec()

      logger info
        s"""
       Using Docker version  ${version.getVersion}
       Docker API version    ${version.getApiVersion}
       Docker Go version     ${version.getGoVersion}
       Architecture          ${version.getArch}
       Kernel version        ${version.getKernelVersion}"""

      client = Some(dockerClient)
      dockerClient.asInstanceOf[DockerClient]
    }
}

trait Docker {

  implicit val logger  : LoggingAdapter
  implicit val config  : Config
  implicit val client  : DockerClient

  def searchByTag(s: String): Image => Boolean = { img: Image =>
    val tags : Array[String] = Option(img.getRepoTags).getOrElse(Array.empty)
    val matched = tags.exists(_.matches(s))

    if (matched)
      logger.info(s"Image [$img] matches [$s]")

    matched
  }

  def images : List[Image] =
    JListWrapper(client.listImagesCmd().exec()).toList
    
  def running : List[Container] =
    JListWrapper(client.listContainersCmd().exec()).toList

  def search(f : Image => Boolean) = {
    val li = images
    li.filter(f)
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy