Skip to content

Commit

Permalink
Merge pull request #3578 from dougm/vcsim-object-update
Browse files Browse the repository at this point in the history
vcsim: add *Context param to UpdateObject interface method
  • Loading branch information
dougm authored Oct 7, 2024
2 parents 3177dc0 + 6267644 commit 131e69e
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 89 deletions.
32 changes: 21 additions & 11 deletions object/vm_guest_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2019 VMware, Inc. All Rights Reserved.
Copyright (c) 2019-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -37,8 +37,21 @@ func TestVirtualMachineWaitForIP(t *testing.T) {
return err
}

obj := simulator.Map.Get(vm.Reference()).(*simulator.VirtualMachine)
obj.Guest.IpAddress = "fe80::250:56ff:fe97:2458"
reconfig := func(ip string) error {
task, err := vm.Reconfigure(ctx, types.VirtualMachineConfigSpec{
ExtraConfig: []types.BaseOptionValue{
&types.OptionValue{Key: "SET.guest.ipAddress", Value: ip},
},
})
if err != nil {
return err
}
return task.Wait(ctx)
}

if err := reconfig("fe80::250:56ff:fe97:2458"); err != nil {
return err
}

ip, err := vm.WaitForIP(ctx)
if err != nil {
Expand All @@ -49,20 +62,17 @@ func TestVirtualMachineWaitForIP(t *testing.T) {
t.Errorf("expected v6 ip, but %q is v4", ip)
}

delay := time.Second
delay := time.Second / 2
var wg sync.WaitGroup

wg.Add(1)
go func() {
defer wg.Done()
t.Logf("delaying map update for %v", delay)
time.Sleep(delay)

simulator.Map.WithLock(simulator.SpoofContext(), obj.Reference(), func() {
simulator.Map.Update(obj, []types.PropertyChange{
{Name: "guest.ipAddress", Val: "10.0.0.1"},
})
})
if err := reconfig("10.0.0.1"); err != nil {
t.Logf("reconfig error: %s", err)
}
}()

ip, err = vm.WaitForIP(ctx, true)
Expand Down
12 changes: 6 additions & 6 deletions simulator/container_virtual_machine.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2023-2023 VMware, Inc. All Rights Reserved.
Copyright (c) 2023-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -193,7 +193,7 @@ func (svm *simVM) start(ctx *Context) error {
if err != nil {
log.Printf("%s %s: %s", svm.vm.Name, "start", err)
} else {
ctx.Map.Update(svm.vm, toolsRunning)
ctx.Update(svm.vm, toolsRunning)
}

return err
Expand Down Expand Up @@ -287,7 +287,7 @@ func (svm *simVM) start(ctx *Context) error {
return err
}

ctx.Map.Update(svm.vm, toolsRunning)
ctx.Update(svm.vm, toolsRunning)

svm.vm.logPrintf("%s: %s", args, svm.c.id)

Expand Down Expand Up @@ -345,7 +345,7 @@ func (svm *simVM) stop(ctx *Context) error {
return err
}

ctx.Map.Update(svm.vm, toolsNotRunning)
ctx.Update(svm.vm, toolsNotRunning)

return nil
}
Expand All @@ -363,7 +363,7 @@ func (svm *simVM) pause(ctx *Context) error {
return err
}

ctx.Map.Update(svm.vm, toolsNotRunning)
ctx.Update(svm.vm, toolsNotRunning)

return nil
}
Expand All @@ -381,7 +381,7 @@ func (svm *simVM) restart(ctx *Context) error {
return err
}

ctx.Map.Update(svm.vm, toolsRunning)
ctx.Update(svm.vm, toolsRunning)

