| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "os/exec" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "the-valley/change" |
| 15 | "the-valley/composed" |
| 16 | "the-valley/rebase" |
| 17 | ) |
| 18 | |
| 19 | // hookEnv names the rendered pre-receive hook restack-unit wires origin |
| 20 | // with (nix/checks/restack.nix). Without it, the tests that need the hook |
| 21 | // are skipped. |
| 22 | const hookEnv = "VALLEY_RESTACK_TEST_HOOK" |
| 23 | |
| 24 | var idA = strings.Repeat("k", 32) |
| 25 | |
| 26 | // fixture is a bare origin and a clone of it to work in. The clone's git |
| 27 | // commits as an author of its own, named in its environment, whatever the |
| 28 | // test's process inherits. |
| 29 | type fixture struct { |
| 30 | t *testing.T |
| 31 | origin, work string |
| 32 | env []string |
| 33 | } |
| 34 | |
| 35 | func newFixture(t *testing.T, hook string) *fixture { |
| 36 | t.Helper() |
| 37 | dir := t.TempDir() |
| 38 | f := &fixture{t: t, origin: filepath.Join(dir, "origin.git"), work: filepath.Join(dir, "work")} |
| 39 | f.env = append(os.Environ(), "HOME="+dir, "GIT_CONFIG_NOSYSTEM=1", "GIT_CONFIG_GLOBAL=/dev/null", |
| 40 | "GIT_AUTHOR_NAME=A. Author", "GIT_AUTHOR_EMAIL=author@example.invalid", |
| 41 | "GIT_COMMITTER_NAME=A. Author", "GIT_COMMITTER_EMAIL=author@example.invalid") |
| 42 | f.run(dir, "init", "--quiet", "--bare", "--initial-branch=main", f.origin) |
| 43 | if hook != "" { |
| 44 | data, err := os.ReadFile(hook) |
| 45 | if err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | if err := os.WriteFile(filepath.Join(f.origin, "hooks", "pre-receive"), data, 0o755); err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | } |
| 52 | f.run(dir, "clone", "--quiet", f.origin, f.work) |
| 53 | f.git("checkout", "--quiet", "-b", "main") |
| 54 | f.commit("base", "base.txt") |
| 55 | f.git("push", "--quiet", "origin", "main") |
| 56 | return f |
| 57 | } |
| 58 | |
| 59 | func (f *fixture) run(dir string, args ...string) string { |
| 60 | f.t.Helper() |
| 61 | cmd := exec.Command("git", args...) |
| 62 | cmd.Dir, cmd.Env = dir, f.env |
| 63 | out, err := cmd.CombinedOutput() |
| 64 | if err != nil { |
| 65 | f.t.Fatalf("git %s: %v\n%s", strings.Join(args, " "), err, out) |
| 66 | } |
| 67 | return strings.TrimSpace(string(out)) |
| 68 | } |
| 69 | |
| 70 | func (f *fixture) git(args ...string) string { f.t.Helper(); return f.run(f.work, args...) } |
| 71 | |
| 72 | func (f *fixture) commit(subject, file string) string { |
| 73 | f.t.Helper() |
| 74 | if err := os.WriteFile(filepath.Join(f.work, file), []byte(subject+"\n"), 0o644); err != nil { |
| 75 | f.t.Fatal(err) |
| 76 | } |
| 77 | f.git("add", file) |
| 78 | f.git("commit", "--quiet", "-m", subject) |
| 79 | return f.git("rev-parse", "HEAD") |
| 80 | } |
| 81 | |
| 82 | // as is the clone's git pushing as a principal. |
| 83 | func (f *fixture) as(principal string) rebase.Git { |
| 84 | return rebase.Git{Dir: f.work, Env: append(append([]string(nil), f.env...), "VALLEY_PRINCIPAL="+principal)} |
| 85 | } |
| 86 | |
| 87 | // readied pushes a branch off main with one commit, and readies it as a |
| 88 | // change owned by owner, under a title unlike the commit's subject. |
| 89 | // It returns the head. |
| 90 | func (f *fixture) readied(owner, branch, title string) string { |
| 91 | f.t.Helper() |
| 92 | f.git("checkout", "--quiet", "-b", branch, "main") |
| 93 | head := f.commit("layer "+branch, branch+".txt") |
| 94 | f.git("push", "--quiet", "origin", branch) |
| 95 | r := &change.Record{ |
| 96 | Schema: change.Schema, ChangeID: idA, Target: rebase.Target, Owner: owner, State: change.Ready, Seq: 1, Active: 1, |
| 97 | Versions: []change.Version{{N: 1, Head: change.HeadPrefix + head, Title: title, Branch: branch, Uploader: owner}}, |
| 98 | } |
| 99 | ctx := context.Background() |
| 100 | g := f.as(owner) |
| 101 | commit, err := rebase.WriteRecord(ctx, g, r, "test record", head) |
| 102 | if err != nil { |
| 103 | f.t.Fatal(err) |
| 104 | } |
| 105 | if w := rebase.PushRecord(ctx, g, idA, "", commit); w.Push != rebase.Pushed { |
| 106 | f.t.Fatalf("the record of %s was not pushed: %+v", idA, w) |
| 107 | } |
| 108 | return head |
| 109 | } |
| 110 | |
| 111 | // moveMain moves origin's main on by a commit of its own. |
| 112 | func (f *fixture) moveMain() { |
| 113 | f.t.Helper() |
| 114 | f.git("checkout", "--quiet", "main") |
| 115 | f.commit("main moves on", "main.txt") |
| 116 | f.git("push", "--quiet", "origin", "main") |
| 117 | } |
| 118 | |
| 119 | // at is a ref on origin, or "" for none. |
| 120 | func (f *fixture) at(ref string) string { |
| 121 | cmd := exec.Command("git", "rev-parse", "--verify", "--quiet", ref) |
| 122 | cmd.Dir, cmd.Env = f.origin, f.env |
| 123 | out, _ := cmd.Output() |
| 124 | return strings.TrimSpace(string(out)) |
| 125 | } |
| 126 | |
| 127 | // withoutIdentity leaves the test's process with no identity variable, |
| 128 | // as a service may run. |
| 129 | func withoutIdentity(t *testing.T) { |
| 130 | t.Helper() |
| 131 | for _, name := range []string{"GIT_AUTHOR_NAME", "GIT_AUTHOR_EMAIL", "GIT_AUTHOR_DATE", |
| 132 | "GIT_COMMITTER_NAME", "GIT_COMMITTER_EMAIL", "GIT_COMMITTER_DATE", "EMAIL"} { |
| 133 | t.Setenv(name, "") |
| 134 | os.Unsetenv(name) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // restacked reads origin into the restacker's scratch repository, rebases |
| 139 | // the change's family there, and pushes its branch. It returns the |
| 140 | // family and the prepared head of its bottom layer. |
| 141 | func restacked(t *testing.T, g rebase.Git) (*rebase.Family, string) { |
| 142 | t.Helper() |
| 143 | ctx := context.Background() |
| 144 | s, err := rebase.ReadSnapshot(ctx, g, rebase.Verifier{}) |
| 145 | if err != nil { |
| 146 | t.Fatal(err) |
| 147 | } |
| 148 | fam, err := rebase.Capture(s, rebase.Selection{ID: idA}) |
| 149 | if err != nil { |
| 150 | t.Fatal(err) |
| 151 | } |
| 152 | p, err := rebase.Prepare(ctx, g, fam) |
| 153 | if err != nil { |
| 154 | t.Fatalf("prepare: %v", err) |
| 155 | } |
| 156 | defer p.Release(ctx, g) |
| 157 | pub, err := rebase.Publish(ctx, g, p) |
| 158 | if err != nil || pub.Push != rebase.Pushed { |
| 159 | t.Fatalf("publish: %v %v %+v", err, pub.Push, pub.Refused) |
| 160 | } |
| 161 | return fam, p.Heads[fam.Bottom()] |
| 162 | } |
| 163 | |
| 164 | func logged(t *testing.T, g rebase.Git, format, commit string) string { |
| 165 | t.Helper() |
| 166 | out, err := gitOut(context.Background(), g, "log", "-1", "--format="+format, commit) |
| 167 | if err != nil { |
| 168 | t.Fatal(err) |
| 169 | } |
| 170 | return strings.TrimSpace(out) |
| 171 | } |
| 172 | |
| 173 | // With no identity variable in its environment, the restacker commits |
| 174 | // under the identity its flags give: it is the committer of each commit |
| 175 | // it rebases, whose author stays the change's, and the author and the |
| 176 | // committer of each record commit. An identity in the environment is not |
| 177 | // inherited. |
| 178 | func TestScratchCommitsAsTheController(t *testing.T) { |
| 179 | withoutIdentity(t) |
| 180 | f := newFixture(t, "") |
| 181 | f.readied("author", "a", "A title") |
| 182 | f.moveMain() |
| 183 | c := config{repo: f.origin, state: t.TempDir(), principal: "restacker", |
| 184 | commitName: "The Restacker", commitEmail: "restacker@example.invalid"} |
| 185 | ctx := context.Background() |
| 186 | g, err := scratchRepo(ctx, c) |
| 187 | if err != nil { |
| 188 | t.Fatal(err) |
| 189 | } |
| 190 | |
| 191 | fam, head := restacked(t, g) |
| 192 | const restacker = "The Restacker <restacker@example.invalid>" |
| 193 | if got := logged(t, g, "%an <%ae>|%cn <%ce>", head); got != "A. Author <author@example.invalid>|"+restacker { |
| 194 | t.Errorf("the rebased commit is authored and committed as %q", got) |
| 195 | } |
| 196 | p, err := rebase.BuildRestack(ctx, g, rebase.RestackAppend{ID: idA, Head: head, IfActive: fam.Bottom().Head, Principal: "restacker"}) |
| 197 | if err != nil { |
| 198 | t.Fatal(err) |
| 199 | } |
| 200 | if got := logged(t, g, "%an <%ae>|%cn <%ce>", p.Commit); got != restacker+"|"+restacker { |
| 201 | t.Errorf("the record commit is authored and committed as %q", got) |
| 202 | } |
| 203 | |
| 204 | t.Setenv("GIT_AUTHOR_NAME", "Inherited") |
| 205 | t.Setenv("GIT_COMMITTER_EMAIL", "inherited@example.invalid") |
| 206 | t.Setenv("EMAIL", "inherited@example.invalid") |
| 207 | g, err = scratchRepo(ctx, c) |
| 208 | if err != nil { |
| 209 | t.Fatal(err) |
| 210 | } |
| 211 | tree, err := gitOut(ctx, g, "mktree") |
| 212 | if err != nil { |
| 213 | t.Fatal(err) |
| 214 | } |
| 215 | commit, err := gitOut(ctx, g, "commit-tree", strings.TrimSpace(tree), "-m", "inherited") |
| 216 | if err != nil { |
| 217 | t.Fatal(err) |
| 218 | } |
| 219 | if got := logged(t, g, "%an <%ae>|%cn <%ce>", strings.TrimSpace(commit)); got != restacker+"|"+restacker { |
| 220 | t.Errorf("with an identity in the environment, a commit is authored and committed as %q", got) |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // Configuration the environment injects reaches no git the restacker |
| 225 | // runs. An author and a committer given in GIT_CONFIG_COUNT entries and in |
| 226 | // GIT_CONFIG_PARAMETERS would beat the scratch repository's user.name and |
| 227 | // user.email. They are not inherited: the restacker is the committer of |
| 228 | // each commit it rebases, whose author stays the change's, and the author |
| 229 | // and the committer of each record commit. |
| 230 | func TestInheritedConfigurationDoesNotCommit(t *testing.T) { |
| 231 | withoutIdentity(t) |
| 232 | f := newFixture(t, "") |
| 233 | f.readied("author", "a", "A title") |
| 234 | f.moveMain() |
| 235 | for name, value := range map[string]string{ |
| 236 | "GIT_CONFIG_COUNT": "2", |
| 237 | "GIT_CONFIG_KEY_0": "author.name", "GIT_CONFIG_VALUE_0": "Inherited", |
| 238 | "GIT_CONFIG_KEY_1": "author.email", "GIT_CONFIG_VALUE_1": "inherited@example.invalid", |
| 239 | "GIT_CONFIG_PARAMETERS": "'committer.name'='Inherited' 'committer.email'='inherited@example.invalid'", |
| 240 | } { |
| 241 | t.Setenv(name, value) |
| 242 | } |
| 243 | c := config{repo: f.origin, state: t.TempDir(), principal: "restacker", |
| 244 | commitName: "The Restacker", commitEmail: "restacker@example.invalid"} |
| 245 | ctx := context.Background() |
| 246 | g, err := scratchRepo(ctx, c) |
| 247 | if err != nil { |
| 248 | t.Fatal(err) |
| 249 | } |
| 250 | |
| 251 | fam, head := restacked(t, g) |
| 252 | const restacker = "The Restacker <restacker@example.invalid>" |
| 253 | if got := logged(t, g, "%an <%ae>|%cn <%ce>", head); got != "A. Author <author@example.invalid>|"+restacker { |
| 254 | t.Errorf("the rebased commit is authored and committed as %q", got) |
| 255 | } |
| 256 | p, err := rebase.BuildRestack(ctx, g, rebase.RestackAppend{ID: idA, Head: head, IfActive: fam.Bottom().Head, Principal: "restacker"}) |
| 257 | if err != nil { |
| 258 | t.Fatal(err) |
| 259 | } |
| 260 | if got := logged(t, g, "%an <%ae>|%cn <%ce>", p.Commit); got != restacker+"|"+restacker { |
| 261 | t.Errorf("the record commit is authored and committed as %q", got) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // A pass locks its state directory. A second lock fails at once, saying |
| 266 | // another pass holds it, and succeeds once the first is let go. |
| 267 | func TestAPassLocksItsState(t *testing.T) { |
| 268 | dir := filepath.Join(t.TempDir(), "state") |
| 269 | release, err := lockState(dir) |
| 270 | if err != nil { |
| 271 | t.Fatal(err) |
| 272 | } |
| 273 | if _, err := lockState(dir); err == nil || !strings.Contains(err.Error(), "another pass is using the state directory") { |
| 274 | t.Fatalf("a second lock while one is held: %v", err) |
| 275 | } |
| 276 | release() |
| 277 | again, err := lockState(dir) |
| 278 | if err != nil { |
| 279 | t.Fatalf("a lock once the first is let go: %v", err) |
| 280 | } |
| 281 | again() |
| 282 | } |
| 283 | |
| 284 | // A state directory belongs to the repository, target and principal the |
| 285 | // first pass used it for, and a pass for any other is refused. |
| 286 | func TestStateBelongsToOneRepositoryTargetAndPrincipal(t *testing.T) { |
| 287 | dir := t.TempDir() |
| 288 | first := binding{Repository: "ssh://valley/project.git", Target: "main", Principal: "restacker"} |
| 289 | if err := bindState(dir, first); err != nil { |
| 290 | t.Fatal(err) |
| 291 | } |
| 292 | if err := bindState(dir, first); err != nil { |
| 293 | t.Fatalf("the same binding again: %v", err) |
| 294 | } |
| 295 | for _, other := range []binding{ |
| 296 | {Repository: "ssh://valley/other.git", Target: "main", Principal: "restacker"}, |
| 297 | {Repository: "ssh://valley/project.git", Target: "main", Principal: "requester"}, |
| 298 | {Repository: "ssh://valley/project.git", Target: "next", Principal: "restacker"}, |
| 299 | } { |
| 300 | if err := bindState(dir, other); err == nil || !strings.Contains(err.Error(), "give each its own --state") { |
| 301 | t.Errorf("a pass for %+v: %v", other, err) |
| 302 | } |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | // blocker is a script that stands for a program that does not return. |
| 307 | // The first time it runs, it says it has started, then keeps its output |
| 308 | // open for a minute and beats while it does: it adds a line to a file |
| 309 | // every tenth of a second. |
| 310 | type blocker struct { |
| 311 | script, started, beats string |
| 312 | } |
| 313 | |
| 314 | func newBlocker(t *testing.T, script string) *blocker { |
| 315 | t.Helper() |
| 316 | dir := t.TempDir() |
| 317 | b := &blocker{script: script, started: filepath.Join(dir, "started"), beats: filepath.Join(dir, "beats")} |
| 318 | body := fmt.Sprintf("#!/bin/sh\n[ -e '%s' ] && exit 0\n: > '%s'\n"+ |
| 319 | "i=0\nwhile [ $i -lt 600 ]; do echo >> '%s'; sleep 0.1; i=$((i+1)); done\n", |
| 320 | b.started, b.started, b.beats) |
| 321 | if err := os.WriteFile(script, []byte(body), 0o755); err != nil { |
| 322 | t.Fatal(err) |
| 323 | } |
| 324 | return b |
| 325 | } |
| 326 | |
| 327 | func (b *blocker) wait(t *testing.T) { |
| 328 | t.Helper() |
| 329 | for deadline := time.Now().Add(30 * time.Second); time.Now().Before(deadline); time.Sleep(20 * time.Millisecond) { |
| 330 | if _, err := os.Stat(b.started); err == nil { |
| 331 | return |
| 332 | } |
| 333 | } |
| 334 | t.Fatal("the blocker never started") |
| 335 | } |
| 336 | |
| 337 | func (b *blocker) beating() bool { |
| 338 | size := func() int64 { |
| 339 | fi, err := os.Stat(b.beats) |
| 340 | if err != nil { |
| 341 | return 0 |
| 342 | } |
| 343 | return fi.Size() |
| 344 | } |
| 345 | time.Sleep(200 * time.Millisecond) |
| 346 | before := size() |
| 347 | time.Sleep(time.Second) |
| 348 | return size() != before |
| 349 | } |
| 350 | |
| 351 | // bounded runs read under a context with a two-second deadline, and holds |
| 352 | // that it fails, returns soon after the deadline, and leaves nothing of |
| 353 | // the blocker it ran into running. |
| 354 | func bounded(t *testing.T, b *blocker, read func(ctx context.Context) error) { |
| 355 | t.Helper() |
| 356 | ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) |
| 357 | defer cancel() |
| 358 | start := time.Now() |
| 359 | if err := read(ctx); err == nil { |
| 360 | t.Fatal("a read that never returned succeeded") |
| 361 | } |
| 362 | if took := time.Since(start); took > 15*time.Second { |
| 363 | t.Errorf("the read took %v, with a deadline of 2s", took) |
| 364 | } |
| 365 | b.wait(t) |
| 366 | if b.beating() { |
| 367 | t.Error("what the read started outlived the deadline") |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | // Composing the policy ends at the pass's deadline, with the cue that |
| 372 | // never returned killed. |
| 373 | func TestPolicyCompositionEndsAtTheDeadline(t *testing.T) { |
| 374 | f := newFixture(t, "") |
| 375 | floor := t.TempDir() |
| 376 | if err := os.WriteFile(filepath.Join(floor, "floor.cue"), []byte("package verification\n"), 0o644); err != nil { |
| 377 | t.Fatal(err) |
| 378 | } |
| 379 | b := newBlocker(t, filepath.Join(t.TempDir(), "cue")) |
| 380 | c := config{cue: b.script, schema: filepath.Join(floor, "floor.cue"), floor: composed.Floor{Path: floor}, projectPolicy: "policy/project"} |
| 381 | g := rebase.Git{Dir: f.work, Env: f.env} |
| 382 | tip := f.git("rev-parse", "HEAD") |
| 383 | bounded(t, b, func(ctx context.Context) error { |
| 384 | _, err := policyCap(ctx, g, c, tip) |
| 385 | return err |
| 386 | }) |
| 387 | } |
| 388 | |
| 389 | // Reading the floor from the instance repository ends at the pass's |
| 390 | // deadline, with the ssh that never answered killed: the scratch |
| 391 | // repository's git reaches origin unattended. |
| 392 | func TestInstanceReadEndsAtTheDeadline(t *testing.T) { |
| 393 | b := newBlocker(t, filepath.Join(t.TempDir(), "ssh")) |
| 394 | t.Setenv("GIT_SSH_COMMAND", b.script) |
| 395 | c := config{repo: "ssh://valley/project.git", state: t.TempDir(), principal: "restacker", |
| 396 | commitName: "The Restacker", commitEmail: "restacker@example.invalid", |
| 397 | instanceRepo: "ssh://valley/instance.git", floor: composed.Floor{Ref: "refs/heads/main", Dir: "policy/instance"}} |
| 398 | g, err := scratchRepo(context.Background(), c) |
| 399 | if err != nil { |
| 400 | t.Fatal(err) |
| 401 | } |
| 402 | bounded(t, b, func(ctx context.Context) error { |
| 403 | _, err := policyCap(ctx, g, c, strings.Repeat("0", 40)) |
| 404 | return err |
| 405 | }) |
| 406 | } |
| 407 | |
| 408 | // Through the host's rendered hook, the restacker principal, which holds |
| 409 | // the restack grant and not the request grant, adds a restack version to |
| 410 | // a change another principal owns: the version keeps the active title |
| 411 | // though its head's subject differs. The hook refuses a version it adds |
| 412 | // under any other title. It also takes a restack report from the |
| 413 | // restacker, built and then pushed, and refuses one from the owner, who |
| 414 | // holds no restack grant. |
| 415 | func TestRenderedHookTakesTheRestackAppend(t *testing.T) { |
| 416 | hook := os.Getenv(hookEnv) |
| 417 | if hook == "" { |
| 418 | t.Skip(hookEnv + " names no rendered hook; restack-unit supplies one") |
| 419 | } |
| 420 | withoutIdentity(t) |
| 421 | f := newFixture(t, hook) |
| 422 | was := f.readied("alice", "a", "A title unlike its commit's subject") |
| 423 | f.moveMain() |
| 424 | t.Setenv("VALLEY_PRINCIPAL", "restacker") |
| 425 | c := config{repo: f.origin, state: t.TempDir(), principal: "restacker", |
| 426 | commitName: "The Restacker", commitEmail: "restacker@example.invalid"} |
| 427 | ctx := context.Background() |
| 428 | g, err := scratchRepo(ctx, c) |
| 429 | if err != nil { |
| 430 | t.Fatal(err) |
| 431 | } |
| 432 | var stderr bytes.Buffer |
| 433 | g.Stderr = &stderr |
| 434 | _, head := restacked(t, g) |
| 435 | ref := change.Ref(rebase.Target, idA) |
| 436 | old := f.at(ref) |
| 437 | |
| 438 | before, err := rebase.ReadRecord(ctx, g, old) |
| 439 | if err != nil { |
| 440 | t.Fatal(err) |
| 441 | } |
| 442 | forged, err := rebase.NextRecord("kkkk", before, change.Version{ |
| 443 | Head: change.HeadPrefix + head, Title: "A forged title", Branch: "a", Uploader: "restacker", |
| 444 | }) |
| 445 | if err != nil { |
| 446 | t.Fatal(err) |
| 447 | } |
| 448 | commit, err := rebase.WriteRecord(ctx, g, forged, "forged", old, head) |
| 449 | if err != nil { |
| 450 | t.Fatal(err) |
| 451 | } |
| 452 | if w := rebase.PushRecord(ctx, g, idA, old, commit); w.Push != rebase.Refused || f.at(ref) != old { |
| 453 | t.Fatalf("a restack version under a forged title: %+v", w) |
| 454 | } |
| 455 | if !strings.Contains(stderr.String(), "under the title of the active one") { |
| 456 | t.Errorf("the hook's refusal does not name the title:\n%s", stderr.String()) |
| 457 | } |
| 458 | |
| 459 | p, err := rebase.BuildRestack(ctx, g, rebase.RestackAppend{ID: idA, Head: head, IfActive: was, Principal: "restacker"}) |
| 460 | if err != nil { |
| 461 | t.Fatal(err) |
| 462 | } |
| 463 | if ap := p.Push(ctx, g); ap.Err != nil || ap.Version != 2 || f.at(ref) != p.Commit { |
| 464 | t.Fatalf("the restack append: %+v\n%s", ap, stderr.String()) |
| 465 | } |
| 466 | if err := rebase.Fetch(ctx, g, f.at(ref)); err != nil { |
| 467 | t.Fatal(err) |
| 468 | } |
| 469 | r, err := rebase.ReadRecord(ctx, g, f.at(ref)) |
| 470 | if err != nil { |
| 471 | t.Fatal(err) |
| 472 | } |
| 473 | if v := r.ActiveVersion(); v.Title != "A title unlike its commit's subject" || v.Uploader != "restacker" || v.Branch != "a" || r.Owner != "alice" { |
| 474 | t.Errorf("the record on origin is %+v", r) |
| 475 | } |
| 476 | |
| 477 | read, tip, record := change.HeadPrefix+was, change.HeadPrefix+head, change.HeadPrefix+p.Commit |
| 478 | report := &change.RestackReport{ |
| 479 | Schema: change.RestackSchema, ChangeID: idA, Target: rebase.Target, |
| 480 | TargetTip: change.HeadPrefix + f.at("refs/heads/main"), Operation: "test-1", Result: change.RestackComplete, |
| 481 | Layers: []change.RestackLayer{{ |
| 482 | ChangeID: idA, Record: change.HeadPrefix + old, Version: 1, Head: change.HeadPrefix + was, |
| 483 | Branch: "a", BranchTip: &read, Published: &change.RestackPublished{Head: tip, Version: &p.Version, Record: &record}, |
| 484 | }}, |
| 485 | } |
| 486 | alice := f.as("alice") |
| 487 | alice.Stderr = &stderr |
| 488 | byAlice, err := rebase.WriteReport(ctx, alice, report, rebase.ReportMessage("kkkk", report), "") |
| 489 | if err != nil { |
| 490 | t.Fatal(err) |
| 491 | } |
| 492 | if w := rebase.PushReport(ctx, alice, idA, "", byAlice); w.Push != rebase.Refused { |
| 493 | t.Errorf("a report from a principal with no restack grant: %+v", w) |
| 494 | } |
| 495 | byRestacker, err := rebase.WriteReport(ctx, g, report, rebase.ReportMessage("kkkk", report), "") |
| 496 | if err != nil { |
| 497 | t.Fatal(err) |
| 498 | } |
| 499 | if w := rebase.PushReport(ctx, g, idA, "", byRestacker); w.Push != rebase.Pushed || f.at(rebase.RestackPrefix+idA) != byRestacker { |
| 500 | t.Errorf("a report from the restacker: %+v\n%s", w, stderr.String()) |
| 501 | } |
| 502 | } |