Skip to content

Commit

Permalink
Added unit tests to check config properties and tags
Browse files Browse the repository at this point in the history
  • Loading branch information
noctarius committed Jul 24, 2023
1 parent b5c5eac commit 2f69317
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
4 changes: 2 additions & 2 deletions spi/config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
}

Expand Down
32 changes: 32 additions & 0 deletions spi/config/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"os"
"reflect"
"runtime"
"strings"
"testing"
)

Expand Down Expand Up @@ -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, "")
}
45 changes: 45 additions & 0 deletions spi/config/constants_test.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 2f69317

Please sign in to comment.