Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 491cbee

Browse files
Prepared anothermock1 collector for incoming specified dynamic metric
1 parent c98e195 commit 491cbee

2 files changed

Lines changed: 337 additions & 9 deletions

File tree

plugin/collector/snap-plugin-collector-anothermock1/anothermock/anothermock.go

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ const (
4444
type AnotherMock struct {
4545
}
4646

47+
// list of available hosts
48+
var availableHosts = getAllHostnames()
49+
4750
// CollectMetrics collects metrics for testing
4851
func (f *AnotherMock) CollectMetrics(mts []plugin.MetricType) ([]plugin.MetricType, error) {
4952
for _, p := range mts {
@@ -56,18 +59,44 @@ func (f *AnotherMock) CollectMetrics(mts []plugin.MetricType) ([]plugin.MetricTy
5659
if c, ok := mts[i].Config().Table()["panic"]; ok && c.(ctypes.ConfigValueBool).Value {
5760
panic("Oops!")
5861
}
59-
if mts[i].Namespace()[2].Value == "*" {
60-
for j := 0; j < 10; j++ {
62+
63+
if isDynamic, _ := mts[i].Namespace().IsDynamic(); isDynamic {
64+
requestedHosts := []string{}
65+
66+
if mts[i].Namespace()[2].Value == "*" {
67+
// when dynamic element is not specified (equals an asterisk)
68+
// then consider all available hosts as requested hosts
69+
requestedHosts = append(requestedHosts, availableHosts...)
70+
} else {
71+
// when the dynamic element is specified
72+
// then consider this specified host as requested hosts
73+
host := mts[i].Namespace()[2].Value
74+
75+
// check if specified host is available in system
76+
if contains(availableHosts, host) {
77+
requestedHosts = append(requestedHosts, host)
78+
} else {
79+
return nil, fmt.Errorf("requested hostname `%s` is not available (list of available hosts: %s)", host, availableHosts)
80+
}
81+
82+
}
83+
// collect data for each of requested hosts
84+
for _, host := range requestedHosts {
85+
//generate random data
86+
data := 9000 + randInt(65, 90)
87+
// prepare namespace as a copy of incoming dynamic namespace,
88+
// but with the set value of dynamic element
6189
ns := make([]core.NamespaceElement, len(mts[i].Namespace()))
6290
copy(ns, mts[i].Namespace())
63-
ns[2].Value = fmt.Sprintf("host%d", j)
64-
data := 9000 + randInt(65, 90)
91+
ns[2].Value = host
92+
93+
// metric with set data, ns, timestamp and the version of the plugin
6594
mt := plugin.MetricType{
6695
Data_: data,
6796
Namespace_: ns,
6897
Timestamp_: time.Now(),
69-
Version_: mts[i].Version(),
7098
Unit_: mts[i].Unit(),
99+
Version_: mts[i].Version(),
71100
}
72101
metrics = append(metrics, mt)
73102
}
@@ -81,7 +110,7 @@ func (f *AnotherMock) CollectMetrics(mts []plugin.MetricType) ([]plugin.MetricTy
81110
return metrics, nil
82111
}
83112

84-
//GetMetricTypes returns metric types for testing
113+
// GetMetricTypes returns metric types for testing
85114
func (f *AnotherMock) GetMetricTypes(cfg plugin.ConfigType) ([]plugin.MetricType, error) {
86115
mts := []plugin.MetricType{}
87116
if _, ok := cfg.Table()["test-fail"]; ok {
@@ -114,7 +143,7 @@ func (f *AnotherMock) GetMetricTypes(cfg plugin.ConfigType) ([]plugin.MetricType
114143
return mts, nil
115144
}
116145

117-
//GetConfigPolicy returns a ConfigPolicy for testing
146+
// GetConfigPolicy returns a ConfigPolicy for testing
118147
func (f *AnotherMock) GetConfigPolicy() (*cpolicy.ConfigPolicy, error) {
119148
c := cpolicy.New()
120149
rule, _ := cpolicy.NewStringRule("name", false, "bob")
@@ -126,7 +155,7 @@ func (f *AnotherMock) GetConfigPolicy() (*cpolicy.ConfigPolicy, error) {
126155
return c, nil
127156
}
128157

129-
//Meta returns meta data for testing
158+
// Meta returns meta data for testing
130159
func Meta() *plugin.PluginMeta {
131160
return plugin.NewPluginMeta(
132161
Name,
@@ -139,7 +168,26 @@ func Meta() *plugin.PluginMeta {
139168
)
140169
}
141170

142-
//Random number generator
171+
// contains reports whether a given item is found in a slice
172+
func contains(slice []string, item string) bool {
173+
for _, s := range slice {
174+
if s == item {
175+
return true
176+
}
177+
}
178+
return false
179+
}
180+
181+
// getAllHostnames returns all available hostnames ('host0', 'host1', ..., 'host9')
182+
func getAllHostnames() []string {
183+
res := []string{}
184+
for j := 0; j < 10; j++ {
185+
res = append(res, fmt.Sprintf("host%d", j))
186+
}
187+
return res
188+
}
189+
190+
// random number generator
143191
func randInt(min int, max int) int {
144192
return min + rand.Intn(max-min)
145193
}
Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,280 @@
1+
// +build medium
2+
3+
/*
4+
http://www.apache.org/licenses/LICENSE-2.0.txt
5+
6+
7+
Copyright 2016 Intel Corporation
8+
9+
Licensed under the Apache License, Version 2.0 (the "License");
10+
you may not use this file except in compliance with the License.
11+
You may obtain a copy of the License at
12+
13+
http://www.apache.org/licenses/LICENSE-2.0
14+
15+
Unless required by applicable law or agreed to in writing, software
16+
distributed under the License is distributed on an "AS IS" BASIS,
17+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
See the License for the specific language governing permissions and
19+
limitations under the License.
20+
*/
21+
22+
package anothermock
23+
24+
import (
25+
"math/rand"
26+
"testing"
27+
"time"
28+
29+
"github.com/intelsdi-x/snap-plugin-utilities/str"
30+
"github.com/intelsdi-x/snap/control/plugin"
31+
"github.com/intelsdi-x/snap/core"
32+
"github.com/intelsdi-x/snap/core/cdata"
33+
"github.com/intelsdi-x/snap/core/ctypes"
34+
. "github.com/smartystreets/goconvey/convey"
35+
)
36+
37+
func TestCollectMetric(t *testing.T) {
38+
ns0 := core.NewNamespace("intel", "anothermock", "test")
39+
ns1 := core.NewNamespace("intel", "anothermock", "foo")
40+
ns2 := core.NewNamespace("intel", "anothermock", "bar")
41+
ns3 := core.NewNamespace("intel", "anothermock").AddDynamicElement("host", "name of the host").AddStaticElement("baz")
42+
43+
Convey("Testing CollectMetric", t, func() {
44+
45+
newPlg := new(AnotherMock)
46+
So(newPlg, ShouldNotBeNil)
47+
48+
Convey("with 'test' config variable'", func() {
49+
50+
node := cdata.NewNode()
51+
node.AddItem("test", ctypes.ConfigValueBool{Value: true})
52+
cfg := plugin.ConfigType{ConfigDataNode: node}
53+
54+
Convey("testing specific metrics", func() {
55+
mTypes := []plugin.MetricType{
56+
plugin.MetricType{Namespace_: ns0, Config_: cfg.ConfigDataNode},
57+
plugin.MetricType{Namespace_: ns1, Config_: cfg.ConfigDataNode},
58+
plugin.MetricType{Namespace_: ns2, Config_: cfg.ConfigDataNode},
59+
}
60+
mts, _ := newPlg.CollectMetrics(mTypes)
61+
62+
Convey("returned metrics should have data type integer", func() {
63+
for _, mt := range mts {
64+
_, ok := mt.Data_.(int)
65+
So(ok, ShouldBeTrue)
66+
}
67+
})
68+
})
69+
70+
Convey("testing dynamic metric", func() {
71+
72+
mt := plugin.MetricType{Namespace_: ns3, Config_: cfg.ConfigDataNode}
73+
74+
Convey("for none specified instance", func() {
75+
mts, _ := newPlg.CollectMetrics([]plugin.MetricType{mt})
76+
77+
// there is 10 available hosts (host0, host1, ..., host9)
78+
So(len(mts), ShouldEqual, 10)
79+
80+
Convey("returned metrics should have data type integer", func() {
81+
for _, mt := range mts {
82+
_, ok := mt.Data_.(int)
83+
So(ok, ShouldBeTrue)
84+
}
85+
})
86+
87+
Convey("returned metrics should remain dynamic", func() {
88+
for _, mt := range mts {
89+
isDynamic, _ := mt.Namespace().IsDynamic()
90+
So(isDynamic, ShouldBeTrue)
91+
}
92+
})
93+
94+
})
95+
96+
Convey("for specified instance which is available - host0", func() {
97+
mt.Namespace()[2].Value = "host0"
98+
mts, _ := newPlg.CollectMetrics([]plugin.MetricType{mt})
99+
100+
// only one metric for this specific hostname should be returned
101+
So(len(mts), ShouldEqual, 1)
102+
So(mts[0].Namespace().String(), ShouldEqual, "/intel/anothermock/host0/baz")
103+
104+
Convey("returned metric should have data type integer", func() {
105+
_, ok := mts[0].Data_.(int)
106+
So(ok, ShouldBeTrue)
107+
})
108+
109+
Convey("returned metric should remain dynamic", func() {
110+
isDynamic, _ := mt.Namespace().IsDynamic()
111+
So(isDynamic, ShouldBeTrue)
112+
})
113+
114+
})
115+
116+
Convey("for specified instance which is not available - host10", func() {
117+
mt.Namespace()[2].Value = "host10"
118+
mts, err := newPlg.CollectMetrics([]plugin.MetricType{mt})
119+
120+
So(mts, ShouldBeNil)
121+
So(err, ShouldNotBeNil)
122+
So(err.Error(), ShouldStartWith, "requested hostname `host10` is not available")
123+
124+
})
125+
})
126+
127+
})
128+
129+
Convey("without config variables", func() {
130+
131+
node := cdata.NewNode()
132+
cfg := plugin.ConfigType{ConfigDataNode: node}
133+
134+
Convey("testing specific metrics", func() {
135+
mTypes := []plugin.MetricType{
136+
plugin.MetricType{Namespace_: ns0, Config_: cfg.ConfigDataNode},
137+
plugin.MetricType{Namespace_: ns1, Config_: cfg.ConfigDataNode},
138+
plugin.MetricType{Namespace_: ns2, Config_: cfg.ConfigDataNode},
139+
}
140+
mts, _ := newPlg.CollectMetrics(mTypes)
141+
142+
Convey("returned metrics should have data type integer", func() {
143+
for _, mt := range mts {
144+
_, ok := mt.Data_.(int)
145+
So(ok, ShouldBeTrue)
146+
}
147+
})
148+
})
149+
150+
Convey("testing dynamic metics", func() {
151+
mTypes := []plugin.MetricType{
152+
plugin.MetricType{Namespace_: ns3, Config_: cfg.ConfigDataNode},
153+
}
154+
mts, _ := newPlg.CollectMetrics(mTypes)
155+
156+
Convey("returned metrics should have data type integer", func() {
157+
for _, mt := range mts {
158+
_, ok := mt.Data_.(int)
159+
So(ok, ShouldBeTrue)
160+
}
161+
})
162+
163+
Convey("returned metrics should remain dynamic", func() {
164+
for _, mt := range mts {
165+
isDynamic, _ := mt.Namespace().IsDynamic()
166+
So(isDynamic, ShouldBeTrue)
167+
}
168+
})
169+
170+
})
171+
172+
})
173+
174+
})
175+
}
176+
177+
func TestGetMetricTypes(t *testing.T) {
178+
Convey("Tesing GetMetricTypes", t, func() {
179+
180+
newPlg := new(AnotherMock)
181+
So(newPlg, ShouldNotBeNil)
182+
183+
Convey("with missing on-load plugin config entry", func() {
184+
node := cdata.NewNode()
185+
node.AddItem("test-fail", ctypes.ConfigValueStr{Value: ""})
186+
187+
_, err := newPlg.GetMetricTypes(plugin.ConfigType{ConfigDataNode: node})
188+
189+
So(err, ShouldNotBeNil)
190+
})
191+
192+
Convey("with 'test' config variable", func() {
193+
node := cdata.NewNode()
194+
node.AddItem("test", ctypes.ConfigValueStr{Value: ""})
195+
196+
mts, err := newPlg.GetMetricTypes(plugin.ConfigType{ConfigDataNode: node})
197+
198+
So(err, ShouldBeNil)
199+
So(len(mts), ShouldEqual, 4)
200+
201+
Convey("checking namespaces", func() {
202+
metricNames := []string{}
203+
for _, m := range mts {
204+
metricNames = append(metricNames, m.Namespace().String())
205+
}
206+
207+
ns := core.NewNamespace("intel", "anothermock", "test")
208+
So(str.Contains(metricNames, ns.String()), ShouldBeTrue)
209+
210+
ns = core.NewNamespace("intel", "anothermock", "foo")
211+
So(str.Contains(metricNames, ns.String()), ShouldBeTrue)
212+
213+
ns = core.NewNamespace("intel", "anothermock", "bar")
214+
So(str.Contains(metricNames, ns.String()), ShouldBeTrue)
215+
216+
ns = core.NewNamespace("intel", "anothermock").AddDynamicElement("host", "name of the host").AddStaticElement("baz")
217+
So(str.Contains(metricNames, ns.String()), ShouldBeTrue)
218+
})
219+
})
220+
221+
Convey("without config variables", func() {
222+
node := cdata.NewNode()
223+
mts, err := newPlg.GetMetricTypes(plugin.ConfigType{ConfigDataNode: node})
224+
225+
So(err, ShouldBeNil)
226+
So(len(mts), ShouldEqual, 3)
227+
228+
Convey("checking namespaces", func() {
229+
metricNames := []string{}
230+
for _, m := range mts {
231+
metricNames = append(metricNames, m.Namespace().String())
232+
}
233+
234+
ns := core.NewNamespace("intel", "anothermock", "foo")
235+
So(str.Contains(metricNames, ns.String()), ShouldBeTrue)
236+
237+
ns = core.NewNamespace("intel", "anothermock", "bar")
238+
So(str.Contains(metricNames, ns.String()), ShouldBeTrue)
239+
240+
ns = core.NewNamespace("intel", "anothermock").AddDynamicElement("host", "name of the host").AddStaticElement("baz")
241+
So(str.Contains(metricNames, ns.String()), ShouldBeTrue)
242+
})
243+
})
244+
245+
})
246+
}
247+
248+
func TestMeta(t *testing.T) {
249+
Convey("Testing Meta", t, func() {
250+
meta := Meta()
251+
So(meta.Name, ShouldEqual, Name)
252+
So(meta.Version, ShouldEqual, Version)
253+
So(meta.Type, ShouldEqual, Type)
254+
So(meta.AcceptedContentTypes[0], ShouldEqual, plugin.SnapGOBContentType)
255+
So(meta.ReturnedContentTypes[0], ShouldEqual, plugin.SnapGOBContentType)
256+
So(meta.Unsecure, ShouldEqual, false)
257+
So(meta.RoutingStrategy, ShouldEqual, plugin.StickyRouting)
258+
So(meta.CacheTTL, ShouldEqual, 100*time.Millisecond)
259+
})
260+
}
261+
262+
func TestRandInt(t *testing.T) {
263+
Convey("Testing randInt", t, func() {
264+
rand.Seed(time.Now().UTC().UnixNano())
265+
data := randInt(65, 90)
266+
So(data, ShouldBeBetween, 64, 91)
267+
})
268+
}
269+
270+
func TestGetConfigPolicy(t *testing.T) {
271+
Convey("Testing GetConfigPolicy", t, func() {
272+
newPlg := new(AnotherMock)
273+
So(newPlg, ShouldNotBeNil)
274+
275+
configPolicy, err := newPlg.GetConfigPolicy()
276+
277+
So(err, ShouldBeNil)
278+
So(configPolicy, ShouldNotBeNil)
279+
})
280+
}

0 commit comments

Comments
 (0)