package api_test import ( "encoding/json" "fmt" "net/http" "testing" "github.com/v10networks/nmon/internal/api" "github.com/v10networks/nmon/internal/fixturegen" "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) }