Added tests for /monitor/metrics/{metrics_type}

Added API and client tests for GET /monitor/metrics/{metrics_type}

Fixes #587

License: MIT
Signed-off-by: Kishan Mohanbhai Sagathiya <kishansagathiya@gmail.com>
This commit is contained in:
Kishan Sagathiya 2018-12-14 18:52:03 +05:30 committed by Kishan Mohanbhai Sagathiya
parent 17599dac63
commit e9cf8abb3a
3 changed files with 50 additions and 5 deletions

View File

@ -318,6 +318,27 @@ func TestGetConnectGraph(t *testing.T) {
testClients(t, api, testF) testClients(t, api, testF)
} }
func TestMetrics(t *testing.T) {
api := testAPI(t)
defer shutdown(api)
//metricsList := []string{"freespace", "ping"}
testF := func(t *testing.T, c Client) {
for _, metricsType := range []string{"freespace", "ping"} {
m, err := c.Metrics(metricsType)
if err != nil {
t.Fatal(err)
}
if len(m) == 0 {
t.Fatal("No metrics found")
}
}
}
testClients(t, api, testF)
}
type waitService struct { type waitService struct {
l sync.Mutex l sync.Mutex
pinStart time.Time pinStart time.Time

View File

@ -42,7 +42,7 @@ func testAPI(t *testing.T) *API {
rest, err := NewAPIWithHost(cfg, h) rest, err := NewAPIWithHost(cfg, h)
if err != nil { if err != nil {
t.Fatal("should be able to create a new Api: ", err) t.Fatal("should be able to create a new API: ", err)
} }
// No keep alive for tests // No keep alive for tests
@ -556,6 +556,30 @@ func TestAPIAllocationEndpoint(t *testing.T) {
testBothEndpoints(t, tf) testBothEndpoints(t, tf)
} }
func TestAPIMetricsEndpoint(t *testing.T) {
rest := testAPI(t)
defer rest.Shutdown()
tf := func(t *testing.T, url urlF) {
for _, metricsType := range []string{"freespace", "ping"} {
var resp []api.MetricSerial
makeGet(t, rest, url(rest)+"/monitor/metrics/"+metricsType, &resp)
if len(resp) == 0 {
t.Fatal("No metrics found")
}
for _, m := range resp {
if m.Name != "test" {
t.Error("Unexpected metric name: ", m.Name)
}
if m.Peer != test.TestPeerID1.Pretty() {
t.Error("Unexpected peer id: ", m.Peer)
}
}
}
}
testBothEndpoints(t, tf)
}
func TestAPIStatusAllEndpoint(t *testing.T) { func TestAPIStatusAllEndpoint(t *testing.T) {
rest := testAPI(t) rest := testAPI(t)
defer rest.Shutdown() defer rest.Shutdown()

View File

@ -897,9 +897,9 @@ func (m *Metric) Discard() bool {
return !m.Valid || m.Expired() return !m.Valid || m.Expired()
} }
// helper for JSON marshaling. The Metric type is already // MetricSerial is a helper for JSON marshaling. The Metric type is already
// serializable, but not pretty to humans (API). // serializable, but not pretty to humans (API).
type metricSerial struct { type MetricSerial struct {
Name string `json:"name"` Name string `json:"name"`
Peer string `json:"peer"` Peer string `json:"peer"`
Value string `json:"value"` Value string `json:"value"`
@ -910,7 +910,7 @@ type metricSerial struct {
// MarshalJSON allows a Metric to produce a JSON representation // MarshalJSON allows a Metric to produce a JSON representation
// of itself. // of itself.
func (m *Metric) MarshalJSON() ([]byte, error) { func (m *Metric) MarshalJSON() ([]byte, error) {
return json.Marshal(&metricSerial{ return json.Marshal(&MetricSerial{
Name: m.Name, Name: m.Name,
Peer: peer.IDB58Encode(m.Peer), Peer: peer.IDB58Encode(m.Peer),
Value: m.Value, Value: m.Value,
@ -924,7 +924,7 @@ func (m *Metric) UnmarshalJSON(j []byte) error {
return nil return nil
} }
ms := &metricSerial{} ms := &MetricSerial{}
err := json.Unmarshal(j, ms) err := json.Unmarshal(j, ms)
if err != nil { if err != nil {
return err return err