| 1 | package cage |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "path/filepath" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | func TestDebianMirrorFailureFallbackAndPreference(t *testing.T) { |
| 12 | old := "https://mirrors.nju.edu.cn/debian\tpriority:1\nhttps://mirrors.ustc.edu.cn/debian\tpriority:2\nhttps://deb.debian.org/debian\tpriority:3\n" |
| 13 | for _, mode := range []string{"reachable", "bookworm", "tls-failure", "official-failure", "nju-failure", "official", "custom", "old", "reboot", "invalid"} { |
| 14 | t.Run(mode, func(t *testing.T) { |
| 15 | dir := t.TempDir() |
| 16 | mirrors := filepath.Join(dir, "mirrors") |
| 17 | os.MkdirAll(mirrors, 0755) |
| 18 | list := filepath.Join(mirrors, "debian.list") |
| 19 | content := "https://deb.debian.org/debian\n" |
| 20 | if mode == "custom" { |
| 21 | content = "https://custom.invalid/debian\n" |
| 22 | } |
| 23 | if mode == "old" { |
| 24 | content = old |
| 25 | } |
| 26 | if mode == "reboot" { |
| 27 | content = "# Cage managed Debian mirrors\nhttps://deb.debian.org/debian\tpriority:1\n" |
| 28 | } |
| 29 | os.WriteFile(list, []byte(content), 0644) |
| 30 | os.WriteFile(filepath.Join(mirrors, "debian-security.list"), []byte("https://deb.debian.org/debian-security\n"), 0644) |
| 31 | preference := filepath.Join(dir, "preference") |
| 32 | if mode == "official" || mode == "invalid" { |
| 33 | os.WriteFile(preference, []byte(mode+"\n"), 0644) |
| 34 | } |
| 35 | for name, body := range map[string]string{ |
| 36 | "sudo": "#!/bin/sh\nexec \"$@\"\n", |
| 37 | "apt-helper": `#!/bin/sh |
| 38 | printf '%s\n' "$*" >> "$FIXTURE/probes" |
| 39 | [ "$MODE" != tls-failure ] || exit 1 |
| 40 | case "$*" in |
| 41 | *deb.debian.org*) [ "$MODE" != nju-failure ] && [ "$MODE" != official-failure ] || exit 1;; |
| 42 | *mirrors.nju*) [ "$MODE" != nju-failure ] || exit 1;; |
| 43 | esac |
| 44 | exit 0 |
| 45 | `, |
| 46 | "timeout": "#!/bin/sh\nshift\nexec \"$@\"\n", |
| 47 | } { |
| 48 | os.WriteFile(filepath.Join(dir, name), []byte(body), 0755) |
| 49 | } |
| 50 | script := strings.ReplaceAll(DebianMirrorsScript, "/etc/apt/mirrors", mirrors) |
| 51 | script = strings.ReplaceAll(script, "/etc/claude-cage/debian-mirror", preference) |
| 52 | script = strings.ReplaceAll(script, "/usr/lib/apt/apt-helper", filepath.Join(dir, "apt-helper")) |
| 53 | suite := "trixie" |
| 54 | if mode == "bookworm" { |
| 55 | suite = "bookworm" |
| 56 | } |
| 57 | osRelease := filepath.Join(dir, "os-release") |
| 58 | if err := os.WriteFile(osRelease, []byte("VERSION_CODENAME="+suite+"\n"), 0644); err != nil { |
| 59 | t.Fatal(err) |
| 60 | } |
| 61 | script = strings.ReplaceAll(script, "/etc/os-release", osRelease) |
| 62 | run := func() ([]byte, error) { |
| 63 | cmd := exec.Command("sh", "-c", script) |
| 64 | cmd.Env = append(os.Environ(), "PATH="+dir+":"+os.Getenv("PATH"), "FIXTURE="+dir, "MODE="+mode) |
| 65 | return cmd.CombinedOutput() |
| 66 | } |
| 67 | out, err := run() |
| 68 | if mode == "invalid" { |
| 69 | if err == nil { |
| 70 | t.Fatal("invalid preference accepted") |
| 71 | } |
| 72 | return |
| 73 | } |
| 74 | if err != nil { |
| 75 | t.Fatalf("%v %s", err, out) |
| 76 | } |
| 77 | data, _ := os.ReadFile(list) |
| 78 | got := string(data) |
| 79 | switch mode { |
| 80 | case "custom": |
| 81 | if got != content { |
| 82 | t.Fatal("custom source overwritten") |
| 83 | } |
| 84 | case "official", "tls-failure", "reachable", "bookworm", "old", "reboot": |
| 85 | if strings.Contains(got, "mirrors.") || !strings.Contains(got, "deb.debian.org") { |
| 86 | t.Fatal(got) |
| 87 | } |
| 88 | case "nju-failure": |
| 89 | if strings.Contains(got, "mirrors.nju") || !strings.Contains(got, "mirrors.ustc") { |
| 90 | t.Fatal(got) |
| 91 | } |
| 92 | default: |
| 93 | if !strings.Contains(got, "mirrors.nju") { |
| 94 | t.Fatal(got) |
| 95 | } |
| 96 | } |
| 97 | if mode == "official" { |
| 98 | if probes, _ := os.ReadFile(filepath.Join(dir, "probes")); len(probes) > 0 { |
| 99 | t.Fatal("official preference contacted mirrors") |
| 100 | } |
| 101 | } |
| 102 | if mode == "reachable" || mode == "bookworm" { |
| 103 | probes, err := os.ReadFile(filepath.Join(dir, "probes")) |
| 104 | if err != nil || !strings.Contains(string(probes), "/dists/"+suite+"/InRelease") || !strings.Contains(string(probes), "/dists/"+suite+"-security/InRelease") { |
| 105 | t.Fatalf("wrong distribution probes: %s, %v", probes, err) |
| 106 | } |
| 107 | if strings.Contains(string(probes), "mirrors.") { |
| 108 | t.Fatal("reachable official source still contacted domestic mirrors") |
| 109 | } |
| 110 | } |
| 111 | if out, err := run(); err != nil { |
| 112 | t.Fatalf("repeat: %v %s", err, out) |
| 113 | } |
| 114 | after, _ := os.ReadFile(list) |
| 115 | if string(after) != got { |
| 116 | t.Fatal("preference changed on repeated setup") |
| 117 | } |
| 118 | }) |
| 119 | } |
| 120 | } |