| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- // owut_test.go - tests de detección y check de owut (#695) con un Runner
- // fakeable. Sin SSH real: un fake por subcadena de comando cubre owut ausente,
- // owut presente + check OK con upgrade, y owut presente + check con fallo de
- // infra ASU.
- package firmware
- import (
- "errors"
- "strings"
- "testing"
- "time"
- )
- // errNoCommand simula `command -v owut` sin resultado (owut ausente).
- // errSSHDown simula un fallo de transporte SSH (router caído).
- var (
- errNoCommand = errors.New("exit status 127")
- errSSHDown = errors.New("ssh 192.168.1.50: dial tcp: connection refused")
- )
- // fakeRunner devuelve, por subcadena del comando, una salida y un error fijos.
- type fakeRunner struct {
- rules []fakeRule
- cmds []string
- }
- type fakeRule struct {
- contains string
- out string
- err error
- }
- func (f *fakeRunner) Run(_ string, cmd string, _ time.Duration) (string, error) {
- f.cmds = append(f.cmds, cmd)
- for _, r := range f.rules {
- if strings.Contains(cmd, r.contains) {
- return r.out, r.err
- }
- }
- return "", nil
- }
- func (f *fakeRunner) saw(substr string) bool {
- for _, c := range f.cmds {
- if strings.Contains(c, substr) {
- return true
- }
- }
- return false
- }
- // checkOKOut simula la salida de `owut check` con upgrade disponible y
- // buildable (exit 0), según la fuente oficial de owut.
- const checkOKOut = `ASU-Server https://sysupgrade.openwrt.org
- Device Redmi AX6
- 3 packages are out-of-date
- It is safe to proceed with an upgrade (re-run with '--verbose' for details)
- __owut_exit__=0
- `
- // checkInfraFailOut simula la salida de `owut check` cuando la infra ASU no
- // puede construir (exit 1: paquetes missing en el target).
- const checkInfraFailOut = `ASU-Server https://sysupgrade.openwrt.org
- Device Redmi AX6
- 2 packages missing in target version, cannot upgrade
- ERROR: Checks reveal errors, do not upgrade (re-run with '--verbose' for details)
- __owut_exit__=1
- `
- func TestCheckOwutAbsent(t *testing.T) {
- r := &fakeRunner{rules: []fakeRule{
- {contains: "command -v owut", err: errNoCommand},
- }}
- got := CheckOwut(r, "192.168.1.50")
- if got.OwutAvailable {
- t.Fatalf("OwutAvailable = true, esperaba false")
- }
- if got.CheckRan {
- t.Fatalf("CheckRan = true, esperaba false sin owut")
- }
- if r.saw("owut check") {
- t.Fatalf("no debería lanzarse owut check si owut no existe")
- }
- }
- func TestCheckOwutPresentUpgradeAvailable(t *testing.T) {
- r := &fakeRunner{rules: []fakeRule{
- {contains: "command -v owut", out: "/usr/bin/owut\n"},
- {contains: "owut check", out: checkOKOut},
- }}
- got := CheckOwut(r, "192.168.1.50")
- if !got.OwutAvailable {
- t.Fatalf("OwutAvailable = false, esperaba true")
- }
- if !got.CheckRan {
- t.Fatalf("CheckRan = false, esperaba true")
- }
- if !got.UpgradeAvailable {
- t.Fatalf("UpgradeAvailable = false, esperaba true (out-of-date)")
- }
- if !got.Buildable {
- t.Fatalf("Buildable = false, esperaba true (exit 0)")
- }
- if strings.Contains(got.RawOutput, "__owut_exit__") {
- t.Fatalf("RawOutput no debe contener el marcador __owut_exit__: %q", got.RawOutput)
- }
- if !strings.Contains(got.RawOutput, "safe to proceed") {
- t.Fatalf("RawOutput perdió contenido: %q", got.RawOutput)
- }
- }
- func TestCheckOwutPresentInfraFailure(t *testing.T) {
- r := &fakeRunner{rules: []fakeRule{
- {contains: "command -v owut", out: "/usr/bin/owut\n"},
- {contains: "owut check", out: checkInfraFailOut},
- }}
- got := CheckOwut(r, "192.168.1.50")
- if !got.OwutAvailable {
- t.Fatalf("OwutAvailable = false, esperaba true")
- }
- if !got.CheckRan {
- t.Fatalf("CheckRan = false, esperaba true (el check corrió aunque fallara)")
- }
- if got.Buildable {
- t.Fatalf("Buildable = true, esperaba false (exit 1, cannot upgrade)")
- }
- if got.UpgradeAvailable {
- t.Fatalf("UpgradeAvailable = true, esperaba false (cannot upgrade)")
- }
- if !strings.Contains(got.RawOutput, "cannot upgrade") {
- t.Fatalf("RawOutput debe conservar el fallo: %q", got.RawOutput)
- }
- }
- func TestCheckOwutSSHFailure(t *testing.T) {
- // Detección OK, pero el check falla por transporte (router caído): el
- // resultado lleva el Error y CheckRan queda en false.
- r := &fakeRunner{rules: []fakeRule{
- {contains: "command -v owut", out: "/usr/bin/owut\n"},
- {contains: "owut check", err: errSSHDown},
- }}
- got := CheckOwut(r, "192.168.1.50")
- if !got.OwutAvailable {
- t.Fatalf("OwutAvailable = false, esperaba true")
- }
- if got.CheckRan {
- t.Fatalf("CheckRan = true, esperaba false (SSH caído)")
- }
- if got.Error == "" {
- t.Fatalf("Error vacío, esperaba el fallo de transporte")
- }
- }
- func TestCheckOwutNilRunner(t *testing.T) {
- got := CheckOwut(nil, "192.168.1.50")
- if got.OwutAvailable || got.CheckRan || got.Buildable || got.UpgradeAvailable {
- t.Fatalf("runner nil debe devolver todo a false: %+v", got)
- }
- }
- // #1773: el Version-to del check es la fuente fiable del siguiente destino.
- func TestParseOwutVersionTo(t *testing.T) {
- out := "Version-from 25.12.5 r33311-981c978eff\n" +
- "Version-to 25.12.6 r33311-981c978eff (kernel 6.12.112)\n" +
- "ERROR: Checks reveal errors, do not upgrade\n"
- if got := parseOwutVersionTo(out); got != "25.12.6" {
- t.Fatalf("parseOwutVersionTo = %q, esperaba 25.12.6", got)
- }
- if got := parseOwutVersionTo("sin linea Version-to\n"); got != "" {
- t.Fatalf("parseOwutVersionTo = %q, esperaba vacio", got)
- }
- }
- // #1773: CheckOwut expone el VersionTo del check en el payload.
- func TestCheckOwutExposesVersionTo(t *testing.T) {
- r := &fakeRunner{rules: []fakeRule{
- {contains: "command -v owut", out: "/usr/bin/owut\n"},
- {contains: "owut check", out: checkOKOut + "\nVersion-to 25.12.6 r33311-981c978eff (kernel 6.12.112)\n"},
- }}
- got := CheckOwut(r, "192.168.1.50")
- if got.VersionTo != "25.12.6" {
- t.Fatalf("VersionTo = %q, esperaba 25.12.6", got.VersionTo)
- }
- }
- // #1769: la lista que el router ya excluye de sus propias builds sale del
- // check, para que la UI la ofrezca en vez de pedir que se retipee. La línea
- // del ejemplo es la que reportó David en el issue.
- func TestParseUciRemoveList(t *testing.T) {
- got := ParseUciRemoveList("attendedsysupgrade.owut.remove='bandix' 'luci-app-bandix'\n")
- if len(got) != 2 || got[0] != "bandix" || got[1] != "luci-app-bandix" {
- t.Fatalf("ParseUciRemoveList = %v, esperaba [bandix luci-app-bandix]", got)
- }
- }
- func TestParseUciRemoveListEmpty(t *testing.T) {
- for _, out := range []string{"", "\n", "uci: Entry not found\n"} {
- if got := ParseUciRemoveList(out); len(got) != 0 {
- t.Fatalf("ParseUciRemoveList(%q) = %v, esperaba vacío", out, got)
- }
- }
- }
- func TestParseUciRemoveListIgnoresOtherOptions(t *testing.T) {
- out := "attendedsysupgrade.owut.server='https://sysupgrade.openwrt.org'\n" +
- "attendedsysupgrade.owut.remove='netgrip' 'netgrip'\n"
- got := ParseUciRemoveList(out)
- if len(got) != 1 || got[0] != "netgrip" {
- t.Fatalf("ParseUciRemoveList = %v, esperaba [netgrip] sin duplicados", got)
- }
- }
- // #1769: CheckOwut devuelve la lista configurada en el router junto al resto
- // del estado, y un fallo de uci no rompe el check.
- func TestCheckOwutReadsConfiguredRemoveList(t *testing.T) {
- r := &fakeRunner{rules: []fakeRule{
- {contains: "command -v owut", out: "/usr/bin/owut"},
- {contains: "owut check", out: checkOKOut},
- {contains: "attendedsysupgrade.owut.remove", out: "attendedsysupgrade.owut.remove='bandix' 'luci-app-bandix'\n"},
- }}
- got := CheckOwut(r, "192.168.1.50")
- if !r.saw("uci show attendedsysupgrade.owut.remove") {
- t.Fatal("CheckOwut no consultó la lista configurada en el router")
- }
- if len(got.RemoveConfigured) != 2 {
- t.Fatalf("RemoveConfigured = %v, esperaba dos paquetes", got.RemoveConfigured)
- }
- }
- func TestCheckOwutSurvivesUciFailure(t *testing.T) {
- r := &fakeRunner{rules: []fakeRule{
- {contains: "command -v owut", out: "/usr/bin/owut"},
- {contains: "owut check", out: checkOKOut},
- {contains: "attendedsysupgrade.owut.remove", err: errors.New("uci: not found")},
- }}
- got := CheckOwut(r, "192.168.1.50")
- if len(got.RemoveConfigured) != 0 {
- t.Fatalf("RemoveConfigured = %v, esperaba vacío", got.RemoveConfigured)
- }
- if !got.CheckRan || !got.OwutAvailable {
- t.Fatal("un fallo de uci no debe afectar al resto del check")
- }
- }
|