2015-09-02 04:21:02 +00:00
|
|
|
package monitor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2015-09-02 23:14:03 +00:00
|
|
|
"time"
|
2015-09-02 04:21:02 +00:00
|
|
|
|
2015-09-04 05:12:33 +00:00
|
|
|
"github.com/influxdb/influxdb"
|
2015-09-02 04:21:02 +00:00
|
|
|
"github.com/influxdb/influxdb/influxql"
|
2015-09-02 22:50:09 +00:00
|
|
|
"github.com/influxdb/influxdb/meta"
|
2015-09-02 04:21:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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 22:50:09 +00:00
|
|
|
executor := &StatementExecutor{Monitor: monitor}
|
2015-09-02 04:21:02 +00:00
|
|
|
|
2015-09-04 05:12:33 +00:00
|
|
|
// Register stats without tags.
|
|
|
|
statMap := influxdb.NewStatistics("foo", "foo", nil)
|
|
|
|
statMap.Add("bar", 1)
|
|
|
|
statMap.AddFloat("qux", 2.4)
|
2015-09-02 22:50:09 +00:00
|
|
|
json := executeShowStatsJSON(t, executor)
|
2015-09-03 02:38:31 +00:00
|
|
|
if !strings.Contains(json, `"columns":["bar","qux"],"values":[[1,2.4]]`) || !strings.Contains(json, `"name":"foo"`) {
|
2015-09-02 04:21:02 +00:00
|
|
|
t.Fatalf("SHOW STATS response incorrect, got: %s\n", json)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register a client with tags.
|
2015-09-04 05:12:33 +00:00
|
|
|
statMap = influxdb.NewStatistics("bar", "baz", map[string]string{"proto": "tcp"})
|
|
|
|
statMap.Add("bar", 1)
|
|
|
|
statMap.AddFloat("qux", 2.4)
|
2015-09-02 22:50:09 +00:00
|
|
|
json = executeShowStatsJSON(t, executor)
|
2015-09-03 02:38:31 +00:00
|
|
|
if !strings.Contains(json, `"columns":["bar","qux"],"values":[[1,2.4]]`) ||
|
|
|
|
!strings.Contains(json, `"name":"baz"`) ||
|
|
|
|
!strings.Contains(json, `"proto":"tcp"`) {
|
2015-09-02 04:21:02 +00:00
|
|
|
t.Fatalf("SHOW STATS response incorrect, got: %s\n", json)
|
2015-09-03 02:38:31 +00:00
|
|
|
|
2015-09-02 04:21:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-02 22:50:09 +00:00
|
|
|
type mockMetastore struct{}
|
|
|
|
|
2015-09-15 20:39:02 +00:00
|
|
|
func (m *mockMetastore) ClusterID() (uint64, error) { return 1, nil }
|
|
|
|
func (m *mockMetastore) NodeID() uint64 { return 2 }
|
|
|
|
func (m *mockMetastore) WaitForLeader(d time.Duration) error { return nil }
|
2015-10-05 22:37:59 +00:00
|
|
|
func (m *mockMetastore) IsLeader() bool { return true }
|
2015-09-15 20:39:02 +00:00
|
|
|
func (m *mockMetastore) SetDefaultRetentionPolicy(database, name string) error { return nil }
|
|
|
|
func (m *mockMetastore) DropRetentionPolicy(database, name string) error { return nil }
|
2015-09-02 22:50:09 +00:00
|
|
|
func (m *mockMetastore) CreateDatabaseIfNotExists(name string) (*meta.DatabaseInfo, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2015-09-05 05:12:58 +00:00
|
|
|
func (m *mockMetastore) CreateRetentionPolicyIfNotExists(database string, rpi *meta.RetentionPolicyInfo) (*meta.RetentionPolicyInfo, error) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2015-09-02 22:50:09 +00:00
|
|
|
|
2015-09-02 22:07:30 +00:00
|
|
|
func openMonitor(t *testing.T) *Monitor {
|
|
|
|
monitor := New(NewConfig())
|
2015-09-02 22:50:09 +00:00
|
|
|
monitor.MetaStore = &mockMetastore{}
|
|
|
|
err := monitor.Open()
|
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:50:09 +00:00
|
|
|
func executeShowStatsJSON(t *testing.T, s *StatementExecutor) 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)
|
|
|
|
}
|