return nil
}
Expand Down
16 changes: 8 additions & 8 deletions simulator/dvs.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2017 VMware, Inc. All Rights Reserved.
Copyright (c) 2017-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -166,7 +166,7 @@ func (s *DistributedVirtualSwitch) AddDVPortgroupTask(ctx *Context, c *types.Add

parent := ctx.Map.Get(*host.HostSystem.Parent)
computeNetworks := append(hostParent(&host.HostSystem).Network, pg.Reference())
ctx.Map.Update(parent, []types.PropertyChange{
ctx.Update(parent, []types.PropertyChange{
{Name: "network", Val: computeNetworks},
})
}
Expand All @@ -176,7 +176,7 @@ func (s *DistributedVirtualSwitch) AddDVPortgroupTask(ctx *Context, c *types.Add
})
}

ctx.Map.Update(s, []types.PropertyChange{
ctx.Update(s, []types.PropertyChange{
{Name: "portgroup", Val: portgroups},
{Name: "summary.portgroupName", Val: portgroupNames},
})
Expand Down Expand Up @@ -212,7 +212,7 @@ func (s *DistributedVirtualSwitch) ReconfigureDvsTask(ctx *Context, req *types.R
}

hostNetworks := append(host.Network, s.Portgroup...)
ctx.Map.Update(host, []types.PropertyChange{
ctx.Update(host, []types.PropertyChange{
{Name: "network", Val: hostNetworks},
})
members = append(members, member.Host)
Expand All @@ -224,14 +224,14 @@ func (s *DistributedVirtualSwitch) ReconfigureDvsTask(ctx *Context, req *types.R
pgs = append(pgs, ref)

pgHosts := append(pg.Host, member.Host)
ctx.Map.Update(pg, []types.PropertyChange{
ctx.Update(pg, []types.PropertyChange{
{Name: "host", Val: pgHosts},
})

cr := hostParent(&host.HostSystem)
if FindReference(cr.Network, ref) == nil {
computeNetworks := append(cr.Network, ref)
ctx.Map.Update(parent, []types.PropertyChange{
ctx.Update(parent, []types.PropertyChange{
{Name: "network", Val: computeNetworks},
})
}
Expand Down Expand Up @@ -263,7 +263,7 @@ func (s *DistributedVirtualSwitch) ReconfigureDvsTask(ctx *Context, req *types.R
}
}

ctx.Map.Update(s, []types.PropertyChange{
ctx.Update(s, []types.PropertyChange{
{Name: "summary.hostMember", Val: members},
})

Expand Down
6 changes: 3 additions & 3 deletions simulator/entity.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
Copyright (c) 2017 VMware, Inc. All Rights Reserved.
Copyright (c) 2017-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
Expand Down Expand Up @@ -34,7 +34,7 @@ func RenameTask(ctx *Context, e mo.Entity, r *types.Rename_Task, dup ...bool) so
}
}

ctx.Map.Update(e, []types.PropertyChange{{Name: "name", Val: r.NewName}})
ctx.Update(e, []types.PropertyChange{{Name: "name", Val: r.NewName}})

return nil, nil
})
Expand Down
2 changes: 1 addition & 1 deletion simulator/event_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (m *EventManager) PostEvent(ctx *Context, req *types.PostEvent) soap.HasFau
ctx.WithLock(c, func() {
if c.eventMatches(ctx, req.EventToPost) {
pushHistory(c.page, req.EventToPost)
ctx.Map.Update(c, []types.PropertyChange{{Name: "latestPage", Val: c.GetLatestPage()}})
ctx.Update(c, []types.PropertyChange{{Name: "latestPage", Val: c.GetLatestPage()}})
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions simulator/extension_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (m *ExtensionManager) RegisterExtension(ctx *Context, req *types.RegisterEx
m.ExtensionList = append(m.ExtensionList, req.Extension)

f := mo.Field{Path: "extensionList", Key: req.Extension.Key}
ctx.Map.Update(m, []types.PropertyChange{
ctx.Update(m, []types.PropertyChange{
{Name: f.Path, Val: m.ExtensionList},
{Name: f.String(), Val: req.Extension, Op: types.PropertyChangeOpAdd},
})
Expand All @@ -114,7 +114,7 @@ func (m *ExtensionManager) UnregisterExtension(ctx *Context, req *types.Unregist
m.ExtensionList = append(m.ExtensionList[:i], m.ExtensionList[i+1:]...)

f := mo.Field{Path: "extensionList", Key: req.ExtensionKey}
ctx.Map.Update(m, []types.PropertyChange{
ctx.Update(m, []types.PropertyChange{
{Name: f.Path, Val: m.ExtensionList},
{Name: f.String(), Op: types.PropertyChangeOpRemove},
})
Expand All @@ -137,7 +137,7 @@ func (m *ExtensionManager) UpdateExtension(ctx *Context, req *types.UpdateExtens
m.ExtensionList[i] = req.Extension

f := mo.Field{Path: "extensionList", Key: req.Extension.Key}
ctx.Map.Update(m, []types.PropertyChange{
ctx.Update(m, []types.PropertyChange{
{Name: f.Path, Val: m.ExtensionList},
{Name: f.String(), Val: req.Extension},
})
Expand Down
4 changes: 2 additions & 2 deletions simulator/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func (f *Folder) CreateDatacenter(ctx *Context, c *types.CreateDatacenter) soap.
if folderHasChildType(&f.Folder, "Datacenter") && folderHasChildType(&f.Folder, "Folder") {
dc := NewDatacenter(ctx, &f.Folder)

ctx.Map.Update(dc, []types.PropertyChange{
ctx.Update(dc, []types.PropertyChange{
{Name: "name", Val: c.Name},
})

Expand Down Expand Up @@ -490,7 +490,7 @@ func (c *createVM) Run(task *Task) (types.AnyType, types.BaseMethodFault) {

vm.RefreshStorageInfo(c.ctx, nil)

c.ctx.Map.Update(vm, []types.PropertyChange{
c.ctx.Update(vm, []types.PropertyChange{
{Name: "name", Val: c.req.Config.Name},
})

Expand Down
6 changes: 3 additions & 3 deletions simulator/http_nfc_lease.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2019-2023 VMware, Inc. All Rights Reserved.
Copyright (c) 2019-2024 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -108,7 +108,7 @@ func ServeNFC(w http.ResponseWriter, r *http.Request) {

func (l *HttpNfcLease) error(ctx *Context, err *types.LocalizedMethodFault) {
ctx.WithLock(l, func() {
ctx.Map.Update(l, []types.PropertyChange{
ctx.Update(l, []types.PropertyChange{
{Name: "state", Val: types.HttpNfcLeaseStateError},
{Name: "error", Val: err},
})
Expand All @@ -124,7 +124,7 @@ func (l *HttpNfcLease) ready(ctx *Context, entity types.ManagedObjectReference,
}

ctx.WithLock(l, func() {
ctx.Map.Update(l, []types.PropertyChange{
ctx.Update(l, []types.PropertyChange{
{Name: "state", Val: types.HttpNfcLeaseStateReady},
{Name: "info", Val: info},
})
Expand Down
10 changes: 6 additions & 4 deletions simulator/property_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,8 +597,7 @@ func (pc *PropertyCollector) DestroyPropertyCollector(ctx *Context, c *types.Des
body := &methods.DestroyPropertyCollectorBody{}

for _, ref := range pc.Filter {
filter := ctx.Session.Get(ref).(*PropertyFilter)
filter.DestroyPropertyFilter(ctx, &types.DestroyPropertyFilter{This: ref})
ctx.Session.Remove(ctx, ref)
}

ctx.Session.Remove(ctx, c.This)
Expand Down Expand Up @@ -764,7 +763,7 @@ func (pc *PropertyCollector) PutObject(o mo.Reference) {
})
}

func (pc *PropertyCollector) UpdateObject(o mo.Reference, changes []types.PropertyChange) {
func (pc *PropertyCollector) UpdateObject(_ *Context, o mo.Reference, changes []types.PropertyChange) {
pc.update(types.ObjectUpdate{
Obj: o.Reference(),
Kind: types.ObjectUpdateKindModify,
Expand All @@ -782,7 +781,10 @@ func (pc *PropertyCollector) RemoveObject(_ *Context, ref types.ManagedObjectRef

func (pc *PropertyCollector) apply(ctx *Context, update *types.UpdateSet) types.BaseMethodFault {
for _, ref := range pc.Filter {
filter := ctx.Session.Get(ref).(*PropertyFilter)
filter, ok := ctx.Session.Get(ref).(*PropertyFilter)
if !ok {
continue
}

res, fault := filter.collect(ctx)
if fault != nil {
Expand Down
16 changes: 9 additions & 7 deletions simulator/property_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type PropertyFilter struct {
sync bool
}

func (f *PropertyFilter) UpdateObject(o mo.Reference, changes []types.PropertyChange) {
func (f *PropertyFilter) UpdateObject(ctx *Context, o mo.Reference, changes []types.PropertyChange) {
// A PropertyFilter's traversal spec is "applied" on the initial call to WaitForUpdates,
// with matching objects tracked in the `refs` field.
// New and deleted objects matching the filter are accounted for within PropertyCollector.
Expand All @@ -44,7 +44,7 @@ func (f *PropertyFilter) UpdateObject(o mo.Reference, changes []types.PropertyCh

for _, set := range f.Spec.ObjectSet {
if set.Obj == ref && len(set.SelectSet) != 0 {
f.sync = true
ctx.WithLock(f, func() { f.sync = true })
break
}
}
Expand Down Expand Up @@ -74,11 +74,13 @@ func (f *PropertyFilter) collect(ctx *Context) (*types.RetrieveResult, types.Bas
}

func (f *PropertyFilter) update(ctx *Context) {
if f.sync {
f.sync = false
clear(f.refs)
_, _ = f.collect(ctx)
}
ctx.WithLock(f, func() {
if f.sync {
f.sync = false
clear(f.refs)
_, _ = f.collect(ctx)
}
})
}

// matches returns true if the change matches one of the filter Spec.PropSet
Expand Down
8 changes: 4 additions & 4 deletions simulator/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var Map = NewRegistry()
type RegisterObject interface {
mo.Reference
PutObject(mo.Reference)
UpdateObject(mo.Reference, []types.PropertyChange)
UpdateObject(*Context, mo.Reference, []types.PropertyChange)
RemoveObject(*Context, types.ManagedObjectReference)
}

Expand Down Expand Up @@ -305,7 +305,7 @@ func (r *Registry) Remove(ctx *Context, item types.ManagedObjectReference) {
// such as any PropertyCollector instances with in-progress WaitForUpdates calls.
// The changes are also applied to the given object via mo.ApplyPropertyChange,
// so there is no need to set object fields directly.
func (r *Registry) Update(obj mo.Reference, changes []types.PropertyChange) {
func (r *Registry) Update(ctx *Context, obj mo.Reference, changes []types.PropertyChange) {
for i := range changes {
if changes[i].Op == "" {
changes[i].Op = types.PropertyChangeOpAssign
Expand All @@ -321,13 +321,13 @@ func (r *Registry) Update(obj mo.Reference, changes []types.PropertyChange) {
mo.ApplyPropertyChange(val, changes)

r.applyHandlers(func(o RegisterObject) {
o.UpdateObject(val, changes)
o.UpdateObject(ctx, val, changes)
})
}

func (r *Registry) AtomicUpdate(ctx *Context, obj mo.Reference, changes []types.PropertyChange) {
r.WithLock(ctx, obj, func() {
r.Update(obj, changes)
ctx.Update(obj, changes)
})
}

Expand Down
Loading

0 comments on commit 131e69e

Please sign in to comment.