| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- package httpapi_test
- import (
- "encoding/json"
- "io"
- "net/http"
- "strings"
- "testing"
- "time"
- "github.com/gnacho/netpulse/agent/probe"
- )
- // postAgentCreateHot publica POST /api/agents con body dado y devuelve la
- // respuesta decodificada.
- func postAgentCreateHot(t *testing.T, base, cookie, body string) (int, map[string]any) {
- t.Helper()
- req, _ := http.NewRequest("POST", base+"/api/agents", strings.NewReader(body))
- req.Header.Set("Content-Type", "application/json")
- req.Header.Set("Cookie", "session="+cookie)
- res, err := http.DefaultClient.Do(req)
- if err != nil {
- t.Fatalf("POST /api/agents: %v", err)
- }
- defer res.Body.Close()
- raw, _ := io.ReadAll(res.Body)
- var m map[string]any
- _ = json.Unmarshal(raw, &m)
- return res.StatusCode, m
- }
- // TestRotateTokenHot: con hot:true y un router OpenWrt accesible por SSH,
- // el token se aplica en caliente (método hot + 1 comando SSH de push).
- func TestRotateTokenHot(t *testing.T) {
- ssh := &fakeSSH{}
- ts := makeRearmTestServer(t, ssh, 1500*time.Millisecond)
- status, body := postAgentCreateHot(t, ts.URL, ts.cookie, `{"slug":"patio","hot":true}`)
- if status != 201 {
- t.Fatalf("create: %d", status)
- }
- if body["method"] != "hot" {
- t.Fatalf("quiero method=hot, tuve %v", body["method"])
- }
- if _, ok := body["manualReason"]; ok {
- t.Fatalf("hot no debe traer manualReason, tuve %v", body["manualReason"])
- }
- if body["token"] == "" {
- t.Fatal("sin token")
- }
- if ssh.count() != 1 {
- t.Fatalf("quiero 1 comando SSH de push, tuve %d", ssh.count())
- }
- if !strings.Contains(ssh.cmds[0], "NETPULSE_TOKEN=") || !strings.Contains(ssh.cmds[0], `"$INIT" restart`) {
- t.Fatalf("el comando SSH no reescribe el token/restart: %q", ssh.cmds[0])
- }
- }
- // TestRotateTokenManual: sin router en la tabla (o hot=false) no hay push
- // SSH: método manual y el token se conserva en kv con el one-liner.
- func TestRotateTokenManual(t *testing.T) {
- ssh := &fakeSSH{}
- ts := makeRearmTestServer(t, ssh, 1500*time.Millisecond)
- // Slug sin router asociado → manual, sin SSH.
- status, body := postAgentCreateHot(t, ts.URL, ts.cookie, `{"slug":"solitario","hot":true}`)
- if status != 201 {
- t.Fatalf("create: %d", status)
- }
- if body["method"] != "manual" {
- t.Fatalf("quiero method=manual, tuve %v", body["method"])
- }
- if body["manualReason"] != "no_router" {
- t.Fatalf("sin router quiero manualReason=no_router, tuve %v", body["manualReason"])
- }
- if ssh.count() != 0 {
- t.Fatalf("no debe ejecutarse SSH con router desconocido, tuve %d", ssh.count())
- }
- if body["install"] == "" {
- t.Fatal("manual debe incluir el one-liner de instalación")
- }
- // hot=false → manual aunque el router exista (comportamiento previo).
- status, body = postAgentCreateHot(t, ts.URL, ts.cookie, `{"slug":"patio"}`)
- if status != 201 {
- t.Fatalf("create: %d", status)
- }
- if body["method"] != "manual" {
- t.Fatalf("sin hot=true quiero method=manual, tuve %v", body["method"])
- }
- if ssh.count() != 0 {
- t.Fatalf("sin hot=true no debe ejecutarse SSH, tuve %d", ssh.count())
- }
- }
- // TestRotateTokenHotSSHFail: si el push SSH falla, no se rompe el flujo y el
- // método cae a manual (el token ya rotado se entrega vía Install).
- func TestRotateTokenHotSSHFail(t *testing.T) {
- ssh := &fakeSSH{fail: true}
- ts := makeRearmTestServer(t, ssh, 1500*time.Millisecond)
- status, body := postAgentCreateHot(t, ts.URL, ts.cookie, `{"slug":"patio","hot":true}`)
- if status != 201 {
- t.Fatalf("create: %d", status)
- }
- if body["method"] != "manual" {
- t.Fatalf("SSH caído quiero method=manual, tuve %v", body["method"])
- }
- if body["manualReason"] != "ssh_failed" {
- t.Fatalf("SSH caído quiero manualReason=ssh_failed, tuve %v", body["manualReason"])
- }
- if body["install"] == "" {
- t.Fatal("debe incluir el one-liner para recuperar manualmente")
- }
- }
- // TestRotateTokenNetgrip (#719): con un agente kind=netgrip, hot:true no
- // toca SSH y devuelve manual con motivo netgrip.
- func TestRotateTokenNetgrip(t *testing.T) {
- ssh := &fakeSSH{}
- ts := makeRearmTestServer(t, ssh, 1500*time.Millisecond)
- ts.agents.Ingest(&probe.Payload{Router: "patio", Ts: time.Now().Unix(), Version: "0.23.0", Kind: "netgrip"})
- status, body := postAgentCreateHot(t, ts.URL, ts.cookie, `{"slug":"patio","hot":true}`)
- if status != 201 {
- t.Fatalf("create: %d", status)
- }
- if body["method"] != "manual" {
- t.Fatalf("netgrip quiero method=manual, tuve %v", body["method"])
- }
- if body["manualReason"] != "netgrip" {
- t.Fatalf("netgrip quiero manualReason=netgrip, tuve %v", body["manualReason"])
- }
- if ssh.count() != 0 {
- t.Fatalf("netgrip no debe ejecutarse SSH, tuve %d", ssh.count())
- }
- }
|