You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by mr...@apache.org on 2017/06/15 02:02:09 UTC
[incubator-openwhisk-wskdeploy] branch master updated: Add
zipaction integration test cases. (#256)
This is an automated email from the ASF dual-hosted git repository.
mrutkowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk-wskdeploy.git
The following commit(s) were added to refs/heads/master by this push:
new 6e40a9a Add zipaction integration test cases. (#256)
6e40a9a is described below
commit 6e40a9ace0a181aa724ae96d58e2461bbffa93bc
Author: David Liu <da...@cn.ibm.com>
AuthorDate: Thu Jun 15 10:02:08 2017 +0800
Add zipaction integration test cases. (#256)
Fix zip action error.
Remove some unnecessary files.
Address issue #245.
---
parsers/manifest_parser.go | 10 +-
tests/src/integration/manifest.yaml | 6 --
.../src/integration/zipaction/actions/cat/index.js | 5 +
.../zipaction/actions/cat/node_modules/.bin/cat | 1 +
.../actions/cat/node_modules/cat/README.md | 24 +++++
.../zipaction/actions/cat/node_modules/cat/bin.js | 14 +++
.../actions/cat/node_modules/cat/example.js | 5 +
.../actions/cat/node_modules/cat/index.js | 54 +++++++++++
.../actions/cat/node_modules/cat/package.json | 74 +++++++++++++++
.../integration/zipaction/actions/cat/package.json | 7 ++
tests/src/integration/zipaction/deployment.yml | 15 +++
tests/src/integration/zipaction/manifest.yml | 15 +++
tests/src/integration/zipaction/src/index.js | 5 -
tests/src/integration/zipaction/zipaction_test.go | 5 +-
utils/misc.go | 103 +++++++--------------
15 files changed, 255 insertions(+), 88 deletions(-)
diff --git a/parsers/manifest_parser.go b/parsers/manifest_parser.go
index ad7489e..01ad3ef 100644
--- a/parsers/manifest_parser.go
+++ b/parsers/manifest_parser.go
@@ -25,7 +25,7 @@ import (
"path"
"strings"
- "encoding/base64"
+ "encoding/base64"
"encoding/json"
"github.com/apache/incubator-openwhisk-client-go/whisk"
@@ -238,7 +238,7 @@ func (dm *YAMLParser) ComposeActions(mani *ManifestYAML, manipath string) (ar []
if utils.IsDirectory(filePath) {
zipName := filePath + ".zip"
- err := utils.CreateFolderZip(filePath, zipName)
+ err := utils.NewZipWritter(filePath, zipName).Zip()
defer os.Remove(zipName)
utils.Check(err)
// To do: support docker and main entry as did by go cli?
@@ -262,9 +262,9 @@ func (dm *YAMLParser) ComposeActions(mani *ManifestYAML, manipath string) (ar []
dat, err := utils.Read(filePath)
utils.Check(err)
code := string(dat)
- if ext == ".zip" || ext == ".jar" {
- code = base64.StdEncoding.EncodeToString([]byte(dat))
- }
+ if ext == ".zip" || ext == ".jar" {
+ code = base64.StdEncoding.EncodeToString([]byte(dat))
+ }
wskaction.Exec.Code = &code
}
diff --git a/tests/src/integration/manifest.yaml b/tests/src/integration/manifest.yaml
deleted file mode 100644
index f03bfa1..0000000
--- a/tests/src/integration/manifest.yaml
+++ /dev/null
@@ -1,6 +0,0 @@
-package:
- name: helloworld
- actions:
- helloworld:
- location: src/helloworld.zip
- kind: nodejs:6
diff --git a/tests/src/integration/zipaction/actions/cat/index.js b/tests/src/integration/zipaction/actions/cat/index.js
new file mode 100644
index 0000000..9a2666e
--- /dev/null
+++ b/tests/src/integration/zipaction/actions/cat/index.js
@@ -0,0 +1,5 @@
+function myAction(args) {
+ var cat = require('cat');
+ cat('https://baidu.com', console.log);
+}
+exports.main = myAction;
diff --git a/tests/src/integration/zipaction/actions/cat/node_modules/.bin/cat b/tests/src/integration/zipaction/actions/cat/node_modules/.bin/cat
new file mode 120000
index 0000000..a618fc3
--- /dev/null
+++ b/tests/src/integration/zipaction/actions/cat/node_modules/.bin/cat
@@ -0,0 +1 @@
+../cat/bin.js
\ No newline at end of file
diff --git a/tests/src/integration/zipaction/actions/cat/node_modules/cat/README.md b/tests/src/integration/zipaction/actions/cat/node_modules/cat/README.md
new file mode 100644
index 0000000..46473e8
--- /dev/null
+++ b/tests/src/integration/zipaction/actions/cat/node_modules/cat/README.md
@@ -0,0 +1,24 @@
+# cat
+
+cat will read the contents of an url. it's available through npm
+
+ npm install cat
+
+it will read your files
+
+```js
+var cat = require('cat');
+
+cat('myfile.txt', console.log); // reads the file as utf-8 and returns it output
+cat('file://myfile.txt', console.log); // same as above
+```
+
+and your `http` / `https` urls
+
+```js
+cat('http://google.com', console.log); // cat also follows any redirects
+cat('https://github.com', console.log); // and cat read https
+cat('http://fail.google.com', console.log); // if a status code != 2xx is received it will
+ // call the callback with an error.
+
+```
\ No newline at end of file
diff --git a/tests/src/integration/zipaction/actions/cat/node_modules/cat/bin.js b/tests/src/integration/zipaction/actions/cat/node_modules/cat/bin.js
new file mode 100755
index 0000000..5b61203
--- /dev/null
+++ b/tests/src/integration/zipaction/actions/cat/node_modules/cat/bin.js
@@ -0,0 +1,14 @@
+#!/usr/bin/env node
+
+var fs = require('fs')
+
+var input = process.argv.slice(2)
+
+var loop = function() {
+ if (!input.length) return
+ var next = input.shift()
+ var s = next === '-' ? process.stdin : fs.createReadStream(next)
+ s.on('end', loop).pipe(process.stdout)
+}
+
+loop()
\ No newline at end of file
diff --git a/tests/src/integration/zipaction/actions/cat/node_modules/cat/example.js b/tests/src/integration/zipaction/actions/cat/node_modules/cat/example.js
new file mode 100644
index 0000000..96b6d24
--- /dev/null
+++ b/tests/src/integration/zipaction/actions/cat/node_modules/cat/example.js
@@ -0,0 +1,5 @@
+var cat = require('./index');
+
+cat(__filename, console.log);
+
+cat('https://raw.github.com/mafintosh/cat/master/example.js', console.log);
\ No newline at end of file
diff --git a/tests/src/integration/zipaction/actions/cat/node_modules/cat/index.js b/tests/src/integration/zipaction/actions/cat/node_modules/cat/index.js
new file mode 100644
index 0000000..7ba8889
--- /dev/null
+++ b/tests/src/integration/zipaction/actions/cat/node_modules/cat/index.js
@@ -0,0 +1,54 @@
+var fs = require('fs');
+var parse = require('url').parse;
+
+var catter = function(lib) {
+ var cat = function(url, callback) {
+ if (typeof url === 'string') {
+ url = parse(url);
+ }
+ lib.get({host:url.hostname, port:url.port, path:url.pathname}, function(response) {
+ if (/3\d\d/.test(response.statusCode) && response.headers.location) {
+ cat(parse(response.headers.location), callback);
+ return;
+ }
+ if (!(/2\d\d/).test(response.statusCode)) {
+ callback(new Error('non 2xx status code: ' + response.statusCode));
+ return;
+ }
+ var buffer = '';
+
+ response.setEncoding('utf-8');
+ response.on('data', function(data) {
+ buffer += data;
+ });
+ response.on('close', function() {
+ callback(new Error('unexpected close of response'));
+ });
+ response.on('end', function() {
+ callback(null, buffer);
+ });
+ }).on('error', callback);
+ };
+ return cat;
+};
+
+var http = catter(require('http'));
+var https = catter(require('https'));
+
+module.exports = function(location, callback) {
+ var protocol = (location.match(/^(\w+):\/\//) || [])[1] || 'file';
+
+ if (protocol === 'file') {
+ fs.readFile(location.replace(/^(file:\/\/localhost|file:\/\/)/, ''), 'utf-8', callback);
+ return;
+ }
+ if (protocol === 'http') {
+ http(location, callback);
+ return;
+ }
+ if (protocol === 'https') {
+ https(location, callback);
+ return;
+ }
+ throw new Error('protocol '+protocol+' currently not supported :(');
+};
\ No newline at end of file
diff --git a/tests/src/integration/zipaction/actions/cat/node_modules/cat/package.json b/tests/src/integration/zipaction/actions/cat/node_modules/cat/package.json
new file mode 100644
index 0000000..355bc25
--- /dev/null
+++ b/tests/src/integration/zipaction/actions/cat/node_modules/cat/package.json
@@ -0,0 +1,74 @@
+{
+ "_args": [
+ [
+ {
+ "raw": "cat@0.2.0",
+ "scope": null,
+ "escapedName": "cat",
+ "name": "cat",
+ "rawSpec": "0.2.0",
+ "spec": "0.2.0",
+ "type": "version"
+ },
+ "/Users/dliu/go_work/src/github.com/apache/incubator-openwhisk-wskdeploy/tests/src/integration/zipaction/actions/cat"
+ ]
+ ],
+ "_from": "cat@0.2.0",
+ "_id": "cat@0.2.0",
+ "_inCache": true,
+ "_location": "/cat",
+ "_npmUser": {
+ "name": "mafintosh",
+ "email": "mathiasbuus@gmail.com"
+ },
+ "_npmVersion": "1.4.9",
+ "_phantomChildren": {},
+ "_requested": {
+ "raw": "cat@0.2.0",
+ "scope": null,
+ "escapedName": "cat",
+ "name": "cat",
+ "rawSpec": "0.2.0",
+ "spec": "0.2.0",
+ "type": "version"
+ },
+ "_requiredBy": [
+ "/"
+ ],
+ "_resolved": "https://registry.npmjs.org/cat/-/cat-0.2.0.tgz",
+ "_shasum": "fd850cda7d4162e6904f33b7fcf743b1243fd434",
+ "_shrinkwrap": null,
+ "_spec": "cat@0.2.0",
+ "_where": "/Users/dliu/go_work/src/github.com/apache/incubator-openwhisk-wskdeploy/tests/src/integration/zipaction/actions/cat",
+ "author": {
+ "name": "Mathias Buus Madsen",
+ "email": "mathiasbuus@gmail.com"
+ },
+ "bin": {
+ "cat": "bin.js"
+ },
+ "dependencies": {},
+ "description": "cat will read the contents of an url",
+ "devDependencies": {},
+ "directories": {},
+ "dist": {
+ "shasum": "fd850cda7d4162e6904f33b7fcf743b1243fd434",
+ "tarball": "https://registry.npmjs.org/cat/-/cat-0.2.0.tgz"
+ },
+ "homepage": "https://github.com/mafintosh/cat",
+ "keywords": [
+ "cat",
+ "util",
+ "request"
+ ],
+ "maintainers": [
+ {
+ "name": "mafintosh",
+ "email": "m@ge.tt"
+ }
+ ],
+ "name": "cat",
+ "optionalDependencies": {},
+ "readme": "ERROR: No README data found!",
+ "version": "0.2.0"
+}
diff --git a/tests/src/integration/zipaction/actions/cat/package.json b/tests/src/integration/zipaction/actions/cat/package.json
new file mode 100644
index 0000000..6328214
--- /dev/null
+++ b/tests/src/integration/zipaction/actions/cat/package.json
@@ -0,0 +1,7 @@
+{
+ "name": "my-action",
+ "main": "index.js",
+ "dependencies" : {
+ "cat" : "0.2.0"
+ }
+}
diff --git a/tests/src/integration/zipaction/deployment.yml b/tests/src/integration/zipaction/deployment.yml
new file mode 100644
index 0000000..eda1f1d
--- /dev/null
+++ b/tests/src/integration/zipaction/deployment.yml
@@ -0,0 +1,15 @@
+application:
+ name: wskdeploy-samples
+ namespace: guest
+ credential: 23bc46b1-71f6-4ed5-8c54-816aa4f8c502:123zO3xZCLrMN6v2BKK1dXYFpXlPkccOFqm12CdAsMgRU4VrNZ9lyGVCGuMDGIwP
+ baseUrl: https://172.17.0.1/api
+
+ package:
+ triggerrule:
+ name: cat
+ namespace: guest
+ actions:
+ greeting:
+ inputs:
+ name: Bernie
+ place: Vermont
diff --git a/tests/src/integration/zipaction/manifest.yml b/tests/src/integration/zipaction/manifest.yml
new file mode 100644
index 0000000..72009eb
--- /dev/null
+++ b/tests/src/integration/zipaction/manifest.yml
@@ -0,0 +1,15 @@
+package:
+ name: zipaction
+ version: 1.0
+ license: Apache-2.0
+ actions:
+ cat:
+ version: 1.0
+ location: actions/cat
+ runtime: nodejs:6
+ inputs:
+ name: string
+ place: string
+ outputs:
+ payload: string
+
diff --git a/tests/src/integration/zipaction/src/index.js b/tests/src/integration/zipaction/src/index.js
deleted file mode 100644
index ad8a3ca..0000000
--- a/tests/src/integration/zipaction/src/index.js
+++ /dev/null
@@ -1,5 +0,0 @@
-function helloworld(params) {
- return { payload: 'Hello World!' };
-}
-
-exports.main = helloworld;
diff --git a/tests/src/integration/zipaction/zipaction_test.go b/tests/src/integration/zipaction/zipaction_test.go
index bf2fb23..d2b7463 100644
--- a/tests/src/integration/zipaction/zipaction_test.go
+++ b/tests/src/integration/zipaction/zipaction_test.go
@@ -12,6 +12,7 @@ import (
var wskprops = common.GetWskprops()
+
func TestZipAction(t *testing.T) {
os.Setenv("__OW_API_HOST", wskprops.APIHost)
wskdeploy := common.NewWskdeploy()
@@ -22,6 +23,6 @@ func TestZipAction(t *testing.T) {
}
var (
- manifestPath = os.Getenv("GOPATH") + "/src/github.com/apache/incubator-openwhisk-wskdeploy/tests/src/integration/zipaction/manifest.yaml"
- deploymentPath = ""
+ manifestPath = os.Getenv("GOPATH") + "/src/github.com/apache/incubator-openwhisk-wskdeploy/tests/src/integration/zipaction/manifest.yml"
+ deploymentPath = os.Getenv("GOPATH") + "/src/github.com/apache/incubator-openwhisk-wskdeploy/tests/src/integration/zipaction/deployment.yml"
)
diff --git a/utils/misc.go b/utils/misc.go
index e0c716c..44ea6db 100644
--- a/utils/misc.go
+++ b/utils/misc.go
@@ -156,96 +156,59 @@ func GetJSONType(j interface{}) string {
return kindToJSON[reflect.TypeOf(j).Kind()]
}
-// zip whole folder to a zip file
-func CreateFolderZip(src, des string) error {
- zippedFile, err := os.Create(des)
- Check(err)
- defer zippedFile.Close()
-
- zipWritter := zip.NewWriter(zippedFile)
- defer zipWritter.Close()
-
- sinfo, err := os.Stat(src)
- Check(err)
-
- var basedir string
- if sinfo.IsDir() {
- basedir = filepath.Base(src)
- }
-
- filepath.Walk(src, func(path string, finfo os.FileInfo, err error) error {
- Check(err)
-
- header, err := zip.FileInfoHeader(finfo)
- Check(err)
-
- if basedir != "" {
- header.Name = filepath.Join(basedir, strings.TrimPrefix(path, src))
- }
-
- if finfo.IsDir() {
- header.Name += "/"
- } else {
- header.Method = zip.Deflate
- }
-
- writer, err := zipWritter.CreateHeader(header)
- Check(err)
-
- file, err := os.Open(path)
- Check(err)
- defer file.Close()
- _, err = io.Copy(writer, file)
- Check(err)
- return err
- })
+func NewZipWritter(src, des string) *ZipWritter {
+ zw := &ZipWritter{src: src, des: des}
+ return zw
+}
- return err
+type ZipWritter struct {
+ src string
+ des string
+ zipWritter *zip.Writer
}
-// zip given files to a zip file.
-func CreateFilesZip(filename string, files []string) error {
- file, err := os.Create(filename)
+func (zw *ZipWritter) zipFile(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
- defer file.Close()
- zipwriter := zip.NewWriter(file)
- defer zipwriter.Close()
- for _, name := range files {
- if err := writeFileToZip(zipwriter, name); err != nil {
- return err
- }
+ if !f.Mode().IsRegular() || f.Size() == 0 {
+ return nil
}
- return nil
-}
-
-func writeFileToZip(zipwriter *zip.Writer, filename string) error {
- file, err := os.Open(filename)
+ file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
- finfo, err := file.Stat()
+
+ fileName := strings.TrimPrefix(path, zw.src+"/")
+ wr, err := zw.zipWritter.Create(fileName)
if err != nil {
return err
}
- header, err := zip.FileInfoHeader(finfo)
+
+ _, err = io.Copy(wr, file)
if err != nil {
return err
}
- //add some filter logic if necessary
- //filter(file)
- writer, err := zipwriter.CreateHeader(header)
+ return nil
+}
+
+func (zw *ZipWritter) Zip() error {
+ // create zip file
+ zipFile, err := os.Create(zw.des)
+ if err != nil {
+ return err
+ }
+ defer zipFile.Close()
+ zw.zipWritter = zip.NewWriter(zipFile)
+ err = filepath.Walk(zw.src, zw.zipFile)
+ if err != nil {
+ return nil
+ }
+ err = zw.zipWritter.Close()
if err != nil {
return err
}
- _, err = io.Copy(writer, file)
- return err
-}
-
-func filter(filename string) interface{} {
- //To do
return nil
}
--
To stop receiving notification emails like this one, please contact
['"commits@openwhisk.apache.org" <co...@openwhisk.apache.org>'].