@@ -4217,3 +4217,203 @@ func TestCreateVmWithExistingEncryptedDisk(t *testing.T) {
42174217 })
42184218 }
42194219}
4220+
4221+ func TestVirtualDiskUUIDAssignment (t * testing.T ) {
4222+ m := ESX ()
4223+ defer m .Remove ()
4224+ err := m .Create ()
4225+ if err != nil {
4226+ t .Fatal (err )
4227+ }
4228+
4229+ s := m .Service .NewServer ()
4230+ defer s .Close ()
4231+
4232+ ctx := context .Background ()
4233+ c , err := govmomi .NewClient (ctx , s .URL , true )
4234+ if err != nil {
4235+ t .Fatal (err )
4236+ }
4237+ defer c .Logout (ctx )
4238+
4239+ finder := find .NewFinder (c .Client , false )
4240+ dc , err := finder .DefaultDatacenter (ctx )
4241+ if err != nil {
4242+ t .Fatal (err )
4243+ }
4244+ finder .SetDatacenter (dc )
4245+
4246+ ds , err := finder .DefaultDatastore (ctx )
4247+ if err != nil {
4248+ t .Fatal (err )
4249+ }
4250+
4251+ folders , err := dc .Folders (ctx )
4252+ if err != nil {
4253+ t .Fatal (err )
4254+ }
4255+
4256+ hosts , err := finder .HostSystemList (ctx , "*/*" )
4257+ if err != nil {
4258+ t .Fatal (err )
4259+ }
4260+
4261+ host := hosts [0 ]
4262+ pool , err := host .ResourcePool (ctx )
4263+ if err != nil {
4264+ t .Fatal (err )
4265+ }
4266+
4267+ // Test 1: Add a disk with an empty UUID - it should be assigned one
4268+ t .Run ("empty UUID" , func (t * testing.T ) {
4269+ testVM := createTestVM (ctx , t , c , folders , pool , host , ds , "empty-uuid-vm" )
4270+ defer testVM .Destroy (ctx )
4271+
4272+ devices , err := testVM .Device (ctx )
4273+ if err != nil {
4274+ t .Fatal (err )
4275+ }
4276+ controller , err := devices .FindDiskController ("" )
4277+ if err != nil {
4278+ t .Fatal (err )
4279+ }
4280+ disk := devices .CreateDisk (controller , ds .Reference (), "test-disk-empty-uuid" )
4281+ expectedCapacity := int64 (10 * 1024 * 1024 ) // 10MB
4282+ disk .CapacityInBytes = expectedCapacity
4283+
4284+ err = testVM .AddDevice (ctx , disk )
4285+ if err != nil {
4286+ t .Fatal (err )
4287+ }
4288+
4289+ updatedDevices , err := testVM .Device (ctx )
4290+ if err != nil {
4291+ t .Fatal (err )
4292+ }
4293+ allDisks := updatedDevices .SelectByType ((* types .VirtualDisk )(nil ))
4294+ assert .Equal (t , 1 , len (allDisks ), "exactly one disk should be present" )
4295+
4296+ foundDisk := allDisks [0 ].(* types.VirtualDisk )
4297+ assert .Equal (t , expectedCapacity , foundDisk .CapacityInBytes , "disk capacity should match added disk" )
4298+
4299+ backing , ok := foundDisk .Backing .(* types.VirtualDiskFlatVer2BackingInfo )
4300+ assert .True (t , ok , "disk backing should be VirtualDiskFlatVer2BackingInfo" )
4301+ assert .NotEmpty (t , backing .Uuid , "disk UUID should not be empty" )
4302+ _ , err = uuid .Parse (backing .Uuid )
4303+ assert .NoError (t , err , "disk UUID should be a valid UUID" )
4304+ })
4305+
4306+ // Test 2: Add a disk - a UUID should be generated/assigned
4307+ t .Run ("generated UUID" , func (t * testing.T ) {
4308+ testVM := createTestVM (ctx , t , c , folders , pool , host , ds , "generated-uuid-vm" )
4309+ defer testVM .Destroy (ctx )
4310+
4311+ devices , err := testVM .Device (ctx )
4312+ if err != nil {
4313+ t .Fatal (err )
4314+ }
4315+ controller , err := devices .FindDiskController ("" )
4316+ if err != nil {
4317+ t .Fatal (err )
4318+ }
4319+
4320+ disk := devices .CreateDisk (controller , ds .Reference (), "test-disk-generated-uuid" )
4321+ expectedCapacity := int64 (20 * 1024 * 1024 ) // 20MB
4322+ disk .CapacityInBytes = expectedCapacity
4323+
4324+ err = testVM .AddDevice (ctx , disk )
4325+ if err != nil {
4326+ t .Fatal (err )
4327+ }
4328+
4329+ updatedDevices , err := testVM .Device (ctx )
4330+ if err != nil {
4331+ t .Fatal (err )
4332+ }
4333+ allDisks := updatedDevices .SelectByType ((* types .VirtualDisk )(nil ))
4334+ assert .Equal (t , 1 , len (allDisks ), "exactly one disk should be present" )
4335+
4336+ foundDisk := allDisks [0 ].(* types.VirtualDisk )
4337+ assert .Equal (t , expectedCapacity , foundDisk .CapacityInBytes , "disk capacity should match added disk" )
4338+
4339+ foundDiskBacking , ok := foundDisk .Backing .(* types.VirtualDiskFlatVer2BackingInfo )
4340+ assert .True (t , ok , "found disk backing should be VirtualDiskFlatVer2BackingInfo" )
4341+ assert .NotEmpty (t , foundDiskBacking .Uuid , "disk UUID should be assigned" )
4342+ _ , err = uuid .Parse (foundDiskBacking .Uuid )
4343+ assert .NoError (t , err , "assigned disk UUID should be a valid UUID" )
4344+ })
4345+
4346+ // Test 3: Add a disk with an explicit UUID - it should be preserved
4347+ t .Run ("explicit UUID" , func (t * testing.T ) {
4348+ testVM := createTestVM (ctx , t , c , folders , pool , host , ds , "explicit-uuid-vm" )
4349+ defer testVM .Destroy (ctx )
4350+
4351+ devices , err := testVM .Device (ctx )
4352+ if err != nil {
4353+ t .Fatal (err )
4354+ }
4355+ controller , err := devices .FindDiskController ("" )
4356+ if err != nil {
4357+ t .Fatal (err )
4358+ }
4359+
4360+ disk := devices .CreateDisk (controller , ds .Reference (), "test-disk-explicit-uuid" )
4361+ expectedCapacity := int64 (30 * 1024 * 1024 ) // 30MB
4362+ disk .CapacityInBytes = expectedCapacity
4363+
4364+ explicitUUID := uuid .New ().String ()
4365+ diskBacking := disk .Backing .(* types.VirtualDiskFlatVer2BackingInfo )
4366+ diskBacking .Uuid = explicitUUID
4367+
4368+ err = testVM .AddDevice (ctx , disk )
4369+ if err != nil {
4370+ t .Fatal (err )
4371+ }
4372+
4373+ updatedDevices , err := testVM .Device (ctx )
4374+ if err != nil {
4375+ t .Fatal (err )
4376+ }
4377+ allDisks := updatedDevices .SelectByType ((* types .VirtualDisk )(nil ))
4378+ assert .Equal (t , 1 , len (allDisks ), "exactly one disk should be present" )
4379+
4380+ foundDisk := allDisks [0 ].(* types.VirtualDisk )
4381+ assert .Equal (t , expectedCapacity , foundDisk .CapacityInBytes , "disk capacity should match added disk" )
4382+
4383+ foundDiskBacking , ok := foundDisk .Backing .(* types.VirtualDiskFlatVer2BackingInfo )
4384+ assert .True (t , ok , "found disk backing should be VirtualDiskFlatVer2BackingInfo" )
4385+ assert .Equal (t , explicitUUID , foundDiskBacking .Uuid , "disk UUID should match the explicitly set UUID" )
4386+ })
4387+ }
4388+
4389+ func createTestVM (ctx context.Context , t * testing.T , c * govmomi.Client , folders * object.DatacenterFolders , pool * object.ResourcePool , host * object.HostSystem , ds * object.Datastore , vmName string ) * object.VirtualMachine {
4390+ var devices object.VirtualDeviceList
4391+
4392+ scsi , _ := devices .CreateSCSIController ("pvscsi" )
4393+ ide , _ := devices .CreateIDEController ()
4394+ cdrom , _ := devices .CreateCdrom (ide .(* types.VirtualIDEController ))
4395+
4396+ devices = append (devices , scsi , cdrom )
4397+
4398+ spec := types.VirtualMachineConfigSpec {
4399+ Name : vmName ,
4400+ GuestId : string (types .VirtualMachineGuestOsIdentifierOtherGuest ),
4401+ Files : & types.VirtualMachineFileInfo {
4402+ VmPathName : "[" + ds .Name () + "]" ,
4403+ },
4404+ }
4405+
4406+ spec .DeviceChange , _ = devices .ConfigSpec (types .VirtualDeviceConfigSpecOperationAdd )
4407+
4408+ task , err := folders .VmFolder .CreateVM (ctx , spec , pool , host )
4409+ if err != nil {
4410+ t .Fatal (err )
4411+ }
4412+
4413+ taskInfo , err := task .WaitForResult (ctx , nil )
4414+ if err != nil {
4415+ t .Fatal (err )
4416+ }
4417+
4418+ return object .NewVirtualMachine (c .Client , taskInfo .Result .(types.ManagedObjectReference ))
4419+ }
0 commit comments