Skip to content

Commit 3efce10

Browse files
committed
vcsim: add SearchIndex.FindAllByUuid() support
Signed-off-by: Bryan Venteicher <[email protected]>
1 parent 3b24a71 commit 3efce10

File tree

3 files changed

+148
-4
lines changed

3 files changed

+148
-4
lines changed

object/search_index_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,17 @@ func TestSearch(t *testing.T) {
9292
if !reflect.DeepEqual(ref, shost) {
9393
t.Errorf("%#v != %#v\n", ref, shost)
9494
}
95+
96+
shosts, err := s.FindAllByUuid(context.Background(), dc, host.Hardware.SystemInfo.Uuid, false, nil)
97+
if err != nil {
98+
t.Fatal(err)
99+
}
100+
if len(shosts) != 1 {
101+
t.Errorf("len(shosts) != 1: %d\n", len(shosts))
102+
}
103+
if !reflect.DeepEqual(ref, shosts[0]) {
104+
t.Errorf("%#v != %#v\n", ref, shosts[0])
105+
}
95106
}
96107

97108
vms, err := folders.VmFolder.Children(context.Background())
@@ -121,6 +132,17 @@ func TestSearch(t *testing.T) {
121132
t.Errorf("%#v != %#v\n", ref, svm)
122133
}
123134

