-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
116 lines (103 loc) · 3.36 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright (C) 2017 eschao <esc.chao@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package config
import (
"os"
"path/filepath"
"runtime"
"strconv"
"testing"
"github.com/eschao/config/test"
"github.com/stretchr/testify/assert"
)
const (
LOGIN_USER = "test-login-user"
LOGIN_PASSWORD = "test-login-passwd"
SERVICE_HOST = "test-service-host"
SERVICE_PORT = 8080
SERVICE_LOG_PATH = "/var/log/service"
SERVICE_LOG_LEVEL = "debug"
DB_HOST = "test-db-host"
DB_PORT = 9090
DB_USER = "test-db-user"
DB_PASSWORD = "test-db-password"
DB_LOG_PATH = "/var/log/db"
DB_LOG_LEVEL = "error"
)
func TestDefaultValueConfig(t *testing.T) {
conf := test.DefValueConfig{}
assert := assert.New(t)
assert.NoError(ParseDefault(&conf))
assert.Equal(true, conf.BoolValue)
assert.Equal(123, conf.IntValue)
assert.Equal(float64(123.4567), conf.Float64Value)
assert.Equal("default-string", conf.StrValue)
assert.Equal(3, len(conf.SliceValue))
assert.Equal("xx", conf.SliceValue[0])
assert.Equal("yy", conf.SliceValue[1])
assert.Equal("zz", conf.SliceValue[2])
assert.Equal("", conf.NoDefValue)
}
func TestEnvConfig(t *testing.T) {
dbLogPrefix := "LOG_"
os.Setenv("HOST", DB_HOST)
os.Setenv("PORT", strconv.Itoa(DB_PORT))
os.Setenv("USER", DB_USER)
os.Setenv("PASSWORD", DB_PASSWORD)
os.Setenv(dbLogPrefix+"PATH", DB_LOG_PATH)
os.Setenv(dbLogPrefix+"LEVEL", DB_LOG_LEVEL)
defer os.Unsetenv("HOST")
defer os.Unsetenv("PORT")
defer os.Unsetenv("USER")
defer os.Unsetenv("PASSWORD")
defer os.Unsetenv(dbLogPrefix + "PATH")
defer os.Unsetenv(dbLogPrefix + "LEVEL")
conf := test.DBConfig{}
assert := assert.New(t)
assert.NoError(ParseEnv(&conf))
assert.Equal(DB_HOST, conf.Host)
assert.Equal(DB_PORT, conf.Port)
assert.Equal(DB_USER, conf.User)
assert.Equal(DB_PASSWORD, conf.Password)
assert.Equal(DB_LOG_PATH, conf.Log.Path)
assert.Equal(DB_LOG_LEVEL, conf.Log.Level)
}
func TestJSONConfigFile(t *testing.T) {
_, curTestFile, _, _ := runtime.Caller(0)
path := filepath.Dir(curTestFile)
conf := test.DBConfig{}
assert := assert.New(t)
assert.NoError(ParseConfigFile(&conf, path+"/test/config.json"))
assert.Equal(DB_HOST, conf.Host)
assert.Equal(DB_PORT, conf.Port)
assert.Equal(DB_USER, conf.User)
assert.Equal(DB_PASSWORD, conf.Password)
assert.Equal(DB_LOG_PATH, conf.Log.Path)
assert.Equal(DB_LOG_LEVEL, conf.Log.Level)
}
func TestYamlConfigFile(t *testing.T) {
_, curTestFile, _, _ := runtime.Caller(0)
path := filepath.Dir(curTestFile)
conf := test.DBConfig{}
assert := assert.New(t)
assert.NoError(ParseConfigFile(&conf, path+"/test/config.yaml"))
assert.Equal(DB_HOST, conf.Host)
assert.Equal(DB_PORT, conf.Port)
assert.Equal(DB_USER, conf.User)
assert.Equal(DB_PASSWORD, conf.Password)
assert.Equal(DB_LOG_PATH, conf.Log.Path)
assert.Equal(DB_LOG_LEVEL, conf.Log.Level)
}