package resolution import ( "encoding/json" "testing" mcat "git.novox.be/novox/mesh-sdk/go/catalogue" ) const twoRoutes = `{"module":"minio","version":"1","requires":["route"], "contributes":{"route":{"api":{"label":"files-api","port":9000},"console":{"label":"files","port":9001}}}}` func minimalRouteProxy() mcat.Manifest { return mcat.Manifest{Module: "route-proxy", Version: "1", Provides: mcat.FromAnywhere("route"), Receives: map[string]string{"route": "/var/lib/route-proxy/routes/mesh.json"}, } } func TestAModuleWithTwoRoutesGivesTheProviderTwoContributions(t *testing.T) { minio, err := decodeForTest([]byte(twoRoutes)) if err != nil { t.Fatal(err) } got, err := Resolve(shelf(minimalRouteProxy(), minio), []string{"route-proxy", "minio"}, workstation(), mcat.World{}) if err != nil { t.Fatal(err) } out, err := Declaration(got, mcat.Rendering{}) if err != nil { t.Fatal(err) } var given []mcat.Contribution for _, r := range out { if r["path"] != "/var/lib/route-proxy/routes/mesh.json" { continue } var parsed struct { Given []mcat.Contribution `json:"given"` } if err := json.Unmarshal([]byte(r["content"].(string)), &parsed); err != nil { t.Fatal(err) } given = parsed.Given } if len(given) != 2 { t.Fatalf("two named routes from one module are two contributions: %+v", given) } byPort := map[float64]string{} for _, g := range given { if g.From != "minio" { t.Fatalf("both contributions are minio's: %+v", g) } port, _ := g.Values["port"].(float64) label, _ := g.Values["label"].(string) byPort[port] = label } if byPort[9000] != "files-api" || byPort[9001] != "files" { t.Fatalf("the two routes did not both survive: %+v", given) } } // A module with the ordinary, single-contribution shape resolves exactly as it did before — // ContributesMany being empty must change nothing about it. func TestASingleRouteStillResolvesTheOrdinaryWay(t *testing.T) { got, err := Resolve(shelf(proxy(), published("board", "board", 8080)), []string{"board"}, workstation(), mcat.World{}) if err != nil { t.Fatal(err) } given := received(t, mustDeclare(t, got)) if len(given) != 1 || given[0].From != "board" { t.Fatalf("the plain shape regressed: %+v", given) } } // ContributionsFrom is what mints the ONE pair credential a requiring module is granted // (cmd/mesh-controller/plan.go's grantsFor) — a separate path from Declaration()'s raw file, and // the one the two-routes test above never exercised. Where a module contributes several times, // there is no single "the" value: settling to whichever sorts first would both misrepresent the // grant and collide with that same contribution's own entry from contributions(), which is // exactly the duplicate a live plan against minio surfaced (files-api appearing once with a // credential, once without, while files got neither). func TestContributionsFromHasNoSingleValueWhenAModuleContributesSeveralTimes(t *testing.T) { minio, err := decodeForTest([]byte(twoRoutes)) if err != nil { t.Fatal(err) } got, err := Resolve(shelf(minimalRouteProxy(), minio), []string{"route-proxy", "minio"}, workstation(), mcat.World{}) if err != nil { t.Fatal(err) } values, asks, err := ContributionsFrom(got, "route", "minio", nil) if err != nil { t.Fatal(err) } if !asks { t.Fatal("minio still requires route, so it still asks") } if len(values) != 0 { t.Fatalf("no single value represents two contributions, got %+v", values) } } // The ordinary, single-contribution case is unchanged: exactly one match still settles to it. func TestContributionsFromReturnsTheOneValueForAnOrdinaryContribution(t *testing.T) { got, err := Resolve(shelf(proxy(), published("board", "board", 8080)), []string{"board"}, workstation(), mcat.World{}) if err != nil { t.Fatal(err) } values, asks, err := ContributionsFrom(got, "reverse-proxy", "board", nil) if err != nil { t.Fatal(err) } if !asks || values["host"] != "board" { t.Fatalf("the ordinary single contribution should still settle to its own value: %+v", values) } }