From 84fc2d6667b0b4c8aa81ca3e2632c479c51ef37c Mon Sep 17 00:00:00 2001 From: = Date: Wed, 8 Dec 2021 13:07:08 +0100 Subject: [PATCH] Add some tests for config update from env --- config/config.go | 9 ++++++-- config/config_test.go | 52 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/config/config.go b/config/config.go index 229485d..4c1f3d4 100644 --- a/config/config.go +++ b/config/config.go @@ -88,8 +88,13 @@ func FromDefault() *Config { HTTP: &ServerHTTPConfig{ ListenAddr: ":8089", }, - DataStoreConfig: &ServerDataStoreConfig{}, - FileStoreConfig: &ServerFileStoreConfig{}, + DataStoreConfig: &ServerDataStoreConfig{ + BoltStoreConfig: &BoltStoreConfig{}, + }, + FileStoreConfig: &ServerFileStoreConfig{ + BoltStoreConfig: &BoltStoreConfig{}, + FSStoreConfig: &FSStoreConfig{}, + }, }, Client: &ClientConfig{ Certs: &CertificatePaths{}, diff --git a/config/config_test.go b/config/config_test.go index 449c9f1..7f8414d 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1,6 +1,7 @@ package config_test import ( + "os" "strings" "testing" @@ -206,4 +207,55 @@ func TestConfig(t *testing.T) { }) } }) + t.Run("FromEnv", func(t *testing.T) { + // Unset any existing ezshare env vars + for _, env := range os.Environ() { + if strings.HasPrefix(env, "EZSHARE") { + os.Unsetenv(env) + } + } + + cfg := config.FromDefault() + // Test Server.LogLevel + if cfg.Server.LogLevel == "WARN" { + t.Errorf("Loglevel is WARN before updating from env.") + } + os.Setenv("EZSHARE_SERVER_LOGLEVEL", "WARN") + if err := cfg.UpdateFromEnv(); err != nil { + t.Fatalf("Error updating config from environment: %s", err) + } + if cfg.Server.LogLevel != "WARN" { + t.Errorf("Loglevel is not WARN after updating from env.") + } + + // Test Server.Hostname + hostname := "https://share.example.org" + os.Setenv("EZSHARE_SERVER_HOSTNAME", hostname) + if err := cfg.UpdateFromEnv(); err != nil { + t.Fatalf("Error updating config from environment: %s", err) + } + if cfg.Server.Hostname != hostname { + t.Errorf("Hostname is incorrect after updating from env.") + } + + // Test Server.Datastore.Bolt.Path + boltPath := "/data/bolt.db" + os.Setenv("EZSHARE_SERVER_DATASTORE_BOLT_PATH", boltPath) + if err := cfg.UpdateFromEnv(); err != nil { + t.Fatalf("Error updating config from environment: %s", err) + } + if cfg.Server.DataStoreConfig.BoltStoreConfig.Path != boltPath { + t.Errorf("Bolt path is incorrect after updating from env.") + } + + // Test Server.Datastore.Bolt.Path + caCertPath := "/data/cert.pem" + os.Setenv("EZSHARE_SERVER_GRPC_CACERTS_CERTIFICATEKEYPATH", caCertPath) + if err := cfg.UpdateFromEnv(); err != nil { + t.Fatalf("Error updating config from environment: %s", err) + } + if cfg.Server.GRPC.CACerts.CertificateKeyPath != caCertPath { + t.Errorf("GPRC CA Cert path is incorrect after updating from env.") + } + }) }