github.com/vmware/govmomi@v0.43.0/simulator/example_extend_test.go (about) 1 /* 2 Copyright (c) 2017 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 simulator_test 18 19 import ( 20 "context" 21 "fmt" 22 "log" 23 24 "github.com/vmware/govmomi" 25 "github.com/vmware/govmomi/object" 26 "github.com/vmware/govmomi/simulator" 27 "github.com/vmware/govmomi/vim25/methods" 28 "github.com/vmware/govmomi/vim25/soap" 29 "github.com/vmware/govmomi/vim25/types" 30 ) 31 32 // BusyVM changes the behavior of simulator.VirtualMachine 33 type BusyVM struct { 34 *simulator.VirtualMachine 35 } 36 37 // Override simulator.VirtualMachine.PowerOffVMTask to inject faults 38 func (vm *BusyVM) PowerOffVMTask(ctx *simulator.Context, req *types.PowerOffVM_Task) soap.HasFault { 39 task := simulator.CreateTask(req.This, "powerOff", func(*simulator.Task) (types.AnyType, types.BaseMethodFault) { 40 return nil, &types.TaskInProgress{} 41 }) 42 43 return &methods.PowerOffVM_TaskBody{ 44 Res: &types.PowerOffVM_TaskResponse{ 45 Returnval: task.Run(ctx), 46 }, 47 } 48 } 49 50 // Add AcquireTicket method, not implemented by simulator.VirtualMachine 51 func (vm *BusyVM) AcquireTicket(req *types.AcquireTicket) soap.HasFault { 52 body := &methods.AcquireTicketBody{} 53 54 if req.TicketType != "mks" { 55 body.Fault_ = simulator.Fault("", &types.InvalidArgument{}) 56 } 57 58 body.Res = &types.AcquireTicketResponse{ 59 Returnval: types.VirtualMachineTicket{ 60 Ticket: "welcome 2 the machine", 61 }, 62 } 63 64 return body 65 } 66 67 // Example of extending the simulator to inject faults. 68 func Example() { 69 ctx := context.Background() 70 model := simulator.VPX() 71 72 defer model.Remove() 73 _ = model.Create() 74 75 s := model.Service.NewServer() 76 defer s.Close() 77 78 // NewClient connects to s.URL over https and invokes 2 SOAP methods (RetrieveServiceContent + Login) 79 c, _ := govmomi.NewClient(ctx, s.URL, true) 80 81 // Shortcut to choose any VM, rather than using the more verbose Finder or ContainerView. 82 obj := simulator.Map.Any("VirtualMachine").(*simulator.VirtualMachine) 83 // Validate VM is powered on 84 if obj.Runtime.PowerState != "poweredOn" { 85 log.Fatal(obj.Runtime.PowerState) 86 } 87 88 // Wrap the existing vm object, using the same vm.Self (ManagedObjectReference) value as the Map key. 89 simulator.Map.Put(&BusyVM{obj}) 90 91 vm := object.NewVirtualMachine(c.Client, obj.Reference()) 92 93 // Start a PowerOff task using the SOAP client. 94 task, _ := vm.PowerOff(ctx) 95 96 // Wait for task completion, expecting failure. 97 err := task.Wait(ctx) 98 if err == nil { 99 log.Fatal("expected error") 100 } 101 102 // Invalid ticket type, expecting failure. 103 _, err = vm.AcquireTicket(ctx, "pks") 104 if err == nil { 105 log.Fatal("expected error") 106 } 107 108 mks, _ := vm.AcquireTicket(ctx, "mks") 109 110 fmt.Println(mks.Ticket, obj.Runtime.PowerState) 111 // Output: welcome 2 the machine poweredOn 112 }