github.com/vmware/govmomi@v0.51.0/object/extension_manager_test.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package object_test 6 7 import ( 8 "context" 9 "reflect" 10 "sync" 11 "testing" 12 13 "github.com/vmware/govmomi/object" 14 "github.com/vmware/govmomi/property" 15 "github.com/vmware/govmomi/simulator" 16 "github.com/vmware/govmomi/vim25" 17 "github.com/vmware/govmomi/vim25/mo" 18 "github.com/vmware/govmomi/vim25/types" 19 ) 20 21 func TestExtensionMangerUpdates(t *testing.T) { 22 extension := types.Extension{ 23 Description: &types.Description{ 24 Label: "govmomi-test", 25 Summary: "Extension Manager test", 26 }, 27 Key: t.Name(), 28 Version: "0.0.1", 29 ShownInSolutionManager: types.NewBool(false), 30 } 31 32 description := extension.Description.GetDescription() 33 34 f := func(item string) string { 35 return (&mo.Field{Path: "extensionList", Key: extension.Key, Item: item}).String() 36 } 37 38 tests := []types.PropertyChange{ 39 {Name: f(""), Val: extension, Op: types.PropertyChangeOpAdd}, 40 {Name: f(""), Val: extension, Op: types.PropertyChangeOpAssign}, 41 {Name: f("description"), Val: *description, Op: types.PropertyChangeOpAssign}, 42 {Name: f("description.label"), Val: description.Label, Op: types.PropertyChangeOpAssign}, 43 {Name: f(""), Val: nil, Op: types.PropertyChangeOpRemove}, 44 } 45 46 simulator.Test(func(ctx context.Context, c *vim25.Client) { 47 m := object.NewExtensionManager(c) 48 pc := property.DefaultCollector(c) 49 50 for _, test := range tests { 51 t.Logf("%s: %s", test.Op, test.Name) 52 update := make(chan bool) 53 parked := sync.OnceFunc(func() { update <- true }) 54 55 var change *types.PropertyChange 56 cb := func(p []types.PropertyChange) bool { 57 parked() 58 change = &p[0] 59 if change.Op != test.Op { 60 t.Logf("ignore: change Op=%s, test Op=%s", change.Op, test.Op) 61 return false 62 } 63 return true 64 } 65 66 go func() { 67 werr := property.Wait(ctx, pc, m.Reference(), []string{test.Name}, cb) 68 if werr != nil { 69 t.Log(werr) 70 } 71 update <- true 72 }() 73 <-update // wait until above go func is parked in WaitForUpdatesEx() 74 75 switch test.Op { 76 case types.PropertyChangeOpAdd: 77 if err := m.Register(ctx, extension); err != nil { 78 t.Fatal(err) 79 } 80 case types.PropertyChangeOpAssign: 81 if err := m.Update(ctx, extension); err != nil { 82 t.Fatal(err) 83 } 84 case types.PropertyChangeOpRemove: 85 if err := m.Unregister(ctx, extension.Key); err != nil { 86 t.Fatal(err) 87 } 88 } 89 <-update // wait until update is received (cb returns true) 90 91 if change == nil { 92 t.Fatal("no change") 93 } 94 95 if change.Name != test.Name { 96 t.Errorf("Name: %s", change.Name) 97 } 98 99 if change.Op != test.Op { 100 t.Errorf("Op: %s", change.Op) 101 } 102 103 if !reflect.DeepEqual(change.Val, test.Val) { 104 t.Errorf("change.Val: %#v", change.Val) 105 t.Errorf("test.Val: %#v", test.Val) 106 } 107 } 108 }) 109 }