|
| 1 | +<!-- |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 | +# contributor license agreements. See the NOTICE file distributed with |
| 5 | +# this work for additional information regarding copyright ownership. |
| 6 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 | +# (the "License"); you may not use this file except in compliance with |
| 8 | +# the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | +# |
| 18 | +--> |
| 19 | + |
| 20 | +# Tests for OpenWhisk NodeJS Runtime as Standalone Container |
| 21 | +## Building the Runtime Container |
| 22 | +After a runtime container is built, one should be able to run it as a standalone container. |
| 23 | +The following example shows how to generate a Docker image for the Node.js 18 runtime version and test the standalone runtime using the `curl` command. Testing other runtime versions can be done in the same manner. |
| 24 | + |
| 25 | +- Run the `distDocker` command to generate the local Docker image for the desired runtime version. |
| 26 | +``` |
| 27 | +./gradlew core:nodejs18Action:distDocker |
| 28 | +``` |
| 29 | +This will return the following runtime image with the name `action-nodejs-v18`, which should be listed after using the `docker images` |
| 30 | + |
| 31 | +## Running the Container |
| 32 | +For the purpose of the test. We are going to start the container that has a web service running inside it built using Node/Express. In order to access this service within the container from the outside, as we are about to do using `curl`, port mapping needs to be done next. As a result, we can now access the web service inside docker by first reaching an IP port on `localhost`, which subsequently forwards the request to the docker container's designated port. |
| 33 | +In our example, the `Action` container exposes `port 8080` (see the Dockerfile for the associated Docker image), thus we publish the container's `port 8080` to the `localhost` (here, `port 3008` on `localhost` is chosen arbitrarily, as long as the port is not already assigned for something else): |
| 34 | +``` |
| 35 | +docker run --publish 3008:8080 -i -t action-nodejs-v18:latest |
| 36 | +``` |
| 37 | +A simpler way is to map `port 80` on `localhost` to the container's `port 8080`. The port number assigned to the HTTP protocol is `80` Since we will be sending actions against the runtime using HTTP, using this number will allow us to omit the port in the request later. Without loss of generality, the following examples will use the arbitrarily chosen `port 3008` |
| 38 | + |
| 39 | +## Testing |
| 40 | +This example has prepared a `helloworld.json` file to post using `curl`. |
| 41 | +```json |
| 42 | +{ |
| 43 | + "value": { |
| 44 | + "name" : "nodejs-helloworld", |
| 45 | + "main" : "main", |
| 46 | + "binary": false, |
| 47 | + "code" : "function main() {return {payload: 'Hello World!'};}" |
| 48 | + } |
| 49 | +} |
| 50 | +``` |
| 51 | +The json file contains a simple JavaScript function, which is the actual payload. |
| 52 | + |
| 53 | +### Initialze the Runtime |
| 54 | +Before issuing the action against the runtime, we first initialize the function with by invoking the ```/init``` endpoint. |
| 55 | +``` |
| 56 | +curl -H "Content-Type:application/json" -X POST --data '@openwhisk-runtime-nodejs/tests/src/test/standalone/helloworld/helloworld.json' http://localhost:3008/init |
| 57 | +
|
| 58 | +{"OK":true} |
| 59 | +``` |
| 60 | +being the expected response. |
| 61 | + |
| 62 | +As mentioned above, if `port 80` on `localhost` was used, the command could simply be |
| 63 | +``` |
| 64 | +curl -H "Content-Type:application/json" -X POST --data '@openwhisk-runtime-nodejs/tests/src/test/standalone/helloworld/helloworld.json' http://localhost/init |
| 65 | +``` |
| 66 | + |
| 67 | +#### Run the function |
| 68 | + |
| 69 | +Invoke the function using the ```/run``` endpoint. |
| 70 | + |
| 71 | +``` |
| 72 | +curl -H ""Content-Type:application/json" -X POST --data '@openwhisk-runtime-nodejs/tests/src/test/standalone/helloworld/helloworld.json' http://localhost:3008/run |
| 73 | +``` |
| 74 | + |
| 75 | +The JavaScript function in this example is one without arguments. Using the same json file as during initialization won't be a problem. Strictly speaking, we should have provided another json file with the arguments. In our case, it should simply be |
| 76 | +```json |
| 77 | +{ |
| 78 | + "value": {} |
| 79 | +} |
| 80 | +``` |
| 81 | +The expected response should be |
| 82 | +``` |
| 83 | +{"payload":"Hello World!"} |
| 84 | +``` |
| 85 | +Also the running container will print |
| 86 | +``` |
| 87 | +XXX_THE_END_OF_A_WHISK_ACTIVATION_XXX |
| 88 | +``` |
| 89 | +in the terminal |
0 commit comments