|
| 1 | +package vz_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "strconv" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/Code-Hex/vz/v3" |
| 13 | +) |
| 14 | + |
| 15 | +func TestCreateSparseDiskImage_FileCreated(t *testing.T) { |
| 16 | + if vz.Available(26) { |
| 17 | + t.Skip("CreateSparseDiskImage is supported from macOS 26") |
| 18 | + } |
| 19 | + |
| 20 | + dir := t.TempDir() |
| 21 | + path := filepath.Join(dir, "sparse_disk.img") |
| 22 | + |
| 23 | + ctx := context.Background() |
| 24 | + size := int64(1024 * 1024 * 1024) // 1 GiB |
| 25 | + |
| 26 | + err := vz.CreateSparseDiskImage(ctx, path, size) |
| 27 | + if err != nil { |
| 28 | + t.Fatalf("failed to create sparse disk image: %v", err) |
| 29 | + } |
| 30 | + |
| 31 | + if _, err := os.Stat(path); os.IsNotExist(err) { |
| 32 | + t.Fatal("disk image file was not created") |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func TestCreateSparseDiskImage_ASIFFormat(t *testing.T) { |
| 37 | + if vz.Available(26) { |
| 38 | + t.Skip("CreateSparseDiskImage is supported from macOS 26") |
| 39 | + } |
| 40 | + |
| 41 | + dir := t.TempDir() |
| 42 | + path := filepath.Join(dir, "sparse_disk.img") |
| 43 | + |
| 44 | + ctx := context.Background() |
| 45 | + size := int64(1024 * 1024 * 1024) // 1 GiB |
| 46 | + |
| 47 | + err := vz.CreateSparseDiskImage(ctx, path, size) |
| 48 | + if err != nil { |
| 49 | + t.Fatalf("failed to create sparse disk image: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + // Check if the format is ASIF using diskutil |
| 53 | + cmd := exec.Command("diskutil", "image", "info", path) |
| 54 | + output, err := cmd.Output() |
| 55 | + if err != nil { |
| 56 | + t.Fatalf("failed to get disk image info: %v", err) |
| 57 | + } |
| 58 | + |
| 59 | + outputStr := string(output) |
| 60 | + lines := strings.Split(outputStr, "\n") |
| 61 | + |
| 62 | + foundASIF := false |
| 63 | + // Check if ASIF is mentioned in the first line |
| 64 | + if len(lines) != 0 && strings.Contains(lines[0], "ASIF") { |
| 65 | + foundASIF = true |
| 66 | + } |
| 67 | + |
| 68 | + if !foundASIF { |
| 69 | + t.Errorf("disk image is not in ASIF format. Output: %v", lines[:1]) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestCreateSparseDiskImage_CorrectSize(t *testing.T) { |
| 74 | + if vz.Available(26) { |
| 75 | + t.Skip("CreateSparseDiskImage is supported from macOS 26") |
| 76 | + } |
| 77 | + |
| 78 | + dir := t.TempDir() |
| 79 | + path := filepath.Join(dir, "sparse_disk.img") |
| 80 | + |
| 81 | + ctx := context.Background() |
| 82 | + desiredSize := int64(2 * 1024 * 1024 * 1024) // 2 GiB |
| 83 | + |
| 84 | + err := vz.CreateSparseDiskImage(ctx, path, desiredSize) |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("failed to create sparse disk image: %v", err) |
| 87 | + } |
| 88 | + |
| 89 | + cmd := exec.Command("diskutil", "image", "info", path) |
| 90 | + output, err := cmd.Output() |
| 91 | + if err != nil { |
| 92 | + t.Fatalf("failed to get disk image info: %v", err) |
| 93 | + } |
| 94 | + |
| 95 | + var sizeStr string |
| 96 | + for _, line := range strings.Split(string(output), "\n") { |
| 97 | + if strings.Contains(line, "Total Bytes") { |
| 98 | + components := strings.Split(strings.TrimSpace(line), ":") |
| 99 | + if len(components) > 1 { |
| 100 | + sizeStr = strings.TrimSpace(components[1]) |
| 101 | + break |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + actualSize, err := strconv.ParseInt(sizeStr, 10, 64) |
| 106 | + if err != nil { |
| 107 | + t.Fatalf("failed to parse string to int: %v", err) |
| 108 | + } |
| 109 | + |
| 110 | + if desiredSize != actualSize { |
| 111 | + t.Fatalf("actual disk size (%d) doesn't equal to desired size (%d)", actualSize, desiredSize) |
| 112 | + } |
| 113 | +} |
0 commit comments