func Test_getCephBinaryPath(t *testing.T) { path := "/path/to/ceph/bin" Convey("path Ceph executable in Snap Global Config", t, func() { cmd = &TestCmd{} cfg := plugin.NewPluginConfigType() cfg.AddItem("path", ctypes.ConfigValueStr{Value: path}) So(func() { getCephBinaryPath(cfg.Table()) }, ShouldNotPanic) result := getCephBinaryPath(cfg.Table()) So(result, ShouldEqual, path) }) Convey("looking for Ceph executable in $PATH with no error", t, func() { cfg := plugin.NewPluginConfigType() cmd = &TestCmd{look_path: path} So(func() { getCephBinaryPath(cfg.Table()) }, ShouldNotPanic) result := getCephBinaryPath(cfg.Table()) So(result, ShouldEqual, path) }) Convey("looking for Ceph executable in $PATH with error", t, func() { cfg := plugin.NewPluginConfigType() cmd = &TestCmd{look_path: path, look_err: errors.New("error")} So(func() { getCephBinaryPath(cfg.Table()) }, ShouldNotPanic) result := getCephBinaryPath(cfg.Table()) // getting default path to ceph executable So(result, ShouldEqual, cephBinPathDefault) }) }
func TestGetMetricTypes(t *testing.T) { // Testing GetMetricTypes function covers checking initialization of Ceph plugin // mock ceph socket os.Mkdir("test", os.ModePerm) os.Create("test/ceph-mds.a.asok") os.Create("test/ceph-mds.b.asok") os.Create("test/ceph-mds.c.asok") // set ceph socket conf value cfg := plugin.NewPluginConfigType() cfg.AddItem("socket_path", ctypes.ConfigValueStr{Value: "./test"}) cfg.AddItem("socket_prefix", ctypes.ConfigValueStr{Value: "ceph-"}) cfg.AddItem("socket_ext", ctypes.ConfigValueStr{Value: "asok"}) Convey("init ceph plugin and getting metric namespace", t, func() { ceph := &Ceph{} cmd = &TestCmd{out: mockOut} So(func() { ceph.GetMetricTypes(cfg) }, ShouldNotPanic) result, err := ceph.GetMetricTypes(cfg) So(result, ShouldNotBeNil) So(err, ShouldBeNil) }) Convey("invalid path to ceph-daemon sockets", t, func() { ceph := &Ceph{} cmd = &TestCmd{out: mockOut} cfg_invalid := plugin.NewPluginConfigType() cfg_invalid.AddItem("socket_path", ctypes.ConfigValueStr{Value: "./test_invalid"}) cfg_invalid.AddItem("socket_prefix", ctypes.ConfigValueStr{Value: "ceph-"}) cfg_invalid.AddItem("socket_ext", ctypes.ConfigValueStr{Value: "asok"}) So(func() { ceph.Init(cfg_invalid.Table()) }, ShouldPanic) }) Convey("no ceph-daemon sockets available with set prefix", t, func() { ceph := &Ceph{} cmd = &TestCmd{out: mockOut} cfg_inv_prefix := plugin.NewPluginConfigType() cfg_inv_prefix.AddItem("socket_path", ctypes.ConfigValueStr{Value: "./test"}) cfg_inv_prefix.AddItem("socket_prefix", ctypes.ConfigValueStr{Value: "ceph_inv-"}) So(func() { ceph.Init(cfg_inv_prefix.Table()) }, ShouldNotPanic) err := ceph.Init(cfg_inv_prefix.Table()) So(err, ShouldNotBeNil) }) Convey("no ceph-daemon sockets available with set extension", t, func() { ceph := &Ceph{} cmd = &TestCmd{out: mockOut} cfg_inv_ext := plugin.NewPluginConfigType() cfg_inv_ext.AddItem("socket_path", ctypes.ConfigValueStr{Value: "./test"}) cfg_inv_ext.AddItem("socket_ext", ctypes.ConfigValueStr{Value: "asok_inv"}) So(func() { ceph.Init(cfg_inv_ext.Table()) }, ShouldNotPanic) err := ceph.Init(cfg_inv_ext.Table()) So(err, ShouldNotBeNil) }) os.RemoveAll("test/") }
func TestGetGlobalConfigItem(t *testing.T) { Convey("Get a value of item from Global Config with no error", t, func() { //create global config and add item (different types) cfg := plugin.NewPluginConfigType() cfg.AddItem("dummy_string", ctypes.ConfigValueStr{Value: dummy_str}) cfg.AddItem("dummy_bool", ctypes.ConfigValueBool{Value: dummy_bool}) cfg.AddItem("dummy_int", ctypes.ConfigValueInt{Value: dummy_int}) cfg.AddItem("dummy_float", ctypes.ConfigValueFloat{Value: dummy_float}) Convey("string type of item", func() { result, err := GetGlobalConfigItem(cfg, "dummy_string") So(err, ShouldBeNil) So(result, ShouldEqual, dummy_str) }) Convey("bool type of item", func() { result, err := GetGlobalConfigItem(cfg, "dummy_bool") So(err, ShouldBeNil) So(result, ShouldEqual, dummy_bool) }) Convey("int type of item", func() { result, err := GetGlobalConfigItem(cfg, "dummy_int") So(err, ShouldBeNil) So(result, ShouldEqual, dummy_int) }) Convey("float type of item", func() { result, err := GetGlobalConfigItem(cfg, "dummy_float") So(err, ShouldBeNil) So(result, ShouldEqual, dummy_float) }) }) Convey("Try to get value of item not defined in Global Config", t, func() { cfg := plugin.NewPluginConfigType() cfg.AddItem("foo", ctypes.ConfigValueStr{Value: "foo"}) result, err := GetGlobalConfigItem(cfg, "foo_not_exist") So(err, ShouldNotBeNil) So(result, ShouldBeNil) }) Convey("No item defined in Global Config", t, func() { cfg_empty := plugin.NewPluginConfigType() result, err := GetGlobalConfigItem(cfg_empty, "foo") So(err, ShouldNotBeNil) So(result, ShouldBeNil) }) }
func testingConfig() (cfg1 plugin.ConfigType, cfg2 *cdata.ConfigDataNode) { cfg1 = plugin.NewPluginConfigType() cfg2 = cdata.NewNode() cfg1.AddItem("openstack_user", ctypes.ConfigValueStr{Value: "x"}) cfg1.AddItem("openstack_pass", ctypes.ConfigValueStr{Value: "x"}) cfg1.AddItem("openstack_tenant", ctypes.ConfigValueStr{Value: "asdf"}) cfg1.AddItem("openstack_auth_url", ctypes.ConfigValueStr{Value: "x"}) cfg2.AddItem("openstack_user", ctypes.ConfigValueStr{Value: "x"}) cfg2.AddItem("openstack_pass", ctypes.ConfigValueStr{Value: "x"}) cfg2.AddItem("openstack_tenant", ctypes.ConfigValueStr{Value: "asdf"}) cfg2.AddItem("openstack_auth_url", ctypes.ConfigValueStr{Value: "x"}) cfg1.AddItem("allocation_ratio_cores", ctypes.ConfigValueFloat{Value: 3}) cfg1.AddItem("allocation_ratio_ram", ctypes.ConfigValueFloat{Value: 4}) cfg1.AddItem("reserved_node_cores", ctypes.ConfigValueFloat{Value: 5}) cfg1.AddItem("reserved_node_ram_mb", ctypes.ConfigValueFloat{Value: 6}) cfg2.AddItem("allocation_ratio_cores", ctypes.ConfigValueFloat{Value: 3}) cfg2.AddItem("allocation_ratio_ram", ctypes.ConfigValueFloat{Value: 4}) cfg2.AddItem("reserved_node_cores", ctypes.ConfigValueFloat{Value: 5}) cfg2.AddItem("reserved_node_ram_mb", ctypes.ConfigValueFloat{Value: 6}) return cfg1, cfg2 }
func Test_getCephBinaryPath(t *testing.T) { path := "/path/to/ceph/bin" ceph := New() Convey("path Ceph executable in Snap Global Config", t, func() { cmd = &TestCmd{} cfg := plugin.NewPluginConfigType() cfg.AddItem("path", ctypes.ConfigValueStr{Value: path}) So(func() { getCephBinaryPath(cfg.Table()) }, ShouldNotPanic) result := getCephBinaryPath(cfg.Table()) So(result, ShouldEqual, path) }) Convey("looking for Ceph executable default path", t, func() { cp, _ := ceph.GetConfigPolicy() cfgRules := cp.Get([]string{"intel", "storage", "ceph"}).RulesAsTable() for i := range cfgRules { switch cfgRules[i].Name { case "path": So(cfgRules[i].Default.(*ctypes.ConfigValueStr).Value, ShouldEqual, cephBinPathDefault) } } }) }
func Test_getSocketConf(t *testing.T) { ceph := New() Convey("check Ceph socket default config", t, func() { cmd = &TestCmd{} cp, _ := ceph.GetConfigPolicy() cfgRules := cp.Get([]string{"intel", "storage", "ceph"}).RulesAsTable() for i := range cfgRules { switch cfgRules[i].Name { case "socket_path": So(cfgRules[i].Default.(*ctypes.ConfigValueStr).Value, ShouldEqual, socketPathDefault) case "socket_prefix": So(cfgRules[i].Default.(*ctypes.ConfigValueStr).Value, ShouldEqual, socketPrefixDefault) case "socket_ext": So(cfgRules[i].Default.(*ctypes.ConfigValueStr).Value, ShouldEqual, socketExtDefault) } } }) Convey("customize Ceph socket conf in Snap Global Conf", t, func() { path := "path/to/ceph/socket" prefix := "test-" extension := "asdf" cmd = &TestCmd{} cfg := plugin.NewPluginConfigType() cfg.AddItem("socket_path", ctypes.ConfigValueStr{Value: path}) cfg.AddItem("socket_prefix", ctypes.ConfigValueStr{Value: prefix}) cfg.AddItem("socket_ext", ctypes.ConfigValueStr{Value: extension}) So(func() { getCephSocketConf(cfg.Table()) }, ShouldNotPanic) result := getCephSocketConf(cfg.Table()) So(result.path, ShouldEqual, path) So(result.prefix, ShouldEqual, prefix) So(result.ext, ShouldEqual, extension) }) Convey("customize Ceph socket prefix, set none", t, func() { cmd = &TestCmd{} cfg := plugin.NewPluginConfigType() cfg.AddItem("socket_prefix", ctypes.ConfigValueStr{Value: "none"}) So(func() { getCephSocketConf(cfg.Table()) }, ShouldNotPanic) result := getCephSocketConf(cfg.Table()) So(result.prefix, ShouldBeEmpty) }) }
func getMockPluginConfig() plugin.ConfigType { // mocking global config cfg := plugin.NewPluginConfigType() cfg.AddItem("host", ctypes.ConfigValueStr{Value: "hostname"}) cfg.AddItem("port", ctypes.ConfigValueInt{Value: 1234}) cfg.AddItem("user", ctypes.ConfigValueStr{Value: "test"}) cfg.AddItem("password", ctypes.ConfigValueStr{Value: "passwd"}) return cfg }
func Test_getSocketConf(t *testing.T) { Convey("defaults Ceph socket details", t, func() { cmd = &TestCmd{} cfg := plugin.NewPluginConfigType() So(func() { getCephSocketConf(cfg.Table()) }, ShouldNotPanic) result := getCephSocketConf(cfg.Table()) So(result.path, ShouldEqual, socketPathDefault) So(result.prefix, ShouldEqual, socketPrefixDefault) So(result.ext, ShouldEqual, socketExtDefault) }) Convey("customize Ceph socket conf in Snap Global Conf", t, func() { path := "path/to/ceph/socket" prefix := "test-" extension := "asdf" cmd = &TestCmd{} cfg := plugin.NewPluginConfigType() cfg.AddItem("socket_path", ctypes.ConfigValueStr{Value: path}) cfg.AddItem("socket_prefix", ctypes.ConfigValueStr{Value: prefix}) cfg.AddItem("socket_ext", ctypes.ConfigValueStr{Value: extension}) So(func() { getCephSocketConf(cfg.Table()) }, ShouldNotPanic) result := getCephSocketConf(cfg.Table()) So(result.path, ShouldEqual, path) So(result.prefix, ShouldEqual, prefix) So(result.ext, ShouldEqual, extension) }) Convey("customize Ceph socket prefix, set none", t, func() { cmd = &TestCmd{} cfg := plugin.NewPluginConfigType() cfg.AddItem("socket_prefix", ctypes.ConfigValueStr{Value: "none"}) So(func() { getCephSocketConf(cfg.Table()) }, ShouldNotPanic) result := getCephSocketConf(cfg.Table()) So(result.prefix, ShouldBeEmpty) }) }
func TestGetGlobalConfigItems(t *testing.T) { Convey("Get values of items from Global Config with no error", t, func() { //create global config and add item (different types) cfg := plugin.NewPluginConfigType() cfg.AddItem("dummy_string", ctypes.ConfigValueStr{Value: dummy_str}) cfg.AddItem("dummy_bool", ctypes.ConfigValueBool{Value: dummy_bool}) cfg.AddItem("dummy_int", ctypes.ConfigValueInt{Value: dummy_int}) cfg.AddItem("dummy_float", ctypes.ConfigValueFloat{Value: dummy_float}) names := []string{"dummy_string", "dummy_bool", "dummy_int", "dummy_float"} result, err := GetGlobalConfigItems(cfg, names) So(err, ShouldBeNil) for _, name := range names { So(result[name], ShouldNotBeEmpty) } }) Convey("Try to get values of items not defined in Global Config", t, func() { cfg := plugin.NewPluginConfigType() cfg.AddItem("foo", ctypes.ConfigValueStr{Value: "foo"}) names := []string{"foo1", "foo2"} result, err := GetGlobalConfigItems(cfg, names) So(err, ShouldNotBeNil) So(result, ShouldBeNil) }) Convey("No item defined in Global Config", t, func() { cfg_empty := plugin.NewPluginConfigType() names := []string{"foo", "bar"} result, err := GetGlobalConfigItems(cfg_empty, names) So(err, ShouldNotBeNil) So(result, ShouldBeNil) }) }
func TestConfigItems(t *testing.T) { names := []string{"dummy_string", "dummy_bool", "dummy_int", "dummy_float"} Convey("Get values of configuration items with no error", t, func() { Convey("Source: global config", func() { //create global config and add item (different types) cfg := plugin.NewPluginConfigType() cfg.AddItem("dummy_string", ctypes.ConfigValueStr{Value: dummy_str}) cfg.AddItem("dummy_bool", ctypes.ConfigValueBool{Value: dummy_bool}) cfg.AddItem("dummy_int", ctypes.ConfigValueInt{Value: dummy_int}) cfg.AddItem("dummy_float", ctypes.ConfigValueFloat{Value: dummy_float}) result, err := GetConfigItems(cfg, names...) So(err, ShouldBeNil) for _, name := range names { So(result[name], ShouldNotBeEmpty) } }) Convey("Source: metrics config", func() { // create metrics config config := cdata.NewNode() config.AddItem("dummy_string", ctypes.ConfigValueStr{Value: dummy_str}) config.AddItem("dummy_bool", ctypes.ConfigValueBool{Value: dummy_bool}) config.AddItem("dummy_int", ctypes.ConfigValueInt{Value: dummy_int}) config.AddItem("dummy_float", ctypes.ConfigValueFloat{Value: dummy_float}) // create metric and set config metric := plugin.MetricType{} metric.Config_ = config result, err := GetConfigItems(metric, names...) So(err, ShouldBeNil) for _, name := range names { So(result[name], ShouldNotBeEmpty) } }) }) Convey("Try to get values of items from invalid config (unsupported type)", t, func() { invalid_cfg := []string{"invalid", "config", "source"} result, err := GetConfigItems(invalid_cfg, names...) So(err, ShouldNotBeNil) So(result, ShouldBeNil) }) }
func TestGetMetricTypes(t *testing.T) { Convey("When having two devices with known smart attribute", t, func() { Convey("And system lets you to list devices", func() { provider := &fakeSysutilProvider2{} orgProvider := sysUtilProvider sysUtilProvider = provider collector := SmartCollector{ logger: log.New(), initializedMutex: new(sync.Mutex), proc_path: "/proc", dev_path: "/dev", } Convey("Both devices should be present in metric list", func() { new_hier, is_dynamic := false, false metrics, err := collector.GetMetricTypes(plugin.NewPluginConfigType()) So(err, ShouldBeNil) for _, m := range metrics { switch m.Namespace().Strings()[2] { case "smart": new_hier = true } switch m.Namespace().Strings()[3] { case "*": is_dynamic = true } } So(new_hier, ShouldBeTrue) So(is_dynamic, ShouldBeTrue) }) Reset(func() { sysUtilProvider = orgProvider }) }) }) }
func TestConfigItem(t *testing.T) { Convey("Get a value of item with no error", t, func() { Convey("Source: global config", func() { //create global config and add item (different types) cfg := plugin.NewPluginConfigType() cfg.AddItem("dummy_string", ctypes.ConfigValueStr{Value: dummy_str}) cfg.AddItem("dummy_bool", ctypes.ConfigValueBool{Value: dummy_bool}) cfg.AddItem("dummy_int", ctypes.ConfigValueInt{Value: dummy_int}) cfg.AddItem("dummy_float", ctypes.ConfigValueFloat{Value: dummy_float}) Convey("string type of item", func() { result, err := GetConfigItem(cfg, "dummy_string") So(err, ShouldBeNil) So(result, ShouldEqual, dummy_str) }) Convey("bool type of item", func() { result, err := GetConfigItem(cfg, "dummy_bool") So(err, ShouldBeNil) So(result, ShouldEqual, dummy_bool) }) Convey("int type of item", func() { result, err := GetConfigItem(cfg, "dummy_int") So(err, ShouldBeNil) So(result, ShouldEqual, dummy_int) }) Convey("float type of itemr", func() { result, err := GetConfigItem(cfg, "dummy_float") So(err, ShouldBeNil) So(result, ShouldEqual, dummy_float) }) }) Convey("Source: metrics config", func() { // create metric's config config := cdata.NewNode() config.AddItem("dummy_string", ctypes.ConfigValueStr{Value: dummy_str}) config.AddItem("dummy_bool", ctypes.ConfigValueBool{Value: dummy_bool}) config.AddItem("dummy_int", ctypes.ConfigValueInt{Value: dummy_int}) config.AddItem("dummy_float", ctypes.ConfigValueFloat{Value: dummy_float}) // create metric and set config metric := plugin.MetricType{} metric.Config_ = config Convey("string type of item", func() { result, err := GetConfigItem(metric, "dummy_string") So(err, ShouldBeNil) So(result, ShouldEqual, dummy_str) }) Convey("bool type of item", func() { result, err := GetConfigItem(metric, "dummy_bool") So(err, ShouldBeNil) So(result, ShouldEqual, dummy_bool) }) Convey("int type of item", func() { result, err := GetConfigItem(metric, "dummy_int") So(err, ShouldBeNil) So(result, ShouldEqual, dummy_int) }) Convey("float type of item", func() { result, err := GetConfigItem(metric, "dummy_float") So(err, ShouldBeNil) So(result, ShouldEqual, dummy_float) }) }) }) Convey("Try to get a value of item not defined in config", t, func() { Convey("Source: metrics config", func() { config := cdata.NewNode() config.AddItem("foo", ctypes.ConfigValueStr{Value: "foo_val"}) metric := plugin.MetricType{} metric.Config_ = config result, err := GetMetricConfigItem(metric, "foo_not_exist") So(err, ShouldNotBeNil) So(result, ShouldBeNil) }) Convey("Source: global config", func() { cfg := plugin.NewPluginConfigType() cfg.AddItem("foo", ctypes.ConfigValueStr{Value: "foo"}) names := []string{"foo1", "foo2"} result, err := GetGlobalConfigItems(cfg, names) So(err, ShouldNotBeNil) So(result, ShouldBeNil) }) }) Convey("No item defined in config", t, func() { Convey("Source: metrics config", func() { metric := plugin.MetricType{} metric.Config_ = cdata.NewNode() result, err := GetMetricConfigItem(metric, "foo") So(err, ShouldNotBeNil) So(result, ShouldBeNil) }) Convey("Source: global config", func() { cfg_empty := plugin.NewPluginConfigType() names := []string{"foo", "bar"} result, err := GetGlobalConfigItems(cfg_empty, names) So(err, ShouldNotBeNil) So(result, ShouldBeNil) }) }) Convey("Try to get a value of item from invalid config (unsupported type)", t, func() { invalid_cfg := []string{"invalid", "config", "source"} result, err := GetConfigItem(invalid_cfg, "foo") So(err, ShouldNotBeNil) So(result, ShouldBeNil) }) }
func TestHTTPJSONRPC(t *testing.T) { log.SetLevel(log.DebugLevel) addr, session := startHTTPJSONRPC() time.Sleep(time.Millisecond * 100) Convey("Collector Client", t, func() { session.c = true c, err := NewCollectorHttpJSONRPCClient(fmt.Sprintf("http://%v", addr), 1*time.Second, &key.PublicKey, true) So(err, ShouldBeNil) So(c, ShouldNotBeNil) cl := c.(*httpJSONRPCClient) cl.encrypter.Key = symkey Convey("Ping", func() { err := c.Ping() So(err, ShouldBeNil) }) Convey("Kill", func() { err := c.Kill("somereason") So(err, ShouldBeNil) }) Convey("GetMetricTypes", func() { cfg := plugin.NewPluginConfigType() cfg.AddItem("test", ctypes.ConfigValueBool{Value: true}) mts, err := c.GetMetricTypes(cfg) So(err, ShouldBeNil) So(mts, ShouldNotBeNil) So(mts, ShouldHaveSameTypeAs, []core.Metric{}) So(len(mts), ShouldBeGreaterThan, 0) So(len(mts[0].Config().Table()), ShouldBeGreaterThan, 0) }) Convey("CollectMetrics provided a valid config", func() { cdn := cdata.NewNode() cdn.AddItem("someInt", ctypes.ConfigValueInt{Value: 1}) cdn.AddItem("password", ctypes.ConfigValueStr{Value: "secure"}) time.Sleep(500 * time.Millisecond) mts, err := c.CollectMetrics([]core.Metric{ &plugin.MetricType{ Namespace_: core.NewNamespace("foo", "bar"), Config_: cdn, }, }) So(err, ShouldBeNil) So(mts, ShouldNotBeNil) So(mts, ShouldHaveSameTypeAs, []core.Metric{}) So(len(mts), ShouldBeGreaterThan, 0) So(mts[0].Config().Table(), ShouldNotBeEmpty) So(mts[0].Config().Table()["someInt"].Type(), ShouldResemble, "integer") Convey("Get and process the ConfigPolicy", func() { cp, err := c.GetConfigPolicy() So(err, ShouldBeNil) So(cp, ShouldNotBeNil) So(cp.Get([]string{"foo", "bar"}), ShouldNotBeNil) node := cp.Get([]string{"foo", "bar"}) So(node, ShouldNotBeNil) cpn, cserrs := node.Process(mts[0].Config().Table()) So(cpn, ShouldNotBeNil) So((*cpn)["somefloat"].Type(), ShouldResemble, "float") So((*cpn)["somefloat"].(ctypes.ConfigValueFloat).Value, ShouldResemble, 3.14) So(cserrs.Errors(), ShouldBeEmpty) }) }) Convey("CollectMetrics provided an invalid config", func() { cdn := cdata.NewNode() cdn.AddItem("someInt", ctypes.ConfigValueInt{Value: 1}) time.Sleep(500 * time.Millisecond) mts, err := c.CollectMetrics([]core.Metric{ &plugin.MetricType{ Namespace_: core.NewNamespace("foo", "bar"), Config_: cdn, }, }) So(err, ShouldBeNil) So(mts, ShouldNotBeNil) So(mts, ShouldHaveSameTypeAs, []core.Metric{}) So(len(mts), ShouldBeGreaterThan, 0) So(mts[0].Config().Table(), ShouldNotBeEmpty) So(mts[0].Config().Table()["someInt"].Type(), ShouldResemble, "integer") Convey("Get and process the ConfigPolicy", func() { cp, err := c.GetConfigPolicy() So(err, ShouldBeNil) So(cp, ShouldNotBeNil) node := cp.Get([]string{"foo", "bar"}) So(node, ShouldNotBeNil) So(err, ShouldBeNil) _, cserrs := node.Process(mts[0].Config().Table()) //So(cpn, ShouldBeNil) So(cserrs.Errors(), ShouldNotBeEmpty) So(len(cserrs.Errors()), ShouldEqual, 1) So(cserrs.Errors()[0].Error(), ShouldContainSubstring, "password") }) }) }) }
func TestGetMetricTypes(t *testing.T) { Convey("getting exposed metric types", t, func() { Convey("when no configuration item available", func() { cfg := plugin.NewPluginConfigType() dbiPlugin := New() So(func() { dbiPlugin.GetMetricTypes(cfg) }, ShouldNotPanic) results, err := dbiPlugin.GetMetricTypes(cfg) So(err, ShouldNotBeNil) So(results, ShouldBeNil) }) Convey("when path to setfile is incorrect", func() { cfg := plugin.NewPluginConfigType() dbiPlugin := New() cfg.AddItem("setfile", ctypes.ConfigValueStr{Value: "./noFile.json"}) So(func() { dbiPlugin.GetMetricTypes(cfg) }, ShouldNotPanic) results, err := dbiPlugin.GetMetricTypes(cfg) So(err, ShouldNotBeNil) So(results, ShouldBeNil) }) Convey("when setfile is empty", func() { cfg := plugin.NewPluginConfigType() dbiPlugin := New() os.Create(mockdata.FileName) defer os.Remove(mockdata.FileName) cfg.AddItem("setfile", ctypes.ConfigValueStr{Value: mockdata.FileName}) So(func() { dbiPlugin.GetMetricTypes(cfg) }, ShouldNotPanic) results, err := dbiPlugin.GetMetricTypes(cfg) So(err, ShouldNotBeNil) So(results, ShouldBeNil) }) Convey("when there is wrong driver typed", func() { cfg := plugin.NewPluginConfigType() dbiPlugin := New() cfg.AddItem("setfile", ctypes.ConfigValueStr{Value: mockdata.SetfileIncorr}) So(func() { dbiPlugin.GetMetricTypes(cfg) }, ShouldNotPanic) results, err := dbiPlugin.GetMetricTypes(cfg) So(err, ShouldNotBeNil) So(results, ShouldBeNil) }) Convey("when cannot open db", func() { cfg := plugin.NewPluginConfigType() dbiPlugin := New() cfg.AddItem("setfile", ctypes.ConfigValueStr{Value: mockdata.SetfileCorr}) mc := &mcMock{stmts: make(map[string]*sql.Stmt)} //mockExecution outputs mc.mockExecution( errors.New("x"), // errOpen nil, // errClose nil, // errPing nil, // errSwitchToDB nil, // errQuery map[string][]interface{}{}, // outQuery ) So(func() { dbiPlugin.GetMetricTypes(cfg) }, ShouldNotPanic) results, err := dbiPlugin.GetMetricTypes(cfg) So(err, ShouldNotBeNil) So(results, ShouldBeEmpty) }) Convey("when cannot open, neither close db", func() { cfg := plugin.NewPluginConfigType() dbiPlugin := New() //createMockFile(correct) //defer deleteMockFile() cfg.AddItem("setfile", ctypes.ConfigValueStr{Value: mockdata.SetfileCorr}) mc := &mcMock{stmts: make(map[string]*sql.Stmt)} //mockExecution outputs mc.mockExecution( errors.New("x"), // errOpen errors.New("x"), // errClose nil, // errPing nil, // errSwitchToDB nil, // errQuery map[string][]interface{}{}, // outQuery ) So(func() { dbiPlugin.GetMetricTypes(cfg) }, ShouldNotPanic) results, err := dbiPlugin.GetMetricTypes(cfg) So(err, ShouldNotBeNil) So(results, ShouldBeEmpty) }) Convey("when cannot ping the open db", func() { cfg := plugin.NewPluginConfigType() dbiPlugin := New() cfg.AddItem("setfile", ctypes.ConfigValueStr{Value: mockdata.SetfileCorr}) mc := &mcMock{stmts: make(map[string]*sql.Stmt)} //mockExecution outputs mc.mockExecution( nil, // errOpen nil, // errClose errors.New("x"), // errPing nil, // errSwitchToDB nil, // errQuery map[string][]interface{}{}, // outQuery ) So(func() { dbiPlugin.GetMetricTypes(cfg) }, ShouldNotPanic) results, err := dbiPlugin.GetMetricTypes(cfg) So(err, ShouldNotBeNil) So(results, ShouldBeEmpty) }) Convey("when cannot switch to selected db", func() { cfg := plugin.NewPluginConfigType() dbiPlugin := New() cfg.AddItem("setfile", ctypes.ConfigValueStr{Value: mockdata.SetfileCorr}) mc := &mcMock{stmts: make(map[string]*sql.Stmt)} //mockExecution outputs mc.mockExecution( nil, // errOpen nil, // errClose nil, // errPing errors.New("x"), // errSwitchToDB nil, // errQuery map[string][]interface{}{}, // outQuery ) So(func() { dbiPlugin.GetMetricTypes(cfg) }, ShouldNotPanic) results, err := dbiPlugin.GetMetricTypes(cfg) So(err, ShouldNotBeNil) So(results, ShouldBeEmpty) }) Convey("when execution of query returns error", func() { cfg := plugin.NewPluginConfigType() dbiPlugin := New() cfg.AddItem("setfile", ctypes.ConfigValueStr{Value: mockdata.SetfileCorr}) mc := &mcMock{stmts: make(map[string]*sql.Stmt)} //mockExecution outputs mc.mockExecution( nil, // errOpen nil, // errClose nil, // errPing nil, // errSwitchToDB errors.New("x"), // errQuery map[string][]interface{}{}, // outQuery ) So(func() { dbiPlugin.GetMetricTypes(cfg) }, ShouldNotPanic) results, err := dbiPlugin.GetMetricTypes(cfg) So(err, ShouldNotBeNil) So(results, ShouldBeEmpty) }) Convey("when query returns empty output", func() { cfg := plugin.NewPluginConfigType() dbiPlugin := New() cfg.AddItem("setfile", ctypes.ConfigValueStr{Value: mockdata.SetfileCorr}) mc := &mcMock{stmts: make(map[string]*sql.Stmt)} //mockExecution outputs mc.mockExecution( nil, // errOpen nil, // errClose nil, // errPing nil, // errSwitchToDB nil, // errQuery map[string][]interface{}{}, // outQuery ) So(func() { dbiPlugin.GetMetricTypes(cfg) }, ShouldNotPanic) results, err := dbiPlugin.GetMetricTypes(cfg) So(err, ShouldNotBeNil) So(results, ShouldBeEmpty) }) Convey("successfully obtain metrics name", func() { cfg := plugin.NewPluginConfigType() dbiPlugin := New() cfg.AddItem("setfile", ctypes.ConfigValueStr{Value: mockdata.SetfileCorr}) mc := &mcMock{stmts: make(map[string]*sql.Stmt)} // prepare mock output data of query execution mockQueryOut := mockdata.QueryOutput //mockExecution outputs mc.mockExecution(nil, nil, nil, nil, nil, mockQueryOut) So(func() { dbiPlugin.GetMetricTypes(cfg) }, ShouldNotPanic) results, err := dbiPlugin.GetMetricTypes(cfg) So(err, ShouldBeNil) So(results, ShouldNotBeEmpty) }) }) }
func TestGetMetricTypes(t *testing.T) { // Testing GetMetricTypes function covers checking initialization of Ceph plugin // mock ceph socket os.Mkdir("test", os.ModePerm) os.Create("test/ceph-mds.a.asok") os.Create("test/ceph-mds.b.asok") os.Create("test/ceph-mds.c.asok") // set ceph socket conf value cfg := plugin.NewPluginConfigType() cfg.AddItem("socket_path", ctypes.ConfigValueStr{Value: "./test"}) cfg.AddItem("socket_prefix", ctypes.ConfigValueStr{Value: "ceph-"}) cfg.AddItem("socket_ext", ctypes.ConfigValueStr{Value: "asok"}) Convey("init ceph plugin and getting metric namespace", t, func() { ceph := &Ceph{} cmd = &TestCmd{out: mockOut} So(func() { ceph.GetMetricTypes(cfg) }, ShouldNotPanic) result, err := ceph.GetMetricTypes(cfg) So(result, ShouldNotBeNil) So(err, ShouldBeNil) }) Convey("getting metric namespace with error - perf dump output is not valid", t, func() { ceph := &Ceph{} cmd = &TestCmd{out: mockOutInvalid} So(func() { ceph.GetMetricTypes(cfg) }, ShouldNotPanic) result, err := ceph.GetMetricTypes(cfg) So(result, ShouldBeEmpty) So(err, ShouldNotBeNil) }) Convey("getting metric namespace with error - perf dump execution error", t, func() { ceph := &Ceph{} cmd = &TestCmd{err: errors.New("execution error")} So(func() { ceph.GetMetricTypes(cfg) }, ShouldNotPanic) result, err := ceph.GetMetricTypes(cfg) So(result, ShouldBeEmpty) So(err, ShouldNotBeNil) }) Convey("getting metric namespace with error - perf dump output is empty", t, func() { ceph := &Ceph{} moi := []byte(fmt.Sprintf("\n\n\n\n")) cmd = &TestCmd{out: moi} So(func() { ceph.GetMetricTypes(cfg) }, ShouldNotPanic) result, err := ceph.GetMetricTypes(cfg) So(result, ShouldBeEmpty) So(err, ShouldNotBeNil) }) Convey("getting metric namespace with error - none perf dump output", t, func() { ceph := &Ceph{} cmd = &TestCmd{} So(func() { ceph.GetMetricTypes(cfg) }, ShouldNotPanic) result, err := ceph.GetMetricTypes(cfg) So(result, ShouldBeEmpty) So(err, ShouldNotBeNil) }) Convey("invalid path to ceph-daemon sockets", t, func() { ceph := &Ceph{} cmd = &TestCmd{out: mockOut} cfg_invalid := plugin.NewPluginConfigType() cfg_invalid.AddItem("socket_path", ctypes.ConfigValueStr{Value: "./test_invalid"}) cfg_invalid.AddItem("socket_prefix", ctypes.ConfigValueStr{Value: "ceph-"}) cfg_invalid.AddItem("socket_ext", ctypes.ConfigValueStr{Value: "asok"}) So(func() { ceph.GetMetricTypes(cfg_invalid) }, ShouldPanic) }) Convey("no ceph-daemon sockets available with set prefix", t, func() { ceph := &Ceph{} cmd = &TestCmd{out: mockOut} cfg_inv_prefix := plugin.NewPluginConfigType() cfg_inv_prefix.AddItem("socket_path", ctypes.ConfigValueStr{Value: "./test"}) cfg_inv_prefix.AddItem("socket_prefix", ctypes.ConfigValueStr{Value: "ceph_inv-"}) So(func() { ceph.GetMetricTypes(cfg_inv_prefix) }, ShouldNotPanic) result, err := ceph.GetMetricTypes(cfg_inv_prefix) So(result, ShouldBeNil) So(err, ShouldNotBeNil) }) Convey("no ceph-daemon sockets available with set extension", t, func() { ceph := &Ceph{} cmd = &TestCmd{out: mockOut} cfg_inv_ext := plugin.NewPluginConfigType() cfg_inv_ext.AddItem("socket_path", ctypes.ConfigValueStr{Value: "./test"}) cfg_inv_ext.AddItem("socket_ext", ctypes.ConfigValueStr{Value: "asok_inv"}) So(func() { ceph.GetMetricTypes(cfg_inv_ext) }, ShouldNotPanic) result, err := ceph.GetMetricTypes(cfg_inv_ext) So(result, ShouldBeNil) So(err, ShouldNotBeNil) }) os.RemoveAll("test/") }
func TestHTTPJSONRPC(t *testing.T) { log.SetLevel(log.DebugLevel) addr, session := startHTTPJSONRPC() time.Sleep(time.Millisecond * 100) Convey("Collector Client", t, func() { session.c = true c, err := NewCollectorHttpJSONRPCClient(fmt.Sprintf("http://%v", addr), 1*time.Second, &key.PublicKey, true) So(err, ShouldBeNil) So(c, ShouldNotBeNil) cl := c.(*httpJSONRPCClient) cl.encrypter.Key = symkey Convey("Ping", func() { err := c.Ping() So(err, ShouldBeNil) }) Convey("Kill", func() { err := c.Kill("somereason") So(err, ShouldBeNil) }) Convey("GetMetricTypes", func() { cfg := plugin.NewPluginConfigType() cfg.AddItem("test", ctypes.ConfigValueBool{Value: true}) mts, err := c.GetMetricTypes(cfg) So(err, ShouldBeNil) So(mts, ShouldNotBeNil) So(mts, ShouldHaveSameTypeAs, []core.Metric{}) So(len(mts), ShouldBeGreaterThan, 0) So(len(mts[0].Config().Table()), ShouldBeGreaterThan, 0) }) Convey("CollectMetrics provided a valid config", func() { cdn := cdata.NewNode() cdn.AddItem("someInt", ctypes.ConfigValueInt{Value: 1}) cdn.AddItem("password", ctypes.ConfigValueStr{Value: "secure"}) time.Sleep(500 * time.Millisecond) mts, err := c.CollectMetrics([]core.Metric{ &plugin.PluginMetricType{ Namespace_: []string{"foo", "bar"}, Config_: cdn, }, }) So(err, ShouldBeNil) So(mts, ShouldNotBeNil) So(mts, ShouldHaveSameTypeAs, []core.Metric{}) So(len(mts), ShouldBeGreaterThan, 0) So(mts[0].Config().Table(), ShouldNotBeEmpty) So(mts[0].Config().Table()["someInt"].Type(), ShouldResemble, "integer") Convey("Get and process the ConfigPolicy", func() { cp, err := c.GetConfigPolicy() So(err, ShouldBeNil) So(cp, ShouldNotBeNil) So(cp.Get([]string{"foo", "bar"}), ShouldNotBeNil) node := cp.Get([]string{"foo", "bar"}) So(node, ShouldNotBeNil) cpn, cserrs := node.Process(mts[0].Config().Table()) So(cpn, ShouldNotBeNil) So((*cpn)["somefloat"].Type(), ShouldResemble, "float") So((*cpn)["somefloat"].(*ctypes.ConfigValueFloat).Value, ShouldResemble, 3.14) So(cserrs.Errors(), ShouldBeEmpty) }) }) Convey("CollectMetrics provided an invalid config", func() { cdn := cdata.NewNode() cdn.AddItem("someInt", ctypes.ConfigValueInt{Value: 1}) time.Sleep(500 * time.Millisecond) mts, err := c.CollectMetrics([]core.Metric{ &plugin.PluginMetricType{ Namespace_: []string{"foo", "bar"}, Config_: cdn, }, }) So(err, ShouldBeNil) So(mts, ShouldNotBeNil) So(mts, ShouldHaveSameTypeAs, []core.Metric{}) So(len(mts), ShouldBeGreaterThan, 0) So(mts[0].Config().Table(), ShouldNotBeEmpty) So(mts[0].Config().Table()["someInt"].Type(), ShouldResemble, "integer") Convey("Get and process the ConfigPolicy", func() { cp, err := c.GetConfigPolicy() So(err, ShouldBeNil) So(cp, ShouldNotBeNil) node := cp.Get([]string{"foo", "bar"}) So(node, ShouldNotBeNil) So(err, ShouldBeNil) _, cserrs := node.Process(mts[0].Config().Table()) //So(cpn, ShouldBeNil) So(cserrs.Errors(), ShouldNotBeEmpty) So(len(cserrs.Errors()), ShouldEqual, 1) So(cserrs.Errors()[0].Error(), ShouldContainSubstring, "password") }) }) }) Convey("Processor Client", t, func() { session.c = false p, _ := NewProcessorHttpJSONRPCClient(fmt.Sprintf("http://%v", addr), 1*time.Second, &key.PublicKey, true) cl := p.(*httpJSONRPCClient) cl.encrypter.Key = symkey So(p, ShouldNotBeNil) Convey("GetConfigPolicy", func() { cp, err := p.GetConfigPolicy() So(err, ShouldBeNil) So(cp, ShouldNotBeNil) cp_ := cpolicy.New() cpn_ := cpolicy.NewPolicyNode() r1, err := cpolicy.NewIntegerRule("SomeRequiredInt", true, 1) r2, _ := cpolicy.NewStringRule("password", true) r3, _ := cpolicy.NewFloatRule("somefloat", false, 3.14) So(err, ShouldBeNil) cpn_.Add(r1, r2, r3) cp_.Add([]string{""}, cpn_) cpjson, _ := cp.MarshalJSON() cp_json, _ := cp_.MarshalJSON() So(string(cpjson), ShouldResemble, string(cp_json)) }) Convey("Process metrics", func() { pmt := plugin.NewPluginMetricType([]string{"foo", "bar"}, time.Now(), "", nil, nil, 1) b, _ := json.Marshal([]plugin.PluginMetricType{*pmt}) contentType, content, err := p.Process(plugin.SnapJSONContentType, b, nil) So(contentType, ShouldResemble, plugin.SnapJSONContentType) So(content, ShouldNotBeNil) So(err, ShouldEqual, nil) var pmts []plugin.PluginMetricType err = json.Unmarshal(content, &pmts) So(err, ShouldBeNil) So(len(pmts), ShouldEqual, 1) So(pmts[0].Data(), ShouldEqual, 1) So(pmts[0].Namespace(), ShouldResemble, []string{"foo", "bar"}) }) }) Convey("Publisher Client", t, func() { session.c = false p, _ := NewPublisherHttpJSONRPCClient(fmt.Sprintf("http://%v", addr), 1*time.Second, &key.PublicKey, true) cl := p.(*httpJSONRPCClient) cl.encrypter.Key = symkey So(p, ShouldNotBeNil) Convey("GetConfigPolicy", func() { cp, err := p.GetConfigPolicy() So(err, ShouldBeNil) So(cp, ShouldNotBeNil) cp_ := cpolicy.New() cpn_ := cpolicy.NewPolicyNode() r1, err := cpolicy.NewIntegerRule("SomeRequiredInt", true, 1) r2, _ := cpolicy.NewStringRule("password", true) r3, _ := cpolicy.NewFloatRule("somefloat", false, 3.14) So(err, ShouldBeNil) cpn_.Add(r1, r2, r3) cp_.Add([]string{""}, cpn_) cpjson, _ := cp.MarshalJSON() cp_json, _ := cp_.MarshalJSON() So(string(cpjson), ShouldResemble, string(cp_json)) }) Convey("Publish metrics", func() { pmt := plugin.NewPluginMetricType([]string{"foo", "bar"}, time.Now(), "", nil, nil, 1) b, _ := json.Marshal([]plugin.PluginMetricType{*pmt}) err := p.Publish(plugin.SnapJSONContentType, b, nil) So(err, ShouldBeNil) }) }) }
func TestGetMetricTypes(t *testing.T) { Convey("initialization fails", t, func() { Convey("when no config items available", func() { influxdbPlugin := New() cfg := plugin.NewPluginConfigType() So(func() { influxdbPlugin.GetMetricTypes(cfg) }, ShouldPanic) }) Convey("when one of config item is not available", func() { influxdbPlugin := New() cfg := plugin.NewPluginConfigType() cfg.DeleteItem("user") So(func() { influxdbPlugin.GetMetricTypes(cfg) }, ShouldPanic) }) Convey("when config item has different type than expected", func() { influxdbPlugin := New() cfg := plugin.NewPluginConfigType() cfg.DeleteItem("port") cfg.AddItem("port", ctypes.ConfigValueStr{Value: "1234"}) So(func() { influxdbPlugin.GetMetricTypes(cfg) }, ShouldPanic) }) Convey("when initialization of URLs returns error", func() { mc := &mcMock{} influxdbPlugin := New() influxdbPlugin.service = mc cfg := plugin.NewPluginConfigType() mc.On("InitURLs").Return(errors.New("x")) So(func() { influxdbPlugin.GetMetricTypes(cfg) }, ShouldPanic) }) }) Convey("Metrics are not available", t, func() { Convey("when cannot obtain any data", func() { mc := &mcMock{} influxdbPlugin := New() influxdbPlugin.service = mc cfg := getMockPluginConfig() mc.On("InitURLs").Return(nil) mc.On("GetStatistics").Return(dtype.Results{}, errors.New("x")) mc.On("GetDiagnostics").Return(dtype.Results{}, errors.New("x")) So(func() { influxdbPlugin.GetMetricTypes(cfg) }, ShouldNotPanic) results, err := influxdbPlugin.GetMetricTypes(cfg) So(results, ShouldBeEmpty) So(err, ShouldBeNil) }) Convey("when cannot obtain statictics data", func() { mc := &mcMock{} influxdbPlugin := New() influxdbPlugin.service = mc cfg := getMockPluginConfig() mc.On("InitURLs").Return(nil) mc.On("GetStatistics").Return(dtype.Results{}, errors.New("x")) mc.On("GetDiagnostics").Return(mockDiagn, nil) So(func() { influxdbPlugin.GetMetricTypes(cfg) }, ShouldNotPanic) results, err := influxdbPlugin.GetMetricTypes(cfg) So(results, ShouldNotBeEmpty) So(err, ShouldBeNil) }) Convey("when cannot obtain diagnostics data", func() { mc := &mcMock{} influxdbPlugin := New() influxdbPlugin.service = mc cfg := getMockPluginConfig() mc.On("InitURLs").Return(nil) mc.On("GetStatistics").Return(mockStats, nil) mc.On("GetDiagnostics").Return(dtype.Results{}, errors.New("x")) So(func() { influxdbPlugin.GetMetricTypes(cfg) }, ShouldNotPanic) So(func() { influxdbPlugin.GetMetricTypes(cfg) }, ShouldNotPanic) results, err := influxdbPlugin.GetMetricTypes(cfg) So(results, ShouldNotBeEmpty) So(err, ShouldBeNil) }) }) Convey("successfull getting metrics types", t, func() { mc := &mcMock{} influxdbPlugin := New() influxdbPlugin.service = mc cfg := getMockPluginConfig() mc.On("InitURLs").Return(nil) mc.On("GetStatistics").Return(mockStats, nil) mc.On("GetDiagnostics").Return(mockDiagn, nil) So(func() { influxdbPlugin.GetMetricTypes(cfg) }, ShouldNotPanic) So(func() { influxdbPlugin.GetMetricTypes(cfg) }, ShouldNotPanic) results, err := influxdbPlugin.GetMetricTypes(cfg) So(results, ShouldNotBeEmpty) So(err, ShouldBeNil) }) }