From 7c95e1055ac64f55663e26c09cc4cfbecc710e3a Mon Sep 17 00:00:00 2001 From: Vincent Hou Date: Fri, 18 Aug 2017 15:24:13 -0400 Subject: [PATCH] Load the credentials for the test cases --- tests/dat/.whiskClientConfig | 5 + tests/dat/hello.js | 8 + tests/src/integration/credentials_test.go | 74 +++++++++ whisk/client.go | 3 + whisk/wskprops.go | 184 ++++++++++++++++++++++ whisk/wskprops_test.go | 50 ++++++ 6 files changed, 324 insertions(+) create mode 100644 tests/dat/.whiskClientConfig create mode 100644 tests/dat/hello.js create mode 100644 tests/src/integration/credentials_test.go create mode 100644 whisk/wskprops.go create mode 100644 whisk/wskprops_test.go diff --git a/tests/dat/.whiskClientConfig b/tests/dat/.whiskClientConfig new file mode 100644 index 00000000..1c13d0e3 --- /dev/null +++ b/tests/dat/.whiskClientConfig @@ -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 \ No newline at end of file diff --git a/tests/dat/hello.js b/tests/dat/hello.js new file mode 100644 index 00000000..82b3bcfa --- /dev/null +++ b/tests/dat/hello.js @@ -0,0 +1,8 @@ +/** + * Hello, world. + */ +function main(params) { + greeting = 'hello, ' + params.payload + '!' + console.log(greeting); + return {payload: greeting} +} diff --git a/tests/src/integration/credentials_test.go b/tests/src/integration/credentials_test.go new file mode 100644 index 00000000..a15f0bbc --- /dev/null +++ b/tests/src/integration/credentials_test.go @@ -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) +} diff --git a/whisk/client.go b/whisk/client.go index 9ea58239..3b499717 100644 --- a/whisk/client.go +++ b/whisk/client.go @@ -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") diff --git a/whisk/wskprops.go b/whisk/wskprops.go new file mode 100644 index 00000000..5daa90d9 --- /dev/null +++ b/whisk/wskprops.go @@ -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 +} diff --git a/whisk/wskprops_test.go b/whisk/wskprops_test.go new file mode 100644 index 00000000..a3dfbd7a --- /dev/null +++ b/whisk/wskprops_test.go @@ -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") +}