This repository has been archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 294
/
Copy pathmetrics_small_test.go
202 lines (175 loc) · 5.52 KB
/
metrics_small_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
// +build small
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015-2016 Intel Corporation
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 control
import (
"testing"
"github.com/intelsdi-x/snap/control/plugin"
"github.com/intelsdi-x/snap/core"
. "github.com/smartystreets/goconvey/convey"
)
const (
foo = "/intel/foo"
bar = "/intel/foo/bar"
tar = "/intel/tar/qaz"
)
func TestAddTagsFromWorkflow(t *testing.T) {
hostnameReader = &mockHostnameReader{}
tcs := prepareTestCases()
Convey("Adding tags to metric type", t, func() {
p := newPluginManager()
for _, tc := range tcs {
outputTags := p.AddStandardAndWorkflowTags(tc.Metric, tc.InputTags).Tags()
So(outputTags, ShouldNotBeNil)
So(outputTags, ShouldResemble, tc.ExpectedTags)
}
})
}
func TestContainsTuplePositive(t *testing.T) {
Convey("when tuple contains two items", t, func() {
dut := "(host0;host1)"
hasTuple, tuple := containsTuple(dut)
So(hasTuple, ShouldBeTrue)
So(len(tuple), ShouldEqual, 2)
So(tuple, ShouldContain, "host0")
So(tuple, ShouldContain, "host1")
})
Convey("when tuple contains three items", t, func() {
dut := "(host0;host1;host2)"
hasTuple, tuple := containsTuple(dut)
So(hasTuple, ShouldBeTrue)
So(len(tuple), ShouldEqual, 3)
So(tuple, ShouldContain, "host0")
So(tuple, ShouldContain, "host2")
So(tuple, ShouldContain, "host2")
})
Convey("when tuple contains white spaces", t, func() {
dut := "( host0 ; host1 ; host2 )"
hasTuple, tuple := containsTuple(dut)
So(hasTuple, ShouldBeTrue)
So(len(tuple), ShouldEqual, 3)
So(tuple, ShouldContain, "host0")
So(tuple, ShouldContain, "host2")
So(tuple, ShouldContain, "host2")
})
Convey("when tuple contains multiple-word items", t, func() {
dut := "(mock host0;the mock host1;this is mock host2)"
hasTuple, tuple := containsTuple(dut)
So(hasTuple, ShouldBeTrue)
So(len(tuple), ShouldEqual, 3)
So(tuple, ShouldContain, "mock host0")
So(tuple, ShouldContain, "the mock host1")
So(tuple, ShouldContain, "this is mock host2")
})
Convey("when tuple's item contains brackets by itself", t, func() {
dut := "(host0(some details);host1(some details))"
hasTuple, tuple := containsTuple(dut)
So(hasTuple, ShouldBeTrue)
So(len(tuple), ShouldEqual, 2)
So(tuple, ShouldContain, "host0(some details)")
So(tuple, ShouldContain, "host1(some details)")
})
}
func TestContainsTupleNegative(t *testing.T) {
Convey("for one-word namespace's element", t, func() {
dut := "host0"
hasTuple, tuple := containsTuple(dut)
So(hasTuple, ShouldBeFalse)
So(tuple, ShouldBeNil)
})
Convey("for two-word namespace's element", t, func() {
dut := "my host0"
hasTuple, tuple := containsTuple(dut)
So(hasTuple, ShouldBeFalse)
So(tuple, ShouldBeNil)
})
Convey("missing brackets in tuple", t, func() {
// that also means that using semicolon in metric namespace is allowed
// because without brackets it won't be recognized as a tuple
dut := "host0;host1"
hasTuple, tuple := containsTuple(dut)
So(hasTuple, ShouldBeFalse)
So(tuple, ShouldBeNil)
})
Convey("missing semicolon in tuple", t, func() {
// that also means that using brackets in metric namespace is allowed
// because without semicolon used as separator it won't be recognized as a tuple
dut := "(host0, host1)"
hasTuple, tuple := containsTuple(dut)
So(hasTuple, ShouldBeFalse)
So(tuple, ShouldBeNil)
})
}
type mockHostnameReader struct{}
func (m *mockHostnameReader) Hostname() string {
return "hostname"
}
type testCase struct {
Metric plugin.MetricType
InputTags map[string]map[string]string
ExpectedTags map[string]string
}
func prepareTestCases() []testCase {
hostname := hostnameReader.Hostname()
fooTags := map[string]string{
"foo_tag": "foo_val",
}
barTags := map[string]string{
"foobar_tag": "foobar_val",
}
tarTags := map[string]string{
"tarqaz_tag": "tarqaz_val",
}
allTags := map[string]map[string]string{
foo: fooTags,
bar: barTags,
tar: tarTags,
}
foobazMetric := plugin.MetricType{
Namespace_: core.NewNamespace("intel", "foo", "baz"),
}
foobarMetric := plugin.MetricType{
Namespace_: core.NewNamespace("intel", "foo", "bar"),
}
tarqazMetric := plugin.MetricType{
Namespace_: core.NewNamespace("intel", "tar", "qaz"),
}
stdMetric := plugin.MetricType{
Namespace_: core.NewNamespace("intel", "std"),
}
foobazExpected := map[string]string{
core.STD_TAG_PLUGIN_RUNNING_ON: hostname,
"foo_tag": "foo_val",
}
foobarExpected := map[string]string{
core.STD_TAG_PLUGIN_RUNNING_ON: hostname,
"foo_tag": "foo_val",
"foobar_tag": "foobar_val",
}
tarqazExpected := map[string]string{
core.STD_TAG_PLUGIN_RUNNING_ON: hostname,
"tarqaz_tag": "tarqaz_val",
}
stdExpected := map[string]string{
core.STD_TAG_PLUGIN_RUNNING_ON: hostname,
}
testCases := []testCase{
{foobazMetric, allTags, foobazExpected},
{foobarMetric, allTags, foobarExpected},
{tarqazMetric, allTags, tarqazExpected},
{stdMetric, allTags, stdExpected},
{foobazMetric, nil, stdExpected},
}
return testCases
}