github.com/vmware/govmomi@v0.37.2/guest/file_manager_test.go (about)

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