owut_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. }