The grants part's old build wrote its wrapper for every membership, a held-back one too, with the unpublished composition inside; the new part never rewrites such a key, so the decoder took the held-back membership as given. It now reads a wrapper only when its published says published or unchanged. The codec's rows join the round-trip test (mesh-sdk is over its unit budget). Review round 1 of issue 774's fix.
112 lines
4.7 KiB
Go
112 lines
4.7 KiB
Go
package membership
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestACredentialIsReadOutOfABusAddress(t *testing.T) {
|
|
for _, c := range []struct{ in, user, password, bare string }{
|
|
{"nats://controller:secret@127.0.0.1:4222", "controller", "secret", "nats://127.0.0.1:4222"},
|
|
{"controller:secret@127.0.0.1:4222", "controller", "secret", "127.0.0.1:4222"},
|
|
{"nats://127.0.0.1:4222", "", "", "nats://127.0.0.1:4222"},
|
|
{"127.0.0.1:4222", "", "", "127.0.0.1:4222"},
|
|
// A password containing an at-sign: split on the last one.
|
|
{"nats://controller:a@b@127.0.0.1:4222", "controller", "a@b", "nats://127.0.0.1:4222"},
|
|
} {
|
|
user, password, bare := CredentialIn(c.in)
|
|
if user != c.user || password != c.password || bare != c.bare {
|
|
t.Errorf("%q read as %q/%q at %q; wanted %q/%q at %q", c.in, user, password, bare, c.user, c.password, c.bare)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestABareAddressCarriesNoCredentialAndAScheme(t *testing.T) {
|
|
for in, want := range map[string]string{
|
|
"nats://controller:secret@10.0.0.1:4222": "nats://10.0.0.1:4222",
|
|
"controller:secret@10.0.0.1:4222": "nats://10.0.0.1:4222",
|
|
"nats://10.0.0.1:4222": "nats://10.0.0.1:4222",
|
|
"10.0.0.1:4222": "nats://10.0.0.1:4222",
|
|
} {
|
|
if got := BareAddress(in); got != want {
|
|
t.Errorf("BareAddress(%q) = %q, want %q", in, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAMembershipIsPublishedAndReadAtTheSubjectDerivedFromTheTwoNames(t *testing.T) {
|
|
if got := Subject("ace", "route-proxy"); got != "mesh.assignment.ace.route-proxy" {
|
|
t.Errorf("subject: %q", got)
|
|
}
|
|
if got := DirectGetSubject("ace", "route-proxy"); got != "$JS.API.DIRECT.GET.ASSIGNMENTS.mesh.assignment.ace.route-proxy" {
|
|
t.Errorf("direct get: %q", got)
|
|
}
|
|
if AssignmentsStream != "ASSIGNMENTS" {
|
|
t.Errorf("stream: %q", AssignmentsStream)
|
|
}
|
|
}
|
|
|
|
// The shape on the wire is the controller's: a membership as issued reads back whole, with the routes kept raw.
|
|
func TestAMembershipRoundTripsAsIssued(t *testing.T) {
|
|
in := Membership{Node: "ace", Module: "route-proxy", Emits: "e", Tools: "t",
|
|
Serves: []Served{{Subject: "s", Queue: "q"}},
|
|
Receives: map[string]json.RawMessage{"route": json.RawMessage(`[{"name":"a"}]`)},
|
|
Mesh: []string{"10.10.0.7"},
|
|
State: []StateIssued{{Name: "n", Bucket: "b", Writes: true, Key: "ace"}},
|
|
SeatTraffic: &SeatTraffic{Publish: []string{"p"}, Kinds: []KindHeld{{Seat: "channel", Kind: "tg"}}}}
|
|
raw, err := json.Marshal(in)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var out Membership
|
|
if err := json.Unmarshal(raw, &out); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !reflect.DeepEqual(in, out) {
|
|
t.Fatalf("round trip changed it:\n%+v\n%+v", in, out)
|
|
}
|
|
var wire map[string]json.RawMessage
|
|
_ = json.Unmarshal(raw, &wire)
|
|
for _, k := range []string{"receives", "mesh", "seat-traffic"} {
|
|
if _, ok := wire[k]; !ok {
|
|
t.Errorf("field %q is not on the wire under that name", k)
|
|
}
|
|
}
|
|
|
|
// The grants part's memberships hold the bare membership through this package's codec; the wrapper written before
|
|
// 2026-10-30 still reads when it says the membership was published or unchanged, and anything else is an error,
|
|
// never an empty membership or one the part held back (issue 774).
|
|
m := Membership{Node: "ace", Module: "forge", Tools: "mesh.mod.forge.tool", Serves: []Served{{Subject: "mesh.mod.forge.tool.{tool}"}},
|
|
State: []StateIssued{{Name: "repos", Bucket: "forge_repos"}}}
|
|
inner, _ := json.Marshal(m)
|
|
wrapped := func(published string) []byte {
|
|
return []byte(`{"node":"ace","module":"forge","digest":"sha256:x","membership":` + string(inner) + published + `,"retired":["a b"]}`)
|
|
}
|
|
for _, c := range []struct {
|
|
name string
|
|
raw []byte
|
|
ok bool
|
|
}{
|
|
{"bare, as written", EncodeGrantsMembership(m), true},
|
|
{"wrapped and published, as the grants part wrote it before", wrapped(`,"published":"published"`), true},
|
|
{"wrapped and unchanged", wrapped(`,"published":"unchanged"`), true},
|
|
{"wrapped and held back", wrapped(`,"published":"not published, a shadow difference: publishing would lose x"`), false},
|
|
{"wrapped, saying nothing of its publishing", wrapped(""), false},
|
|
{"not json", []byte("garbage"), false},
|
|
{"an array", []byte(`[1,2]`), false},
|
|
{"an object naming no node or module", []byte(`{"serves":[{"subject":"s"}]}`), false},
|
|
{"a wrapper whose membership is no object", []byte(`{"node":"ace","module":"forge","membership":"x","published":"published"}`), false},
|
|
{"a wrapper whose membership names nothing", []byte(`{"node":"ace","module":"forge","membership":{},"published":"published"}`), false},
|
|
} {
|
|
got, err := DecodeGrantsMembership(c.raw)
|
|
if (err == nil) != c.ok {
|
|
t.Errorf("%s: err %v", c.name, err)
|
|
continue
|
|
}
|
|
if c.ok && !reflect.DeepEqual(got, m) {
|
|
t.Errorf("%s: read %+v, want %+v", c.name, got, m)
|
|
}
|
|
}
|
|
}
|