@@ -405,6 +405,143 @@ func TestDeviceInUseByZFSLocalPV(t *testing.T) {
405405 }
406406}
407407
408+ func TestProbeEvent_deviceInUseByLVMLocalPV (t * testing.T ) {
409+ fakeFsID := "fake-fs-uuid"
410+ fakeBD := blockdevice.BlockDevice {
411+ FSInfo : blockdevice.FileSystemInformation {
412+ FileSystemUUID : fakeFsID ,
413+ },
414+ }
415+ fakeUUID , _ := generateUUID (fakeBD )
416+
417+ tests := map [string ]struct {
418+ bd blockdevice.BlockDevice
419+ bdAPIList * apis.BlockDeviceList
420+ createdOrUpdatedBDName string
421+ want bool
422+ wantErr bool
423+ }{
424+ "device not in use" : {
425+ bd : blockdevice.BlockDevice {
426+ DevUse : blockdevice.DeviceUsage {
427+ InUse : false ,
428+ },
429+ },
430+ bdAPIList : & apis.BlockDeviceList {},
431+ createdOrUpdatedBDName : "" ,
432+ want : true ,
433+ wantErr : false ,
434+ },
435+ "device in use, not by lvm localPV" : {
436+ bd : blockdevice.BlockDevice {
437+ DevUse : blockdevice.DeviceUsage {
438+ InUse : true ,
439+ UsedBy : blockdevice .CStor ,
440+ },
441+
442+ },
443+ bdAPIList : & apis.BlockDeviceList {},
444+ createdOrUpdatedBDName : "" ,
445+ want : true ,
446+ wantErr : false ,
447+ },
448+ "deviceType disk, used by lvm PV and is connected to the cluster for the first time" : {
449+ bd : blockdevice.BlockDevice {
450+ Identifier : blockdevice.Identifier {
451+ DevPath : "/dev/sda" ,
452+ },
453+ DeviceAttributes : blockdevice.DeviceAttribute {
454+ DeviceType : blockdevice .BlockDeviceTypeDisk ,
455+ },
456+ DevUse : blockdevice.DeviceUsage {
457+ InUse : true ,
458+ UsedBy : blockdevice .LVMLocalPV ,
459+ },
460+ FSInfo : blockdevice.FileSystemInformation {
461+ FileSystemUUID : fakeFsID ,
462+ },
463+ },
464+ bdAPIList : & apis.BlockDeviceList {},
465+ createdOrUpdatedBDName : fakeUUID ,
466+ want : false ,
467+ wantErr : false ,
468+ },
469+ "deviceType disk, used by lvm PV and is moved from disconnected and reconnected to the node at a different path" : {
470+ bd : blockdevice.BlockDevice {
471+ Identifier : blockdevice.Identifier {
472+ DevPath : "/dev/sda" ,
473+ },
474+ DeviceAttributes : blockdevice.DeviceAttribute {
475+ DeviceType : blockdevice .BlockDeviceTypeDisk ,
476+ },
477+ DevUse : blockdevice.DeviceUsage {
478+ InUse : true ,
479+ UsedBy : blockdevice .LVMLocalPV ,
480+ },
481+ FSInfo : blockdevice.FileSystemInformation {
482+ FileSystemUUID : fakeFsID ,
483+ },
484+ },
485+ bdAPIList : & apis.BlockDeviceList {
486+ Items : []apis.BlockDevice {
487+ {
488+ ObjectMeta : metav1.ObjectMeta {
489+ Name : fakeUUID ,
490+ },
491+ Spec : apis.DeviceSpec {
492+ Path : "/dev/sdb" ,
493+ },
494+ },
495+ },
496+ },
497+ createdOrUpdatedBDName : fakeUUID ,
498+ want : false ,
499+ wantErr : false ,
500+ },
501+ }
502+ for name , tt := range tests {
503+ t .Run (name , func (t * testing.T ) {
504+ s := scheme .Scheme
505+ s .AddKnownTypes (apis .GroupVersion , & apis.BlockDevice {})
506+ s .AddKnownTypes (apis .GroupVersion , & apis.BlockDeviceList {})
507+ cl := fake .NewFakeClientWithScheme (s )
508+
509+ // initialize client with all the bd resources
510+ for _ , bdAPI := range tt .bdAPIList .Items {
511+ cl .Create (context .TODO (), & bdAPI )
512+ }
513+
514+ ctrl := & controller.Controller {
515+ Clientset : cl ,
516+ }
517+ pe := & ProbeEvent {
518+ Controller : ctrl ,
519+ }
520+ got , err := pe .deviceInUseByLVMLocalPV (tt .bd , tt .bdAPIList )
521+ if (err != nil ) != tt .wantErr {
522+ t .Errorf ("deviceInUseByLVMLocalPV() error = %v, wantErr %v" , err , tt .wantErr )
523+ return
524+ }
525+ if got != tt .want {
526+ t .Errorf ("deviceInUseByLVMLocalPV() got = %v, want %v" , got , tt .want )
527+ }
528+
529+ // check if a BD has been created or updated
530+ if len (tt .createdOrUpdatedBDName ) != 0 {
531+ gotBDAPI := & apis.BlockDevice {}
532+ err := cl .Get (context .TODO (), client.ObjectKey {Name : tt .createdOrUpdatedBDName }, gotBDAPI )
533+ if err != nil {
534+ t .Errorf ("error in getting blockdevice %s" , tt .createdOrUpdatedBDName )
535+ }
536+ // verify the block-device-tag on the resource, also verify the path and node name
537+ assert .Equal (t , string (blockdevice .LVMLocalPV ), gotBDAPI .GetLabels ()[kubernetes .BlockDeviceTagLabel ])
538+ assert .Equal (t , tt .bd .DevPath , gotBDAPI .Spec .Path )
539+ assert .Equal (t , tt .bd .NodeAttributes [blockdevice .NodeName ], gotBDAPI .Spec .NodeAttributes .NodeName )
540+ }
541+ })
542+ }
543+ }
544+
408545func TestIsParentDeviceInUse (t * testing.T ) {
409546 cache := map [string ]blockdevice.BlockDevice {
410547 "/dev/sda" : {
0 commit comments