diff options
author | Huabing Zhao <zhaohuabing@gmail.com> | 2018-08-02 02:08:53 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-08-02 02:08:53 +0000 |
commit | 6220c5f61fc044e1c3e96c8078f989e563113028 (patch) | |
tree | 2931e0fe5bdf5ba2a4c001b66e5bd8cf96ab7cae | |
parent | 7c9c0cf0c06901526b29eebce20982b16463c509 (diff) | |
parent | 933e3c6f0ce1640c7d00d70c929266603136cb3b (diff) |
Merge "support config consul by env"
-rw-r--r-- | msb2pilot/src/msb2pilot/consul/controller.go | 7 | ||||
-rw-r--r-- | msb2pilot/src/msb2pilot/consul/controller_test.go | 23 | ||||
-rw-r--r-- | msb2pilot/src/msb2pilot/models/config.go | 16 |
3 files changed, 45 insertions, 1 deletions
diff --git a/msb2pilot/src/msb2pilot/consul/controller.go b/msb2pilot/src/msb2pilot/consul/controller.go index 1ba2cd8..b6e798f 100644 --- a/msb2pilot/src/msb2pilot/consul/controller.go +++ b/msb2pilot/src/msb2pilot/consul/controller.go @@ -13,7 +13,9 @@ package consul import ( "msb2pilot/log" + "msb2pilot/models" "msb2pilot/util" + "os" "path/filepath" "github.com/hashicorp/consul/api" @@ -44,6 +46,11 @@ func init() { } func getConsulAddress(path string) string { + res := os.Getenv(models.EnvConsulAddress) + if res != "" { + return res + } + cfg, err := loadCfgInfo(path) if err != nil { log.Log.Error("load consul config info error", err) diff --git a/msb2pilot/src/msb2pilot/consul/controller_test.go b/msb2pilot/src/msb2pilot/consul/controller_test.go index 1b05653..a6569bd 100644 --- a/msb2pilot/src/msb2pilot/consul/controller_test.go +++ b/msb2pilot/src/msb2pilot/consul/controller_test.go @@ -12,33 +12,54 @@ package consul import ( + "msb2pilot/models" + "os" "testing" ) func TestSetConsulAddress(t *testing.T) { cases := []struct { - path, want string + env, path, want string }{ { + env: "testEnv", + path: "", + want: `testEnv`, + }, + { + env: "", path: cfgFilePath, want: `http://127.0.0.1:8500`, }, { + env: "testEnvWithPath", + path: cfgFilePath, + want: `testEnvWithPath`, + }, + { + env: "", path: ``, want: `http://localhost:8500`, }, { + env: "", path: `controller.go`, want: `http://localhost:8500`, }, } + oldEnv := os.Getenv(models.EnvConsulAddress) + for _, cas := range cases { + os.Setenv(models.EnvConsulAddress, cas.env) + res := getConsulAddress(cas.path) if res != cas.want { t.Errorf("getConsulAddress() => want %s, got %s", cas.want, res) } } + + os.Setenv(models.EnvConsulAddress, oldEnv) } func TestLoadCfgInfo(t *testing.T) { diff --git a/msb2pilot/src/msb2pilot/models/config.go b/msb2pilot/src/msb2pilot/models/config.go new file mode 100644 index 0000000..7f34514 --- /dev/null +++ b/msb2pilot/src/msb2pilot/models/config.go @@ -0,0 +1,16 @@ +/** + * Copyright (c) 2018 ZTE Corporation. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and the Apache License 2.0 which both accompany this distribution, + * and are available at http://www.eclipse.org/legal/epl-v10.html + * and http://www.apache.org/licenses/LICENSE-2.0 + * + * Contributors: + * ZTE - initial Project + */ +package models + +const ( + EnvConsulAddress = "ConsulAddress" //http://localhost:8500 +) |