-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathdial_test.go
More file actions
301 lines (268 loc) · 7.13 KB
/
dial_test.go
File metadata and controls
301 lines (268 loc) · 7.13 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2016 Google LLC.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package grpc
import (
"bytes"
"context"
"log"
"strings"
"testing"
"cloud.google.com/go/auth/grpctransport"
"cloud.google.com/go/compute/metadata"
"github.com/google/go-cmp/cmp"
"golang.org/x/oauth2/google"
"google.golang.org/api/internal"
"google.golang.org/api/option"
"google.golang.org/grpc"
)
func TestDial(t *testing.T) {
oldDialContext := dialContext
// Replace package var in order to assert DialContext args.
dialContext = func(ctxGot context.Context, target string, opts ...grpc.DialOption) (conn *grpc.ClientConn, err error) {
if len(opts) != 3 {
t.Fatalf("got: %d, want: 3", len(opts))
}
return nil, nil
}
defer func() {
dialContext = oldDialContext
}()
var o internal.DialSettings
dial(context.Background(), false, &o)
}
func TestDialPoolNewAuthDialOptions(t *testing.T) {
oldDialContextNewAuth := dialContextNewAuth
var wantNumOpts int
var universeDomain string
// Replace package var in order to assert DialContext args.
dialContextNewAuth = func(ctx context.Context, secure bool, opts *grpctransport.Options) (grpctransport.GRPCClientConnPool, error) {
if len(opts.GRPCDialOpts) != wantNumOpts {
t.Fatalf("got: %d, want: %d", len(opts.GRPCDialOpts), wantNumOpts)
}
if opts.UniverseDomain != universeDomain {
t.Fatalf("got: %q, want: %q", opts.UniverseDomain, universeDomain)
}
return nil, nil
}
defer func() {
dialContextNewAuth = oldDialContextNewAuth
}()
for _, testcase := range []struct {
name string
ds *internal.DialSettings
wantNumOpts int
}{
{
name: "no dial options",
ds: &internal.DialSettings{},
wantNumOpts: 0,
},
{
name: "with user agent",
ds: &internal.DialSettings{
UserAgent: "test",
},
wantNumOpts: 1,
},
{
name: "universe domain",
ds: &internal.DialSettings{
UniverseDomain: "example.com",
},
wantNumOpts: 0,
},
} {
t.Run(testcase.name, func(t *testing.T) {
wantNumOpts = testcase.wantNumOpts
universeDomain = testcase.ds.UniverseDomain
dialPoolNewAuth(context.Background(), false, 1, testcase.ds)
})
}
}
func TestPrepareDialOptsNewAuth(t *testing.T) {
for _, testcase := range []struct {
name string
ds *internal.DialSettings
wantNumOpts int
}{
{
name: "empty",
ds: &internal.DialSettings{},
wantNumOpts: 0,
},
{
name: "user agent",
ds: &internal.DialSettings{
UserAgent: "test",
},
wantNumOpts: 1,
},
} {
t.Run(testcase.name, func(t *testing.T) {
got := prepareDialOptsNewAuth(testcase.ds)
if len(got) != testcase.wantNumOpts {
t.Fatalf("got %d options, want %d options", len(got), testcase.wantNumOpts)
}
})
}
}
func TestCheckDirectPathEndPoint(t *testing.T) {
for _, testcase := range []struct {
name string
endpoint string
want bool
}{
{
name: "empty endpoint are disallowed",
endpoint: "",
want: false,
},
{
name: "dns schemes are allowed",
endpoint: "dns:///foo",
want: true,
},
{
name: "host without no prefix are allowed",
endpoint: "foo",
want: true,
},
{
name: "host with port are allowed",
endpoint: "foo:1234",
want: true,
},
{
name: "non-dns schemes are disallowed",
endpoint: "https://foo",
want: false,
},
} {
t.Run(testcase.name, func(t *testing.T) {
if got := checkDirectPathEndPoint(testcase.endpoint); got != testcase.want {
t.Fatalf("got %v, want %v", got, testcase.want)
}
})
}
}
func TestLogDirectPathMisconfigAttrempDirectPathNotSet(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
o := &internal.DialSettings{}
o.EnableDirectPathXds = true
endpoint := "abc.googleapis.com"
creds, err := internal.Creds(context.Context(context.Background()), o)
if err != nil {
t.Fatalf("failed to create creds")
}
buf := bytes.Buffer{}
log.SetOutput(&buf)
logDirectPathMisconfig(endpoint, creds.TokenSource, o)
wantedLog := "WARNING: DirectPath is misconfigured. Please set the EnableDirectPath option along with the EnableDirectPathXds option."
if !strings.Contains(buf.String(), wantedLog) {
t.Fatalf("got: %v, want: %v", buf.String(), wantedLog)
}
}
func TestLogDirectPathMisconfigWrongCredential(t *testing.T) {
o := &internal.DialSettings{}
o.EnableDirectPath = true
o.EnableDirectPathXds = true
endpoint := "abc.googleapis.com"
creds := &google.Credentials{}
buf := bytes.Buffer{}
log.SetOutput(&buf)
logDirectPathMisconfig(endpoint, creds.TokenSource, o)
wantedLog := "WARNING: DirectPath is misconfigured. Please make sure the token source is fetched from GCE metadata server and the default service account is used."
if !strings.Contains(buf.String(), wantedLog) {
t.Fatalf("got: %v, want: %v", buf.String(), wantedLog)
}
}
func TestLogDirectPathMisconfigNotOnGCE(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
o := &internal.DialSettings{}
o.EnableDirectPath = true
o.EnableDirectPathXds = true
endpoint := "abc.googleapis.com"
creds, err := internal.Creds(context.Context(context.Background()), o)
if err != nil {
t.Fatalf("failed to create creds")
}
buf := bytes.Buffer{}
log.SetOutput(&buf)
logDirectPathMisconfig(endpoint, creds.TokenSource, o)
if !metadata.OnGCE() {
wantedLog := "WARNING: DirectPath is misconfigured. DirectPath is only available in a GCE environment."
if !strings.Contains(buf.String(), wantedLog) {
t.Fatalf("got: %v, want: %v", buf.String(), wantedLog)
}
}
}
func TestGRPCAPIKey_GetRequestMetadata(t *testing.T) {
for _, test := range []struct {
apiKey string
reason string
}{
{
apiKey: "MY_API_KEY",
reason: "MY_REQUEST_REASON",
},
} {
ts := grpcAPIKey{
apiKey: test.apiKey,
requestReason: test.reason,
}
got, err := ts.GetRequestMetadata(context.Background())
if err != nil {
t.Fatal(err)
}
want := map[string]string{
"X-goog-api-key": ts.apiKey,
"X-goog-request-reason": ts.requestReason,
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("mismatch (-want +got):\n%s", diff)
}
}
}
func TestConfiguredGRPCConnPoolSize(t *testing.T) {
tests := []struct {
name string
opts []option.ClientOption
wantSize int
wantErr bool
}{
{
name: "No options",
opts: []option.ClientOption{},
wantSize: 0,
wantErr: false,
},
{
name: "WithGRPCConnectionPool(5)",
opts: []option.ClientOption{option.WithGRPCConnectionPool(5)},
wantSize: 5,
wantErr: false,
},
{
name: "WithGRPCConnectionPool(0)",
opts: []option.ClientOption{option.WithGRPCConnectionPool(0)},
wantSize: 0,
wantErr: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
gotSize, err := UserConfiguredGRPCConnPoolSize(tc.opts...)
if (err != nil) != tc.wantErr {
t.Errorf("ConfiguredGRPCConnPoolSize() returned error: %v, wantErr: %v", err, tc.wantErr)
}
if !tc.wantErr && gotSize != tc.wantSize {
t.Errorf("ConfiguredGRPCConnPoolSize() = %d, want %d", gotSize, tc.wantSize)
}
})
}
}