-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmaxctrl_exporter_test.go
More file actions
209 lines (178 loc) · 5.93 KB
/
maxctrl_exporter_test.go
File metadata and controls
209 lines (178 loc) · 5.93 KB
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
// Copyright 2023 Dylan Northrup [@dylan-tock on github]
// 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 main
import (
"fmt"
"log"
"os"
"testing"
)
// Verify GetEnvVar does what it says on the tin
func TestGetInitializedEnvVar(t *testing.T) {
// Save possibly pre-existing value of env variable for restoration later
envVariableName := "maxctrlExporterTestEnvVariable"
previousValue := os.Getenv(envVariableName)
defer func() { os.Setenv(envVariableName, previousValue) }()
fallback := "fallback value"
envValue := "Test Value"
want := "Test Value"
os.Setenv(envVariableName, envValue)
got := GetEnvVar(envVariableName, fallback)
if want != got {
t.Fatalf("Did not get expected value from env variable. Wanted: '%s'. Got '%s'", want, got)
}
if previousValue == "" {
os.Unsetenv(envVariableName)
} else {
os.Setenv(envVariableName, previousValue)
}
}
func TestGetEmptyEnvVar(t *testing.T) {
// Save possibly pre-existing value of env variable for restoration later
envVariableName := "maxctrlExporterTestEnvVariable"
previousValue := os.Getenv(envVariableName)
defer func() { os.Setenv(envVariableName, previousValue) }()
fallback := "fallback value"
envValue := ""
want := fallback
os.Setenv(envVariableName, envValue)
got := GetEnvVar(envVariableName, fallback)
if want != got {
t.Fatalf("Did not get expected value from env variable. Wanted: '%s'. Got '%s'", want, got)
}
if previousValue == "" {
os.Unsetenv(envVariableName)
} else {
os.Setenv(envVariableName, previousValue)
}
}
func TestGettingConfigFromEnvironment(t *testing.T) {
maxScaleUrl = ""
maxScaleUsername = ""
maxScalePassword = ""
maxScaleExporterPort = ""
maxScaleCACertificate = ""
maxctrlExporterConfigFile = ""
maxScaleTLSInsecureSkipVerify = false
keys := []string{"MAXSCALE_URL", "MAXSCALE_USERNAME", "MAXSCALE_PASSWORD", "MAXSCALE_EXPORTER_PORT",
"MAXSCALE_CA_CERTIFICATE", "MAXCTRL_EXPORTER_CFG_FILE"}
want := map[string]string{
keys[0]: "http://10.10.10.1:8989",
keys[1]: "userMcUserFace",
keys[2]: "secretPassword",
keys[3]: "8080",
keys[4]: "cert.pem",
keys[5]: "exporterConfig.yml",
}
for k, v := range want {
os.Setenv(k, v)
}
os.Setenv("MAXSCALE_TLS_INSECURE_SKIP_VERIFY", "true")
setConfigFromEnvironmentVars()
got := make(map[string]string)
got[keys[0]] = maxScaleUrl
got[keys[1]] = maxScaleUsername
got[keys[2]] = maxScalePassword
got[keys[3]] = maxScaleExporterPort
got[keys[4]] = maxScaleCACertificate
got[keys[5]] = maxctrlExporterConfigFile
for _, k := range keys {
if want[k] != got[k] {
log.Fatalf("Config key '%s' had unexpected value. wanted '%s' and got '%s'", k, want[k], got[k])
}
// Unset these as we test
os.Unsetenv(k)
}
for k, v := range want {
if k == "MAXSCALE_EXPORTER_PORT" || k == "MAXCTRL_EXPORTER_CFG_FILE" {
continue
}
os.Setenv(k, v)
}
for _, k := range keys {
if want[k] != got[k] {
log.Fatalf("Config key '%s' had unexpected value. wanted '%s' and got '%s'", k, want[k], got[k])
}
// Unset these as we test
os.Unsetenv(k)
}
if maxScaleTLSInsecureSkipVerify != true {
log.Fatalf("Config key 'MAXSCALE_TLS_INSECURE_SKIP_VERIFY' had unexpected value. wanted 'true' and got '%v'", maxScaleTLSInsecureSkipVerify)
}
}
// Test parsing config contents
func TestConfigParsing(t *testing.T) {
// Pre-initialize the variables
setConfigFromEnvironmentVars()
keys := []string{"url", "username", "password", "exporter_port", "caCertificate", "tlsInsecureSkipVerify"}
want := map[string]string{
keys[0]: "http://10.10.10.1:8989",
keys[1]: "userMcUserFace",
keys[2]: "secretPassword",
keys[3]: "8080",
keys[4]: "",
keys[5]: "true",
}
contents := ""
for k, v := range want {
contents = fmt.Sprintf("%s%s: %s\n", contents, k, v)
}
// We have to call this to set up proper default values
parseConfigFile([]byte(contents))
got := make(map[string]string)
got[keys[0]] = maxScaleUrl
got[keys[1]] = maxScaleUsername
got[keys[2]] = maxScalePassword
got[keys[3]] = maxScaleExporterPort
got[keys[4]] = maxScaleCACertificate
for _, k := range keys {
if k == "tlsInsecureSkipVerify" {
continue
}
if want[k] != got[k] {
log.Fatalf("Config key '%s' had unexpected value. wanted '%s' and got '%s'", k, want[k], got[k])
}
}
// Test value of maxScaleTLSInsecureSkipVerify
if maxScaleTLSInsecureSkipVerify != true {
log.Fatalf("Config key 'MAXSCALE_TLS_INSECURE_SKIP_VERIFY' had unexpected value. wanted 'true' and got '%v'", maxScaleTLSInsecureSkipVerify)
}
// Redo the test, but with some values missing from the config file
contents = ""
for k, v := range want {
if k == "exporter_port" || k == "caCertificate" || k == "tlsInsecureSkipVerify" {
continue
}
contents = fmt.Sprintf("%s%s: %s\n", contents, k, v)
}
// We have to call this to set up proper default values
setConfigFromEnvironmentVars()
parseConfigFile([]byte(contents))
got[keys[0]] = maxScaleUrl
got[keys[1]] = maxScaleUsername
got[keys[2]] = maxScalePassword
got[keys[3]] = maxScaleExporterPort
got[keys[4]] = maxScaleCACertificate
for _, k := range keys {
if k == "tlsInsecureSkipVerify" {
continue
}
if want[k] != got[k] {
log.Fatalf("Config key '%s' had unexpected value. wanted '%s' and got '%s'", k, want[k], got[k])
}
}
// Test default value of maxScaleTLSInsecureSkipVerify
if maxScaleTLSInsecureSkipVerify != false {
log.Fatalf("Config key 'MAXSCALE_TLS_INSECURE_SKIP_VERIFY' had unexpected value. wanted 'false' and got '%v'", maxScaleTLSInsecureSkipVerify)
}
}