diff --git a/plugin/collector/snap-plugin-collector-mock1/mock/mock.go b/plugin/collector/snap-plugin-collector-mock1/mock/mock.go index cb0ef4e23..ef5f6ecfc 100644 --- a/plugin/collector/snap-plugin-collector-mock1/mock/mock.go +++ b/plugin/collector/snap-plugin-collector-mock1/mock/mock.go @@ -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) @@ -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 { @@ -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") @@ -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, @@ -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) } diff --git a/plugin/collector/snap-plugin-collector-mock1/mock/mock_medium_test.go b/plugin/collector/snap-plugin-collector-mock1/mock/mock_medium_test.go index 91d725343..1b9aea53e 100644 --- a/plugin/collector/snap-plugin-collector-mock1/mock/mock_medium_test.go +++ b/plugin/collector/snap-plugin-collector-mock1/mock/mock_medium_test.go @@ -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") + + }) }) }) @@ -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) + } + }) + }) })