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