owut_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // owut_test.go - tests de detección y check de owut (#695) con un Runner
  2. // fakeable. Sin SSH real: un fake por subcadena de comando cubre owut ausente,
  3. // owut presente + check OK con upgrade, y owut presente + check con fallo de
  4. // infra ASU.
  5. package firmware
  6. import (
  7. "errors"
  8. "strings"
  9. "testing"
  10. "time"
  11. )
  12. // errNoCommand simula `command -v owut` sin resultado (owut ausente).
  13. // errSSHDown simula un fallo de transporte SSH (router caído).
  14. var (
  15. errNoCommand = errors.New("exit status 127")
  16. errSSHDown = errors.New("ssh 192.168.1.50: dial tcp: connection refused")
  17. )
  18. // fakeRunner devuelve, por subcadena del comando, una salida y un error fijos.
  19. type fakeRunner struct {
  20. rules []fakeRule
  21. cmds []string
  22. }
  23. type fakeRule struct {
  24. contains string
  25. out string
  26. err error
  27. }
  28. func (f *fakeRunner) Run(_ string, cmd string, _ time.Duration) (string, error) {
  29. f.cmds = append(f.cmds, cmd)
  30. for _, r := range f.rules {
  31. if strings.Contains(cmd, r.contains) {
  32. return r.out, r.err
  33. }
  34. }
  35. return "", nil
  36. }
  37. func (f *fakeRunner) saw(substr string) bool {
  38. for _, c := range f.cmds {
  39. if strings.Contains(c, substr) {
  40. return true
  41. }
  42. }
  43. return false
  44. }
  45. // checkOKOut simula la salida de `owut check` con upgrade disponible y
  46. // buildable (exit 0), según la fuente oficial de owut.
  47. const checkOKOut = `ASU-Server https://sysupgrade.openwrt.org
  48. Device Redmi AX6
  49. 3 packages are out-of-date
  50. It is safe to proceed with an upgrade (re-run with '--verbose' for details)
  51. __owut_exit__=0
  52. `
  53. // checkInfraFailOut simula la salida de `owut check` cuando la infra ASU no
  54. // puede construir (exit 1: paquetes missing en el target).
  55. const checkInfraFailOut = `ASU-Server https://sysupgrade.openwrt.org
  56. Device Redmi AX6
  57. 2 packages missing in target version, cannot upgrade
  58. ERROR: Checks reveal errors, do not upgrade (re-run with '--verbose' for details)
  59. __owut_exit__=1
  60. `
  61. func TestCheckOwutAbsent(t *testing.T) {
  62. r := &fakeRunner{rules: []fakeRule{
  63. {contains: "command -v owut", err: errNoCommand},
  64. }}
  65. got := CheckOwut(r, "192.168.1.50")
  66. if got.OwutAvailable {
  67. t.Fatalf("OwutAvailable = true, esperaba false")
  68. }
  69. if got.CheckRan {
  70. t.Fatalf("CheckRan = true, esperaba false sin owut")
  71. }
  72. if r.saw("owut check") {
  73. t.Fatalf("no debería lanzarse owut check si owut no existe")
  74. }
  75. }
  76. func TestCheckOwutPresentUpgradeAvailable(t *testing.T) {
  77. r := &fakeRunner{rules: []fakeRule{
  78. {contains: "command -v owut", out: "/usr/bin/owut\n"},
  79. {contains: "owut check", out: checkOKOut},
  80. }}
  81. got := CheckOwut(r, "192.168.1.50")
  82. if !got.OwutAvailable {
  83. t.Fatalf("OwutAvailable = false, esperaba true")
  84. }
  85. if !got.CheckRan {
  86. t.Fatalf("CheckRan = false, esperaba true")
  87. }
  88. if !got.UpgradeAvailable {
  89. t.Fatalf("UpgradeAvailable = false, esperaba true (out-of-date)")
  90. }
  91. if !got.Buildable {
  92. t.Fatalf("Buildable = false, esperaba true (exit 0)")
  93. }
  94. if strings.Contains(got.RawOutput, "__owut_exit__") {
  95. t.Fatalf("RawOutput no debe contener el marcador __owut_exit__: %q", got.RawOutput)
  96. }
  97. if !strings.Contains(got.RawOutput, "safe to proceed") {
  98. t.Fatalf("RawOutput perdió contenido: %q", got.RawOutput)
  99. }
  100. }
  101. func TestCheckOwutPresentInfraFailure(t *testing.T) {
  102. r := &fakeRunner{rules: []fakeRule{
  103. {contains: "command -v owut", out: "/usr/bin/owut\n"},
  104. {contains: "owut check", out: checkInfraFailOut},
  105. }}
  106. got := CheckOwut(r, "192.168.1.50")
  107. if !got.OwutAvailable {
  108. t.Fatalf("OwutAvailable = false, esperaba true")
  109. }
  110. if !got.CheckRan {
  111. t.Fatalf("CheckRan = false, esperaba true (el check corrió aunque fallara)")
  112. }
  113. if got.Buildable {
  114. t.Fatalf("Buildable = true, esperaba false (exit 1, cannot upgrade)")
  115. }
  116. if got.UpgradeAvailable {
  117. t.Fatalf("UpgradeAvailable = true, esperaba false (cannot upgrade)")
  118. }
  119. if !strings.Contains(got.RawOutput, "cannot upgrade") {
  120. t.Fatalf("RawOutput debe conservar el fallo: %q", got.RawOutput)
  121. }
  122. }
  123. func TestCheckOwutSSHFailure(t *testing.T) {
  124. // Detección OK, pero el check falla por transporte (router caído): el
  125. // resultado lleva el Error y CheckRan queda en false.
  126. r := &fakeRunner{rules: []fakeRule{
  127. {contains: "command -v owut", out: "/usr/bin/owut\n"},
  128. {contains: "owut check", err: errSSHDown},
  129. }}
  130. got := CheckOwut(r, "192.168.1.50")
  131. if !got.OwutAvailable {
  132. t.Fatalf("OwutAvailable = false, esperaba true")
  133. }
  134. if got.CheckRan {
  135. t.Fatalf("CheckRan = true, esperaba false (SSH caído)")
  136. }
  137. if got.Error == "" {
  138. t.Fatalf("Error vacío, esperaba el fallo de transporte")
  139. }
  140. }
  141. func TestCheckOwutNilRunner(t *testing.T) {
  142. got := CheckOwut(nil, "192.168.1.50")
  143. if got.OwutAvailable || got.CheckRan || got.Buildable || got.UpgradeAvailable {
  144. t.Fatalf("runner nil debe devolver todo a false: %+v", got)
  145. }
  146. }
  147. // #1773: el Version-to del check es la fuente fiable del siguiente destino.
  148. func TestParseOwutVersionTo(t *testing.T) {
  149. out := "Version-from 25.12.5 r33311-981c978eff\n" +
  150. "Version-to 25.12.6 r33311-981c978eff (kernel 6.12.112)\n" +
  151. "ERROR: Checks reveal errors, do not upgrade\n"
  152. if got := parseOwutVersionTo(out); got != "25.12.6" {
  153. t.Fatalf("parseOwutVersionTo = %q, esperaba 25.12.6", got)
  154. }
  155. if got := parseOwutVersionTo("sin linea Version-to\n"); got != "" {
  156. t.Fatalf("parseOwutVersionTo = %q, esperaba vacio", got)
  157. }
  158. }
  159. // #1773: CheckOwut expone el VersionTo del check en el payload.
  160. func TestCheckOwutExposesVersionTo(t *testing.T) {
  161. r := &fakeRunner{rules: []fakeRule{
  162. {contains: "command -v owut", out: "/usr/bin/owut\n"},
  163. {contains: "owut check", out: checkOKOut + "\nVersion-to 25.12.6 r33311-981c978eff (kernel 6.12.112)\n"},
  164. }}
  165. got := CheckOwut(r, "192.168.1.50")
  166. if got.VersionTo != "25.12.6" {
  167. t.Fatalf("VersionTo = %q, esperaba 25.12.6", got.VersionTo)
  168. }
  169. }
  170. // #1769: la lista que el router ya excluye de sus propias builds sale del
  171. // check, para que la UI la ofrezca en vez de pedir que se retipee. La línea
  172. // del ejemplo es la que reportó David en el issue.
  173. func TestParseUciRemoveList(t *testing.T) {
  174. got := ParseUciRemoveList("attendedsysupgrade.owut.remove='bandix' 'luci-app-bandix'\n")
  175. if len(got) != 2 || got[0] != "bandix" || got[1] != "luci-app-bandix" {
  176. t.Fatalf("ParseUciRemoveList = %v, esperaba [bandix luci-app-bandix]", got)
  177. }
  178. }
  179. func TestParseUciRemoveListEmpty(t *testing.T) {
  180. for _, out := range []string{"", "\n", "uci: Entry not found\n"} {
  181. if got := ParseUciRemoveList(out); len(got) != 0 {
  182. t.Fatalf("ParseUciRemoveList(%q) = %v, esperaba vacío", out, got)
  183. }
  184. }
  185. }
  186. func TestParseUciRemoveListIgnoresOtherOptions(t *testing.T) {
  187. out := "attendedsysupgrade.owut.server='https://sysupgrade.openwrt.org'\n" +
  188. "attendedsysupgrade.owut.remove='netgrip' 'netgrip'\n"
  189. got := ParseUciRemoveList(out)
  190. if len(got) != 1 || got[0] != "netgrip" {
  191. t.Fatalf("ParseUciRemoveList = %v, esperaba [netgrip] sin duplicados", got)
  192. }
  193. }
  194. // #1769: CheckOwut devuelve la lista configurada en el router junto al resto
  195. // del estado, y un fallo de uci no rompe el check.
  196. func TestCheckOwutReadsConfiguredRemoveList(t *testing.T) {
  197. r := &fakeRunner{rules: []fakeRule{
  198. {contains: "command -v owut", out: "/usr/bin/owut"},
  199. {contains: "owut check", out: checkOKOut},
  200. {contains: "attendedsysupgrade.owut.remove", out: "attendedsysupgrade.owut.remove='bandix' 'luci-app-bandix'\n"},
  201. }}
  202. got := CheckOwut(r, "192.168.1.50")
  203. if !r.saw("uci show attendedsysupgrade.owut.remove") {
  204. t.Fatal("CheckOwut no consultó la lista configurada en el router")
  205. }
  206. if len(got.RemoveConfigured) != 2 {
  207. t.Fatalf("RemoveConfigured = %v, esperaba dos paquetes", got.RemoveConfigured)
  208. }
  209. }
  210. func TestCheckOwutSurvivesUciFailure(t *testing.T) {
  211. r := &fakeRunner{rules: []fakeRule{
  212. {contains: "command -v owut", out: "/usr/bin/owut"},
  213. {contains: "owut check", out: checkOKOut},
  214. {contains: "attendedsysupgrade.owut.remove", err: errors.New("uci: not found")},
  215. }}
  216. got := CheckOwut(r, "192.168.1.50")
  217. if len(got.RemoveConfigured) != 0 {
  218. t.Fatalf("RemoveConfigured = %v, esperaba vacío", got.RemoveConfigured)
  219. }
  220. if !got.CheckRan || !got.OwutAvailable {
  221. t.Fatal("un fallo de uci no debe afectar al resto del check")
  222. }
  223. }