github.com/vmware/govmomi@v0.51.0/object/virtual_machine_test.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package object 6 7 import ( 8 "testing" 9 "time" 10 11 "github.com/vmware/govmomi/vim25/types" 12 ) 13 14 // VirtualMachine should implement the Reference interface. 15 var _ Reference = VirtualMachine{} 16 17 // pretty.Printf generated 18 var snapshot = &types.VirtualMachineSnapshotInfo{ 19 DynamicData: types.DynamicData{}, 20 CurrentSnapshot: &types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-11"}, 21 RootSnapshotList: []types.VirtualMachineSnapshotTree{ 22 { 23 DynamicData: types.DynamicData{}, 24 Snapshot: types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-1"}, 25 Vm: types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"}, 26 Name: "root", 27 Description: "", 28 Id: 1, 29 CreateTime: time.Now(), 30 State: "poweredOn", 31 Quiesced: false, 32 BackupManifest: "", 33 ChildSnapshotList: []types.VirtualMachineSnapshotTree{ 34 { 35 DynamicData: types.DynamicData{}, 36 Snapshot: types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-2"}, 37 Vm: types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"}, 38 Name: "child", 39 Description: "", 40 Id: 2, 41 CreateTime: time.Now(), 42 State: "poweredOn", 43 Quiesced: false, 44 BackupManifest: "", 45 ChildSnapshotList: nil, 46 ReplaySupported: types.NewBool(false), 47 }, 48 { 49 DynamicData: types.DynamicData{}, 50 Snapshot: types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-3"}, 51 Vm: types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"}, 52 Name: "child", 53 Description: "", 54 Id: 3, 55 CreateTime: time.Now(), 56 State: "poweredOn", 57 Quiesced: false, 58 BackupManifest: "", 59 ChildSnapshotList: []types.VirtualMachineSnapshotTree{ 60 { 61 DynamicData: types.DynamicData{}, 62 Snapshot: types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-9"}, 63 Vm: types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"}, 64 Name: "grandkid", 65 Description: "", 66 Id: 9, 67 CreateTime: time.Now(), 68 State: "poweredOn", 69 Quiesced: false, 70 BackupManifest: "", 71 ChildSnapshotList: []types.VirtualMachineSnapshotTree{ 72 { 73 DynamicData: types.DynamicData{}, 74 Snapshot: types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-10"}, 75 Vm: types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"}, 76 Name: "great", 77 Description: "", 78 Id: 10, 79 CreateTime: time.Now(), 80 State: "poweredOn", 81 Quiesced: false, 82 BackupManifest: "", 83 ChildSnapshotList: nil, 84 ReplaySupported: types.NewBool(false), 85 }, 86 }, 87 ReplaySupported: types.NewBool(false), 88 }, 89 }, 90 ReplaySupported: types.NewBool(false), 91 }, 92 { 93 DynamicData: types.DynamicData{}, 94 Snapshot: types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-5"}, 95 Vm: types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"}, 96 Name: "voodoo", 97 Description: "", 98 Id: 5, 99 CreateTime: time.Now(), 100 State: "poweredOn", 101 Quiesced: false, 102 BackupManifest: "", 103 ChildSnapshotList: []types.VirtualMachineSnapshotTree{ 104 { 105 DynamicData: types.DynamicData{}, 106 Snapshot: types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-11"}, 107 Vm: types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"}, 108 Name: "child", 109 Description: "", 110 Id: 11, 111 CreateTime: time.Now(), 112 State: "poweredOn", 113 Quiesced: false, 114 BackupManifest: "", 115 ChildSnapshotList: nil, 116 ReplaySupported: types.NewBool(false), 117 }, 118 }, 119 ReplaySupported: types.NewBool(false), 120 }, 121 { 122 DynamicData: types.DynamicData{}, 123 Snapshot: types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-6"}, 124 Vm: types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"}, 125 Name: "better", 126 Description: "", 127 Id: 6, 128 CreateTime: time.Now(), 129 State: "poweredOn", 130 Quiesced: false, 131 BackupManifest: "", 132 ChildSnapshotList: []types.VirtualMachineSnapshotTree{ 133 { 134 DynamicData: types.DynamicData{}, 135 Snapshot: types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-7"}, 136 Vm: types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"}, 137 Name: "best", 138 Description: "", 139 Id: 7, 140 CreateTime: time.Now(), 141 State: "poweredOn", 142 Quiesced: false, 143 BackupManifest: "", 144 ChildSnapshotList: []types.VirtualMachineSnapshotTree{ 145 { 146 DynamicData: types.DynamicData{}, 147 Snapshot: types.ManagedObjectReference{Type: "VirtualMachineSnapshot", Value: "2-snapshot-8"}, 148 Vm: types.ManagedObjectReference{Type: "VirtualMachine", Value: "2"}, 149 Name: "betterer", 150 Description: "", 151 Id: 8, 152 CreateTime: time.Now(), 153 State: "poweredOn", 154 Quiesced: false, 155 BackupManifest: "", 156 ChildSnapshotList: nil, 157 ReplaySupported: types.NewBool(false), 158 }, 159 }, 160 ReplaySupported: types.NewBool(false), 161 }, 162 }, 163 ReplaySupported: types.NewBool(false), 164 }, 165 }, 166 ReplaySupported: types.NewBool(false), 167 }, 168 }, 169 } 170 171 func TestVirtualMachineSnapshotMap(t *testing.T) { 172 m := make(snapshotMap) 173 m.add("", snapshot.RootSnapshotList) 174 175 tests := []struct { 176 name string 177 expect int 178 }{ 179 {"enoent", 0}, 180 {"root", 1}, 181 {"child", 3}, 182 {"root/child", 2}, 183 {"root/voodoo/child", 1}, 184 {"2-snapshot-6", 1}, 185 } 186 187 for _, test := range tests { 188 s := m[test.name] 189 190 if len(s) != test.expect { 191 t.Errorf("%s: %d != %d", test.name, len(s), test.expect) 192 } 193 } 194 } 195 196 func TestDiskFileOperation(t *testing.T) { 197 backing := &types.VirtualDiskFlatVer2BackingInfo{ 198 VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{ 199 FileName: "[datastore1] data/disk1.vmdk", 200 }, 201 Parent: nil, 202 } 203 204 parent := &types.VirtualDiskFlatVer2BackingInfo{ 205 VirtualDeviceFileBackingInfo: types.VirtualDeviceFileBackingInfo{ 206 FileName: "[datastore1] data/parent.vmdk", 207 }, 208 } 209 210 disk := &types.VirtualDisk{ 211 VirtualDevice: types.VirtualDevice{ 212 Backing: backing, 213 }, 214 } 215 216 op := types.VirtualDeviceConfigSpecOperationAdd 217 fop := types.VirtualDeviceConfigSpecFileOperationCreate 218 219 res := diskFileOperation(op, fop, disk) 220 if res != "" { 221 t.Errorf("res=%s", res) 222 } 223 224 disk.CapacityInKB = 1 225 res = diskFileOperation(op, fop, disk) 226 if res != types.VirtualDeviceConfigSpecFileOperationCreate { 227 t.Errorf("res=%s", res) 228 } 229 230 disk.CapacityInKB = 0 231 disk.CapacityInBytes = 1 232 res = diskFileOperation(op, fop, disk) 233 if res != types.VirtualDeviceConfigSpecFileOperationCreate { 234 t.Errorf("res=%s", res) 235 } 236 237 disk.CapacityInBytes = 0 238 backing.Parent = parent 239 res = diskFileOperation(op, fop, disk) 240 if res != types.VirtualDeviceConfigSpecFileOperationCreate { 241 t.Errorf("res=%s", res) 242 } 243 }