|
| 1 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package driverutil |
| 5 | + |
| 6 | +import ( |
| 7 | + "crypto/sha256" |
| 8 | + "encoding/hex" |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "path/filepath" |
| 12 | + "runtime" |
| 13 | + "strconv" |
| 14 | + "strings" |
| 15 | + "testing" |
| 16 | + |
| 17 | + "github.com/lima-vm/go-qcow2reader" |
| 18 | + "github.com/lima-vm/go-qcow2reader/image" |
| 19 | + "github.com/lima-vm/lima/v2/pkg/iso9660util" |
| 20 | + "github.com/lima-vm/lima/v2/pkg/limatype/filenames" |
| 21 | + "gotest.tools/v3/assert" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + typeRAW = image.Type("raw") |
| 26 | + typeASIF = image.Type("asif") |
| 27 | +) |
| 28 | + |
| 29 | +func makeTempInstanceDir(t *testing.T) string { |
| 30 | + t.Helper() |
| 31 | + return t.TempDir() |
| 32 | +} |
| 33 | + |
| 34 | +func writeMinimalISO(t *testing.T, path string) { |
| 35 | + t.Helper() |
| 36 | + entries := []iso9660util.Entry{ |
| 37 | + {Path: "/hello.txt", Reader: strings.NewReader("hello world")}, |
| 38 | + } |
| 39 | + assert.NilError(t, iso9660util.Write(path, "TESTISO", entries)) |
| 40 | +} |
| 41 | + |
| 42 | +func writeNonISO(t *testing.T, path string) { |
| 43 | + t.Helper() |
| 44 | + size := 64 * 1024 |
| 45 | + buf := make([]byte, size) |
| 46 | + copy(buf[0x8001:], "XXXXX") |
| 47 | + assert.NilError(t, os.WriteFile(path, buf, 0o644)) |
| 48 | +} |
| 49 | + |
| 50 | +func sha256File(t *testing.T, path string) string { |
| 51 | + t.Helper() |
| 52 | + b, err := os.ReadFile(path) |
| 53 | + assert.NilError(t, err) |
| 54 | + sum := sha256.Sum256(b) |
| 55 | + return hex.EncodeToString(sum[:]) |
| 56 | +} |
| 57 | + |
| 58 | +func detectImageType(t *testing.T, path string) image.Type { |
| 59 | + t.Helper() |
| 60 | + f, err := os.Open(path) |
| 61 | + assert.NilError(t, err) |
| 62 | + defer f.Close() |
| 63 | + img, err := qcow2reader.Open(f) |
| 64 | + assert.NilError(t, err) |
| 65 | + return img.Type() |
| 66 | +} |
| 67 | + |
| 68 | +func checkDisk(t *testing.T, diff string, expectedType image.Type) { |
| 69 | + t.Helper() |
| 70 | + fi, err := os.Stat(diff) |
| 71 | + assert.NilError(t, err) |
| 72 | + assert.Assert(t, fi.Size() > 0) |
| 73 | + assert.Equal(t, detectImageType(t, diff), expectedType) |
| 74 | +} |
| 75 | + |
| 76 | +func removeAndCheck(t *testing.T, path string) { |
| 77 | + t.Helper() |
| 78 | + assert.NilError(t, os.Remove(path)) |
| 79 | + _, err := os.Stat(path) |
| 80 | + assert.ErrorIs(t, err, os.ErrNotExist) |
| 81 | +} |
| 82 | + |
| 83 | +func isMacOS26OrHigher(t *testing.T) bool { |
| 84 | + if runtime.GOOS != "darwin" { |
| 85 | + return false |
| 86 | + } |
| 87 | + out, err := exec.CommandContext(t.Context(), "sw_vers", "-productVersion").Output() |
| 88 | + if err != nil { |
| 89 | + return false |
| 90 | + } |
| 91 | + parts := strings.Split(strings.TrimSpace(string(out)), ".") |
| 92 | + if len(parts) < 1 { |
| 93 | + return false |
| 94 | + } |
| 95 | + major, err := strconv.Atoi(parts[0]) |
| 96 | + if err != nil { |
| 97 | + return false |
| 98 | + } |
| 99 | + return major >= 26 |
| 100 | +} |
| 101 | + |
| 102 | +func TestEnsureDisk_WithISOBaseImage(t *testing.T) { |
| 103 | + instDir := makeTempInstanceDir(t) |
| 104 | + base := filepath.Join(instDir, filenames.BaseDisk) |
| 105 | + diff := filepath.Join(instDir, filenames.DiffDisk) |
| 106 | + |
| 107 | + writeMinimalISO(t, base) |
| 108 | + isISO, err := iso9660util.IsISO9660(base) |
| 109 | + assert.NilError(t, err) |
| 110 | + assert.Assert(t, isISO) |
| 111 | + baseHashBefore := sha256File(t, base) |
| 112 | + |
| 113 | + formats := []image.Type{typeRAW} |
| 114 | + if isMacOS26OrHigher(t) { |
| 115 | + formats = append(formats, typeASIF) |
| 116 | + } |
| 117 | + |
| 118 | + for _, format := range formats { |
| 119 | + assert.NilError(t, EnsureDisk(t.Context(), instDir, "2MiB", format)) |
| 120 | + isISO, err = iso9660util.IsISO9660(base) |
| 121 | + assert.NilError(t, err) |
| 122 | + assert.Assert(t, isISO) |
| 123 | + assert.Equal(t, baseHashBefore, sha256File(t, base)) |
| 124 | + checkDisk(t, diff, format) |
| 125 | + if format != formats[len(formats)-1] { |
| 126 | + removeAndCheck(t, diff) |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestEnsureDisk_WithNonISOBaseImage(t *testing.T) { |
| 132 | + instDir := makeTempInstanceDir(t) |
| 133 | + base := filepath.Join(instDir, filenames.BaseDisk) |
| 134 | + diff := filepath.Join(instDir, filenames.DiffDisk) |
| 135 | + |
| 136 | + writeNonISO(t, base) |
| 137 | + isISO, err := iso9660util.IsISO9660(base) |
| 138 | + assert.NilError(t, err) |
| 139 | + assert.Assert(t, !isISO) |
| 140 | + |
| 141 | + formats := []image.Type{typeRAW} |
| 142 | + if isMacOS26OrHigher(t) { |
| 143 | + formats = append(formats, typeASIF) |
| 144 | + } |
| 145 | + |
| 146 | + for _, format := range formats { |
| 147 | + assert.NilError(t, EnsureDisk(t.Context(), instDir, "2MiB", format)) |
| 148 | + checkDisk(t, diff, format) |
| 149 | + if format != formats[len(formats)-1] { |
| 150 | + removeAndCheck(t, diff) |
| 151 | + } |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +func TestEnsureDisk_ExistingDiffDisk(t *testing.T) { |
| 156 | + instDir := makeTempInstanceDir(t) |
| 157 | + base := filepath.Join(instDir, filenames.BaseDisk) |
| 158 | + diff := filepath.Join(instDir, filenames.DiffDisk) |
| 159 | + |
| 160 | + writeNonISO(t, base) |
| 161 | + |
| 162 | + formats := []image.Type{typeRAW} |
| 163 | + if isMacOS26OrHigher(t) { |
| 164 | + formats = append(formats, typeASIF) |
| 165 | + } |
| 166 | + |
| 167 | + for _, format := range formats { |
| 168 | + assert.NilError(t, os.WriteFile(diff, []byte("preexisting"), 0o644)) |
| 169 | + origHash := sha256File(t, diff) |
| 170 | + assert.NilError(t, EnsureDisk(t.Context(), instDir, "2MiB", format)) |
| 171 | + assert.Equal(t, sha256File(t, diff), origHash) |
| 172 | + removeAndCheck(t, diff) |
| 173 | + } |
| 174 | +} |
0 commit comments