Skip to content

Commit

Permalink
fix: vcsim: re-parent children in ResourcePool.Destroy
Browse files Browse the repository at this point in the history
Fixes #3396
  • Loading branch information
dougm committed Apr 2, 2024
1 parent 89a6a4c commit dcf063c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
11 changes: 11 additions & 0 deletions govc/test/vcsim.bats
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,17 @@ EOF
wait $pid
}

@test "vcsim issue #3396" {
vcsim_env

govc pool.create /DC0/host/DC0_C0/Resources/foo
govc pool.create /DC0/host/DC0_C0/Resources/foo/bar
govc find -l /DC0/host/DC0_C0/Resources
govc pool.destroy /DC0/host/DC0_C0/Resources/foo
# prior to the fix, "bar"'s Parent was still "foo", causing the following to hang
govc find -l /DC0/host/DC0_C0/Resources
}

@test "vcsim run container" {
require_docker

Expand Down
34 changes: 34 additions & 0 deletions simulator/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/types"
)

Expand Down Expand Up @@ -273,3 +274,36 @@ func TestFinderDefaultHostVPX(t *testing.T) {
t.Errorf("unexpected error type=%T", err)
}
}

func TestFinderDestroyedParentResourcePool(t *testing.T) {
Test(func(ctx context.Context, c *vim25.Client) {
finder := find.NewFinder(c)

rp, err := finder.ResourcePool(ctx, "/DC0/host/DC0_C0/Resources")
if err != nil {
t.Fatal(err)
}

foo, err := rp.Create(ctx, "foo", types.DefaultResourceConfigSpec())
if err != nil {
t.Fatal(err)
}

bar, err := foo.Create(ctx, "bar", types.DefaultResourceConfigSpec())
if err != nil {
t.Fatal(err)
}

task, err := foo.Destroy(ctx)
if err != nil {
t.Fatal(err)
}
if err := task.WaitEx(ctx); err != nil {
t.Fatal(err)
}

if _, err := finder.Element(ctx, bar.Reference()); err != nil {
t.Fatal(err)
}
})
}
6 changes: 5 additions & 1 deletion simulator/resource_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,11 @@ func (p *ResourcePool) DestroyTask(ctx *Context, req *types.Destroy_Task) soap.H
RemoveReference(&parent.ResourcePool, req.This)

// The grandchildren become children of the parent (rp)
parent.ResourcePool = append(parent.ResourcePool, p.ResourcePool.ResourcePool...)
for _, ref := range p.ResourcePool.ResourcePool {
child := ctx.Map.Get(ref).(*ResourcePool)
ctx.WithLock(child, func() { child.Parent = &parent.Self })
parent.ResourcePool = append(parent.ResourcePool, ref)
}
})

// And VMs move to the parent
Expand Down

0 comments on commit dcf063c

Please sign in to comment.