support config consul by env
[msb/service-mesh.git] / msb2pilot / src / msb2pilot / consul / controller_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 consul
13
14 import (
15         "msb2pilot/models"
16         "os"
17         "testing"
18 )
19
20 func TestSetConsulAddress(t *testing.T) {
21         cases := []struct {
22                 env, path, want string
23         }{
24                 {
25                         env:  "testEnv",
26                         path: "",
27                         want: `testEnv`,
28                 },
29                 {
30                         env:  "",
31                         path: cfgFilePath,
32                         want: `http://127.0.0.1:8500`,
33                 },
34                 {
35                         env:  "testEnvWithPath",
36                         path: cfgFilePath,
37                         want: `testEnvWithPath`,
38                 },
39                 {
40                         env:  "",
41                         path: ``,
42                         want: `http://localhost:8500`,
43                 },
44                 {
45                         env:  "",
46                         path: `controller.go`,
47                         want: `http://localhost:8500`,
48                 },
49         }
50
51         oldEnv := os.Getenv(models.EnvConsulAddress)
52
53         for _, cas := range cases {
54                 os.Setenv(models.EnvConsulAddress, cas.env)
55
56                 res := getConsulAddress(cas.path)
57                 if res != cas.want {
58                         t.Errorf("getConsulAddress() => want %s, got %s", cas.want, res)
59                 }
60         }
61
62         os.Setenv(models.EnvConsulAddress, oldEnv)
63 }
64
65 func TestLoadCfgInfo(t *testing.T) {
66         cases := []struct {
67                 path, status string
68         }{
69                 {
70                         path:   cfgFilePath,
71                         status: `success`,
72                 },
73                 {
74                         path:   ``,
75                         status: `path is empty`,
76                 },
77                 {
78                         path:   `controller.go`,
79                         status: `yaml format error`,
80                 },
81         }
82
83         for _, cas := range cases {
84                 _, err := loadCfgInfo(cas.path)
85                 if (cas.status == "success" && err != nil) || (cas.status != "success" && err == nil) {
86                         t.Errorf("loadCfgInfo() => want %s, got %v", cas.status, err)
87                 }
88         }
89 }