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) } } } // A policy other than roll says why, and an unknown one is refused. func TestAnUpgradePolicySaysWhy(t *testing.T) { 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) } }