This is an automated email from the ASF dual-hosted git repository. dubeejw pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk-catalog.git
The following commit(s) were added to refs/heads/master by this push: new ad24d4c Update websocket test to use local websocket server (#250) ad24d4c is described below commit ad24d4ca7c8985122b9c93b2279065b0b25a6ef1 Author: Carlos Santana <csantan...@gmail.com> AuthorDate: Sat Dec 9 01:23:02 2017 -0500 Update websocket test to use local websocket server (#250) * Update websocket test to use local websocket server * scala version is selected by gradle not travis --- .gitignore | 7 + .scalafmt.conf | 6 + .travis.yml | 2 - settings.gradle | 2 +- .../scala/packages/websocket/WebSocketTests.scala | 174 ++++++++++++++------- 5 files changed, 131 insertions(+), 60 deletions(-) diff --git a/.gitignore b/.gitignore index 879f234..b3c6415 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,10 @@ tests/.classpath tests/.gitignore tests/.project tests/.settings/ + +# NodeJS +node_modules/ + +# IntelliJ +.idea +out/ \ No newline at end of file diff --git a/.scalafmt.conf b/.scalafmt.conf new file mode 100644 index 0000000..0dc41ec --- /dev/null +++ b/.scalafmt.conf @@ -0,0 +1,6 @@ +style = intellij +danglingParentheses = false +maxColumn = 120 +docstrings = JavaDoc +rewrite.rules = [SortImports] +project.git = true \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index a15e4c4..edb2a67 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,8 +3,6 @@ sudo: required language: scala -scala: - - 2.11.6 services: - docker diff --git a/settings.gradle b/settings.gradle index 81f4288..093728f 100644 --- a/settings.gradle +++ b/settings.gradle @@ -11,6 +11,6 @@ include 'tests' rootProject.name = 'openwhisk-catalog' gradle.ext.scala = [ - version: '2.11.8', + version: '2.11.11', compileFlags: ['-feature', '-unchecked', '-deprecation', '-Xfatal-warnings', '-Ywarn-unused-import'] ] diff --git a/tests/src/test/scala/packages/websocket/WebSocketTests.scala b/tests/src/test/scala/packages/websocket/WebSocketTests.scala index a9a1767..54353eb 100644 --- a/tests/src/test/scala/packages/websocket/WebSocketTests.scala +++ b/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 + 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) + running shouldBe true + } + + override def afterAll() { + TestUtils.runCmd(TestUtils.DONTCARE_EXIT, new File("."), "docker", "kill", containerName) + TestUtils.runCmd(TestUtils.DONTCARE_EXIT, new File("."), "docker", "rm", containerName) + } } -- To stop receiving notification emails like this one, please contact ['"commits@openwhisk.apache.org" <commits@openwhisk.apache.org>'].