package broker import ( "flag" "os" "path/filepath" "testing" ) var update = flag.Bool("update", false, "rewrite the golden composition") // The composed file is the mesh's whole authority model, so a change to it should be visible in a // review rather than inferred from a diff of Go. The fixture is also the exact text checked // against the real server's parser (`nats-server -t`), which is what says this syntax is the // server's and not one we invented. func TestTheComposedConfigMatchesTheGolden(t *testing.T) { seat := Seat{Name: "telegram-sender", Accepts: []string{"send"}, Emits: []string{"delivered", "failed"}} got, err := Compose( Server{ClientPort: 4222, MonitoringPort: 8222, StoreDir: "/data", TLSCert: "/tls/tls.crt", TLSKey: "/tls/tls.key", TLSCA: "/tls/ca.crt"}, []Principal{ {Kind: KindController, PasswordHash: "$2a$11$cccccccccccccccccccccc"}, {Kind: KindEnrolment, PasswordHash: "$2a$11$eeeeeeeeeeeeeeeeeeeeee"}, {Kind: KindNode, Node: "one", PasswordHash: "$2a$11$nnnnnnnnnnnnnnnnnnnnnn"}, {Kind: KindModule, Node: "one", Module: "telegram", Holds: []Seat{seat}, Serves: []string{"status"}, PasswordHash: "$2a$11$tttttttttttttttttttttt"}, {Kind: KindModule, Node: "two", Module: "shop", Uses: []Seat{seat}, Emits: []string{"order.placed"}, PasswordHash: "$2a$11$ssssssssssssssssssssss"}, {Kind: KindModule, Node: "two", Module: "audit", Consumes: []string{"shop.order.placed"}, PasswordHash: "$2a$11$aaaaaaaaaaaaaaaaaaaaaa"}, }) if err != nil { t.Fatal(err) } golden := filepath.Join("testdata", "composed.conf") if *update { if err := os.WriteFile(golden, []byte(got), 0o644); err != nil { t.Fatal(err) } return } want, err := os.ReadFile(golden) if err != nil { t.Fatal(err) } if got != string(want) { t.Errorf("composition changed; re-run with -update and read the diff:\n%s", got) } }