135+
svms, err := s.FindAllByUuid(context.Background(), dc, vm.Config.Uuid, true, nil)
136+
if err != nil {
137+
t.Fatal(err)
138+
}
139+
if len(svms) != 1 {
140+
t.Errorf("len(svms) != 1: %d\n", len(svms))
141+
}
142+
if !reflect.DeepEqual(ref, svms[0]) {
143+
t.Errorf("%#v != %#v\n", ref, svms[0])
144+
}
145+
124146
if vm.Guest.HostName != "" {
125147
svm, err := s.FindByDnsName(context.Background(), dc, vm.Guest.HostName, true)
126148
if err != nil {

simulator/search_index.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,45 @@ func (s *SearchIndex) FindByUuid(ctx *Context, req *types.FindByUuid) soap.HasFa
156156
return body
157157
}
158158

159+
func (s *SearchIndex) FindAllByUuid(ctx *Context, req *types.FindAllByUuid) soap.HasFault {
160+
body := &methods.FindAllByUuidBody{Res: new(types.FindAllByUuidResponse)}
161+
162+
ctx.Map.m.Lock()
163+
defer ctx.Map.m.Unlock()
164+
165+
if req.VmSearch {
166+
// Find Virtual Machines using UUID
167+
for ref, obj := range ctx.Map.objects {
168+
vm, ok := asVirtualMachineMO(obj)
169+
if !ok {
170+
continue
171+
}
172+
if req.InstanceUuid != nil && *req.InstanceUuid {
173+
if vm.Config.InstanceUuid == req.Uuid {
174+
body.Res.Returnval = append(body.Res.Returnval, ref)
175+
}
176+
} else {
177+
if vm.Config.Uuid == req.Uuid {
178+
body.Res.Returnval = append(body.Res.Returnval, ref)
179+
}
180+
}
181+
}
182+
} else {
183+
// Find Host System using UUID
184+
for ref, obj := range ctx.Map.objects {
185+
host, ok := asHostSystemMO(obj)
186+
if !ok {
187+
continue
188+
}
189+
if host.Summary.Hardware.Uuid == req.Uuid {
190+
body.Res.Returnval = append(body.Res.Returnval, ref)
191+
}
192+
}
193+
}
194+
195+
return body
196+
}
197+
159198
func (s *SearchIndex) FindByDnsName(ctx *Context, req *types.FindByDnsName) soap.HasFault {
160199
body := &methods.FindByDnsNameBody{Res: new(types.FindByDnsNameResponse)}
161200

simulator/search_index_test.go

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,44 +78,92 @@ func TestSearchIndex(t *testing.T) {
7878
t.Errorf("moref mismatch %s != %s", ref, vm.Reference())
7979
}
8080

81-
ref, err = si.FindByUuid(ctx, dc, vm.Config.Uuid, true, types.NewBool(false))
81+
refs, err := si.FindAllByUuid(ctx, dc, vm.Config.Uuid, true, nil)
8282
if err != nil {
8383
t.Fatal(err)
8484
}
85+
if len(refs) != 1 {
86+
t.Errorf("len(refs) %d != 1", len(refs))
87+
}
88+
if refs[0].Reference() != vm.Reference() {
89+
t.Errorf("moref mismatch %s != %s", refs[0], vm.Reference())
90+
}
8591

92+
ref, err = si.FindByUuid(ctx, dc, vm.Config.Uuid, true, types.NewBool(false))
93+
if err != nil {
94+
t.Fatal(err)
95+
}
8696
if ref.Reference() != vm.Reference() {
8797
t.Errorf("moref mismatch %s != %s", ref, vm.Reference())
8898
}
8999

90-
ref, err = si.FindByUuid(ctx, dc, vm.Config.InstanceUuid, true, types.NewBool(true))
100+
refs, err = si.FindAllByUuid(ctx, dc, vm.Config.Uuid, true, types.NewBool(false))
91101
if err != nil {
92102
t.Fatal(err)
93103
}
104+
if len(refs) != 1 {
105+
t.Errorf("len(refs) %d != 1", len(refs))
106+
}
107+
if refs[0].Reference() != vm.Reference() {
108+
t.Errorf("moref mismatch %s != %s", refs[0], vm.Reference())
109+
}
94110

111+
ref, err = si.FindByUuid(ctx, dc, vm.Config.InstanceUuid, true, types.NewBool(true))
112+
if err != nil {
113+
t.Fatal(err)
114+
}
95115
if ref.Reference() != vm.Reference() {
96116
t.Errorf("moref mismatch %s != %s", ref, vm.Reference())
97117
}
98118

99-
ref, err = si.FindByUuid(ctx, dc, vm.Config.Uuid, false, nil)
119+
refs, err = si.FindAllByUuid(ctx, dc, vm.Config.InstanceUuid, true, types.NewBool(true))
100120
if err != nil {
101121
t.Fatal(err)
102122
}
123+
if len(refs) != 1 {
124+
t.Errorf("len(refs) %d != 1", len(refs))
125+
}
126+
if refs[0].Reference() != vm.Reference() {
127+
t.Errorf("moref mismatch %s != %s", refs[0], vm.Reference())
128+
}
103129

130+
ref, err = si.FindByUuid(ctx, dc, vm.Config.Uuid, false, nil)
131+
if err != nil {
132+
t.Fatal(err)
133+
}
104134
if ref != nil {
105135
t.Error("expected nil")
106136
}
107137

138+
refs, err = si.FindAllByUuid(ctx, dc, vm.Config.Uuid, false, nil)
139+
if err != nil {
140+
t.Fatal(err)
141+
}
142+
if refs != nil {
143+
t.Error("refs != nil")
144+
}
145+
108146
host := model.Map().Any("HostSystem").(*HostSystem)
109147

110148
ref, err = si.FindByUuid(ctx, dc, host.Summary.Hardware.Uuid, false, nil)
111149
if err != nil {
112150
t.Fatal(err)
113151
}
114-
115152
if ref.Reference() != host.Reference() {
116153
t.Errorf("moref mismatch %s != %s", ref, host.Reference())
117154
}
118155

156+
refs, err = si.FindAllByUuid(ctx, dc, host.Summary.Hardware.Uuid, false, nil)
157+
if err != nil {
158+
t.Fatal(err)
159+
}
160+
if len(refs) != 1 {
161+
t.Errorf("len(refs) %d != 1", len(refs))
162+
}
163+
if refs[0].Reference() != host.Reference() {
164+
t.Errorf("moref mismatch %s != %s", refs[0], host.Reference())
165+
}
166+
119167
rootFolder, err := finder.Folder(ctx, "/")
120168
if err != nil {
121169
t.Fatal(err)
@@ -129,6 +177,41 @@ func TestSearchIndex(t *testing.T) {
129177
if ref.Reference() != rootFolder.Reference() {
130178
t.Errorf("moref mismatch %s != %s", ref, rootFolder.Reference())
131179
}
180+
181+
{
182+
// Duplicate UUIDs to test multiple results from FindAllByUuid().
183+
184+
if len(vms) == 1 {
185+
t.Errorf("len(vms) %d == 1", len(vms))
186+
}
187+
188+
task, err := vms[1].Reconfigure(ctx, types.VirtualMachineConfigSpec{
189+
InstanceUuid: vm.Config.InstanceUuid,
190+
Uuid: vm.Config.Uuid,
191+
})
192+
if err != nil {
193+
t.Fatal(err)
194+
}
195+
if err := task.Wait(ctx); err != nil {
196+
t.Fatal(err)
197+
}
198+
199+
refs, err = si.FindAllByUuid(ctx, dc, vm.Config.InstanceUuid, true, types.NewBool(true))
200+
if err != nil {
201+
t.Fatal(err)
202+
}
203+
if len(refs) != 2 {
204+
t.Errorf("len(refs) %d != 2", len(refs))
205+
}
206+
207+
refs, err = si.FindAllByUuid(ctx, dc, vm.Config.Uuid, true, nil)
208+
if err != nil {
209+
t.Fatal(err)
210+
}
211+
if len(refs) != 2 {
212+
t.Errorf("len(refs) %d != 2", len(refs))
213+
}
214+
}
132215
}
133216
}
134217

0 commit comments

Comments
 (0)