1package app
2
3import (
4 "os"
5 "path/filepath"
6 "testing"
7
8 "github.com/stretchr/testify/assert"
9)
10
11func TestResolveRuntimeConfig(t *testing.T) {
12 tests := []struct {
13 name string
14 value string
15 xdgDir string // non-empty means set XDG_RUNTIME_DIR
16 createSock bool // whether to create the socket file under xdgDir
17 want string
18 }{
19 {
20 name: "empty string returns empty",
21 value: "",
22 want: "",
23 },
24 {
25 name: "auto returns empty",
26 value: "auto",
27 want: "",
28 },
29 {
30 name: "podman with XDG_RUNTIME_DIR and socket present",
31 value: "podman",
32 xdgDir: "tmpXDG", // replaced with t.TempDir() in the loop
33 createSock: true,
34 },
35 {
36 name: "podman with XDG_RUNTIME_DIR but socket absent falls back to system socket",
37 value: "podman",
38 xdgDir: "tmpXDG",
39 want: "/run/podman/podman.sock",
40 },
41 {
42 name: "podman without XDG_RUNTIME_DIR falls back to system socket",
43 value: "podman",
44 want: "/run/podman/podman.sock",
45 },
46 {
47 name: "docker returns well-known path",
48 value: "docker",
49 want: "/var/run/docker.sock",
50 },
51 {
52 name: "unix:// prefix is stripped",
53 value: "unix:///custom/path.sock",
54 want: "/custom/path.sock",
55 },
56 {
57 name: "bare path returned as-is",
58 value: "/custom/path.sock",
59 want: "/custom/path.sock",
60 },
61 }
62
63 for _, tt := range tests {
64 t.Run(tt.name, func(t *testing.T) {
65 xdgDir := tt.xdgDir
66 if xdgDir == "tmpXDG" {
67 xdgDir = t.TempDir()
68 }
69
70 if xdgDir != "" {
71 t.Setenv("XDG_RUNTIME_DIR", xdgDir)
72 } else {
73 t.Setenv("XDG_RUNTIME_DIR", "")
74 }
75
76 want := tt.want
77 if tt.createSock && xdgDir != "" {
78 sockDir := filepath.Join(xdgDir, "podman")
79 os.MkdirAll(sockDir, 0o755)
80 os.WriteFile(filepath.Join(sockDir, "podman.sock"), nil, 0o600)
81 want = filepath.Join(xdgDir, "podman", "podman.sock")
82 }
83
84 got := resolveRuntimeConfig(tt.value)
85 assert.Equal(t, want, got)
86 })
87 }
88}