github.com/vmware/govmomi@v0.43.0/lookup/client_test.go (about) 1 /* 2 Copyright (c) 2018-2023 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 lookup_test 18 19 // lookup/simulator/simulator_test.go has more tests 20 21 import ( 22 "context" 23 "fmt" 24 "net/http" 25 "net/url" 26 "os" 27 "strings" 28 "testing" 29 30 "github.com/stretchr/testify/require" 31 32 "github.com/vmware/govmomi/lookup" 33 "github.com/vmware/govmomi/simulator" 34 "github.com/vmware/govmomi/ssoadmin" 35 "github.com/vmware/govmomi/sts" 36 "github.com/vmware/govmomi/vim25" 37 38 lsim "github.com/vmware/govmomi/lookup/simulator" 39 _ "github.com/vmware/govmomi/ssoadmin/simulator" 40 _ "github.com/vmware/govmomi/sts/simulator" 41 ) 42 43 // test lookup.EndpointURL usage by the ssoadmin and sts clients 44 func TestEndpointURL(t *testing.T) { 45 // these client calls should fail since we'll break the URL paths 46 simulator.Test(func(ctx context.Context, vc *vim25.Client) { 47 lsim.BreakLookupServiceURLs() 48 49 { 50 _, err := ssoadmin.NewClient(ctx, vc) 51 if err == nil { 52 t.Error("expected error") 53 } 54 if !strings.Contains(err.Error(), http.StatusText(404)) { 55 t.Error(err) 56 } 57 } 58 59 { 60 c, err := sts.NewClient(ctx, vc) 61 if err != nil { 62 t.Fatal(err) 63 } 64 65 req := sts.TokenRequest{ 66 Userinfo: url.UserPassword("Administrator@VSPHERE.LOCAL", "password"), 67 } 68 _, err = c.Issue(ctx, req) 69 if err == nil { 70 t.Error("expected error") 71 } 72 if !strings.Contains(err.Error(), http.StatusText(404)) { 73 t.Error(err) 74 } 75 } 76 }) 77 // these client calls should not fail 78 simulator.Test(func(ctx context.Context, vc *vim25.Client) { 79 { 80 // NewClient calls ServiceInstance methods 81 _, err := ssoadmin.NewClient(ctx, vc) 82 if err != nil { 83 t.Fatal(err) 84 } 85 } 86 87 { 88 c, err := sts.NewClient(ctx, vc) 89 if err != nil { 90 t.Fatal(err) 91 } 92 93 req := sts.TokenRequest{ 94 Userinfo: url.UserPassword("Administrator@VSPHERE.LOCAL", "password"), 95 } 96 97 _, err = c.Issue(ctx, req) 98 if err != nil { 99 t.Fatal(err) 100 } 101 } 102 }) 103 104 t.Run("With Envoy sidecar and a malfunctioning lookup service, endpoint url should still succeed", func(t *testing.T) { 105 model := simulator.VPX() 106 model.Create() 107 simulator.Test(func(ctx context.Context, vc *vim25.Client) { 108 lsim.BreakLookupServiceURLs() 109 // Map Envoy sidecar on the same port as the vcsim client. 110 os.Setenv("GOVMOMI_ENVOY_SIDECAR_PORT", vc.Client.URL().Port()) 111 os.Setenv("GOVMOMI_ENVOY_SIDECAR_HOST", vc.Client.URL().Hostname()) 112 testPath := "/fake/path" 113 expectedUrl := fmt.Sprintf("http://%s%s", vc.Client.URL().Host, testPath) 114 url := lookup.EndpointURL(ctx, vc, testPath, nil) 115 require.Equal(t, expectedUrl, url) 116 }, model) 117 }) 118 }