support config consul by env 35/58535/1
authorLvbo163 <lv.bo163@zte.com.cn>
Thu, 2 Aug 2018 01:42:01 +0000 (09:42 +0800)
committerLvbo163 <lv.bo163@zte.com.cn>
Thu, 2 Aug 2018 01:42:01 +0000 (09:42 +0800)
add env variable 'ConsulAddress' to config consul

Issue-ID: MSB-247

Change-Id: I37770d098751fa448199d692166081584106bf3a
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
msb2pilot/src/msb2pilot/consul/controller.go
msb2pilot/src/msb2pilot/consul/controller_test.go
msb2pilot/src/msb2pilot/models/config.go [new file with mode: 0644]

index 1ba2cd8..b6e798f 100644 (file)
@@ -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)
index 1b05653..a6569bd 100644 (file)
 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 (file)
index 0000000..7f34514
--- /dev/null
@@ -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
+)