| 1 | |
| 2 | |
| 3 | |
| 4 | package autostart |
| 5 | |
| 6 | import ( |
| 7 | "runtime" |
| 8 | "testing" |
| 9 | |
| 10 | "gotest.tools/v3/assert" |
| 11 | |
| 12 | "github.com/lima-vm/lima/v2/pkg/autostart/launchd" |
| 13 | "github.com/lima-vm/lima/v2/pkg/autostart/systemd" |
| 14 | ) |
| 15 | |
| 16 | var ( |
| 17 | Launchd = &TemplateFileBasedManager{ |
| 18 | filePath: launchd.GetPlistPath, |
| 19 | template: launchd.Template, |
| 20 | enabler: launchd.EnableDisableService, |
| 21 | autoStartedIdentifier: launchd.AutoStartedServiceName, |
| 22 | requestStart: launchd.RequestStart, |
| 23 | requestStop: launchd.RequestStop, |
| 24 | } |
| 25 | LaunchdKeepAlive = &TemplateFileBasedManager{ |
| 26 | filePath: launchd.GetPlistPath, |
| 27 | template: launchd.Template, |
| 28 | enabler: launchd.EnableDisableService, |
| 29 | autoStartedIdentifier: launchd.AutoStartedServiceName, |
| 30 | requestStart: launchd.RequestStart, |
| 31 | requestStop: launchd.RequestStop, |
| 32 | extraTemplateVars: map[string]string{"KeepAlive": "true"}, |
| 33 | } |
| 34 | LaunchdDaemon = &TemplateFileBasedManager{ |
| 35 | filePath: launchd.GetDaemonPlistPath, |
| 36 | template: launchd.DaemonTemplate, |
| 37 | extraTemplateVars: map[string]string{"UserName": "alice"}, |
| 38 | } |
| 39 | LaunchdDaemonKeepAlive = &TemplateFileBasedManager{ |
| 40 | filePath: launchd.GetDaemonPlistPath, |
| 41 | template: launchd.DaemonTemplate, |
| 42 | extraTemplateVars: map[string]string{"UserName": "alice", "KeepAlive": "true"}, |
| 43 | } |
| 44 | Systemd = &TemplateFileBasedManager{ |
| 45 | filePath: systemd.GetUnitPath, |
| 46 | template: systemd.Template, |
| 47 | enabler: systemd.EnableDisableUnit, |
| 48 | autoStartedIdentifier: systemd.AutoStartedUnitName, |
| 49 | requestStart: systemd.RequestStart, |
| 50 | requestStop: systemd.RequestStop, |
| 51 | extraTemplateVars: map[string]string{"Restart": "on-failure"}, |
| 52 | } |
| 53 | SystemdNoKeepAlive = &TemplateFileBasedManager{ |
| 54 | filePath: systemd.GetUnitPath, |
| 55 | template: systemd.Template, |
| 56 | enabler: systemd.EnableDisableUnit, |
| 57 | autoStartedIdentifier: systemd.AutoStartedUnitName, |
| 58 | requestStart: systemd.RequestStart, |
| 59 | requestStop: systemd.RequestStop, |
| 60 | extraTemplateVars: map[string]string{"Restart": "no"}, |
| 61 | } |
| 62 | ) |
| 63 | |
| 64 | func TestRenderTemplate(t *testing.T) { |
| 65 | if runtime.GOOS == "windows" { |
| 66 | t.Skip("skipping testing on windows host") |
| 67 | } |
| 68 | tests := []struct { |
| 69 | Manager *TemplateFileBasedManager |
| 70 | Name string |
| 71 | InstanceName string |
| 72 | Expected string |
| 73 | WorkDir string |
| 74 | GetExecutable func() (string, error) |
| 75 | }{ |
| 76 | { |
| 77 | Manager: Launchd, |
| 78 | Name: "render darwin launchd plist", |
| 79 | InstanceName: "default", |
| 80 | Expected: `<?xml version="1.0" encoding="UTF-8"?> |
| 81 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 82 | <plist version="1.0"> |
| 83 | <dict> |
| 84 | <key>Label</key> |
| 85 | <string>io.lima-vm.autostart.default</string> |
| 86 | <key>EnvironmentVariables</key> |
| 87 | <dict> |
| 88 | <key>LIMA_HOME</key> |
| 89 | <string>/some/lima/home</string> |
| 90 | </dict> |
| 91 | <key>ProgramArguments</key> |
| 92 | <array> |
| 93 | <string>/limactl</string> |
| 94 | <string>start</string> |
| 95 | <string>default</string> |
| 96 | <string>--foreground</string> |
| 97 | </array> |
| 98 | <key>RunAtLoad</key> |
| 99 | <true/> |
| 100 | <key>StandardErrorPath</key> |
| 101 | <string>launchd.stderr.log</string> |
| 102 | <key>StandardOutPath</key> |
| 103 | <string>launchd.stdout.log</string> |
| 104 | <key>WorkingDirectory</key> |
| 105 | <string>/some/path</string> |
| 106 | <key>ProcessType</key> |
| 107 | <string>Background</string> |
| 108 | </dict> |
| 109 | </plist> |
| 110 | `, |
| 111 | GetExecutable: func() (string, error) { |
| 112 | return "/limactl", nil |
| 113 | }, |
| 114 | WorkDir: "/some/path", |
| 115 | }, |
| 116 | { |
| 117 | Manager: LaunchdDaemon, |
| 118 | Name: "render darwin launchd daemon plist", |
| 119 | InstanceName: "k3s", |
| 120 | Expected: `<?xml version="1.0" encoding="UTF-8"?> |
| 121 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 122 | <plist version="1.0"> |
| 123 | <dict> |
| 124 | <key>Label</key> |
| 125 | <string>io.lima-vm.daemon.k3s</string> |
| 126 | <key>UserName</key> |
| 127 | <string>alice</string> |
| 128 | <key>EnvironmentVariables</key> |
| 129 | <dict> |
| 130 | <key>LIMA_HOME</key> |
| 131 | <string>/some/lima/home</string> |
| 132 | </dict> |
| 133 | <key>ProgramArguments</key> |
| 134 | <array> |
| 135 | <string>/limactl</string> |
| 136 | <string>start</string> |
| 137 | <string>k3s</string> |
| 138 | <string>--foreground</string> |
| 139 | </array> |
| 140 | <key>RunAtLoad</key> |
| 141 | <true/> |
| 142 | <key>StandardErrorPath</key> |
| 143 | <string>launchd.stderr.log</string> |
| 144 | <key>StandardOutPath</key> |
| 145 | <string>launchd.stdout.log</string> |
| 146 | <key>WorkingDirectory</key> |
| 147 | <string>/some/path</string> |
| 148 | <key>ProcessType</key> |
| 149 | <string>Background</string> |
| 150 | </dict> |
| 151 | </plist> |
| 152 | `, |
| 153 | GetExecutable: func() (string, error) { |
| 154 | return "/limactl", nil |
| 155 | }, |
| 156 | WorkDir: "/some/path", |
| 157 | }, |
| 158 | { |
| 159 | Manager: LaunchdKeepAlive, |
| 160 | Name: "render darwin launchd plist with keep-alive enabled", |
| 161 | InstanceName: "default", |
| 162 | Expected: `<?xml version="1.0" encoding="UTF-8"?> |
| 163 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 164 | <plist version="1.0"> |
| 165 | <dict> |
| 166 | <key>Label</key> |
| 167 | <string>io.lima-vm.autostart.default</string> |
| 168 | <key>EnvironmentVariables</key> |
| 169 | <dict> |
| 170 | <key>LIMA_HOME</key> |
| 171 | <string>/some/lima/home</string> |
| 172 | </dict> |
| 173 | <key>ProgramArguments</key> |
| 174 | <array> |
| 175 | <string>/limactl</string> |
| 176 | <string>start</string> |
| 177 | <string>default</string> |
| 178 | <string>--foreground</string> |
| 179 | </array> |
| 180 | <key>RunAtLoad</key> |
| 181 | <true/> |
| 182 | <key>KeepAlive</key> |
| 183 | <dict> |
| 184 | <key>SuccessfulExit</key> |
| 185 | <false/> |
| 186 | </dict> |
| 187 | <key>StandardErrorPath</key> |
| 188 | <string>launchd.stderr.log</string> |
| 189 | <key>StandardOutPath</key> |
| 190 | <string>launchd.stdout.log</string> |
| 191 | <key>WorkingDirectory</key> |
| 192 | <string>/some/path</string> |
| 193 | <key>ProcessType</key> |
| 194 | <string>Background</string> |
| 195 | </dict> |
| 196 | </plist> |
| 197 | `, |
| 198 | GetExecutable: func() (string, error) { |
| 199 | return "/limactl", nil |
| 200 | }, |
| 201 | WorkDir: "/some/path", |
| 202 | }, |
| 203 | { |
| 204 | Manager: LaunchdDaemonKeepAlive, |
| 205 | Name: "render darwin launchd daemon plist with keep-alive enabled", |
| 206 | InstanceName: "k3s", |
| 207 | Expected: `<?xml version="1.0" encoding="UTF-8"?> |
| 208 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 209 | <plist version="1.0"> |
| 210 | <dict> |
| 211 | <key>Label</key> |
| 212 | <string>io.lima-vm.daemon.k3s</string> |
| 213 | <key>UserName</key> |
| 214 | <string>alice</string> |
| 215 | <key>EnvironmentVariables</key> |
| 216 | <dict> |
| 217 | <key>LIMA_HOME</key> |
| 218 | <string>/some/lima/home</string> |
| 219 | </dict> |
| 220 | <key>ProgramArguments</key> |
| 221 | <array> |
| 222 | <string>/limactl</string> |
| 223 | <string>start</string> |
| 224 | <string>k3s</string> |
| 225 | <string>--foreground</string> |
| 226 | </array> |
| 227 | <key>RunAtLoad</key> |
| 228 | <true/> |
| 229 | <key>KeepAlive</key> |
| 230 | <dict> |
| 231 | <key>SuccessfulExit</key> |
| 232 | <false/> |
| 233 | </dict> |
| 234 | <key>StandardErrorPath</key> |
| 235 | <string>launchd.stderr.log</string> |
| 236 | <key>StandardOutPath</key> |
| 237 | <string>launchd.stdout.log</string> |
| 238 | <key>WorkingDirectory</key> |
| 239 | <string>/some/path</string> |
| 240 | <key>ProcessType</key> |
| 241 | <string>Background</string> |
| 242 | </dict> |
| 243 | </plist> |
| 244 | `, |
| 245 | GetExecutable: func() (string, error) { |
| 246 | return "/limactl", nil |
| 247 | }, |
| 248 | WorkDir: "/some/path", |
| 249 | }, |
| 250 | { |
| 251 | Manager: Systemd, |
| 252 | Name: "render linux systemd service", |
| 253 | InstanceName: "default", |
| 254 | Expected: `[Unit] |
| 255 | Description=Lima - Linux virtual machines, with a focus on running containers. |
| 256 | Documentation=man:lima(1) |
| 257 | |
| 258 | [Service] |
| 259 | Environment=LIMA_HOME=/some/lima/home |
| 260 | ExecStart=/limactl start %i --foreground |
| 261 | WorkingDirectory=%h |
| 262 | Type=simple |
| 263 | TimeoutSec=10 |
| 264 | Restart=on-failure |
| 265 | |
| 266 | [Install] |
| 267 | WantedBy=default.target |
| 268 | `, |
| 269 | GetExecutable: func() (string, error) { |
| 270 | return "/limactl", nil |
| 271 | }, |
| 272 | WorkDir: "/some/path", |
| 273 | }, |
| 274 | { |
| 275 | Manager: SystemdNoKeepAlive, |
| 276 | Name: "render linux systemd service with keep-alive disabled", |
| 277 | InstanceName: "default", |
| 278 | Expected: `[Unit] |
| 279 | Description=Lima - Linux virtual machines, with a focus on running containers. |
| 280 | Documentation=man:lima(1) |
| 281 | |
| 282 | [Service] |
| 283 | Environment=LIMA_HOME=/some/lima/home |
| 284 | ExecStart=/limactl start %i --foreground |
| 285 | WorkingDirectory=%h |
| 286 | Type=simple |
| 287 | TimeoutSec=10 |
| 288 | Restart=no |
| 289 | |
| 290 | [Install] |
| 291 | WantedBy=default.target |
| 292 | `, |
| 293 | GetExecutable: func() (string, error) { |
| 294 | return "/limactl", nil |
| 295 | }, |
| 296 | WorkDir: "/some/path", |
| 297 | }, |
| 298 | } |
| 299 | for _, tt := range tests { |
| 300 | t.Run(tt.Name, func(t *testing.T) { |
| 301 | |
| 302 | |
| 303 | t.Setenv("LIMA_HOME", "/some/lima/home") |
| 304 | tmpl, err := tt.Manager.renderTemplate(tt.InstanceName, tt.WorkDir, tt.GetExecutable) |
| 305 | assert.NilError(t, err) |
| 306 | assert.Equal(t, string(tmpl), tt.Expected) |
| 307 | }) |
| 308 | } |
| 309 | } |