forked from vmware/govmomi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.go
More file actions
73 lines (62 loc) · 1.41 KB
/
helper.go
File metadata and controls
73 lines (62 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// © Broadcom. All Rights Reserved.
// The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
// SPDX-License-Identifier: Apache-2.0
package test
import (
"context"
"net/url"
"os"
"os/exec"
"runtime"
"testing"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/soap"
"github.
60A8
com/vmware/govmomi/vim25/types"
)
func HasDocker() bool {
if runtime.GOOS != "linux" {
return false
}
if _, err := exec.LookPath("docker"); err != nil {
return false
}
return true
}
// URL parses the GOVMOMI_TEST_URL environment variable if set.
func URL() *url.URL {
s := os.Getenv("GOVMOMI_TEST_URL")
if s == "" {
return nil
}
u, err := soap.ParseURL(s)
if err != nil {
panic(err)
}
return u
}
// NewAuthenticatedClient creates a new vim25.Client, authenticates the user
// specified in the test URL, and returns it.
func NewAuthenticatedClient(t *testing.T) *vim25.Client {
u := URL()
if u == nil {
t.SkipNow()
}
soapClient := soap.NewClient(u, true)
vimClient, err := vim25.NewClient(context.Background(), soapClient)
if err != nil {
t.Fatal(err)
}
req := types.Login{
This: *vimClient.ServiceContent.SessionManager,
}
req.UserName = u.User.Username()
if pw, ok := u.User.Password(); ok {
req.Password = pw
}
_, err = methods.Login(context.Background(), vimClient, &req)
if err != nil {
t.Fatal(err)
}
return vimClient
}