| 1 | |
| 2 | |
| 3 | |
| 4 | package cidata |
| 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "gotest.tools/v3/assert" |
| 13 | |
| 14 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 15 | ) |
| 16 | |
| 17 | var defaultRemoveDefaults = false |
| 18 | |
| 19 | func TestConfig(t *testing.T) { |
| 20 | args := &TemplateArgs{ |
| 21 | Name: "default", |
| 22 | User: "foo", |
| 23 | UID: 501, |
| 24 | Comment: "Foo", |
| 25 | Home: "/home/foo.guest", |
| 26 | Shell: "/bin/bash", |
| 27 | SSHPubKeys: []string{ |
| 28 | "ssh-rsa dummy [email protected]", |
| 29 | }, |
| 30 | MountType: "reverse-sshfs", |
| 31 | } |
| 32 | config, err := ExecuteTemplateCloudConfig(args) |
| 33 | assert.NilError(t, err) |
| 34 | t.Log(string(config)) |
| 35 | assert.Assert(t, !strings.Contains(string(config), "ca_certs:")) |
| 36 | assert.Assert(t, !strings.Contains(string(config), "mounts:")) |
| 37 | } |
| 38 | |
| 39 | func TestConfigCACerts(t *testing.T) { |
| 40 | args := &TemplateArgs{ |
| 41 | Name: "default", |
| 42 | User: "foo", |
| 43 | UID: 501, |
| 44 | Comment: "Foo", |
| 45 | Home: "/home/foo.guest", |
| 46 | Shell: "/bin/bash", |
| 47 | SSHPubKeys: []string{ |
| 48 | "ssh-rsa dummy [email protected]", |
| 49 | }, |
| 50 | MountType: "reverse-sshfs", |
| 51 | CACerts: CACerts{ |
| 52 | RemoveDefaults: &defaultRemoveDefaults, |
| 53 | }, |
| 54 | } |
| 55 | config, err := ExecuteTemplateCloudConfig(args) |
| 56 | assert.NilError(t, err) |
| 57 | t.Log(string(config)) |
| 58 | assert.Assert(t, strings.Contains(string(config), "ca_certs:")) |
| 59 | } |
| 60 | |
| 61 | var defaultMounts = []Mount{ |
| 62 | {MountPoint: "/home/foo.guest", Tag: "mount0", Type: "virtiofs", Options: "ro"}, |
| 63 | {MountPoint: "/tmp/lima", Tag: "mount1", Type: "virtiofs"}, |
| 64 | } |
| 65 | |
| 66 | func TestConfigMounts(t *testing.T) { |
| 67 | args := &TemplateArgs{ |
| 68 | Name: "default", |
| 69 | User: "foo", |
| 70 | UID: 501, |
| 71 | Comment: "Foo", |
| 72 | Home: "/home/foo.guest", |
| 73 | Shell: "/bin/bash", |
| 74 | SSHPubKeys: []string{ |
| 75 | "ssh-rsa dummy [email protected]", |
| 76 | }, |
| 77 | MountType: "virtiofs", |
| 78 | Mounts: defaultMounts, |
| 79 | } |
| 80 | config, err := ExecuteTemplateCloudConfig(args) |
| 81 | assert.NilError(t, err) |
| 82 | t.Log(string(config)) |
| 83 | assert.Assert(t, strings.Contains(string(config), "mounts:")) |
| 84 | } |
| 85 | |
| 86 | func TestConfigMountsNone(t *testing.T) { |
| 87 | args := &TemplateArgs{ |
| 88 | Name: "default", |
| 89 | User: "foo", |
| 90 | UID: 501, |
| 91 | Comment: "Foo", |
| 92 | Home: "/home/foo.guest", |
| 93 | Shell: "/bin/bash", |
| 94 | SSHPubKeys: []string{ |
| 95 | "ssh-rsa dummy [email protected]", |
| 96 | }, |
| 97 | MountType: "virtiofs", |
| 98 | Mounts: []Mount{}, |
| 99 | } |
| 100 | config, err := ExecuteTemplateCloudConfig(args) |
| 101 | assert.NilError(t, err) |
| 102 | t.Log(string(config)) |
| 103 | assert.Assert(t, !strings.Contains(string(config), "mounts:")) |
| 104 | } |
| 105 | |
| 106 | func TestTemplate(t *testing.T) { |
| 107 | args := &TemplateArgs{ |
| 108 | Name: "default", |
| 109 | User: "foo", |
| 110 | UID: 501, |
| 111 | Home: "/home/foo.guest", |
| 112 | Shell: "/bin/bash", |
| 113 | SSHPubKeys: []string{ |
| 114 | "ssh-rsa dummy [email protected]", |
| 115 | }, |
| 116 | Mounts: []Mount{ |
| 117 | {MountPoint: "/Users/dummy"}, |
| 118 | {MountPoint: "/Users/dummy/lima"}, |
| 119 | }, |
| 120 | MountType: "reverse-sshfs", |
| 121 | CACerts: CACerts{ |
| 122 | RemoveDefaults: &defaultRemoveDefaults, |
| 123 | Trusted: []Cert{}, |
| 124 | }, |
| 125 | } |
| 126 | layout, err := ExecuteTemplateCIDataISO(args) |
| 127 | assert.NilError(t, err) |
| 128 | for _, f := range layout { |
| 129 | t.Logf("=== %#q ===", f.Path) |
| 130 | b, err := io.ReadAll(f.Reader) |
| 131 | assert.NilError(t, err) |
| 132 | t.Log(string(b)) |
| 133 | if f.Path == "user-data" { |
| 134 | |
| 135 | assert.Assert(t, !strings.Contains(string(b), "mounts:")) |
| 136 | |
| 137 | assert.Assert(t, !strings.Contains(string(b), "trusted:")) |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | func TestTemplate9p(t *testing.T) { |
| 143 | args := &TemplateArgs{ |
| 144 | Name: "default", |
| 145 | User: "foo", |
| 146 | UID: 501, |
| 147 | Home: "/home/foo.guest", |
| 148 | Shell: "/bin/bash", |
| 149 | SSHPubKeys: []string{ |
| 150 | "ssh-rsa dummy [email protected]", |
| 151 | }, |
| 152 | Mounts: []Mount{ |
| 153 | {Tag: "mount0", MountPoint: "/Users/dummy", Type: "9p", Options: "ro,trans=virtio"}, |
| 154 | {Tag: "mount1", MountPoint: "/Users/dummy/lima", Type: "9p", Options: "rw,trans=virtio"}, |
| 155 | }, |
| 156 | MountType: "9p", |
| 157 | CACerts: CACerts{ |
| 158 | RemoveDefaults: &defaultRemoveDefaults, |
| 159 | }, |
| 160 | } |
| 161 | layout, err := ExecuteTemplateCIDataISO(args) |
| 162 | assert.NilError(t, err) |
| 163 | for _, f := range layout { |
| 164 | t.Logf("=== %#q ===", f.Path) |
| 165 | b, err := io.ReadAll(f.Reader) |
| 166 | assert.NilError(t, err) |
| 167 | t.Log(string(b)) |
| 168 | if f.Path == "user-data" { |
| 169 | |
| 170 | assert.Assert(t, strings.Contains(string(b), "mounts:")) |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | func TestTemplateNICRename(t *testing.T) { |
| 182 | args := &TemplateArgs{ |
| 183 | Name: "default", |
| 184 | User: "foo", |
| 185 | UID: 501, |
| 186 | Home: "/home/foo.guest", |
| 187 | Shell: "/bin/bash", |
| 188 | SSHPubKeys: []string{"ssh-rsa dummy [email protected]"}, |
| 189 | MountType: "reverse-sshfs", |
| 190 | OS: "Linux", |
| 191 | SlirpNICName: "eth0", |
| 192 | Networks: []Network{ |
| 193 | {MACAddress: "52:55:55:12:34:56", Interface: "eth0", Metric: 200}, |
| 194 | {MACAddress: "52:55:55:ab:cd:ef", Interface: "lima0", Metric: 300}, |
| 195 | }, |
| 196 | } |
| 197 | for _, optional := range []bool{false, true} { |
| 198 | if optional { |
| 199 | args.Param = map[string]string{"internal_netplanOptional": "true"} |
| 200 | } |
| 201 | layout, err := ExecuteTemplateCIDataISO(args) |
| 202 | assert.NilError(t, err) |
| 203 | files := make(map[string]string) |
| 204 | for _, f := range layout { |
| 205 | b, err := io.ReadAll(f.Reader) |
| 206 | assert.NilError(t, err) |
| 207 | files[f.Path] = string(b) |
| 208 | } |
| 209 | assert.Assert(t, strings.Contains(files["user-data"], "52:55:55:12:34:56=eth0")) |
| 210 | assert.Assert(t, strings.Contains(files["user-data"], "52:55:55:ab:cd:ef=lima0")) |
| 211 | assert.Assert(t, strings.Contains(files["network-config"], "set-name: eth0")) |
| 212 | assert.Equal(t, strings.Contains(files["network-config"], "optional: true"), optional) |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | func TestExecuteTemplateWindowsISO(t *testing.T) { |
| 217 | testCases := []struct { |
| 218 | name string |
| 219 | args *TemplateArgs |
| 220 | expectedAutounattendStrings []string |
| 221 | expectedFirstLogonStrings []string |
| 222 | }{ |
| 223 | { |
| 224 | name: "windows server 2025", |
| 225 | args: &TemplateArgs{ |
| 226 | Name: "windows", |
| 227 | UID: 501, |
| 228 | Home: "/home/foo.guest", |
| 229 | Shell: "powershell.exe", |
| 230 | SSHPubKeys: []string{"ssh-rsa dummy [email protected]"}, |
| 231 | Arch: limatype.X8664, |
| 232 | User: "windows-user", |
| 233 | WindowsInitialPassword: "dummy-password", |
| 234 | TPM: true, |
| 235 | IsWindowsServer: true, |
| 236 | }, |
| 237 | expectedAutounattendStrings: []string{ |
| 238 | `<Path>E:\viostor\2k25\amd64</Path>`, |
| 239 | `<Username>windows-user</Username>`, |
| 240 | `<Value>dummy-password</Value>`, |
| 241 | `<Type>EFI</Type>`, |
| 242 | `<Value>1</Value>`, |
| 243 | }, |
| 244 | expectedFirstLogonStrings: []string{ |
| 245 | `$logfile = "C:\Users\windows-user\lima-setup.log"`, |
| 246 | }, |
| 247 | }, |
| 248 | { |
| 249 | name: "windows 11 x86_64", |
| 250 | args: &TemplateArgs{ |
| 251 | Name: "windows", |
| 252 | UID: 501, |
| 253 | Home: "/home/foo.guest", |
| 254 | Shell: "powershell.exe", |
| 255 | SSHPubKeys: []string{"ssh-rsa dummy [email protected]"}, |
| 256 | Arch: limatype.X8664, |
| 257 | User: "windows-user", |
| 258 | WindowsInitialPassword: "dummy-password", |
| 259 | TPM: true, |
| 260 | }, |
| 261 | expectedAutounattendStrings: []string{ |
| 262 | `<Path>E:\viostor\w11\amd64</Path>`, |
| 263 | `<Username>windows-user</Username>`, |
| 264 | `<Value>dummy-password</Value>`, |
| 265 | `<Type>EFI</Type>`, |
| 266 | `<Value>6</Value>`, |
| 267 | }, |
| 268 | expectedFirstLogonStrings: []string{ |
| 269 | `$logfile = "C:\Users\windows-user\lima-setup.log"`, |
| 270 | }, |
| 271 | }, |
| 272 | { |
| 273 | name: "legacyBIOS", |
| 274 | args: &TemplateArgs{ |
| 275 | Name: "windows", |
| 276 | UID: 501, |
| 277 | Home: "/home/foo.guest", |
| 278 | Shell: "powershell.exe", |
| 279 | SSHPubKeys: []string{"ssh-rsa dummy [email protected]"}, |
| 280 | Arch: limatype.X8664, |
| 281 | User: "windows-user", |
| 282 | WindowsInitialPassword: "dummy-password", |
| 283 | LegacyBIOS: true, |
| 284 | TPM: true, |
| 285 | IsWindowsServer: true, |
| 286 | }, |
| 287 | expectedAutounattendStrings: []string{ |
| 288 | `<Path>E:\viostor\2k25\amd64</Path>`, |
| 289 | `<Username>windows-user</Username>`, |
| 290 | `<Value>dummy-password</Value>`, |
| 291 | `<Label>BIOS</Label>`, |
| 292 | `<Value>1</Value>`, |
| 293 | }, |
| 294 | expectedFirstLogonStrings: []string{ |
| 295 | `$logfile = "C:\Users\windows-user\lima-setup.log"`, |
| 296 | }, |
| 297 | }, |
| 298 | { |
| 299 | name: "disable TPM on Windows 11", |
| 300 | args: &TemplateArgs{ |
| 301 | Name: "windows", |
| 302 | UID: 501, |
| 303 | Home: "/home/foo.guest", |
| 304 | Shell: "powershell.exe", |
| 305 | SSHPubKeys: []string{"ssh-rsa dummy [email protected]"}, |
| 306 | Arch: limatype.X8664, |
| 307 | User: "windows-user", |
| 308 | WindowsInitialPassword: "dummy-password", |
| 309 | TPM: false, |
| 310 | }, |
| 311 | expectedAutounattendStrings: []string{ |
| 312 | `<Path>E:\viostor\w11\amd64</Path>`, |
| 313 | `<Username>windows-user</Username>`, |
| 314 | `<Value>dummy-password</Value>`, |
| 315 | `<Type>EFI</Type>`, |
| 316 | `BypassTPMCheck`, |
| 317 | `<Value>6</Value>`, |
| 318 | }, |
| 319 | expectedFirstLogonStrings: []string{ |
| 320 | `$logfile = "C:\Users\windows-user\lima-setup.log"`, |
| 321 | }, |
| 322 | }, |
| 323 | { |
| 324 | name: "Windows 11 arm64", |
| 325 | args: &TemplateArgs{ |
| 326 | Name: "windows", |
| 327 | UID: 501, |
| 328 | Home: "/home/foo.guest", |
| 329 | Shell: "powershell.exe", |
| 330 | SSHPubKeys: []string{"ssh-rsa dummy [email protected]"}, |
| 331 | Arch: limatype.AARCH64, |
| 332 | User: "windows-user", |
| 333 | WindowsInitialPassword: "dummy-password", |
| 334 | TPM: false, |
| 335 | }, |
| 336 | expectedAutounattendStrings: []string{ |
| 337 | `<Path>E:\viostor\w11\arm64</Path>`, |
| 338 | `<Username>windows-user</Username>`, |
| 339 | `<Value>dummy-password</Value>`, |
| 340 | `<Type>EFI</Type>`, |
| 341 | `BypassTPMCheck`, |
| 342 | `<Value>3</Value>`, |
| 343 | }, |
| 344 | expectedFirstLogonStrings: []string{ |
| 345 | `$logfile = "C:\Users\windows-user\lima-setup.log"`, |
| 346 | }, |
| 347 | }, |
| 348 | } |
| 349 | |
| 350 | for _, tt := range testCases { |
| 351 | t.Run(tt.name, func(t *testing.T) { |
| 352 | layout, err := ExecuteTemplateWindowsISO(tt.args) |
| 353 | assert.NilError(t, err) |
| 354 | for _, f := range layout { |
| 355 | b, err := io.ReadAll(f.Reader) |
| 356 | s := string(b) |
| 357 | assert.NilError(t, err) |
| 358 | switch f.Path { |
| 359 | case "autounattend.xml": |
| 360 | for _, expected := range tt.expectedAutounattendStrings { |
| 361 | assert.Assert(t, strings.Contains(s, expected), fmt.Sprintf("expected: %s", expected)) |
| 362 | } |
| 363 | case "first_logon.ps1": |
| 364 | for _, expected := range tt.expectedFirstLogonStrings { |
| 365 | assert.Assert(t, strings.Contains(s, expected), fmt.Sprintf("expected: %s", expected)) |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | }) |
| 370 | } |
| 371 | } |