140 lines
5.3 KiB
Go
140 lines
5.3 KiB
Go
package dashboard
|
||
|
||
import (
|
||
"encoding/json"
|
||
"io"
|
||
"net/http"
|
||
"net/http/httptest"
|
||
"net/url"
|
||
"strings"
|
||
"sync"
|
||
"sync/atomic"
|
||
"testing"
|
||
"time"
|
||
)
|
||
|
||
// newDiagAPI is a minimal control-api serving the diagnostics endpoints.
|
||
func newDiagAPI(t *testing.T, attachBody *string) string {
|
||
t.Helper()
|
||
var mu sync.Mutex
|
||
mux := http.NewServeMux()
|
||
mux.HandleFunc("GET /api/v1/admin/diagnostics", func(w http.ResponseWriter, r *http.Request) {
|
||
writeJSON(w, 200, []diagnosticDTO{{Address: "203.0.113.5", ValidatorID: "vkiplab-v1", FIPID: "fip-1", Note: "проверка", CreatedAt: time.Now().Add(-time.Hour)}})
|
||
})
|
||
mux.HandleFunc("GET /api/v1/admin/config/validators", func(w http.ResponseWriter, r *http.Request) {
|
||
writeJSON(w, 200, []validatorDTO{
|
||
{ValidatorID: "vkiplab-v2", OSPortID: "port-2", State: "idle"},
|
||
{ValidatorID: "vkiplab-v3", OSPortID: "", State: "idle"},
|
||
{ValidatorID: "vkiplab-v4", OSPortID: "port-4", State: "checking"},
|
||
})
|
||
})
|
||
mux.HandleFunc("POST /api/v1/admin/diagnostics/attach", func(w http.ResponseWriter, r *http.Request) {
|
||
b, _ := io.ReadAll(r.Body)
|
||
mu.Lock()
|
||
*attachBody = string(b)
|
||
mu.Unlock()
|
||
writeJSON(w, 200, map[string]interface{}{"results": []diagnosticResult{
|
||
{Address: "203.0.113.5", Status: "attached", ValidatorID: "vkiplab-v2"},
|
||
{Address: "203.0.113.6", Status: "rejected", Reason: "нет свободных валидаторов"},
|
||
}})
|
||
})
|
||
ts := httptest.NewServer(mux)
|
||
t.Cleanup(ts.Close)
|
||
return ts.URL
|
||
}
|
||
|
||
func TestDiagnosticsPageRendersList(t *testing.T) {
|
||
var sent string
|
||
ts := newTestServer(t, newDiagAPI(t, &sent))
|
||
body := get(t, ts, "/diagnostics")
|
||
for _, want := range []string{"203.0.113.5", "vkiplab-v1", "fip-1", "проверка", "Отвязать"} {
|
||
if !strings.Contains(body, want) {
|
||
t.Fatalf("expected %q in page, got:\n%s", want, body)
|
||
}
|
||
}
|
||
if !strings.Contains(body, `value="vkiplab-v2"`) || strings.Contains(body, `value="vkiplab-v3"`) || strings.Contains(body, `value="vkiplab-v4"`) {
|
||
t.Fatalf("dropdown must hold only idle validators with a port, got:\n%s", body)
|
||
}
|
||
}
|
||
|
||
func TestDiagnosticsAttachBodyAndRejectedReason(t *testing.T) {
|
||
var sent string
|
||
ts := newTestServer(t, newDiagAPI(t, &sent))
|
||
body := postForm(t, ts, "POST", "/diagnostics/attach", url.Values{
|
||
"addresses": {"203.0.113.5\n203.0.113.6"},
|
||
"validator_id": {"vkiplab-v2"},
|
||
"note": {"n1"},
|
||
})
|
||
var got struct {
|
||
Items []diagnosticAttachItem `json:"items"`
|
||
Note string `json:"note"`
|
||
}
|
||
if err := json.Unmarshal([]byte(sent), &got); err != nil {
|
||
t.Fatalf("attach body: %v (%q)", err, sent)
|
||
}
|
||
if len(got.Items) != 2 || got.Items[0].ValidatorID != "vkiplab-v2" || got.Items[1].ValidatorID != "" || got.Note != "n1" {
|
||
t.Fatalf("unexpected attach body: %s", sent)
|
||
}
|
||
if !strings.Contains(body, "привязан к vkiplab-v2") || !strings.Contains(body, "нет свободных валидаторов") {
|
||
t.Fatalf("expected attached and rejected results, got:\n%s", body)
|
||
}
|
||
}
|
||
|
||
func TestDiagnosticsOperationRefreshesListsAfterResult(t *testing.T) {
|
||
const opID = "diagnostic-operation-test"
|
||
var lists, statusCalls atomic.Int32
|
||
mux := http.NewServeMux()
|
||
mux.HandleFunc("POST /api/v1/admin/diagnostics/operations", func(w http.ResponseWriter, r *http.Request) {
|
||
var body struct {
|
||
ID string `json:"id"`
|
||
}
|
||
if err := json.NewDecoder(r.Body).Decode(&body); err != nil || body.ID != opID {
|
||
t.Errorf("start operation: %+v, %v", body, err)
|
||
}
|
||
writeJSON(w, http.StatusAccepted, map[string]string{"id": opID})
|
||
})
|
||
mux.HandleFunc("GET /api/v1/admin/diagnostics/operations/"+opID, func(w http.ResponseWriter, r *http.Request) {
|
||
status := "running"
|
||
if statusCalls.Add(1) == 2 {
|
||
status = "attached"
|
||
}
|
||
writeJSON(w, http.StatusOK, diagnosticOperation{
|
||
ID: opID, Items: []diagnosticResult{{Address: "203.0.113.5", Status: status, ValidatorID: "vkiplab-v1"}},
|
||
})
|
||
})
|
||
for _, path := range []string{"/api/v1/admin/diagnostics", "/api/v1/admin/config/validators"} {
|
||
mux.HandleFunc("GET "+path, func(w http.ResponseWriter, r *http.Request) {
|
||
lists.Add(1)
|
||
writeJSON(w, 200, []struct{}{})
|
||
})
|
||
}
|
||
api := httptest.NewServer(mux)
|
||
t.Cleanup(api.Close)
|
||
ts := newTestServer(t, api.URL)
|
||
form := url.Values{"operation_id": {opID}, "addresses": {"203.0.113.5"}}
|
||
body := postForm(t, ts, "POST", "/diagnostics/operations", form)
|
||
if !strings.Contains(body, "В очереди") || !strings.Contains(body, "load, every 1s") {
|
||
t.Fatalf("missing initial progress: %s", body)
|
||
}
|
||
body = get(t, ts, "/diagnostics/operations/"+opID)
|
||
if !strings.Contains(body, "Привязываем…") || !strings.Contains(body, "every 1s") {
|
||
t.Fatalf("missing running progress: %s", body)
|
||
}
|
||
resp, err := ts.Client().Get(ts.URL + "/diagnostics/operations/" + opID)
|
||
if err != nil {
|
||
t.Fatal(err)
|
||
}
|
||
completed, err := io.ReadAll(resp.Body)
|
||
resp.Body.Close()
|
||
if err != nil || !strings.Contains(string(completed), "привязан к vkiplab-v1") || strings.Contains(string(completed), "every 1s") {
|
||
t.Fatalf("wrong terminal fragment: %s, %v", completed, err)
|
||
}
|
||
if event := resp.Header.Get("HX-Trigger-After-Swap"); !strings.Contains(event, opID) || lists.Load() != 0 {
|
||
t.Fatalf("refresh event=%q, premature list requests=%d", event, lists.Load())
|
||
}
|
||
body = get(t, ts, "/diagnostics?operation="+opID)
|
||
if !strings.Contains(body, `hx-get="/diagnostics/operations/`+opID+`"`) {
|
||
t.Fatalf("page refresh lost the operation: %s", body)
|
||
}
|
||
}
|