| new file mode 100644 |
| index 000000000..cf6e55ae6 |
| --- /dev/null |
| +++ b/internal/release/promote_record_test.go |
| @@ -0,0 +1,120 @@ |
| +package release |
| + |
| +import ( |
| + "context" |
| + "errors" |
| + "strings" |
| + "testing" |
| + "time" |
| +) |
| + |
| +func TestPromoteWritesTheRecord(t *testing.T) { |
| + s := NewDirStore(t.TempDir()) |
| + ctx := context.Background() |
| + priv, pub := testKeys(t) |
| + shipRelease(t, s, "bab", "km", "v1", adoptPlatform, []byte("one")) |
| + if err := Promote(ctx, s, priv, "bab", "km", "stable", "v1", Promotion{Actor: "a@example.com"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + rec, err := ReadPromotionRecord(ctx, s, pub, "bab", "km", "v1") |
| + if err != nil || rec.Channel != "stable" || rec.Actor != "a@example.com" || rec.ManifestDigest != manifestDigestOf(t, s, "bab", "km", "v1") { |
| + t.Fatalf("record = %+v, %v", rec, err) |
| + } |
| +} |
| + |
| +// Spec §8: a promoted build that has left the pointer's history is accepted |
| +// through its signed promotion record. |
| +func TestABuildOutOfHistoryIsStillAuthorized(t *testing.T) { |
| + s := NewDirStore(t.TempDir()) |
| + ctx := context.Background() |
| + priv, pub := testKeys(t) |
| + for i := 0; i < historyMax+2; i++ { |
| + v := "v" + string(rune('a'+i)) |
| + shipRelease(t, s, "bab", "km", v, adoptPlatform, []byte(v)) |
| + if err := Promote(ctx, s, priv, "bab", "km", "canary/x", v); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + p, _ := Channel(ctx, s, pub, "bab", "km", "canary/x") |
| + for _, h := range append([]string{p.Version}, p.History...) { |
| + if h == "va" { |
| + t.Fatal("va should have fallen out of the pointer") |
| + } |
| + } |
| + if _, err := AuthorizedManifest(ctx, s, pub, "bab", "km", "va"); err != nil { |
| + t.Fatalf("va left the history and lost its authorization: %v", err) |
| + } |
| +} |
| + |
| +// Review Focus 4. |
| +func TestOneRecordPerVersionAcrossChannels(t *testing.T) { |
| + s := NewDirStore(t.TempDir()) |
| + ctx := context.Background() |
| + priv, pub := testKeys(t) |
| + shipRelease(t, s, "bab", "km", "v1", adoptPlatform, []byte("one")) |
| + for _, ch := range []string{"canary/x", "stable"} { |
| + if err := Promote(ctx, s, priv, "bab", "km", ch, "v1"); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + if rec, _ := ReadPromotionRecord(ctx, s, pub, "bab", "km", "v1"); rec.Channel != "canary/x" { |
| + t.Fatalf("record channel %q; want the first promotion's", rec.Channel) |
| + } |
| +} |
| + |
| +// Astra I1 #3: a repeat or a renewal of a legacy head never signs its |
| +// current bytes. Only `carlos releases authorize` does. |
| +func TestARepeatOrRetryNeverCreatesARecord(t *testing.T) { |
| + s := NewDirStore(t.TempDir()) |
| + ctx := context.Background() |
| + priv, pub := testKeys(t) |
| + shipRelease(t, s, "bab", "km", "v1", adoptPlatform, []byte("one")) |
| + if err := Promote(ctx, s, priv, "bab", "km", "canary/x", "v1"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := s.Delete(ctx, PromotionRecordKey("bab", "km", "v1")); err != nil { // a head promoted before records |
| + t.Fatal(err) |
| + } |
| + res, err := PromoteOnLadder(ctx, s, priv, "bab", "km", "canary/x", "v1", PromoteOptions{}) |
| + if err != nil || !res.NoOp { |
| + t.Fatalf("repeat = %+v, %v", res, err) |
| + } |
| + if _, err := ReadPromotionRecord(ctx, s, pub, "bab", "km", "v1"); !errors.Is(err, ErrNoPromotionRecord) { |
| + t.Fatalf("a no-op repeat created a record: %v", err) |
| + } |
| + time.Sleep(1100 * time.Millisecond) |
| + if _, err := PromoteOnLadder(ctx, s, priv, "bab", "km", "canary/x", "v1", PromoteOptions{Retry: true}); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := ReadPromotionRecord(ctx, s, pub, "bab", "km", "v1"); !errors.Is(err, ErrNoPromotionRecord) { |
| + t.Fatalf("a retry created a record: %v", err) |
| + } |
| +} |
| + |
| +// refusingStore refuses writes under one prefix. |
| +type refusingStore struct { |
| + Store |
| + prefix string |
| +} |
| + |
| +func (r refusingStore) Put(ctx context.Context, key string, body []byte, ifMatch string) (string, error) { |
| + if strings.HasPrefix(key, r.prefix) { |
| + return "", errors.New("refused") |
| + } |
| + return r.Store.Put(ctx, key, body, ifMatch) |
| +} |
| + |
| +// Review Focus 1. |
| +func TestPromotionFailsBeforeThePointerWhenTheRecordCannotBeWritten(t *testing.T) { |
| + base := NewDirStore(t.TempDir()) |
| + ctx := context.Background() |
| + priv, pub := testKeys(t) |
| + shipRelease(t, base, "bab", "km", "v1", adoptPlatform, []byte("one")) |
| + s := refusingStore{Store: base, prefix: "releases/bab/km/promoted/"} |
| + if err := Promote(ctx, s, priv, "bab", "km", "stable", "v1"); err == nil { |
| + t.Fatal("the promotion succeeded without its record") |
| + } |
| + if _, err := Channel(ctx, base, pub, "bab", "km", "stable"); err == nil { |
| + t.Fatal("the pointer moved although the record was not written") |
| + } |
| +} |