github.com/vmware/govmomi@v0.51.0/test/helper.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 test 6 7 import ( 8 "context" 9 "net/url" 10 "os" 11 "os/exec" 12 "runtime" 13 "testing" 14 15 "github.com/vmware/govmomi/vim25" 16 "github.com/vmware/govmomi/vim25/methods" 17 "github.com/vmware/govmomi/vim25/soap" 18 "github.com/vmware/govmomi/vim25/types" 19 ) 20 21 func HasDocker() bool { 22 if runtime.GOOS != "linux" { 23 return false 24 } 25 if _, err := exec.LookPath("docker"); err != nil { 26 return false 27 } 28 return true 29 } 30 31 // URL parses the GOVMOMI_TEST_URL environment variable if set. 32 func URL() *url.URL { 33 s := os.Getenv("GOVMOMI_TEST_URL") 34 if s == "" { 35 return nil 36 } 37 u, err := soap.ParseURL(s) 38 if err != nil { 39 panic(err) 40 } 41 return u 42 } 43 44 // NewAuthenticatedClient creates a new vim25.Client, authenticates the user 45 // specified in the test URL, and returns it. 46 func NewAuthenticatedClient(t *testing.T) *vim25.Client { 47 u := URL() 48 if u == nil { 49 t.SkipNow() 50 } 51 52 soapClient := soap.NewClient(u, true) 53 vimClient, err := vim25.NewClient(context.Background(), soapClient) 54 if err != nil { 55 t.Fatal(err) 56 } 57 58 req := types.Login{ 59 This: *vimClient.ServiceContent.SessionManager, 60 } 61 62 req.UserName = u.User.Username() 63 if pw, ok := u.User.Password(); ok { 64 req.Password = pw 65 } 66 67 _, err = methods.Login(context.Background(), vimClient, &req) 68 if err != nil { 69 t.Fatal(err) 70 } 71 72 return vimClient 73 }