2015-09-02 04:21:02 +00:00
|
|
|
package monitor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/influxdb/influxdb/influxql"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Test that a registered stats client results in the correct SHOW STATS output.
|
2015-09-02 22:07:30 +00:00
|
|
|
func Test_RegisterStats(t *testing.T) {
|
|
|
|
monitor := openMonitor(t)
|
2015-09-02 04:21:02 +00:00
|
|
|
|
|
|
|
client := mockStatsClient{
|
|
|
|
StatisticsFn: func() (map[string]interface{}, error) {
|
|
|
|
return map[string]interface{}{
|
|
|
|
"bar": 1,
|
|
|
|
"qux": 2.4,
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a client without tags.
|
2015-09-02 22:07:30 +00:00
|
|
|
if err := monitor.Register("foo", nil, client); err != nil {
|
2015-09-02 04:21:02 +00:00
|
|
|
t.Fatalf("failed to register client: %s", err.Error())
|
|
|
|
}
|
2015-09-02 22:07:30 +00:00
|
|
|
json := executeShowStatsJSON(t, monitor)
|
2015-09-02 04:21:02 +00:00
|
|
|
if !strings.Contains(json, `{"name":"foo","columns":["bar","qux"],"values":[[1,2.4]]}]}`) {
|
|
|
|
t.Fatalf("SHOW STATS response incorrect, got: %s\n", json)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a client with tags.
|
2015-09-02 22:07:30 +00:00
|
|
|
if err := monitor.Register("baz", map[string]string{"proto": "tcp"}, client); err != nil {
|
2015-09-02 04:21:02 +00:00
|
|
|
t.Fatalf("failed to register client: %s", err.Error())
|
|
|
|
}
|
2015-09-02 22:07:30 +00:00
|
|
|
json = executeShowStatsJSON(t, monitor)
|
2015-09-02 04:21:02 +00:00
|
|
|
if !strings.Contains(json, `{"name":"baz","tags":{"proto":"tcp"},"columns":["bar","qux"],"values":[[1,2.4]]}]}`) {
|
|
|
|
t.Fatalf("SHOW STATS response incorrect, got: %s\n", json)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockStatsClient struct {
|
|
|
|
StatisticsFn func() (map[string]interface{}, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockStatsClient) Statistics() (map[string]interface{}, error) {
|
|
|
|
return m.StatisticsFn()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mockStatsClient) Diagnostics() (map[string]interface{}, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-09-02 22:07:30 +00:00
|
|
|
func openMonitor(t *testing.T) *Monitor {
|
|
|
|
monitor := New(NewConfig())
|
|
|
|
err := monitor.Open(1, 2, "serverA")
|
2015-09-02 04:21:02 +00:00
|
|
|
if err != nil {
|
2015-09-02 22:07:30 +00:00
|
|
|
t.Fatalf("failed to open monitor: %s", err.Error())
|
2015-09-02 04:21:02 +00:00
|
|
|
}
|
2015-09-02 22:07:30 +00:00
|
|
|
return monitor
|
2015-09-02 04:21:02 +00:00
|
|
|
}
|
|
|
|
|
2015-09-02 22:07:30 +00:00
|
|
|
func executeShowStatsJSON(t *testing.T, s *Monitor) string {
|
2015-09-02 04:21:02 +00:00
|
|
|
r := s.ExecuteStatement(&influxql.ShowStatsStatement{})
|
|
|
|
b, err := r.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to decode SHOW STATS response: %s", err.Error())
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|