|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/tronprotocol/tron-deployment/internal/paths" |
| 11 | +) |
| 12 | + |
| 13 | +// A recipe run is the most capable single command trond has — since |
| 14 | +// `kind: host`, one step can be an arbitrary program — and it was the |
| 15 | +// only one that could act and leave nothing in the audit log. Command |
| 16 | +// steps re-exec trond, so the child writes its own entry; host steps |
| 17 | +// never re-enter trond, and the run itself was never recorded at all. |
| 18 | + |
| 19 | +func writeTempRecipe(t *testing.T, body string) string { |
| 20 | + t.Helper() |
| 21 | + p := filepath.Join(t.TempDir(), "r.yaml") |
| 22 | + if err := os.WriteFile(p, []byte(body), 0o600); err != nil { |
| 23 | + t.Fatalf("write recipe: %v", err) |
| 24 | + } |
| 25 | + return p |
| 26 | +} |
| 27 | + |
| 28 | +// auditLines returns (command, detail, result) for each audit entry. |
| 29 | +func auditLines(t *testing.T) [][3]string { |
| 30 | + t.Helper() |
| 31 | + raw, err := os.ReadFile(paths.AuditLog()) |
| 32 | + if err != nil { |
| 33 | + if os.IsNotExist(err) { |
| 34 | + return nil |
| 35 | + } |
| 36 | + t.Fatalf("read audit log: %v", err) |
| 37 | + } |
| 38 | + var out [][3]string |
| 39 | + for _, line := range strings.Split(strings.TrimSpace(string(raw)), "\n") { |
| 40 | + if line == "" { |
| 41 | + continue |
| 42 | + } |
| 43 | + var e struct{ Command, Detail, Result string } |
| 44 | + if err := json.Unmarshal([]byte(line), &e); err != nil { |
| 45 | + t.Fatalf("audit line %q: %v", line, err) |
| 46 | + } |
| 47 | + out = append(out, [3]string{e.Command, e.Detail, e.Result}) |
| 48 | + } |
| 49 | + return out |
| 50 | +} |
| 51 | + |
| 52 | +func setupRecipeAudit(t *testing.T) { |
| 53 | + t.Helper() |
| 54 | + paths.SetBaseDir(t.TempDir()) |
| 55 | + t.Cleanup(func() { paths.SetBaseDir("") }) |
| 56 | + t.Cleanup(func() { |
| 57 | + recipeFile, recipeAllowHostExec, recipeRunDryRun = "", false, false |
| 58 | + recipeRunParams = nil |
| 59 | + }) |
| 60 | +} |
| 61 | + |
| 62 | +func TestRecipeRun_AuditsRunAndHostSteps(t *testing.T) { |
| 63 | + setupRecipeAudit(t) |
| 64 | + marker := filepath.Join(t.TempDir(), "ran") |
| 65 | + recipeFile = writeTempRecipe(t, `name: audited |
| 66 | +steps: |
| 67 | + - id: stage |
| 68 | + kind: host |
| 69 | + run: ["touch", "`+marker+`"] |
| 70 | +`) |
| 71 | + recipeAllowHostExec = true |
| 72 | + |
| 73 | + if err := runRecipeRun(newCmd(), nil); err != nil { |
| 74 | + t.Fatalf("runRecipeRun: %v", err) |
| 75 | + } |
| 76 | + |
| 77 | + lines := auditLines(t) |
| 78 | + var host, run [3]string |
| 79 | + for _, l := range lines { |
| 80 | + switch l[0] { |
| 81 | + case "recipe host-step": |
| 82 | + host = l |
| 83 | + case "recipe run": |
| 84 | + run = l |
| 85 | + } |
| 86 | + } |
| 87 | + if host[0] == "" { |
| 88 | + t.Fatalf("no `recipe host-step` entry; a host step ran and the log never saw it.\ngot: %v", lines) |
| 89 | + } |
| 90 | + if !strings.Contains(host[1], "stage") || !strings.Contains(host[1], "touch") { |
| 91 | + t.Errorf("host-step detail = %q, want it to name the step and its program", host[1]) |
| 92 | + } |
| 93 | + if run[0] == "" { |
| 94 | + t.Fatalf("no `recipe run` entry.\ngot: %v", lines) |
| 95 | + } |
| 96 | + if run[1] != recipeFile { |
| 97 | + t.Errorf("run detail = %q, want the recipe source %q", run[1], recipeFile) |
| 98 | + } |
| 99 | + if run[2] != "success" { |
| 100 | + t.Errorf("run result = %q, want success", run[2]) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// TestRecipeRun_RefusedHostStepLeavesNoStepEntry: the audit log records |
| 105 | +// what ran. A step refused by --allow-host-exec did not run, so claiming |
| 106 | +// it did would be worse than silence — but the run itself is still |
| 107 | +// recorded, as a failure. |
| 108 | +func TestRecipeRun_RefusedHostStepLeavesNoStepEntry(t *testing.T) { |
| 109 | + setupRecipeAudit(t) |
| 110 | + marker := filepath.Join(t.TempDir(), "ran") |
| 111 | + recipeFile = writeTempRecipe(t, `name: refused |
| 112 | +steps: |
| 113 | + - id: stage |
| 114 | + kind: host |
| 115 | + run: ["touch", "`+marker+`"] |
| 116 | +`) |
| 117 | + // recipeAllowHostExec stays false. |
| 118 | + |
| 119 | + if err := runRecipeRun(newCmd(), nil); err == nil { |
| 120 | + t.Fatal("want the refusal to surface as an error") |
| 121 | + } |
| 122 | + if _, err := os.Stat(marker); err == nil { |
| 123 | + t.Fatal("the refused step ran") |
| 124 | + } |
| 125 | + |
| 126 | + for _, l := range auditLines(t) { |
| 127 | + if l[0] == "recipe host-step" { |
| 128 | + t.Errorf("audited a host step that never ran: %v", l) |
| 129 | + } |
| 130 | + } |
| 131 | + var found bool |
| 132 | + for _, l := range auditLines(t) { |
| 133 | + if l[0] == "recipe run" { |
| 134 | + found = true |
| 135 | + if l[2] != "error" { |
| 136 | + t.Errorf("run result = %q, want error", l[2]) |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + if !found { |
| 141 | + t.Error("a failed run left no `recipe run` entry") |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +// TestRecipeRun_DryRunAuditsNoHostStep — a preview executed nothing. |
| 146 | +func TestRecipeRun_DryRunAuditsNoHostStep(t *testing.T) { |
| 147 | + setupRecipeAudit(t) |
| 148 | + recipeFile = writeTempRecipe(t, `name: preview |
| 149 | +steps: |
| 150 | + - id: stage |
| 151 | + kind: host |
| 152 | + run: ["touch", "/tmp/should-not-happen"] |
| 153 | +`) |
| 154 | + recipeAllowHostExec = true |
| 155 | + recipeRunDryRun = true |
| 156 | + |
| 157 | + if err := runRecipeRun(newCmd(), nil); err != nil { |
| 158 | + t.Fatalf("dry-run: %v", err) |
| 159 | + } |
| 160 | + for _, l := range auditLines(t) { |
| 161 | + if l[0] == "recipe host-step" { |
| 162 | + t.Errorf("dry-run audited a host step: %v", l) |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments