Download this file
| 1 | //go:build linux |
| 2 | |
| 3 | package hostfloor |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | // A guarded run returns the image's stdout only. A candidate that writes |
| 14 | // progress or an alert to stderr must not corrupt the version or JSON the |
| 15 | // updater parses from stdout (an armed box refused every router update that |
| 16 | // way); stderr still explains a failure. |
| 17 | func TestRunImageReturnsStdoutOnly(t *testing.T) { |
| 18 | dir := t.TempDir() |
| 19 | script := filepath.Join(dir, "img") |
| 20 | if err := os.WriteFile(script, []byte("#!/bin/sh\necho noise >&2\necho '{\"ok\":true}'\n[ \"$1\" = fail ] && { echo why >&2; exit 3; }\nexit 0\n"), 0o755); err != nil { |
| 21 | t.Fatal(err) |
| 22 | } |
| 23 | f, err := os.Open(script) |
| 24 | if err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | defer f.Close() |
| 28 | out, err := runImage(context.Background(), f, "ok") |
| 29 | if err != nil || out != "{\"ok\":true}\n" { |
| 30 | t.Fatalf("out %q err %v", out, err) |
| 31 | } |
| 32 | if _, err = f.Seek(0, 0); err != nil { |
| 33 | t.Fatal(err) |
| 34 | } |
| 35 | _, err = runImage(context.Background(), f, "fail") |
| 36 | if err == nil || !strings.Contains(err.Error(), "why") { |
| 37 | t.Fatalf("failure lost its stderr: %v", err) |
| 38 | } |
| 39 | } |
| 40 | |