125 lines
3.2 KiB
Go
125 lines
3.2 KiB
Go
package config_test
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/uopi/uopi/internal/config"
|
|
)
|
|
|
|
func TestDefault(t *testing.T) {
|
|
cfg := config.Default()
|
|
if cfg.Server.Listen == "" {
|
|
t.Error("Default Listen must not be empty")
|
|
}
|
|
if cfg.Server.StorageDir == "" {
|
|
t.Error("Default StorageDir must not be empty")
|
|
}
|
|
if !cfg.Datasource.Stub.Enabled {
|
|
t.Error("Stub should be enabled by default")
|
|
}
|
|
if !cfg.Datasource.EPICS.Enabled {
|
|
t.Error("EPICS should be enabled by default")
|
|
}
|
|
if !cfg.Datasource.Synthetic.Enabled {
|
|
t.Error("Synthetic should be enabled by default")
|
|
}
|
|
}
|
|
|
|
func TestLoadEmptyPath(t *testing.T) {
|
|
// Empty path should return defaults without error.
|
|
cfg, err := config.Load("")
|
|
if err != nil {
|
|
t.Fatalf("Load with empty path: %v", err)
|
|
}
|
|
if cfg.Server.Listen != config.Default().Server.Listen {
|
|
t.Errorf("Listen = %q, want default %q", cfg.Server.Listen, config.Default().Server.Listen)
|
|
}
|
|
}
|
|
|
|
func TestLoadTOML(t *testing.T) {
|
|
toml := `
|
|
[server]
|
|
listen = ":9090"
|
|
storage_dir = "/tmp/uopi-test"
|
|
|
|
[datasource.stub]
|
|
enabled = false
|
|
|
|
[datasource.epics]
|
|
enabled = false
|
|
ca_addr_list = "192.168.1.1"
|
|
`
|
|
f := filepath.Join(t.TempDir(), "uopi.toml")
|
|
if err := os.WriteFile(f, []byte(toml), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
cfg, err := config.Load(f)
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if cfg.Server.Listen != ":9090" {
|
|
t.Errorf("Listen = %q, want :9090", cfg.Server.Listen)
|
|
}
|
|
if cfg.Server.StorageDir != "/tmp/uopi-test" {
|
|
t.Errorf("StorageDir = %q", cfg.Server.StorageDir)
|
|
}
|
|
if cfg.Datasource.Stub.Enabled {
|
|
t.Error("Stub.Enabled should be false")
|
|
}
|
|
if cfg.Datasource.EPICS.Enabled {
|
|
t.Error("EPICS.Enabled should be false")
|
|
}
|
|
if cfg.Datasource.EPICS.CAAddrList != "192.168.1.1" {
|
|
t.Errorf("CAAddrList = %q", cfg.Datasource.EPICS.CAAddrList)
|
|
}
|
|
}
|
|
|
|
func TestLoadBadPath(t *testing.T) {
|
|
_, err := config.Load("/no/such/file.toml")
|
|
if err == nil {
|
|
t.Error("expected error for missing file")
|
|
}
|
|
}
|
|
|
|
func TestEnvOverrides(t *testing.T) {
|
|
t.Setenv("UOPI_SERVER_LISTEN", ":7777")
|
|
t.Setenv("UOPI_SERVER_STORAGE_DIR", "/env/storage")
|
|
t.Setenv("UOPI_EPICS_CA_ADDR_LIST", "10.0.0.1 10.0.0.2")
|
|
t.Setenv("UOPI_EPICS_ARCHIVE_URL", "http://archive/")
|
|
t.Setenv("UOPI_EPICS_CHANNEL_FINDER_URL", "http://cf/")
|
|
|
|
cfg, err := config.Load("")
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if cfg.Server.Listen != ":7777" {
|
|
t.Errorf("Listen = %q, want :7777", cfg.Server.Listen)
|
|
}
|
|
if cfg.Server.StorageDir != "/env/storage" {
|
|
t.Errorf("StorageDir = %q", cfg.Server.StorageDir)
|
|
}
|
|
if cfg.Datasource.EPICS.CAAddrList != "10.0.0.1 10.0.0.2" {
|
|
t.Errorf("CAAddrList = %q", cfg.Datasource.EPICS.CAAddrList)
|
|
}
|
|
if cfg.Datasource.EPICS.ArchiveURL != "http://archive/" {
|
|
t.Errorf("ArchiveURL = %q", cfg.Datasource.EPICS.ArchiveURL)
|
|
}
|
|
if cfg.Datasource.EPICS.ChannelFinderURL != "http://cf/" {
|
|
t.Errorf("ChannelFinderURL = %q", cfg.Datasource.EPICS.ChannelFinderURL)
|
|
}
|
|
}
|
|
|
|
func TestEnvTrimsWhitespace(t *testing.T) {
|
|
t.Setenv("UOPI_SERVER_LISTEN", " :8888 ")
|
|
cfg, err := config.Load("")
|
|
if err != nil {
|
|
t.Fatalf("Load: %v", err)
|
|
}
|
|
if cfg.Server.Listen != ":8888" {
|
|
t.Errorf("Listen = %q, expected whitespace trimmed", cfg.Server.Listen)
|
|
}
|
|
}
|