-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig_test.go
116 lines (106 loc) · 3.38 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
package main
import (
"errors"
"io/ioutil"
"os"
"testing"
)
const validConfigFileContent = `[f5]
auth_method = "basic"
url = "http://localhost/bigip"
user = "admin"
password = "admin"
ssl_check = false
`
const invalidConfigFileContent = `{invalid}`
func createTempConfigFile(data string) (*os.File, error) {
f, err := ioutil.TempFile(os.TempDir(), "f5-auto-uploader-test")
if err != nil {
return nil, errors.New("cannot create temporary configuration file")
}
if _, err := f.Write([]byte(data)); err != nil {
f.Close()
return nil, errors.New("cannot write configuration in temporary configuration file")
}
return f, nil
}
func TestReadConfig(t *testing.T) {
// Setup temporary configuration files and make sure they will be deleted
// at the end of this test.
validFile, err := createTempConfigFile(validConfigFileContent)
if err != nil {
t.Fatal("setup: ", err)
}
defer os.Remove(validFile.Name())
defer validFile.Close()
invalidFile, err := createTempConfigFile(invalidConfigFileContent)
if err != nil {
t.Fatal("setup: ", err)
}
defer os.Remove(invalidFile.Name())
defer invalidFile.Close()
// Run subtests
t.Run("Happy Path", func(t *testing.T) { testReadConfigHappyPath(t, validFile) })
t.Run("Fail Open", testReadConfigFailOpen)
t.Run("Fail Decode", func(t *testing.T) { testReadConfigFailDecode(t, invalidFile) })
}
func testReadConfigHappyPath(t *testing.T, validFile *os.File) {
path := validFile.Name()
cfg, err := readConfig(path)
if err != nil {
t.Fatalf("readConfig(%q): unexpected error %q", path, err.Error())
}
want := config{}
want.F5 = f5Config{
AuthMethod: "basic",
URL: "http://localhost/bigip",
User: "admin",
Password: "admin",
}
if got := cfg.F5.AuthMethod; got != want.F5.AuthMethod {
t.Errorf("readConfig(%q): got auth_method %q; want %q",
path, got, want.F5.AuthMethod)
}
if got := cfg.F5.URL; got != want.F5.URL {
t.Errorf("readConfig(%q): got url %q; want %q",
path, got, want.F5.URL)
}
if got := cfg.F5.User; got != want.F5.User {
t.Errorf("readConfig(%q): got user %q; want %q",
path, got, want.F5.User)
}
if got := cfg.F5.Password; got != want.F5.Password {
t.Errorf("readConfig(%q): got password %q; want %q",
path, got, want.F5.Password)
}
if got := cfg.F5.SSLCheck; got != want.F5.SSLCheck {
t.Errorf("readConfig(%q): got password %v; want %v",
path, got, want.F5.SSLCheck)
}
if got := cfg.F5.LoginProviderName; got != want.F5.LoginProviderName {
t.Errorf("readConfig(%q): got password %q; want %q",
path, got, want.F5.LoginProviderName)
}
}
func testReadConfigFailOpen(t *testing.T) {
invalidPath := "some-path-that-does-not-exist"
_, err := readConfig(invalidPath)
if err == nil {
t.Fatalf("readConfig(%q): expected error, got nil", invalidPath)
}
wantErr := "cannot open configuration file: open some-path-that-does-not-exist: no such file or directory"
if err.Error() != wantErr {
t.Errorf("readConfig(%q): got error %q; want %q", invalidPath, err.Error(), wantErr)
}
}
func testReadConfigFailDecode(t *testing.T, invalidFile *os.File) {
path := invalidFile.Name()
_, err := readConfig(path)
if err == nil {
t.Fatalf("readConfig(%q): expected error, got nil", path)
}
wantErr := "cannot read configuration file: Near line 0 (last key parsed ''): bare keys cannot contain '{'"
if err.Error() != wantErr {
t.Errorf("readConfig(%q): got error %q; want %q", path, err.Error(), wantErr)
}
}