Files
mesh-sdk/go/membership/membership_test.go
T
jschoubben c61212eba7
mesh/repo-check head c61212eba77c merged into trunk c6e669f71e8e — pass: its merge-check.sh passed, each part in its toolchain: merge-check.sh (typescript)…
mesh/merge-gate head c61212eba77c merged into trunk c6e669f71e8e — pass: group feat/r4d composed: every machine composes with the change as it did without …
mesh/delivery delivered
mesh/delivery-group group feat/r4d merged: every member is merged
mesh/check failed: unit: ..test: node:internal/modules/esm/resolve:241 | c61212eba77c on 5652312f09d2 | unit 1629 run, 0 kept, 3 skipped | integration…
Make the membership, its subject, stream, bare address and the pin public, so the route proxy leaves the controller's module (issue 1633)
2026-10-22 09:51:01 +02:00

77 lines
2.9 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)
}
}
}