| 1 | |
| 2 | |
| 3 | |
| 4 | package hcs |
| 5 | |
| 6 | import ( |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "reflect" |
| 10 | "runtime" |
| 11 | "testing" |
| 12 | |
| 13 | "github.com/lima-vm/go-qcow2reader/image/vhdx" |
| 14 | "gotest.tools/v3/assert" |
| 15 | |
| 16 | "github.com/lima-vm/lima/v2/pkg/limatype" |
| 17 | "github.com/lima-vm/lima/v2/pkg/limatype/filenames" |
| 18 | ) |
| 19 | |
| 20 | func newTestInstance(t *testing.T, cfg *limatype.LimaYAML) *limatype.Instance { |
| 21 | t.Helper() |
| 22 | return &limatype.Instance{ |
| 23 | Name: "hcs", |
| 24 | Dir: t.TempDir(), |
| 25 | SSHLocalPort: 60022, |
| 26 | Config: cfg, |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func TestValidateConfig(t *testing.T) { |
| 31 | arch := limatype.NewArch(runtime.GOARCH) |
| 32 | |
| 33 | tests := []struct { |
| 34 | name string |
| 35 | cfg *limatype.LimaYAML |
| 36 | expectedErr string |
| 37 | }{ |
| 38 | { |
| 39 | name: "nil config is rejected", |
| 40 | cfg: nil, |
| 41 | expectedErr: "configuration is nil", |
| 42 | }, |
| 43 | { |
| 44 | name: "minimal config is accepted", |
| 45 | cfg: &limatype.LimaYAML{ |
| 46 | VMType: new(limatype.HCS), |
| 47 | Arch: &arch, |
| 48 | }, |
| 49 | }, |
| 50 | { |
| 51 | name: "Linux guest is accepted", |
| 52 | cfg: &limatype.LimaYAML{ |
| 53 | VMType: new(limatype.HCS), |
| 54 | Arch: &arch, |
| 55 | OS: new(limatype.LINUX), |
| 56 | }, |
| 57 | }, |
| 58 | { |
| 59 | name: "Windows guest is rejected", |
| 60 | cfg: &limatype.LimaYAML{ |
| 61 | VMType: new(limatype.HCS), |
| 62 | Arch: &arch, |
| 63 | OS: new(limatype.WINDOWS), |
| 64 | }, |
| 65 | expectedErr: "currently Windows guest OS is only supported on QEMU", |
| 66 | }, |
| 67 | { |
| 68 | name: "emulation of a non-native arch is rejected", |
| 69 | cfg: &limatype.LimaYAML{ |
| 70 | VMType: new(limatype.HCS), |
| 71 | Arch: new(limatype.Arch("unknown")), |
| 72 | }, |
| 73 | expectedErr: "unsupported arch", |
| 74 | }, |
| 75 | { |
| 76 | name: "tpm is rejected", |
| 77 | cfg: &limatype.LimaYAML{ |
| 78 | VMType: new(limatype.HCS), |
| 79 | Arch: &arch, |
| 80 | TPM: new(true), |
| 81 | }, |
| 82 | expectedErr: "field `tpm` is not supported on HCS driver", |
| 83 | }, |
| 84 | { |
| 85 | name: "tpm=false is accepted", |
| 86 | cfg: &limatype.LimaYAML{ |
| 87 | VMType: new(limatype.HCS), |
| 88 | Arch: &arch, |
| 89 | TPM: new(false), |
| 90 | }, |
| 91 | }, |
| 92 | { |
| 93 | name: "unsupported fields are only warned about, not rejected", |
| 94 | cfg: &limatype.LimaYAML{ |
| 95 | VMType: new(limatype.HCS), |
| 96 | Arch: &arch, |
| 97 | Audio: limatype.Audio{Device: new("none")}, |
| 98 | Mounts: []limatype.Mount{{Location: "~"}}, |
| 99 | }, |
| 100 | }, |
| 101 | } |
| 102 | |
| 103 | for _, tt := range tests { |
| 104 | t.Run(tt.name, func(t *testing.T) { |
| 105 | err := validateConfig(t.Context(), tt.cfg) |
| 106 | if tt.expectedErr == "" { |
| 107 | assert.NilError(t, err) |
| 108 | } else { |
| 109 | assert.ErrorContains(t, err, tt.expectedErr) |
| 110 | } |
| 111 | }) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestKnownYamlProperties(t *testing.T) { |
| 116 | yamlType := reflect.TypeFor[limatype.LimaYAML]() |
| 117 | for _, name := range knownYamlProperties { |
| 118 | t.Run(name, func(t *testing.T) { |
| 119 | _, ok := yamlType.FieldByName(name) |
| 120 | assert.Assert(t, ok, "limatype.LimaYAML has no field %#q", name) |
| 121 | }) |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func TestConfigure(t *testing.T) { |
| 126 | arch := limatype.NewArch(runtime.GOARCH) |
| 127 | |
| 128 | t.Run("fills in the HCS defaults", func(t *testing.T) { |
| 129 | inst := newTestInstance(t, &limatype.LimaYAML{Arch: &arch}) |
| 130 | inst.SSHAddress = "192.0.2.10" |
| 131 | |
| 132 | l := New() |
| 133 | configured, err := l.Configure(t.Context(), inst) |
| 134 | assert.NilError(t, err) |
| 135 | assert.Assert(t, configured != nil) |
| 136 | |
| 137 | assert.Equal(t, *inst.Config.VMType, limatype.HCS) |
| 138 | assert.Equal(t, *inst.Config.MountType, limatype.REVSSHFS) |
| 139 | assert.Equal(t, l.Instance, inst) |
| 140 | assert.Equal(t, l.SSHLocalPort, inst.SSHLocalPort) |
| 141 | assert.Equal(t, l.sshAddress, inst.SSHAddress) |
| 142 | }) |
| 143 | |
| 144 | t.Run("keeps the values that are already set", func(t *testing.T) { |
| 145 | inst := newTestInstance(t, &limatype.LimaYAML{ |
| 146 | Arch: &arch, |
| 147 | VMType: new(limatype.HCS), |
| 148 | MountType: new(limatype.NINEP), |
| 149 | }) |
| 150 | |
| 151 | l := New() |
| 152 | _, err := l.Configure(t.Context(), inst) |
| 153 | assert.NilError(t, err) |
| 154 | assert.Equal(t, *inst.Config.MountType, limatype.NINEP) |
| 155 | }) |
| 156 | |
| 157 | t.Run("propagates a validation error", func(t *testing.T) { |
| 158 | inst := newTestInstance(t, &limatype.LimaYAML{ |
| 159 | Arch: &arch, |
| 160 | TPM: new(true), |
| 161 | }) |
| 162 | |
| 163 | l := New() |
| 164 | configured, err := l.Configure(t.Context(), inst) |
| 165 | assert.ErrorContains(t, err, "field `tpm` is not supported") |
| 166 | assert.Assert(t, configured == nil) |
| 167 | }) |
| 168 | } |
| 169 | |
| 170 | func TestValidate(t *testing.T) { |
| 171 | arch := limatype.NewArch(runtime.GOARCH) |
| 172 | |
| 173 | l := New() |
| 174 | l.Instance = newTestInstance(t, &limatype.LimaYAML{ |
| 175 | Arch: &arch, |
| 176 | VMType: new(limatype.HCS), |
| 177 | }) |
| 178 | assert.NilError(t, l.Validate(t.Context())) |
| 179 | |
| 180 | l.Instance.Config.TPM = new(true) |
| 181 | assert.ErrorContains(t, l.Validate(t.Context()), "field `tpm` is not supported") |
| 182 | } |
| 183 | |
| 184 | func TestInfo(t *testing.T) { |
| 185 | l := New() |
| 186 | |
| 187 | info := l.Info(t.Context()) |
| 188 | assert.Equal(t, info.Name, "hcs") |
| 189 | assert.Equal(t, info.InstanceDir, "") |
| 190 | |
| 191 | assert.Equal(t, info.Features.NoCloudInit, false) |
| 192 | assert.Equal(t, info.Features.CanRunGUI, false) |
| 193 | |
| 194 | assert.Equal(t, info.Features.DynamicSSHAddress, true) |
| 195 | assert.Equal(t, info.Features.StaticSSHPort, true) |
| 196 | assert.DeepEqual(t, info.Features.SupportedImageFormats, []string{string(vhdx.Type)}) |
| 197 | |
| 198 | l.Instance = newTestInstance(t, &limatype.LimaYAML{}) |
| 199 | assert.Equal(t, l.Info(t.Context()).InstanceDir, l.Instance.Dir) |
| 200 | } |
| 201 | |
| 202 | func TestSSHAddress(t *testing.T) { |
| 203 | l := New() |
| 204 | |
| 205 | _, err := l.SSHAddress(t.Context()) |
| 206 | assert.ErrorContains(t, err, "not known yet") |
| 207 | |
| 208 | l.sshAddress = "192.168.10.5" |
| 209 | got, err := l.SSHAddress(t.Context()) |
| 210 | assert.NilError(t, err) |
| 211 | assert.Equal(t, got, "192.168.10.5") |
| 212 | } |
| 213 | |
| 214 | func TestCreateDisk(t *testing.T) { |
| 215 | l := New() |
| 216 | l.Instance = newTestInstance(t, &limatype.LimaYAML{}) |
| 217 | |
| 218 | diskPath := filepath.Join(l.Instance.Dir, filenames.Disk) |
| 219 | const content = "not a real disk" |
| 220 | assert.NilError(t, os.WriteFile(diskPath, []byte(content), 0o600)) |
| 221 | |
| 222 | assert.NilError(t, l.CreateDisk(t.Context())) |
| 223 | |
| 224 | st, err := os.Stat(diskPath + ".vhdx") |
| 225 | assert.NilError(t, err) |
| 226 | assert.Equal(t, st.Size(), int64(len(content))) |
| 227 | |
| 228 | assert.NilError(t, l.CreateDisk(t.Context())) |
| 229 | } |
| 230 | |
| 231 | func TestGuestAgentIsForwardedByHostAgent(t *testing.T) { |
| 232 | l := New() |
| 233 | assert.Equal(t, l.ForwardGuestAgent(t.Context()), true) |
| 234 | |
| 235 | conn, addr, err := l.GuestAgentConn(t.Context()) |
| 236 | assert.NilError(t, err) |
| 237 | assert.Assert(t, conn == nil) |
| 238 | assert.Equal(t, addr, "") |
| 239 | } |
| 240 | |
| 241 | func TestStopBeforeStart(t *testing.T) { |
| 242 | l := New() |
| 243 | l.Instance = newTestInstance(t, &limatype.LimaYAML{}) |
| 244 | |
| 245 | assert.NilError(t, l.Stop(t.Context())) |
| 246 | } |