summaryrefslogtreecommitdiff
path: root/crashboxd/src/test/scala/io/crashbox/ci/BuildStageSpec.scala
blob: a3a68b683af8c686ffd59f0313b7ab5352ae73f9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package io.crashbox.ci

import akka.actor.ActorSystem
import akka.stream.{ ClosedShape, KillSwitch }
import akka.stream.scaladsl.{ GraphDSL, RunnableGraph, Sink, Source }
import akka.stream.{ ActorMaterializer, FanInShape2 }
import java.io.{ ByteArrayOutputStream, File }
import java.nio.file.Files
import org.scalatest._
import scala.concurrent.Await
import scala.concurrent.duration._

class BuildStageSpec extends FlatSpec with Matchers with DockerSuite{

  implicit val materializer = ActorMaterializer()

  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()
  }

  "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

        ClosedShape
      })

      graph.run()
      Thread.sleep(30000)
      println("terminating")
      Await.result(system.terminate(), 60.seconds)
      println("teminated")
      Thread.sleep(5000)
      println("eot")
    }

  }
}