package catalogue import ( "strings" "testing" ) // A module rolls out by default; it records where it says so with why, where it keeps irreplaceable // data, and always where it is the bus (novox/hq ADR 0236). func TestWhereAModulesUpgradePolicyComesFrom(t *testing.T) { cases := []struct { name string m Manifest policy, from string }{ {"default", Manifest{Module: "app"}, PolicyRoll, FromDefault}, {"says record", Manifest{Module: "db", Upgrade: &UpgradePolicy{Policy: PolicyRecord, Why: "a restart costs"}}, PolicyRecord, FromModule}, {"says together", Manifest{Module: "dns", Upgrade: &UpgradePolicy{Policy: PolicyTogether, Why: "one zone"}}, PolicyTogether, FromModule}, {"irreplaceable data", Manifest{Module: "media", Data: &Data{Own: []DataItem{{ID: "library", Class: ClassIrreplaceable}}}}, PolicyRecord, FromData}, {"valuable data rolls", Manifest{Module: "notes", Data: &Data{Own: []DataItem{{ID: "db", Class: ClassValuable}}}}, PolicyRoll, FromDefault}, {"irreplaceable but says roll", Manifest{Module: "photos", Upgrade: &UpgradePolicy{Policy: PolicyRoll}, Data: &Data{Own: []DataItem{{ID: "originals", Class: ClassIrreplaceable}}}}, PolicyRoll, FromModule}, {"the bus, whatever it says", Manifest{Module: "nats", Provides: []Offer{{Name: "mesh-bus"}}, Upgrade: &UpgradePolicy{Policy: PolicyRoll}}, PolicyRecord, FromBus}, } for _, c := range cases { policy, from, _ := DerivedUpgrade(c.m) if policy != c.policy || from != c.from { t.Errorf("%s: %s from %s, want %s from %s", c.name, policy, from, c.policy, c.from) } // ADR 0565 clause 7 (issue 1953 L5-S3): a person's choice wins over each source but the bus; none leaves the // derivation as it was. person := &PersonPolicy{V: PersonPolicyVersion, Module: c.m.Module, Policy: PolicyTogether, Why: "one zone", By: "person.jochen"} policy, from, _, by := Upgrade(c.m, person) wantPolicy, wantFrom, wantBy := PolicyTogether, FromPerson, "person.jochen" if c.from == FromBus { wantPolicy, wantFrom, wantBy = c.policy, FromBus, "" } if policy != wantPolicy || from != wantFrom || by != wantBy { t.Errorf("%s with a person's choice: %s from %s by %q", c.name, policy, from, by) } if policy, from, _, _ := Upgrade(c.m, nil); policy != c.policy || from != c.from { t.Errorf("%s with no choice: %s from %s", c.name, policy, from) } } for _, p := range []PersonPolicy{{Module: "m", Policy: "sometimes", Why: "x", By: "p"}, {Module: "m", Policy: PolicyRecord, By: "p"}, {Module: "m", Policy: PolicyRecord, Why: "x"}} { if p.Check() == nil { t.Errorf("%+v was accepted", p) } } // A policy other than roll says why, and an unknown one is refused. for _, u := range []UpgradePolicy{{Policy: PolicyRecord}, {Policy: PolicyTogether}, {Policy: "sometimes", Why: "x"}} { if problems := (Manifest{Module: "m", Upgrade: &u}).upgradeProblems(); len(problems) == 0 { t.Errorf("%+v was accepted", u) } } if problems := (Manifest{Module: "m", Upgrade: &UpgradePolicy{Policy: PolicyRecord, Why: "a restart costs"}}).upgradeProblems(); len(problems) != 0 { t.Errorf("a record with why was refused: %v", problems) } if _, err := ParseManifest([]byte(`{"module":"m","upgrade":{"policy":"record"}}`)); err == nil || !strings.Contains(err.Error(), "says why") { t.Errorf("a manifest recording without why parsed: %v", err) } }