-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_test.go
149 lines (130 loc) · 3.03 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package logy
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"gopkg.in/yaml.v3"
"reflect"
"testing"
)
type testConfigFileReader struct {
mock.Mock
}
func (r *testConfigFileReader) ReadFile(name string) (data []byte, err error) {
var (
ok bool
)
args := r.Called(name)
if len(args) == 2 {
data, ok = args[0].([]byte)
if !ok {
data = nil
}
err, ok = args[1].(error)
if !ok {
err = nil
}
}
return
}
var (
mockConfigReader = &testConfigFileReader{}
)
func init() {
configReader = mockConfigReader
}
func TestLoadConfig(t *testing.T) {
LoadConfig(&Config{})
}
func TestLoadConfigFromYaml(t *testing.T) {
testConfig := &Config{
Level: LevelAll,
IncludeCaller: true,
Handlers: Handlers{"testHandler", "console", "file"},
Console: &ConsoleConfig{
Enabled: false,
Target: TargetStderr,
Format: "%d %s%e%n",
Color: true,
Level: LevelAll,
Json: &JsonConfig{
Enabled: true,
ExcludedKeys: ExcludedKeys{
"timestamp", "anyKey",
},
KeyOverrides: KeyOverrides{
"timestamp": "@timestamp",
},
AdditionalFields: AdditionalFields{
"application": "any_application",
},
},
},
File: &FileConfig{
Name: "anyFileName",
Enabled: false,
Path: "anyFilePath",
Format: "%d %s%e%n",
Level: LevelWarn,
Json: &JsonConfig{
Enabled: true,
ExcludedKeys: ExcludedKeys{
"timestamp", "anyKey",
},
KeyOverrides: KeyOverrides{
"timestamp": "@timestamp",
},
AdditionalFields: AdditionalFields{
"application": "any_application",
},
},
},
Syslog: &SyslogConfig{
Enabled: false,
Endpoint: "anyEndpoint",
AppName: "anyAppName",
Hostname: "anyHostname",
Facility: FacilityLogAlert,
LogType: RFC5424,
Protocol: ProtocolUDP,
Format: "%d %s%e%n",
Level: LevelInfo,
BlockOnReconnect: false,
},
Package: map[string]*PackageConfig{
"anyPackage": {
Level: LevelAll,
UseParentHandlers: true,
Handlers: Handlers{"file"},
},
},
ExternalHandlers: map[string]ConfigProperties{
"external_handler": {
"anyPropertyKey": "anyPropertyValue",
},
},
}
configData, _ := yaml.Marshal(testConfig)
testConfigMap := make(map[string]interface{})
yaml.Unmarshal(configData, &testConfigMap)
logyMap := make(map[string]interface{})
testConfigMap["logy"] = logyMap
logyMap["external_handler"] = ConfigProperties{
"anyPropertyKey": "anyPropertyValue",
}
for key, value := range testConfigMap {
if key == "logy" {
continue
}
if logyField, ok := testConfigMap["logy"]; ok {
subMap := logyField.(map[string]interface{})
subMap[key] = value
}
delete(testConfigMap, key)
}
configData, _ = yaml.Marshal(testConfigMap)
mockConfigReader.On("ReadFile", "anyFileName").Return(configData, nil)
err := LoadConfigFromYaml("anyFileName")
assert.Nil(t, err)
same := reflect.DeepEqual(testConfig, config)
assert.True(t, same)
}