github.com/vmware/govmomi@v0.51.0/vslm/global_object_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 vslm_test 6 7 import ( 8 "context" 9 "fmt" 10 "strings" 11 "testing" 12 "time" 13 14 "github.com/vmware/govmomi/find" 15 "github.com/vmware/govmomi/simulator" 16 "github.com/vmware/govmomi/vim25" 17 "github.com/vmware/govmomi/vim25/types" 18 "github.com/vmware/govmomi/vslm" 19 _ "github.com/vmware/govmomi/vslm/simulator" 20 vso "github.com/vmware/govmomi/vslm/types" 21 ) 22 23 func TestList(t *testing.T) { 24 model := simulator.VPX() 25 model.Datastore = 4 26 27 now := time.Now().Format(time.RFC3339Nano) 28 29 simulator.Test(func(ctx context.Context, vc *vim25.Client) { 30 datastores, err := find.NewFinder(vc).DatastoreList(ctx, "*") 31 if err != nil { 32 t.Fatal(err) 33 } 34 35 c, err := vslm.NewClient(ctx, vc) 36 if err != nil { 37 t.Fatal(err) 38 } 39 40 m := vslm.NewGlobalObjectManager(c) 41 42 namespaces := []string{"foo", "bar", "baz"} 43 44 dsIDs := make([]string, len(datastores)) 45 46 for i, ds := range datastores { 47 dsIDs[i] = strings.SplitN(ds.Reference().Value, "-", 2)[1] 48 49 for j, ns := range namespaces { 50 spec := types.VslmCreateSpec{ 51 Name: fmt.Sprintf("%s-disk-%d", ns, i+j), 52 CapacityInMB: int64(i+j) * 10, 53 BackingSpec: &types.VslmCreateSpecDiskFileBackingSpec{ 54 VslmCreateSpecBackingSpec: types.VslmCreateSpecBackingSpec{ 55 Datastore: ds.Reference(), 56 }, 57 }, 58 } 59 t.Logf("CreateDisk %s (%dMB) on datastore-%s", spec.Name, spec.CapacityInMB, dsIDs[i]) 60 task, err := m.CreateDisk(ctx, spec) 61 if err != nil { 62 t.Fatal(err) 63 } 64 disk, err := task.Wait(ctx, time.Hour) 65 if err != nil { 66 t.Fatal(err) 67 } 68 69 id := disk.(types.VStorageObject).Config.Id 70 71 metadata := []types.KeyValue{ 72 {Key: "namespace", Value: ns}, 73 {Key: "name", Value: spec.Name}, 74 } 75 76 task, err = m.UpdateMetadata(ctx, id, metadata, nil) 77 if err != nil { 78 t.Fatal(err) 79 } 80 _, err = task.Wait(ctx, time.Hour) 81 if err != nil { 82 t.Fatal(err) 83 } 84 } 85 } 86 87 tests := []struct { 88 expect int 89 query []vso.VslmVsoVStorageObjectQuerySpec 90 }{ 91 {model.Datastore * len(namespaces), nil}, 92 {-1, []vso.VslmVsoVStorageObjectQuerySpec{{ 93 QueryField: "invalid", 94 QueryOperator: string(vso.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumEquals), 95 QueryValue: []string{"any"}, 96 }}}, 97 {-1, []vso.VslmVsoVStorageObjectQuerySpec{{ 98 QueryField: string(vso.VslmVsoVStorageObjectQuerySpecQueryFieldEnumName), 99 QueryOperator: "invalid", 100 QueryValue: []string{"any"}, 101 }}}, 102 {-1, []vso.VslmVsoVStorageObjectQuerySpec{{ 103 QueryField: string(vso.VslmVsoVStorageObjectQuerySpecQueryFieldEnumName), 104 QueryOperator: string(vso.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumEquals), 105 QueryValue: nil, 106 }}}, 107 {-1, []vso.VslmVsoVStorageObjectQuerySpec{{ 108 QueryField: string(vso.VslmVsoVStorageObjectQuerySpecQueryFieldEnumName), 109 QueryOperator: string(vso.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumEquals), 110 QueryValue: []string{"one", "two"}, 111 }}}, 112 {-1, []vso.VslmVsoVStorageObjectQuerySpec{{ 113 QueryField: string(vso.VslmVsoVStorageObjectQuerySpecQueryFieldEnumCapacity), 114 QueryOperator: string(vso.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumStartsWith), 115 QueryValue: []string{"10"}, 116 }}}, 117 {-1, []vso.VslmVsoVStorageObjectQuerySpec{{ 118 QueryField: string(vso.VslmVsoVStorageObjectQuerySpecQueryFieldEnumCapacity), 119 QueryOperator: string(vso.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumGreaterThan), 120 QueryValue: []string{"ten"}, 121 }}}, 122 {3, []vso.VslmVsoVStorageObjectQuerySpec{{ 123 QueryField: string(vso.VslmVsoVStorageObjectQuerySpecQueryFieldEnumCapacity), 124 QueryOperator: string(vso.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumGreaterThan), 125 QueryValue: []string{"30"}, 126 }}}, 127 {0, []vso.VslmVsoVStorageObjectQuerySpec{{ 128 QueryField: string(vso.VslmVsoVStorageObjectQuerySpecQueryFieldEnumCapacity), 129 QueryOperator: string(vso.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumGreaterThan), 130 QueryValue: []string{"5000"}, 131 }}}, 132 {len(namespaces), []vso.VslmVsoVStorageObjectQuerySpec{{ 133 QueryField: string(vso.VslmVsoVStorageObjectQuerySpecQueryFieldEnumDatastoreMoId), 134 QueryOperator: string(vso.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumEquals), 135 QueryValue: []string{dsIDs[0]}, 136 }}}, 137 {model.Datastore, []vso.VslmVsoVStorageObjectQuerySpec{ 138 { 139 QueryField: string(vso.VslmVsoVStorageObjectQuerySpecQueryFieldEnumMetadataKey), 140 QueryOperator: string(vso.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumEquals), 141 QueryValue: []string{"namespace"}, 142 }, 143 { 144 QueryField: string(vso.VslmVsoVStorageObjectQuerySpecQueryFieldEnumMetadataValue), 145 QueryOperator: string(vso.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumEquals), 146 QueryValue: []string{namespaces[0]}, 147 }, 148 }}, 149 {model.Datastore, []vso.VslmVsoVStorageObjectQuerySpec{{ 150 QueryField: string(vso.VslmVsoVStorageObjectQuerySpecQueryFieldEnumName), 151 QueryOperator: string(vso.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumStartsWith), 152 QueryValue: []string{namespaces[1]}, 153 }}}, 154 {model.Datastore, []vso.VslmVsoVStorageObjectQuerySpec{{ 155 QueryField: string(vso.VslmVsoVStorageObjectQuerySpecQueryFieldEnumName), 156 QueryOperator: string(vso.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumStartsWith), 157 QueryValue: []string{namespaces[1]}, 158 }}}, 159 {model.Datastore * len(namespaces), []vso.VslmVsoVStorageObjectQuerySpec{{ 160 QueryField: string(vso.VslmVsoVStorageObjectQuerySpecQueryFieldEnumName), 161 QueryOperator: string(vso.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumContains), 162 QueryValue: []string{"disk"}, 163 }}}, 164 {0, []vso.VslmVsoVStorageObjectQuerySpec{ 165 { 166 QueryField: string(vso.VslmVsoVStorageObjectQuerySpecQueryFieldEnumName), 167 QueryOperator: string(vso.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumStartsWith), 168 QueryValue: []string{namespaces[0]}, 169 }, 170 { 171 QueryField: string(vso.VslmVsoVStorageObjectQuerySpecQueryFieldEnumName), 172 QueryOperator: string(vso.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumStartsWith), 173 QueryValue: []string{namespaces[1]}, 174 }, 175 }}, 176 {model.Datastore * len(namespaces), []vso.VslmVsoVStorageObjectQuerySpec{{ 177 QueryField: string(vso.VslmVsoVStorageObjectQuerySpecQueryFieldEnumCreateTime), 178 QueryOperator: string(vso.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumGreaterThan), 179 QueryValue: []string{now}, 180 }}}, 181 {0, []vso.VslmVsoVStorageObjectQuerySpec{{ 182 QueryField: string(vso.VslmVsoVStorageObjectQuerySpecQueryFieldEnumCreateTime), 183 QueryOperator: string(vso.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumLessThan), 184 QueryValue: []string{now}, 185 }}}, 186 {-1, []vso.VslmVsoVStorageObjectQuerySpec{{ 187 QueryField: string(vso.VslmVsoVStorageObjectQuerySpecQueryFieldEnumCreateTime), 188 QueryOperator: string(vso.VslmVsoVStorageObjectQuerySpecQueryOperatorEnumContains), 189 QueryValue: []string{now}, 190 }}}, 191 } 192 193 for _, test := range tests { 194 if test.expect > 2 { 195 vslm.DefaultMaxResults = test.expect / 2 // test pagination 196 } 197 t.Run(queryString(test.query), func(t *testing.T) { 198 res, err := m.List(ctx, test.query...) 199 if test.expect == -1 { 200 if err == nil { 201 t.Error("expected error") 202 } 203 } else { 204 if err != nil { 205 t.Fatal(err) 206 } 207 208 if len(res.Id) != test.expect { 209 t.Errorf("expected %d, got: %d", test.expect, len(res.Id)) 210 } 211 } 212 }) 213 } 214 }, model) 215 } 216 217 func queryString(query []vso.VslmVsoVStorageObjectQuerySpec) string { 218 if query == nil { 219 return "no query" 220 } 221 res := make([]string, len(query)) 222 for i, q := range query { 223 res[i] = fmt.Sprintf("%s.%s=%s", 224 q.QueryField, q.QueryOperator, 225 strings.Join(q.QueryValue, ",")) 226 } 227 return strings.Join(res, " and ") 228 }