github.com/vmware/govmomi@v0.51.0/simulator/feature_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 simulator_test
     6  
     7  import (
     8  	"bytes"
     9  	"context"
    10  	"fmt"
    11  	"log"
    12  	"net/url"
    13  	"os"
    14  	"os/exec"
    15  	"path/filepath"
    16  	"strings"
    17  
    18  	"github.com/vmware/govmomi"
    19  	"github.com/vmware/govmomi/find"
    20  	"github.com/vmware/govmomi/object"
    21  	"github.com/vmware/govmomi/simulator"
    22  	"github.com/vmware/govmomi/test"
    23  	"github.com/vmware/govmomi/vim25"
    24  	"github.com/vmware/govmomi/vim25/types"
    25  )
    26  
    27  // Custom username + password authentication
    28  func Example_usernamePasswordLogin() {
    29  	model := simulator.VPX()
    30  
    31  	defer model.Remove()
    32  	err := model.Create()
    33  	if err != nil {
    34  		log.Fatal(err)
    35  	}
    36  
    37  	model.Service.Listen = &url.URL{
    38  		User: url.UserPassword("my-username", "my-password"),
    39  	}
    40  
    41  	s := model.Service.NewServer()
    42  	defer s.Close()
    43  
    44  	c, err := govmomi.NewClient(context.Background(), s.URL, true)
    45  	if err != nil {
    46  		log.Fatal(err)
    47  	}
    48  
    49  	fmt.Printf("login to %s as %s", c.Client.ServiceContent.About.ApiType, s.URL.User)
    50  	// Output: login to VirtualCenter as my-username:my-password
    51  }
    52  
    53  // Set VM properties that the API cannot change in a real vCenter.
    54  func Example_setVirtualMachineProperties() {
    55  	simulator.Test(func(ctx context.Context, c *vim25.Client) {
    56  		vm, err := find.NewFinder(c).VirtualMachine(ctx, "DC0_H0_VM0")
    57  		if err != nil {
    58  			log.Fatal(err)
    59  		}
    60  
    61  		spec := types.VirtualMachineConfigSpec{
    62  			ExtraConfig: []types.BaseOptionValue{
    63  				&types.OptionValue{Key: "SET.guest.ipAddress", Value: "10.0.0.42"},
    64  			},
    65  		}
    66  
    67  		task, _ := vm.Reconfigure(ctx, spec)
    68  
    69  		_ = task.Wait(ctx)
    70  
    71  		ip, _ := vm.WaitForIP(ctx)
    72  		fmt.Printf("ip is %s", ip)
    73  	})
    74  	// Output: ip is 10.0.0.42
    75  }
    76  
    77  // Tie a docker container to the lifecycle of a vcsim VM
    78  func Example_runContainer() {
    79  	simulator.Test(func(ctx context.Context, c *vim25.Client) {
    80  		if !test.HasDocker() {
    81  			fmt.Println("0 diff")
    82  			return
    83  		}
    84  
    85  		finder := find.NewFinder(c)
    86  		pool, _ := finder.ResourcePool(ctx, "DC0_H0/Resources")
    87  		dc, err := finder.Datacenter(ctx, "DC0")
    88  		if err != nil {
    89  			log.Fatal(err)
    90  		}
    91  		f, _ := dc.Folders(ctx)
    92  		dir, err := os.MkdirTemp("", "example")
    93  		if err != nil {
    94  			log.Fatal(err)
    95  		}
    96  		os.Chmod(dir, 0755)
    97  		fpath := filepath.Join(dir, "index.html")
    98  		fcontent := "foo"
    99  		os.WriteFile(fpath, []byte(fcontent), 0644)
   100  		// just in case umask gets in the way
   101  		os.Chmod(fpath, 0644)
   102  		defer os.RemoveAll(dir)
   103  
   104  		args := fmt.Sprintf("-v '%s:/usr/share/nginx/html:ro' nginx", dir)
   105  
   106  		spec := types.VirtualMachineConfigSpec{
   107  			Name: "nginx",
   108  			Files: &types.VirtualMachineFileInfo{
   109  				VmPathName: "[LocalDS_0] nginx",
   110  			},
   111  			ExtraConfig: []types.BaseOptionValue{
   112  				&types.OptionValue{Key: "RUN.container", Value: args}, // run nginx
   113  				&types.OptionValue{Key: "RUN.port.80", Value: "8888"}, // test port remap
   114  			},
   115  		}
   116  
   117  		// Create a new VM
   118  		task, err := f.VmFolder.CreateVM(ctx, spec, pool, nil)
   119  
   120  		if err != nil {
   121  			log.Fatal(err)
   122  		}
   123  		info, err := task.WaitForResult(ctx, nil)
   124  		if err != nil {
   125  			log.Fatal(err)
   126  		}
   127  		vm := object.NewVirtualMachine(c, info.Result.(types.ManagedObjectReference))
   128  
   129  		// PowerOn VM starts the nginx container
   130  		task, _ = vm.PowerOn(ctx)
   131  		err = task.Wait(ctx)
   132  		if err != nil {
   133  			log.Fatal(err)
   134  		}
   135  
   136  		ip, _ := vm.WaitForIP(ctx, true) // Returns the docker container's IP
   137  
   138  		// Count the number of bytes in feature_test.go via nginx going direct to the container
   139  		cmd := exec.Command("docker", "run", "--rm", "curlimages/curl", "curl", "-f", fmt.Sprintf("http://%s", ip))
   140  		var buf bytes.Buffer
   141  		cmd.Stdout = &buf
   142  		err = cmd.Run()
   143  		res := buf.String()
   144  
   145  		if err != nil || strings.TrimSpace(res) != fcontent {
   146  			log.Fatal(err, buf.String())
   147  		}
   148  
   149  		// Count the number of bytes in feature_test.go via nginx going via port remap on host
   150  		cmd = exec.Command("curl", "-f", "http://localhost:8888")
   151  		buf.Reset()
   152  		cmd.Stdout = &buf
   153  		err = cmd.Run()
   154  		res = buf.String()
   155  		if err != nil || strings.TrimSpace(res) != fcontent {
   156  			log.Fatal(err, buf.String())
   157  		}
   158  
   159  		// PowerOff stops the container
   160  		task, _ = vm.PowerOff(ctx)
   161  		_ = task.Wait(ctx)
   162  		// Destroy deletes the container
   163  		task, _ = vm.Destroy(ctx)
   164  		_ = task.Wait(ctx)
   165  
   166  		fmt.Printf("%d diff", buf.Len()-len(fcontent))
   167  	})
   168  	// Output: 0 diff
   169  }