init project
This commit is contained in:
1 parent
d63cc8d9f5
commit
ee07869839
39 files changed
+1560
-622
No files matched your search
@@ -0,0 +1,73 @@
|
||||
package configs_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
cronconfig "micro_layout/cmd/crontab/config"
|
||||
srvconfig "micro_layout/cmd/srv/config"
|
||||
"micro_layout/internal/configs"
|
||||
)
|
||||
|
||||
func TestLoadEntryConfigurations(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
selected := filepath.Join(dir, "local.ymal")
|
||||
if err := os.WriteFile(selected, []byte("app:\n name: from-file\nlog:\n level: debug\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("CONFIG_FILE", filepath.Join(dir, "missing.yaml"))
|
||||
t.Setenv("APP_NAME", "from-env")
|
||||
t.Setenv("APP_ENV", "test")
|
||||
t.Setenv("APP_STORAGE_MYSQL_ENABLED", "false")
|
||||
t.Setenv("APP_STORAGE_REDIS_ENABLED", "false")
|
||||
t.Setenv("APP_JETSTREAM_ENABLED", "false")
|
||||
var srv srvconfig.Config
|
||||
if err := configs.Load(selected, &srv); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if srv.App.Name != "from-env" || srv.Log.Level != "debug" || srv.Server.ShutdownTimeout != 10*time.Second {
|
||||
t.Fatalf("environment, file and default precedence changed: app=%+v log=%+v server=%+v", srv.App, srv.Log, srv.Server)
|
||||
}
|
||||
var cron cronconfig.Config
|
||||
if err := configs.Load(selected, &cron); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cron.App != srv.App || cron.Log != srv.Log || cron.Server != srv.Server {
|
||||
t.Fatalf("srv and crontab disagree when reading the shared configuration")
|
||||
}
|
||||
if err := configs.Load("", &srv); err == nil {
|
||||
t.Fatal("explicit CONFIG_FILE pointing to a missing file was accepted")
|
||||
}
|
||||
t.Setenv("CONFIG_FILE", selected)
|
||||
if err := configs.Load("", &srv); err != nil {
|
||||
t.Fatalf("CONFIG_FILE was not used: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadRejectsInvalidConfiguration(t *testing.T) {
|
||||
t.Setenv("APP_STORAGE_MYSQL_ENABLED", "false")
|
||||
t.Setenv("APP_STORAGE_REDIS_ENABLED", "false")
|
||||
t.Setenv("APP_JETSTREAM_ENABLED", "false")
|
||||
for _, data := range []string{
|
||||
"unknown_key: value\n",
|
||||
"server:\n shutdown_timeout: 0s\n",
|
||||
"storage:\n mysql:\n enabled: true\n",
|
||||
} {
|
||||
t.Run(data, func(t *testing.T) {
|
||||
file := filepath.Join(t.TempDir(), "invalid.yaml")
|
||||
if err := os.WriteFile(file, []byte(data), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if data == "storage:\n mysql:\n enabled: true\n" {
|
||||
t.Setenv("APP_STORAGE_MYSQL_ENABLED", "true")
|
||||
t.Setenv("APP_STORAGE_MYSQL_DSN", "")
|
||||
}
|
||||
var cfg srvconfig.Config
|
||||
if err := configs.Load(file, &cfg); err == nil {
|
||||
t.Fatal("invalid configuration was accepted")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user