-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathforeman_test.go
245 lines (228 loc) · 7.33 KB
/
foreman_test.go
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package foreman
import (
"net/http"
"strings"
"testing"
)
func TestNewDefaults(t *testing.T) {
c := New(Options{})
if c.httpClient != defaultHTTPClient {
t.Errorf("expected the default used http client to be %p %#v but got %p %#v", defaultHTTPClient, defaultHTTPClient, c.httpClient, c.httpClient)
}
if c.mods == nil {
t.Errorf("expected mods not to be nil")
}
}
func setRequest(t *testing.T, method, resource string) *http.Request {
req, err := http.NewRequest(method, resource, nil)
if err != nil {
t.Errorf("expected the request creation not to return error: %s", err)
}
return req
}
func setTestRequest(t *testing.T) *http.Request {
return setRequest(t, http.MethodGet, "")
}
func assertModifyNotNil(t *testing.T, c Client) {
if c.mods == nil {
t.Errorf("expected mods not to be nil")
t.FailNow()
}
}
func TestNewSetsHeadersCorrectly(t *testing.T) {
tt := []struct {
key string
val string
}{
{"Content-Type", "application/json"},
{"User-Agent", "GoForemanAPIClient"},
}
c := New(Options{})
assertModifyNotNil(t, c)
req, err := c.mods.Modify(setTestRequest(t))
if err != nil {
t.Errorf("expected Modify to return error: %s", err)
}
for _, te := range tt {
if req.Header.Get(te.key) != te.val {
t.Errorf("expected request %s to be '%s' but got '%s'", te.key, te.val, req.Header.Get(te.key))
}
}
}
func TestNewDefaultsIsCorrect(t *testing.T) {
c := New(Options{})
assertModifyNotNil(t, c)
req, err := c.mods.Modify(setTestRequest(t))
if err != nil {
t.Errorf("expected Modify to return error: %s", err)
}
if err != nil {
t.Errorf("expected Modify to return error: %s", err)
}
expURL := strings.TrimPrefix(defaultAddress, "http://")
if req.URL.Host != expURL {
t.Errorf("expected request URL Host to be '%s' but got '%s'", expURL, req.URL.Host)
}
if req.URL.Scheme != "http" {
t.Errorf("expected request URL Scheme to be 'http' but got '%s'", req.URL.Scheme)
}
if req.URL.Path != "/api/"+defaultAPIVersion+"/" {
t.Errorf("expected request URL Path to be '%s' but got '%s'", "/api/"+defaultAPIVersion+"/", req.URL.Path)
}
_, _, k := req.BasicAuth()
if k {
t.Error("expected request to not have BasicAuth")
}
}
func TestNewSetsForemanURL(t *testing.T) {
tt := []struct {
address string
apiVer string
scheme string
host string
path string
}{
{"http://10.20.30.40:8989", "", "http", "10.20.30.40:8989", "/api/v2/"},
{"https://172.20.30.40:8989", "", "https", "172.20.30.40:8989", "/api/v2/"},
{"https://foreman.internal.acme.io:8989", "", "https", "foreman.internal.acme.io:8989", "/api/v2/"},
{"https://foreman.acme.io:80", "v5", "https", "foreman.acme.io:80", "/api/v5/"},
}
for _, te := range tt {
c := New(Options{
Address: te.address,
APIVersion: te.apiVer,
})
assertModifyNotNil(t, c)
req, err := c.mods.Modify(setTestRequest(t))
if err != nil {
t.Errorf("expected Modify to not return error: %s", err)
t.FailNow()
}
if req.URL.Scheme != te.scheme {
t.Errorf("expected request URL Scheme to be '%s' but got '%s'", te.scheme, req.URL.Scheme)
}
if req.URL.Host != te.host {
t.Errorf("expected request URL Host to be '%s' but got '%s'", te.host, req.URL.Host)
}
if req.URL.Path != te.path {
t.Errorf("expected request URL Host to be '%s' but got '%s'", te.path, req.URL.Path)
}
}
}
func TestBadAddress(t *testing.T) {
opts := Options{
Address: "10.20.30.40:8989",
}
c := New(opts)
assertModifyNotNil(t, c)
if _, err := c.mods.Modify(setTestRequest(t)); err == nil {
t.Error("expected Modify to return error")
}
if _, err := c.Do(setTestRequest(t)); err == nil {
t.Error("expected Modify to return error")
}
}
func TestHasBasicAuth(t *testing.T) {
tt := []Options{
{Username: "dave"},
{Username: "admin", Password: "helloworld!"},
}
for _, te := range tt {
c := New(te)
assertModifyNotNil(t, c)
req, err := c.mods.Modify(setTestRequest(t))
if err != nil {
t.Errorf("expected Modify to return error: %s", err)
}
u, p, k := req.BasicAuth()
if !k {
t.Error("expected request to have BasicAuth")
}
if u != te.Username {
t.Errorf("expected Username to be '%s' but got '%s'", te.Username, u)
}
if p != te.Password {
t.Errorf("expected Username to be '%s' but got '%s'", te.Password, p)
}
}
}
func TestNewSetHTTPClient(t *testing.T) {
opts := Options{
HTTPClient: &http.Client{},
}
c := New(opts)
if c.httpClient == defaultHTTPClient {
t.Errorf("expected HTTP client(%p) '%v' not to be equal to the default one(%p) '%v'", c.httpClient, c.httpClient, defaultHTTPClient, defaultHTTPClient)
}
}
type funcRoundTripper func(*http.Request) (*http.Response, error)
func (frt funcRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
return frt(req)
}
func newTestingHTTPClient(ops func(*http.Request) (*http.Response, error)) *http.Client {
return &http.Client{
Transport: funcRoundTripper(ops),
}
}
func TestClientDo(t *testing.T) {
tt := []struct {
resource string
username string
password string
basicAuth bool
address string
apiVer string
scheme string
host string
path string
rawQuery string
}{
{"hosts", "admin", "newpass1", true, "http://foreman.acme.io", "", "http", "foreman.acme.io", "/api/v2/hosts", ""},
{"", "admin", "", true, "https://foreman.acme.io", "v3", "https", "foreman.acme.io", "/api/v3/", ""},
{"", "", "", false, "https://foreman.acme.io", "v3", "https", "foreman.acme.io", "/api/v3/", ""},
{"http://hello.world.com/hostl", "", "", false, "https://foreman.acme.io", "v3", "https", "foreman.acme.io", "/api/v3/hostl", ""},
{"hosts?search=host2", "", "", false, "https://foreman.acme.io", "v3", "https", "foreman.acme.io", "/api/v3/hosts", "search=host2"},
{"hosts?search=host2&page=1", "", "", false, "https://foreman.acme.io", "v3", "https", "foreman.acme.io", "/api/v3/hosts", "search=host2&page=1"},
}
for _, te := range tt {
c := New(Options{
Username: te.username,
Password: te.password,
Address: te.address,
APIVersion: te.apiVer,
HTTPClient: newTestingHTTPClient(func(req *http.Request) (*http.Response, error) {
u, p, k := req.BasicAuth()
if k != te.basicAuth {
t.Errorf("expected the request BasicAuth to be set to '%v' but got '%v'", te.basicAuth, k)
}
if u != te.username {
t.Errorf("expected the Basic Auth username to be '%s' but got '%s'", te.username, u)
}
if p != te.password {
t.Errorf("expected the Basic Auth password to be '%s' but got '%s'", te.password, p)
}
if req.URL.Scheme != te.scheme {
t.Errorf("expected the request URL Scheme to be '%s' but got '%s'", te.scheme, req.URL.Scheme)
}
if req.URL.Host != te.host {
t.Errorf("expected the request URL Host to be '%s' but got '%s'", te.host, req.URL.Host)
}
if req.URL.Path != te.path {
t.Errorf("expected the request URL Path to be '%s' but got '%s'", te.path, req.URL.Path)
}
if req.URL.RawQuery != te.rawQuery {
t.Errorf("expected the request URL RawQuery to be '%s' but got '%s'", te.rawQuery, req.URL.RawQuery)
}
if req.Method != http.MethodHead {
t.Errorf("expected the request Method to be '%s' but got '%s'", http.MethodHead, req.Method)
}
return &http.Response{}, nil
}),
})
assertModifyNotNil(t, c)
_, err := c.Do(setRequest(t, http.MethodHead, te.resource))
if err != nil {
t.Errorf("expected Head not to return errors: %s", err)
}
}
}