github.com/vmware/govmomi@v0.51.0/lookup/simulator/simulator_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 simulator 6 7 import ( 8 "context" 9 "log" 10 "testing" 11 12 "github.com/vmware/govmomi" 13 "github.com/vmware/govmomi/lookup" 14 "github.com/vmware/govmomi/lookup/types" 15 "github.com/vmware/govmomi/simulator" 16 ) 17 18 func TestClient(t *testing.T) { 19 ctx := context.Background() 20 21 model := simulator.VPX() 22 23 defer model.Remove() 24 err := model.Create() 25 if err != nil { 26 log.Fatal(err) 27 } 28 29 s := model.Service.NewServer() 30 defer s.Close() 31 32 model.Service.RegisterSDK(New()) 33 34 vc, err := govmomi.NewClient(ctx, s.URL, true) 35 if err != nil { 36 t.Fatal(err) 37 } 38 39 c, err := lookup.NewClient(ctx, vc.Client) 40 if err != nil { 41 t.Fatal(err) 42 } 43 44 id, err := c.SiteID(ctx) 45 if err != nil { 46 t.Fatal(err) 47 } 48 if id != siteID { 49 t.Errorf("SiteID=%s", id) 50 } 51 52 vc.Logout(ctx) // List does not require authentication 53 54 _, err = c.List(ctx, nil) 55 if err == nil { 56 t.Error("expected error") 57 } 58 59 // test filters that should return 1 service 60 filters := []*types.LookupServiceRegistrationFilter{ 61 { 62 ServiceType: &types.LookupServiceRegistrationServiceType{ 63 Product: "com.vmware.cis", 64 Type: "vcenterserver", 65 }, 66 EndpointType: &types.LookupServiceRegistrationEndpointType{ 67 Protocol: "vmomi", 68 Type: "com.vmware.vim", 69 }, 70 }, 71 { 72 ServiceType: &types.LookupServiceRegistrationServiceType{ 73 Type: "cs.identity", 74 }, 75 EndpointType: &types.LookupServiceRegistrationEndpointType{ 76 Protocol: "wsTrust", 77 }, 78 }, 79 { 80 ServiceType: &types.LookupServiceRegistrationServiceType{}, 81 EndpointType: &types.LookupServiceRegistrationEndpointType{ 82 Protocol: "vmomi", 83 Type: "com.vmware.vim", 84 }, 85 }, 86 } 87 88 for _, filter := range filters { 89 info, err := c.List(ctx, filter) 90 if err != nil { 91 t.Fatal(err) 92 } 93 94 if len(info) != 1 { 95 t.Errorf("len=%d", len(info)) 96 } 97 98 filter.ServiceType.Type = "enoent" 99 100 info, err = c.List(ctx, filter) 101 if err != nil { 102 t.Fatal(err) 103 } 104 105 if len(info) != 0 { 106 t.Errorf("len=%d", len(info)) 107 } 108 } 109 110 // "empty" filters should return all services 111 filters = []*types.LookupServiceRegistrationFilter{ 112 {}, 113 { 114 ServiceType: new(types.LookupServiceRegistrationServiceType), 115 EndpointType: new(types.LookupServiceRegistrationEndpointType), 116 }, 117 { 118 EndpointType: new(types.LookupServiceRegistrationEndpointType), 119 }, 120 { 121 ServiceType: new(types.LookupServiceRegistrationServiceType), 122 }, 123 } 124 125 for _, filter := range filters { 126 info, err := c.List(ctx, filter) 127 if err != nil { 128 t.Fatal(err) 129 } 130 131 if len(info) != 4 { 132 t.Errorf("len=%d", len(info)) 133 } 134 } 135 136 vc.Client.ServiceContent.Setting = nil // ensure we don't NPE without this set 137 _, err = lookup.NewClient(ctx, vc.Client) 138 if err != nil { 139 t.Fatal(err) 140 } 141 }