Skip to content

Commit

Permalink
Load the credentials for the test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Hou committed Aug 21, 2017
1 parent 2117d60 commit 7c95e10
Show file tree
Hide file tree
Showing 6 changed files with 324 additions and 0 deletions.
5 changes: 5 additions & 0 deletions tests/dat/.whiskClientConfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__OW_API_HOST=https://192.168.99.100:443/api
__OW_NAMESPACE=LOACL_OW_NAMESPACE
__OW_API_KEY=LOACL_OW_API_KEY
__OW_APIGW_TOKEN=LOACL_OW_APIGW_TOKEN
__OW_APIGW_SPACE_SUID=LOACL_OW_APIGW_SPACE_SUID
8 changes: 8 additions & 0 deletions tests/dat/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Hello, world.
*/
function main(params) {
greeting = 'hello, ' + params.payload + '!'
console.log(greeting);
return {payload: greeting}
}
74 changes: 74 additions & 0 deletions tests/src/integration/credentials_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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 tests

import (
"github.com/apache/incubator-openwhisk-client-go/whisk"
//"github.com/stretchr/testify/assert"
"testing"
"fmt"
"os"
"net/http"
"io/ioutil"
)

// TODO: write the integration against openwhisk
func TestCredentials(t *testing.T) {
config := whisk.GetDefaultConfig()
client, err := whisk.NewClient(http.DefaultClient, nil)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
options := &whisk.ActionListOptions{
Limit: 30,
Skip: 30,
}

action := new(whisk.Action)
action.Name = "testAction"
action.Namespace = config.Namespace
exec := new(whisk.Exec)

file, err := ioutil.ReadFile(os.Getenv("GOPATH") + "/src/github.com/apache/incubator-openwhisk-client-go/tests/dat/hello.js")
if err != nil{
fmt.Println("Failed to read the file.")
}
str := string(file)
fmt.Println(str)
exec.Code = &str
exec.Kind = "nodejs:default"
action.Exec = exec

action_rec, resp, err := client.Actions.Insert(action, true)

fmt.Println("Returned with status: ", resp)
fmt.Println("Returned action: \n%+v", action_rec)
actions, resp, err := client.Actions.List("", options)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}

fmt.Println("Returned with status: ", resp.Status)
fmt.Println("Returned actions: \n%+v", actions)

resp, err = client.Actions.Delete(action.Name)

