github.com/vmware/govmomi@v0.51.0/vim25/debug/debug_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 debug_test
     6  
     7  import (
     8  	"context"
     9  	"net/http"
    10  	"sync"
    11  	"testing"
    12  
    13  	"github.com/vmware/govmomi/find"
    14  	"github.com/vmware/govmomi/simulator"
    15  	"github.com/vmware/govmomi/vapi/rest"
    16  	"github.com/vmware/govmomi/vim25"
    17  	"github.com/vmware/govmomi/vim25/debug"
    18  
    19  	_ "github.com/vmware/govmomi/vapi/simulator"
    20  )
    21  
    22  func TestSetProvider(t *testing.T) {
    23  	p := debug.FileProvider{
    24  		Path: t.TempDir(),
    25  	}
    26  	debug.SetProvider(&p)
    27  
    28  	simulator.Test(func(ctx context.Context, c *vim25.Client) {
    29  		var wg sync.WaitGroup
    30  		rc := rest.NewClient(c)
    31  
    32  		// hit the debug package with some concurrency (see PR #2469)
    33  		for i := 0; i < 5; i++ {
    34  			wg.Add(1)
    35  			go func() {
    36  				defer wg.Done()
    37  				finder := find.NewFinder(c)
    38  
    39  				_, err := finder.VirtualMachineList(ctx, "*")
    40  				if err != nil {
    41  					t.Error(err)
    42  				}
    43  			}()
    44  		}
    45  
    46  		wg.Wait()
    47  
    48  		// send an http request with a nil Body to ensure debug trace doesn't panic in this case
    49  		u := rc.URL().String() + "/com/vmware/cis/session"
    50  
    51  		req, err := http.NewRequest(http.MethodPost, u, nil)
    52  		if err != nil {
    53  			t.Fatal(err)
    54  		}
    55  
    56  		req.SetBasicAuth("user", "pass")
    57  		var id string
    58  		if err = rc.Do(ctx, req, &id); err != nil {
    59  			t.Fatal(err)
    60  		}
    61  	})
    62  }