Files
nmon/internal/api/ops_test.go

67 lines
2.3 KiB
Go

package api_test
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"testing"
"github.com/v10networks/nmon/internal/api"
"github.com/v10networks/nmon/internal/fixturegen"
"github.com/v10networks/nmon/internal/poller"
"github.com/v10networks/nmon/internal/simagent"
"github.com/v10networks/nmon/internal/testutil"
)
func TestDiscoverEndpointReportsAndRefreshes(t *testing.T) {
a := testutil.StartAgent(t, simagent.Config{
Recording: fixturegen.Generic(fixturegen.Options{Ports: 6, SysName: "d1", HCCounters: true}), Community: community,
})
e := newEnv(t, int(a.Addr().Port()))
body := fmt.Sprintf(`{"name":"D One","addresses":["::1"],"snmp":{"version":"v2c","community":%q}}`, community)
rec := e.do("POST", "/api/v1/devices", body)
if rec.Code != http.StatusCreated {
t.Fatalf("POST: %d %s", rec.Code, rec.Body)
}
var dev api.DeviceView
_ = json.Unmarshal(rec.Body.Bytes(), &dev)
rec = e.do("POST", "/api/v1/devices/"+dev.Slug+"/discover", "")
if rec.Code != http.StatusOK {
t.Fatalf("discover: %d %s", rec.Code, rec.Body)
}
var rep api.DiscoverView
if err := json.Unmarshal(rec.Body.Bytes(), &rep); err != nil {
t.Fatal(err)
}
if rep.InterfacesMatched != 6 || rep.InterfacesNew != 0 || rep.InterfacesRetired != 0 || rep.Partial || len(rep.Tables) == 0 {
t.Errorf("report %+v", rep)
}
if e.afterID.Load() == 0 {
t.Error("AfterDiscover not called")
}
isProblem(t, e.do("POST", "/api/v1/devices/no-such-device/discover", ""), 404)
}
func TestPollerHealthUsesSlugs(t *testing.T) {
e := newEnv(t, 0)
rec := e.do("GET", "/api/v1/poller/health", "")
if rec.Code != 200 || strings.TrimSpace(rec.Body.String()) != "[]" {
t.Fatalf("empty health: %d %s", rec.Code, rec.Body)
}
e.status = []poller.DeviceStatus{{Device: "core-1", DeviceID: 77, State: "degraded", Varbinds: 20, Profile: "juniper-junos"}}
rec = e.do("GET", "/api/v1/poller/health", "")
if rec.Code != 200 || !strings.Contains(rec.Body.String(), `"device":"core-1"`) || strings.Contains(rec.Body.String(), "77") {
t.Fatalf("health: %s", rec.Body)
}
}
func TestIdentityReviewsList(t *testing.T) {
e := newEnv(t, 0)
rec := e.do("GET", "/api/v1/identity-reviews", "")
if rec.Code != 200 || strings.TrimSpace(rec.Body.String()) != "[]" {
t.Fatalf("reviews: %d %s", rec.Code, rec.Body)
}
}