|
| 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