|
| 1 | +// Copyright 2025 CoreOS, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package disks |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | +) |
| 20 | + |
| 21 | +func TestPartitionNumberPrefix(t *testing.T) { |
| 22 | + tests := []struct { |
| 23 | + in string |
| 24 | + out string |
| 25 | + }{ |
| 26 | + {"/dev/sda", ""}, |
| 27 | + {"/dev/vda", ""}, |
| 28 | + {"/dev/nvme0n1", "p"}, |
| 29 | + {"/dev/mmcblk0", "p"}, |
| 30 | + {"/dev/loop0", "p"}, |
| 31 | + } |
| 32 | + for _, tt := range tests { |
| 33 | + t.Run(tt.in, func(t *testing.T) { |
| 34 | + got := partitionNumberPrefix(tt.in) |
| 35 | + if got != tt.out { |
| 36 | + t.Errorf("partitionNumberPrefix(%q) = %q, want %q", tt.in, got, tt.out) |
| 37 | + } |
| 38 | + }) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestPartitionDevPath(t *testing.T) { |
| 43 | + tests := []struct { |
| 44 | + blockDev string |
| 45 | + dmName string |
| 46 | + prefix string |
| 47 | + partNum int |
| 48 | + out string |
| 49 | + }{ |
| 50 | + {"/dev/sda", "", "", 1, "/dev/sda1"}, |
| 51 | + {"/dev/sda", "", "", 12, "/dev/sda12"}, |
| 52 | + {"/dev/nvme0n1", "", "p", 1, "/dev/nvme0n1p1"}, |
| 53 | + {"/dev/nvme0n1", "", "p", 3, "/dev/nvme0n1p3"}, |
| 54 | + {"/dev/mmcblk0", "", "p", 2, "/dev/mmcblk0p2"}, |
| 55 | + {"/dev/dm-0", "mpath0", "p", 1, "/dev/disk/by-id/dm-name-mpath0p1"}, |
| 56 | + {"/dev/dm-0", "mpath0", "p", 5, "/dev/disk/by-id/dm-name-mpath0p5"}, |
| 57 | + } |
| 58 | + for _, tt := range tests { |
| 59 | + t.Run(tt.out, func(t *testing.T) { |
| 60 | + got := partitionDevPath(tt.blockDev, tt.dmName, tt.prefix, tt.partNum) |
| 61 | + if got != tt.out { |
| 62 | + t.Errorf("partitionDevPath(%q, %q, %q, %d) = %q, want %q", |
| 63 | + tt.blockDev, tt.dmName, tt.prefix, tt.partNum, got, tt.out) |
| 64 | + } |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
0 commit comments