From 355d078327c728c626c437f8c882c87897f1d0ab Mon Sep 17 00:00:00 2001 From: Lvbo163 Date: Thu, 2 Aug 2018 09:11:30 +0800 Subject: [PATCH] add ut for consul config info Issue-ID: MSB-246 Change-Id: I21242b6f83785e4d89bb813456ac23c2ead44968 Signed-off-by: Lvbo163 --- msb2pilot/src/msb2pilot/conf/consul.yml | 2 + msb2pilot/src/msb2pilot/consul/controller_test.go | 68 +++++++++++++++++++++++ msb2pilot/src/msb2pilot/log/log_test.go | 10 ++-- msb2pilot/src/msb2pilot/main.go | 2 + msb2pilot/src/msb2pilot/util/common.go | 1 + 5 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 msb2pilot/src/msb2pilot/conf/consul.yml create mode 100644 msb2pilot/src/msb2pilot/consul/controller_test.go diff --git a/msb2pilot/src/msb2pilot/conf/consul.yml b/msb2pilot/src/msb2pilot/conf/consul.yml new file mode 100644 index 0000000..3c30cba --- /dev/null +++ b/msb2pilot/src/msb2pilot/conf/consul.yml @@ -0,0 +1,2 @@ +# consul api address, default to be http://localhost:8500 +address: http://127.0.0.1:8500 diff --git a/msb2pilot/src/msb2pilot/consul/controller_test.go b/msb2pilot/src/msb2pilot/consul/controller_test.go new file mode 100644 index 0000000..1b05653 --- /dev/null +++ b/msb2pilot/src/msb2pilot/consul/controller_test.go @@ -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) + } + } +} diff --git a/msb2pilot/src/msb2pilot/log/log_test.go b/msb2pilot/src/msb2pilot/log/log_test.go index 816e10f..9ed192a 100644 --- a/msb2pilot/src/msb2pilot/log/log_test.go +++ b/msb2pilot/src/msb2pilot/log/log_test.go @@ -12,14 +12,13 @@ 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", }, { diff --git a/msb2pilot/src/msb2pilot/main.go b/msb2pilot/src/msb2pilot/main.go index c19000a..236e369 100644 --- a/msb2pilot/src/msb2pilot/main.go +++ b/msb2pilot/src/msb2pilot/main.go @@ -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() } diff --git a/msb2pilot/src/msb2pilot/util/common.go b/msb2pilot/src/msb2pilot/util/common.go index 1bb10df..d46bb6f 100644 --- a/msb2pilot/src/msb2pilot/util/common.go +++ b/msb2pilot/src/msb2pilot/util/common.go @@ -19,6 +19,7 @@ import ( const ( ConfigPath = "conf" + PathSep = string(os.PathSeparator) ) func GetCfgPath() string { -- 2.16.6