houshengbo closed pull request #223: Sync tests
URL: https://github.com/apache/incubator-openwhisk-cli/pull/223
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/tests/src/test/scala/system/basic/WskBasicTests.scala 
b/tests/src/test/scala/system/basic/WskBasicTests.scala
index 19e7b81e..a7855599 100644
--- a/tests/src/test/scala/system/basic/WskBasicTests.scala
+++ b/tests/src/test/scala/system/basic/WskBasicTests.scala
@@ -28,6 +28,7 @@ import org.scalatest.junit.JUnitRunner
 import common.ActivationResult
 import common.TestHelpers
 import common.TestCLIUtils
+import common.TestUtils
 import common.TestUtils._
 import common.Wsk
 import common.WskProps
@@ -45,6 +46,11 @@ class WskBasicTests extends TestHelpers with WskTestHelpers {
   val wsk = new Wsk
   val defaultAction = Some(TestCLIUtils.getTestActionFilename("hello.js"))
 
+  /**
+   * Append the current timestamp in ms
+   */
+  def withTimestamp(text: String) = s"${text}-${System.currentTimeMillis}"
+
   behavior of "Wsk CLI"
 
   it should "reject creating duplicate entity" in withAssetCleaner(wskprops) { 
(wp, assetHelper) =>
@@ -464,34 +470,67 @@ class WskBasicTests extends TestHelpers with 
WskTestHelpers {
   behavior of "Wsk Trigger CLI"
 
   it should "create, update, get, fire and list trigger" in 
withAssetCleaner(wskprops) { (wp, assetHelper) =>
-    val name = "listTriggers"
+    val ruleName = withTimestamp("r1toa1")
+    val triggerName = withTimestamp("t1tor1")
+    val actionName = withTimestamp("a1")
     val params = Map("a" -> "A".toJson)
-    assetHelper.withCleaner(wsk.trigger, name) { (trigger, _) =>
-      trigger.create(name, parameters = params)
-      trigger.create(name, update = true)
+    val ns = wsk.namespace.whois()
+
+    assetHelper.withCleaner(wsk.trigger, triggerName) { (trigger, _) =>
+      trigger.create(triggerName, parameters = params)
+      trigger.create(triggerName, update = true)
+    }
+
+    assetHelper.withCleaner(wsk.action, actionName) { (action, name) =>
+      action.create(name, defaultAction)
+    }
+
+    assetHelper.withCleaner(wsk.rule, ruleName) { (rule, name) =>
+      rule.create(name, trigger = triggerName, action = actionName)
     }
-    val stdout = wsk.trigger.get(name).stdout
-    stdout should include regex (""""key": "a"""")
-    stdout should include regex (""""value": "A"""")
-    stdout should include regex (""""publish": false""")
-    stdout should include regex (""""version": "0.0.2"""")
+
+    val trigger = wsk.trigger.get(triggerName)
+    getJSONFromResponse(trigger.stdout, true).fields("parameters") shouldBe 
JsArray(JsObject("key" -> JsString("a"), "value" -> JsString("A")))
+    getJSONFromResponse(trigger.stdout, true).fields("publish") shouldBe 
false.toJson
+    getJSONFromResponse(trigger.stdout, true).fields("version") shouldBe 
"0.0.2".toJson
+
+    val expectedRules = JsObject(
+      ns + "/" + ruleName -> JsObject(
+        "action" -> JsObject("name" -> JsString(actionName), "path" -> 
JsString(ns)),
+        "status" -> JsString("active")))
+
+    // getJSONFromResponse(trigger.stdout, true).fields("rules") shouldBe 
expectedRules
 
     val dynamicParams = Map("t" -> "T".toJson)
-    val run = wsk.trigger.fire(name, dynamicParams)
+    val run = wsk.trigger.fire(triggerName, dynamicParams)
     withActivation(wsk.activation, run) { activation =>
       activation.response.result shouldBe Some(dynamicParams.toJson)
       activation.duration shouldBe 0L // shouldn't exist but CLI generates it
       activation.end shouldBe Instant.EPOCH // shouldn't exist but CLI 
generates it
+      activation.logs shouldBe defined
+      activation.logs.get.size shouldBe 1
+
+      val logEntry = activation.logs.get(0).parseJson.asJsObject
+      val logs = JsArray(logEntry)
+      val ruleActivationId: String = 
logEntry.fields("activationId").convertTo[String]
+      val expectedLogs = JsArray(
+        JsObject(
+          "statusCode" -> JsNumber(0),
+          "activationId" -> JsString(ruleActivationId),
+          "success" -> JsBoolean(true),
+          "rule" -> JsString(ns + "/" + ruleName),
+          "action" -> JsString(ns + "/" + actionName)))
+      logs shouldBe expectedLogs
     }
 
-    val runWithNoParams = wsk.trigger.fire(name, Map())
+    val runWithNoParams = wsk.trigger.fire(triggerName, Map())
     withActivation(wsk.activation, runWithNoParams) { activation =>
       activation.response.result shouldBe Some(JsObject())
       activation.duration shouldBe 0L // shouldn't exist but CLI generates it
       activation.end shouldBe Instant.EPOCH // shouldn't exist but CLI 
generates it
     }
 
-    wsk.trigger.list().stdout should include(name)
+    wsk.trigger.list().stdout should include(triggerName)
   }
 
   it should "create, and get a trigger summary" in withAssetCleaner(wskprops) 
{ (wp, assetHelper) =>
@@ -506,10 +545,20 @@ class WskBasicTests extends TestHelpers with 
WskTestHelpers {
       trigger.create(name, annotations = annots)
     }
 
-    val stdout = wsk.trigger.get(name, summary = true).stdout
+    val result = wsk.trigger.get(name)
     val ns = wsk.namespace.whois()
+    val annos = getJSONFromResponse(result.stdout, true).fields("annotations")
 
-    stdout should include regex (s"trigger /$ns/$name: Trigger 
description\\s*\\(parameters: paramName1, paramName2\\)")
+    getJSONFromResponse(result.stdout, true).fields("name") shouldBe 
name.toJson
+    getJSONFromResponse(result.stdout, true).fields("namespace") shouldBe 
ns.toJson
+
+    annos shouldBe JsArray(
+      JsObject("key" -> JsString("description"), "value" -> JsString("Trigger 
description")),
+      JsObject(
+        "key" -> JsString("parameters"),
+        "value" -> JsArray(
+          JsObject("name" -> JsString("paramName1"), "description" -> 
JsString("Parameter description 1")),
+          JsObject("name" -> JsString("paramName2"), "description" -> 
JsString("Parameter description 2")))))
   }
 
   it should "create a trigger with a name that contains spaces" in 
withAssetCleaner(wskprops) { (wp, assetHelper) =>
@@ -523,17 +572,26 @@ class WskBasicTests extends TestHelpers with 
WskTestHelpers {
   }
 
   it should "create, and fire a trigger using a parameter file" in 
withAssetCleaner(wskprops) {
-    val name = "paramFileTrigger"
-    val file = Some(TestCLIUtils.getTestActionFilename("argCheck.js"))
-    val argInput = Some(TestCLIUtils.getTestActionFilename("validInput2.json"))
+    val ruleName = withTimestamp("r1toa1")
+    val triggerName = withTimestamp("paramFileTrigger")
+    val actionName = withTimestamp("a1")
+    val argInput = Some(TestUtils.getTestActionFilename("validInput2.json"))
 
     (wp, assetHelper) =>
-      assetHelper.withCleaner(wsk.trigger, name) { (trigger, _) =>
-        trigger.create(name)
+      assetHelper.withCleaner(wsk.trigger, triggerName) { (trigger, _) =>
+        trigger.create(triggerName)
+      }
+
+      assetHelper.withCleaner(wsk.action, actionName) { (action, name) =>
+        action.create(name, defaultAction)
+      }
+
+      assetHelper.withCleaner(wsk.rule, ruleName) { (rule, name) =>
+        rule.create(name, trigger = triggerName, action = actionName)
       }
 
       val expectedOutput = JsObject("payload" -> JsString("test"))
-      val run = wsk.trigger.fire(name, parameterFile = argInput)
+      val run = wsk.trigger.fire(triggerName, parameterFile = argInput)
       withActivation(wsk.activation, run) { activation =>
         activation.response.result shouldBe Some(expectedOutput)
       }
@@ -568,12 +626,23 @@ class WskBasicTests extends TestHelpers with 
WskTestHelpers {
   }
 
   it should "create, and fire a trigger to ensure result is empty" in 
withAssetCleaner(wskprops) { (wp, assetHelper) =>
-    val name = "emptyResultTrigger"
-    assetHelper.withCleaner(wsk.trigger, name) { (trigger, _) =>
-      trigger.create(name)
+    val ruleName = withTimestamp("r1toa1")
+    val triggerName = withTimestamp("emptyResultTrigger")
+    val actionName = withTimestamp("a1")
+
+    assetHelper.withCleaner(wsk.trigger, triggerName) { (trigger, _) =>
+      trigger.create(triggerName)
+    }
+
+    assetHelper.withCleaner(wsk.action, actionName) { (action, name) =>
+      action.create(name, defaultAction)
+    }
+
+    assetHelper.withCleaner(wsk.rule, ruleName) { (rule, name) =>
+      rule.create(name, trigger = triggerName, action = actionName)
     }
 
-    val run = wsk.trigger.fire(name)
+    val run = wsk.trigger.fire(triggerName)
     withActivation(wsk.activation, run) { activation =>
       activation.response.result shouldBe Some(JsObject())
     }
@@ -609,6 +678,65 @@ class WskBasicTests extends TestHelpers with 
WskTestHelpers {
     stderr should include regex (s"""Unable to fire trigger '$name': The 
requested resource does not exist. \\(code \\d+\\)""")
   }
 
+  it should "create and fire a trigger with a rule whose action has been 
deleted" in withAssetCleaner(wskprops) {
+    (wp, assetHelper) =>
+      val ruleName1 = withTimestamp("r1toa1")
+      val ruleName2 = withTimestamp("r2toa2")
+      val triggerName = withTimestamp("t1tor1r2")
+      val actionName1 = withTimestamp("a1")
+      val actionName2 = withTimestamp("a2")
+      val ns = wsk.namespace.whois()
+
+      assetHelper.withCleaner(wsk.trigger, triggerName) { (trigger, _) =>
+        trigger.create(triggerName)
+        trigger.create(triggerName, update = true)
+      }
+
+      assetHelper.withCleaner(wsk.action, actionName1) { (action, name) =>
+        action.create(name, defaultAction)
+      }
+      wsk.action.create(actionName2, defaultAction) // Delete this after the 
rule is created
+
+      assetHelper.withCleaner(wsk.rule, ruleName1) { (rule, name) =>
+        rule.create(name, trigger = triggerName, action = actionName1)
+      }
+      assetHelper.withCleaner(wsk.rule, ruleName2) { (rule, name) =>
+        rule.create(name, trigger = triggerName, action = actionName2)
+      }
+      wsk.action.delete(actionName2)
+
+      val run = wsk.trigger.fire(triggerName)
+      withActivation(wsk.activation, run) { activation =>
+        activation.duration shouldBe 0L // shouldn't exist but CLI generates it
+        activation.end shouldBe Instant.EPOCH // shouldn't exist but CLI 
generates it
+        activation.logs shouldBe defined
+        activation.logs.get.size shouldBe 2
+
+        val logEntry1 = activation.logs.get(0).parseJson.asJsObject
+        val logEntry2 = activation.logs.get(1).parseJson.asJsObject
+        val logs = JsArray(logEntry1, logEntry2)
+        val ruleActivationId: String = if 
(logEntry1.getFields("activationId").size == 1) {
+          logEntry1.fields("activationId").convertTo[String]
+        } else {
+          logEntry2.fields("activationId").convertTo[String]
+        }
+        val expectedLogs = JsArray(
+          JsObject(
+            "statusCode" -> JsNumber(0),
+            "activationId" -> JsString(ruleActivationId),
+            "success" -> JsBoolean(true),
+            "rule" -> JsString(ns + "/" + ruleName1),
+            "action" -> JsString(ns + "/" + actionName1)),
+          JsObject(
+            "statusCode" -> JsNumber(1),
+            "success" -> JsBoolean(false),
+            "error" -> JsString("The requested resource does not exist."),
+            "rule" -> JsString(ns + "/" + ruleName2),
+            "action" -> JsString(ns + "/" + actionName2)))
+        logs shouldBe expectedLogs
+      }
+  }
+
   behavior of "Wsk Rule CLI"
 
   it should "create rule, get rule, update rule and list rule" in 
withAssetCleaner(wskprops) { (wp, assetHelper) =>
@@ -784,41 +912,36 @@ class WskBasicTests extends TestHelpers with 
WskTestHelpers {
 
   it should "create a trigger, and fire a trigger to get its individual fields 
from an activation" in withAssetCleaner(
     wskprops) { (wp, assetHelper) =>
-    val name = "activationFields"
+    val ruleName = withTimestamp("r1toa1")
+    val triggerName = withTimestamp("activationFields")
+    val actionName = withTimestamp("a1")
 
-    assetHelper.withCleaner(wsk.trigger, name) { (trigger, _) =>
-      trigger.create(name)
+    assetHelper.withCleaner(wsk.trigger, triggerName) { (trigger, _) =>
+      trigger.create(triggerName)
     }
 
-    val ns = s""""${wsk.namespace.whois()}""""
-    val run = wsk.trigger.fire(name)
+    assetHelper.withCleaner(wsk.action, actionName) { (action, name) =>
+      action.create(name, defaultAction)
+    }
+
+    assetHelper.withCleaner(wsk.rule, ruleName) { (rule, name) =>
+      rule.create(name, trigger = triggerName, action = actionName)
+    }
+
+    val ns = wsk.namespace.whois()
+    val run = wsk.trigger.fire(triggerName)
     withActivation(wsk.activation, run) { activation =>
-      val successMsg = s"ok: got activation ${activation.activationId}, 
displaying field"
-      wsk.activation
-        .get(Some(activation.activationId), fieldFilter = Some("namespace"))
-        .stdout should include regex (s"""(?i)$successMsg namespace\n$ns""")
-      wsk.activation.get(Some(activation.activationId), fieldFilter = 
Some("name")).stdout should include(
-        s"""$successMsg name\n"$name"""")
-      wsk.activation.get(Some(activation.activationId), fieldFilter = 
Some("version")).stdout should include(
-        s"""$successMsg version\n"0.0.1"""")
-      wsk.activation.get(Some(activation.activationId), fieldFilter = 
Some("publish")).stdout should include(
-        s"""$successMsg publish\nfalse""")
-      wsk.activation
-        .get(Some(activation.activationId), fieldFilter = Some("subject"))
-        .stdout should include regex (s"""(?i)$successMsg subject\n""")
-      wsk.activation.get(Some(activation.activationId), fieldFilter = 
Some("activationid")).stdout should include(
-        s"""$successMsg activationid\n"${activation.activationId}""")
-      wsk.activation
-        .get(Some(activation.activationId), fieldFilter = Some("start"))
-        .stdout should include regex (s"""$successMsg start\n\\d""")
-      wsk.activation
-        .get(Some(activation.activationId), fieldFilter = Some("end"))
-        .stdout should include regex (s"""$successMsg end\n\\d""")
-      wsk.activation
-        .get(Some(activation.activationId), fieldFilter = Some("duration"))
-        .stdout should include regex (s"""$successMsg duration\n\\d""")
-      wsk.activation.get(Some(activation.activationId), fieldFilter = 
Some("annotations")).stdout should include(
-        s"""$successMsg annotations\n[]""")
+      var result = wsk.activation.get(Some(activation.activationId))
+      getJSONFromResponse(result.stdout, 
true).fields("namespace").convertTo[String] shouldBe ns
+      getJSONFromResponse(result.stdout, 
true).fields("name").convertTo[String] shouldBe triggerName
+      getJSONFromResponse(result.stdout, 
true).fields("version").convertTo[String] shouldBe "0.0.1"
+      getJSONFromResponse(result.stdout, true).fields("publish") shouldBe 
false.toJson
+      getJSONFromResponse(result.stdout, 
true).fields("subject").convertTo[String] shouldBe ns
+      getJSONFromResponse(result.stdout, 
true).fields("activationId").convertTo[String] shouldBe activation.activationId
+      getJSONFromResponse(result.stdout, true).fields("start") should not be 
JsObject()
+      getJSONFromResponse(result.stdout, true).fields("end") shouldBe 0.toJson
+      getJSONFromResponse(result.stdout, true).fields("duration") shouldBe 
0.toJson
+      getJSONFromResponse(result.stdout, 
true).fields("annotations").convertTo[JsArray].elements.length shouldBe 0
     }
   }
 
diff --git a/tests/src/test/scala/system/basic/WskRuleTests.scala 
b/tests/src/test/scala/system/basic/WskRuleTests.scala
index 89f0a217..c740caf3 100644
--- a/tests/src/test/scala/system/basic/WskRuleTests.scala
+++ b/tests/src/test/scala/system/basic/WskRuleTests.scala
@@ -100,13 +100,11 @@ abstract class WskRuleTests extends TestHelpers with 
WskTestHelpers {
 
     withActivation(wsk.activation, run) { triggerActivation =>
       triggerActivation.cause shouldBe None
-
-      withActivationsFromEntity(
-        wsk.activation,
-        ruleName,
-        since = 
Some(triggerActivation.start.minusMillis(activationTimeSkewFactorMs))) {
-        _.head.cause shouldBe Some(triggerActivation.activationId)
-      }
+      triggerActivation.logs.get.size shouldBe (1)
+      val logs = triggerActivation.logs.get.mkString(" ")
+      logs should include(""""statusCode":0""")
+      logs should include(""""activationId":""")
+      logs should include(""""success":true""")
 
       withActivationsFromEntity(
         wsk.activation,
@@ -137,13 +135,11 @@ abstract class WskRuleTests extends TestHelpers with 
WskTestHelpers {
 
     withActivation(wsk.activation, run) { triggerActivation =>
       triggerActivation.cause shouldBe None
-
-      withActivationsFromEntity(
-        wsk.activation,
-        ruleName,
-        since = 
Some(triggerActivation.start.minusMillis(activationTimeSkewFactorMs))) {
-        _.head.cause shouldBe Some(triggerActivation.activationId)
-      }
+      triggerActivation.logs.get.size shouldBe (1)
+      val logs = triggerActivation.logs.get.mkString(" ")
+      logs should include(""""statusCode":0""")
+      logs should include(""""activationId":""")
+      logs should include(""""success":true""")
 
       withActivationsFromEntity(
         wsk.activation,
@@ -159,7 +155,7 @@ abstract class WskRuleTests extends TestHelpers with 
WskTestHelpers {
     val ruleName = withTimestamp("pr1to1")
     val triggerName = withTimestamp("pt1to1")
     val pkgName = withTimestamp("rule pkg") // spaces in name intended to test 
uri path encoding
-    val pkgBindingName = withTimestamp("rule pkg binding")
+  val pkgBindingName = withTimestamp("rule pkg binding")
     val actionName = withTimestamp("a1 to 1")
     val pkgActionName = s"$pkgName/$actionName"
 
@@ -177,13 +173,11 @@ abstract class WskRuleTests extends TestHelpers with 
WskTestHelpers {
 
     withActivation(wsk.activation, run) { triggerActivation =>
       triggerActivation.cause shouldBe None
-
-      withActivationsFromEntity(
-        wsk.activation,
-        ruleName,
-        since = 
Some(triggerActivation.start.minusMillis(activationTimeSkewFactorMs))) {
-        _.head.cause shouldBe Some(triggerActivation.activationId)
-      }
+      triggerActivation.logs.get.size shouldBe (1)
+      val logs = triggerActivation.logs.get.mkString(" ")
+      logs should include(""""statusCode":0""")
+      logs should include(""""activationId":""")
+      logs should include(""""success":true""")
 
       withActivationsFromEntity(
         wsk.activation,


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to