|
| 1 | +package disks |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | + |
| 8 | + internalErrors "github.com/coreos/ignition/v2/config/shared/errors" |
| 9 | +) |
| 10 | + |
| 11 | +func TestPartitionParse(t *testing.T) { |
| 12 | + // Define test cases |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + sfdiskOut string |
| 16 | + partitionNumbers []int |
| 17 | + expectedOutput map[int]sfdiskOutput |
| 18 | + expectedError error |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "valid input with single partition", |
| 22 | + sfdiskOut: ` |
| 23 | +Disk /dev/vda: 2 GiB, 2147483648 bytes, 4194304 sectors |
| 24 | +Units: sectors of 1 * 512 = 512 bytes |
| 25 | +Sector size (logical/physical): 512 bytes / 512 bytes |
| 26 | +I/O size (minimum/optimal): 512 bytes / 512 bytes |
| 27 | +
|
| 28 | +>>> Created a new DOS (MBR) disklabel with disk identifier 0x501fc254. |
| 29 | +/dev/vda1: Created a new partition 1 of type 'Linux' and of size 5 KiB. |
| 30 | +/dev/vda2: Done. |
| 31 | +
|
| 32 | +New situation: |
| 33 | +Disklabel type: dos |
| 34 | +Disk identifier: 0x501fc254 |
| 35 | +
|
| 36 | +Device Boot Start End Sectors Size Id Type |
| 37 | +/dev/vda1 2048 2057 10 5K 83 Linux |
| 38 | +The partition table is unchanged (--no-act).`, |
| 39 | + partitionNumbers: []int{1}, |
| 40 | + expectedOutput: map[int]sfdiskOutput{ |
| 41 | + 1: {start: 2048, size: 10}, |
| 42 | + }, |
| 43 | + expectedError: nil, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "valid input with two partitions", |
| 47 | + sfdiskOut: ` |
| 48 | +Disk /dev/vda: 2 GiB, 2147483648 bytes, 4194304 sectors |
| 49 | +Units: sectors of 1 * 512 = 512 bytes |
| 50 | +Sector size (logical/physical): 512 bytes / 512 bytes |
| 51 | +I/O size (minimum/optimal): 512 bytes / 512 bytes |
| 52 | +
|
| 53 | +>>> Created a new DOS (MBR) disklabel with disk identifier 0x8d8dd38c. |
| 54 | +/dev/vda1: Created a new partition 1 of type 'Linux' and of size 5 KiB. |
| 55 | +/dev/vda2: Created a new partition 2 of type 'Linux' and of size 5 KiB. |
| 56 | +/dev/vda3: Done. |
| 57 | +
|
| 58 | +New situation: |
| 59 | +Disklabel type: dos |
| 60 | +Disk identifier: 0x8d8dd38c |
| 61 | +
|
| 62 | +Device Boot Start End Sectors Size Id Type |
| 63 | +/dev/vda1 2048 2057 10 5K 83 Linux |
| 64 | +/dev/vda2 4096 4105 10 5K 83 Linux |
| 65 | +The partition table is unchanged (--no-act).`, |
| 66 | + partitionNumbers: []int{1, 2}, |
| 67 | + expectedOutput: map[int]sfdiskOutput{ |
| 68 | + 1: {start: 2048, size: 10}, |
| 69 | + 2: {start: 4096, size: 10}, |
| 70 | + }, |
| 71 | + expectedError: nil, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "invalid input with 1 partition starting on sector 0", |
| 75 | + sfdiskOut: ` |
| 76 | +Disk /dev/vda: 2 GiB, 2147483648 bytes, 4194304 sectors |
| 77 | +Units: sectors of 1 * 512 = 512 bytes |
| 78 | +Sector size (logical/physical): 512 bytes / 512 bytes |
| 79 | +I/O size (minimum/optimal): 512 bytes / 512 bytes |
| 80 | +
|
| 81 | +>>> Created a new DOS (MBR) disklabel with disk identifier 0xdebbe997. |
| 82 | +/dev/vda1: Start sector 0 out of range. |
| 83 | +Failed to add #1 partition: Numerical result out of range |
| 84 | +Leaving. |
| 85 | +`, |
| 86 | + partitionNumbers: []int{1}, |
| 87 | + expectedOutput: map[int]sfdiskOutput{ |
| 88 | + 1: {start: 0, size: 0}, |
| 89 | + }, |
| 90 | + expectedError: internalErrors.ErrBadSfdiskPretend, |
| 91 | + }, |
| 92 | + } |
| 93 | + |
| 94 | + for i, tt := range tests { |
| 95 | + t.Run(tt.name, func(t *testing.T) { |
| 96 | + output, err := parseSfdiskPretend(tt.sfdiskOut, tt.partitionNumbers) |
| 97 | + if tt.expectedError != nil { |
| 98 | + if !errors.Is(err, tt.expectedError) { |
| 99 | + t.Errorf("#%d: bad error: result = %v, expected = %v", i, err, tt.expectedError) |
| 100 | + } |
| 101 | + } else if !reflect.DeepEqual(output, tt.expectedOutput) { |
| 102 | + t.Errorf("#%d: result = %v, expected = %v", i, output, tt.expectedOutput) |
| 103 | + } |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
0 commit comments