Skip to content

Commit 795dafe

Browse files
authored
add watson tests (ibm-functions#11)
1 parent 8572dde commit 795dafe

File tree

9 files changed

+173
-6
lines changed

9 files changed

+173
-6
lines changed

.travis.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
sudo: required
22
group: deprecated-2017Q3
33
language: scala
4-
scala:
5-
- 2.11.8
64
services:
75
- docker
86
before_install:
7+
- 'if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then openssl aes-256-cbc -K $encrypted_10110adb91b4_key -iv $encrypted_10110adb91b4_iv -in tests/credentials.json.enc -out tests/credentials.json -d; fi'
98
- "./tools/travis/setup.sh"
109
install: true
1110
script:
@@ -21,4 +20,8 @@ deploy:
2120
on:
2221
tags: true
2322
all_branches: true
24-
repo: ibm-functions/runtime-python
23+
repo: ibm-functions/runtime-python
24+
env:
25+
global:
26+
- secure: QTl+38OboTb8QyI6FqOyXEx932GNbEx25dSz4dITvXgItBk4cWhq1MKFy2qmbXlZE4Xt1I8Jqrx4DVv0uuyJJN4tA9B7g8L0Bul87ataL9x8fAM4o3q3OBNxvlQ7R7FesQHeYB6W+v8t89BCHrQgq+YMZCsCHto1Z+BkpZlUryiQJd2oiMICP05GPMk87VSFNVJNeSKhpcOB6A/QfgIWVs6pl+JdB/KbVwL1oEysHwguUfMIWKwhEqFTQRuspTxd6OEEUuxGMoxTuEirj02uZQB/H4xzQcC5DdyjD66XYVAlkar/6eYuLA7DaW4WUxoixc+92PDe9vZC43g2mDXNMF82R50LoOdI8kyP8libKnBhw1BRR7u5zTIVj3Hckqf894K5YGqA3mVhTTxuAjBdcYJBvnNhWstpE3p8fl7qARk1pkyUB6c2CS2JC504gKxqh6HVNOGd9edNo9+N8NKAFhwXNniXkpFcEBa1TmWn0zVBTlBFm5EwtNKVFpyyIlf2DIqUUnCHg2qxL5Kp4wC98syiJCB6PXov7V97p0Y5enwGBTO4CEtgmnK1XcZenWcJvFUETSMNVZ5whDlYgT1atdULo77pHA1QxTaVsX/eyAHgpdQdP5iRSdMZSL6myOhj22UEeziam/iGouqMb7PZg8SSMpLomkjZ/A6caGI8DTQ=
27+
- secure: ewJfkWCR1AaLB78t1ENt3Z+zfrNJsOE2wpOc26cWDHZyYplZSdGQJqcvLVqWT+ZEPCsevxI6wFdq9Wbv26n1aQhF5uD1kbZwWqVFiKp8SCGGF2zzigDljvQ/5hDeVcytsbqDSEh+ZT4VXUsOgogA6oO1oLoBo2Qig4aj9Teyro9pTeiQmH8/7x+K6CUEm6svb2kbgIPpOffmdjj60LYsGewOjZlenubYm+ti5yUOZ9ruUeDT3CKEDiP2zTlF/TpLU6SRALkDZUNCPTF5lZwHd8yvpEIkH/qddNrqyQUhoiPQJxBQ2XVQLJ+pIv4KQQRIMH8cJ4dR4l1PGX+mj3Eq9AR5zbhzrOd3AsXVmxWkbDGNOGLUWXZKoi17q6U3RapRj0/S5Vc90LOBloJ4lTpH/stZmHKcrWxetodVj2Du5G2PJbAYf0MavnF/NbXYoUfr8EL6gQwbqaWPwKknK6SgDPmrSxOrK3sffGnH2Myv5UdksCokKLbvF32fhR44VF/H7/9RMBHfbWNGAzR6adFN2/T8d4nUpQg3FPnsZzg1uTRC6mCA9M5D7ctjKimjrB/iS1E1DI75GQHcdJaxPm1Zh7XGUrzKepL+caSfvyniNes2cwCwM5HF5OVXgOcu8O4l/3keFHkzxXJSzs7nZ+hnOe7W2GWphs28Y75Z3U+T2yg=

python3/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ twisted == 17.1.0
1919
# IBM specific python modules
2020
ibm_db == 2.0.7
2121
cloudant == 2.7.0
22-
watson-developer-cloud == 1.0.1
22+
watson-developer-cloud == 1.0.2

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ include 'python3'
1313
rootProject.name = 'runtime-python-ibm'
1414

1515
gradle.ext.scala = [
16-
version: '2.11.8',
16+
version: '2.11.11',
1717
compileFlags: ['-feature', '-unchecked', '-deprecation', '-Xfatal-warnings', '-Ywarn-unused-import']
1818
]
1919

tests/credentials.json.enc

4.45 KB
Binary file not shown.

tests/dat/watson/testWatsonSDK.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Python test fixture to check that watson sdk loads"""
2+
3+
from watson_developer_cloud import DiscoveryV1
4+
5+
def main(dict):
6+
return {"message": DiscoveryV1.VERSION_DATE_2017_11_07}
7+
8+
if __name__ == "__main__":
9+
# execute only if run as a script
10+
print(main({}))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Python test fixture to check that watson service"""
2+
3+
from watson_developer_cloud import LanguageTranslatorV2
4+
5+
def main(args):
6+
language_translator = LanguageTranslatorV2(
7+
url=args.get("url"),
8+
username=args.get("username"),
9+
password=args.get("password"))
10+
return language_translator.translate("Hello", source='en', target='es')
11+
12+
if __name__ == "__main__":
13+
# execute only if run as a script
14+
input = {"url":"<url from credentials>",
15+
"username":"<username from credentials>",
16+
"password":"<password from credentials>"}
17+
print(main(input))
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2017 IBM Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package actionContainers
17+
18+
import org.junit.runner.RunWith
19+
import org.scalatest.junit.JUnitRunner
20+
import common.WskProps
21+
import java.io.File
22+
import common.rest.WskRest
23+
import spray.json._
24+
import spray.json.DefaultJsonProtocol._
25+
import common.TestHelpers
26+
import common.WskTestHelpers
27+
28+
@RunWith(classOf[JUnitRunner])
29+
class IBMPythonActionWatsonTests extends TestHelpers with WskTestHelpers {
30+
31+
implicit val wskprops: WskProps = WskProps()
32+
var defaultKind = Some("python-jessie:3")
33+
val wsk = new WskRest
34+
val datdir = System.getProperty("user.dir") + "/dat/watson/"
35+
val actionName = "testWatsonSDK"
36+
val actionFileName = "testWatsonSDK.py"
37+
38+
it should "Test whether or not watson package is usable within a python action" in withAssetCleaner(wskprops) {
39+
(wp, assetHelper) =>
40+
/*
41+
Disclaimer : Does not Use / Connect to a watson service! Tests that the
42+
watson-developer-cloud npm package is useable by verifying creating a new
43+
discover object creates the expected object with the expected properties.
44+
*/
45+
46+
val file = Some(new File(datdir, actionFileName).toString())
47+
48+
assetHelper.withCleaner(wsk.action, actionName) { (action, _) =>
49+
action.create(
50+
actionName,
51+
file,
52+
main = Some("main"),
53+
kind = defaultKind,
54+
parameters = Map("hostname" -> wskprops.apihost.toJson))
55+
}
56+
57+
withActivation(wsk.activation, wsk.action.invoke(actionName)) { activation =>
58+
val response = activation.response
59+
response.result.get.fields.get("error") shouldBe empty
60+
response.result.get.fields.get("message") should be(Some(JsString("2017-11-07")))
61+
}
62+
63+
}
64+
65+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2017 IBM Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package integration
17+
18+
import org.junit.runner.RunWith
19+
import org.scalatest.junit.JUnitRunner
20+
import common.WskProps
21+
import java.io.File
22+
import spray.json._
23+
import common.TestUtils
24+
import common.rest.WskRest
25+
import common.TestHelpers
26+
import common.WskTestHelpers
27+
28+
@RunWith(classOf[JUnitRunner])
29+
class CredentialsIBMPythonWatsonTests extends TestHelpers with WskTestHelpers {
30+
31+
implicit val wskprops: WskProps = WskProps()
32+
var defaultKind = Some("python-jessie:3")
33+
val wsk = new WskRest
34+
val datdir = System.getProperty("user.dir") + "/dat/watson"
35+
val actionName = "testWatsonService"
36+
val actionFileName = "testWatsonService.py"
37+
38+
var creds = TestUtils.getVCAPcredentials("language_translator")
39+
40+
/*
41+
Uses Watson Translation Service to translate the word "Hello" in English, to "Hola" in Spanish.
42+
*/
43+
it should "Test whether watson translate service is reachable" in withAssetCleaner(wskprops) { (wp, assetHelper) =>
44+
val file = Some(new File(datdir, actionFileName).toString())
45+
assetHelper.withCleaner(wsk.action, actionName) { (action, _) =>
46+
action.create(
47+
actionName,
48+
file,
49+
main = Some("main"),
50+
kind = defaultKind,
51+
parameters = Map(
52+
"url" -> JsString(creds.get("url")),
53+
"username" -> JsString(creds.get("username")),
54+
"password" -> JsString(creds.get("password"))))
55+
}
56+
57+
withActivation(wsk.activation, wsk.action.invoke(actionName)) { activation =>
58+
val response = activation.response
59+
response.result.get.fields.get("error") shouldBe empty
60+
response.result.get.fields.get("translations") should be(
61+
Some(JsArray(JsObject("translation" -> JsString("Hola")))))
62+
}
63+
64+
}
65+
66+
}

tools/travis/deploy.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ $ANSIBLE_CMD openwhisk.yml -e cli_installation_mode=remote
2424
docker images
2525
docker ps
2626

27-
cat $WHISKDIR/whisk.properties
27+
#update whisk.properties to add tests/credentials.json file to vcap.services.file, which is needed in tests
28+
VCAP_SERVICES_FILE="$(readlink -f ${ROOTDIR}/tests/credentials.json)"
29+
WHISKPROPS_FILE="$WHISKDIR/whisk.properties"
30+
sed -i 's:^[ \t]*vcap.services.file[ \t]*=\([ \t]*.*\)$:vcap.services.file='$VCAP_SERVICES_FILE':' $WHISKPROPS_FILE
31+
32+
cat $WHISKPROPS_FILE
33+
2834
curl -s -k https://172.17.0.1 | jq .
2935
curl -s -k https://172.17.0.1/api/v1 | jq .
3036

0 commit comments

Comments
 (0)