You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by md...@apache.org on 2018/07/03 20:20:51 UTC

[incubator-openwhisk] 05/19: More cleanup of BaseRunWsk, and some redundant methods.

This is an automated email from the ASF dual-hosted git repository.

mdeuser pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk.git

commit a75d063640480892b3398da9b7b73795707c6979
Author: Rodric Rabbah <ro...@gmail.com>
AuthorDate: Fri Jun 8 11:38:37 2018 -0400

    More cleanup of BaseRunWsk, and some redundant methods.
---
 tests/src/test/scala/common/BaseWsk.scala          | 52 +++++++---------------
 tests/src/test/scala/common/Wsk.scala              | 22 ++++++++-
 tests/src/test/scala/common/WskTestHelpers.scala   |  8 ----
 tests/src/test/scala/common/rest/WskRest.scala     |  2 +-
 .../test/scala/system/basic/WskActionTests.scala   |  7 ++-
 .../scala/system/basic/WskRestActionTests.scala    |  2 +-
 .../test/scala/system/basic/WskRestRuleTests.scala |  4 +-
 7 files changed, 44 insertions(+), 53 deletions(-)

diff --git a/tests/src/test/scala/common/BaseWsk.scala b/tests/src/test/scala/common/BaseWsk.scala
index 8a1611a..d91c578 100644
--- a/tests/src/test/scala/common/BaseWsk.scala
+++ b/tests/src/test/scala/common/BaseWsk.scala
@@ -23,10 +23,8 @@ import java.io.FileWriter
 import java.time.Instant
 
 import scala.concurrent.duration.DurationInt
-import scala.collection.mutable.Buffer
 import scala.concurrent.duration.Duration
 import scala.language.postfixOps
-import org.scalatest.Matchers
 
 import TestUtils._
 import spray.json._
@@ -88,6 +86,16 @@ trait WaitFor {
   }
 }
 
