From 2f693175242f49b6b365e56911e250771a93e553 Mon Sep 17 00:00:00 2001 From: "Christoph Engelbert (noctarius)" Date: Mon, 24 Jul 2023 13:59:10 +0200 Subject: [PATCH] Added unit tests to check config properties and tags --- spi/config/configuration.go | 4 +-- spi/config/configuration_test.go | 32 +++++++++++++++++++++++ spi/config/constants_test.go | 45 ++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 spi/config/constants_test.go diff --git a/spi/config/configuration.go b/spi/config/configuration.go index 77c082f..6b38bc1 100644 --- a/spi/config/configuration.go +++ b/spi/config/configuration.go @@ -72,7 +72,6 @@ type PostgreSQLConfig struct { ReplicationSlot ReplicationSlotConfig `toml:"replicationslot" yaml:"replicationSlot"` Transaction TransactionConfig `toml:"transaction" yaml:"transaction"` Snapshot SnapshotConfig `toml:"snapshot" yaml:"snapshot"` - Internal InternalConfig `toml:"internal" yaml:"internal"` } type InternalConfig struct { @@ -161,7 +160,7 @@ type NatsConfig struct { } type KafkaSaslConfig struct { - Enabled bool `toml:"user" yaml:"enabled"` + Enabled bool `toml:"enabled" yaml:"enabled"` User string `toml:"user" yaml:"user"` Password string `toml:"password" yaml:"password"` Mechanism sarama.SASLMechanism `toml:"mechanism" yaml:"mechanism"` @@ -265,6 +264,7 @@ type Config struct { TimescaleDB TimescaleDBConfig `toml:"timescaledb" yaml:"timescaledb"` Logging LoggerConfig `toml:"logging" yaml:"logging"` StateStorage StateStorageConfig `toml:"statestorage" yaml:"stateStorage"` + Internal InternalConfig `toml:"internal" yaml:"internal"` Plugins []string `toml:"plugins" yaml:"plugins"` } diff --git a/spi/config/configuration_test.go b/spi/config/configuration_test.go index f9eea5e..a38c6eb 100644 --- a/spi/config/configuration_test.go +++ b/spi/config/configuration_test.go @@ -23,6 +23,7 @@ import ( "os" "reflect" "runtime" + "strings" "testing" ) @@ -326,3 +327,34 @@ postgresql.transaction.window.maxsize = 100000` assert.Equal(t, 60, config.PostgreSQL.Transaction.Window.Timeout) assert.Equal(t, uint(100000), config.PostgreSQL.Transaction.Window.MaxSize) } + +func Test_Config_Tags_Match_Between_Yaml_Toml(t *testing.T) { + configValue := reflect.ValueOf(Config{}) + + var recursiveCheck func(value reflect.Value, path string) + recursiveCheck = func(value reflect.Value, path string) { + numOfFields := value.NumField() + for i := 0; i < numOfFields; i++ { + fieldType := value.Type().Field(i) + + toml := fieldType.Tag.Get("toml") + yaml := fieldType.Tag.Get("yaml") + + fieldPath := toml + if len(path) > 0 { + fieldPath = path + "." + fieldPath + } + + if strings.ToLower(yaml) != toml { + t.Errorf("Yaml and Toml tags aren't matching at path %s: %s != %s", fieldPath, yaml, toml) + } + + fieldValue := value.Field(i) + if fieldValue.Kind() == reflect.Struct { + recursiveCheck(fieldValue, fieldPath) + } + } + } + + recursiveCheck(configValue, "") +} diff --git a/spi/config/constants_test.go b/spi/config/constants_test.go new file mode 100644 index 0000000..5bcecac --- /dev/null +++ b/spi/config/constants_test.go @@ -0,0 +1,45 @@ +package config + +import ( + "go/ast" + "go/parser" + "go/token" + "reflect" + "strings" + "testing" +) + +func Test_Constants_Properties(t *testing.T) { + file, err := parser.ParseFile(&token.FileSet{}, "./constants.go", nil, 0) + if err != nil { + t.Error(err) + } + + ast.Walk(&visitor{t: t}, file) +} + +type visitor struct { + config Config + t *testing.T +} + +func (v *visitor) Visit(node ast.Node) (w ast.Visitor) { + if valueSpec, ok := node.(*ast.ValueSpec); ok { + name := valueSpec.Names[0].Name + literal := valueSpec.Values[0].(*ast.BasicLit) + + element := reflect.ValueOf(v.config) + value := literal.Value[1 : len(literal.Value)-1] + + properties := strings.Split(value, ".") + for _, property := range properties { + if e, ok := findProperty(element, property); ok { + element = e + } else { + v.t.Errorf("Property %s isn't defined in Config", name) + break + } + } + } + return v +}