summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Odersky <jakob@odersky.com>2017-04-09 16:32:19 -0700
committerJakob Odersky <jakob@odersky.com>2017-04-09 16:32:19 -0700
commitb492dcf46718f50ca051017bddd4551faa867339 (patch)
tree30ec6c2494b3c9c97d5e476d360168ad5ddac6b6
parent382152098459a5783e3a794161ed2da2a321af37 (diff)
downloadcrashbox-ci-b492dcf46718f50ca051017bddd4551faa867339.tar.gz
crashbox-ci-b492dcf46718f50ca051017bddd4551faa867339.tar.bz2
crashbox-ci-b492dcf46718f50ca051017bddd4551faa867339.zip
Refactor testing utilities
-rw-r--r--crashboxd/src/test/scala/io/crashbox/ci/BuildStageSpec.scala44
-rw-r--r--crashboxd/src/test/scala/io/crashbox/ci/DockerExecutorSpec.scala34
-rw-r--r--crashboxd/src/test/scala/io/crashbox/ci/DockerUtil.scala27
-rw-r--r--crashboxd/src/test/scala/io/crashbox/ci/IOUtil.scala (renamed from crashboxd/src/test/scala/io/crashbox/ci/TestUtil.scala)13
4 files changed, 63 insertions, 55 deletions
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/TestUtil.scala b/crashboxd/src/test/scala/io/crashbox/ci/IOUtil.scala
index b5bacf2..034c6a3 100644
--- a/crashboxd/src/test/scala/io/crashbox/ci/TestUtil.scala
+++ b/crashboxd/src/test/scala/io/crashbox/ci/IOUtil.scala
@@ -3,10 +3,9 @@ package io.crashbox.ci
import java.io.{ ByteArrayOutputStream, File }
import java.nio.file.Files
+object IOUtil {
-object TestUtil {
-
- def withTempFile[A](action: File => A): A = {
+ def withTempDir[A](action: File => A): A = {
def rm(parent: File): Unit = if (parent.isDirectory) {
parent.listFiles.foreach{ child =>
rm(child)
@@ -23,4 +22,12 @@ object TestUtil {
finally out.close()
}
+ def withTemp[A](action: (File, ByteArrayOutputStream) => A, size: Int = 1024): A = {
+ withTempDir { d =>
+ withTempStream { s =>
+ action(d, s)
+ }
+ }
+ }
+
}