add ut for consul config info
[msb/service-mesh.git] / msb2pilot / src / msb2pilot / log / log_test.go
1 /**
2  * Copyright (c) 2018 ZTE Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * and the Apache License 2.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     ZTE - initial Project
11  */
12 package log
13
14 import (
15         "msb2pilot/util"
16         "os"
17         "strings"
18         "testing"
19 )
20
21 func TestCheckLogDir(t *testing.T) {
22         cases := []struct {
23                 path  string
24                 exist bool
25         }{
26                 {
27                         path:  ``,
28                         exist: false,
29                 },
30                 {
31                         path:  `test.log`,
32                         exist: false,
33                 },
34                 {
35                         path:  `.` + util.PathSep + `test` + util.PathSep + `test.log`,
36                         exist: true,
37                 },
38         }
39
40         for _, cas := range cases {
41                 checkLogDir(cas.path)
42
43                 index := strings.LastIndex(cas.path, util.PathSep)
44                 if cas.exist && !util.FileExists(cas.path[0:index]) {
45                         t.Errorf("checkLogDir() => dir not exist, want %s", cas.path)
46                 }
47         }
48
49         // clear
50         os.RemoveAll("test")
51 }
52
53 func TestLoadCustom(t *testing.T) {
54         cases := []struct {
55                 path string
56                 want string
57         }{
58                 {
59                         path: `..` + util.PathSep + "conf" + util.PathSep + cfgFileName,
60                         want: "success",
61                 },
62                 {
63                         path: ``,
64                         want: "read file error",
65                 },
66                 {
67                         path: `log_test.go`,
68                         want: "parse config file error",
69                 },
70         }
71
72         for _, cas := range cases {
73                 res := loadCustom(cas.path)
74
75                 if (res == nil && cas.want == "success") || (res != nil && cas.want != "success") {
76                         t.Errorf("loadCustom() => want %s, got %v", cas.want, res)
77                 }
78         }
79 }