sqm_test.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package orchestr
  2. import "testing"
  3. // Fixture real de rt2 (Redmi AX6, OpenWrt apk) — sqm-scripts NO instalado,
  4. // sección sqm.eth1=queue preexistente con enabled=0 (config del paquete).
  5. const sqmOut = `===PKG_MGR===
  6. apk
  7. ===INSTALLED===
  8. no
  9. ===SQM_UCI===
  10. sqm.eth1=queue
  11. sqm.eth1.enabled='0'
  12. sqm.eth1.interface='eth1'
  13. sqm.eth1.download='85000'
  14. sqm.eth1.upload='10000'
  15. sqm.eth1.qdisc='cake'
  16. sqm.eth1.script='piece_of_cake.qos'
  17. sqm.eth1.qdisc_advanced='0'
  18. sqm.eth1.ingress_ecn='ECN'
  19. sqm.eth1.egress_ecn='ECN'
  20. sqm.eth1.qdisc_really_really_advanced='0'
  21. sqm.eth1.itarget='auto'
  22. sqm.eth1.etarget='auto'
  23. sqm.eth1.linklayer='none'
  24. ===END===`
  25. // TestParseSqm: gestor apk, NO instalado, sección eth1 detectada con sus
  26. // options interface/enabled.
  27. func TestParseSqm(t *testing.T) {
  28. sc := parseSqm(sqmOut)
  29. if sc.Manager != "apk" {
  30. t.Errorf("Manager: got %q want apk", sc.Manager)
  31. }
  32. if sc.Installed {
  33. t.Error("Installed: esperaba false (sqm-scripts no instalado)")
  34. }
  35. if len(sc.Sections) != 1 {
  36. t.Fatalf("Sections: got %d want 1", len(sc.Sections))
  37. }
  38. sec := sc.Sections[0]
  39. if sec.Idx != "eth1" {
  40. t.Errorf("Sections[0].Idx: got %q want eth1", sec.Idx)
  41. }
  42. if sec.Interface != "eth1" {
  43. t.Errorf("Sections[0].Interface: got %q want eth1", sec.Interface)
  44. }
  45. if sec.Enabled {
  46. t.Error("Sections[0].Enabled: esperaba false (enabled='0')")
  47. }
  48. }
  49. // TestParseSqmSinConfig: sin sqm en uci → sin secciones.
  50. func TestParseSqmSinConfig(t *testing.T) {
  51. out := "===PKG_MGR===\napk\n===INSTALLED===\nno\n===SQM_UCI===\n===END==="
  52. sc := parseSqm(out)
  53. if sc.Manager != "apk" {
  54. t.Errorf("Manager: got %q want apk", sc.Manager)
  55. }
  56. if len(sc.Sections) != 0 {
  57. t.Errorf("Sections: got %d want 0", len(sc.Sections))
  58. }
  59. }
  60. // TestSqmEnableConSeccionExistente: enable reusa la sección eth1, instala
  61. // sqm-scripts (apk) y configura. Sin uci_add (sección ya existe).
  62. func TestSqmEnableConSeccionExistente(t *testing.T) {
  63. sc := parseSqm(sqmOut)
  64. desired := SqmDesired{
  65. Enabled: true,
  66. Interface: "eth1",
  67. Download: "90000",
  68. Upload: "15000",
  69. }
  70. ops := SqmOps(desired, sc)
  71. if len(ops) < 10 {
  72. t.Fatalf("ops: got %d, esperaba >= 10", len(ops))
  73. }
  74. // Primer op: apk_install sqm-scripts.
  75. if ops[0].Kind != "apk_install" || ops[0].Args["package"] != "sqm-scripts" {
  76. t.Errorf("ops[0]: got %v, esperaba apk_install sqm-scripts", ops[0])
  77. }
  78. // Sin uci_add (la sección eth1 ya existe).
  79. for _, op := range ops {
  80. if op.Kind == "uci_add" {
  81. t.Errorf("no esperaba uci_add: la sección eth1 ya existe")
  82. }
  83. }
  84. // La sección gestionada es eth1 (no @queue[-1]).
  85. found := false
  86. for _, op := range ops {
  87. if op.Kind == "uci_set" && op.Args["section"] == "eth1" && op.Args["option"] == "enabled" {
  88. found = true
  89. if op.Args["value"] != "1" {
  90. t.Errorf("enabled: got %q want 1", op.Args["value"])
  91. }
  92. }
  93. }
  94. if !found {
  95. t.Error("esperaba uci_set sqm.eth1.enabled=1")
  96. }
  97. if err := validateSqmOps(ops); err != nil {
  98. t.Fatalf("validateSqmOps: %v", err)
  99. }
  100. }
  101. // TestSqmEnableCreaSeccion: sin sección previa → uci_add + @queue[-1].
  102. func TestSqmEnableCreaSeccion(t *testing.T) {
  103. out := "===PKG_MGR===\nopkg\n===INSTALLED===\nno\n===SQM_UCI===\n===END==="
  104. sc := parseSqm(out)
  105. desired := SqmDesired{Enabled: true, Interface: "wan", Download: "50000", Upload: "5000"}
  106. ops := SqmOps(desired, sc)
  107. hasAdd := false
  108. hasNewRef := false
  109. for _, op := range ops {
  110. if op.Kind == "uci_add" {
  111. hasAdd = true
  112. }
  113. if op.Kind == "uci_set" && op.Args["section"] == "@queue[-1]" && op.Args["option"] == "interface" {
  114. hasNewRef = true
  115. }
  116. }
  117. if !hasAdd {
  118. t.Error("esperaba uci_add (no hay sección)")
  119. }
  120. if !hasNewRef {
  121. t.Error("esperaba uci_set en @queue[-1] (sección recién creada)")
  122. }
  123. // opkg → install (no apk_install).
  124. if ops[0].Kind != "install" {
  125. t.Errorf("ops[0].Kind: got %q want install (opkg)", ops[0].Kind)
  126. }
  127. if err := validateSqmOps(ops); err != nil {
  128. t.Fatalf("validateSqmOps: %v", err)
  129. }
  130. }
  131. // TestSqmDisable: usa la sección existente, enabled=0 + stop + disable.
  132. func TestSqmDisable(t *testing.T) {
  133. sc := parseSqm(sqmOut)
  134. desired := SqmDesired{Enabled: false, Interface: "eth1"}
  135. ops := SqmOps(desired, sc)
  136. if len(ops) != 4 {
  137. t.Fatalf("ops: got %d want 4 (uci_set+commit+stop+disable)", len(ops))
  138. }
  139. if ops[0].Kind != "uci_set" || ops[0].Args["option"] != "enabled" || ops[0].Args["value"] != "0" {
  140. t.Errorf("ops[0]: got %v, esperaba uci_set enabled=0", ops[0])
  141. }
  142. if ops[3].Kind != "service" || ops[3].Args["action"] != "disable" {
  143. t.Errorf("ops[3]: got %v, esperaba service sqm disable", ops[3])
  144. }
  145. if err := validateSqmOps(ops); err != nil {
  146. t.Fatalf("validateSqmOps: %v", err)
  147. }
  148. }
  149. // TestSqmDisableSinSeccion: sin sección → no-op (0 ops).
  150. func TestSqmDisableSinSeccion(t *testing.T) {
  151. out := "===PKG_MGR===\napk\n===INSTALLED===\nno\n===SQM_UCI===\n===END==="
  152. sc := parseSqm(out)
  153. desired := SqmDesired{Enabled: false}
  154. ops := SqmOps(desired, sc)
  155. if len(ops) != 0 {
  156. t.Errorf("ops: got %d want 0 (no hay sección que desactivar)", len(ops))
  157. }
  158. }