From 0cf6446c2c51fe3b235d7a7de03d26062b3e8555 Mon Sep 17 00:00:00 2001 From: katarzyna-z Date: Mon, 23 May 2016 15:36:39 +0200 Subject: [PATCH] small tests for snap-collector-mock1 --- Godeps/Godeps.json | 4 + .../snap-collector-mock1/main_small_test.go | 34 +++ .../mock/mock_small_test.go | 220 ++++++++++++++++++ 3 files changed, 258 insertions(+) create mode 100644 plugin/collector/snap-collector-mock1/main_small_test.go create mode 100644 plugin/collector/snap-collector-mock1/mock/mock_small_test.go diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index c120b2f81..5708ae5e7 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -157,6 +157,10 @@ "ImportPath": "k8s.io/kubernetes/third_party/forked/reflect", "Comment": "v1.3.0-alpha.3-706-ga7c9c07", "Rev": "a7c9c07b5acb3c8c25e918148a782cb70e86a48c" + }, + { + "ImportPath": "github.com/intelsdi-x/snap-plugin-utilities/str", + "Rev": "04621af7a3979bcb8aaf08c3b6542b43fe0bf6cf" } ] } diff --git a/plugin/collector/snap-collector-mock1/main_small_test.go b/plugin/collector/snap-collector-mock1/main_small_test.go new file mode 100644 index 000000000..6f4edabaf --- /dev/null +++ b/plugin/collector/snap-collector-mock1/main_small_test.go @@ -0,0 +1,34 @@ +// +build small + +/* +http://www.apache.org/licenses/LICENSE-2.0.txt + + +Copyright 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 main + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestMain(t *testing.T) { + Convey("Testing main", t, func() { + So(func() { main() }, ShouldNotPanic) + }) +} diff --git a/plugin/collector/snap-collector-mock1/mock/mock_small_test.go b/plugin/collector/snap-collector-mock1/mock/mock_small_test.go new file mode 100644 index 000000000..3ed678836 --- /dev/null +++ b/plugin/collector/snap-collector-mock1/mock/mock_small_test.go @@ -0,0 +1,220 @@ +// +build small + +/* +http://www.apache.org/licenses/LICENSE-2.0.txt + + +Copyright 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 mock + +import ( + "math/rand" + "testing" + "time" + + "github.com/intelsdi-x/snap-plugin-utilities/str" + "github.com/intelsdi-x/snap/control/plugin" + "github.com/intelsdi-x/snap/core" + "github.com/intelsdi-x/snap/core/cdata" + "github.com/intelsdi-x/snap/core/ctypes" + . "github.com/smartystreets/goconvey/convey" +) + +func TestCollectMetric(t *testing.T) { + ns0 := core.NewNamespace("intel", "mock", "test") + ns1 := core.NewNamespace("intel", "mock", "foo") + ns2 := core.NewNamespace("intel", "mock", "bar") + ns3 := core.NewNamespace("intel", "mock").AddDynamicElement("host", "name of the host").AddStaticElement("baz") + + Convey("Testing CollectMetric", t, func() { + + newPlg := new(Mock) + So(newPlg, ShouldNotBeNil) + + Convey("with 'test' config variable'", func() { + + node := cdata.NewNode() + node.AddItem("test", ctypes.ConfigValueBool{Value: true}) + cfg := plugin.ConfigType{ConfigDataNode: node} + + Convey("testing specific metrics", func() { + mTypes := []plugin.MetricType{ + plugin.MetricType{Namespace_: ns0, Config_: cfg.ConfigDataNode}, + plugin.MetricType{Namespace_: ns1, Config_: cfg.ConfigDataNode}, + plugin.MetricType{Namespace_: ns2, Config_: cfg.ConfigDataNode}, + } + mts, _ := newPlg.CollectMetrics(mTypes) + + for _, mt := range mts { + _, ok := mt.Data_.(string) + So(ok, ShouldBeTrue) + } + }) + + Convey("testing specified 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("without config variables", func() { + + node := cdata.NewNode() + cfg := plugin.ConfigType{ConfigDataNode: node} + + Convey("testing specific metrics", func() { + mTypes := []plugin.MetricType{ + plugin.MetricType{Namespace_: ns0, Config_: cfg.ConfigDataNode}, + plugin.MetricType{Namespace_: ns1, Config_: cfg.ConfigDataNode}, + plugin.MetricType{Namespace_: ns2, Config_: cfg.ConfigDataNode}, + } + mts, _ := newPlg.CollectMetrics(mTypes) + + for _, mt := range mts { + _, ok := mt.Data_.(string) + So(ok, ShouldBeTrue) + } + }) + + Convey("testing specified 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) + } + }) + + }) + }) +} + +func TestGetMetricTypes(t *testing.T) { + Convey("Tesing GetMetricTypes", t, func() { + + newPlg := new(Mock) + So(newPlg, ShouldNotBeNil) + + Convey("with missing on-load plugin config entry", func() { + node := cdata.NewNode() + node.AddItem("test-fail", ctypes.ConfigValueStr{Value: ""}) + + _, err := newPlg.GetMetricTypes(plugin.ConfigType{ConfigDataNode: node}) + + So(err, ShouldNotBeNil) + }) + + Convey("with 'test' config variable", func() { + node := cdata.NewNode() + node.AddItem("test", ctypes.ConfigValueStr{Value: ""}) + + mts, err := newPlg.GetMetricTypes(plugin.ConfigType{ConfigDataNode: node}) + + So(err, ShouldBeNil) + So(len(mts), ShouldEqual, 4) + + Convey("checking namespaces", func() { + metricNames := []string{} + for _, m := range mts { + metricNames = append(metricNames, m.Namespace().String()) + } + + ns := core.NewNamespace("intel", "mock", "test") + So(str.Contains(metricNames, ns.String()), ShouldBeTrue) + + ns = core.NewNamespace("intel", "mock", "foo") + So(str.Contains(metricNames, ns.String()), ShouldBeTrue) + + ns = core.NewNamespace("intel", "mock", "bar") + So(str.Contains(metricNames, ns.String()), ShouldBeTrue) + + ns = core.NewNamespace("intel", "mock").AddDynamicElement("host", "name of the host").AddStaticElement("baz") + So(str.Contains(metricNames, ns.String()), ShouldBeTrue) + }) + }) + + Convey("without config variables", func() { + node := cdata.NewNode() + mts, err := newPlg.GetMetricTypes(plugin.ConfigType{ConfigDataNode: node}) + + So(err, ShouldBeNil) + So(len(mts), ShouldEqual, 3) + + Convey("checking namespaces", func() { + metricNames := []string{} + for _, m := range mts { + metricNames = append(metricNames, m.Namespace().String()) + } + + ns := core.NewNamespace("intel", "mock", "foo") + So(str.Contains(metricNames, ns.String()), ShouldBeTrue) + + ns = core.NewNamespace("intel", "mock", "bar") + So(str.Contains(metricNames, ns.String()), ShouldBeTrue) + + ns = core.NewNamespace("intel", "mock").AddDynamicElement("host", "name of the host").AddStaticElement("baz") + So(str.Contains(metricNames, ns.String()), ShouldBeTrue) + }) + }) + + }) +} + +func TestMeta(t *testing.T) { + Convey("Testing Meta", t, func() { + meta := Meta() + So(meta.Name, ShouldEqual, Name) + So(meta.Version, ShouldEqual, Version) + So(meta.Type, ShouldEqual, Type) + So(meta.AcceptedContentTypes[0], ShouldEqual, plugin.SnapGOBContentType) + So(meta.ReturnedContentTypes[0], ShouldEqual, plugin.SnapGOBContentType) + So(meta.Unsecure, ShouldEqual, true) + So(meta.RoutingStrategy, ShouldEqual, plugin.DefaultRouting) + So(meta.CacheTTL, ShouldEqual, 1100*time.Millisecond) + }) +} + +func TestRandInt(t *testing.T) { + Convey("Testing randInt", t, func() { + rand.Seed(time.Now().UTC().UnixNano()) + data := randInt(65, 90) + So(data, ShouldBeBetween, 64, 91) + }) +} + +func TestGetConfigPolicy(t *testing.T) { + Convey("Testing GetConfigPolicy", t, func() { + newPlg := new(Mock) + So(newPlg, ShouldNotBeNil) + + configPolicy, err := newPlg.GetConfigPolicy() + + So(err, ShouldBeNil) + So(configPolicy, ShouldNotBeNil) + }) +}