Files
unitdore/config/config_test.go
sgcommand 5078558d01 Phase 5-7: README, Makefile, unit tests
- README.md: full usage docs
- Makefile: build/install/uninstall/test/lint/clean targets
- config/config_test.go: 7 tests (load, save, find, add, roundtrip, invalid yaml)
- runtime/runtime_test.go: 6 tests (get, available, ListRunning graceful degradation)
- systemd/generator_test.go: 7 tests (ServiceName, Generate system/user/custom/docker/unknown, buildExecCommands)
- fix: docker/podman ListRunning returns nil (not error) when daemon unavailable
- fix: invalid YAML test uses tab-indent which is genuinely unparseable
2026-04-03 14:50:36 +02:00

147 lines
3.4 KiB
Go

package config
import (
"os"
"path/filepath"
"testing"
)
func TestLoadOrEmpty_NoFile(t *testing.T) {
cfg, err := LoadOrEmpty("/tmp/unitdore-nonexistent-test.yaml")
if err != nil {
t.Fatalf("expected no error for missing file, got: %v", err)
}
if len(cfg.Units) != 0 {
t.Errorf("expected empty units, got %d", len(cfg.Units))
}
}
func TestSaveAndLoad(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "units.yaml")
cfg := &Config{
Units: []Unit{
{Name: "nginx", Runtime: "podman", Order: 1, Enabled: true},
{Name: "myapp", Runtime: "docker", User: "hein", Order: 2, Enabled: true},
},
}
if err := cfg.Save(path); err != nil {
t.Fatalf("Save failed: %v", err)
}
loaded, err := Load(path)
if err != nil {
t.Fatalf("Load failed: %v", err)
}
if len(loaded.Units) != 2 {
t.Errorf("expected 2 units, got %d", len(loaded.Units))
}
if loaded.Units[0].Name != "nginx" {
t.Errorf("expected nginx, got %s", loaded.Units[0].Name)
}
if loaded.Units[1].User != "hein" {
t.Errorf("expected user hein, got %s", loaded.Units[1].User)
}
}
func TestFindUnit(t *testing.T) {
cfg := &Config{
Units: []Unit{
{Name: "nginx", Runtime: "podman"},
{Name: "myapp", Runtime: "docker"},
},
}
u := cfg.FindUnit("nginx")
if u == nil {
t.Fatal("expected to find nginx, got nil")
}
if u.Runtime != "podman" {
t.Errorf("expected podman, got %s", u.Runtime)
}
missing := cfg.FindUnit("nothere")
if missing != nil {
t.Errorf("expected nil for missing unit, got %+v", missing)
}
}
func TestAddUnit(t *testing.T) {
cfg := &Config{}
added := cfg.AddUnit(Unit{Name: "nginx", Runtime: "podman", Enabled: true})
if !added {
t.Error("expected AddUnit to return true for new unit")
}
if len(cfg.Units) != 1 {
t.Errorf("expected 1 unit, got %d", len(cfg.Units))
}
// Adding a duplicate should return false and not grow the list
added = cfg.AddUnit(Unit{Name: "nginx", Runtime: "docker"})
if added {
t.Error("expected AddUnit to return false for duplicate")
}
if len(cfg.Units) != 1 {
t.Errorf("expected still 1 unit, got %d", len(cfg.Units))
}
}
func TestLoad_InvalidYAML(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "bad.yaml")
// Use a tab character in a mapping key — this is genuinely invalid YAML
os.WriteFile(path, []byte("units:\n - name: ok\n\t broken: tab-indent"), 0644)
_, err := Load(path)
if err == nil {
t.Error("expected error for invalid YAML, got nil")
}
}
func TestSave_CreatesParentDir(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "subdir", "nested", "units.yaml")
cfg := &Config{Units: []Unit{{Name: "test", Runtime: "podman", Enabled: true}}}
if err := cfg.Save(path); err != nil {
t.Fatalf("Save failed to create parent dirs: %v", err)
}
if _, err := os.Stat(path); err != nil {
t.Errorf("expected file to exist: %v", err)
}
}
func TestDisabledReason_Roundtrip(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "units.yaml")
cfg := &Config{
Units: []Unit{
{
Name: "ghost",
Runtime: "podman",
Enabled: false,
DisabledReason: "container not found",
},
},
}
if err := cfg.Save(path); err != nil {
t.Fatalf("Save failed: %v", err)
}
loaded, err := Load(path)
if err != nil {
t.Fatalf("Load failed: %v", err)
}
if loaded.Units[0].DisabledReason != "container not found" {
t.Errorf("expected disabled_reason to survive roundtrip, got: %q", loaded.Units[0].DisabledReason)
}
}