+trait BaseRunWsk {
+  /*
+   * Utility function to return a JSON object from the CLI output that returns
+   * an optional a status line following by the JSON data
+   */
+  def parseJsonString(jsonStr: String): JsObject = {
+    jsonStr.substring(jsonStr.indexOf("\n") + 1).parseJson.asJsObject // Skip optional status line before parsing
+  }
+}
+
 trait BaseWsk extends BaseRunWsk {
   val action: BaseAction
   val trigger: BaseTrigger
@@ -185,7 +193,7 @@ trait BaseDeleteFromCollection extends FullyQualifiedNames {
   def sanitize(name: String)(implicit wp: WskProps): RunResult
 }
 
-trait BaseAction extends BaseRunWsk with BaseDeleteFromCollection with BaseListOrGetFromCollection {
+trait BaseAction extends BaseDeleteFromCollection with BaseListOrGetFromCollection {
 
   def create(name: String,
              artifact: Option[String],
@@ -213,7 +221,7 @@ trait BaseAction extends BaseRunWsk with BaseDeleteFromCollection with BaseListO
              expectedExitCode: Int = SUCCESS_EXIT)(implicit wp: WskProps): RunResult
 }
 
-trait BasePackage extends BaseRunWsk with BaseDeleteFromCollection with BaseListOrGetFromCollection {
+trait BasePackage extends BaseDeleteFromCollection with BaseListOrGetFromCollection {
 
   def create(name: String,
              parameters: Map[String, JsValue] = Map(),
@@ -231,7 +239,7 @@ trait BasePackage extends BaseRunWsk with BaseDeleteFromCollection with BaseList
            expectedExitCode: Int = SUCCESS_EXIT)(implicit wp: WskProps): RunResult
 }
 
-trait BaseTrigger extends BaseRunWsk with BaseDeleteFromCollection with BaseListOrGetFromCollection {
+trait BaseTrigger extends BaseDeleteFromCollection with BaseListOrGetFromCollection {
 
   def create(name: String,
              parameters: Map[String, JsValue] = Map(),
@@ -249,7 +257,7 @@ trait BaseTrigger extends BaseRunWsk with BaseDeleteFromCollection with BaseList
            expectedExitCode: Int = SUCCESS_EXIT)(implicit wp: WskProps): RunResult
 }
 
-trait BaseRule extends BaseRunWsk with BaseDeleteFromCollection with BaseListOrGetFromCollection {
+trait BaseRule extends BaseDeleteFromCollection with BaseListOrGetFromCollection {
 
   def create(name: String,
              trigger: String,
@@ -266,7 +274,7 @@ trait BaseRule extends BaseRunWsk with BaseDeleteFromCollection with BaseListOrG
   def state(name: String, expectedExitCode: Int = SUCCESS_EXIT)(implicit wp: WskProps): RunResult
 }
 
-trait BaseActivation extends BaseRunWsk {
+trait BaseActivation {
 
   def extractActivationId(result: RunResult): Option[String]
 
@@ -298,14 +306,14 @@ trait BaseActivation extends BaseRunWsk {
     implicit wp: WskProps): RunResult
 }
 
-trait BaseNamespace extends BaseRunWsk {
+trait BaseNamespace {
 
   def list(expectedExitCode: Int = SUCCESS_EXIT, nameSort: Option[Boolean] = None)(implicit wp: WskProps): RunResult
 
   def whois()(implicit wskprops: WskProps): String
 }
 
-trait BaseApi extends BaseRunWsk {
+trait BaseApi {
 
   def create(basepath: Option[String] = None,
              relpath: Option[String] = None,
@@ -339,29 +347,3 @@ trait BaseApi extends BaseRunWsk {
              expectedExitCode: Int = SUCCESS_EXIT,
              cliCfgFile: Option[String] = None)(implicit wp: WskProps): RunResult
 }
-
-trait BaseRunWsk extends Matchers {
-
-  // Takes a string and a list of sensitive strings. Any sensistive string found in
-  // the target string will be replaced with "XXXXX", returning the processed string
-  def hideStr(str: String, hideThese: Seq[String]): String = {
-    // Iterate through each string to hide, replacing it in the target string (str)
-    hideThese.fold(str)((updatedStr, replaceThis) => updatedStr.replace(replaceThis, "XXXXX"))
-  }
-
-  /*
-   * Utility function to return a JSON object from the CLI output that returns
-   * an optional a status line following by the JSON data
-   */
-  def parseJsonString(jsonStr: String): JsObject = {
-    jsonStr.substring(jsonStr.indexOf("\n") + 1).parseJson.asJsObject // Skip optional status line before parsing
-  }
-
-  def reportFailure(args: Buffer[String], ec: Integer, rr: RunResult) = {
-    val s = new StringBuilder()
-    s.append(args.mkString(" ") + "\n")
-    if (rr.stdout.nonEmpty) s.append(rr.stdout + "\n")
-    if (rr.stderr.nonEmpty) s.append(rr.stderr)
-    s.append("exit code:")
-  }
-}
diff --git a/tests/src/test/scala/common/Wsk.scala b/tests/src/test/scala/common/Wsk.scala
index af59e19..4d1f3ce 100644
--- a/tests/src/test/scala/common/Wsk.scala
+++ b/tests/src/test/scala/common/Wsk.scala
@@ -18,12 +18,14 @@
 package common
 
 import java.io.File
-import scala.collection.JavaConversions.mapAsJavaMap
 
+import scala.collection.JavaConversions.mapAsJavaMap
 import scala.collection.mutable.Buffer
 import scala.language.postfixOps
 import scala.util.Try
 
+import org.scalatest.Matchers
+
 import TestUtils._
 
 trait HasActivation {
@@ -82,7 +84,7 @@ trait HasActivation {
   }
 }
 
-trait RunWskCmd extends BaseRunWsk {
+trait RunWskCmd extends Matchers {
 
   /**
    * The base command to run. This returns a new mutable buffer, intended for building the rest of the command line.
@@ -138,6 +140,22 @@ trait RunWskCmd extends BaseRunWsk {
       println(s"command will retry to due to network error: $rr")
       retry(i + 1, N, cmd)
     } else rr
+
+  /**
+   * Takes a string and a list of sensitive strings. Any sensistive string found in
+   * the target string will be replaced with "XXXXX", returning the processed string.
+   */
+  private def hideStr(str: String, hideThese: Seq[String]): String = {
+    // Iterate through each string to hide, replacing it in the target string (str)
+    hideThese.fold(str)((updatedStr, replaceThis) => updatedStr.replace(replaceThis, "XXXXX"))
+  }
+
+  private def reportFailure(args: Buffer[String], ec: Integer, rr: RunResult) = {
+    val s = new StringBuilder()
+    s.append(args.mkString(" ") + "\n")
+    if (rr.stdout.nonEmpty) s.append(rr.stdout + "\n")
+    if (rr.stderr.nonEmpty) s.append(rr.stderr)
+    s.append("exit code:")
   }
 }
 
diff --git a/tests/src/test/scala/common/WskTestHelpers.scala b/tests/src/test/scala/common/WskTestHelpers.scala
index 098d5cc..3afa610 100644
--- a/tests/src/test/scala/common/WskTestHelpers.scala
+++ b/tests/src/test/scala/common/WskTestHelpers.scala
@@ -276,14 +276,6 @@ trait WskTestHelpers extends Matchers {
     }
   }
 
-  def removeCLIHeader(response: String): String = {
-    if (response.contains("\n")) response.substring(response.indexOf("\n")) else response
-  }
-
-  def getJSONFromResponse(response: String, isCli: Boolean = false): JsObject = {
-    if (isCli) removeCLIHeader(response).parseJson.asJsObject else response.parseJson.asJsObject
-  }
-
   def getAdditionalTestSubject(newUser: String): WskProps = {
     val wskadmin = new RunWskAdminCmd {}
     WskProps(namespace = newUser, authKey = wskadmin.cli(Seq("user", "create", newUser)).stdout.trim)
diff --git a/tests/src/test/scala/common/rest/WskRest.scala b/tests/src/test/scala/common/rest/WskRest.scala
index 19e093e..b981013 100644
--- a/tests/src/test/scala/common/rest/WskRest.scala
+++ b/tests/src/test/scala/common/rest/WskRest.scala
@@ -23,8 +23,8 @@ import java.util.Base64
 import java.security.cert.X509Certificate
 
 import org.apache.commons.io.FileUtils
-import org.scalatest.Matchers
 import org.scalatest.FlatSpec
+import org.scalatest.Matchers
 import org.scalatest.concurrent.ScalaFutures
 import org.scalatest.time.Span.convertDurationToSpan
 
diff --git a/tests/src/test/scala/system/basic/WskActionTests.scala b/tests/src/test/scala/system/basic/WskActionTests.scala
index db9af72..bee8170 100644
--- a/tests/src/test/scala/system/basic/WskActionTests.scala
+++ b/tests/src/test/scala/system/basic/WskActionTests.scala
@@ -26,7 +26,6 @@ import common.TestUtils
 import common.BaseWsk
 import common.WskProps
 import common.WskTestHelpers
-import common.rest.WskRest
 import spray.json._
 import spray.json.DefaultJsonProtocol._
 
@@ -138,8 +137,8 @@ abstract class WskActionTests extends TestHelpers with WskTestHelpers with JsHel
         action.create(copiedActionName, Some(origActionName), Some("copy"))
       }
 
-      val copiedAction = getJSONFromResponse(wsk.action.get(copiedActionName).stdout, !wsk.isInstanceOf[WskRest])
-      val origAction = getJSONFromResponse(wsk.action.get(copiedActionName).stdout, !wsk.isInstanceOf[WskRest])
+      val copiedAction = wsk.parseJsonString(wsk.action.get(copiedActionName).stdout)
+      val origAction = wsk.parseJsonString(wsk.action.get(copiedActionName).stdout)
 
       copiedAction.fields("annotations") shouldBe origAction.fields("annotations")
       copiedAction.fields("parameters") shouldBe origAction.fields("parameters")
@@ -178,7 +177,7 @@ abstract class WskActionTests extends TestHelpers with WskTestHelpers with JsHel
         action.create(copiedName, Some(origName), Some("copy"), parameters = copiedParams, annotations = copiedAnnots)
       }
 
-      val copiedAction = getJSONFromResponse(wsk.action.get(copiedName).stdout, !wsk.isInstanceOf[WskRest])
+      val copiedAction = wsk.parseJsonString(wsk.action.get(copiedName).stdout)
 
       // CLI does not guarantee order of annotations and parameters so do a diff to compare the values
       copiedAction.fields("parameters").convertTo[Seq[JsObject]] diff resParams shouldBe List()
diff --git a/tests/src/test/scala/system/basic/WskRestActionTests.scala b/tests/src/test/scala/system/basic/WskRestActionTests.scala
index dbafbcd..de2bd2d 100644
--- a/tests/src/test/scala/system/basic/WskRestActionTests.scala
+++ b/tests/src/test/scala/system/basic/WskRestActionTests.scala
@@ -35,6 +35,6 @@ class WskRestActionTests extends WskActionTests {
       action.create(name, Some(TestUtils.getTestActionFilename("empty.js")))
     }
     val rr = wsk.action.get(name)
-    getJSONFromResponse(rr.stdout, false).getFieldPath("exec", "code") shouldBe Some(JsString(""))
+    wsk.parseJsonString(rr.stdout).getFieldPath("exec", "code") shouldBe Some(JsString(""))
   }
 }
diff --git a/tests/src/test/scala/system/basic/WskRestRuleTests.scala b/tests/src/test/scala/system/basic/WskRestRuleTests.scala
index 8b07374..d281a6b 100644
--- a/tests/src/test/scala/system/basic/WskRestRuleTests.scala
+++ b/tests/src/test/scala/system/basic/WskRestRuleTests.scala
@@ -71,8 +71,8 @@ class WskRestRuleTests extends WskRuleTests {
         retry({
           val createStdout = wsk.rule.create(ruleName, trigger, actionName, update = true).stdout
           val getStdout = wsk.rule.get(ruleName).stdout
-          getJSONFromResponse(createStdout, false).fields.get("status") shouldBe status
-          getJSONFromResponse(getStdout, false).fields.get("status") shouldBe status
+          wsk.parseJsonString(createStdout).fields.get("status") shouldBe status
+          wsk.parseJsonString(getStdout).fields.get("status") shouldBe status
         }, 10, Some(1.second))
     }
   }