|
@@ -2,7 +2,9 @@ package com.cloudera.hue.livy.server.batches
|
|
|
|
|
|
|
|
import java.io.FileWriter
|
|
import java.io.FileWriter
|
|
|
import java.nio.file.{Files, Path}
|
|
import java.nio.file.{Files, Path}
|
|
|
|
|
+import java.util.concurrent.TimeUnit
|
|
|
|
|
|
|
|
|
|
+import com.cloudera.hue.livy.Utils
|
|
|
import com.cloudera.hue.livy.server.batch._
|
|
import com.cloudera.hue.livy.server.batch._
|
|
|
import org.json4s.JsonAST.{JArray, JInt, JObject, JString}
|
|
import org.json4s.JsonAST.{JArray, JInt, JObject, JString}
|
|
|
import org.json4s.jackson.JsonMethods._
|
|
import org.json4s.jackson.JsonMethods._
|
|
@@ -11,33 +13,32 @@ import org.json4s.{DefaultFormats, Formats}
|
|
|
import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll, FunSpecLike}
|
|
import org.scalatest.{BeforeAndAfter, BeforeAndAfterAll, FunSpecLike}
|
|
|
import org.scalatra.test.scalatest.ScalatraSuite
|
|
import org.scalatra.test.scalatest.ScalatraSuite
|
|
|
|
|
|
|
|
|
|
+import scala.concurrent.duration.Duration
|
|
|
|
|
+
|
|
|
class BatchServletSpec extends ScalatraSuite with FunSpecLike with BeforeAndAfterAll with BeforeAndAfter {
|
|
class BatchServletSpec extends ScalatraSuite with FunSpecLike with BeforeAndAfterAll with BeforeAndAfter {
|
|
|
|
|
|
|
|
protected implicit def jsonFormats: Formats = DefaultFormats
|
|
protected implicit def jsonFormats: Formats = DefaultFormats
|
|
|
|
|
|
|
|
- var script: Path = _
|
|
|
|
|
-
|
|
|
|
|
- val batchFactory = new BatchProcessFactory()
|
|
|
|
|
- val batchManager = new BatchManager(batchFactory)
|
|
|
|
|
- val servlet = new BatchServlet(batchManager)
|
|
|
|
|
-
|
|
|
|
|
- addServlet(servlet, "/*")
|
|
|
|
|
-
|
|
|
|
|
- override def beforeAll() = {
|
|
|
|
|
- super.beforeAll()
|
|
|
|
|
- script = Files.createTempFile("test", "livy-test")
|
|
|
|
|
|
|
+ val script: Path = {
|
|
|
|
|
+ val script = Files.createTempFile("livy-test", ".py")
|
|
|
|
|
+ script.toFile.deleteOnExit()
|
|
|
val writer = new FileWriter(script.toFile)
|
|
val writer = new FileWriter(script.toFile)
|
|
|
try {
|
|
try {
|
|
|
- writer.write("print 'hello world'")
|
|
|
|
|
|
|
+ writer.write(
|
|
|
|
|
+ """
|
|
|
|
|
+ |print "hello world"
|
|
|
|
|
+ """.stripMargin)
|
|
|
} finally {
|
|
} finally {
|
|
|
writer.close()
|
|
writer.close()
|
|
|
}
|
|
}
|
|
|
|
|
+ script
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- override def afterAll() = {
|
|
|
|
|
- script.toFile.delete()
|
|
|
|
|
- super.afterAll()
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ val batchFactory = new BatchProcessFactory()
|
|
|
|
|
+ val batchManager = new BatchManager(batchFactory)
|
|
|
|
|
+ val servlet = new BatchServlet(batchManager)
|
|
|
|
|
+
|
|
|
|
|
+ addServlet(servlet, "/*")
|
|
|
|
|
|
|
|
after {
|
|
after {
|
|
|
batchManager.shutdown()
|
|
batchManager.shutdown()
|
|
@@ -67,6 +68,28 @@ class BatchServletSpec extends ScalatraSuite with FunSpecLike with BeforeAndAfte
|
|
|
batch should be (defined)
|
|
batch should be (defined)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // Wait for the process to finish.
|
|
|
|
|
+ {
|
|
|
|
|
+ val batch: Batch = batchManager.getBatch(0).get
|
|
|
|
|
+ Utils.waitUntil({ () =>
|
|
|
|
|
+ batch.state == Dead()
|
|
|
|
|
+ }, Duration(10, TimeUnit.SECONDS))
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ get("/0") {
|
|
|
|
|
+ status should equal (200)
|
|
|
|
|
+ header("Content-Type") should include("application/json")
|
|
|
|
|
+ val parsedBody = parse(body)
|
|
|
|
|
+ parsedBody \ "id" should equal (JInt(0))
|
|
|
|
|
+ parsedBody \ "state" should equal (JString("dead"))
|
|
|
|
|
+ parsedBody \ "lines" should equal (JArray(List(
|
|
|
|
|
+ JString("hello world")
|
|
|
|
|
+ )))
|
|
|
|
|
+
|
|
|
|
|
+ val batch = batchManager.getBatch(0)
|
|
|
|
|
+ batch should be (defined)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
delete("/0") {
|
|
delete("/0") {
|
|
|
status should equal (200)
|
|
status should equal (200)
|
|
|
header("Content-Type") should include("application/json")
|
|
header("Content-Type") should include("application/json")
|