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

medium tests for snap-collector-mock1 #946

Merged
merged 1 commit into from
Jun 29, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
221 changes: 221 additions & 0 deletions plugin/collector/snap-collector-mock1/mock/mock_medium_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
// +build medium

/*
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)
})
}