dubeejw commented on a change in pull request #250: Update websocket test to 
use local websocket server
URL: 
https://github.com/apache/incubator-openwhisk-catalog/pull/250#discussion_r155823363
 
 

 ##########
 File path: tests/src/test/scala/packages/websocket/WebSocketTests.scala
 ##########
 @@ -17,81 +17,141 @@
 
 package packages.websocket
 
+import java.io.File
 import java.net.URI
 
 import scala.concurrent.duration.DurationInt
 import scala.language.postfixOps
-
 import org.junit.runner.RunWith
 import org.scalatest.BeforeAndAfterAll
 import org.scalatest.junit.JUnitRunner
-
-import common.TestHelpers
-import common.Wsk
-import common.WskProps
-import common.WskTestHelpers
+import common._
 import spray.json._
 import spray.json.DefaultJsonProtocol._
 
 @RunWith(classOf[JUnitRunner])
-class WebSocketTests
-    extends TestHelpers
-    with WskTestHelpers
-    with BeforeAndAfterAll {
-
-    implicit val wskprops = WskProps()
-    val wsk = new Wsk()
-
-    val websocketSendAction = "/whisk.system/websocket/send"
-
-    behavior of "Websocket action"
-
-    /**
-     * This test requires a websocket server running on the given URI.
-     */
-    var serverURI: URI = new URI("ws://169.46.21.246:80")
-
-    ignore should "Use the websocket action to send a payload" in {
-        val uniquePayload = s"The cow says 
${System.currentTimeMillis()}".toJson
-        val run = wsk.action.invoke(websocketSendAction, Map("uri" -> 
serverURI.toString.toJson, "payload" -> uniquePayload))
-        withActivation(wsk.activation, run, 1 second, 1 second, 180 seconds) {
-            activation =>
-                activation.response.success shouldBe true
-                activation.response.result shouldBe Some(JsObject(
-                    "payload" -> uniquePayload))
-        }
+class WebSocketTests extends TestHelpers with WskTestHelpers with 
BeforeAndAfterAll {
+
+  implicit val wskprops: WskProps = WskProps()
+  val wsk = new Wsk()
+
+  val websocketSendAction = "/whisk.system/websocket/send"
+
+  val containerName = "websocketServerOW"
+  val userDir: String = System.getProperty("user.dir")
+  val websocketDir: String = userDir + "/dat/websocket"
+  val websocketHost: String = wskprops.apihost
+  val websocketPort = "20002"
+
+  behavior of "Websocket action"
+
+  /**
+   * This test requires a websocket server running on the given URI.
+   */
+  val serverURI: URI = new URI(s"ws://$websocketHost:$websocketPort")
+
+  it should "Use the websocket action to send a payload" in {
+    val uniquePayload = s"The cow says ${System.currentTimeMillis()}".toJson
+    val run =
+      wsk.action.invoke(websocketSendAction, Map("uri" -> 
serverURI.toString.toJson, "payload" -> uniquePayload))
+    withActivation(wsk.activation, run, 1 second, 1 second, 180 seconds) { 
activation =>
+      activation.response.success shouldBe true
+      activation.response.result shouldBe Some(JsObject("payload" -> 
uniquePayload))
     }
+  }
 
-    it should "Return an error due to a malformed URI" in {
-        val badURI = new URI("ws://localhost:80")
+  it should "Return an error due to a malformed URI" in {
+    val badURI = new URI("ws://localhost:80")
 
-        val run = wsk.action.invoke(websocketSendAction, Map("uri" -> 
badURI.toString.toJson, "payload" -> "This is the message to send".toJson))
-        withActivation(wsk.activation, run) {
-            activation =>
-                activation.response.success shouldBe false
+    val run = wsk.action.invoke(
+      websocketSendAction,
+      Map("uri" -> badURI.toString.toJson, "payload" -> "This is the message 
to send".toJson))
+    withActivation(wsk.activation, run) { activation =>
+      activation.response.success shouldBe false
 
-                // the exact error content comes from the ws Node module
-                activation.response.result.get.fields.get("error") shouldBe 
defined
-        }
+      // the exact error content comes from the ws Node module
+      activation.response.result.get.fields.get("error") shouldBe defined
     }
+  }
 
-    it should "Require a payload parameter" in {
-        val run = wsk.action.invoke(websocketSendAction, Map("uri" -> 
serverURI.toString.toJson))
-        withActivation(wsk.activation, run) {
-            activation =>
-                activation.response.success shouldBe false
-                activation.response.result shouldBe Some(JsObject(
-                    "error" -> "You must specify a payload parameter.".toJson))
-        }
+  it should "Require a payload parameter" in {
+    val run = wsk.action.invoke(websocketSendAction, Map("uri" -> 
serverURI.toString.toJson))
+    withActivation(wsk.activation, run) { activation =>
+      activation.response.success shouldBe false
+      activation.response.result shouldBe Some(JsObject("error" -> "You must 
specify a payload parameter.".toJson))
     }
+  }
 
-    it should "Require a uri parameter" in {
-        val run = wsk.action.invoke(websocketSendAction, Map("payload" -> 
"This is the message to send".toJson))
-        withActivation(wsk.activation, run) {
-            activation =>
-                activation.response.success shouldBe false
-                activation.response.result shouldBe Some(JsObject(
-                    "error" -> "You must specify a uri parameter.".toJson))
-        }
+  it should "Require a uri parameter" in {
+    val run = wsk.action.invoke(websocketSendAction, Map("payload" -> "This is 
the message to send".toJson))
+    withActivation(wsk.activation, run) { activation =>
+      activation.response.success shouldBe false
+      activation.response.result shouldBe Some(JsObject("error" -> "You must 
specify a uri parameter.".toJson))
     }
+  }
+
+  override def beforeAll() {
+
+    val websocketServer =
+      """
+         | const WebSocket = require("ws");
+         | const wss = new WebSocket.Server({ port: 8080 });
+         | wss.on("connection", function connection(ws) {
+         |  ws.on("message", function incoming(message) {
+         |    ws.send(message);
+         |  });
+         | });
+         """.replace("\r", "").stripMargin.lines.mkString
+    println(websocketServer)
+    TestUtils.runCmd(TestUtils.DONTCARE_EXIT, new File("."), "docker", "kill", 
containerName)
+    TestUtils.runCmd(TestUtils.DONTCARE_EXIT, new File("."), "docker", "rm", 
containerName)
+    TestUtils.runCmd(
+      0,
+      new File("."),
+      "docker",
+      "run",
+      "-d",
+      "-p",
+      "20002:8080",
+      "--name",
+      containerName,
+      "node",
+      "sh",
+      "-c",
+      s"npm install ws; echo '$websocketServer' | node")
+    sleepUntilContainerRunning()
+
+  }
+
+  def sleepUntilContainerRunning() {
+    var counter = 12
+    var running = false
+    do {
+      counter = counter - 1
+      val isdb2Running = TestUtils
+        .runCmd(
+          TestUtils.DONTCARE_EXIT,
+          new File("."),
+          "docker",
+          "exec",
+          "-t",
+          containerName,
+          "curl",
+          "http://localhost:8080";,
+          "--connect-timeout",
+          "5")
+
+      if (isdb2Running.exitCode != 0) {
+        println("sleeping 5 seconds to wait for websocket server")
+        Thread.sleep(5000)
+      } else {
+        running = true
+      }
+    } while (counter > 0 && !running)
+  }
 
 Review comment:
   Can you do an assert here to ensure that running == true and print a message 
saying that the container didn't start if the assert fails.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to