func TestCpuPlugin(t *testing.T) {
	httpmock.Mock = true

	Convey("getcpuTime Should return cputime value", t, func() {

		defer httpmock.ResetResponders()
		httpmock.RegisterResponder("GET", "http://192.168.192.200:8000/trace/count", `{"time_ms": 144123232, "list": []}`, 200)

		cpuTime, err := getCPUTime("http://192.168.192.200:8000")
		So(err, ShouldBeNil)
		So(cpuTime, ShouldEqual, 144123232)

	})
	Convey("CpuStat Should return pluginMetricType Data", t, func() {

		defer httpmock.ResetResponders()
		httpmock.RegisterResponder("GET", "http://192.168.192.200:8000/trace/count", `{"time_ms": 144123232, "list": []}`, 200)

		ns := core.NewNamespace("intel", "osv", "cpu", "cputime")
		cpuTime, err := cpuStat(ns, "http://192.168.192.200:8000")
		So(err, ShouldBeNil)
		So(cpuTime.Namespace(), ShouldResemble, ns)
		So(cpuTime.Data_, ShouldEqual, 144123232)

	})
}
func TestMemPlugin(t *testing.T) {
	httpmock.Mock = true

	Convey("getMemstat Should return memory amount value", t, func() {

		defer httpmock.ResetResponders()
		httpmock.RegisterResponder("GET", "http://192.168.192.200:8000/os/memory/free", "20000", 200)
		httpmock.RegisterResponder("GET", "http://192.168.192.200:8000/os/memory/total", "10000", 200)

		memFree, err := getMemStat("http://192.168.192.200:8000", "free")
		So(err, ShouldBeNil)
		So(memFree, ShouldEqual, 20000)
		memTotal, err := getMemStat("http://192.168.192.200:8000", "total")
		So(err, ShouldBeNil)
		So(memTotal, ShouldEqual, 10000)

	})
	Convey("MemStat Should return pluginMetricType Data", t, func() {

		defer httpmock.ResetResponders()
		httpmock.RegisterResponder("GET", "http://192.168.192.200:8000/os/memory/free", "20000", 200)
		httpmock.RegisterResponder("GET", "http://192.168.192.200:8000/os/memory/total", "10000", 200)

		ns := core.NewNamespace("intel", "osv", "memory", "free")
		ns2 := core.NewNamespace("intel", "osv", "memory", "total")
		memFree, err := memStat(ns, "http://192.168.192.200:8000")
		So(err, ShouldBeNil)
		So(memFree.Namespace(), ShouldResemble, ns)
		So(memFree.Data_, ShouldEqual, 20000)
		memTotal, err := memStat(ns2, "http://192.168.192.200:8000")
		So(err, ShouldBeNil)
		So(memTotal.Namespace(), ShouldResemble, ns2)
		So(memTotal.Data_, ShouldEqual, 10000)

	})
}
func TestTracePlugin(t *testing.T) {
	httpmock.Mock = true

	Convey("getMemstat Should return memory amount value", t, func() {

		defer httpmock.ResetResponders()
		httpmock.RegisterResponder("GET", "http://192.168.192.200:8000/trace/count", `{"time_ms": 144123232, "list": [{"name": "waitqueue_wake_one", "count": 1000}]}`, 200)
		httpmock.RegisterResponder("POST", "http://192.168.192.200:8000/trace/count/waitqueue_wake_one?enabled=True", "", 200)

		trace, err := getTrace("waitqueue_wake_one", "http://192.168.192.200:8000")
		So(err, ShouldBeNil)
		So(trace, ShouldEqual, 1000)

	})
	Convey("MemStat Should return pluginMetricType Data", t, func() {

		defer httpmock.ResetResponders()
		httpmock.RegisterResponder("GET", "http://192.168.192.200:8000/trace/count", `{"time_ms": 144123232, "list": [{"name": "waitqueue_wake_one", "count": 1000}]}`, 200)

		ns := core.NewNamespace("intel", "osv", "trace", "wait", "waitqueue_wake_one")
		memFree, err := traceStat(ns, "http://192.168.192.200:8000")
		So(err, ShouldBeNil)
		So(memFree.Namespace(), ShouldResemble, ns)
		So(memFree.Data_, ShouldEqual, 1000)

	})
	Convey("osvCallRest should return nil", t, func() {

		defer httpmock.ResetResponders()
		httpmock.RegisterResponder("POST", "http://192.168.192.200:8000/trace/count/waitqueue_wake_one?enabled=True", "", 200)

		resp := osvRestPost("http://192.168.192.200:8000", "trace/count/waitqueue_wake_one?enabled=True")
		So(resp, ShouldBeNil)

	})
}
func TestLibirtPlugin(t *testing.T) {
	httpmock.Mock = true

	Convey("Meta should return metadata for the plugin", t, func() {
		meta := Meta()
		So(meta.Name, ShouldResemble, Name)
		So(meta.Version, ShouldResemble, Version)
		So(meta.Type, ShouldResemble, plugin.CollectorPluginType)
	})

	Convey("Create Osv Collector", t, func() {
		osvCol := NewOsvCollector()
		Convey("So psCol should not be nil", func() {
			So(osvCol, ShouldNotBeNil)
		})
		Convey("So psCol should be of Osv type", func() {
			So(osvCol, ShouldHaveSameTypeAs, &Osv{})
		})
		Convey("osvCol.GetConfigPolicy() should return a config policy", func() {
			configPolicy, _ := osvCol.GetConfigPolicy()
			Convey("So config policy should not be nil", func() {
				So(configPolicy, ShouldNotBeNil)
			})
			Convey("So config policy should be a cpolicy.ConfigPolicy", func() {
				So(configPolicy, ShouldHaveSameTypeAs, &cpolicy.ConfigPolicy{})
			})
		})
	})
	Convey("Get URI ", t, func() {
		Convey("So should return 10.1.0.1:8000", func() {
			swagIP := "10.1.0.1"
			swagPort := 8000
			uri := osvRestURL(swagIP, swagPort)
			So("http://10.1.0.1:8000", ShouldResemble, uri)
		})
	})
	Convey("Get Metrics ", t, func() {
		osvCol := NewOsvCollector()
		cfgNode := cdata.NewNode()
		var cfg = plugin.ConfigType{
			ConfigDataNode: cfgNode,
		}
		Convey("So should return 187 types of metrics", func() {
			metrics, err := osvCol.GetMetricTypes(cfg)
			So(187, ShouldResemble, len(metrics))
			So(err, ShouldBeNil)
		})
		Convey("So should check namespace", func() {
			metrics, err := osvCol.GetMetricTypes(cfg)
			waitNamespace := metrics[0].Namespace().String()
			wait := regexp.MustCompile(`^/intel/osv/trace/virtio/virtio_wait_for_queue`)
			So(true, ShouldEqual, wait.MatchString(waitNamespace))
			So(err, ShouldBeNil)

		})

	})
	Convey("Get Metrics ", t, func() {
		osvCol := NewOsvCollector()
		cfgNode := cdata.NewNode()
		cfgNode.AddItem("swagIP", ctypes.ConfigValueStr{Value: "192.168.192.200"})
		cfgNode.AddItem("swagPort", ctypes.ConfigValueInt{Value: 8000})

		defer httpmock.ResetResponders()
		httpmock.RegisterResponder("GET", "http://192.168.192.200:8000/os/memory/free", "20000", 200)
		httpmock.RegisterResponder("GET", "http://192.168.192.200:8000/trace/count",
			`{"time_ms": 144123232, "list": [{"name": "waitqueue_wake_one", "count": 1000}]}`, 200)

		Convey("So should get memory metrics", func() {
			metrics := []plugin.MetricType{{
				Namespace_: core.NewNamespace("intel", "osv", "memory", "free"),
				Config_:    cfgNode,
			}}
			collect, err := osvCol.CollectMetrics(metrics)
			So(err, ShouldBeNil)
			So(collect[0].Data_, ShouldNotBeNil)
			So(len(collect), ShouldResemble, 1)

		})
		Convey("So should get cpu metrics", func() {
			metrics := []plugin.MetricType{{
				Namespace_: core.NewNamespace("intel", "osv", "cpu", "cputime"),
				Config_:    cfgNode,
			}}
			collect, err := osvCol.CollectMetrics(metrics)
			So(err, ShouldBeNil)
			So(collect[0].Data_, ShouldNotBeNil)
			So(collect[0].Data_, ShouldEqual, 144123232)
			So(len(collect), ShouldEqual, 1)

		})
		Convey("So should get trace metrics", func() {
			metrics := []plugin.MetricType{{
				Namespace_: core.NewNamespace("intel", "osv", "trace", "wait", "waitqueue_wake_one"),
				Config_:    cfgNode,
			}}
			collect, err := osvCol.CollectMetrics(metrics)
			So(err, ShouldBeNil)
			So(collect[0].Data_, ShouldNotBeNil)
			So(collect[0].Data_, ShouldEqual, 1000)
			So(len(collect), ShouldEqual, 1)

		})

	})
}