From 098fb1257536ecd7f061d2fc182c98f5bec766b8 Mon Sep 17 00:00:00 2001 From: Tommie Gannert Date: Tue, 4 Oct 2022 13:52:47 +0200 Subject: [PATCH] Add a test for cmd/generate-config. Veriying the generated configuration. global.jetstream.addresses is required in polylith mode. Adding a dummy value for CI. --- cmd/generate-config/main.go | 45 ++++++++++++++++++++++---------- cmd/generate-config/main_test.go | 33 +++++++++++++++++++++++ 2 files changed, 64 insertions(+), 14 deletions(-) create mode 100644 cmd/generate-config/main_test.go diff --git a/cmd/generate-config/main.go b/cmd/generate-config/main.go index e9e261cf8..a8ba52533 100644 --- a/cmd/generate-config/main.go +++ b/cmd/generate-config/main.go @@ -3,6 +3,7 @@ package main import ( "flag" "fmt" + "os" "path/filepath" "github.com/matrix-org/dendrite/setup/config" @@ -12,13 +13,31 @@ import ( ) func main() { - defaultsForCI := flag.Bool("ci", false, "Populate the configuration with sane defaults for use in CI") - serverName := flag.String("server", "", "The domain name of the server if not 'localhost'") - dbURI := flag.String("db", "", "The DB URI to use for all components (PostgreSQL only)") - dirPath := flag.String("dir", "./", "The folder to use for paths (like SQLite databases, media storage)") - normalise := flag.String("normalise", "", "Normalise an existing configuration file by adding new/missing options and defaults") - polylith := flag.Bool("polylith", false, "Generate a config that makes sense for polylith deployments") - flag.Parse() + cfg, err := buildConfig(flag.CommandLine, os.Args[1:]) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + + bs, err := yaml.Marshal(cfg) + if err != nil { + panic(err) + } + + fmt.Println(string(bs)) +} + +func buildConfig(fs *flag.FlagSet, args []string) (*config.Dendrite, error) { + defaultsForCI := fs.Bool("ci", false, "Populate the configuration with sane defaults for use in CI") + serverName := fs.String("server", "", "The domain name of the server if not 'localhost'") + dbURI := fs.String("db", "", "The DB URI to use for all components (PostgreSQL only)") + dirPath := fs.String("dir", "./", "The folder to use for paths (like SQLite databases, media storage)") + normalise := fs.String("normalise", "", "Normalise an existing configuration file by adding new/missing options and defaults") + polylith := fs.Bool("polylith", false, "Generate a config that makes sense for polylith deployments") + + if err := fs.Parse(args); err != nil { + return nil, err + } var cfg *config.Dendrite if *normalise == "" { @@ -95,6 +114,9 @@ func main() { cfg.UserAPI.BCryptCost = bcrypt.MinCost cfg.Global.JetStream.InMemory = true cfg.Global.JetStream.StoragePath = config.Path(*dirPath) + if *polylith { + cfg.Global.JetStream.Addresses = []string{"localhost"} + } cfg.ClientAPI.RegistrationDisabled = false cfg.ClientAPI.OpenRegistrationWithoutVerificationEnabled = true cfg.ClientAPI.RegistrationSharedSecret = "complement" @@ -106,14 +128,9 @@ func main() { } else { var err error if cfg, err = config.Load(*normalise, !*polylith); err != nil { - panic(err) + return nil, err } } - j, err := yaml.Marshal(cfg) - if err != nil { - panic(err) - } - - fmt.Println(string(j)) + return cfg, nil } diff --git a/cmd/generate-config/main_test.go b/cmd/generate-config/main_test.go new file mode 100644 index 000000000..df7aeca3c --- /dev/null +++ b/cmd/generate-config/main_test.go @@ -0,0 +1,33 @@ +package main + +import ( + "flag" + "testing" + + "github.com/matrix-org/dendrite/setup/config" +) + +func TestBuildConfig(t *testing.T) { + tsts := []struct { + Name string + Args []string + IsMonolith bool + }{ + {"ciMonolith", []string{"-ci"}, true}, + {"ciPolylith", []string{"-ci", "-polylith"}, false}, + } + for _, tst := range tsts { + t.Run(tst.Name, func(t *testing.T) { + cfg, err := buildConfig(flag.NewFlagSet("main_test", flag.ContinueOnError), tst.Args) + if err != nil { + t.Fatalf("buildConfig failed: %v", err) + } + + var ss config.ConfigErrors + cfg.Verify(&ss, tst.IsMonolith) + for _, s := range ss { + t.Errorf("Verify: %s", s) + } + }) + } +}