コード例 #1
0
ファイル: mock.go プロジェクト: IRCody/snap
// GetMetricTypes returns metric types for testing
func (f *Mock) GetMetricTypes(cfg plugin.Config) ([]plugin.Metric, error) {
	mts := []plugin.Metric{}
	if _, err := cfg.GetBool("test-fail"); err == nil {
		return mts, fmt.Errorf("testing")
	}
	if _, err := cfg.GetBool("test"); err == nil {
		mts = append(mts, plugin.Metric{
			Namespace:   plugin.NewNamespace("intel", "mock", "test"),
			Description: "mock description",
			Unit:        "mock unit",
		})
	}
	if _, err := cfg.GetBool("test-less"); err != nil {
		mts = append(mts, plugin.Metric{
			Namespace:   plugin.NewNamespace("intel", "mock", "foo"),
			Description: "mock description",
			Unit:        "mock unit",
		})
	}
	mts = append(mts, plugin.Metric{
		Namespace:   plugin.NewNamespace("intel", "mock", "bar"),
		Description: "mock description",
		Unit:        "mock unit",
	})
	mts = append(mts, plugin.Metric{
		Namespace: plugin.NewNamespace("intel", "mock").
			AddDynamicElement("host", "name of the host").
			AddStaticElement("baz"),
		Description: "mock description",
		Unit:        "mock unit",
	})
	return mts, nil
}
コード例 #2
0
// GetMetricTypes returns metric types that can be collected
func (LibvirtCollector) GetMetricTypes(cfg plugin.Config) ([]plugin.Metric, error) {

	var metrics []plugin.Metric

	ns := plugin.NewNamespace(Vendor, Plugin).
		AddDynamicElement("domain_id", "an id of libvirt domain")

	for _, value := range nsTypes.cpu {
		metrics = append(metrics, createMetric(ns.AddStaticElements("cpu", value)))
		metrics = append(metrics, createMetric(ns.AddStaticElement("cpu").AddDynamicElement("cpu_id", "id of vcpu").AddStaticElement(value)))

	}
	for _, value := range nsTypes.disk {
		metrics = append(metrics, createMetric(
			ns.AddStaticElements("disk").AddDynamicElement("device_name", "a name of filesystem device").AddStaticElement(value)))
	}
	for _, value := range nsTypes.network {
		metrics = append(metrics, createMetric(
			ns.AddStaticElements("network").AddDynamicElement("network_interface", "a name of network interface").AddStaticElement(value)))
	}
	for _, value := range nsTypes.memory {
		metrics = append(metrics, createMetric(ns.AddStaticElements("memory").AddStaticElement(value)))
	}
	return metrics, nil
}
コード例 #3
0
ファイル: file_small_test.go プロジェクト: IRCody/snap
func TestFilePublish(t *testing.T) {
	metrics := []plugin.Metric{plugin.Metric{Namespace: plugin.NewNamespace("foo"), Timestamp: time.Now(), Data: 99, Tags: nil}}
	config := plugin.Config{"file": "/tmp/pub.out"}
	Convey("TestFilePublish", t, func() {
		fp := NewFilePublisher()
		So(fp, ShouldNotBeNil)
		err := fp.Publish(metrics, config)
		So(err, ShouldBeNil)
		filename, _ := config.GetString("file")
		_, err = os.Stat(filename)
		So(err, ShouldBeNil)
	})
}
コード例 #4
0
ファイル: mock_medium_test.go プロジェクト: IRCody/snap
func TestCollectMetric(t *testing.T) {
	ns0 := plugin.NewNamespace("intel", "mock", "test")
	ns1 := plugin.NewNamespace("intel", "mock", "foo")
	ns2 := plugin.NewNamespace("intel", "mock", "bar")
	ns3 := plugin.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() {

			cfg := plugin.Config{"test": true}

			Convey("testing specific metrics", func() {
				mTypes := []plugin.Metric{
					plugin.Metric{Namespace: ns0, Config: cfg},
					plugin.Metric{Namespace: ns1, Config: cfg},
					plugin.Metric{Namespace: ns2, Config: cfg},
				}
				mts, _ := newPlg.CollectMetrics(mTypes)

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

			Convey("testing dynamic metric", func() {

				mt := plugin.Metric{Namespace: ns3, Config: cfg}
				isDynamic, _ := mt.Namespace.IsDynamic()
				So(isDynamic, ShouldBeTrue)

				Convey("for none specified instance", func() {
					mts, _ := newPlg.CollectMetrics([]plugin.Metric{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.Metric{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.Metric{mt})

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

				})
			})

		})

		Convey("without config variables", func() {

			cfg := plugin.Config{}

			Convey("testing specific metrics", func() {
				mTypes := []plugin.Metric{
					plugin.Metric{Namespace: ns0, Config: cfg},
					plugin.Metric{Namespace: ns1, Config: cfg},
					plugin.Metric{Namespace: ns2, Config: cfg},
				}
				mts, _ := newPlg.CollectMetrics(mTypes)

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

			Convey("testing dynamic metics", func() {
				mTypes := []plugin.Metric{
					plugin.Metric{Namespace: ns3, Config: cfg},
				}
				mts, _ := newPlg.CollectMetrics(mTypes)

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

			})

		})

	})
}
コード例 #5
0
ファイル: mock_medium_test.go プロジェクト: IRCody/snap
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() {
			cfg := plugin.Config{"test-fail": true}
			_, err := newPlg.GetMetricTypes(cfg)

			So(err, ShouldNotBeNil)
		})

		Convey("with 'test' config variable", func() {
			cfg := plugin.Config{"test": true}

			mts, err := newPlg.GetMetricTypes(cfg)

			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 := plugin.NewNamespace("intel", "mock", "test")
				So(str.Contains(metricNames, ns.String()), ShouldBeTrue)

				ns = plugin.NewNamespace("intel", "mock", "foo")
				So(str.Contains(metricNames, ns.String()), ShouldBeTrue)

				ns = plugin.NewNamespace("intel", "mock", "bar")
				So(str.Contains(metricNames, ns.String()), ShouldBeTrue)

				ns = plugin.NewNamespace("intel", "mock").AddDynamicElement("host", "name of the host").AddStaticElement("baz")
				So(str.Contains(metricNames, ns.String()), ShouldBeTrue)
			})
		})

		Convey("without config variables", func() {
			mts, err := newPlg.GetMetricTypes(plugin.Config{})

			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 := plugin.NewNamespace("intel", "mock", "foo")
				So(str.Contains(metricNames, ns.String()), ShouldBeTrue)

				ns = plugin.NewNamespace("intel", "mock", "bar")
				So(str.Contains(metricNames, ns.String()), ShouldBeTrue)

				ns = plugin.NewNamespace("intel", "mock").AddDynamicElement("host", "name of the host").AddStaticElement("baz")
				So(str.Contains(metricNames, ns.String()), ShouldBeTrue)
			})
		})

	})
}
func TestLibirtPlugin(t *testing.T) {

	Convey("Create Libvirt Collector", t, func() {
		libvirtCol := LibvirtCollector{}
		Convey("So psCol should not be nil", func() {
			So(libvirtCol, ShouldNotBeNil)
		})
		Convey("So psCol should be of Libvirt type", func() {
			So(libvirtCol, ShouldHaveSameTypeAs, LibvirtCollector{})
		})
		Convey("libvirtCol.GetConfigPolicy() should return a config policy", func() {
			configPolicy, _ := libvirtCol.GetConfigPolicy()
			Convey("So config policy should not be nil", func() {
				So(configPolicy, ShouldNotBeNil)
			})
			Convey("So config policy should be a plugin.ConfigPolicy", func() {
				So(configPolicy, ShouldHaveSameTypeAs, plugin.ConfigPolicy{})
			})
		})
	})
	Convey("Get Metric Types", t, func() {
		libvirtCol := LibvirtCollector{}
		var cfg = plugin.Config{}
		metrics, err := libvirtCol.GetMetricTypes(cfg)
		So(err, ShouldBeNil)
		So(len(metrics), ShouldResemble, 25)
	})
	Convey("Collect Metrics", t, func() {
		libvirtCol := LibvirtCollector{}

		config := plugin.Config{
			"uri":  "test:///default",
			"nova": false,
		}
		mts := []plugin.Metric{}

		mts = append(mts, plugin.Metric{Namespace: plugin.NewNamespace(Vendor, Plugin).AddDynamicElement("domain_id", "an id of libvirt domain").AddStaticElements("cpu", "cputime"), Config: config})

		metrics, err := libvirtCol.CollectMetrics(mts)
		So(err, ShouldBeNil)
		So(len(metrics), ShouldResemble, 1)
		So(metrics[0].Data, ShouldNotBeNil)
		So(metrics[0].Namespace.Strings()[2], ShouldResemble, "test")

	})
	Convey("Collect Vcpu Metrics", t, func() {
		libvirtCol := LibvirtCollector{}

		config := plugin.Config{
			"uri":  "test:///default",
			"nova": false,
		}
		mts := []plugin.Metric{}

		mts = append(mts, plugin.Metric{Namespace: plugin.NewNamespace(Vendor, Plugin).AddDynamicElement("domain_id", "an id of libvirt domain").AddStaticElement("cpu").AddDynamicElement("cpu_id", "id of vcpu").AddStaticElement("cputime"), Config: config})

		metrics, err := libvirtCol.CollectMetrics(mts)
		So(err, ShouldBeNil)
		So(len(metrics), ShouldResemble, 2)
		So(metrics[0].Data, ShouldNotBeNil)
		secondLastElement := len(metrics[0].Namespace.Strings()) - 2
		So(metrics[0].Namespace.Strings()[secondLastElement], ShouldBeIn, []string{"0", "1"})
		So(metrics[0].Namespace.Strings()[2], ShouldResemble, "test")
		So(metrics[1].Namespace.Strings()[secondLastElement], ShouldBeIn, []string{"0", "1"})
		So(metrics[1].Namespace.Strings()[2], ShouldResemble, "test")

	})

	Convey("Merge two map[string]string", t, func() {
		one := make(map[string]string)
		two := make(map[string]string)

		one["test"] = "test"
		two["test1"] = "test"

		three := merge(one, two)

		result := make(map[string]string)
		result["test"] = "test"
		result["test1"] = "test"

		So(result, ShouldResemble, three)

	})
	Convey("copy namespace", t, func() {

		mt := plugin.Metric{Namespace: plugin.NewNamespace(Vendor, Plugin).AddDynamicElement("domain_id", "an id of libvirt domain").AddStaticElements("cpu", "cputime")}
		ns := copyNamespace(mt)
		result := []plugin.NamespaceElement{plugin.NamespaceElement{Value: "intel", Description: "", Name: ""}, plugin.NamespaceElement{Value: "libvirt", Description: "", Name: ""}, plugin.NamespaceElement{Value: "*", Description: "an id of libvirt domain", Name: "domain_id"}, plugin.NamespaceElement{Value: "cpu", Description: "", Name: ""}, plugin.NamespaceElement{Value: "cputime", Description: "", Name: ""}}

		So(ns, ShouldResemble, result)

	})

	Convey("filter Namespace", t, func() {
		mts := []plugin.Metric{
			plugin.Metric{Namespace: plugin.NewNamespace(Vendor, Plugin).AddDynamicElement("domain_id", "an id of libvirt domain").AddStaticElements("cpu", "cputime")},
		}

		countResult, result := filterNamespace("cpu", mts)
		So(mts, ShouldResemble, result)
		So(countResult, ShouldResemble, 1)

		countResult, result2 := filterNamespace("memory", mts)
		expectedType := []plugin.Metric{}
		So(result2, ShouldHaveSameTypeAs, expectedType)
		So(countResult, ShouldResemble, 0)

	})

}