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/09/19 02:31:24 UTC
[incubator-openwhisk-wskdeploy] branch master updated: Add unit
test to fileoperations (#497)
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 594cf57 Add unit test to fileoperations (#497)
594cf57 is described below
commit 594cf57e8db01334173df6de88e2240a06b18fd4
Author: Vincent <sh...@us.ibm.com>
AuthorDate: Mon Sep 18 22:31:22 2017 -0400
Add unit test to fileoperations (#497)
Closes-Bug: #342
---
utils/fileoperations.go | 63 +------------------
utils/fileoperations_test.go | 144 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 145 insertions(+), 62 deletions(-)
diff --git a/utils/fileoperations.go b/utils/fileoperations.go
index 6d5d6ed..3d01ee4 100644
--- a/utils/fileoperations.go
+++ b/utils/fileoperations.go
@@ -19,14 +19,9 @@ package utils
import (
"bufio"
- "errors"
"fmt"
"os"
- "path"
- "path/filepath"
"strings"
-
- "github.com/apache/incubator-openwhisk-client-go/whisk"
)
func MayExists(file string) bool {
@@ -40,7 +35,6 @@ func MayExists(file string) bool {
func FileExists(file string) bool {
_, err := os.Stat(file)
if err != nil {
- err = errors.New("File not found.")
return false
} else {
return true
@@ -70,61 +64,6 @@ func IsDirectory(filePath string) bool {
}
}
-func CreateActionFromFile(manipath, filePath string) (*whisk.Action, error) {
- ext := path.Ext(filePath)
- baseName := path.Base(filePath)
- //check if the file if from local or from web
- //currently only consider http
- islocal := !strings.HasPrefix(filePath, "http")
- name := strings.TrimSuffix(baseName, filepath.Ext(baseName))
- action := new(whisk.Action)
- //better refactor this
- if islocal {
- splitmanipath := strings.Split(manipath, string(os.PathSeparator))
- filePath = strings.TrimRight(manipath, splitmanipath[len(splitmanipath)-1]) + filePath
- }
- // process source code files
- if ext == ".swift" || ext == ".js" || ext == ".py" {
-
- kind := "nodejs:default"
-
- switch ext {
- case ".swift":
- kind = "swift:default"
- case ".js":
- kind = "nodejs:default"
- case ".py":
- kind = "python"
- }
-
- var dat []byte
- var err error
-
- if islocal {
- dat, err = new(ContentReader).LocalReader.ReadLocal(filePath)
- } else {
- dat, err = new(ContentReader).URLReader.ReadUrl(filePath)
- }
-
- if err != nil {
- return action, err
- }
- code := string(dat)
- pub := false
- action.Exec = new(whisk.Exec)
- action.Exec.Code = &code
- action.Exec.Kind = kind
- action.Name = name
- action.Publish = &pub
- return action, nil
- //dat, err := new(ContentReader).URLReader.ReadUrl(filePath)
- //Check(err)
-
- }
- // If the action is not supported, we better to return an error.
- return nil, errors.New("Unsupported action type.")
-}
-
func ReadProps(path string) (map[string]string, error) {
props := map[string]string{}
@@ -133,7 +72,7 @@ func ReadProps(path string) (map[string]string, error) {
if err != nil {
// If file does not exist, just return props
fmt.Printf("Warning: Unable to read whisk properties file '%s' (file open error: %s)\n", path, err)
- return props, nil
+ return props, err
}
defer file.Close()
diff --git a/utils/fileoperations_test.go b/utils/fileoperations_test.go
new file mode 100644
index 0000000..a816a9c
--- /dev/null
+++ b/utils/fileoperations_test.go
@@ -0,0 +1,144 @@
+// +build unit
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package utils
+
+import (
+ "testing"
+ "github.com/stretchr/testify/assert"
+ "os"
+ "io/ioutil"
+ "bufio"
+ "fmt"
+)
+
+func CreateTmpfile(filename string) (f *os.File, err error) {
+ dir, _ := os.Getwd()
+ tmpfile, err := ioutil.TempFile(dir, filename)
+ if err != nil {
+ return nil, err
+ }
+ return tmpfile, nil
+}
+
+func CreateTmpDir(dir string) error {
+ return os.Mkdir(dir, 0777)
+}
+
+func CreateFile(lines []string, path string) error {
+ file, err := os.Create(path)
+ if err != nil {
+ return err
+ }
+ defer file.Close()
+
+ w := bufio.NewWriter(file)
+ for _, line := range lines {
+ fmt.Fprintln(w, line)
+ }
+ return w.Flush()
+}
+
+func TestFileExists(t *testing.T) {
+ testfile := "testFile"
+ tmpFile, err := CreateTmpfile(testfile)
+ assert.Nil(t, err, "Failed to create the temporary file.")
+
+ // The file exists.
+ fileName := tmpFile.Name()
+ result := FileExists(fileName)
+ assert.True(t, result)
+
+ // After the file is removed, the file does not exist.
+ tmpFile.Close()
+ os.Remove(tmpFile.Name())
+ result = FileExists(fileName)
+ assert.False(t, result)
+}
+
+func TestIsDirectory(t *testing.T) {
+ testDir := "testFile"
+ err := CreateTmpDir(testDir)
+ assert.Nil(t, err, "Failed to create the temporary file.")
+
+ // The directory exists.
+ result := IsDirectory(testDir)
+ assert.True(t, result)
+
+ // After the file is removed, the file does not exist.
+ os.Remove(testDir)
+ result = IsDirectory(testDir)
+ assert.False(t, result)
+
+ // If this function is tested against a valid file, we will get false.
+ testfile := "testFile"
+ tmpFile, err := CreateTmpfile(testfile)
+ assert.Nil(t, err, "Failed to create the temporary file.")
+ fileName := tmpFile.Name()
+ result = IsDirectory(fileName)
+ assert.False(t, result)
+ tmpFile.Close()
+ os.Remove(tmpFile.Name())
+
+ // If this function is tested against an invalid path, we will get false.
+ result = IsDirectory("NonExistent")
+ assert.False(t, result)
+}
+
+func TestReadProps(t *testing.T) {
+ testfile := "testWskPropsRead"
+ testKey := "testKey"
+ testValue := "testValue"
+ testKeySec := "testKeySec"
+ testValueSec := "testValueSec"
+ lines := []string{ testKey + "=" + testValue, testKeySec + "=" + testValueSec }
+ CreateFile(lines, testfile)
+ props, err := ReadProps(testfile)
+ assert.Nil(t, err, "Failed to read the test prop file.")
+ assert.Equal(t, testValue, props[testKey])
+ assert.Equal(t, testValueSec, props[testKeySec])
+ err = os.Remove(testfile)
+ assert.Nil(t, err, "Failed to delete the test prop file.")
+
+ // Failed to read wskprops file if it does not exist.
+ props, err = ReadProps(testfile)
+ assert.NotNil(t, err)
+ assert.Equal(t, 0, len(props))
+}
+
+func TestWriteProps(t *testing.T) {
+ testfile := "testWskPropsWrite"
+ testKey := "testKeyWrite"
+ testValue := "testValueWrite"
+ testKeySec := "testKeyWriteSec"
+ testValueSec := "testValueWriteSec"
+ props := map[string]string{
+ testKey: testValue,
+ testKeySec: testValueSec,
+ }
+ err := WriteProps(testfile, props)
+ assert.Nil(t, err, "Failed to write the test prop file.")
+
+ propsResult, error := ReadProps(testfile)
+ assert.Nil(t, error, "Failed to read the test prop file.")
+ assert.Equal(t, testValue, propsResult[testKey])
+ assert.Equal(t, testValueSec, propsResult[testKeySec])
+ err = os.Remove(testfile)
+ assert.Nil(t, err, "Failed to delete the test prop file.")
+}
--
To stop receiving notification emails like this one, please contact
['"commits@openwhisk.apache.org" <co...@openwhisk.apache.org>'].