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

Commit

Permalink
Prepared mock1 collector for incoming specified dynamic metric
Browse files Browse the repository at this point in the history
  • Loading branch information
IzabellaRaulin committed Sep 12, 2016
1 parent 14a1611 commit 55a683b
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 23 deletions.
63 changes: 54 additions & 9 deletions plugin/collector/snap-plugin-collector-mock1/mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,58 @@ const (
Type = plugin.CollectorPluginType
)

// make sure that we actually satisify requierd interface
// make sure that we actually satisfying required interface
var _ plugin.CollectorPlugin = (*Mock)(nil)

// Mock collector implementation used for testing
type Mock struct {
}

var availableHosts = getAllHostnames()

// CollectMetrics collects metrics for testing
func (f *Mock) CollectMetrics(mts []plugin.MetricType) ([]plugin.MetricType, error) {
metrics := []plugin.MetricType{}
rand.Seed(time.Now().UTC().UnixNano())
for i, p := range mts {
if mts[i].Namespace()[2].Value == "*" {
for j := 0; j < 10; j++ {
if isDynamic, _ := mts[i].Namespace().IsDynamic(); isDynamic {
requestedHosts := []string{}

if mts[i].Namespace()[2].Value == "*" {
// when dynamic element is not specified (equals an asterisk)
// then consider all available hosts as requested hosts
requestedHosts = append(requestedHosts, availableHosts...)

} else {
// when the dynamic element is specified
// then consider this specified host as requested hosts
host := mts[i].Namespace()[2].Value

// check if specified host is available in system
if contains(availableHosts, host) {
requestedHosts = append(requestedHosts, host)
} else {
return nil, fmt.Errorf("requested hostname `%s` is not available (list of available hosts: %s)", host, availableHosts)
}

}

// collect data for each of requested hosts
for _, host := range requestedHosts {
data := randInt(65, 90)
ns := make([]core.NamespaceElement, len(mts[i].Namespace()))
copy(ns, mts[i].Namespace())
ns[2].Value = fmt.Sprintf("host%d", j)
data := randInt(65, 90)
ns[2].Value = host
mt := plugin.MetricType{
Data_: data,
Namespace_: ns,
Timestamp_: time.Now(),
Unit_: mts[i].Unit(),
Version_: mts[i].Version(),
}
metrics = append(metrics, mt)
}

} else {
if cv, ok := p.Config().Table()["test"]; ok {
p.Data_ = fmt.Sprintf("The mock collected data! config data: name=%s password=%s test=%v", p.Config().Table()["name"], p.Config().Table()["password"], cv.(ctypes.ConfigValueBool).Value)
Expand All @@ -78,7 +104,7 @@ func (f *Mock) CollectMetrics(mts []plugin.MetricType) ([]plugin.MetricType, err
return metrics, nil
}

//GetMetricTypes returns metric types for testing
// GetMetricTypes returns metric types for testing
func (f *Mock) GetMetricTypes(cfg plugin.ConfigType) ([]plugin.MetricType, error) {
mts := []plugin.MetricType{}
if _, ok := cfg.Table()["test-fail"]; ok {
Expand All @@ -95,7 +121,7 @@ func (f *Mock) GetMetricTypes(cfg plugin.ConfigType) ([]plugin.MetricType, error
return mts, nil
}

//GetConfigPolicy returns a ConfigPolicyTree for testing
// GetConfigPolicy returns a ConfigPolicyTree for testing
func (f *Mock) GetConfigPolicy() (*cpolicy.ConfigPolicy, error) {
c := cpolicy.New()
rule, _ := cpolicy.NewStringRule("name", false, "bob")
Expand All @@ -107,7 +133,7 @@ func (f *Mock) GetConfigPolicy() (*cpolicy.ConfigPolicy, error) {
return c, nil
}

//Meta returns meta data for testing
// Meta returns meta data for testing
func Meta() *plugin.PluginMeta {
return plugin.NewPluginMeta(
Name,
Expand All @@ -121,7 +147,26 @@ func Meta() *plugin.PluginMeta {
)
}

//Random number generator
// contains reports whether a given item is found in a slice
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}

// getAllHostnames returns all available hostnames ('host0', 'host1', ..., 'host9')
func getAllHostnames() []string {
res := []string{}
for j := 0; j < 10; j++ {
res = append(res, fmt.Sprintf("host%d", j))
}
return res
}

// Random number generator
func randInt(min int, max int) int {
return min + rand.Intn(max-min)
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,61 @@ func TestCollectMetric(t *testing.T) {
}
})

Convey("testing specified metics", func() {
mTypes := []plugin.MetricType{
plugin.MetricType{Namespace_: ns3, Config_: cfg.ConfigDataNode},
}
mts, _ := newPlg.CollectMetrics(mTypes)
Convey("testing dynamic metric", func() {

for _, mt := range mts {
_, ok := mt.Data_.(int)
So(ok, ShouldBeTrue)
}
mt := plugin.MetricType{Namespace_: ns3, Config_: cfg.ConfigDataNode}

Convey("for none specified instance", func() {
mts, _ := newPlg.CollectMetrics([]plugin.MetricType{mt})

// there is 10 available hosts (host0, host1, ..., host9)
So(len(mts), ShouldEqual, 10)

Convey("returned metrics should have data type integer", func() {
for _, mt := range mts {
_, ok := mt.Data_.(int)
So(ok, ShouldBeTrue)
}
})

Convey("returned metrics should remain dynamic", func() {
for _, mt := range mts {
isDynamic, _ := mt.Namespace().IsDynamic()
So(isDynamic, ShouldBeTrue)
}
})

})

Convey("for specified instance which is available - host0", func() {
mt.Namespace()[2].Value = "host0"
mts, _ := newPlg.CollectMetrics([]plugin.MetricType{mt})

// only one metric for this specific hostname should be returned
So(len(mts), ShouldEqual, 1)
So(mts[0].Namespace().String(), ShouldEqual, "/intel/mock/host0/baz")

Convey("returned metric should have data type integer", func() {
_, ok := mts[0].Data_.(int)
So(ok, ShouldBeTrue)
})

Convey("returned metric should remain dynamic", func() {
isDynamic, _ := mt.Namespace().IsDynamic()
So(isDynamic, ShouldBeTrue)
})

})

Convey("for specified instance which is not available - host10", func() {
mt.Namespace()[2].Value = "host10"
mts, err := newPlg.CollectMetrics([]plugin.MetricType{mt})

So(mts, ShouldBeNil)
So(err, ShouldNotBeNil)
So(err.Error(), ShouldStartWith, "requested hostname `host10` is not available")

})
})

})
Expand All @@ -98,16 +143,26 @@ func TestCollectMetric(t *testing.T) {
}
})

Convey("testing specified metics", func() {
Convey("testing dynamic metics", func() {
mTypes := []plugin.MetricType{
plugin.MetricType{Namespace_: ns3, Config_: cfg.ConfigDataNode},
}
mts, _ := newPlg.CollectMetrics(mTypes)

for _, mt := range mts {
_, ok := mt.Data_.(int)
So(ok, ShouldBeTrue)
}
Convey("returned metrics should have data type integer", func() {
for _, mt := range mts {
_, ok := mt.Data_.(int)
So(ok, ShouldBeTrue)
}
})

Convey("returned metrics should remain dynamic", func() {
for _, mt := range mts {
isDynamic, _ := mt.Namespace().IsDynamic()
So(isDynamic, ShouldBeTrue)
}
})

})

})
Expand Down

0 comments on commit 55a683b

Please sign in to comment.