-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
233 lines (228 loc) · 5.81 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
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
package main
import (
"path/filepath"
"strings"
"testing"
)
// TestNewConfig tests NewConfig (and indirectly all the validation functions
// that are used to reduce the complexity of NewConfig).
func TestNewConfig(t *testing.T) {
for _, tc := range []struct {
name string // Name of test case.
configFile string // Name of config file in testdata dir.
ErrSubstr string // Substring expected to be in error string.
expSuccess bool // Is NewConfig call expected to succeed?
}{{
name: "1_complete",
configFile: "config-complete.yaml",
ErrSubstr: "",
expSuccess: true,
}, {
name: "2_file_missing",
configFile: "config-nonexisting.yaml",
ErrSubstr: "read config",
expSuccess: false,
}, {
name: "3_invalid_yaml",
configFile: "config-invalid-yaml.yaml",
ErrSubstr: "unmarshal",
expSuccess: false,
}, {
name: "4_seconds_too_small",
configFile: "config-seconds.yaml",
ErrSubstr: "seconds smaller",
expSuccess: false,
}, {
name: "5_invalid_log_level",
configFile: "config-invalid-log-level.yaml",
ErrSubstr: "logging.level not supported",
expSuccess: false,
}, {
name: "6_log_level_default",
configFile: "config-log-level-default.yaml",
expSuccess: true,
}, {
name: "7_invalid_metric",
configFile: "config-invalid-metric.yaml",
ErrSubstr: "failed validating metric config",
expSuccess: false,
}, {
name: "8_invalid_targets",
configFile: "config-invalid-targets.yaml",
ErrSubstr: "failed validating targets config:",
expSuccess: false,
}} {
t.Run(tc.name, func(t *testing.T) {
c, err := NewConfig(filepath.Join("testdata", tc.configFile))
if tc.expSuccess && err != nil {
t.Fatalf("Unexpected failure: %s", err.Error())
}
if !tc.expSuccess && err == nil {
t.Fatal("Unexpected success.")
}
if !tc.expSuccess && !strings.Contains(err.Error(), tc.ErrSubstr) {
t.Fatalf(
"Error does not contain expected substring: got %q, want substring %q",
err.Error(), tc.ErrSubstr,
)
}
if err == nil {
if c.Seconds < 5 {
t.Errorf("Config seconds must be > 5: got %v", c.Seconds)
}
}
})
}
}
// TestValidateMetric tests ValidateMetric.
func TestValidateMetric(t *testing.T) {
for _, tc := range []struct {
name string // Name of test case.
metric Metric // Initialized metric struct.
errSubstr string // Substring expected in error string.
}{{
name: "1_namespace_empty",
metric: Metric{
Name: "Name",
},
errSubstr: "missing: metric.namespace",
}, {
name: "2_name_empty",
metric: Metric{
Namespace: "Namespace",
},
errSubstr: "missing: metric.name",
}, {
name: "3_dimension_name_empty",
metric: Metric{
Name: "Name",
Namespace: "Namespace",
Dimensions: []Dimension{{Value: "Value"}},
},
errSubstr: "missing: metric.dimensions[0].name",
}, {
name: "4_dimension_value_empty",
metric: Metric{
Name: "Name",
Namespace: "Namespace",
Dimensions: []Dimension{{Name: "Name"}},
},
errSubstr: "missing: metric.dimensions[0].value",
}, {
name: "5_all_is_good",
metric: Metric{
Name: "Name",
Namespace: "Namespace",
Dimensions: []Dimension{
{Name: "Name1", Value: "Value1"},
{Name: "Name2", Value: "Value2"},
},
},
}} {
t.Run(tc.name, func(t *testing.T) {
err := ValidateMetric(tc.metric)
if err != nil {
if len(tc.errSubstr) == 0 {
t.Errorf("Unexpected failure: %s", err.Error())
} else if !strings.Contains(err.Error(), tc.errSubstr) {
t.Errorf(
"Err does not contain substr: got %q, want substr %q",
err.Error(), tc.errSubstr,
)
}
} else {
if len(tc.errSubstr) != 0 {
t.Error("Unexpected success.")
}
}
})
}
}
// TestValidateTargets tests ValidateTargets.
func TestValidateTargets(t *testing.T) {
for _, tc := range []struct {
name string // Name of test case.
targets []Target // Initialized target structs.
errSubstr string // Substring expected in error string.
}{{
name: "1_no_targets",
errSubstr: "missing: targets",
}, {
name: "2_kind_not_supported",
targets: []Target{{
Kind: KindDaemonSet,
Namespace: "Namespace",
Name: "Name",
Mode: ModeAllOfThem,
}, {
Kind: "Job",
Namespace: "Namespace",
Name: "Name",
Mode: ModeAllOfThem,
}},
errSubstr: "target[1].kind not supported: Job",
}, {
name: "3_mode_not_supported",
targets: []Target{{
Kind: KindDaemonSet,
Namespace: "Namespace",
Name: "Name",
Mode: "AtLeastTwo",
}},
errSubstr: "target[0].mode not supported: AtLeastTwo",
}, {
name: "4_kind_empty",
targets: []Target{{
Kind: "",
Namespace: "Namespace",
Name: "Name",
Mode: ModeAtLeastOne,
}},
errSubstr: "missing: target[0].kind",
}, {
name: "5_name_empty",
targets: []Target{{
Kind: KindDeployment,
Namespace: "Namespace",
Name: "",
Mode: ModeAtLeastOne,
}},
errSubstr: "missing: target[0].name",
}, {
name: "6_namespace_empty",
targets: []Target{{
Kind: KindDeployment,
Namespace: "",
Name: "Name",
Mode: ModeAtLeastOne,
}},
errSubstr: "missing: target[0].namespace",
}, {
name: "7_mode_empty",
targets: []Target{{
Kind: KindDeployment,
Namespace: "Namespace",
Name: "Name",
Mode: "",
}},
errSubstr: "missing: target[0].mode",
}} {
t.Run(tc.name, func(t *testing.T) {
err := ValidateTargets(tc.targets)
if err != nil {
if len(tc.errSubstr) == 0 {
t.Errorf("Unexpected failure: %s", err.Error())
} else if !strings.Contains(err.Error(), tc.errSubstr) {
t.Errorf(
"Err does not contain substr: got %q, want substr %q",
err.Error(), tc.errSubstr,
)
}
} else {
if len(tc.errSubstr) != 0 {
t.Error("Unexpected success.")
}
}
})
}
}