github.com/vmware/govmomi@v0.37.1/test/functional/helper.go (about) 1 /* 2 Copyright (c) 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 functional 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/vmware/govmomi/find" 24 "github.com/vmware/govmomi/object" 25 "github.com/vmware/govmomi/property" 26 "github.com/vmware/govmomi/test" 27 "github.com/vmware/govmomi/vim25" 28 "github.com/vmware/govmomi/vim25/mo" 29 "github.com/vmware/govmomi/vim25/types" 30 ) 31 32 type Helper struct { 33 *testing.T 34 35 c *vim25.Client 36 f *find.Finder 37 fns []func() 38 } 39 40 func NewHelper(t *testing.T) *Helper { 41 h := &Helper{ 42 T: t, 43 44 c: test.NewAuthenticatedClient(t), 45 fns: make([]func(), 0), 46 } 47 48 h.f = find.NewFinder(h.c, true) 49 50 return h 51 } 52 53 func (h *Helper) Defer(fn func()) { 54 h.fns = append(h.fns, fn) 55 } 56 57 func (h *Helper) Teardown() { 58 for _, fn := range h.fns { 59 fn() 60 } 61 } 62 63 func (h *Helper) RequireVirtualCenter() { 64 var expect = "VirtualCenter" 65 var actual = h.c.ServiceContent.About.ApiType 66 if actual != expect { 67 h.Skipf("Requires %s, running against %s", expect, actual) 68 } 69 } 70 71 func (h *Helper) Datacenter() *object.Datacenter { 72 dc, err := h.f.DefaultDatacenter(context.Background()) 73 if err != nil { 74 h.Fatal(err) 75 } 76 77 h.f.SetDatacenter(dc) 78 79 return dc 80 } 81 82 func (h *Helper) DatacenterFolders() *object.DatacenterFolders { 83 df, err := h.Datacenter().Folders(context.Background()) 84 if err != nil { 85 h.Fatal(err) 86 } 87 88 return df 89 } 90 91 func (h *Helper) ComputeResource() *object.ComputeResource { 92 cr, err := h.f.DefaultComputeResource(context.Background()) 93 if err != nil { 94 h.Fatal(err) 95 } 96 97 return cr 98 } 99 100 func (h *Helper) LocalDatastores(ctx context.Context, cr *object.ComputeResource) ([]*object.Datastore, error) { 101 // List datastores for compute resource 102 dss, err := cr.Datastores(ctx) 103 if err != nil { 104 return nil, err 105 } 106 107 // Filter local datastores 108 var ldss []*object.Datastore 109 for _, ds := range dss { 110 var mds mo.Datastore 111 err = property.DefaultCollector(h.c).RetrieveOne(ctx, ds.Reference(), nil, &mds) 112 if err != nil { 113 return nil, err 114 } 115 116 switch i := mds.Info.(type) { 117 case *types.VmfsDatastoreInfo: 118 if i.Vmfs.Local != nil && *i.Vmfs.Local { 119 break 120 } 121 default: 122 continue 123 } 124 125 ds.InventoryPath = mds.Name 126 ldss = append(ldss, ds) 127 } 128 129 return ldss, nil 130 }