| 1 | |
| 2 | |
| 3 | |
| 4 | package driverutil |
| 5 | |
| 6 | import ( |
| 7 | "crypto/sha256" |
| 8 | "encoding/hex" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "runtime" |
| 12 | "strings" |
| 13 | "testing" |
| 14 | |
| 15 | "github.com/lima-vm/go-qcow2reader" |
| 16 | "github.com/lima-vm/go-qcow2reader/image" |
| 17 | "gotest.tools/v3/assert" |
| 18 | |
| 19 | "github.com/lima-vm/lima/v2/pkg/iso9660util" |
| 20 | "github.com/lima-vm/lima/v2/pkg/limatype/filenames" |
| 21 | "github.com/lima-vm/lima/v2/pkg/osutil" |
| 22 | ) |
| 23 | |
| 24 | const ( |
| 25 | typeRAW = image.Type("raw") |
| 26 | typeASIF = image.Type("asif") |
| 27 | ) |
| 28 | |
| 29 | func writeMinimalISO(t *testing.T, path string) { |
| 30 | t.Helper() |
| 31 | entries := []iso9660util.Entry{ |
| 32 | {Path: "/hello.txt", Reader: strings.NewReader("hello world")}, |
| 33 | } |
| 34 | assert.NilError(t, iso9660util.Write(path, "TESTISO", entries)) |
| 35 | } |
| 36 | |
| 37 | func writeNonISO(t *testing.T, path string) { |
| 38 | t.Helper() |
| 39 | size := 64 * 1024 |
| 40 | buf := make([]byte, size) |
| 41 | copy(buf[0x8001:], "XXXXX") |
| 42 | assert.NilError(t, os.WriteFile(path, buf, 0o644)) |
| 43 | } |
| 44 | |
| 45 | func sha256File(t *testing.T, path string) string { |
| 46 | t.Helper() |
| 47 | b, err := os.ReadFile(path) |
| 48 | assert.NilError(t, err) |
| 49 | sum := sha256.Sum256(b) |
| 50 | return hex.EncodeToString(sum[:]) |
| 51 | } |
| 52 | |
| 53 | func detectImageType(t *testing.T, path string) image.Type { |
| 54 | t.Helper() |
| 55 | f, err := os.Open(path) |
| 56 | assert.NilError(t, err) |
| 57 | defer f.Close() |
| 58 | img, err := qcow2reader.Open(f) |
| 59 | assert.NilError(t, err) |
| 60 | return img.Type() |
| 61 | } |
| 62 | |
| 63 | func checkDisk(t *testing.T, diskPath string, expectedType image.Type) { |
| 64 | t.Helper() |
| 65 | fi, err := os.Stat(diskPath) |
| 66 | assert.NilError(t, err) |
| 67 | assert.Assert(t, fi.Size() > 0) |
| 68 | assert.Equal(t, detectImageType(t, diskPath), expectedType) |
| 69 | } |
| 70 | |
| 71 | func assertSymlink(t *testing.T, path, expectedTarget string) { |
| 72 | t.Helper() |
| 73 | target, err := os.Readlink(path) |
| 74 | assert.NilError(t, err) |
| 75 | assert.Equal(t, target, expectedTarget) |
| 76 | } |
| 77 | |
| 78 | func isMacOS26OrHigher() bool { |
| 79 | if runtime.GOOS != "darwin" { |
| 80 | return false |
| 81 | } |
| 82 | version, err := osutil.ProductVersion() |
| 83 | if err != nil { |
| 84 | return false |
| 85 | } |
| 86 | return version.Major >= 26 |
| 87 | } |
| 88 | |
| 89 | func TestEnsureDisk_WithISOImage(t *testing.T) { |
| 90 | instDir := t.TempDir() |
| 91 | imagePath := filepath.Join(instDir, filenames.Image) |
| 92 | diskPath := filepath.Join(instDir, filenames.Disk) |
| 93 | isoPath := filepath.Join(instDir, filenames.ISO) |
| 94 | |
| 95 | formats := []image.Type{typeRAW} |
| 96 | if isMacOS26OrHigher() { |
| 97 | formats = append(formats, typeASIF) |
| 98 | } |
| 99 | |
| 100 | for _, format := range formats { |
| 101 | writeMinimalISO(t, imagePath) |
| 102 | imageHashBefore := sha256File(t, imagePath) |
| 103 | |
| 104 | assert.NilError(t, EnsureDisk(t.Context(), instDir, "2MiB", format)) |
| 105 | |
| 106 | |
| 107 | assert.Assert(t, !osutil.FileExists(imagePath)) |
| 108 | assert.Equal(t, imageHashBefore, sha256File(t, isoPath)) |
| 109 | isISO, err := iso9660util.IsISO9660(isoPath) |
| 110 | assert.NilError(t, err) |
| 111 | assert.Assert(t, isISO) |
| 112 | |
| 113 | |
| 114 | checkDisk(t, diskPath, format) |
| 115 | |
| 116 | assert.NilError(t, os.Remove(diskPath)) |
| 117 | assert.NilError(t, os.Remove(isoPath)) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | func TestEnsureDisk_WithNonISOImage(t *testing.T) { |
| 122 | instDir := t.TempDir() |
| 123 | imagePath := filepath.Join(instDir, filenames.Image) |
| 124 | diskPath := filepath.Join(instDir, filenames.Disk) |
| 125 | |
| 126 | formats := []image.Type{typeRAW} |
| 127 | if isMacOS26OrHigher() { |
| 128 | formats = append(formats, typeASIF) |
| 129 | } |
| 130 | |
| 131 | for _, format := range formats { |
| 132 | writeNonISO(t, imagePath) |
| 133 | |
| 134 | assert.NilError(t, EnsureDisk(t.Context(), instDir, "2MiB", format)) |
| 135 | |
| 136 | |
| 137 | assert.Assert(t, !osutil.FileExists(imagePath)) |
| 138 | |
| 139 | |
| 140 | checkDisk(t, diskPath, format) |
| 141 | |
| 142 | assert.NilError(t, os.Remove(diskPath)) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | func TestEnsureDisk_ExistingDisk(t *testing.T) { |
| 147 | instDir := t.TempDir() |
| 148 | imagePath := filepath.Join(instDir, filenames.Image) |
| 149 | diskPath := filepath.Join(instDir, filenames.Disk) |
| 150 | |
| 151 | writeNonISO(t, imagePath) |
| 152 | |
| 153 | formats := []image.Type{typeRAW} |
| 154 | if isMacOS26OrHigher() { |
| 155 | formats = append(formats, typeASIF) |
| 156 | } |
| 157 | |
| 158 | for _, format := range formats { |
| 159 | assert.NilError(t, os.WriteFile(diskPath, []byte("preexisting"), 0o644)) |
| 160 | origHash := sha256File(t, diskPath) |
| 161 | assert.NilError(t, EnsureDisk(t.Context(), instDir, "2MiB", format)) |
| 162 | assert.Equal(t, sha256File(t, diskPath), origHash) |
| 163 | assert.NilError(t, os.Remove(diskPath)) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | func TestMigrateDiskLayout_LegacyDiffDisk(t *testing.T) { |
| 168 | instDir := t.TempDir() |
| 169 | diffDiskPath := filepath.Join(instDir, filenames.DiffDiskLegacy) |
| 170 | |
| 171 | assert.NilError(t, os.WriteFile(diffDiskPath, []byte("legacy-disk"), 0o644)) |
| 172 | origHash := sha256File(t, diffDiskPath) |
| 173 | |
| 174 | assert.NilError(t, MigrateDiskLayout(instDir)) |
| 175 | |
| 176 | |
| 177 | diskPath := filepath.Join(instDir, filenames.Disk) |
| 178 | assertSymlink(t, diskPath, filenames.DiffDiskLegacy) |
| 179 | assert.Equal(t, sha256File(t, diskPath), origHash) |
| 180 | |
| 181 | |
| 182 | assert.Assert(t, osutil.FileExists(diffDiskPath)) |
| 183 | } |
| 184 | |
| 185 | func TestMigrateDiskLayout_LegacyISOBaseDisk(t *testing.T) { |
| 186 | instDir := t.TempDir() |
| 187 | baseDiskPath := filepath.Join(instDir, filenames.BaseDiskLegacy) |
| 188 | diffDiskPath := filepath.Join(instDir, filenames.DiffDiskLegacy) |
| 189 | |
| 190 | writeMinimalISO(t, baseDiskPath) |
| 191 | baseHash := sha256File(t, baseDiskPath) |
| 192 | assert.NilError(t, os.WriteFile(diffDiskPath, []byte("legacy-disk"), 0o644)) |
| 193 | diffHash := sha256File(t, diffDiskPath) |
| 194 | |
| 195 | assert.NilError(t, MigrateDiskLayout(instDir)) |
| 196 | |
| 197 | |
| 198 | diskPath := filepath.Join(instDir, filenames.Disk) |
| 199 | assertSymlink(t, diskPath, filenames.DiffDiskLegacy) |
| 200 | assert.Equal(t, sha256File(t, diskPath), diffHash) |
| 201 | |
| 202 | |
| 203 | isoPath := filepath.Join(instDir, filenames.ISO) |
| 204 | assertSymlink(t, isoPath, filenames.BaseDiskLegacy) |
| 205 | assert.Equal(t, sha256File(t, isoPath), baseHash) |
| 206 | |
| 207 | |
| 208 | assert.Assert(t, osutil.FileExists(diffDiskPath)) |
| 209 | assert.Assert(t, osutil.FileExists(baseDiskPath)) |
| 210 | } |
| 211 | |
| 212 | func TestMigrateDiskLayout_LegacyNonISOBaseDisk(t *testing.T) { |
| 213 | instDir := t.TempDir() |
| 214 | baseDiskPath := filepath.Join(instDir, filenames.BaseDiskLegacy) |
| 215 | diffDiskPath := filepath.Join(instDir, filenames.DiffDiskLegacy) |
| 216 | |
| 217 | writeNonISO(t, baseDiskPath) |
| 218 | baseHash := sha256File(t, baseDiskPath) |
| 219 | assert.NilError(t, os.WriteFile(diffDiskPath, []byte("legacy-disk"), 0o644)) |
| 220 | |
| 221 | assert.NilError(t, MigrateDiskLayout(instDir)) |
| 222 | |
| 223 | |
| 224 | diskPath := filepath.Join(instDir, filenames.Disk) |
| 225 | assertSymlink(t, diskPath, filenames.DiffDiskLegacy) |
| 226 | |
| 227 | |
| 228 | assert.Equal(t, sha256File(t, baseDiskPath), baseHash) |
| 229 | |
| 230 | |
| 231 | isoPath := filepath.Join(instDir, filenames.ISO) |
| 232 | _, err := os.Lstat(isoPath) |
| 233 | assert.Assert(t, os.IsNotExist(err)) |
| 234 | } |
| 235 | |
| 236 | func TestMigrateDiskLayout_AlreadyMigrated(t *testing.T) { |
| 237 | instDir := t.TempDir() |
| 238 | diskPath := filepath.Join(instDir, filenames.Disk) |
| 239 | |
| 240 | assert.NilError(t, os.WriteFile(diskPath, []byte("current-disk"), 0o644)) |
| 241 | origHash := sha256File(t, diskPath) |
| 242 | |
| 243 | assert.NilError(t, MigrateDiskLayout(instDir)) |
| 244 | |
| 245 | |
| 246 | assert.Equal(t, sha256File(t, diskPath), origHash) |
| 247 | } |