Skip to content

Commit a68dc42

Browse files
authored
Introduce GRPC Proxy unit tests (#2428)
This commit introduces a series of unit tests that ensure that GRPC connections are configurable through the use of the GRPC_PROXY environment variable. Signed-off-by: Alexander Greene <[email protected]>
1 parent d36949b commit a68dc42

File tree

1 file changed

+243
-0
lines changed

1 file changed

+243
-0
lines changed

pkg/controller/registry/grpc/source_test.go

+243
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"context"
66
"fmt"
77
"net"
8+
"net/url"
9+
"os"
810
"sync"
911
"testing"
1012
"time"
@@ -149,3 +151,244 @@ func TestConnectionEvents(t *testing.T) {
149151
t.Run(tt.name, test(tt))
150152
}
151153
}
154+
155+
func TestGetEnvAny(t *testing.T) {
156+
type envVar struct {
157+
key string
158+
value string
159+
}
160+
161+
type testcase struct {
162+
name string
163+
envVars []envVar
164+
expectedValue string
165+
}
166+
167+
test := func(tt testcase) func(t *testing.T) {
168+
return func(t *testing.T) {
169+
for _, envVar := range tt.envVars {
170+
os.Setenv(envVar.key, envVar.value)
171+
}
172+
173+
defer func() {
174+
for _, envVar := range tt.envVars {
175+
os.Setenv(envVar.key, "")
176+
}
177+
}()
178+
179+
require.Equal(t, getEnvAny("NO_PROXY", "no_proxy"), tt.expectedValue)
180+
}
181+
}
182+
183+
cases := []testcase{
184+
{
185+
name: "NotFound",
186+
expectedValue: "",
187+
},
188+
{
189+
name: "LowerCaseFound",
190+
envVars: []envVar{
191+
{
192+
key: "no_proxy",
193+
value: "foo",
194+
},
195+
},
196+
expectedValue: "foo",
197+
},
198+
{
199+
name: "UpperCaseFound",
200+
envVars: []envVar{
201+
{
202+
key: "NO_PROXY",
203+
value: "bar",
204+
},
205+
},
206+
expectedValue: "bar",
207+
},
208+
{
209+
name: "OrderPreference",
210+
envVars: []envVar{
211+
{
212+
key: "no_proxy",
213+
value: "foo",
214+
},
215+
{
216+
key: "NO_PROXY",
217+
value: "bar",
218+
},
219+
},
220+
expectedValue: "bar",
221+
},
222+
}
223+
224+
for _, tt := range cases {
225+
t.Run(tt.name, test(tt))
226+
}
227+
}
228+
229+
func TestGetGRPCProxyEnv(t *testing.T) {
230+
type envVar struct {
231+
key string
232+
value string
233+
}
234+
235+
type testcase struct {
236+
name string
237+
envVars []envVar
238+
expectedValue string
239+
}
240+
241+
test := func(tt testcase) func(t *testing.T) {
242+
return func(t *testing.T) {
243+
for _, envVar := range tt.envVars {
244+
os.Setenv(envVar.key, envVar.value)
245+
}
246+
247+
defer func() {
248+
for _, envVar := range tt.envVars {
249+
os.Setenv(envVar.key, "")
250+
}
251+
}()
252+
253+
require.Equal(t, getGRPCProxyEnv(), tt.expectedValue)
254+
}
255+
}
256+
257+
cases := []testcase{
258+
{
259+
name: "NotFound",
260+
expectedValue: "",
261+
},
262+
{
263+
name: "LowerCaseFound",
264+
envVars: []envVar{
265+
{
266+
key: "grpc_proxy",
267+
value: "foo",
268+
},
269+
},
270+
expectedValue: "foo",
271+
},
272+
{
273+
name: "UpperCaseFound",
274+
envVars: []envVar{
275+
{
276+
key: "GRPC_PROXY",
277+
value: "bar",
278+
},
279+
},
280+
expectedValue: "bar",
281+
},
282+
{
283+
name: "UpperCasePreference",
284+
envVars: []envVar{
285+
{
286+
key: "grpc_proxy",
287+
value: "foo",
288+
},
289+
{
290+
key: "GRPC_PROXY",
291+
value: "bar",
292+
},
293+
},
294+
expectedValue: "bar",
295+
},
296+
}
297+
298+
for _, tt := range cases {
299+
t.Run(tt.name, test(tt))
300+
}
301+
}
302+
303+
func TestGRPCProxyURL(t *testing.T) {
304+
type envVar struct {
305+
key string
306+
value string
307+
}
308+
309+
type testcase struct {
310+
name string
311+
address string
312+
envVars []envVar
313+
expectedProxy string
314+
expectedError error
315+
}
316+
317+
test := func(tt testcase) func(t *testing.T) {
318+
return func(t *testing.T) {
319+
for _, envVar := range tt.envVars {
320+
os.Setenv(envVar.key, envVar.value)
321+
}
322+
323+
defer func() {
324+
for _, envVar := range tt.envVars {
325+
os.Setenv(envVar.key, "")
326+
}
327+
}()
328+
329+
var expectedProxyURL *url.URL
330+
var err error
331+
if tt.expectedProxy != "" {
332+
expectedProxyURL, err = url.Parse(tt.expectedProxy)
333+
require.NoError(t, err)
334+
}
335+
336+
proxyUrl, err := grpcProxyURL(tt.address)
337+
require.Equal(t, expectedProxyURL, proxyUrl)
338+
require.Equal(t, tt.expectedError, err)
339+
}
340+
}
341+
342+
cases := []testcase{
343+
{
344+
name: "NoGRPCProxySet",
345+
address: "foo.com:8080",
346+
expectedProxy: "",
347+
expectedError: nil,
348+
},
349+
{
350+
name: "GRPCProxyFoundForAddress",
351+
address: "foo.com:8080",
352+
envVars: []envVar{
353+
{
354+
key: "GRPC_PROXY",
355+
value: "http://my-proxy:8080",
356+
},
357+
},
358+
expectedProxy: "http://my-proxy:8080",
359+
expectedError: nil,
360+
},
361+
{
362+
name: "GRPCNoProxyIncludesAddress",
363+
address: "foo.com:8080",
364+
envVars: []envVar{
365+
{
366+
key: "GRPC_PROXY",
367+
value: "http://my-proxy:8080",
368+
},
369+
{
370+
key: "NO_PROXY",
371+
value: "foo.com:8080",
372+
},
373+
},
374+
expectedProxy: "",
375+
expectedError: nil,
376+
},
377+
{
378+
name: "MissingPort",
379+
address: "foo.com",
380+
expectedProxy: "",
381+
expectedError: error(&net.AddrError{Err: "missing port in address", Addr: "foo.com"}),
382+
},
383+
{
384+
name: "TooManyColons",
385+
address: "http://bar.com:8080",
386+
expectedProxy: "",
387+
expectedError: error(&net.AddrError{Err: "too many colons in address", Addr: "http://bar.com:8080"}),
388+
},
389+
}
390+
391+
for _, tt := range cases {
392+
t.Run(tt.name, test(tt))
393+
}
394+
}

0 commit comments

Comments
 (0)