| 1 | package release |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | func TestPromoteWritesTheRecord(t *testing.T) { |
| 12 | s := NewDirStore(t.TempDir()) |
| 13 | ctx := context.Background() |
| 14 | priv, pub := testKeys(t) |
| 15 | shipRelease(t, s, "bab", "km", "v1", adoptPlatform, []byte("one")) |
| 16 | if err := Promote(ctx, s, priv, "bab", "km", "stable", "v1", Promotion{Actor: "a@example.com"}); err != nil { |
| 17 | t.Fatal(err) |
| 18 | } |
| 19 | rec, err := ReadPromotionRecord(ctx, s, pub, "bab", "km", "v1") |
| 20 | if err != nil || rec.Channel != "stable" || rec.Actor != "a@example.com" || rec.ManifestDigest != manifestDigestOf(t, s, "bab", "km", "v1") { |
| 21 | t.Fatalf("record = %+v, %v", rec, err) |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | // Spec §8: a promoted build that has left the pointer's history is accepted |
| 26 | // through its signed promotion record. |
| 27 | func TestABuildOutOfHistoryIsStillAuthorized(t *testing.T) { |
| 28 | s := NewDirStore(t.TempDir()) |
| 29 | ctx := context.Background() |
| 30 | priv, pub := testKeys(t) |
| 31 | for i := 0; i < historyMax+2; i++ { |
| 32 | v := "v" + string(rune('a'+i)) |
| 33 | shipRelease(t, s, "bab", "km", v, adoptPlatform, []byte(v)) |
| 34 | if err := Promote(ctx, s, priv, "bab", "km", "canary/x", v); err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | } |
| 38 | p, _ := Channel(ctx, s, pub, "bab", "km", "canary/x") |
| 39 | for _, h := range append([]string{p.Version}, p.History...) { |
| 40 | if h == "va" { |
| 41 | t.Fatal("va should have fallen out of the pointer") |
| 42 | } |
| 43 | } |
| 44 | if _, err := AuthorizedManifest(ctx, s, pub, "bab", "km", "va"); err != nil { |
| 45 | t.Fatalf("va left the history and lost its authorization: %v", err) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // Review Focus 4. |
| 50 | func TestOneRecordPerVersionAcrossChannels(t *testing.T) { |
| 51 | s := NewDirStore(t.TempDir()) |
| 52 | ctx := context.Background() |
| 53 | priv, pub := testKeys(t) |
| 54 | shipRelease(t, s, "bab", "km", "v1", adoptPlatform, []byte("one")) |
| 55 | for _, ch := range []string{"canary/x", "stable"} { |
| 56 | if err := Promote(ctx, s, priv, "bab", "km", ch, "v1"); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | } |
| 60 | if rec, _ := ReadPromotionRecord(ctx, s, pub, "bab", "km", "v1"); rec.Channel != "canary/x" { |
| 61 | t.Fatalf("record channel %q; want the first promotion's", rec.Channel) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Astra I1 #3: a repeat or a renewal of a legacy head never signs its |
| 66 | // current bytes. Only `carlos releases authorize` does. |
| 67 | func TestARepeatOrRetryNeverCreatesARecord(t *testing.T) { |
| 68 | s := NewDirStore(t.TempDir()) |
| 69 | ctx := context.Background() |
| 70 | priv, pub := testKeys(t) |
| 71 | shipRelease(t, s, "bab", "km", "v1", adoptPlatform, []byte("one")) |
| 72 | if err := Promote(ctx, s, priv, "bab", "km", "canary/x", "v1"); err != nil { |
| 73 | t.Fatal(err) |
| 74 | } |
| 75 | if err := s.Delete(ctx, PromotionRecordKey("bab", "km", "v1")); err != nil { // a head promoted before records |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | res, err := PromoteOnLadder(ctx, s, priv, "bab", "km", "canary/x", "v1", PromoteOptions{}) |
| 79 | if err != nil || !res.NoOp { |
| 80 | t.Fatalf("repeat = %+v, %v", res, err) |
| 81 | } |
| 82 | if _, err := ReadPromotionRecord(ctx, s, pub, "bab", "km", "v1"); !errors.Is(err, ErrNoPromotionRecord) { |
| 83 | t.Fatalf("a no-op repeat created a record: %v", err) |
| 84 | } |
| 85 | time.Sleep(1100 * time.Millisecond) |
| 86 | if _, err := PromoteOnLadder(ctx, s, priv, "bab", "km", "canary/x", "v1", PromoteOptions{Retry: true}); err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | if _, err := ReadPromotionRecord(ctx, s, pub, "bab", "km", "v1"); !errors.Is(err, ErrNoPromotionRecord) { |
| 90 | t.Fatalf("a retry created a record: %v", err) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | // refusingStore refuses writes under one prefix. |
| 95 | type refusingStore struct { |
| 96 | Store |
| 97 | prefix string |
| 98 | } |
| 99 | |
| 100 | func (r refusingStore) Put(ctx context.Context, key string, body []byte, ifMatch string) (string, error) { |
| 101 | if strings.HasPrefix(key, r.prefix) { |
| 102 | return "", errors.New("refused") |
| 103 | } |
| 104 | return r.Store.Put(ctx, key, body, ifMatch) |
| 105 | } |
| 106 | |
| 107 | // Review Focus 1. |
| 108 | func TestPromotionFailsBeforeThePointerWhenTheRecordCannotBeWritten(t *testing.T) { |
| 109 | base := NewDirStore(t.TempDir()) |
| 110 | ctx := context.Background() |
| 111 | priv, pub := testKeys(t) |
| 112 | shipRelease(t, base, "bab", "km", "v1", adoptPlatform, []byte("one")) |
| 113 | s := refusingStore{Store: base, prefix: "releases/bab/km/promoted/"} |
| 114 | if err := Promote(ctx, s, priv, "bab", "km", "stable", "v1"); err == nil { |
| 115 | t.Fatal("the promotion succeeded without its record") |
| 116 | } |
| 117 | if _, err := Channel(ctx, base, pub, "bab", "km", "stable"); err == nil { |
| 118 | t.Fatal("the pointer moved although the record was not written") |
| 119 | } |
| 120 | } |
| 121 | |