route_change_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package routing
  2. import (
  3. "encoding/json"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "strings"
  8. "testing"
  9. )
  10. func useTempRouteStores(t *testing.T) {
  11. t.Helper()
  12. oldStatic, oldPolicy, oldTables := StaticRoutesStorePath, PolicyRulesStorePath, CustomTablesStorePath
  13. dir := t.TempDir()
  14. StaticRoutesStorePath = filepath.Join(dir, "static.json")
  15. PolicyRulesStorePath = filepath.Join(dir, "rules.json")
  16. CustomTablesStorePath = filepath.Join(dir, "tables.json")
  17. t.Cleanup(func() {
  18. StaticRoutesStorePath, PolicyRulesStorePath, CustomTablesStorePath = oldStatic, oldPolicy, oldTables
  19. })
  20. }
  21. func TestRouteEnableFailureRestoresStoredAndMemoryState(t *testing.T) {
  22. useTempRouteStores(t)
  23. bin := t.TempDir()
  24. t.Setenv("PATH", bin)
  25. script := "#!/bin/sh\nif [ \"$2\" = del ]; then echo 'RTNETLINK answers: No such process' >&2; exit 2; fi\necho 'permission denied' >&2; exit 2\n"
  26. if err := os.WriteFile(filepath.Join(bin, "ip"), []byte(script), 0755); err != nil {
  27. t.Fatal(err)
  28. }
  29. old := RouteItem{ID: "r1", DstAddress: "198.51.100.0/24", Gateway: "192.0.2.1", Interface: "eth0", RoutingTable: "main", Distance: 10, Disabled: true, Active: false}
  30. m := &RoutingManager{staticRoutes: []RouteItem{old}}
  31. if err := m.save(); err != nil {
  32. t.Fatal(err)
  33. }
  34. err := m.SetRouteState("r1", false)
  35. if err == nil || !strings.Contains(err.Error(), "applying route change") {
  36. t.Fatalf("expected apply error: %v", err)
  37. }
  38. if !m.staticRoutes[0].Disabled || m.staticRoutes[0].Active {
  39. t.Fatalf("memory not restored: %+v", m.staticRoutes[0])
  40. }
  41. data, err := os.ReadFile(StaticRoutesStorePath)
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. var persisted []RouteItem
  46. if err = json.Unmarshal(data, &persisted); err != nil {
  47. t.Fatal(err)
  48. }
  49. if len(persisted) != 1 || !persisted[0].Disabled {
  50. t.Fatalf("storage not restored: %+v", persisted)
  51. }
  52. }
  53. func TestRouteDeletionNeverUsesUnqualifiedFallback(t *testing.T) {
  54. useTempRouteStores(t)
  55. bin := t.TempDir()
  56. t.Setenv("PATH", bin)
  57. logPath := filepath.Join(t.TempDir(), "commands")
  58. t.Setenv("DEBTIK_TEST_IP_LOG", logPath)
  59. script := "#!/bin/sh\nprintf '%s\\n' \"$*\" >> \"$DEBTIK_TEST_IP_LOG\"\necho 'RTNETLINK answers: No such process' >&2\nexit 2\n"
  60. if err := os.WriteFile(filepath.Join(bin, "ip"), []byte(script), 0755); err != nil {
  61. t.Fatal(err)
  62. }
  63. r := RouteItem{ID: "r1", DstAddress: "198.51.100.0/24", Gateway: "192.0.2.1", Interface: "eth0", RoutingTable: "100", Distance: 10, Active: true}
  64. m := &RoutingManager{staticRoutes: []RouteItem{r}}
  65. if err := m.save(); err != nil {
  66. t.Fatal(err)
  67. }
  68. if err := m.DeleteRoute("r1"); err != nil {
  69. t.Fatal(err)
  70. }
  71. data, err := os.ReadFile(logPath)
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. lines := strings.Split(strings.TrimSpace(string(data)), "\n")
  76. if len(lines) != 2 {
  77. t.Fatalf("unexpected commands: %s", data)
  78. }
  79. for _, line := range lines {
  80. if !strings.Contains(line, "metric 10 table 100 proto static") {
  81. t.Fatalf("unsafe deletion: %s", line)
  82. }
  83. }
  84. }
  85. func TestIsolatedRouteGroupDeletionKeepsOtherMetric(t *testing.T) {
  86. if os.Getenv("DEBTIK_TEST_ISOLATED_NETNS") != "1" {
  87. t.Skip("requires a dedicated network namespace")
  88. }
  89. ip := func(args ...string) {
  90. t.Helper()
  91. if out, err := exec.Command("ip", args...).CombinedOutput(); err != nil {
  92. t.Fatalf("ip %v: %s: %v", args, out, err)
  93. }
  94. }
  95. ip("link", "set", "lo", "up")
  96. ip("route", "add", "198.51.100.0/24", "dev", "lo", "metric", "10", "proto", "static")
  97. ip("route", "add", "198.51.100.0/24", "dev", "lo", "metric", "20", "proto", "static")
  98. m := &RoutingManager{}
  99. if err := m.syncRouteGroupToKernel("198.51.100.0/24", "main", 10); err != nil {
  100. t.Fatal(err)
  101. }
  102. out, err := exec.Command("ip", "route", "show", "198.51.100.0/24").CombinedOutput()
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. if strings.Contains(string(out), "metric 10") || !strings.Contains(string(out), "metric 20") {
  107. t.Fatalf("unexpected routes: %s", out)
  108. }
  109. }