Skip to content

Commit

Permalink
vcsim: add PropertyCollector index support
Browse files Browse the repository at this point in the history
PropertyCollector supports use of index (unique key) to collect a single array element.
Further, a property path may also be applied to the given element.
  • Loading branch information
dougm committed May 30, 2024
1 parent 7a2712e commit 3b7ff25
Show file tree
Hide file tree
Showing 11 changed files with 801 additions and 38 deletions.
4 changes: 2 additions & 2 deletions govc/object/collect.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2017-2023 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.
Expand Down Expand Up @@ -416,7 +416,7 @@ func (cmd *collect) Run(ctx context.Context, f *flag.FlagSet) error {
}
res, err := p.RetrieveProperties(ctx, req)
if err != nil {
return nil
return err
}
content := res.Returnval
if len(content) != 1 {
Expand Down
60 changes: 60 additions & 0 deletions govc/test/object.bats
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,66 @@ EOF
govc object.collect -O -json | jq .
}

@test "object.collect index" {
vcsim_env

export GOVC_VM=/DC0/vm/DC0_H0_VM0

# NOTE: '-o' flag uses RetrievePropertiesEx() and mo.ObjectContentToType()
# By default, WaitForUpdatesEx() is used with raw types.ObjectContent

run govc object.collect -o $GOVC_VM 'config.hardware[4000]'
assert_failure

run govc object.collect -o $GOVC_VM 'config.hardware.device[4000'
assert_failure

run govc object.collect -o $GOVC_VM 'config.hardware.device["4000"]'
assert_failure # Key is int, not string

run govc object.collect -o -json $GOVC_VM 'config.hardware.device[4000]'
assert_success

run jq -r .config.hardware.device[].deviceInfo.label <<<"$output"
assert_success ethernet-0

run govc object.collect -o $GOVC_VM 'config.hardware.device[4000].enoent'
assert_failure # InvalidProperty

run govc object.collect -o -json $GOVC_VM 'config.hardware.device[4000].deviceInfo.label'
assert_success

run govc object.collect -s $GOVC_VM 'config.hardware.device[4000].deviceInfo.label'
assert_success ethernet-0

run govc object.collect -o $GOVC_VM 'config.extraConfig[guestinfo.a]'
assert_failure # string Key requires quotes

run govc object.collect -o $GOVC_VM 'config["guestinfo.a"]'
assert_failure

run govc object.collect -o $GOVC_VM 'config.extraConfig["guestinfo.a"]'
assert_success # Key does not exist, not an error

run govc vm.change -e "guestinfo.a=1" -e "guestinfo.b=2"
assert_success

run govc object.collect -json $GOVC_VM 'config.extraConfig["guestinfo.b"]'
assert_success

run jq -r .[].val.value <<<"$output"
assert_success 2

run govc object.collect -o -json $GOVC_VM 'config.extraConfig["guestinfo.b"]'
assert_success

run jq -r .config.extraConfig[].value <<<"$output"
assert_success 2

run govc object.collect -s $GOVC_VM 'config.extraConfig["guestinfo.b"].value'
assert_success 2
}

@test "object.find" {
vcsim_env -ds 2

Expand Down
121 changes: 121 additions & 0 deletions object/extension_manager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
Copyright (c) 2024-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
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package object_test

import (
"context"
"reflect"
"sync"
"testing"

"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/property"
"github.com/vmware/govmomi/simulator"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)

func TestExtensionMangerUpdates(t *testing.T) {
extension := types.Extension{
Description: &types.Description{
Label: "govmomi-test",
Summary: "Extension Manager test",
},
Key: t.Name(),
Version: "0.0.1",
ShownInSolutionManager: types.NewBool(false),
}

description := extension.Description.GetDescription()

f := func(item string) string {
return (&mo.Field{Path: "extensionList", Key: extension.Key, Item: item}).String()
}

tests := []types.PropertyChange{
{Name: f(""), Val: extension, Op: types.PropertyChangeOpAdd},
{Name: f(""), Val: extension, Op: types.PropertyChangeOpAssign},
{Name: f("description"), Val: *description, Op: types.PropertyChangeOpAssign},
{Name: f("description.label"), Val: description.Label, Op: types.PropertyChangeOpAssign},
{Name: f(""), Val: nil, Op: types.PropertyChangeOpRemove},
}

simulator.Test(func(ctx context.Context, c *vim25.Client) {
m := object.NewExtensionManager(c)
pc := property.DefaultCollector(c)

for _, test := range tests {
t.Logf("%s: %s", test.Op, test.Name)
update := make(chan bool)
parked := sync.OnceFunc(func() { update <- true })

var change *types.PropertyChange
cb := func(p []types.PropertyChange) bool {
parked()
change = &p[0]
if change.Op != test.Op {
t.Logf("ignore: change Op=%s, test Op=%s", change.Op, test.Op)
return false
}
return true
}

go func() {
werr := property.Wait(ctx, pc, m.Reference(), []string{test.Name}, cb)
if werr != nil {
t.Log(werr)
}
update <- true
}()
<-update // wait until above go func is parked in WaitForUpdatesEx()

switch test.Op {
case types.PropertyChangeOpAdd:
if err := m.Register(ctx, extension); err != nil {
t.Fatal(err)
}
case types.PropertyChangeOpAssign:
if err := m.Update(ctx, extension); err != nil {
t.Fatal(err)
}
case types.PropertyChangeOpRemove:
if err := m.Unregister(ctx, extension.Key); err != nil {
t.Fatal(err)
}
}
<-update // wait until update is received (cb returns true)

if change == nil {
t.Fatal("no change")
}

if change.Name != test.Name {
t.Errorf("Name: %s", change.Name)
}

if change.Op != test.Op {
t.Errorf("Op: %s", change.Op)
}

if !reflect.DeepEqual(change.Val, test.Val) {
t.Errorf("change.Val: %#v", change.Val)
t.Errorf("test.Val: %#v", test.Val)
}
}
})
}
Loading

0 comments on commit 3b7ff25

Please sign in to comment.