github.com/openebs/node-disk-manager@v1.9.1-0.20230225014141-4531f06ffa1e/cmd/ndm_daemonset/probe/uuid_test.go (about) 1 /* 2 Copyright 2020 The OpenEBS Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package probe 18 19 import ( 20 "os" 21 "testing" 22 23 "github.com/openebs/node-disk-manager/blockdevice" 24 "github.com/openebs/node-disk-manager/pkg/features" 25 "github.com/openebs/node-disk-manager/pkg/util" 26 "github.com/stretchr/testify/assert" 27 ) 28 29 func TestGenerateUUID(t *testing.T) { 30 fakeWWN := "50E5495131BBB060892FBC8E" 31 fakeSerial := "CT500MX500SSD1" 32 fakeFileSystemUUID := "149108ca-f404-4556-a263-04943e6cb0b3" 33 fakePartitionUUID := "065e2357-05" 34 fakePartitionTableUUID := "6f479331-dad4-4ccb-b146-5c359c55399b" 35 fakeLVM_DM_UUID := "LVM-j2xmqvbcVWBQK9Jdttte3CyeVTGgxtVV5VcCi3nxdwihZDxSquMOBaGL5eymBNvk" 36 fakeCRYPT_DM_UUID := "CRYPT-LUKS1-f4608c76343d4b5badaf6651d32f752b-backup" 37 loopDevicePath := "/dev/loop98" 38 hostName, _ := os.Hostname() 39 features.FeatureGates.SetFeatureFlag([]string{ 40 "GPTBasedUUID=1", 41 "PartitionTableUUID=1", 42 }) 43 tests := map[string]struct { 44 bd blockdevice.BlockDevice 45 wantUUID string 46 wantOk bool 47 }{ 48 "debiceType-disk with PartitionTableUUID": { 49 bd: blockdevice.BlockDevice{ 50 PartitionInfo: blockdevice.PartitionInformation{ 51 PartitionTableType: "gpt", 52 PartitionTableUUID: fakePartitionTableUUID, 53 }, 54 }, 55 wantUUID: blockdevice.BlockDevicePrefix + util.Hash(fakePartitionTableUUID), 56 wantOk: true, 57 }, 58 "deviceType-disk with WWN": { 59 bd: blockdevice.BlockDevice{ 60 DeviceAttributes: blockdevice.DeviceAttribute{ 61 DeviceType: blockdevice.BlockDeviceTypeDisk, 62 WWN: fakeWWN, 63 }, 64 }, 65 wantUUID: blockdevice.BlockDevicePrefix + util.Hash(fakeWWN), 66 wantOk: true, 67 }, 68 "deviceType-disk with WWN and serial": { 69 bd: blockdevice.BlockDevice{ 70 DeviceAttributes: blockdevice.DeviceAttribute{ 71 DeviceType: blockdevice.BlockDeviceTypeDisk, 72 WWN: fakeWWN, 73 Serial: fakeSerial, 74 }, 75 }, 76 wantUUID: blockdevice.BlockDevicePrefix + util.Hash(fakeWWN+fakeSerial), 77 wantOk: true, 78 }, 79 "deviceType-disk with a filesystem and no wwn": { 80 bd: blockdevice.BlockDevice{ 81 FSInfo: blockdevice.FileSystemInformation{ 82 FileSystemUUID: fakeFileSystemUUID, 83 }, 84 DeviceAttributes: blockdevice.DeviceAttribute{ 85 DeviceType: blockdevice.BlockDeviceTypeDisk, 86 }, 87 }, 88 wantUUID: blockdevice.BlockDevicePrefix + util.Hash(fakeFileSystemUUID), 89 wantOk: true, 90 }, 91 "deviceType-disk with a filesystem and wwn": { 92 bd: blockdevice.BlockDevice{ 93 FSInfo: blockdevice.FileSystemInformation{ 94 FileSystemUUID: fakeFileSystemUUID, 95 }, 96 DeviceAttributes: blockdevice.DeviceAttribute{ 97 DeviceType: blockdevice.BlockDeviceTypeDisk, 98 WWN: fakeWWN, 99 }, 100 }, 101 wantUUID: blockdevice.BlockDevicePrefix + util.Hash(fakeWWN), 102 wantOk: true, 103 }, 104 "deviceType-partition with wwn on the disk": { 105 bd: blockdevice.BlockDevice{ 106 DeviceAttributes: blockdevice.DeviceAttribute{ 107 DeviceType: blockdevice.BlockDeviceTypePartition, 108 WWN: fakeWWN, 109 }, 110 PartitionInfo: blockdevice.PartitionInformation{ 111 PartitionEntryUUID: fakePartitionUUID, 112 }, 113 }, 114 wantUUID: blockdevice.BlockDevicePrefix + util.Hash(fakePartitionUUID), 115 wantOk: true, 116 }, 117 "deviceType-disk with no wwn or filesystem": { 118 bd: blockdevice.BlockDevice{ 119 DeviceAttributes: blockdevice.DeviceAttribute{ 120 DeviceType: blockdevice.BlockDeviceTypeDisk, 121 }, 122 }, 123 wantUUID: "", 124 wantOk: false, 125 }, 126 "deviceType-lvm device": { 127 bd: blockdevice.BlockDevice{ 128 DMInfo: blockdevice.DeviceMapperInformation{ 129 DMUUID: fakeLVM_DM_UUID, 130 }, 131 DeviceAttributes: blockdevice.DeviceAttribute{ 132 DeviceType: blockdevice.BlockDeviceTypeLVM, 133 }, 134 }, 135 wantUUID: blockdevice.BlockDevicePrefix + util.Hash(fakeLVM_DM_UUID), 136 wantOk: true, 137 }, 138 "deviceType-crypt device": { 139 bd: blockdevice.BlockDevice{ 140 DMInfo: blockdevice.DeviceMapperInformation{ 141 DMUUID: fakeCRYPT_DM_UUID, 142 }, 143 DeviceAttributes: blockdevice.DeviceAttribute{ 144 DeviceType: blockdevice.BlockDeviceTypeCrypt, 145 }, 146 }, 147 wantUUID: blockdevice.BlockDevicePrefix + util.Hash(fakeCRYPT_DM_UUID), 148 wantOk: true, 149 }, 150 "deviceType-loop device": { 151 bd: blockdevice.BlockDevice{ 152 Identifier: blockdevice.Identifier{ 153 DevPath: loopDevicePath, 154 }, 155 DeviceAttributes: blockdevice.DeviceAttribute{ 156 DeviceType: blockdevice.BlockDeviceTypeLoop, 157 }, 158 }, 159 wantUUID: blockdevice.BlockDevicePrefix + util.Hash(hostName+loopDevicePath), 160 wantOk: true, 161 }, 162 } 163 for name, tt := range tests { 164 t.Run(name, func(t *testing.T) { 165 gotUUID, gotOk := generateUUID(tt.bd) 166 assert.Equal(t, tt.wantUUID, gotUUID) 167 assert.Equal(t, tt.wantOk, gotOk) 168 }) 169 } 170 } 171 172 func TestGenerateLegacyUUID(t *testing.T) { 173 fakePath := "/dev/sda" 174 fakeWWN := "50E5495131BBB060892FBC8E" 175 fakeSerial := "CT500MX500SSD1" 176 fakeModel := "DataTraveler_3.0" 177 fakeVendor := "Kingston" 178 hostname, _ := os.Hostname() 179 tests := map[string]struct { 180 bd blockdevice.BlockDevice 181 wantUUID string 182 wantOk bool 183 }{ 184 "NonLocal Disk Model with wwn/vendor/model/serial": { 185 bd: blockdevice.BlockDevice{ 186 Identifier: blockdevice.Identifier{ 187 DevPath: fakePath, 188 }, 189 DeviceAttributes: blockdevice.DeviceAttribute{ 190 IDType: "disk", 191 WWN: fakeWWN, 192 Vendor: fakeVendor, 193 Model: fakeModel, 194 Serial: fakeSerial, 195 }, 196 }, 197 wantUUID: blockdevice.BlockDevicePrefix + util.Hash(fakeWWN+fakeModel+fakeSerial+fakeVendor), 198 wantOk: false, 199 }, 200 "local disk model with wwn": { 201 bd: blockdevice.BlockDevice{ 202 Identifier: blockdevice.Identifier{ 203 DevPath: fakePath, 204 }, 205 DeviceAttributes: blockdevice.DeviceAttribute{ 206 WWN: fakeWWN, 207 Model: "Virtual_disk", 208 }, 209 }, 210 wantUUID: blockdevice.BlockDevicePrefix + util.Hash(fakeWWN+"Virtual_disk"+hostname+fakePath), 211 wantOk: true, 212 }, 213 } 214 for name, tt := range tests { 215 t.Run(name, func(t *testing.T) { 216 gotUUID, gotOk := generateLegacyUUID(tt.bd) 217 assert.Equal(t, tt.wantUUID, gotUUID) 218 assert.Equal(t, tt.wantOk, gotOk) 219 }) 220 } 221 }