Add some tests for config update from env

This commit is contained in:
Torjus Håkestad 2021-12-08 13:07:08 +01:00
parent 94b5b45396
commit 84fc2d6667
2 changed files with 59 additions and 2 deletions

View File

@ -88,8 +88,13 @@ func FromDefault() *Config {
HTTP: &ServerHTTPConfig{ HTTP: &ServerHTTPConfig{
ListenAddr: ":8089", ListenAddr: ":8089",
}, },
DataStoreConfig: &ServerDataStoreConfig{}, DataStoreConfig: &ServerDataStoreConfig{
FileStoreConfig: &ServerFileStoreConfig{}, BoltStoreConfig: &BoltStoreConfig{},
},
FileStoreConfig: &ServerFileStoreConfig{
BoltStoreConfig: &BoltStoreConfig{},
FSStoreConfig: &FSStoreConfig{},
},
}, },
Client: &ClientConfig{ Client: &ClientConfig{
Certs: &CertificatePaths{}, Certs: &CertificatePaths{},

View File

@ -1,6 +1,7 @@
package config_test package config_test
import ( import (
"os"
"strings" "strings"
"testing" "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.")
}
})
} }