github.com/vmware/govmomi@v0.51.0/object/common_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 "testing" 11 12 "github.com/vmware/govmomi/object" 13 "github.com/vmware/govmomi/simulator" 14 "github.com/vmware/govmomi/vim25" 15 "github.com/vmware/govmomi/vim25/types" 16 ) 17 18 func TestCommonName(t *testing.T) { 19 c := &object.Common{} 20 21 name := c.Name() 22 if name != "" { 23 t.Errorf("Name=%s", name) 24 } 25 26 c.InventoryPath = "/foo/bar" 27 name = c.Name() 28 if name != "bar" { 29 t.Errorf("Name=%s", name) 30 } 31 } 32 33 func TestObjectName(t *testing.T) { 34 type common interface { 35 ObjectName(context.Context) (string, error) 36 } 37 38 simulator.Test(func(ctx context.Context, c *vim25.Client) { 39 kinds := []string{"VirtualMachine", "Network", "DistributedVirtualPortgroup"} 40 41 for _, kind := range kinds { 42 ref := simulator.Map(ctx).Any(kind) 43 obj := object.NewReference(c, ref.Reference()) 44 45 name, err := obj.(common).ObjectName(ctx) 46 if err != nil { 47 t.Fatal(err) 48 } 49 50 if name == "" { 51 t.Errorf("empty name for %s", ref.Reference()) 52 } 53 } 54 }) 55 } 56 57 func TestReferenceFromString(t *testing.T) { 58 tests := []struct { 59 in string 60 out *types.ManagedObjectReference 61 }{ 62 {"no:no", nil}, 63 {"Datacenter:yes", &types.ManagedObjectReference{Type: "Datacenter", Value: "yes"}}, 64 {"datacenter-yes", &types.ManagedObjectReference{Type: "Datacenter", Value: "datacenter-yes"}}, 65 {"VirtualMachine:vm-2", &types.ManagedObjectReference{Type: "VirtualMachine", Value: "vm-2"}}, 66 {"vm-2", &types.ManagedObjectReference{Type: "VirtualMachine", Value: "vm-2"}}, 67 {"domain-s2", &types.ManagedObjectReference{Type: "ComputeResource", Value: "domain-s2"}}, 68 {"domain-c2", &types.ManagedObjectReference{Type: "ClusterComputeResource", Value: "domain-c2"}}, 69 {"group-d1", &types.ManagedObjectReference{Type: "Folder", Value: "group-d1"}}, 70 {"group-p2", &types.ManagedObjectReference{Type: "StoragePod", Value: "group-p2"}}, 71 {"resgroup-42", &types.ManagedObjectReference{Type: "ResourcePool", Value: "resgroup-42"}}, 72 {"resgroup-v32", &types.ManagedObjectReference{Type: "VirtualApp", Value: "resgroup-v32"}}, 73 } 74 75 for _, test := range tests { 76 ref := object.ReferenceFromString(test.in) 77 if !reflect.DeepEqual(test.out, ref) { 78 t.Errorf("%s: expected %v, got %v", test.in, test.out, ref) 79 } 80 } 81 }