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.
This commit is contained in:
Tommie Gannert 2022-10-04 13:52:47 +02:00
parent a773f24782
commit 098fb12575
2 changed files with 64 additions and 14 deletions

View file

@ -3,6 +3,7 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"os"
"path/filepath" "path/filepath"
"github.com/matrix-org/dendrite/setup/config" "github.com/matrix-org/dendrite/setup/config"
@ -12,13 +13,31 @@ import (
) )
func main() { func main() {
defaultsForCI := flag.Bool("ci", false, "Populate the configuration with sane defaults for use in CI") cfg, err := buildConfig(flag.CommandLine, os.Args[1:])
serverName := flag.String("server", "", "The domain name of the server if not 'localhost'") if err != nil {
dbURI := flag.String("db", "", "The DB URI to use for all components (PostgreSQL only)") fmt.Fprintln(os.Stderr, err)
dirPath := flag.String("dir", "./", "The folder to use for paths (like SQLite databases, media storage)") os.Exit(1)
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() 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 var cfg *config.Dendrite
if *normalise == "" { if *normalise == "" {
@ -95,6 +114,9 @@ func main() {
cfg.UserAPI.BCryptCost = bcrypt.MinCost cfg.UserAPI.BCryptCost = bcrypt.MinCost
cfg.Global.JetStream.InMemory = true cfg.Global.JetStream.InMemory = true
cfg.Global.JetStream.StoragePath = config.Path(*dirPath) cfg.Global.JetStream.StoragePath = config.Path(*dirPath)
if *polylith {
cfg.Global.JetStream.Addresses = []string{"localhost"}
}
cfg.ClientAPI.RegistrationDisabled = false cfg.ClientAPI.RegistrationDisabled = false
cfg.ClientAPI.OpenRegistrationWithoutVerificationEnabled = true cfg.ClientAPI.OpenRegistrationWithoutVerificationEnabled = true
cfg.ClientAPI.RegistrationSharedSecret = "complement" cfg.ClientAPI.RegistrationSharedSecret = "complement"
@ -106,14 +128,9 @@ func main() {
} else { } else {
var err error var err error
if cfg, err = config.Load(*normalise, !*polylith); err != nil { if cfg, err = config.Load(*normalise, !*polylith); err != nil {
panic(err) return nil, err
} }
} }
j, err := yaml.Marshal(cfg) return cfg, nil
if err != nil {
panic(err)
}
fmt.Println(string(j))
} }

View file

@ -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)
}
})
}
}