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