| 1 | package provision |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/release" |
| 9 | ) |
| 10 | |
| 11 | func intp(n int) *int { return &n } |
| 12 | |
| 13 | func TestARecordWithoutSettingsIsToday(t *testing.T) { |
| 14 | rec := Record{Schema: 1, Host: "a.example.com", App: "km", Account: "bab", Channel: "stable"} |
| 15 | u := EffectiveUpdates(rec, nil) |
| 16 | if !u.Default() || u.Channel != "stable" || u.Column() != "" { |
| 17 | t.Fatalf("EffectiveUpdates = %+v (column %q)", u, u.Column()) |
| 18 | } |
| 19 | b, _ := json.Marshal(rec) |
| 20 | for _, k := range []string{`"updates"`, `"major"`, `"pin"`, `"install_request"`} { |
| 21 | if strings.Contains(string(b), k) { |
| 22 | t.Fatalf("an unset %s leaked into the record: %s", k, b) |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestDefaultsApplyWhereNothingIsSet(t *testing.T) { |
| 28 | p := &UpdatePolicy{DefaultUpdates: release.UpdatesManual, DefaultMajor: intp(3), InstanceControl: []string{"pin"}} |
| 29 | u := EffectiveUpdates(Record{Channel: "stable"}, p) |
| 30 | if u.Updates != release.UpdatesManual || *u.Major != 3 || len(u.Control) != 1 { |
| 31 | t.Fatalf("defaults = %+v", u) |
| 32 | } |
| 33 | u = EffectiveUpdates(Record{Channel: "stable", Updates: release.UpdatesAuto, Major: intp(4)}, p) |
| 34 | if u.Updates != release.UpdatesAuto || *u.Major != 4 { |
| 35 | t.Fatalf("explicit values lost to defaults: %+v", u) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | // Review Focus 1. |
| 40 | func TestEffectiveUpdatesDropsMajorOffStable(t *testing.T) { |
| 41 | u := EffectiveUpdates(Record{Channel: "beta", Major: intp(2)}, &UpdatePolicy{DefaultMajor: intp(1)}) |
| 42 | if u.Major != nil { |
| 43 | t.Fatalf("major off stable = %d; it can never apply there", *u.Major) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestEffectiveUpdatesCarriesTheRequest(t *testing.T) { |
| 48 | u := EffectiveUpdates(Record{Channel: "stable", InstallRequest: &InstallRequest{ID: 7, FastTrack: true}}, nil) |
| 49 | if u.InstallRequest != 7 || !u.InstallFastTrack { |
| 50 | t.Fatalf("request = %+v", u) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func TestValidateUpdates(t *testing.T) { |
| 55 | ok := []Record{ |
| 56 | {Channel: "stable"}, |
| 57 | {Channel: "stable", Updates: "manual", Major: intp(0), Pin: "v1"}, |
| 58 | } |
| 59 | for _, r := range ok { |
| 60 | if err := ValidateUpdates(r); err != nil { |
| 61 | t.Errorf("%+v: %v", r, err) |
| 62 | } |
| 63 | } |
| 64 | bad := []Record{ |
| 65 | {Channel: "stable", Updates: "sometimes"}, |
| 66 | {Channel: "stable", Major: intp(-1)}, |
| 67 | {Channel: "beta", Major: intp(1)}, |
| 68 | {Channel: "stable", Pin: "../etc"}, |
| 69 | } |
| 70 | for _, r := range bad { |
| 71 | if err := ValidateUpdates(r); err == nil { |
| 72 | t.Errorf("%+v was accepted", r) |
| 73 | } |
| 74 | } |
| 75 | if ValidatePolicy(&UpdatePolicy{InstanceControl: []string{"bake"}}) == nil || ValidatePolicy(&UpdatePolicy{DefaultUpdates: "x"}) == nil { |
| 76 | t.Fatal("ValidatePolicy accepted nonsense") |
| 77 | } |
| 78 | } |
| 79 | |