|
| 1 | +//go:build darwin |
| 2 | + |
| 3 | +package register |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func Test_parseDiskutilSize(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + val string |
| 13 | + want int64 |
| 14 | + }{ |
| 15 | + { |
| 16 | + "BytesFirst", |
| 17 | + "500107862016 Bytes (exactly 976773168 512-Byte-Units)", |
| 18 | + 500107862016, |
| 19 | + }, |
| 20 | + { |
| 21 | + "HumanReadableFirst", |
| 22 | + "1.0 TB (1,000,000,000,000 Bytes)", |
| 23 | + 1000000000000, |
| 24 | + }, |
| 25 | + { |
| 26 | + "GBWithParenBytes", |
| 27 | + "500.1 GB (500,107,862,016 Bytes)", |
| 28 | + 500107862016, |
| 29 | + }, |
| 30 | + { |
| 31 | + "NoParensFallback", |
| 32 | + "500107862016", |
| 33 | + 500107862016, |
| 34 | + }, |
| 35 | + { |
| 36 | + "Empty", |
| 37 | + "", |
| 38 | + 0, |
| 39 | + }, |
| 40 | + } |
| 41 | + for _, tt := range tests { |
| 42 | + t.Run(tt.name, func(t *testing.T) { |
| 43 | + dev := &StorageDevice{} |
| 44 | + parseDiskutilSize(tt.val, dev) |
| 45 | + if dev.StorageBytes != tt.want { |
| 46 | + t.Errorf("parseDiskutilSize(%q) = %d, want %d", tt.val, dev.StorageBytes, tt.want) |
| 47 | + } |
| 48 | + }) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func Test_isWholeDisk(t *testing.T) { |
| 53 | + tests := []struct { |
| 54 | + name string |
| 55 | + want bool |
| 56 | + }{ |
| 57 | + {"disk0", true}, |
| 58 | + {"disk1", true}, |
| 59 | + {"disk0s1", false}, |
| 60 | + {"disk2s3", false}, |
| 61 | + {"notadisk", false}, |
| 62 | + } |
| 63 | + for _, tt := range tests { |
| 64 | + t.Run(tt.name, func(t *testing.T) { |
| 65 | + if got := isWholeDisk(tt.name); got != tt.want { |
| 66 | + t.Errorf("isWholeDisk(%q) = %v, want %v", tt.name, got, tt.want) |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func Test_parseDiskutilSolidState(t *testing.T) { |
| 73 | + tests := []struct { |
| 74 | + val string |
| 75 | + want string |
| 76 | + }{ |
| 77 | + {"Yes", "SSD"}, |
| 78 | + {"yes", "SSD"}, |
| 79 | + {"No", "HDD"}, |
| 80 | + {"no", "HDD"}, |
| 81 | + } |
| 82 | + for _, tt := range tests { |
| 83 | + t.Run(tt.val, func(t *testing.T) { |
| 84 | + dev := &StorageDevice{} |
| 85 | + parseDiskutilSolidState(tt.val, dev) |
| 86 | + if dev.StorageType != tt.want { |
| 87 | + t.Errorf("parseDiskutilSolidState(%q) → %q, want %q", tt.val, dev.StorageType, tt.want) |
| 88 | + } |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func Test_parseDiskutilInfoOutput(t *testing.T) { |
| 94 | + output := `********** |
| 95 | +
|
| 96 | + Device Identifier: disk0 |
| 97 | + Device Node: /dev/disk0 |
| 98 | + Whole: Yes |
| 99 | + Part of Whole: disk0 |
| 100 | + Disk Size: 500.1 GB (500,107,862,016 Bytes) |
| 101 | + Protocol: NVMe |
| 102 | + Solid State: Yes |
| 103 | + Device / Media Name: APPLE SSD AP0512Q |
| 104 | +
|
| 105 | +********** |
| 106 | +
|
| 107 | + Device Identifier: disk0s1 |
| 108 | + Device Node: /dev/disk0s1 |
| 109 | + Whole: No |
| 110 | + Part of Whole: disk0 |
| 111 | + Disk Size: 524.3 MB (524,288,000 Bytes) |
| 112 | +
|
| 113 | +********** |
| 114 | +
|
| 115 | + Device Identifier: disk1 |
| 116 | + Device Node: /dev/disk1 |
| 117 | + Whole: Yes |
| 118 | + Part of Whole: disk1 |
| 119 | + Disk Size: 1.0 TB (1,000,000,000,000 Bytes) |
| 120 | + Solid State: No |
| 121 | +
|
| 122 | +` |
| 123 | + |
| 124 | + devices := parseDiskutilInfoOutput(output) |
| 125 | + if len(devices) != 2 { |
| 126 | + t.Fatalf("expected 2 devices, got %d", len(devices)) |
| 127 | + } |
| 128 | + |
| 129 | + if devices[0].Name != "disk0" { |
| 130 | + t.Errorf("device 0 name = %q, want disk0", devices[0].Name) |
| 131 | + } |
| 132 | + if devices[0].StorageBytes != 500107862016 { |
| 133 | + t.Errorf("device 0 bytes = %d, want 500107862016", devices[0].StorageBytes) |
| 134 | + } |
| 135 | + if devices[0].StorageType != "NVMe" { |
| 136 | + t.Errorf("device 0 type = %q, want NVMe", devices[0].StorageType) |
| 137 | + } |
| 138 | + |
| 139 | + if devices[1].Name != "disk1" { |
| 140 | + t.Errorf("device 1 name = %q, want disk1", devices[1].Name) |
| 141 | + } |
| 142 | + if devices[1].StorageBytes != 1000000000000 { |
| 143 | + t.Errorf("device 1 bytes = %d, want 1000000000000", devices[1].StorageBytes) |
| 144 | + } |
| 145 | + if devices[1].StorageType != "HDD" { |
| 146 | + t.Errorf("device 1 type = %q, want HDD", devices[1].StorageType) |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +func Test_parseDiskutilInfoOutput_Empty(t *testing.T) { |
| 151 | + devices := parseDiskutilInfoOutput("") |
| 152 | + if len(devices) != 0 { |
| 153 | + t.Errorf("expected 0 devices, got %d", len(devices)) |
| 154 | + } |
| 155 | +} |
0 commit comments