forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_configuration_test.go
351 lines (310 loc) · 10 KB
/
client_configuration_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright (c) 2023 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"
"testing"
)
func TestFindConfigFileFromConnectionParameters(t *testing.T) {
dirs := createTestDirectories(t)
connParameterConfigPath := createFile(t, "conn_parameters_config.json", "random content", dirs.dir)
envConfigPath := createFile(t, "env_var_config.json", "random content", dirs.dir)
t.Setenv(clientConfEnvName, envConfigPath)
createFile(t, defaultConfigName, "random content", dirs.predefinedDir1)
createFile(t, defaultConfigName, "random content", dirs.predefinedDir2)
clientConfigFilePath, err := findClientConfigFilePath(connParameterConfigPath, predefinedTestDirs(dirs))
assertEqualE(t, err, nil)
assertEqualE(t, clientConfigFilePath, connParameterConfigPath, "config file path")
}
func TestFindConfigFileFromEnvVariable(t *testing.T) {
dirs := createTestDirectories(t)
envConfigPath := createFile(t, "env_var_config.json", "random content", dirs.dir)
t.Setenv(clientConfEnvName, envConfigPath)
createFile(t, defaultConfigName, "random content", dirs.predefinedDir1)
createFile(t, defaultConfigName, "random content", dirs.predefinedDir2)
clientConfigFilePath, err := findClientConfigFilePath("", predefinedTestDirs(dirs))
assertEqualE(t, err, nil)
assertEqualE(t, clientConfigFilePath, envConfigPath, "config file path")
}
func TestFindConfigFileFromFirstPredefinedDir(t *testing.T) {
dirs := createTestDirectories(t)
configPath := createFile(t, defaultConfigName, "random content", dirs.predefinedDir1)
createFile(t, defaultConfigName, "random content", dirs.predefinedDir2)
clientConfigFilePath, err := findClientConfigFilePath("", predefinedTestDirs(dirs))
assertEqualE(t, err, nil)
assertEqualE(t, clientConfigFilePath, configPath, "config file path")
}
func TestFindConfigFileFromSubsequentDirectoryIfNotFoundInPreviousOne(t *testing.T) {
dirs := createTestDirectories(t)
createFile(t, "wrong_file_name.json", "random content", dirs.predefinedDir1)
configPath := createFile(t, defaultConfigName, "random content", dirs.predefinedDir2)
clientConfigFilePath, err := findClientConfigFilePath("", predefinedTestDirs(dirs))
assertEqualE(t, err, nil)
assertEqualE(t, clientConfigFilePath, configPath, "config file path")
}
func TestNotFindConfigFileWhenNotDefined(t *testing.T) {
dirs := createTestDirectories(t)
createFile(t, "wrong_file_name.json", "random content", dirs.predefinedDir1)
createFile(t, "wrong_file_name.json", "random content", dirs.predefinedDir2)
clientConfigFilePath, err := findClientConfigFilePath("", predefinedTestDirs(dirs))
assertEqualE(t, err, nil)
assertEqualE(t, clientConfigFilePath, "", "config file path")
}
func TestCreatePredefinedDirs(t *testing.T) {
exeDir, _ := os.Executable()
appDir := filepath.Dir(exeDir)
homeDir, err := os.UserHomeDir()
assertNilF(t, err, "get home dir error")
locations := clientConfigPredefinedDirs()
assertEqualF(t, len(locations), 2, "size")
assertEqualE(t, locations[0], appDir, "driver directory")
assertEqualE(t, locations[1], homeDir, "home directory")
}
func TestGetClientConfig(t *testing.T) {
dir := t.TempDir()
fileName := "config.json"
configContents := createClientConfigContent("INFO", "/some-path/some-directory")
createFile(t, fileName, configContents, dir)
filePath := path.Join(dir, fileName)
clientConfigFilePath, _, err := getClientConfig(filePath)
assertNilF(t, err)
assertNotNilF(t, clientConfigFilePath)
assertEqualE(t, clientConfigFilePath.Common.LogLevel, "INFO", "log level")
assertEqualE(t, clientConfigFilePath.Common.LogPath, "/some-path/some-directory", "log path")
}
func TestNoResultForGetClientConfigWhenNoFileFound(t *testing.T) {
clientConfigFilePath, _, err := getClientConfig("")
assertNilF(t, err)
assertNilF(t, clientConfigFilePath)
}
func TestParseConfiguration(t *testing.T) {
dir := t.TempDir()
testCases := []struct {
testName string
fileName string
fileContents string
expectedLogLevel string
expectedLogPath string
}{
{
testName: "TestWithLogLevelUpperCase",
fileName: "config_1.json",
fileContents: createClientConfigContent("INFO", "/some-path/some-directory"),
expectedLogLevel: "INFO",
expectedLogPath: "/some-path/some-directory",
},
{
testName: "TestWithLogLevelLowerCase",
fileName: "config_2.json",
fileContents: createClientConfigContent("info", "/some-path/some-directory"),
expectedLogLevel: "info",
expectedLogPath: "/some-path/some-directory",
},
{
testName: "TestWithMissingValues",
fileName: "config_3.json",
fileContents: `{
"common": {}
}`,
expectedLogLevel: "",
expectedLogPath: "",
},
}
for _, tc := range testCases {
t.Run(tc.testName, func(t *testing.T) {
fileName := createFile(t, tc.fileName, tc.fileContents, dir)
config, err := parseClientConfiguration(fileName)
assertNilF(t, err, "parse client configuration error")
assertEqualE(t, config.Common.LogLevel, tc.expectedLogLevel, "log level")
assertEqualE(t, config.Common.LogPath, tc.expectedLogPath, "log path")
})
}
}
func TestParseAllLogLevels(t *testing.T) {
dir := t.TempDir()
for _, logLevel := range []string{"OFF", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"} {
t.Run(logLevel, func(t *testing.T) {
fileContents := fmt.Sprintf(`{
"common": {
"log_level" : "%s",
"log_path" : "/some-path/some-directory"
}
}`, logLevel)
fileName := createFile(t, fmt.Sprintf("config_%s.json", logLevel), fileContents, dir)
config, err := parseClientConfiguration(fileName)
assertNilF(t, err, "parse client config error")
assertEqualE(t, config.Common.LogLevel, logLevel, "log level")
})
}
}
func TestParseConfigurationFails(t *testing.T) {
dir := t.TempDir()
testCases := []struct {
testName string
fileName string
FileContents string
expectedErrorMessageToContain string
}{
{
testName: "TestWithWrongLogLevel",
fileName: "config_1.json",
FileContents: createClientConfigContent("something weird", "/some-path/some-directory"),
expectedErrorMessageToContain: "unknown log level",
},
{
testName: "TestWithWrongTypeOfLogLevel",
fileName: "config_2.json",
FileContents: `{
"common": {
"log_level" : 15,
"log_path" : "/some-path/some-directory"
}
}`,
expectedErrorMessageToContain: "ClientConfigCommonProps.common.log_level",
},
{
testName: "TestWithWrongTypeOfLogPath",
fileName: "config_3.json",
FileContents: `{
"common": {
"log_level" : "INFO",
"log_path" : true
}
}`,
expectedErrorMessageToContain: "ClientConfigCommonProps.common.log_path",
},
{
testName: "TestWithoutCommon",
fileName: "config_4.json",
FileContents: "{}",
expectedErrorMessageToContain: "common section in client config not found",
},
}
for _, tc := range testCases {
t.Run(tc.testName, func(t *testing.T) {
fileName := createFile(t, tc.fileName, tc.FileContents, dir)
_, err := parseClientConfiguration(fileName)
assertNotNilF(t, err, "parse client configuration error")
errMessage := fmt.Sprint(err)
expectedPrefix := "parsing client config failed"
assertHasPrefixE(t, errMessage, expectedPrefix, "error message")
assertStringContainsE(t, errMessage, tc.expectedErrorMessageToContain, "error message")
})
}
}
func TestUnknownValues(t *testing.T) {
testCases := []struct {
testName string
inputString string
expectedOutput map[string]string
}{
{
testName: "EmptyCommon",
inputString: `{
"common": {}
}`,
expectedOutput: map[string]string{},
},
{
testName: "CommonMissing",
inputString: `{
}`,
expectedOutput: map[string]string{},
},
{
testName: "UnknownProperty",
inputString: `{
"common": {
"unknown_key": "unknown_value"
}
}`,
expectedOutput: map[string]string{
"unknown_key": "unknown_value",
},
},
{
testName: "KnownAndUnknownProperty",
inputString: `{
"common": {
"lOg_level": "level",
"log_PATH": "path",
"unknown_key": "unknown_value"
}
}`,
expectedOutput: map[string]string{
"unknown_key": "unknown_value",
},
},
{
testName: "KnownProperties",
inputString: `{
"common": {
"log_level": "level",
"log_path": "path"
}
}`,
expectedOutput: map[string]string{},
},
{
testName: "EmptyInput",
inputString: "",
expectedOutput: map[string]string{},
},
}
for _, tc := range testCases {
t.Run(tc.testName, func(t *testing.T) {
inputBytes := []byte(tc.inputString)
result := getUnknownValues(inputBytes)
assertEqualE(t, fmt.Sprint(result), fmt.Sprint(tc.expectedOutput))
})
}
}
func createFile(t *testing.T, fileName string, fileContents string, directory string) string {
fullFileName := path.Join(directory, fileName)
err := os.WriteFile(fullFileName, []byte(fileContents), 0644)
assertNilF(t, err, "create file error")
return fullFileName
}
func createTestDirectories(t *testing.T) struct {
dir string
predefinedDir1 string
predefinedDir2 string
} {
dir := t.TempDir()
predefinedDir1 := path.Join(dir, "dir1")
err := os.Mkdir(predefinedDir1, 0700)
assertNilF(t, err, "predefined dir1 error")
predefinedDir2 := path.Join(dir, "dir2")
err = os.Mkdir(predefinedDir2, 0700)
assertNilF(t, err, "predefined dir2 error")
return struct {
dir string
predefinedDir1 string
predefinedDir2 string
}{
dir: dir,
predefinedDir1: predefinedDir1,
predefinedDir2: predefinedDir2,
}
}
func predefinedTestDirs(dirs struct {
dir string
predefinedDir1 string
predefinedDir2 string
}) []string {
return []string{dirs.predefinedDir1, dirs.predefinedDir2}
}
func createClientConfigContent(logLevel string, logPath string) string {
return fmt.Sprintf(`{
"common": {
"log_level" : "%s",
"log_path" : "%s"
}
}`,
logLevel,
strings.ReplaceAll(logPath, "\\", "\\\\"),
)
}