forked from HewlettPackard/morpheus-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
168 lines (152 loc) · 5.06 KB
/
client_test.go
File metadata and controls
168 lines (152 loc) · 5.06 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// This provides common functions for all morpheus.Client tests.
package morpheus_test
import (
_ "encoding/json"
"fmt"
"log"
"os"
_ "strconv"
"testing"
"github.com/gomorpheus/morpheus-go-sdk"
)
var (
// DEBUG = (os.Getenv("MORPHEUS_DEBUG") == "true")
testUrl = os.Getenv("MORPHEUS_TEST_URL")
testUsername = os.Getenv("MORPHEUS_TEST_USERNAME")
testPassword = os.Getenv("MORPHEUS_TEST_PASSWORD")
testAccessToken = os.Getenv("MORPHEUS_TEST_TOKEN")
// if os.Getenv("MORPHEUS_TEST_ACCESS_TOKEN") != "" {
// testAccessToken = os.Getenv("MORPHEUS_TEST_ACCESS_TOKEN")
// }
testRefreshToken = os.Getenv("MORPHEUS_TEST_REFRESH_TOKEN")
// testUrl string
// testUsername string
// testPassword string
sharedTestClient *morpheus.Client
)
// TestMain hooks up setup/teardown methods
// func TestMain(m *testing.M) {
// setup()
// code := m.Run()
// teardown()
// os.Exit(code)
// }
// func setup() {
// //t.Logf(fmt.Sprintf("Setup test suite..."))
// log.Printf(fmt.Sprintf("Setup test suite..."))
// }
// func teardown() {
// //log.Printf("Teardown test suite...")
// if sharedTestClient != nil {
// // t.Logf(fmt.Sprintf("Test Client Summary | Success: %d, Error: %d", sharedTestClient.RequestCount(), sharedTestClient.ErrorCount()))
// log.Printf(fmt.Sprintf("Test Client Summary | Success: %d, Error: %d", sharedTestClient.RequestCount(), sharedTestClient.ErrorCount())
// }
// }
func getNewClient(t *testing.T) *morpheus.Client {
if testUrl == "" {
t.Errorf("MORPHEUS_TEST_URL must be set to to run tests")
}
t.Logf("Initializing new client for %v", testUrl)
client := morpheus.NewClient(testUrl)
return client
}
// getTestClient returns a Client that is shared between all tests.
// It is configured via the following environment variables.
// MORPHEUS_TEST_URL - Morpheus Appliance URL
// MORPHEUS_TEST_ACCESS_TOKEN - Morpheus API access token
// MORPHEUS_TEST_USERNAME - Morpheus username
// MORPHEUS_TEST_PASSWORD - Morpheus password
func getTestClient(t *testing.T) *morpheus.Client {
if sharedTestClient == nil {
if testUrl == "" {
panic("MORPHEUS_TEST_URL must be set to to run tests.")
}
client := morpheus.NewClient(testUrl)
if testAccessToken != "" {
client.SetAccessToken(testAccessToken, testRefreshToken, 0, "write")
// validate api access token by hitting /api/whoami
resp, err := client.Whoami()
// assertResponse(t, resp, err)
if resp.Success == true {
whoamiResult := resp.Result.(*morpheus.WhoamiResult)
currentUsername := whoamiResult.User.Username
// So this just sets the testUsername
// it might be better to error if the name does not match...
if testUsername != currentUsername {
// need to stop the test right away!
panic(fmt.Sprintf("MORPHEUS_TEST_USERNAME does not match that of MORPHEUS_TEST_TOKEN. Expected [%v], got [%v]", testUsername, currentUsername))
// t.Fatalf(fmt.Sprintf("MORPHEUS_TEST_USERNAME does not match that of MORPHEUS_TEST_TOKEN. Expected [%v], got [%v]", testUsername, currentUsername))
}
testUsername = currentUsername
} else {
assertResponse(t, resp, err)
// panic("MORPHEUS_TEST_TOKEN could not be validated.")
}
} else {
// authenticate with username and password
if testUsername == "" || testPassword == "" {
panic("MORPHEUS_TEST_TOKEN or MORPHEUS_TEST_USERNAME and MORPHEUS_TEST_PASSWORD must be set to to run tests.")
}
log.Printf("Initializing test client for %v @ %v", testUsername, testUrl)
client.SetUsernameAndPassword(testUsername, testPassword)
resp, err := client.Login()
assertResponse(t, resp, err)
}
sharedTestClient = client
}
return sharedTestClient
}
// func parseInt64(s string) (int64, error) {
// n, err := strconv.ParseInt(s, 10, 64)
// return n, err
// }
func TestPing(t *testing.T) {
client := getNewClient(t)
testRequest := &morpheus.Request{
Method: "GET",
Path: "/api/setup/check",
QueryParams: map[string]string{
"foo": "bar",
"gotest": "true",
},
}
resp, err := client.Execute(testRequest)
assertResponse(t, resp, err)
}
func TestLogin(t *testing.T) {
client := getNewClient(t)
client.SetUsernameAndPassword(testUsername, testPassword)
resp, err := client.Login()
assertResponse(t, resp, err)
}
func TestLoginWithToken(t *testing.T) {
client := getNewClient(t)
client.SetAccessToken(testAccessToken, testRefreshToken, 0, "write")
resp, err := client.Whoami()
assertResponse(t, resp, err)
}
func TestLogout(t *testing.T) {
client := getNewClient(t)
client.SetUsernameAndPassword(testUsername, testPassword)
resp, err := client.Login()
assertResponse(t, resp, err)
if err == nil {
_, err = client.Logout()
assertError(t, err)
// assertResponse(t, resp, err)
}
}
func TestLoginRepeated(t *testing.T) {
client := getNewClient(t)
client.SetUsernameAndPassword(testUsername, testPassword)
resp, err := client.Login()
assertResponse(t, resp, err)
if err == nil {
_, _ = client.Login()
_, _ = client.Login()
_, _ = client.Login()
_, _ = client.Login()
// assertResponse(t, resp, err)
assertError(t, err)
}
}