- syncup: podman first, docker only as fallback (podman wins on name clash;
existing docker units found in podman switch to podman)
- syncup: group podman-compose containers into one `kind: stack` unit per
project (project, working_dir, compose_files, compose_bin from labels);
disable existing member units ("part of stack <project>")
- generator: compose mode (podman-compose up/stop, WorkingDirectory) with
pod start/stop fallback when compose binary/dir/files are missing
- reconcile: stacks disabled with "stack not found" when neither compose
files nor pod exist
- root: scan root + rootless users listed in `users:` (podman only)
- non-root: config ~/.config/unitdore/units.yaml, own user units only
- sysuser package: runuser with XDG_RUNTIME_DIR/DBUS env, home/linger lookup
- install: warn when linger is not enabled for user units
- testdata/stack: podman-compose test stack and captured podman ps fixture
- docs: README, generated-units, man page, AGENTS, PLAN
186 lines
4.6 KiB
Go
186 lines
4.6 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)
|
|
}
|
|
}
|
|
|
|
func TestSaveAndLoad_Stack(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "units.yaml")
|
|
cfg := &Config{
|
|
Users: []string{"hein"},
|
|
Units: []Unit{{
|
|
Name: "shop", Runtime: "podman", Kind: KindStack, Project: "shop",
|
|
WorkingDir: "/srv/shop", ComposeFiles: []string{"compose.yaml"},
|
|
ComposeBin: "/usr/bin/podman-compose", User: "hein", 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)
|
|
}
|
|
u := loaded.Units[0]
|
|
if !u.IsStack() || u.Project != "shop" || u.WorkingDir != "/srv/shop" ||
|
|
len(u.ComposeFiles) != 1 || u.ComposeBin != "/usr/bin/podman-compose" {
|
|
t.Errorf("stack fields not round-tripped: %+v", u)
|
|
}
|
|
if len(loaded.Users) != 1 || loaded.Users[0] != "hein" {
|
|
t.Errorf("users not round-tripped: %v", loaded.Users)
|
|
}
|
|
}
|
|
|
|
func TestIsStack_DefaultKind(t *testing.T) {
|
|
if (Unit{Name: "x"}).IsStack() {
|
|
t.Error("unit without kind must be a container")
|
|
}
|
|
}
|
|
|
|
func TestUserConfigPath(t *testing.T) {
|
|
if got := UserConfigPath("/home/hein"); got != "/home/hein/.config/unitdore/units.yaml" {
|
|
t.Errorf("UserConfigPath() = %q", got)
|
|
}
|
|
}
|