package catalogue import ( "encoding/json" "testing" ) // A module may answer one requirement more than once, the sibling of ADR 0094 for `contributes` // rather than `secrets`: an object store's data API and its console are two different public // names, not one. `contributes` maps a requirement to several sets of values under local names, // each reaching the provider as its own entry — the same "several from one" shape ADR 0094 gave // `secrets`, applied to the other half of an edge. const twoRoutes = `{"module":"minio","version":"1","requires":["route"], "contributes":{"route":{"api":{"label":"files-api","port":9000},"console":{"label":"files","port":9001}}}}` func TestContributesReadsBothShapesAndWritesThemBack(t *testing.T) { m, err := decodeForTest([]byte(twoRoutes)) if err != nil { t.Fatal(err) } locals := m.ContributesMany["route"] if len(locals) != 2 || locals["api"]["label"] != "files-api" || locals["console"]["port"] != float64(9001) { t.Fatalf("two contributions under local names: %+v", locals) } plain, err := decodeForTest([]byte(`{"module":"board","version":"1","requires":["route"], "contributes":{"route":{"label":"board","port":8080}}}`)) if err != nil { t.Fatal(err) } if got := plain.Contributes["route"]; got["label"] != "board" || len(plain.ContributesMany) != 0 { t.Fatalf("the plain shape is one contribution with no local names: %+v / %+v", got, plain.ContributesMany) } // Written back in the shape it was read, so a built manifest keeps its local names. raw, err := json.Marshal(m) if err != nil { t.Fatal(err) } again, err := decodeForTest(raw) if err != nil { t.Fatalf("what was written does not read: %v\n%s", err, raw) } if len(again.ContributesMany["route"]) != 2 { t.Fatalf("the local names did not survive a round trip:\n%s", raw) } }