github.com/vmware/govmomi@v0.43.0/object/extension_manager_test.go (about) 1 /* 2 Copyright (c) 2024-2024 VMware, Inc. All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package object_test 18 19 import ( 20 "context" 21 "reflect" 22 "sync" 23 "testing" 24 25 "github.com/vmware/govmomi/object" 26 "github.com/vmware/govmomi/property" 27 "github.com/vmware/govmomi/simulator" 28 "github.com/vmware/govmomi/vim25" 29 "github.com/vmware/govmomi/vim25/mo" 30 "github.com/vmware/govmomi/vim25/types" 31 ) 32 33 func TestExtensionMangerUpdates(t *testing.T) { 34 extension := types.Extension{ 35 Description: &types.Description{ 36 Label: "govmomi-test", 37 Summary: "Extension Manager test", 38 }, 39 Key: t.Name(), 40 Version: "0.0.1", 41 ShownInSolutionManager: types.NewBool(false), 42 } 43 44 description := extension.Description.GetDescription() 45 46 f := func(item string) string { 47 return (&mo.Field{Path: "extensionList", Key: extension.Key, Item: item}).String() 48 } 49 50 tests := []types.PropertyChange{ 51 {Name: f(""), Val: extension, Op: types.PropertyChangeOpAdd}, 52 {Name: f(""), Val: extension, Op: types.PropertyChangeOpAssign}, 53 {Name: f("description"), Val: *description, Op: types.PropertyChangeOpAssign}, 54 {Name: f("description.label"), Val: description.Label, Op: types.PropertyChangeOpAssign}, 55 {Name: f(""), Val: nil, Op: types.PropertyChangeOpRemove}, 56 } 57 58 simulator.Test(func(ctx context.Context, c *vim25.Client) { 59 m := object.NewExtensionManager(c) 60 pc := property.DefaultCollector(c) 61 62 for _, test := range tests { 63 t.Logf("%s: %s", test.Op, test.Name) 64 update := make(chan bool) 65 parked := sync.OnceFunc(func() { update <- true }) 66 67 var change *types.PropertyChange 68 cb := func(p []types.PropertyChange) bool { 69 parked() 70 change = &p[0] 71 if change.Op != test.Op { 72 t.Logf("ignore: change Op=%s, test Op=%s", change.Op, test.Op) 73 return false 74 } 75 return true 76 } 77 78 go func() { 79 werr := property.Wait(ctx, pc, m.Reference(), []string{test.Name}, cb) 80 if werr != nil { 81 t.Log(werr) 82 } 83 update <- true 84 }() 85 <-update // wait until above go func is parked in WaitForUpdatesEx() 86 87 switch test.Op { 88 case types.PropertyChangeOpAdd: 89 if err := m.Register(ctx, extension); err != nil { 90 t.Fatal(err) 91 } 92 case types.PropertyChangeOpAssign: 93 if err := m.Update(ctx, extension); err != nil { 94 t.Fatal(err) 95 } 96 case types.PropertyChangeOpRemove: 97 if err := m.Unregister(ctx, extension.Key); err != nil { 98 t.Fatal(err) 99 } 100 } 101 <-update // wait until update is received (cb returns true) 102 103 if change == nil { 104 t.Fatal("no change") 105 } 106 107 if change.Name != test.Name { 108 t.Errorf("Name: %s", change.Name) 109 } 110 111 if change.Op != test.Op { 112 t.Errorf("Op: %s", change.Op) 113 } 114 115 if !reflect.DeepEqual(change.Val, test.Val) { 116 t.Errorf("change.Val: %#v", change.Val) 117 t.Errorf("test.Val: %#v", test.Val) 118 } 119 } 120 }) 121 }