add ut for consul config info 27/58527/1
authorLvbo163 <lv.bo163@zte.com.cn>
Thu, 2 Aug 2018 01:11:30 +0000 (09:11 +0800)
committerLvbo163 <lv.bo163@zte.com.cn>
Thu, 2 Aug 2018 01:11:30 +0000 (09:11 +0800)
Issue-ID: MSB-246

Change-Id: I21242b6f83785e4d89bb813456ac23c2ead44968
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
msb2pilot/src/msb2pilot/conf/consul.yml [new file with mode: 0644]
msb2pilot/src/msb2pilot/consul/controller_test.go [new file with mode: 0644]
msb2pilot/src/msb2pilot/log/log_test.go
msb2pilot/src/msb2pilot/main.go
msb2pilot/src/msb2pilot/util/common.go

diff --git a/msb2pilot/src/msb2pilot/conf/consul.yml b/msb2pilot/src/msb2pilot/conf/consul.yml
new file mode 100644 (file)
index 0000000..3c30cba
--- /dev/null
@@ -0,0 +1,2 @@
+# consul api address, default to be http://localhost:8500\r
+address: http://127.0.0.1:8500\r
diff --git a/msb2pilot/src/msb2pilot/consul/controller_test.go b/msb2pilot/src/msb2pilot/consul/controller_test.go
new file mode 100644 (file)
index 0000000..1b05653
--- /dev/null
@@ -0,0 +1,68 @@
+/**
+ * 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 consul
+
+import (
+       "testing"
+)
+
+func TestSetConsulAddress(t *testing.T) {
+       cases := []struct {
+               path, want string
+       }{
+               {
+                       path: cfgFilePath,
+                       want: `http://127.0.0.1:8500`,
+               },
+               {
+                       path: ``,
+                       want: `http://localhost:8500`,
+               },
+               {
+                       path: `controller.go`,
+                       want: `http://localhost:8500`,
+               },
+       }
+
+       for _, cas := range cases {
+               res := getConsulAddress(cas.path)
+               if res != cas.want {
+                       t.Errorf("getConsulAddress() => want %s, got %s", cas.want, res)
+               }
+       }
+}
+
+func TestLoadCfgInfo(t *testing.T) {
+       cases := []struct {
+               path, status string
+       }{
+               {
+                       path:   cfgFilePath,
+                       status: `success`,
+               },
+               {
+                       path:   ``,
+                       status: `path is empty`,
+               },
+               {
+                       path:   `controller.go`,
+                       status: `yaml format error`,
+               },
+       }
+
+       for _, cas := range cases {
+               _, err := loadCfgInfo(cas.path)
+               if (cas.status == "success" && err != nil) || (cas.status != "success" && err == nil) {
+                       t.Errorf("loadCfgInfo() => want %s, got %v", cas.status, err)
+               }
+       }
+}
index 816e10f..9ed192a 100644 (file)
 package log
 
 import (
-       "apiroute/util"
+       "msb2pilot/util"
        "os"
        "strings"
        "testing"
 )
 
 func TestCheckLogDir(t *testing.T) {
-       pathSep := string(os.PathSeparator)
        cases := []struct {
                path  string
                exist bool
@@ -33,7 +32,7 @@ func TestCheckLogDir(t *testing.T) {
                        exist: false,
                },
                {
-                       path:  `.` + pathSep + `test` + pathSep + `test.log`,
+                       path:  `.` + util.PathSep + `test` + util.PathSep + `test.log`,
                        exist: true,
                },
        }
@@ -41,7 +40,7 @@ func TestCheckLogDir(t *testing.T) {
        for _, cas := range cases {
                checkLogDir(cas.path)
 
-               index := strings.LastIndex(cas.path, pathSep)
+               index := strings.LastIndex(cas.path, util.PathSep)
                if cas.exist && !util.FileExists(cas.path[0:index]) {
                        t.Errorf("checkLogDir() => dir not exist, want %s", cas.path)
                }
@@ -52,13 +51,12 @@ func TestCheckLogDir(t *testing.T) {
 }
 
 func TestLoadCustom(t *testing.T) {
-       pathSep := string(os.PathSeparator)
        cases := []struct {
                path string
                want string
        }{
                {
-                       path: `..` + pathSep + "conf" + pathSep + cfgFileName,
+                       path: `..` + util.PathSep + "conf" + util.PathSep + cfgFileName,
                        want: "success",
                },
                {
index c19000a..236e369 100644 (file)
@@ -12,6 +12,7 @@
 package main
 
 import (
+       _ "msb2pilot/consul"
        "msb2pilot/log"
        _ "msb2pilot/routers"
 
@@ -20,5 +21,6 @@ import (
 
 func main() {
        log.Log.Informational("**************** init msb2pilot ************************")
+
        beego.Run()
 }
index 1bb10df..d46bb6f 100644 (file)
@@ -19,6 +19,7 @@ import (
 
 const (
        ConfigPath = "conf"
+       PathSep    = string(os.PathSeparator)
 )
 
 func GetCfgPath() string {