| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370 |
- // firmware_owut_test.go - contrato HTTP del ciclo owut completo (#761):
- // lista de versiones, instalación, upgrade desatendido y recurrencia.
- package httpapi_test
- import (
- "bytes"
- "errors"
- "fmt"
- "io"
- "net/http"
- "strings"
- "testing"
- "time"
- )
- // owutVersionsSample replica `owut versions` (salida real recortada).
- const owutVersionsSample = `Available 'version-to' values from https://sysupgrade.openwrt.org:
- 24.10 release branch
- 24.10.0
- 24.10.2 (latest)
- 25.12 release branch
- 25.12.5
- 25.12.7 (latest)
- `
- func reqJSON(t *testing.T, method, base, path, payload, cookie string) *http.Response {
- t.Helper()
- var body io.Reader
- if payload != "" {
- body = bytes.NewBufferString(payload)
- }
- req, err := http.NewRequest(method, base+path, body)
- if err != nil {
- t.Fatalf("new request: %v", err)
- }
- req.Header.Set("Content-Type", "application/json")
- if cookie != "" {
- req.Header.Set("Cookie", "session="+cookie)
- }
- res, err := http.DefaultClient.Do(req)
- if err != nil {
- t.Fatalf("do: %v", err)
- }
- return res
- }
- func getJSONPath(t *testing.T, base, path, cookie string) *http.Response {
- return reqJSON(t, http.MethodGet, base, path, "", cookie)
- }
- func putJSONPath(t *testing.T, base, path, payload, cookie string) *http.Response {
- return reqJSON(t, http.MethodPut, base, path, payload, cookie)
- }
- func deleteJSONPath(t *testing.T, base, path, cookie string) *http.Response {
- return reqJSON(t, http.MethodDelete, base, path, "", cookie)
- }
- func TestOwutVersionsEndpoint(t *testing.T) {
- ssh := &scriptedSSH{rules: []sshRule{
- {contains: "command -v owut", out: "__owut__\n"},
- {contains: "owut versions", out: owutVersionsSample},
- }}
- ts, rid := makeOwutTestServer(t, ssh)
- _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
- res := getJSONPath(t, ts.URL, "/api/firmware-upgrades/"+rid+"/owut-versions", cookie)
- body := readJSON(t, res)
- if res.StatusCode != 200 {
- t.Fatalf("status %d: %v", res.StatusCode, body)
- }
- if body["owutAvailable"] != true {
- t.Fatalf("owutAvailable: %v", body["owutAvailable"])
- }
- versions, _ := body["versions"].([]any)
- if len(versions) != 4 {
- t.Fatalf("esperaba 4 versiones, got %v", versions)
- }
- }
- func TestOwutVersionsEndpointWithoutOwut(t *testing.T) {
- ssh := &scriptedSSH{rules: []sshRule{
- {contains: "command -v owut", out: "\n"}, // PATH sin owut
- }}
- ts, rid := makeOwutTestServer(t, ssh)
- _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
- res := getJSONPath(t, ts.URL, "/api/firmware-upgrades/"+rid+"/owut-versions", cookie)
- body := readJSON(t, res)
- if res.StatusCode != 200 {
- t.Fatalf("status %d: %v", res.StatusCode, body)
- }
- if body["owutAvailable"] != false {
- t.Fatalf("owutAvailable: %v", body["owutAvailable"])
- }
- if versions, _ := body["versions"].([]any); len(versions) != 0 {
- t.Fatalf("sin owut no hay versiones: %v", versions)
- }
- if ssh.saw("owut versions") {
- t.Fatalf("no debe lanzarse owut versions sin owut: %v", ssh.cmdsSnapshot())
- }
- }
- func TestOwutInstallEndpoint(t *testing.T) {
- ssh := &scriptedSSH{rules: []sshRule{
- {contains: "apk add owut", out: "OK: 1 installed\n__owut_exit__=0\n"},
- }}
- ts, rid := makeOwutTestServer(t, ssh)
- _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
- res := postJSON(t, ts.URL, "/api/firmware-upgrades/"+rid+"/owut-install", "{}", cookie)
- body := readJSON(t, res)
- if res.StatusCode != 200 {
- t.Fatalf("status %d: %v", res.StatusCode, body)
- }
- if body["ok"] != true {
- t.Fatalf("ok: %v", body)
- }
- if !ssh.saw("apk add owut") {
- t.Fatalf("no se lanzó la instalación: %v", ssh.cmdsSnapshot())
- }
- }
- func TestOwutInstallEndpointNoPackageManager(t *testing.T) {
- ssh := &scriptedSSH{rules: []sshRule{
- {contains: "apk update", out: "__no_pm__\n__owut_exit__=0\n"},
- }}
- ts, rid := makeOwutTestServer(t, ssh)
- _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
- res := postJSON(t, ts.URL, "/api/firmware-upgrades/"+rid+"/owut-install", "{}", cookie)
- if res.StatusCode != 500 {
- t.Fatalf("status %d, esperaba 500", res.StatusCode)
- }
- }
- // waitForUpgradeStatus polea la lista hasta que el upgrade llegue al estado
- // terminal (la goroutine del server corre en background).
- func waitForUpgradeStatus(t *testing.T, ts *testServer, rid, want, cookie string) map[string]any {
- t.Helper()
- deadline := time.Now().Add(5 * time.Second)
- for time.Now().Before(deadline) {
- res := getJSONPath(t, ts.URL, "/api/firmware-upgrades", cookie)
- for _, it := range readJSONArray(t, res) {
- m, _ := it.(map[string]any)
- if m["routerId"] != rid {
- continue
- }
- if up, ok := m["upgrade"].(map[string]any); ok && up["status"] == want {
- return up
- }
- }
- time.Sleep(50 * time.Millisecond)
- }
- t.Fatalf("el upgrade no llegó a %s", want)
- return nil
- }
- func TestOwutUpgradeEndpointNoChanges(t *testing.T) {
- ssh := &scriptedSSH{rules: []sshRule{
- {contains: "command -v owut", out: "__owut__\n"},
- {contains: "owut upgrade", out: "There are no changes to upgrade (see '--force')\n__owut_exit__=0\n"},
- }}
- ts, rid := makeOwutTestServer(t, ssh)
- _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
- res := postJSON(t, ts.URL, "/api/firmware-upgrades/"+rid+"/owut-upgrade", `{"targetVersion":"25.12.7"}`, cookie)
- body := readJSON(t, res)
- if res.StatusCode != 202 {
- t.Fatalf("status %d: %v", res.StatusCode, body)
- }
- if body["engine"] != "owut" {
- t.Fatalf("engine: %v", body["engine"])
- }
- up := waitForUpgradeStatus(t, ts, rid, "done", cookie)
- if up["engine"] != "owut" {
- t.Fatalf("engine del upgrade: %v", up["engine"])
- }
- if !ssh.saw("owut upgrade -q") {
- t.Fatalf("comando lanzado: %v", ssh.cmdsSnapshot())
- }
- // #761: antes de flashear se preserva el stack en /etc/sysupgrade.conf.
- if !ssh.saw("/etc/sysupgrade.conf") {
- t.Fatalf("preserve-stack no se ejecutó: %v", ssh.cmdsSnapshot())
- }
- }
- func TestOwutUpgradeEndpointFailure(t *testing.T) {
- ssh := &scriptedSSH{rules: []sshRule{
- {contains: "command -v owut", out: "__owut__\n"},
- {contains: "owut upgrade", out: "Update checks reveal errors, can't proceed\n__owut_exit__=1\n"},
- }}
- ts, rid := makeOwutTestServer(t, ssh)
- _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
- res := postJSON(t, ts.URL, "/api/firmware-upgrades/"+rid+"/owut-upgrade", `{"targetVersion":"25.12.7"}`, cookie)
- if res.StatusCode != 202 {
- t.Fatalf("status %d", res.StatusCode)
- }
- up := waitForUpgradeStatus(t, ts, rid, "failed", cookie)
- if msg, _ := up["error"].(string); !strings.Contains(msg, "can't proceed") {
- t.Fatalf("error del upgrade: %v", up["error"])
- }
- }
- func TestOwutUpgradeRequiresTarget(t *testing.T) {
- ts, rid := makeOwutTestServer(t, nil)
- _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
- res := postJSON(t, ts.URL, "/api/firmware-upgrades/"+rid+"/owut-upgrade", `{}`, cookie)
- if res.StatusCode != 400 {
- t.Fatalf("status %d, esperaba 400 sin targetVersion", res.StatusCode)
- }
- }
- // #1769: el download del flujo por pasos propaga removePackages a owut -r,
- // igual que el one-shot; sin eso ASU rechaza routers con paquetes de
- // terceros y el paso final se queda sin imagen.
- func TestOwutDownloadEndpointPassesRemovePackages(t *testing.T) {
- ssh := &scriptedSSH{rules: []sshRule{
- {contains: "command -v owut", out: "__owut__\n"},
- {contains: "owut download", out: "Downloading image to /tmp/firmware.bin\n__owut_exit__=0\n"},
- }}
- ts, rid := makeOwutTestServer(t, ssh)
- _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
- res := postJSON(t, ts.URL, "/api/firmware-upgrades/"+rid+"/owut-download",
- `{"targetVersion":"25.12.6","removePackages":["netgrip"]}`, cookie)
- body := readJSON(t, res)
- if res.StatusCode != 200 {
- t.Fatalf("status %d: %v", res.StatusCode, body)
- }
- if !ssh.saw("owut download -r 'netgrip'") {
- t.Fatalf("comando lanzado: %v", ssh.cmdsSnapshot())
- }
- }
- // #1769: instalar sin imagen previa no entra en la máquina de estados:
- // 422 con código accionable y ningún upgrade creado.
- func TestOwutInstallImageRequiresDownloadedImage(t *testing.T) {
- ssh := &scriptedSSH{rules: []sshRule{
- {contains: "command -v owut", out: "__owut__\n"},
- {contains: "test -s /tmp/firmware.bin", err: errors.New("exit 1")},
- }}
- ts, rid := makeOwutTestServer(t, ssh)
- _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
- res := postJSON(t, ts.URL, "/api/firmware-upgrades/"+rid+"/owut-install-image", `{}`, cookie)
- body := readJSON(t, res)
- if res.StatusCode != http.StatusPreconditionFailed {
- t.Fatalf("status %d: %v", res.StatusCode, body)
- }
- errObj, _ := body["error"].(string)
- if errObj != "owut_no_image" {
- t.Fatalf("error: %v", body)
- }
- deadline := time.Now().Add(2 * time.Second)
- for time.Now().Before(deadline) {
- res := getJSONPath(t, ts.URL, "/api/firmware-upgrades", cookie)
- for _, it := range readJSONArray(t, res) {
- m, _ := it.(map[string]any)
- if m["routerId"] == rid {
- if up, ok := m["upgrade"].(map[string]any); ok {
- t.Fatalf("no debió crearse ningún upgrade: %v", up)
- }
- }
- }
- time.Sleep(50 * time.Millisecond)
- }
- }
- func TestRecurrencePutGetDelete(t *testing.T) {
- ts, rid := makeOwutTestServer(t, nil)
- _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
- // weekly válido.
- res := putJSONPath(t, ts.URL, "/api/firmware-upgrades/"+rid+"/recurrence",
- `{"enabled":true,"kind":"weekly","dayOfWeek":5,"time":"04:00"}`, cookie)
- if res.StatusCode != 200 {
- t.Fatalf("PUT weekly: %d %v", res.StatusCode, readJSON(t, res))
- }
- res = getJSONPath(t, ts.URL, "/api/firmware-upgrades/"+rid+"/recurrence", cookie)
- body := readJSON(t, res)
- rc, _ := body["recurrence"].(map[string]any)
- if rc["kind"] != "weekly" || rc["enabled"] != true {
- t.Fatalf("GET recurrence: %v", body)
- }
- if _, ok := body["nextRunMs"]; !ok {
- t.Fatalf("GET recurrence debe traer nextRunMs: %v", body)
- }
- // weekly sin time: 400.
- res = putJSONPath(t, ts.URL, "/api/firmware-upgrades/"+rid+"/recurrence",
- `{"enabled":true,"kind":"weekly","dayOfWeek":5}`, cookie)
- if res.StatusCode != 400 {
- t.Fatalf("PUT inválido: %d, esperaba 400", res.StatusCode)
- }
- // monthly y once válidos.
- res = putJSONPath(t, ts.URL, "/api/firmware-upgrades/"+rid+"/recurrence",
- `{"enabled":true,"kind":"monthly","dayOfMonth":1,"time":"05:30"}`, cookie)
- if res.StatusCode != 200 {
- t.Fatalf("PUT monthly: %d", res.StatusCode)
- }
- future := time.Now().Add(48 * time.Hour).UnixMilli()
- res = putJSONPath(t, ts.URL, "/api/firmware-upgrades/"+rid+"/recurrence",
- fmt.Sprintf(`{"enabled":true,"kind":"once","atMs":%d}`, future), cookie)
- if res.StatusCode != 200 {
- t.Fatalf("PUT once: %d", res.StatusCode)
- }
- // DELETE desactiva.
- res = deleteJSONPath(t, ts.URL, "/api/firmware-upgrades/"+rid+"/recurrence", cookie)
- if res.StatusCode != 200 {
- t.Fatalf("DELETE: %d", res.StatusCode)
- }
- res = getJSONPath(t, ts.URL, "/api/firmware-upgrades/"+rid+"/recurrence", cookie)
- body = readJSON(t, res)
- rc, _ = body["recurrence"].(map[string]any)
- if rc["enabled"] != false {
- t.Fatalf("tras DELETE debe quedar disabled: %v", body)
- }
- }
- func TestOwutEndpointsBlockedForVendorFirmware(t *testing.T) {
- // GL.iNet: la plataforma devuelve __gl__ -> install y upgrade 422, y el
- // listado de versiones llega con vendorFirmware y sin sondear ASU.
- ssh := &scriptedSSH{rules: []sshRule{
- {contains: "fw.gl-inet.com", out: "__gl__\n"},
- }}
- ts, rid := makeOwutTestServer(t, ssh)
- _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
- res := postJSON(t, ts.URL, "/api/firmware-upgrades/"+rid+"/owut-install", "{}", cookie)
- if res.StatusCode != 422 {
- t.Fatalf("install status %d, esperaba 422 vendor_firmware", res.StatusCode)
- }
- res = postJSON(t, ts.URL, "/api/firmware-upgrades/"+rid+"/owut-upgrade", `{"targetVersion":"25.12.7"}`, cookie)
- if res.StatusCode != 422 {
- t.Fatalf("upgrade status %d, esperaba 422 vendor_firmware", res.StatusCode)
- }
- res = getJSONPath(t, ts.URL, "/api/firmware-upgrades/"+rid+"/owut-versions", cookie)
- body := readJSON(t, res)
- if body["vendorFirmware"] != "gl-inet" {
- t.Fatalf("vendorFirmware: %v", body["vendorFirmware"])
- }
- if versions, _ := body["versions"].([]any); len(versions) != 0 {
- t.Fatalf("vendor no debe listar versiones: %v", versions)
- }
- if ssh.saw("owut versions") {
- t.Fatalf("vendor no debe sondear versiones ASU: %v", ssh.cmdsSnapshot())
- }
- }
- func TestTargetSaveWithoutURL(t *testing.T) {
- // #761: con owut la imagen la construye ASU; el target se guarda sin URL.
- ts, rid := makeOwutTestServer(t, nil)
- _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
- res := postJSON(t, ts.URL, "/api/firmware-upgrades/"+rid+"/target",
- `{"model":"redmi_ax6","currentVersion":"25.12.5","targetVersion":"25.12.5"}`, cookie)
- if res.StatusCode != 200 {
- t.Fatalf("guardar sin targetUrl: %d %v", res.StatusCode, readJSON(t, res))
- }
- res = getJSONPath(t, ts.URL, "/api/firmware-upgrades", cookie)
- for _, it := range readJSONArray(t, res) {
- m, _ := it.(map[string]any)
- if m["routerId"] == rid && m["targetVersion"] != "25.12.5" {
- t.Fatalf("target no persistió: %v", m)
- }
- }
- }
|