-
Notifications
You must be signed in to change notification settings - Fork 59
/
metrics_test.go
274 lines (249 loc) · 5.76 KB
/
metrics_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
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package charm_test
import (
"sort"
"strings"
gc "gopkg.in/check.v1"
"github.com/juju/charm/v12"
)
// Keys returns a list of all defined metrics keys.
func Keys(m *charm.Metrics) []string {
result := make([]string, 0, len(m.Metrics))
for name := range m.Metrics {
result = append(result, name)
}
sort.Strings(result)
return result
}
type MetricsSuite struct{}
var _ = gc.Suite(&MetricsSuite{})
func (s *MetricsSuite) TestReadEmpty(c *gc.C) {
metrics, err := charm.ReadMetrics(strings.NewReader(""))
c.Assert(err, gc.IsNil)
c.Assert(metrics, gc.NotNil)
}
func (s *MetricsSuite) TestReadAlmostEmpty(c *gc.C) {
metrics, err := charm.ReadMetrics(strings.NewReader(`
metrics:
`))
c.Assert(err, gc.IsNil)
c.Assert(metrics, gc.NotNil)
}
func (s *MetricsSuite) TestNoDescription(c *gc.C) {
metrics, err := charm.ReadMetrics(strings.NewReader(`
metrics:
some-metric:
type: gauge
`))
c.Assert(err, gc.ErrorMatches, "invalid metrics declaration: metric \"some-metric\" lacks description")
c.Assert(metrics, gc.IsNil)
}
func (s *MetricsSuite) TestIncorrectType(c *gc.C) {
metrics, err := charm.ReadMetrics(strings.NewReader(`
metrics:
some-metric:
type: not-a-type
description: Some description.
`))
c.Assert(err, gc.ErrorMatches, "invalid metrics declaration: metric \"some-metric\" has unknown type \"not-a-type\"")
c.Assert(metrics, gc.IsNil)
}
func (s *MetricsSuite) TestMultipleDefinition(c *gc.C) {
metrics, err := charm.ReadMetrics(strings.NewReader(`
metrics:
some-metric:
type: gauge
description: Some description.
some-metric:
type: absolute
description: Some other description.
`))
c.Assert(err, gc.IsNil)
c.Assert(metrics.Metrics, gc.HasLen, 1)
c.Assert(metrics.Metrics["some-metric"].Type, gc.Equals, charm.MetricTypeAbsolute)
}
func (s *MetricsSuite) TestIsBuiltinMetric(c *gc.C) {
tests := []struct {
input string
isbuiltin bool
}{{
"juju-thing",
true,
}, {
"jujuthing",
true,
}, {
"thing",
false,
},
}
for i, test := range tests {
c.Logf("test %d isBuiltinMetric(%v) = %v", i, test.input, test.isbuiltin)
is := charm.IsBuiltinMetric(test.input)
c.Assert(is, gc.Equals, test.isbuiltin)
}
}
func (s *MetricsSuite) TestValidYaml(c *gc.C) {
metrics, err := charm.ReadMetrics(strings.NewReader(`
metrics:
blips:
type: absolute
description: An absolute metric.
blops:
type: gauge
description: A gauge metric.
juju-unit-time:
`))
c.Assert(err, gc.IsNil)
c.Assert(metrics, gc.NotNil)
c.Assert(Keys(metrics), gc.DeepEquals, []string{"blips", "blops", "juju-unit-time"})
testCases := []struct {
about string
name string
value string
err string
}{{
about: "valid gauge metric",
name: "blops",
value: "1",
err: "",
}, {
about: "valid absolute metric",
name: "blips",
value: "0",
err: "",
}, {
about: "valid gauge metric, float value",
name: "blops",
value: "0.15",
err: "",
}, {
about: "valid absolute metric, float value",
name: "blips",
value: "6.015e15",
err: "",
}, {
about: "undeclared metric",
name: "undeclared",
value: "6.015e15",
err: "metric \"undeclared\" not defined",
}, {
about: "invalid type for gauge metric",
name: "blops",
value: "true",
err: "invalid value type: expected float, got \"true\"",
}, {
about: "metric value too large",
name: "blips",
value: "1111111111111111111111111111111",
err: "metric value is too large",
},
}
for i, t := range testCases {
c.Logf("test %d: %s", i, t.about)
err := metrics.ValidateMetric(t.name, t.value)
if t.err == "" {
c.Check(err, gc.IsNil)
} else {
c.Check(err, gc.ErrorMatches, t.err)
}
}
}
func (s *MetricsSuite) TestBuiltInMetrics(c *gc.C) {
tests := []string{`
metrics:
some-metric:
type: gauge
description: Some description.
juju-unit-time:
type: absolute
`, `
metrics:
some-metric:
type: gauge
description: Some description.
juju-unit-time:
description: Some description
`,
}
for _, test := range tests {
c.Logf("%s", test)
_, err := charm.ReadMetrics(strings.NewReader(test))
c.Assert(err, gc.ErrorMatches, `metric "juju-unit-time" is using a prefix reserved for built-in metrics: it should not have type or description specification`)
}
}
func (s *MetricsSuite) TestValidateValue(c *gc.C) {
tests := []struct {
value string
expectedError string
}{{
value: "1234567890",
}, {
value: "0",
}, {
value: "abcd",
expectedError: `invalid value type: expected float, got "abcd"`,
}, {
value: "1234567890123456789012345678901234567890",
expectedError: "metric value is too large",
}, {
value: "-42",
expectedError: "invalid value: value must be greater or equal to zero, got -42",
},
}
for _, test := range tests {
err := charm.ValidateValue(test.value)
if test.expectedError != "" {
c.Assert(err, gc.ErrorMatches, test.expectedError)
} else {
c.Assert(err, gc.IsNil)
}
}
}
func (s *MetricsSuite) TestPlanRequired(c *gc.C) {
tests := []struct {
about string
input string
planRequired bool
}{{
about: "not specified",
input: `
metrics:
some-metric:
type: gauge
description: thing
`,
planRequired: false,
}, {
about: "plan optional",
input: `
plan:
required: false
metrics:
`,
planRequired: false,
}, {
about: "plan required",
input: `
plan:
required: true
metrics:
`,
planRequired: true,
}, {
about: "not set",
input: `
plan:
metrics:
`,
planRequired: false,
},
}
for i, test := range tests {
c.Logf("testplanrequired %d: %s", i, test.about)
metrics, err := charm.ReadMetrics(strings.NewReader(test.input))
c.Assert(err, gc.IsNil)
c.Assert(metrics.PlanRequired(), gc.Equals, test.planRequired)
}
}