Browse Source

Fix configuration validation

Due to my over-cleverness and lack of use a pointer in the right spot
the configuration file was not capable of setting any actual settings.

I use the defaults in my test environment so ... there you go. There are
some basic tests now.

Signed-off-by: Erik Hollensbe <github@hollensbe.org>
master
Erik Hollensbe 7 months ago
parent
commit
790d90043f
3 changed files with 47 additions and 6 deletions
  1. +4
    -4
      config/config.go
  2. +41
    -0
      config/config_test.go
  3. +2
    -2
      service/service.go

+ 4
- 4
config/config.go View File

@ -32,15 +32,15 @@ type Config struct {
}
// Empty is a config that has all the defaults configured; usually for testing.
func Empty() Config {
c := Config{}
func Empty() *Config {
c := &Config{}
c.validateAndFix()
return c
}
// Parse parses the configuration in the file and returns it.
func Parse(filename string) (Config, error) {
var config Config
func Parse(filename string) (*Config, error) {
config := &Config{}
content, err := ioutil.ReadFile(filename)
if err != nil {


+ 41
- 0
config/config_test.go View File

@ -0,0 +1,41 @@
package config
import (
"reflect"
"testing"
)
func TestConfigDefaults(t *testing.T) {
t.Run("empty equals the defaults", func(t *testing.T) {
c := Empty()
if err := c.validateAndFix(); err != nil {
t.Fatalf("empty configuration errored out on validation: %v", err)
}
c2 := Empty()
if !reflect.DeepEqual(c, c2) {
t.Fatal("empty is not equal to the empty after validation")
}
c3 := &Config{}
if err := c3.validateAndFix(); err != nil {
t.Fatal("empty configuration did not validate properly")
}
if !reflect.DeepEqual(c2, c3) {
t.Fatal("empty, validated configuration was not equal to empty configuration")
}
})
t.Run("defaults are overridable before validation", func(t *testing.T) {
c := Empty()
c.Domain = "home"
if err := c.validateAndFix(); err != nil {
t.Fatalf("errored out changing the hostname pre-validation: %v", err)
}
if c.Domain != "home" {
t.Fatalf("previously set value was not preserved post-validation")
}
})
}

+ 2
- 2
service/service.go View File

@ -18,7 +18,7 @@ import (
// Service is the encapsulation of a fully composed service.
type Service struct {
config config.Config
config *config.Config
appName string
grpcS *grpc.Server
l net.Listener
@ -43,7 +43,7 @@ func (s *Service) InstallSignalHandler() {
}
// New constructs a new service from a config.Config
func New(name string, c config.Config) (*Service, error) {
func New(name string, c *config.Config) (*Service, error) {
db, err := dnsdb.New(c.DBFile)
if err != nil {
return nil, errors.Wrap(err, "could not open database")


Loading…
Cancel
Save