-
Notifications
You must be signed in to change notification settings - Fork 82
/
config_test.go
170 lines (132 loc) · 3.61 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package linodego
import (
"fmt"
"os"
"testing"
)
func TestConfig_LoadWithDefaults(t *testing.T) {
client := NewClient(nil)
file := createTestConfig(t, configLoadWithDefault)
err := client.LoadConfig(&LoadConfigOptions{
Path: file.Name(),
})
if err != nil {
t.Fatal(err)
}
if len(client.configProfiles) != 1 {
fmt.Println(client.configProfiles)
t.Fatalf("mismatched profile count: %d != %d", len(client.configProfiles), 1)
}
p, ok := client.configProfiles["default"]
if !ok {
t.Fatalf("default profile does not exist")
}
if p.APIToken != "blah" {
t.Fatalf("mismatched api token: %s != %s", p.APIToken, "blah")
}
if p.APIURL != "api.cool.linode.com" {
t.Fatalf("mismatched api url: %s != %s", p.APIURL, "api.cool.linode.com")
}
if p.APIVersion != "v4beta" {
t.Fatalf("mismatched api version: %s != %s", p.APIVersion, "v4beta")
}
expectedURL := "https://api.cool.linode.com/v4beta"
if client.resty.BaseURL != expectedURL {
t.Fatalf("mismatched host url: %s != %s", client.resty.BaseURL, expectedURL)
}
if client.resty.Header.Get("Authorization") != "Bearer "+p.APIToken {
t.Fatalf("token not found in auth header: %s", p.APIToken)
}
}
func TestConfig_OverrideDefaults(t *testing.T) {
client := NewClient(nil)
file := createTestConfig(t, configOverrideDefaults)
err := client.LoadConfig(&LoadConfigOptions{
Path: file.Name(),
Profile: "cool",
})
if err != nil {
t.Fatal(err)
}
if len(client.configProfiles) != 2 {
fmt.Println(client.configProfiles)
t.Fatalf("mismatched profile count: %d != %d", len(client.configProfiles), 2)
}
p, ok := client.configProfiles["cool"]
if !ok {
t.Fatalf("cool profile does not exist")
}
if p.APIToken != "blah" {
t.Fatalf("mismatched api token: %s != %s", p.APIToken, "blah")
}
if p.APIURL != "api.cool.linode.com" {
t.Fatalf("mismatched api url: %s != %s", p.APIURL, "api.cool.linode.com")
}
if p.APIVersion != "v4" {
t.Fatalf("mismatched api version: %s != %s", p.APIVersion, "v4")
}
expectedURL := "https://api.cool.linode.com/v4"
if client.resty.BaseURL != expectedURL {
t.Fatalf("mismatched host url: %s != %s", client.resty.BaseURL, expectedURL)
}
if client.resty.Header.Get("Authorization") != "Bearer "+p.APIToken {
t.Fatalf("token not found in auth header: %s", p.APIToken)
}
}
func TestConfig_NoDefaults(t *testing.T) {
client := NewClient(nil)
file := createTestConfig(t, configNoDefaults)
err := client.LoadConfig(&LoadConfigOptions{
Path: file.Name(),
Profile: "cool",
})
if err != nil {
t.Fatal(err)
}
if len(client.configProfiles) != 2 {
fmt.Println(client.configProfiles)
t.Fatalf("mismatched profile count: %d != %d", len(client.configProfiles), 2)
}
p, ok := client.configProfiles["cool"]
if !ok {
t.Fatalf("cool profile does not exist")
}
if p.APIToken != "mytoken" {
t.Fatalf("mismatched api token: %s != %s", p.APIToken, "mytoken")
}
if client.resty.Header.Get("Authorization") != "Bearer "+p.APIToken {
t.Fatalf("token not found in auth header: %s", p.APIToken)
}
}
func createTestConfig(t *testing.T, conf string) *os.File {
file, err := os.CreateTemp("", "linode")
if err != nil {
t.Fatal(err)
}
fmt.Fprint(file, conf)
t.Cleanup(func() {
file.Close()
os.Remove(file.Name())
})
return file
}
const configLoadWithDefault = `
[default]
token = blah
api_url = api.cool.linode.com
api_version = v4beta
`
const configOverrideDefaults = `
[default]
token = blah
api_url = api.cool.linode.com
api_version = v4beta
[cool]
api_version = v4
# Values from default are inherited here
`
const configNoDefaults = `
[cool]
token = mytoken
# Linodego default values are inherited here
`