github.com/vmware/govmomi@v0.51.0/guest/file_manager_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 guest_test
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"testing"
    11  
    12  	"github.com/vmware/govmomi/guest"
    13  	"github.com/vmware/govmomi/simulator"
    14  	"github.com/vmware/govmomi/vim25"
    15  	"github.com/vmware/govmomi/vim25/types"
    16  )
    17  
    18  func TestTranferURL(t *testing.T) {
    19  	simulator.Test(func(ctx context.Context, c *vim25.Client) {
    20  		vm := simulator.Map(ctx).Any("VirtualMachine").(*simulator.VirtualMachine)
    21  		host := simulator.Map(ctx).Get(*vm.Runtime.Host).(*simulator.HostSystem)
    22  
    23  		ops := guest.NewOperationsManager(c, vm.Reference())
    24  		m, err := ops.FileManager(ctx)
    25  		if err != nil {
    26  			t.Fatal(err)
    27  		}
    28  
    29  		for i := 0; i < 2; i++ { // 2nd time is to validate the cached value
    30  			turl := "https://esx:443/foo/bar"
    31  			u, err := m.TransferURL(ctx, turl)
    32  			if err != nil {
    33  				t.Fatal(err)
    34  			}
    35  			if u.Hostname() != "127.0.0.1" {
    36  				t.Errorf("hostname=%s", u.Hostname())
    37  			}
    38  
    39  			ipv6 := "[fd01:1:3:1528::10bf]:443"
    40  			turl = fmt.Sprintf("https://%s/foo/bar", ipv6)
    41  			u, err = m.TransferURL(ctx, turl)
    42  			if err != nil {
    43  				t.Fatal(err)
    44  			}
    45  			if u.Host != ipv6 {
    46  				t.Errorf("host=%s", u.Host)
    47  			}
    48  		}
    49  
    50  		// hostname should be returned by TransferURL when there are multiple management IPs
    51  		for _, nc := range host.Config.VirtualNicManagerInfo.NetConfig {
    52  			if nc.NicType == string(types.HostVirtualNicManagerNicTypeManagement) {
    53  				host.Config.VirtualNicManagerInfo.NetConfig = append(host.Config.VirtualNicManagerInfo.NetConfig, nc)
    54  				break
    55  			}
    56  		}
    57  
    58  		for i := 0; i < 2; i++ { // 2nd time is to validate the cached value
    59  			turl := "https://esx2:443/foo/bar"
    60  			u, err := m.TransferURL(ctx, turl)
    61  			if err != nil {
    62  				t.Fatal(err)
    63  			}
    64  			if u.Hostname() != "esx2" {
    65  				t.Errorf("hostname=%s", u.Hostname())
    66  			}
    67  		}
    68  
    69  		// error should be returned if HostSystem.Config is nil
    70  		host.Config = nil
    71  		m, err = ops.FileManager(ctx)
    72  		if err != nil {
    73  			t.Fatal(err)
    74  		}
    75  
    76  		turl := "https://noconfig:443/foo/bar"
    77  		u, err := m.TransferURL(ctx, turl)
    78  		if err == nil {
    79  			t.Errorf("expected error (url=%s)", u)
    80  		}
    81  	})
    82  }