Skip to content

Commit

Permalink
LIVY-210. Add test context for Livy unit test
Browse files Browse the repository at this point in the history
Closes apache#190
  • Loading branch information
jerryshao authored and alex-the-man committed Sep 23, 2016
1 parent 91058d2 commit acac87a
Show file tree
Hide file tree
Showing 26 changed files with 150 additions and 39 deletions.
8 changes: 8 additions & 0 deletions client-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.cloudera.livy</groupId>
<artifactId>livy-core</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import java.util.concurrent.atomic.AtomicLong
import javax.servlet.ServletContext
import javax.servlet.http.HttpServletRequest

import scala.collection.JavaConverters._
import scala.concurrent.{ExecutionContext, Future}

import org.mockito.ArgumentCaptor
Expand All @@ -50,7 +49,7 @@ import com.cloudera.livy.utils.AppInfo
* module, which implements the client session backend. The client servlet has some functionality
* overridden to avoid creating sub-processes for each seession.
*/
class HttpClientSpec extends FunSpecLike with BeforeAndAfterAll {
class HttpClientSpec extends FunSpecLike with BeforeAndAfterAll with LivyBaseUnitTestSuite {

import HttpClientSpec._

Expand Down
16 changes: 16 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,20 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>test-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
35 changes: 35 additions & 0 deletions core/src/test/scala/com/cloudera/livy/LivyBaseUnitTestSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to Cloudera, Inc. under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Cloudera, Inc. licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.cloudera.livy

import org.scalatest.{Outcome, Suite}

trait LivyBaseUnitTestSuite extends Suite with Logging {

protected override def withFixture(test: NoArgTest): Outcome = {
val testName = test.name
val suiteName = this.getClass.getName
try {
info(s"\n\n==== TEST OUTPUT FOR $suiteName: '$testName' ====\n")
test()
} finally {
info(s"\n\n==== FINISHED $suiteName: '$testName' ====\n")
}
}
}
8 changes: 8 additions & 0 deletions repl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@
</exclusions>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>livy-core</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ package com.cloudera.livy.repl

import org.scalatest.{FlatSpec, Matchers}

abstract class BaseInterpreterSpec extends FlatSpec with Matchers {
import com.cloudera.livy.LivyBaseUnitTestSuite

abstract class BaseInterpreterSpec extends FlatSpec with Matchers with LivyBaseUnitTestSuite {

def createInterpreter(): Interpreter

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import org.json4s.DefaultFormats
import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.concurrent.Eventually._

import com.cloudera.livy.LivyBaseUnitTestSuite
import com.cloudera.livy.sessions.SessionState

abstract class BaseSessionSpec extends FlatSpec with Matchers {
abstract class BaseSessionSpec extends FlatSpec with Matchers with LivyBaseUnitTestSuite {

implicit val formats = DefaultFormats

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import com.cloudera.livy._
import com.cloudera.livy.rsc.{PingJob, RSCClient, RSCConf}
import com.cloudera.livy.sessions.Spark

class ReplDriverSuite extends FunSuite {
class ReplDriverSuite extends FunSuite with LivyBaseUnitTestSuite {

private implicit val formats = DefaultFormats

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SparkRInterpreterSpec extends BaseInterpreterSpec {

override protected def withFixture(test: NoArgTest): Outcome = {
assume(!sys.props.getOrElse("skipRTests", "false").toBoolean, "Skipping R tests.")
test()
super.withFixture(test)
}

override def createInterpreter(): Interpreter = SparkRInterpreter(new SparkConf())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SparkRSessionSpec extends BaseSessionSpec {

override protected def withFixture(test: NoArgTest) = {
assume(!sys.props.getOrElse("skipRTests", "false").toBoolean, "Skipping R tests.")
test()
super.withFixture(test)
}

override def createInterpreter(): Interpreter = SparkRInterpreter(new SparkConf())
Expand Down
13 changes: 13 additions & 0 deletions scala-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.cloudera.livy</groupId>
<artifactId>livy-core</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.cloudera.livy</groupId>
<artifactId>livy-core</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_${scala.binary.version}</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,24 @@ import java.util.concurrent.CountDownLatch
import java.util.jar.JarOutputStream
import java.util.zip.ZipEntry

import org.apache.spark.SparkFiles
import org.apache.spark.launcher.SparkLauncher
import org.scalatest.{BeforeAndAfter, FunSuite}
import org.scalatest.concurrent.ScalaFutures
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.language.postfixOps
import scala.util.{Failure, Success}

import org.apache.spark.SparkFiles
import org.apache.spark.launcher.SparkLauncher
import org.scalatest.{BeforeAndAfter, FunSuite}
import org.scalatest.concurrent.ScalaFutures

import com.cloudera.livy.LivyBaseUnitTestSuite
import com.cloudera.livy.rsc.RSCConf.Entry._

class ScalaClientTest extends FunSuite with ScalaFutures with BeforeAndAfter {
class ScalaClientTest extends FunSuite
with ScalaFutures
with BeforeAndAfter
with LivyBaseUnitTestSuite {

import com.cloudera.livy._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ package com.cloudera.livy.scalaapi
import java.util.Random
import java.util.concurrent.{CountDownLatch, TimeUnit}

import org.scalatest.FunSuite
import scala.collection.mutable.ArrayBuffer
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._

object ScalaClientTestUtils extends FunSuite {
import org.scalatest.FunSuite

import com.cloudera.livy.LivyBaseUnitTestSuite

object ScalaClientTestUtils extends FunSuite with LivyBaseUnitTestSuite {

val Timeout = 40

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,24 @@ package com.cloudera.livy.scalaapi

import java.util.concurrent.{CountDownLatch, TimeUnit}

import org.mockito.Matchers._
import org.mockito.Mockito._
import org.scalatest.{BeforeAndAfter, FunSuite}
import org.scalatest.concurrent.ScalaFutures
import scala.concurrent.Await
import scala.concurrent.duration._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.language.postfixOps
import scala.util.{Failure, Success}

import com.cloudera.livy.JobHandle
import org.mockito.Matchers._
import org.mockito.Mockito._
import org.scalatest.{BeforeAndAfter, FunSuite}
import org.scalatest.concurrent.ScalaFutures

import com.cloudera.livy.{JobHandle, LivyBaseUnitTestSuite}
import com.cloudera.livy.JobHandle.{Listener, State}

class ScalaJobHandleTest extends FunSuite with ScalaFutures with BeforeAndAfter {
class ScalaJobHandleTest extends FunSuite
with ScalaFutures
with BeforeAndAfter
with LivyBaseUnitTestSuite {

private var mockJobHandle: JobHandle[String] = null
private var scalaJobHandle: ScalaJobHandle[String] = null
Expand Down
8 changes: 8 additions & 0 deletions server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>livy-core</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>

<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>livy-rsc</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import org.scalatest.FunSpecLike
import org.scalatra.ScalatraServlet
import org.scalatra.test.scalatest.ScalatraSuite

class ApiVersioningSupportSpec extends ScalatraSuite with FunSpecLike {
import com.cloudera.livy.LivyBaseUnitTestSuite

class ApiVersioningSupportSpec extends ScalatraSuite with FunSpecLike with LivyBaseUnitTestSuite {
val LatestVersionOutput = "latest"

object FakeApiVersions extends Enumeration {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import scala.reflect.ClassTag

import com.fasterxml.jackson.databind.ObjectMapper
import org.scalatest.FunSpecLike
import org.scalatra._
import org.scalatra.test.scalatest.ScalatraSuite

import com.cloudera.livy.LivyBaseUnitTestSuite

/**
* Base class that enhances ScalatraSuite so that it's easier to test JsonServlet
* implementations. Variants of the test methods (get, post, etc) exist with the "j"
Expand All @@ -37,7 +38,8 @@ import org.scalatra.test.scalatest.ScalatraSuite
* In case the response is not JSON, the expected type for the test function should be
* `Unit`, and the `response` object should be checked directly.
*/
abstract class BaseJsonServletSpec extends ScalatraSuite with FunSpecLike {
abstract class BaseJsonServletSpec extends ScalatraSuite
with FunSpecLike with LivyBaseUnitTestSuite {

protected val mapper = new ObjectMapper()
.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ package com.cloudera.livy.server

import org.scalatest.FunSuite

import com.cloudera.livy.LivyConf
import com.cloudera.livy.{LivyBaseUnitTestSuite, LivyConf}

class LivyServerSuite extends FunSuite {
class LivyServerSuite extends FunSuite with LivyBaseUnitTestSuite {

private val livyConf = new LivyConf()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ import scala.concurrent.duration.Duration
import org.scalatest.{BeforeAndAfterAll, FunSpec, ShouldMatchers}
import org.scalatest.mock.MockitoSugar.mock

import com.cloudera.livy.{LivyConf, Utils}
import com.cloudera.livy.{LivyBaseUnitTestSuite, LivyConf, Utils}
import com.cloudera.livy.sessions.SessionState
import com.cloudera.livy.utils.{AppInfo, SparkApp}

class BatchSessionSpec
extends FunSpec
with BeforeAndAfterAll
with ShouldMatchers {
with ShouldMatchers
with LivyBaseUnitTestSuite {

val script: Path = {
val script = Files.createTempFile("livy-test", ".py")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ package com.cloudera.livy.server.batch
import com.fasterxml.jackson.databind.{JsonMappingException, ObjectMapper}
import org.scalatest.FunSpec

class CreateBatchRequestSpec extends FunSpec {
import com.cloudera.livy.LivyBaseUnitTestSuite

class CreateBatchRequestSpec extends FunSpec with LivyBaseUnitTestSuite {

private val mapper = new ObjectMapper()
.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ package com.cloudera.livy.server.interactive
import com.fasterxml.jackson.databind.ObjectMapper
import org.scalatest.FunSpec

import com.cloudera.livy.LivyBaseUnitTestSuite
import com.cloudera.livy.sessions.{PySpark, SessionKindModule}

class CreateInteractiveRequestSpec extends FunSpec {
class CreateInteractiveRequestSpec extends FunSpec with LivyBaseUnitTestSuite {

private val mapper = new ObjectMapper()
.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ import org.scalatest.{BeforeAndAfterAll, FunSpec, Matchers}
import org.scalatest.concurrent.Eventually._
import org.scalatest.mock.MockitoSugar.mock

import com.cloudera.livy.{ExecuteRequest, LivyConf}
import com.cloudera.livy.{ExecuteRequest, LivyBaseUnitTestSuite, LivyConf}
import com.cloudera.livy.rsc.RSCConf
import com.cloudera.livy.sessions.{PySpark, SessionState}
import com.cloudera.livy.utils.{AppInfo, SparkApp}

class InteractiveSessionSpec extends FunSpec with Matchers with BeforeAndAfterAll {
class InteractiveSessionSpec extends FunSpec
with Matchers with BeforeAndAfterAll with LivyBaseUnitTestSuite {

private val livyConf = new LivyConf()
livyConf.set(InteractiveSession.LivyReplJars, "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import org.json4s.{DefaultFormats, Extraction}
import org.json4s.JsonAST.JString
import org.scalatest.{FunSpec, Matchers}

import com.cloudera.livy.ExecuteRequest
import com.cloudera.livy.{ExecuteRequest, LivyBaseUnitTestSuite}

class StatementSpec extends FunSpec with Matchers {
class StatementSpec extends FunSpec with Matchers with LivyBaseUnitTestSuite {

implicit val formats = DefaultFormats

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import scala.language.postfixOps
import org.scalatest.{FlatSpec, Matchers}
import org.scalatest.concurrent.Eventually._

import com.cloudera.livy.LivyConf
import com.cloudera.livy.{LivyBaseUnitTestSuite, LivyConf}

class SessionManagerSpec extends FlatSpec with Matchers {
class SessionManagerSpec extends FlatSpec with Matchers with LivyBaseUnitTestSuite {

it should "garbage collect old sessions" in {
val livyConf = new LivyConf()
Expand Down
Loading

0 comments on commit acac87a

Please sign in to comment.