package inventory import ( "encoding/json" "testing" ) func madeOf(t *testing.T, refs map[string]string) []byte { t.Helper() var made []Artifact for name, ref := range refs { made = append(made, Artifact{Name: name, Kind: "bundle", Reference: ref}) } raw, err := json.Marshal(made) if err != nil { t.Fatal(err) } return raw } // R4e A9 (ADR 0453 ยง6, ADR 0499): at the switch, a build taken from the builder's key (`src2:`) whose every artifact has // the digest of the build-agent build before it (`src1:`) is no move: it stands on that build, wherever each was pushed. // One digest that differs is a move, as is a build of the old form after one of the new. func TestAKeysBuildWithTheOldBuildsDigestsIsNoMove(t *testing.T) { const d1, d2 = "sha256:1111111111111111111111111111111111111111111111111111111111111111", "sha256:2222222222222222222222222222222222222222222222222222222222222222" old := sourceRow{id: "build-1", fingerprint: "src1:aa", made: madeOf(t, map[string]string{ "tools": "artifact-store://x/tools/blobs/" + d1, "img": "artifact-store://x/img@" + d2})} key := sourceRow{id: "key-bb-1", fingerprint: "src2:bb", made: madeOf(t, map[string]string{ "tools": "artifact-store://x/tools/blobs/" + d1, "img": "registry.internal:5000/x/img@" + d2})} if at := standing([]sourceRow{key, old}, 0); at != 1 { t.Fatalf("the key's build with the same digests stands on %d, want the old build", at) } moved := key moved.made = madeOf(t, map[string]string{"tools": "artifact-store://x/tools/blobs/" + d2, "img": "x/img@" + d2}) if at := standing([]sourceRow{moved, old}, 0); at != 0 { t.Fatalf("a key's build with another digest stands on %d, want itself (a move)", at) } // Two key builds of one fingerprint are one run, as two builds of one fingerprint always were. again := key again.id = "key-bb-2" if at := standing([]sourceRow{again, key, old}, 0); at != 2 { t.Fatalf("a run of one new fingerprint on an old build of the same digests stands on %d", at) } // An old build after a new one never continues the new one's run. if at := standing([]sourceRow{old, key}, 0); at != 0 { t.Fatalf("an old build continues a new one's run: %d", at) } }