github.com/vmware/govmomi@v0.37.1/vim25/types/types_test.go (about) 1 /* 2 Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved. 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 types 18 19 import ( 20 "bytes" 21 "reflect" 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 26 "github.com/vmware/govmomi/vim25/xml" 27 ) 28 29 func TestManagedObjectReference(t *testing.T) { 30 31 testCases := []struct { 32 name string 33 obj ManagedObjectReference 34 expXML string 35 expJSON string 36 }{ 37 { 38 name: "with server GUID", 39 obj: ManagedObjectReference{ 40 Type: "fake", 41 Value: "fake", 42 ServerGUID: "fake", 43 }, 44 expXML: `<ManagedObjectReference type="fake" serverGuid="fake">fake</ManagedObjectReference>`, 45 expJSON: `{"_typeName":"ManagedObjectReference","type":"fake","value":"fake","serverGuid":"fake"}`, 46 }, 47 { 48 name: "sans server GUID", 49 obj: ManagedObjectReference{ 50 Type: "fake", 51 Value: "fake", 52 }, 53 expXML: `<ManagedObjectReference type="fake">fake</ManagedObjectReference>`, 54 expJSON: `{"_typeName":"ManagedObjectReference","type":"fake","value":"fake"}`, 55 }, 56 } 57 58 for i := range testCases { 59 tc := testCases[i] // capture the test case 60 61 t.Run(tc.name, func(t *testing.T) { 62 t.Run("xml", func(t *testing.T) { 63 act, err := xml.Marshal(tc.obj) 64 if err != nil { 65 t.Fatal(err) 66 } 67 if e, a := tc.expXML, string(act); e != a { 68 t.Fatalf("failed to marshal MoRef to XML: exp=%s, act=%s", e, a) 69 } 70 }) 71 t.Run("json", func(t *testing.T) { 72 var w bytes.Buffer 73 enc := NewJSONEncoder(&w) 74 if err := enc.Encode(tc.obj); err != nil { 75 t.Fatal(err) 76 } 77 assert.JSONEq(t, tc.expJSON, w.String(), 78 "failed to marshal MoRef to JSON") 79 }) 80 }) 81 } 82 } 83 84 func TestVirtualMachineConfigSpec(t *testing.T) { 85 spec := VirtualMachineConfigSpec{ 86 Name: "vm-001", 87 GuestId: "otherGuest", 88 Files: &VirtualMachineFileInfo{VmPathName: "[datastore1]"}, 89 NumCPUs: 1, 90 MemoryMB: 128, 91 DeviceChange: []BaseVirtualDeviceConfigSpec{ 92 &VirtualDeviceConfigSpec{ 93 Operation: VirtualDeviceConfigSpecOperationAdd, 94 Device: &VirtualLsiLogicController{VirtualSCSIController{ 95 SharedBus: VirtualSCSISharingNoSharing, 96 VirtualController: VirtualController{ 97 BusNumber: 0, 98 VirtualDevice: VirtualDevice{ 99 Key: 1000, 100 }, 101 }, 102 }}, 103 }, 104 &VirtualDeviceConfigSpec{ 105 Operation: VirtualDeviceConfigSpecOperationAdd, 106 FileOperation: VirtualDeviceConfigSpecFileOperationCreate, 107 Device: &VirtualDisk{ 108 VirtualDevice: VirtualDevice{ 109 Key: 0, 110 ControllerKey: 1000, 111 UnitNumber: new(int32), // zero default value 112 Backing: &VirtualDiskFlatVer2BackingInfo{ 113 DiskMode: string(VirtualDiskModePersistent), 114 ThinProvisioned: NewBool(true), 115 VirtualDeviceFileBackingInfo: VirtualDeviceFileBackingInfo{ 116 FileName: "[datastore1]", 117 }, 118 }, 119 }, 120 CapacityInKB: 4000000, 121 }, 122 }, 123 &VirtualDeviceConfigSpec{ 124 Operation: VirtualDeviceConfigSpecOperationAdd, 125 Device: &VirtualE1000{VirtualEthernetCard{ 126 VirtualDevice: VirtualDevice{ 127 Key: 0, 128 DeviceInfo: &Description{ 129 Label: "Network Adapter 1", 130 Summary: "VM Network", 131 }, 132 Backing: &VirtualEthernetCardNetworkBackingInfo{ 133 VirtualDeviceDeviceBackingInfo: VirtualDeviceDeviceBackingInfo{ 134 DeviceName: "VM Network", 135 }, 136 }, 137 }, 138 AddressType: string(VirtualEthernetCardMacTypeGenerated), 139 }}, 140 }, 141 }, 142 ExtraConfig: []BaseOptionValue{ 143 &OptionValue{Key: "bios.bootOrder", Value: "ethernet0"}, 144 }, 145 } 146 147 _, err := xml.MarshalIndent(spec, "", " ") 148 if err != nil { 149 t.Fatal(err) 150 } 151 } 152 153 func TestVirtualMachineAffinityInfo(t *testing.T) { 154 // See https://github.com/vmware/govmomi/issues/1008 155 in := VirtualMachineAffinityInfo{ 156 AffinitySet: []int32{0, 1, 2, 3}, 157 } 158 159 b, err := xml.Marshal(in) 160 if err != nil { 161 t.Fatal(err) 162 } 163 164 var out VirtualMachineAffinityInfo 165 166 err = xml.Unmarshal(b, &out) 167 if err != nil { 168 t.Fatal(err) 169 } 170 171 if !reflect.DeepEqual(in, out) { 172 t.Errorf("%#v vs %#v", in, out) 173 } 174 }