| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "strconv" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "amadan.net/oficina/equipo/internal/equipo" |
| 17 | "amadan.net/oficina/equipo/internal/runner" |
| 18 | runnersetup "amadan.net/oficina/equipo/internal/runner/setup" |
| 19 | "amadan.net/rastrillo/rastrillo" |
| 20 | ) |
| 21 | |
| 22 | type linuxRig struct { |
| 23 | h linuxInstallHost |
| 24 | home, root string |
| 25 | installs int |
| 26 | enrolled bool |
| 27 | cwd string |
| 28 | } |
| 29 | |
| 30 | func newLinuxRig(t *testing.T) *linuxRig { |
| 31 | t.Helper() |
| 32 | old := rastrillo.BuildVersion |
| 33 | rastrillo.BuildVersion = "abc1234" |
| 34 | t.Cleanup(func() { rastrillo.BuildVersion = old }) |
| 35 | x := &linuxRig{home: t.TempDir()} |
| 36 | x.root = filepath.Join(x.home, ".local/share/equipo-runner") |
| 37 | x.h = linuxInstallHost{ |
| 38 | home: func() (string, error) { return x.home, nil }, |
| 39 | chdir: func(dir string) error { x.cwd = dir; return nil }, |
| 40 | installable: func() bool { return true }, |
| 41 | install: func(_ context.Context, o runnersetup.InstallOptions) (runnersetup.InstallResult, error) { |
| 42 | if x.cwd != x.home { |
| 43 | t.Errorf("installed from %q", x.cwd) |
| 44 | } |
| 45 | x.installs++ |
| 46 | if err := os.MkdirAll(o.Root, 0700); err != nil { |
| 47 | return runnersetup.InstallResult{}, err |
| 48 | } |
| 49 | if err := os.WriteFile(filepath.Join(o.Root, "equipo-runner.service"), []byte("unit for "+o.InstanceOrigin), 0600); err != nil { |
| 50 | return runnersetup.InstallResult{}, err |
| 51 | } |
| 52 | return runnersetup.InstallResult{BinaryPath: filepath.Join(o.Root, "releases/abc1234/equipo-runner"), State: "installed"}, nil |
| 53 | }, |
| 54 | config: func(root, origin, instance, _ string, _ func(string) string) (runner.Config, error) { |
| 55 | if err := os.WriteFile(filepath.Join(root, "runner.env"), []byte("EQUIPO_ORIGIN="+strconv.Quote(origin)+"\n"), 0600); err != nil { |
| 56 | return runner.Config{}, err |
| 57 | } |
| 58 | return runner.Config{Origin: origin, InstanceID: instance, StateDir: filepath.Join(root, "state")}, nil |
| 59 | }, |
| 60 | verify: func(runner.Config) error { return nil }, |
| 61 | enrol: func(context.Context, runner.Config, io.Writer) error { x.enrolled = true; return nil }, |
| 62 | checkEnv: func(string, string, string) error { return nil }, |
| 63 | // The installer's own files, except one a person edited. |
| 64 | owned: func(path string) bool { |
| 65 | raw, _ := os.ReadFile(path) |
| 66 | return !strings.Contains(string(raw), "edited by hand") |
| 67 | }, |
| 68 | } |
| 69 | return x |
| 70 | } |
| 71 | |
| 72 | func (x *linuxRig) installWith(ctx context.Context, origin string, input io.Reader) (int, string) { |
| 73 | var out, errout bytes.Buffer |
| 74 | code := installLinux(ctx, []string{"--release", "abc1234", "--origin", origin, "--instance", "team1"}, input, &out, &errout, func(string) string { return "" }, x.h) |
| 75 | return code, errout.String() |
| 76 | } |
| 77 | |
| 78 | // A first install (no root yet) that stops before the enrolment leaves |
| 79 | // no unit or environment file behind: nothing would start a runner that |
| 80 | // was never added (Astra, mac-install-safe). |
| 81 | func TestInstallLinuxFirstFailureLeavesNoConfig(t *testing.T) { |
| 82 | x := newLinuxRig(t) |
| 83 | x.h.enrol = func(context.Context, runner.Config, io.Writer) error { return runner.ErrEnrolmentExpired } |
| 84 | if code, errout := x.install("https://equipo.example", "yes"); code != 1 { |
| 85 | t.Fatalf("%d %q", code, errout) |
| 86 | } |
| 87 | if got := x.files(t); len(got) != 0 { |
| 88 | t.Fatalf("left behind %v", got) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // A unit or environment file someone edited by hand is not moved aside |
| 93 | // or replaced: the install stops before changing anything. |
| 94 | func TestInstallLinuxKeepsAnEditedFile(t *testing.T) { |
| 95 | for _, name := range []string{"runner.env", "equipo-runner.service"} { |
| 96 | t.Run(name, func(t *testing.T) { |
| 97 | x := newLinuxRig(t) |
| 98 | x.before(t, "https://equipo.example") |
| 99 | if err := os.WriteFile(filepath.Join(x.root, name), []byte("edited by hand"), 0600); err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | before := x.files(t) |
| 103 | code, errout := x.install("https://equipo-next.example", "yes") |
| 104 | if code != 1 || !strings.Contains(errout, equipo.RunnerSetupCopy("runner.install.destination")) { |
| 105 | t.Fatalf("%d %q", code, errout) |
| 106 | } |
| 107 | if x.installs != 0 || x.enrolled || !mapsEqual(before, x.files(t)) { |
| 108 | t.Fatalf("changed something: %d %v %v", x.installs, x.enrolled, x.files(t)) |
| 109 | } |
| 110 | }) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // Ctrl-C at a prompt (the input stays open, no line comes) ends the |
| 115 | // install and puts the earlier configuration back at once. |
| 116 | func TestInstallLinuxCtrlCAtAPrompt(t *testing.T) { |
| 117 | for _, answered := range []string{"", "\n"} { |
| 118 | x := newLinuxRig(t) |
| 119 | before := x.before(t, "https://equipo.example") |
| 120 | r, w := io.Pipe() |
| 121 | defer w.Close() |
| 122 | installed := make(chan struct{}) |
| 123 | install := x.h.install |
| 124 | x.h.install = func(ctx context.Context, o runnersetup.InstallOptions) (runnersetup.InstallResult, error) { |
| 125 | defer close(installed) |
| 126 | return install(ctx, o) |
| 127 | } |
| 128 | ctx, cancel := context.WithCancel(context.Background()) |
| 129 | done := make(chan int) |
| 130 | go func() { code, _ := x.installWith(ctx, "https://equipo-next.example", r); done <- code }() |
| 131 | <-installed |
| 132 | if answered != "" { |
| 133 | // The label is answered; Ctrl-C comes at the yes prompt. |
| 134 | _, _ = io.WriteString(w, answered) |
| 135 | } |
| 136 | time.Sleep(50 * time.Millisecond) |
| 137 | cancel() |
| 138 | select { |
| 139 | case code := <-done: |
| 140 | if code != 1 { |
| 141 | t.Fatalf("exit %d", code) |
| 142 | } |
| 143 | case <-time.After(5 * time.Second): |
| 144 | t.Fatal("Ctrl-C at the prompt did not end the install") |
| 145 | } |
| 146 | if got := x.files(t); !mapsEqual(before, got) || x.enrolled { |
| 147 | t.Fatalf("not put back: %v", got) |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | // A second install of the same runner while one is running is refused |
| 153 | // and changes nothing: it cannot put its backup back over the first. |
| 154 | func TestInstallLinuxOneAtATime(t *testing.T) { |
| 155 | x := newLinuxRig(t) |
| 156 | before := x.before(t, "https://equipo.example") |
| 157 | lock, err := installLock(filepath.Join(x.root, "state")) |
| 158 | if err != nil { |
| 159 | t.Fatal(err) |
| 160 | } |
| 161 | defer lock.Close() |
| 162 | code, errout := x.install("https://equipo-next.example", "yes") |
| 163 | if code != 1 || !strings.Contains(errout, equipo.RunnerSetupCopy("runner.install.busy")) { |
| 164 | t.Fatalf("%d %q", code, errout) |
| 165 | } |
| 166 | if x.installs != 0 || x.enrolled || !mapsEqual(before, x.files(t)) { |
| 167 | t.Fatalf("changed something: %d %v %v", x.installs, x.enrolled, x.files(t)) |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | func (x *linuxRig) install(origin, answer string) (int, string) { |
| 172 | var out, errout bytes.Buffer |
| 173 | code := installLinux(context.Background(), []string{"--release", "abc1234", "--origin", origin, "--instance", "team1"}, strings.NewReader("\n"+answer+"\n"), &out, &errout, func(string) string { return "" }, x.h) |
| 174 | return code, errout.String() |
| 175 | } |
| 176 | |
| 177 | // before is a runner installed for origin: its unit and environment file. |
| 178 | func (x *linuxRig) before(t *testing.T, origin string) map[string]string { |
| 179 | t.Helper() |
| 180 | files := map[string]string{"runner.env": "EQUIPO_ORIGIN=" + strconv.Quote(origin) + "\n", "equipo-runner.service": "unit for " + origin} |
| 181 | for name, body := range files { |
| 182 | if err := os.MkdirAll(x.root, 0700); err != nil { |
| 183 | t.Fatal(err) |
| 184 | } |
| 185 | if err := os.WriteFile(filepath.Join(x.root, name), []byte(body), 0600); err != nil { |
| 186 | t.Fatal(err) |
| 187 | } |
| 188 | } |
| 189 | return files |
| 190 | } |
| 191 | |
| 192 | func (x *linuxRig) files(t *testing.T) map[string]string { |
| 193 | t.Helper() |
| 194 | got := map[string]string{} |
| 195 | entries, err := os.ReadDir(x.root) |
| 196 | if err != nil { |
| 197 | t.Fatal(err) |
| 198 | } |
| 199 | for _, e := range entries { |
| 200 | // The install log is the installer's own record. |
| 201 | if e.IsDir() || e.Name() == "install.log" { |
| 202 | continue |
| 203 | } |
| 204 | raw, _ := os.ReadFile(filepath.Join(x.root, e.Name())) |
| 205 | got[e.Name()] = string(raw) |
| 206 | } |
| 207 | return got |
| 208 | } |
| 209 | |
| 210 | // The Linux installer asks first too: a runner enrolled with another |
| 211 | // Equipo is told so and nothing is installed, written or enrolled. |
| 212 | func TestInstallLinuxRefusesAnotherEquipo(t *testing.T) { |
| 213 | for name, c := range map[string]struct { |
| 214 | unreadable bool |
| 215 | want string |
| 216 | }{ |
| 217 | "another origin": {want: fmt.Sprintf(equipo.RunnerSetupCopy("runner.install.other_instance"), stagingOrigin)}, |
| 218 | "credential unreadable": {unreadable: true, want: strings.TrimSuffix(equipo.RunnerSetupCopy("runner.enrol.failed"), "%s")}, |
| 219 | } { |
| 220 | t.Run(name, func(t *testing.T) { |
| 221 | x := newLinuxRig(t) |
| 222 | before := x.before(t, stagingOrigin) |
| 223 | state := filepath.Join(x.root, "state") |
| 224 | liveEnrolment(t, state, stagingOrigin, "team1") |
| 225 | if c.unreadable { |
| 226 | if err := os.WriteFile(filepath.Join(state, "credentials.json"), []byte("{"), 0600); err != nil { |
| 227 | t.Fatal(err) |
| 228 | } |
| 229 | } |
| 230 | code, errout := x.install("https://equipo.example", "yes") |
| 231 | if code != 1 || !strings.Contains(errout, c.want) { |
| 232 | t.Fatalf("%d %q", code, errout) |
| 233 | } |
| 234 | if x.installs != 0 || x.enrolled || !mapsEqual(before, x.files(t)) { |
| 235 | t.Fatalf("changed something: %d %v %v", x.installs, x.enrolled, x.files(t)) |
| 236 | } |
| 237 | }) |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // What the installer rewrote before enrolling (the unit and the |
| 242 | // environment file) is put back when it stops before the enrolment is |
| 243 | // done; an install that enrols keeps the new ones and nothing aside. |
| 244 | func TestInstallLinuxPutsBackItsConfig(t *testing.T) { |
| 245 | for name, c := range map[string]struct { |
| 246 | set func(*linuxRig) |
| 247 | answer string |
| 248 | }{ |
| 249 | "link expired": {set: func(x *linuxRig) { |
| 250 | x.h.enrol = func(context.Context, runner.Config, io.Writer) error { return runner.ErrEnrolmentExpired } |
| 251 | }, answer: "yes"}, |
| 252 | "not confirmed": {set: func(*linuxRig) {}, answer: "no"}, |
| 253 | "not the release": {set: func(x *linuxRig) { x.h.verify = func(runner.Config) error { return errScanner } }, answer: "yes"}, |
| 254 | } { |
| 255 | t.Run(name, func(t *testing.T) { |
| 256 | x := newLinuxRig(t) |
| 257 | before := x.before(t, "https://equipo.example") |
| 258 | c.set(x) |
| 259 | if code, errout := x.install("https://equipo-next.example", c.answer); code != 1 { |
| 260 | t.Fatalf("%d %q", code, errout) |
| 261 | } |
| 262 | if got := x.files(t); !mapsEqual(before, got) { |
| 263 | t.Fatalf("not put back: %v", got) |
| 264 | } |
| 265 | if got := len(x.files(t)); got != 2 { |
| 266 | t.Fatalf("left behind %v", x.files(t)) |
| 267 | } |
| 268 | // The unit, the environment file, the install log and the |
| 269 | // state directory with its install lock: no backup left. |
| 270 | if entries, _ := os.ReadDir(x.root); len(entries) > 4 { |
| 271 | t.Fatalf("left behind %v", entries) |
| 272 | } |
| 273 | }) |
| 274 | } |
| 275 | x := newLinuxRig(t) |
| 276 | x.before(t, "https://equipo.example") |
| 277 | if code, errout := x.install("https://equipo-next.example", "yes"); code != 0 || !x.enrolled { |
| 278 | t.Fatalf("%d %q", code, errout) |
| 279 | } |
| 280 | if got := x.files(t); got["runner.env"] != "EQUIPO_ORIGIN=\"https://equipo-next.example\"\n" || got["equipo-runner.service"] != "unit for https://equipo-next.example" { |
| 281 | t.Fatalf("new config %v", got) |
| 282 | } |
| 283 | // The unit, the environment file and the state directory. |
| 284 | if entries, _ := os.ReadDir(x.root); len(entries) != 3 { |
| 285 | t.Fatalf("left behind %v", entries) |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | // As on a Mac: an enrolment that published its credential, then |
| 290 | // answered an error, is enrolled and its configuration kept. |
| 291 | func TestInstallLinuxEnrolledDespiteAnError(t *testing.T) { |
| 292 | x := newLinuxRig(t) |
| 293 | x.before(t, stagingOrigin) |
| 294 | expiredEnrolment(t, filepath.Join(x.root, "state"), stagingOrigin) |
| 295 | x.h.enrol = func(_ context.Context, cfg runner.Config, _ io.Writer) error { |
| 296 | liveEnrolment(t, cfg.StateDir, cfg.Origin, cfg.InstanceID) |
| 297 | x.enrolled = true |
| 298 | return errors.New("sync state: input/output error") |
| 299 | } |
| 300 | code, errout := x.install("https://equipo.example", "yes") |
| 301 | if code != 0 || !x.enrolled { |
| 302 | t.Fatalf("%d %q", code, errout) |
| 303 | } |
| 304 | if got := x.files(t); got["runner.env"] != "EQUIPO_ORIGIN=\"https://equipo.example\"\n" { |
| 305 | t.Fatalf("configuration put back beside the new credential: %v", got) |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // As on a Mac: with the enrolment's outcome unreadable, the earlier |
| 310 | // configuration is not put back beside what may be a new credential. |
| 311 | func TestInstallLinuxEnrolmentUnknown(t *testing.T) { |
| 312 | x := newLinuxRig(t) |
| 313 | x.before(t, stagingOrigin) |
| 314 | expiredEnrolment(t, filepath.Join(x.root, "state"), stagingOrigin) |
| 315 | x.h.enrol = func(_ context.Context, cfg runner.Config, _ io.Writer) error { |
| 316 | path := filepath.Join(cfg.StateDir, "credentials.json") |
| 317 | if err := os.Remove(path); err != nil { |
| 318 | t.Fatal(err) |
| 319 | } |
| 320 | if err := os.Mkdir(path, 0700); err != nil { |
| 321 | t.Fatal(err) |
| 322 | } |
| 323 | return errors.New("sync state: input/output error") |
| 324 | } |
| 325 | if code, errout := x.install("https://equipo.example", "yes"); code != 1 { |
| 326 | t.Fatalf("%d %q", code, errout) |
| 327 | } |
| 328 | if got := x.files(t); got["runner.env"] != "EQUIPO_ORIGIN=\"https://equipo.example\"\n" { |
| 329 | t.Fatalf("put back with the enrolment unknown: %v", got) |
| 330 | } |
| 331 | } |
| 332 | |