github.com/vmware/govmomi@v0.43.0/property/wait_test.go (about) 1 /* 2 Copyright (c) 2019-2024 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 property_test 18 19 import ( 20 "context" 21 "log" 22 "testing" 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 // Test that task.Wait() propagates MissingSet errors 33 func TestWaitPermissionFault(t *testing.T) { 34 ctx := context.Background() 35 36 model := simulator.ESX() 37 38 defer model.Remove() 39 err := model.Create() 40 if err != nil { 41 log.Fatal(err) 42 } 43 44 s := model.Service.NewServer() 45 defer s.Close() 46 47 c, _ := govmomi.NewClient(ctx, s.URL, true) 48 49 pc := new(propCollForWaitForPermsTest) 50 pc.Self = model.ServiceContent.PropertyCollector 51 simulator.Map.Put(pc) 52 53 dm := object.NewVirtualDiskManager(c.Client) 54 55 spec := &types.FileBackedVirtualDiskSpec{ 56 VirtualDiskSpec: types.VirtualDiskSpec{ 57 AdapterType: string(types.VirtualDiskAdapterTypeLsiLogic), 58 DiskType: string(types.VirtualDiskTypeThin), 59 }, 60 CapacityKb: 1024 * 1024, 61 } 62 63 name := "[LocalDS_0] disk1.vmdk" 64 65 task, err := dm.CreateVirtualDisk(ctx, name, nil, spec) 66 if err != nil { 67 t.Fatal(err) 68 } 69 70 err = task.Wait(ctx) 71 if err == nil { 72 t.Fatal("expected error") 73 } 74 75 if !soap.IsVimFault(err) { 76 t.Fatal("expected vim fault") 77 } 78 79 fault, ok := soap.ToVimFault(err).(*types.NoPermission) 80 if !ok { 81 t.Fatalf("unexpected vim fault: %T", fault) 82 } 83 } 84 85 // Test that task.WaitEx() propagates MissingSet errors 86 func TestWaitExPermissionFault(t *testing.T) { 87 ctx := context.Background() 88 89 model := simulator.ESX() 90 91 defer model.Remove() 92 err := model.Create() 93 if err != nil { 94 log.Fatal(err) 95 } 96 97 s := model.Service.NewServer() 98 defer s.Close() 99 100 c, _ := govmomi.NewClient(ctx, s.URL, true) 101 102 pc := new(propCollForWaitForPermsTest) 103 pc.Self = model.ServiceContent.PropertyCollector 104 simulator.Map.Put(pc) 105 106 dm := object.NewVirtualDiskManager(c.Client) 107 108 spec := &types.FileBackedVirtualDiskSpec{ 109 VirtualDiskSpec: types.VirtualDiskSpec{ 110 AdapterType: string(types.VirtualDiskAdapterTypeLsiLogic), 111 DiskType: string(types.VirtualDiskTypeThin), 112 }, 113 CapacityKb: 1024 * 1024, 114 } 115 116 name := "[LocalDS_0] disk1.vmdk" 117 118 task, err := dm.CreateVirtualDisk(ctx, name, nil, spec) 119 if err != nil { 120 t.Fatal(err) 121 } 122 123 err = task.WaitEx(ctx) 124 if err == nil { 125 t.Fatal("expected error") 126 } 127 128 if !soap.IsVimFault(err) { 129 t.Fatal("expected vim fault") 130 } 131 132 fault, ok := soap.ToVimFault(err).(*types.NoPermission) 133 if !ok { 134 t.Fatalf("unexpected vim fault: %T", fault) 135 } 136 } 137 138 type propCollForWaitForPermsTest struct { 139 simulator.PropertyCollector 140 } 141 142 // CreatePropertyCollector overrides the vcsim impl to return this test's PC impl 143 func (pc *propCollForWaitForPermsTest) CreatePropertyCollector( 144 ctx *simulator.Context, 145 c *types.CreatePropertyCollector) soap.HasFault { 146 147 return &methods.CreatePropertyCollectorBody{ 148 Res: &types.CreatePropertyCollectorResponse{ 149 Returnval: ctx.Session.Put(new(propCollForWaitForPermsTest)).Reference(), 150 }, 151 } 152 } 153 154 // WaitForUpdatesEx overrides the vcsim impl to inject a fault via MissingSet 155 func (pc *propCollForWaitForPermsTest) WaitForUpdatesEx( 156 ctx *simulator.Context, 157 r *types.WaitForUpdatesEx) soap.HasFault { 158 159 filter := ctx.Session.Get(pc.Filter[0]).(*simulator.PropertyFilter) 160 161 if r.Version != "" { 162 // Client should fail on the first response w/ MissingSet. 163 // This ensures we don't get into a tight loop if that doesn't happen. 164 select {} 165 } 166 167 return &methods.WaitForUpdatesExBody{ 168 Res: &types.WaitForUpdatesExResponse{ 169 Returnval: &types.UpdateSet{ 170 Version: "-", 171 FilterSet: []types.PropertyFilterUpdate{{ 172 Filter: filter.Reference(), 173 ObjectSet: []types.ObjectUpdate{{ 174 Kind: types.ObjectUpdateKindEnter, 175 Obj: filter.Spec.ObjectSet[0].Obj, 176 MissingSet: []types.MissingProperty{{ 177 Path: "info", 178 Fault: types.LocalizedMethodFault{ 179 Fault: new(types.NoPermission), 180 }, 181 }}, 182 }}, 183 }}, 184 }, 185 }, 186 } 187 }