@@ -4217,3 +4217,223 @@ 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 , err := createTestVM (ctx , c , folders , pool , host , ds , "empty-uuid-vm" )
4270+ if err != nil {
4271+ t .Fatal (err )
4272+ }
4273+ defer testVM .Destroy (ctx )
4274+
4275+ devices , err := testVM .Device (ctx )
4276+ if err != nil {
4277+ t .Fatal (err )
4278+ }
4279+ controller , err := devices .FindDiskController ("" )
4280+ if err != nil {
4281+ t .Fatal (err )
4282+ }
4283+ disk := devices .CreateDisk (controller , ds .Reference (), "test-disk-empty-uuid" )
4284+ expectedCapacity := int64 (10 * 1024 * 1024 ) // 10MB
4285+ disk .CapacityInBytes = expectedCapacity
4286+
4287+ err = testVM .AddDevice (ctx , disk )
4288+ if err != nil {
4289+ t .Fatal (err )
4290+ }
4291+
4292+ updatedDevices , err := testVM .Device (ctx )
4293+ if err != nil {
4294+ t .Fatal (err )
4295+ }
4296+ allDisks := updatedDevices .SelectByType ((* types .VirtualDisk )(nil ))
4297+ assert .Equal (t , 1 , len (allDisks ), "exactly one disk should be present" )
4298+
4299+ foundDisk := allDisks [0 ].(* types.VirtualDisk )
4300+ assert .Equal (t , expectedCapacity , foundDisk .CapacityInBytes , "disk capacity should match added disk" )
4301+
4302+ backing , ok := foundDisk .Backing .(* types.VirtualDiskFlatVer2BackingInfo )
4303+ assert .True (t , ok , "disk backing should be VirtualDiskFlatVer2BackingInfo" )
4304+ assert .NotEmpty (t , backing .Uuid , "disk UUID should not be empty" )
4305+ _ , err = uuid .Parse (backing .Uuid )
4306+ assert .NoError (t , err , "disk UUID should be a valid UUID" )
4307+ })
4308+
4309+ // Test 2: Add a disk - a UUID should be generated/assigned
4310+ t .Run ("generated UUID" , func (t * testing.T ) {
4311+ testVM , err := createTestVM (ctx , c , folders , pool , host , ds , "generated-uuid-vm" )
4312+ if err != nil {
4313+ t .Fatal (err )
4314+ }
4315+ defer testVM .Destroy (ctx )
4316+
4317+ devices , err := testVM .Device (ctx )
4318+ if err != nil {
4319+ t .Fatal (err )
4320+ }
4321+ controller , err := devices .FindDiskController ("" )
4322+ if err != nil {
4323+ t .Fatal (err )
4324+ }
4325+
4326+ disk := devices .CreateDisk (controller , ds .Reference (), "test-disk-generated-uuid" )
4327+ expectedCapacity := int64 (20 * 1024 * 1024 ) // 20MB
4328+ disk .CapacityInBytes = expectedCapacity
4329+
4330+ err = testVM .AddDevice (ctx , disk )
4331+ if err != nil {
4332+ t .Fatal (err )
4333+ }
4334+
4335+ updatedDevices , err := testVM .Device (ctx )
4336+ if err != nil {
4337+ t .Fatal (err )
4338+ }
4339+ allDisks := updatedDevices .SelectByType ((* types .VirtualDisk )(nil ))
4340+ assert .Equal (t , 1 , len (allDisks ), "exactly one disk should be present" )
4341+
4342+ foundDisk := allDisks [0 ].(* types.VirtualDisk )
4343+ assert .Equal (t , expectedCapacity , foundDisk .CapacityInBytes , "disk capacity should match added disk" )
4344+
4345+ foundDiskBacking , ok := foundDisk .Backing .(* types.VirtualDiskFlatVer2BackingInfo )
4346+ assert .True (t , ok , "found disk backing should be VirtualDiskFlatVer2BackingInfo" )
4347+ assert .NotEmpty (t , foundDiskBacking .Uuid , "disk UUID should be assigned" )
4348+ _ , err = uuid .Parse (foundDiskBacking .Uuid )
4349+ assert .NoError (t , err , "assigned disk UUID should be a valid UUID" )
4350+ })
4351+
4352+ // Test 3: Add a disk with an explicit UUID - it should be preserved
4353+ t .Run ("explicit UUID" , func (t * testing.T ) {
4354+ testVM , err := createTestVM (ctx , c , folders , pool , host , ds , "explicit-uuid-vm" )
4355+ if err != nil {
4356+ t .Fatal (err )
4357+ }
4358+ defer testVM .Destroy (ctx )
4359+
4360+ devices , err := testVM .Device (ctx )
4361+ if err != nil {
4362+ t .Fatal (err )
4363+ }
4364+ controller , err := devices .FindDiskController ("" )
4365+ if err != nil {
4366+ t .Fatal (err )
4367+ }
4368+
4369+ disk := devices .CreateDisk (controller , ds .Reference (), "test-disk-explicit-uuid" )
4370+ expectedCapacity := int64 (30 * 1024 * 1024 ) // 30MB
4371+ disk .CapacityInBytes = expectedCapacity
4372+
4373+ explicitUUID := uuid .New ().String ()
4374+ diskBacking := disk .Backing .(* types.VirtualDiskFlatVer2BackingInfo )
4375+ diskBacking .Uuid = explicitUUID
4376+
4377+ err = testVM .AddDevice (ctx , disk )
4378+ if err != nil {
4379+ t .Fatal (err )
4380+ }
4381+
4382+ updatedDevices , err := testVM .Device (ctx )
4383+ if err != nil {
4384+ t .Fatal (err )
4385+ }
4386+ allDisks := updatedDevices .SelectByType ((* types .VirtualDisk )(nil ))
4387+ assert .Equal (t , 1 , len (allDisks ), "exactly one disk should be present" )
4388+
4389+ foundDisk := allDisks [0 ].(* types.VirtualDisk )
4390+ assert .Equal (t , expectedCapacity , foundDisk .CapacityInBytes , "disk capacity should match added disk" )
4391+
4392+ foundDiskBacking , ok := foundDisk .Backing .(* types.VirtualDiskFlatVer2BackingInfo )
4393+ assert .True (t , ok , "found disk backing should be VirtualDiskFlatVer2BackingInfo" )
4394+ assert .Equal (t , explicitUUID , foundDiskBacking .Uuid , "disk UUID should match the explicitly set UUID" )
4395+ })
4396+ }
4397+
4398+ func createTestVM (
4399+ ctx context.Context ,
4400+ c * govmomi.Client ,
4401+ folders * object.DatacenterFolders ,
4402+ pool * object.ResourcePool ,
4403+ host * object.HostSystem ,
4404+ ds * object.Datastore ,
4405+ vmName string ,
4406+ ) (* object.VirtualMachine , error ) {
4407+ var devices object.VirtualDeviceList
4408+
4409+ scsi , _ := devices .CreateSCSIController ("pvscsi" )
4410+ ide , _ := devices .CreateIDEController ()
4411+ cdrom , _ := devices .CreateCdrom (ide .(* types.VirtualIDEController ))
4412+
4413+ devices = append (devices , scsi , cdrom )
4414+
4415+ spec := types.VirtualMachineConfigSpec {
4416+ Name : vmName ,
4417+ GuestId : string (types .VirtualMachineGuestOsIdentifierOtherGuest ),
4418+ Files : & types.VirtualMachineFileInfo {
4419+ VmPathName : "[" + ds .Name () + "]" ,
4420+ },
4421+ }
4422+
4423+ spec .DeviceChange , _ = devices .ConfigSpec (types .VirtualDeviceConfigSpecOperationAdd )
4424+
4425+ task , err := folders .VmFolder .CreateVM (ctx , spec , pool , host )
4426+ if err != nil {
4427+ return nil , err
4428+ }
4429+
4430+ taskInfo , err := task .WaitForResult (ctx , nil )
4431+ if err != nil {
4432+ return nil , err
4433+ }
4434+
4435+ return object .NewVirtualMachine (
4436+ c .Client ,
4437+ taskInfo .Result .(types.ManagedObjectReference ),
4438+ ), nil
4439+ }
0 commit comments