github.com/vmware/govmomi@v0.51.0/guest/toolbox/example_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 toolbox_test
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"os"
    11  	"os/exec"
    12  
    13  	"github.com/vmware/govmomi/find"
    14  	"github.com/vmware/govmomi/guest/toolbox"
    15  	"github.com/vmware/govmomi/simulator"
    16  	"github.com/vmware/govmomi/test"
    17  	"github.com/vmware/govmomi/vim25"
    18  	"github.com/vmware/govmomi/vim25/types"
    19  )
    20  
    21  func ExampleClient_Run() {
    22  	simulator.Run(func(ctx context.Context, c *vim25.Client) error {
    23  		if !test.HasDocker() {
    24  			fmt.Println("Linux")
    25  			return nil
    26  		}
    27  		vm, err := find.NewFinder(c).VirtualMachine(ctx, "DC0_H0_VM0")
    28  		if err != nil {
    29  			return err
    30  		}
    31  		err = simulator.RunContainer(ctx, c, vm, "nginx")
    32  		if err != nil {
    33  			return err
    34  		}
    35  
    36  		tools, err := toolbox.NewClient(ctx, c, vm, &types.NamePasswordAuthentication{
    37  			Username: "user",
    38  			Password: "pass",
    39  		})
    40  
    41  		cmd := &exec.Cmd{
    42  			Path:   "uname",
    43  			Stdout: os.Stdout,
    44  			Stderr: os.Stderr,
    45  		}
    46  
    47  		return tools.Run(ctx, cmd)
    48  	})
    49  	// Output:
    50  	// Linux
    51  }