diff options
Diffstat (limited to 'crashboxd/src/test/scala/io')
3 files changed, 82 insertions, 109 deletions
diff --git a/crashboxd/src/test/scala/io/crashbox/ci/BuildStageSpec.scala b/crashboxd/src/test/scala/io/crashbox/ci/BuildStageSpec.scala index a3a68b6..ec5ec56 100644 --- a/crashboxd/src/test/scala/io/crashbox/ci/BuildStageSpec.scala +++ b/crashboxd/src/test/scala/io/crashbox/ci/BuildStageSpec.scala @@ -1,6 +1,7 @@ package io.crashbox.ci import akka.actor.ActorSystem +import akka.stream.scaladsl.Keep import akka.stream.{ ClosedShape, KillSwitch } import akka.stream.scaladsl.{ GraphDSL, RunnableGraph, Sink, Source } import akka.stream.{ ActorMaterializer, FanInShape2 } @@ -10,54 +11,47 @@ import org.scalatest._ import scala.concurrent.Await import scala.concurrent.duration._ -class BuildStageSpec extends FlatSpec with Matchers with DockerSuite{ +class BuildStageSpec extends FlatSpec with Matchers with BeforeAndAfterAll { + implicit val system = ActorSystem("crashboxd-buildstage") implicit val materializer = ActorMaterializer() + val executor = new DockerExecutor - val exec = new DockerExecutor - def withTmp[A](action: (File, ByteArrayOutputStream) => A): A = { - val dir = Files.createTempDirectory("crashbox-build-stage-test").toFile - val out = new ByteArrayOutputStream(1024) - try action(dir, out) - finally dir.delete() + override def beforeAll(): Unit = { + DockerUtil.ensureImage(executor.dockerClient) } - "BuildStage" should "have a test!" in { - withTmp{ case (dir, out) => - val taskDef = TaskDef(DockerEnvironment("crashbox"), "sleep 100; exit 0") - - val resultSink = Sink.foreach[Builder.BuildState](x => println(x)) - - val graph = RunnableGraph.fromGraph(GraphDSL.create(resultSink) { - implicit b => sink => - import GraphDSL.Implicits._ - - val builder = b.add(new BuildStage(exec, _ => dir, _ => out)) - - val submissions = b.add( - Source.repeat(TaskId("123", 2) -> taskDef)) - val cancellations = b.add( - Source.tick(10.seconds, 10.seconds, TaskId("0", 0))) - - val ks = b.add(KillSwitch) - - submissions ~> builder.in0 - cancellations ~> builder.in1 - - builder.out ~> sink + override def afterAll(): Unit = { + assert(executor.clean(), "Spawned containers were not removed") + system.terminate() + } - ClosedShape - }) - graph.run() - Thread.sleep(30000) - println("terminating") - Await.result(system.terminate(), 60.seconds) - println("teminated") - Thread.sleep(5000) - println("eot") + "BuildStage" should "have a test!" in { + TestUtil.withTempFile{ dir => + TestUtil.withTempStream{ out => + + val taskDef = TaskDef(DockerEnvironment("crashbox"), "sleep 10; exit 0") + val resultSink = Sink.foreach[Builder.BuildState](x => println(x)) + + val stage = new BuildSource( + TaskId("build", 0), + taskDef, + executor, + dir, + out + ) + val src = Source.fromGraph(stage) + + //val done = src.toMat(resultSink)(Keep.right).run() + + //executor.start("crashbox", "sleep 10000", dir, out) + Thread.sleep(1000) + assert(executor.clean()) + //Await.ready(done, 30.seconds) + println("eot") + } } - } } diff --git a/crashboxd/src/test/scala/io/crashbox/ci/DockerExecutorSpec.scala b/crashboxd/src/test/scala/io/crashbox/ci/DockerExecutorSpec.scala index 23cbced..5ec95cd 100644 --- a/crashboxd/src/test/scala/io/crashbox/ci/DockerExecutorSpec.scala +++ b/crashboxd/src/test/scala/io/crashbox/ci/DockerExecutorSpec.scala @@ -1,5 +1,6 @@ package io.crashbox.ci +import com.spotify.docker.client.DockerClient import com.spotify.docker.client.DockerClient.ListContainersParam import java.io.{ByteArrayOutputStream, File} import java.nio.file.Files @@ -13,71 +14,36 @@ import org.scalatest._ import scala.util.Random -trait DockerSuite extends Suite with BeforeAndAfterAll { self => +object DockerUtil { + import TestUtil._ - private val name = self.toString() + val defaultImage = "crashbox" - private def withTmp[A](action: File => A): A = { - val dir = Files.createTempDirectory("crashbox-docker-test-" + name).toFile - try action(dir) - finally dir.delete() - } - - val baseImage = "debian:jessie-backports" - val dockerImage = "crashbox" - val dockerTimeout = 30.seconds - val dockerLabel = "test-" + Random.nextInt() - - implicit val system = ActorSystem("crashbox-docker-test-" + name) - import system.dispatcher - val executor = new DockerExecutor { - override def label = dockerLabel - } - - def buildImage(): Unit = { + def ensureImage(client: DockerClient): Unit = { println("Pulling base docker image for running docker tests") - executor.dockerClient.pull(baseImage) + val baseImage = "debian:jessie-backports" + client.pull(baseImage) - withTmp { dir => + withTempFile { dir => println("Adapting base image for tests") val modifications = s"""|FROM $baseImage |RUN adduser crashbox |USER crashbox |""".stripMargin Files.write((new File(dir, "Dockerfile")).toPath, modifications.getBytes) - executor.dockerClient.build(dir.toPath, dockerImage) - } - } - - def runningDockers: Seq[String] = { - val stale = executor.dockerClient - .listContainers( - ListContainersParam.withLabel("crashbox", dockerLabel) - ).asScala - stale.map(_.id()) - } - - override def beforeAll: Unit = { - buildImage() - } - - override def afterAll: Unit = { - val running = runningDockers - running.foreach { id => - executor.dockerClient.stopContainer(id, 0) - executor.dockerClient.removeContainer(id) + client.build(dir.toPath, defaultImage) } - require(running.isEmpty, "Docker containers were left running after unit tests") - require(runningDockers.isEmpty, "Could not delete left over docker containers.") } } - class DockerExecutorSpec extends FlatSpec with Matchers - with BeforeAndAfterAll { + with BeforeAndAfterAll + with BeforeAndAfterEach { + + import TestUtil._ val image = "crashbox" @@ -88,33 +54,22 @@ class DockerExecutorSpec val exec = new DockerExecutor override def beforeAll: Unit = { - println("Pulling base docker image for running docker tests") - val base = "debian:jessie-backports" - exec.dockerClient.pull(base) - - withTmp { dir => - println("Adapting base image for tests") - val modifications = s"""|FROM $base - |RUN adduser crashbox - |USER crashbox - |""".stripMargin - Files.write((new File(dir, "Dockerfile")).toPath, modifications.getBytes) - exec.dockerClient.build(dir.toPath, image) + sys.addShutdownHook { + println("------------------- fooooo") + exec.clean() } - + DockerUtil.ensureImage(exec.dockerClient) } override def afterAll: Unit = { system.terminate() } - def withTmp[A](action: File => A): A = { - val dir = Files.createTempDirectory("crashbox-docker-test").toFile - try action(dir) - finally dir.delete() + override def afterEach: Unit = { + assert(exec.clean(), "Spawned containers were not removed") } - def run[A](script: String)(tests: (Int, File, String) => A): A = withTmp { + def run[A](script: String)(tests: (Int, File, String) => A): A = withTempFile { dir => val out = new ByteArrayOutputStream(1024) val awaitable = for (id <- exec.start(image, script, dir, out); @@ -167,8 +122,8 @@ class DockerExecutorSpec } } - it should "allow cancellation" in { - withTmp { dir => + it should "allow cancellations" in { + withTempFile { dir => val script = "while true; do sleep 1; echo sleeping; done" val out = new ByteArrayOutputStream(1024) @@ -177,8 +132,6 @@ class DockerExecutorSpec assert(res == 137) } exec.stop(id) - //TODO check if resoruces were cleaned up properly - Await.result(check, timeout) } } diff --git a/crashboxd/src/test/scala/io/crashbox/ci/TestUtil.scala b/crashboxd/src/test/scala/io/crashbox/ci/TestUtil.scala new file mode 100644 index 0000000..b5bacf2 --- /dev/null +++ b/crashboxd/src/test/scala/io/crashbox/ci/TestUtil.scala @@ -0,0 +1,26 @@ +package io.crashbox.ci + +import java.io.{ ByteArrayOutputStream, File } +import java.nio.file.Files + + +object TestUtil { + + def withTempFile[A](action: File => A): A = { + def rm(parent: File): Unit = if (parent.isDirectory) { + parent.listFiles.foreach{ child => + rm(child) + } + } + val dir = Files.createTempDirectory("crashbox-test").toFile + try action(dir) + finally rm(dir) + } + + def withTempStream[A](action: ByteArrayOutputStream => A, size: Int = 1024): A = { + val out = new ByteArrayOutputStream(size) + try action(out) + finally out.close() + } + +} |