-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
227 lines (213 loc) · 7.01 KB
/
Copy pathclient_test.go
File metadata and controls
227 lines (213 loc) · 7.01 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
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
// client_test.go
package cinc
import (
"crypto/tls"
"net/http"
"strings"
"testing"
"time"
)
func TestNewClient_OK(t *testing.T) {
c, err := NewClient(Config{
ServerURL: "https://chef.example.com/", Org: "myorg",
ClientName: "node1", Key: testRSAKey(t),
})
if err != nil {
t.Fatalf("NewClient: %v", err)
}
if got := c.orgPath("/nodes"); got != "/organizations/myorg/nodes" {
t.Fatalf("orgPath = %q", got)
}
if c.Nodes == nil {
t.Fatal("Nodes service not wired")
}
}
func TestNewClient_BadConfig(t *testing.T) {
if _, err := NewClient(Config{Org: "o"}); err == nil {
t.Fatal("expected error for invalid config")
}
}
func TestNewClient_BadURL(t *testing.T) {
if _, err := NewClient(Config{
ServerURL: "://bad", Org: "o", ClientName: "c", Key: testRSAKey(t),
}); err == nil {
t.Fatal("expected error for malformed URL")
}
}
func TestClient_now(t *testing.T) {
c, _ := NewClient(Config{
ServerURL: "https://h", Org: "o", ClientName: "c", Key: testRSAKey(t),
})
c.clock = func() time.Time { return time.Unix(0, 0).UTC() }
if got := strings.TrimSpace(c.timestamp()); got != "1970-01-01T00:00:00Z" {
t.Fatalf("timestamp = %q", got)
}
}
func TestNewClient_CachesBaseURLString(t *testing.T) {
c, err := NewClient(Config{
ServerURL: "https://chef.example.com/", Org: "o",
ClientName: "c", Key: testRSAKey(t),
})
if err != nil {
t.Fatal(err)
}
// The serialized base URL is cached once so the request hot path does not
// re-serialize *url.URL on every call.
if c.baseURLStr != c.baseURL.String() {
t.Fatalf("baseURLStr = %q, want %q", c.baseURLStr, c.baseURL.String())
}
if c.baseURLStr != "https://chef.example.com" {
t.Fatalf("baseURLStr = %q, want trailing slash trimmed", c.baseURLStr)
}
}
func TestNewClient_TrimsTrailingSlash(t *testing.T) {
c, err := NewClient(Config{
ServerURL: "https://chef.example.com/", Org: "o",
ClientName: "c", Key: testRSAKey(t),
})
if err != nil {
t.Fatal(err)
}
if got := c.baseURL.String(); got != "https://chef.example.com" {
t.Fatalf("baseURL = %q, want trailing slash trimmed", got)
}
}
func TestNewClient_OrgPathHandlesLeadingSlash(t *testing.T) {
c, _ := NewClient(Config{
ServerURL: "https://h", Org: "myorg",
ClientName: "c", Key: testRSAKey(t),
})
// Either form should yield the same canonical path.
if got := c.orgPath("nodes"); got != "/organizations/myorg/nodes" {
t.Errorf("orgPath(no slash) = %q", got)
}
if got := c.orgPath("/nodes"); got != "/organizations/myorg/nodes" {
t.Errorf("orgPath(slash) = %q", got)
}
}
func TestNewClient_WiresAllServices(t *testing.T) {
c, err := NewClient(Config{
ServerURL: "https://h", Org: "o",
ClientName: "c", Key: testRSAKey(t),
})
if err != nil {
t.Fatal(err)
}
if c.Nodes == nil || c.Roles == nil || c.Environments == nil ||
c.Clients == nil || c.DataBags == nil || c.Search == nil ||
c.Cookbooks == nil || c.CookbookArtifacts == nil ||
c.Keys == nil || c.Groups == nil ||
c.Status == nil || c.License == nil ||
c.Policies == nil || c.PolicyGroups == nil ||
c.Orgs == nil || c.Users == nil ||
c.Containers == nil || c.ACLs == nil ||
c.RequiredRecipe == nil || c.Associations == nil ||
c.Principals == nil || c.Universe == nil || c.Stats == nil {
t.Fatal("at least one service is unwired")
}
}
func TestNewClient_SkipTLSVerify(t *testing.T) {
c, err := NewClient(Config{
ServerURL: "https://h", Org: "o",
ClientName: "c", Key: testRSAKey(t),
}, WithSkipTLSVerify(true))
if err != nil {
t.Fatal(err)
}
tr, ok := c.httpClient.Transport.(*http.Transport)
if !ok {
t.Fatalf("expected *http.Transport, got %T", c.httpClient.Transport)
}
if tr.TLSClientConfig == nil || !tr.TLSClientConfig.InsecureSkipVerify {
t.Fatal("InsecureSkipVerify not enabled by WithSkipTLSVerify(true)")
}
}
func TestNewClient_SkipTLSVerify_PreservesDefaultTuning(t *testing.T) {
c, err := NewClient(Config{
ServerURL: "https://h", Org: "o",
ClientName: "c", Key: testRSAKey(t),
}, WithSkipTLSVerify(true))
if err != nil {
t.Fatal(err)
}
tr := c.httpClient.Transport.(*http.Transport)
// Cloning http.DefaultTransport must preserve HTTP/2, proxy support, and
// connection pooling rather than dropping them on the skipTLSVerify path.
if !tr.ForceAttemptHTTP2 {
t.Error("ForceAttemptHTTP2 dropped; HTTP/2 negotiation disabled")
}
if tr.Proxy == nil {
t.Error("Proxy dropped; HTTPS_PROXY/NO_PROXY no longer honored")
}
if tr.DialContext == nil {
t.Error("DialContext dropped; keepalive dialing lost")
}
def := http.DefaultTransport.(*http.Transport)
if tr.IdleConnTimeout != def.IdleConnTimeout {
t.Errorf("IdleConnTimeout = %v, want default %v", tr.IdleConnTimeout, def.IdleConnTimeout)
}
}
func TestNewClient_SkipTLSVerify_PreservesCallerTransport(t *testing.T) {
custom := &http.Client{Transport: &http.Transport{
MaxIdleConnsPerHost: 42,
}}
c, err := NewClient(Config{
ServerURL: "https://h", Org: "o",
ClientName: "c", Key: testRSAKey(t),
}, WithHTTPClient(custom), WithSkipTLSVerify(true))
if err != nil {
t.Fatal(err)
}
tr := c.httpClient.Transport.(*http.Transport)
// A caller-supplied *http.Transport must have its tuning preserved, with
// only InsecureSkipVerify flipped on.
if tr.MaxIdleConnsPerHost != 42 {
t.Errorf("MaxIdleConnsPerHost = %d, want 42 (caller transport clobbered)", tr.MaxIdleConnsPerHost)
}
if cfg := tr.TLSClientConfig; cfg == nil || !cfg.InsecureSkipVerify {
t.Fatal("InsecureSkipVerify not enabled")
}
}
func TestNewClient_SkipTLSVerify_PreservesExistingTLSConfig(t *testing.T) {
custom := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{ServerName: "internal.example"},
}}
c, err := NewClient(Config{
ServerURL: "https://h", Org: "o",
ClientName: "c", Key: testRSAKey(t),
}, WithHTTPClient(custom), WithSkipTLSVerify(true))
if err != nil {
t.Fatal(err)
}
cfg := c.httpClient.Transport.(*http.Transport).TLSClientConfig
// Existing TLS settings are preserved; only InsecureSkipVerify is flipped.
if cfg.ServerName != "internal.example" {
t.Errorf("ServerName = %q, want preserved", cfg.ServerName)
}
if !cfg.InsecureSkipVerify {
t.Error("InsecureSkipVerify not enabled")
}
// The caller's TLS config must not be mutated in place.
if custom.Transport.(*http.Transport).TLSClientConfig.InsecureSkipVerify {
t.Error("caller's TLSClientConfig mutated in place")
}
}
func TestNewClient_SkipTLSVerify_PreservesTimeout(t *testing.T) {
custom := &http.Client{Timeout: 99 * time.Second}
c, err := NewClient(Config{
ServerURL: "https://h", Org: "o",
ClientName: "c", Key: testRSAKey(t),
}, WithHTTPClient(custom), WithSkipTLSVerify(true))
if err != nil {
t.Fatal(err)
}
// The httpClient is replaced when skipTLSVerify is set; the timeout
// must still match the original custom client's timeout.
if c.httpClient.Timeout != 99*time.Second {
t.Fatalf("timeout = %v, want 99s", c.httpClient.Timeout)
}
// Sanity check: the resulting Transport really does skip verification.
if cfg := c.httpClient.Transport.(*http.Transport).TLSClientConfig; cfg == nil || !cfg.InsecureSkipVerify {
t.Fatal("InsecureSkipVerify not enabled")
}
}