fmt.Println("Returned with status: ", resp.Status)
}
3 changes: 3 additions & 0 deletions whisk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ type Config struct {

func NewClient(httpClient *http.Client, config *Config) (*Client, error) {

if config == nil {
config = GetDefaultConfig()
}
// Disable certificate checking in the dev environment if in insecure mode
if config.Insecure {
Debug(DbgInfo, "Disabling certificate checking.\n")
Expand Down
184 changes: 184 additions & 0 deletions whisk/wskprops.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* 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 whisk

import (
"github.com/spf13/viper"
"io/ioutil"
"os"
"fmt"
"net/url"
)

const (
OPENWHISK_HOME = "OPENWHISK_HOME"
GOPATH = "GOPATH"
DEFAULT_LOCAL_CONFIG = ".whiskClientConfig"
OW_API_HOST = "__OW_API_HOST"
OW_NAMESPACE = "__OW_NAMESPACE"
OW_API_KEY = "__OW_API_KEY"
OW_APIGW_TOKEN = "__OW_APIGW_TOKEN"
OW_APIGW_SPACE_SUID = "__OW_APIGW_SPACE_SUID"
OPENWHISK_PROPERTIES = "whisk"
TEST_AUTH_FILE = "testing.auth"
OPENWHISK_PRO= "whisk.api.host.proto"
OPENWHISK_PORT= "whisk.api.host.port"
OPENWHISK_HOST= "whisk.api.host.name"
DEFAULT_VERSION = "v1"
DEFAULT_API = "api"
)

type Wskprops struct {
APIHost string
Host string
AuthKey string
Namespace string
AuthAPIGWKey string
APIGWSpaceSuid string
}

func GetDefaultConfig() *Config {
var config Config
dep := GetDefaultWskProp()

var err error
println("check the conf failed " + dep.APIHost)
config.BaseURL, err = url.Parse(dep.APIHost)
if err != nil {
println("check the conf failed ")
config.BaseURL = nil
}
config.Namespace = dep.Namespace
config.Cert = ""
config.Key = ""
config.AuthToken = dep.AuthKey

if config.BaseURL != nil {
config.Host = config.BaseURL.Host
} else {
config.Host = ""
}

config.Version = DEFAULT_VERSION
config.Verbose = false
config.Debug = false
config.Insecure = true

return &config
}

func GetDefaultWskProp() *Wskprops {
var dep *Wskprops
dep = GetWskpropsFromWhisk()
dep = GetWskpropsFromLocalConfig("", dep)
dep = GetWskpropsFromEnv(dep)
return dep
}

func GetWskprops() *Wskprops {
var dep Wskprops
dep.APIHost = ""
dep.AuthKey = ""
dep.Namespace = ""
dep.AuthAPIGWKey = ""
dep.APIGWSpaceSuid = ""
return &dep
}

func GetWskpropsFromEnv(wskprops *Wskprops) *Wskprops {
dep := GetWskprops()

if wskprops != nil {
dep = wskprops
}

dep.APIHost = getEnv(OW_API_HOST, dep.APIHost)
dep.AuthKey = getEnv(OW_API_KEY, dep.AuthKey)
dep.Namespace = getEnv(OW_NAMESPACE, dep.Namespace)
dep.AuthKey = getEnv(OW_APIGW_TOKEN, dep.AuthKey)
dep.APIGWSpaceSuid = getEnv(OW_APIGW_SPACE_SUID, dep.APIGWSpaceSuid)

return dep
}

func getEnv(key, defaultValue string) string {
value := os.Getenv(key)
if len(value) == 0 {
return defaultValue
}
return value
}

func getConfig(key, defaultValue string) string {
value := viper.GetString(key)
if len(value) == 0 {
return defaultValue
}
return value
}

func GetWskpropsFromLocalConfig(path string, wskprops *Wskprops) *Wskprops {
dep := GetWskprops()

if wskprops != nil {
dep = wskprops
}

var configPath string
if path != "" {
configPath = path
} else {
configPath = os.Getenv(GOPATH)
}
viper.SetConfigName(DEFAULT_LOCAL_CONFIG)
viper.AddConfigPath(configPath)

err := viper.ReadInConfig()
if err == nil {
dep.APIHost = getConfig(OW_API_HOST, dep.APIHost)
dep.AuthKey = getConfig(OW_API_KEY, dep.AuthKey)
dep.Namespace = getConfig(OW_NAMESPACE, dep.Namespace)
dep.AuthKey = getConfig(OW_APIGW_TOKEN, dep.AuthKey)
dep.APIGWSpaceSuid = getConfig(OW_APIGW_SPACE_SUID, dep.APIGWSpaceSuid)
}
return dep
}

func GetWskpropsFromWhisk() *Wskprops {
dep := GetWskprops()

viper.SetConfigName(OPENWHISK_PROPERTIES)
viper.AddConfigPath(os.Getenv(OPENWHISK_HOME))

err := viper.ReadInConfig()
if err == nil {
authPath := viper.GetString(TEST_AUTH_FILE)

b, err := ioutil.ReadFile(authPath)
if err == nil {
dep.AuthKey = string(b)
}

var pro = viper.GetString(OPENWHISK_PRO)
var port = viper.GetString(OPENWHISK_PORT)
var host = viper.GetString(OPENWHISK_HOST)
dep.APIHost = fmt.Sprintf("%s://%s:%s/%s", pro, host, port, DEFAULT_API)
dep.Namespace = "_"
}
return dep
}
50 changes: 50 additions & 0 deletions whisk/wskprops_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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 whisk

import (
"testing"
)

func TestGetDefaultConfig(t *testing.T) {
var wskprops = GetDefaultWskProp()
var conf = GetDefaultConfig()
println("the key and api is ")
println(wskprops.AuthKey)
println(wskprops.APIHost)
println(wskprops.Namespace)

println("check the conf ")
println(conf.BaseURL.String())
println(conf.Host)
//println(os.Getenv("OPENWHISK_HOME1") | "OK")
}

func TestGetWskpropsFromWhisk(t *testing.T) {
var wskprops = GetDefaultWskProp()
var conf = GetDefaultConfig()
println("the key and api is ")
println(wskprops.AuthKey)
println(wskprops.APIHost)
println(wskprops.Namespace)

println("check the conf ")
println(conf.BaseURL.String())
println(conf.Host)
//println(os.Getenv("OPENWHISK_HOME1") | "OK")
}

0 comments on commit 7c95e10

Please sign in to comment.