github.com/vmware/govmomi@v0.37.2/vim25/debug/debug_test.go (about)

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