From b492dcf46718f50ca051017bddd4551faa867339 Mon Sep 17 00:00:00 2001 From: Jakob Odersky Date: Sun, 9 Apr 2017 16:32:19 -0700 Subject: Refactor testing utilities --- .../test/scala/io/crashbox/ci/BuildStageSpec.scala | 44 +++++++++++----------- .../scala/io/crashbox/ci/DockerExecutorSpec.scala | 34 +++-------------- .../src/test/scala/io/crashbox/ci/DockerUtil.scala | 27 +++++++++++++ .../src/test/scala/io/crashbox/ci/IOUtil.scala | 33 ++++++++++++++++ .../src/test/scala/io/crashbox/ci/TestUtil.scala | 26 ------------- 5 files changed, 86 insertions(+), 78 deletions(-) create mode 100644 crashboxd/src/test/scala/io/crashbox/ci/DockerUtil.scala create mode 100644 crashboxd/src/test/scala/io/crashbox/ci/IOUtil.scala delete mode 100644 crashboxd/src/test/scala/io/crashbox/ci/TestUtil.scala diff --git a/crashboxd/src/test/scala/io/crashbox/ci/BuildStageSpec.scala b/crashboxd/src/test/scala/io/crashbox/ci/BuildStageSpec.scala index ec5ec56..eb77c40 100644 --- a/crashboxd/src/test/scala/io/crashbox/ci/BuildStageSpec.scala +++ b/crashboxd/src/test/scala/io/crashbox/ci/BuildStageSpec.scala @@ -29,29 +29,27 @@ class BuildStageSpec extends FlatSpec with Matchers with BeforeAndAfterAll { "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") - } + IOUtil.withTemp{ (dir, 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 5ec95cd..bd3159b 100644 --- a/crashboxd/src/test/scala/io/crashbox/ci/DockerExecutorSpec.scala +++ b/crashboxd/src/test/scala/io/crashbox/ci/DockerExecutorSpec.scala @@ -14,36 +14,13 @@ import org.scalatest._ import scala.util.Random -object DockerUtil { - import TestUtil._ - - val defaultImage = "crashbox" - - def ensureImage(client: DockerClient): Unit = { - println("Pulling base docker image for running docker tests") - val baseImage = "debian:jessie-backports" - client.pull(baseImage) - - 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) - client.build(dir.toPath, defaultImage) - } - } - -} - class DockerExecutorSpec extends FlatSpec with Matchers with BeforeAndAfterAll with BeforeAndAfterEach { - import TestUtil._ + import IOUtil._ val image = "crashbox" @@ -69,9 +46,9 @@ class DockerExecutorSpec assert(exec.clean(), "Spawned containers were not removed") } - def run[A](script: String)(tests: (Int, File, String) => A): A = withTempFile { - dir => - val out = new ByteArrayOutputStream(1024) + def run[A](script: String)(tests: (Int, File, String) => A): A = withTemp { + case (dir, out) => + val awaitable = for (id <- exec.start(image, script, dir, out); status <- exec.result(id)) yield { status @@ -123,9 +100,8 @@ class DockerExecutorSpec } it should "allow cancellations" in { - withTempFile { dir => + withTemp { case (dir, out) => val script = "while true; do sleep 1; echo sleeping; done" - val out = new ByteArrayOutputStream(1024) val id = Await.result(exec.start(image, script, dir, out), timeout) val check = exec.result(id).map { res => diff --git a/crashboxd/src/test/scala/io/crashbox/ci/DockerUtil.scala b/crashboxd/src/test/scala/io/crashbox/ci/DockerUtil.scala new file mode 100644 index 0000000..794a390 --- /dev/null +++ b/crashboxd/src/test/scala/io/crashbox/ci/DockerUtil.scala @@ -0,0 +1,27 @@ +package io.crashbox.ci + +object DockerUtil { + import IOUtil._ + import com.spotify.docker.client.DockerClient + import java.io.File + import java.nio.file.Files + + val defaultImage = "crashbox" + + def ensureImage(client: DockerClient): Unit = { + println("Pulling base docker image for running docker tests") + val baseImage = "debian:jessie-backports" + client.pull(baseImage) + + withTempDir { 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) + client.build(dir.toPath, defaultImage) + } + } + +} diff --git a/crashboxd/src/test/scala/io/crashbox/ci/IOUtil.scala b/crashboxd/src/test/scala/io/crashbox/ci/IOUtil.scala new file mode 100644 index 0000000..034c6a3 --- /dev/null +++ b/crashboxd/src/test/scala/io/crashbox/ci/IOUtil.scala @@ -0,0 +1,33 @@ +package io.crashbox.ci + +import java.io.{ ByteArrayOutputStream, File } +import java.nio.file.Files + +object IOUtil { + + def withTempDir[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() + } + + def withTemp[A](action: (File, ByteArrayOutputStream) => A, size: Int = 1024): A = { + withTempDir { d => + withTempStream { s => + action(d, s) + } + } + } + +} diff --git a/crashboxd/src/test/scala/io/crashbox/ci/TestUtil.scala b/crashboxd/src/test/scala/io/crashbox/ci/TestUtil.scala deleted file mode 100644 index b5bacf2..0000000 --- a/crashboxd/src/test/scala/io/crashbox/ci/TestUtil.scala +++ /dev/null @@ -1,26 +0,0 @@ -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() - } - -} -- cgit v1.2.3