openwhisk-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dra...@apache.org
Subject [incubator-openwhisk-devtools] branch master updated: added script to node-local testing for running test.js via the action… (#38)
Date Sat, 03 Jun 2017 00:26:31 GMT
This is an automated email from the ASF dual-hosted git repository.

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

The following commit(s) were added to refs/heads/master by this push:
       new  2aa67e3   added script to node-local testing for running test.js via the action…
(#38)
2aa67e3 is described below

commit 2aa67e35a3dfc42fc5e2b926854e4f7d604ba622
Author: tysonnorris <tysonnorris@gmail.com>
AuthorDate: Fri Jun 2 17:26:30 2017 -0700

    added script to node-local testing for running test.js via the action… (#38)
    
    * added script to node-local testing for running test.js via the action container itself,
so that npm libraries can be tested more simply
    * avoiding merge conflict; JSON output was added by another PR
    * fixing multiple params
---
 node-local/README.md  | 18 +++++++++++++++-
 node-local/runtest.sh | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++
 node-local/test.js    | 11 +++++++---
 3 files changed, 82 insertions(+), 4 deletions(-)

diff --git a/node-local/README.md b/node-local/README.md
index 1ec15b5..f59190e 100644
--- a/node-local/README.md
+++ b/node-local/README.md
@@ -24,4 +24,20 @@ cat input.json | node test.js ./path-to-function.js
 
 If you intend to post-process the result, for instance with `jq`, add the parameter `--json`,
 which will make sure `test.js` returns well-formed JSON. The default is off, which means
you
-will get a slightly more readable output.
\ No newline at end of file
+will get a slightly more readable output.
+
+## using npm libraries
+
+If your action uses npm libraries, you may have trouble running it locally without creating
a special environment just for it to run.
+
+In this case you can run the docker image that open whisk uses to launch your action, along
with its preinstalled libraries:
+
+```bash
+./runtest.sh --debug --action ./path-to-function.js --param name=value --param othername=othervalue
+```
+
+Usage:
+* --debug : enable request debugging (by adding NODE_DEBUG=request)
+* --action : action js file you are trying to test 
+* --param : parameter to pass to main (multiple allowed)
+* --image : the action image used to run node (default is openwhisk/nodejs6action:latest)
diff --git a/node-local/runtest.sh b/node-local/runtest.sh
new file mode 100755
index 0000000..444a740
--- /dev/null
+++ b/node-local/runtest.sh
@@ -0,0 +1,57 @@
+#!/bin/bash
+
+abs_path()
+{
+    pushd $(dirname "$1") > /dev/null
+    echo $(pwd)/$(basename "$1")
+    popd > /dev/null
+
+}
+
+this_dir=$(dirname "$(abs_path "$0")")
+action_executor="$this_dir/test.js"
+action_image="openwhisk/nodejs6action:latest"
+debug_env="debug_disabled"
+
+
+while [[ $# -gt 1 ]]
+do
+key="$1"
+
+case $key in
+    --action)
+    action="$(abs_path "$2")"
+    shift # past argument
+    ;;
+    --param)
+    params+=" $2"
+    shift # past argument
+    ;;
+    --image)
+    actionimage="$2"
+    shift # past argument
+    ;;
+    --debug)
+    debug_env="NODE_DEBUG=request"
+    ;;
+    *)
+            # unknown option
+    ;;
+esac
+shift # past argument or value
+done
+
+echo "#######################################################"
+echo "Testing action $action using image $action_image"
+echo "#######################################################"
+
+
+
+docker_cmd="docker run --name=\"actiontest\" --rm -it \
+    -e \"$debug_env\" \
+    -v \"$action_executor:/nodejsAction/testexecutor.js\" \
+    -v \"$action:/nodejsAction/testaction.js\" \
+    $action_image node testexecutor.js ./testaction.js $params"
+
+#using eval here because $params may have multiple space separate values, which need to be
passed as separate args
+eval "$docker_cmd"
diff --git a/node-local/test.js b/node-local/test.js
index 63f4bda..416f368 100644
--- a/node-local/test.js
+++ b/node-local/test.js
@@ -77,12 +77,11 @@ function run(action, params, outputJSON) {
   }
   
   const imports = require(action);
-  
+
   //support a non-exported main function as a fallback
   const mainfunct = imports.main ? imports.main : fallback(action);
   
   let result = mainfunct(params);
-  
   if (result.then) {
     Promise.resolve(result)
       .then(result => console.log(outputJSON ? JSON.stringify(result) : result))
@@ -90,4 +89,10 @@ function run(action, params, outputJSON) {
   } else {
     console.log(outputJSON ? JSON.stringify(result) : result);
   }
-}
\ No newline at end of file
+}
+
+//allow ctrl-c to exit...
+process.on('SIGINT', function() {
+  console.log("exiting...");
+  process.exit();
+});
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
['"commits@openwhisk.apache.org" <commits@openwhisk.apache.org>'].

Mime
View raw message