github.com/vmware/govmomi@v0.43.0/simulator/search_index_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 18 19 import ( 20 "context" 21 "strings" 22 "testing" 23 24 "github.com/vmware/govmomi" 25 "github.com/vmware/govmomi/find" 26 "github.com/vmware/govmomi/object" 27 "github.com/vmware/govmomi/vim25/soap" 28 "github.com/vmware/govmomi/vim25/types" 29 ) 30 31 func TestSearchIndex(t *testing.T) { 32 ctx := context.Background() 33 34 for _, model := range []*Model{ESX(), VPX()} { 35 defer model.Remove() 36 err := model.Create() 37 if err != nil { 38 t.Fatal(err) 39 } 40 41 s := model.Service.NewServer() 42 defer s.Close() 43 44 c, err := govmomi.NewClient(ctx, s.URL, true) 45 if err != nil { 46 t.Fatal(err) 47 } 48 49 finder := find.NewFinder(c.Client, false) 50 dc, err := finder.DefaultDatacenter(ctx) 51 if err != nil { 52 t.Fatal(err) 53 } 54 55 finder.SetDatacenter(dc) 56 57 vms, err := finder.VirtualMachineList(ctx, "*") 58 if err != nil { 59 t.Fatal(err) 60 } 61 62 vm := Map.Get(vms[0].Reference()).(*VirtualMachine) 63 64 si := object.NewSearchIndex(c.Client) 65 66 ref, err := si.FindByDatastorePath(ctx, dc, vm.Config.Files.VmPathName) 67 if err != nil { 68 t.Fatal(err) 69 } 70 71 if ref.Reference() != vm.Reference() { 72 t.Errorf("moref mismatch %s != %s", ref, vm.Reference()) 73 } 74 75 ref, err = si.FindByDatastorePath(ctx, dc, vm.Config.Files.VmPathName+"enoent") 76 if err != nil { 77 t.Fatal(err) 78 } 79 80 if ref != nil { 81 t.Errorf("ref=%s", ref) 82 } 83 84 ref, err = si.FindByUuid(ctx, dc, vm.Config.Uuid, true, nil) 85 if err != nil { 86 t.Fatal(err) 87 } 88 89 if ref.Reference() != vm.Reference() { 90 t.Errorf("moref mismatch %s != %s", ref, vm.Reference()) 91 } 92 93 ref, err = si.FindByUuid(ctx, dc, vm.Config.Uuid, true, types.NewBool(false)) 94 if err != nil { 95 t.Fatal(err) 96 } 97 98 if ref.Reference() != vm.Reference() { 99 t.Errorf("moref mismatch %s != %s", ref, vm.Reference()) 100 } 101 102 ref, err = si.FindByUuid(ctx, dc, vm.Config.InstanceUuid, true, types.NewBool(true)) 103 if err != nil { 104 t.Fatal(err) 105 } 106 107 if ref.Reference() != vm.Reference() { 108 t.Errorf("moref mismatch %s != %s", ref, vm.Reference()) 109 } 110 111 ref, err = si.FindByUuid(ctx, dc, vm.Config.Uuid, false, nil) 112 if err != nil { 113 t.Fatal(err) 114 } 115 116 if ref != nil { 117 t.Error("expected nil") 118 } 119 120 host := Map.Any("HostSystem").(*HostSystem) 121 122 ref, err = si.FindByUuid(ctx, dc, host.Summary.Hardware.Uuid, false, nil) 123 if err != nil { 124 t.Fatal(err) 125 } 126 127 if ref.Reference() != host.Reference() { 128 t.Errorf("moref mismatch %s != %s", ref, host.Reference()) 129 } 130 131 rootFolder, err := finder.Folder(ctx, "/") 132 if err != nil { 133 t.Fatal(err) 134 } 135 136 ref, err = si.FindByInventoryPath(ctx, "/") 137 if err != nil { 138 t.Fatal(err) 139 } 140 141 if ref.Reference() != rootFolder.Reference() { 142 t.Errorf("moref mismatch %s != %s", ref, rootFolder.Reference()) 143 } 144 } 145 } 146 147 func TestSearchIndexFindChild(t *testing.T) { 148 ctx := context.Background() 149 150 model := VPX() 151 model.Pool = 3 152 153 defer model.Remove() 154 err := model.Create() 155 if err != nil { 156 t.Fatal(err) 157 } 158 159 s := model.Service.NewServer() 160 defer s.Close() 161 162 c, err := govmomi.NewClient(ctx, s.URL, true) 163 if err != nil { 164 t.Fatal(err) 165 } 166 167 si := object.NewSearchIndex(c.Client) 168 169 tests := [][]string{ 170 // Datacenter -> host Folder -> Cluster -> HostSystem 171 {"DC0", "host", "DC0_C0", "DC0_C0_H0"}, 172 // Datacenter -> host Folder -> ComputeResource -> HostSystem 173 {"DC0", "host", "DC0_H0", "DC0_H0"}, 174 // Datacenter -> host Folder -> Cluster -> ResourcePool -> ResourcePool 175 {"DC0", "host", "DC0_C0", "Resources", "DC0_C0_RP1"}, 176 // Datacenter -> host Folder -> Cluster -> ResourcePool -> VirtualMachine 177 {"DC0", "host", "DC0_C0", "Resources", "DC0_C0_RP1", "DC0_C0_RP1_VM0"}, 178 // Datacenter -> vm Folder -> VirtualMachine 179 {"DC0", "vm", "DC0_C0_RP1_VM0"}, 180 } 181 182 root := c.ServiceContent.RootFolder 183 184 for _, path := range tests { 185 parent := root 186 ipath := []string{""} 187 188 for _, name := range path { 189 ref, err := si.FindChild(ctx, parent, name) 190 if err != nil { 191 t.Fatal(err) 192 } 193 194 if ref == nil { 195 t.Fatalf("failed to match %s using %s", name, parent) 196 } 197 198 parent = ref.Reference() 199 200 ipath = append(ipath, name) 201 202 iref, err := si.FindByInventoryPath(ctx, strings.Join(ipath, "/")) 203 if err != nil { 204 t.Fatal(err) 205 } 206 207 if iref.Reference() != ref.Reference() { 208 t.Errorf("%s != %s", iref, ref) 209 } 210 } 211 } 212 213 ref, err := si.FindChild(ctx, root, "enoent") 214 if err != nil { 215 t.Fatal(err) 216 } 217 218 if ref != nil { 219 t.Error("unexpected match") 220 } 221 222 root.Value = "enoent" 223 _, err = si.FindChild(ctx, root, "enoent") 224 if err == nil { 225 t.Error("expected error") 226 } 227 228 if _, ok := soap.ToSoapFault(err).VimFault().(types.ManagedObjectNotFound); !ok { 229 t.Error("expected ManagedObjectNotFound fault") 230 } 231 232 for _, path := range []string{"", "/enoent"} { 233 ref, err := si.FindByInventoryPath(ctx, path) 234 if err != nil { 235 t.Fatal(err) 236 } 237 238 if ref != nil { 239 t.Error("unexpected match") 240 } 241 } 242 }