config_test.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (c) 2016 Arista Networks, Inc.
  2. // Use of this source code is governed by the Apache License 2.0
  3. // that can be found in the COPYING file.
  4. package main
  5. import (
  6. "testing"
  7. "notabug.org/themusicgod1/goarista/test"
  8. )
  9. func TestConfig(t *testing.T) {
  10. cfg, err := loadConfig("/nonexistent.json")
  11. if err == nil {
  12. t.Fatal("Managed to load a nonexistent config!")
  13. }
  14. cfg, err = loadConfig("sampleconfig.json")
  15. if err != nil {
  16. t.Fatal("Failed to load config:", err)
  17. }
  18. testcases := []struct {
  19. path string
  20. metric string
  21. tags map[string]string
  22. }{{
  23. path: "/Sysdb/environment/cooling/status/fan/Fan1/1/speed/value",
  24. metric: "eos.environment.fan.speed",
  25. tags: map[string]string{"fan": "Fan1/1"},
  26. }, {
  27. path: "/Sysdb/environment/power/status/powerSupply/PowerSupply2/outputPower/value",
  28. metric: "eos.environment.power.output",
  29. tags: map[string]string{"sensor": "PowerSupply2"},
  30. }, {
  31. path: "/Sysdb/environment/power/status/voltageSensor/VoltageSensor23/voltage/value",
  32. metric: "eos.environment.voltage",
  33. tags: map[string]string{"sensor": "VoltageSensor23"},
  34. }, {
  35. path: "/Sysdb/environment/power/status/currentSensor/CurrentSensorP2/1/current/value",
  36. metric: "eos.environment.current",
  37. tags: map[string]string{"sensor": "CurrentSensorP2/1"},
  38. }, {
  39. path: "/Sysdb/environment/temperature/status/tempSensor/" +
  40. "TempSensorP2/1/maxTemperature/value",
  41. metric: "eos.environment.maxtemperature",
  42. tags: map[string]string{"sensor": "TempSensorP2/1"},
  43. }, {
  44. path: "/Sysdb/interface/counter/eth/lag/intfCounterDir/" +
  45. "Port-Channel201/intfCounter/current/statistics/outUcastPkts",
  46. metric: "eos.interface.pkt",
  47. tags: map[string]string{"intf": "Port-Channel201", "direction": "out", "type": "Ucast"},
  48. }, {
  49. path: "/Sysdb/interface/counter/eth/slice/phy/1/intfCounterDir/" +
  50. "Ethernet42/intfCounter/current/statistics/inUcastPkts",
  51. metric: "eos.interface.pkt",
  52. tags: map[string]string{"intf": "Ethernet42", "direction": "in", "type": "Ucast"},
  53. }, {
  54. path: "/Sysdb/interface/counter/eth/slice/phy/1/intfCounterDir/" +
  55. "Ethernet42/intfCounter/lastClear/statistics/inErrors",
  56. }, {
  57. path: "/Sysdb/interface/counter/eth/slice/phy/1/intfCounterDir/" +
  58. "Ethernet42/intfCounter/current/ethStatistics/outPfcClassFrames",
  59. metric: "eos.interface.pfcclassframes",
  60. tags: map[string]string{"intf": "Ethernet42", "direction": "out"},
  61. }}
  62. for i, tcase := range testcases {
  63. actualMetric, actualTags := cfg.Match(tcase.path)
  64. if actualMetric != tcase.metric {
  65. t.Errorf("#%d expected metric %q but got %q", i, tcase.metric, actualMetric)
  66. }
  67. if d := test.Diff(tcase.tags, actualTags); actualMetric != "" && d != "" {
  68. t.Errorf("#%d expected tags %q but got %q: %s", i, tcase.tags, actualTags, d)
  69. }
  70. }
  71. }