Skip to content

Commit 19e291d

Browse files
committed
Introduce GRPC Proxy unit tests
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 ff760a1 commit 19e291d

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

Diff for: pkg/controller/registry/grpc/source_test.go

+134
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,135 @@ func TestConnectionEvents(t *testing.T) {
149151
t.Run(tt.name, test(tt))
150152
}
151153
}
154+
155+
func TestGetGRPCProxyEnv(t *testing.T) {
156+
type envVar struct {
157+
key string
158+
value string
159+
}
160+
161+
type testcase struct {
162+
name string
163+
envVar envVar
164+
expectedValue string
165+
}
166+
167+
test := func(tt testcase) func(t *testing.T) {
168+
return func(t *testing.T) {
169+
os.Setenv(tt.envVar.key, tt.envVar.value)
170+
defer func() {
171+
os.Setenv(tt.envVar.key, "")
172+
}()
173+
174+
require.Equal(t, getGRPCProxyEnv(), tt.expectedValue)
175+
}
176+
}
177+
178+
cases := []testcase{
179+
{
180+
name: "NotFound",
181+
expectedValue: "",
182+
},
183+
{
184+
name: "LowerCaseFound",
185+
envVar: envVar{
186+
key: "grpc_proxy",
187+
value: "foo",
188+
},
189+
expectedValue: "foo",
190+
},
191+
{
192+
name: "UpperCaseFound",
193+
envVar: envVar{
194+
key: "GRPC_PROXY",
195+
value: "bar",
196+
},
197+
expectedValue: "bar",
198+
},
199+
}
200+
201+
for _, tt := range cases {
202+
t.Run(tt.name, test(tt))
203+
}
204+
}
205+
206+
func TestGRPCProxyURL(t *testing.T) {
207+
type envVar struct {
208+
key string
209+
value string
210+
}
211+
212+
type testcase struct {
213+
name string
214+
address string
215+
envVars []envVar
216+
expectedProxy string
217+
expectedError error
218+
}
219+
220+
test := func(tt testcase) func(t *testing.T) {
221+
return func(t *testing.T) {
222+
for _, envVar := range tt.envVars {
223+
os.Setenv(envVar.key, envVar.value)
224+
}
225+
226+
defer func() {
227+
for _, envVar := range tt.envVars {
228+
os.Setenv(envVar.key, "")
229+
}
230+
}()
231+
232+
var expectedProxyURL *url.URL
233+
var err error
234+
if tt.expectedProxy != "" {
235+
expectedProxyURL, err = url.Parse(tt.expectedProxy)
236+
require.NoError(t, err)
237+
}
238+
239+
proxyUrl, err := grpcProxyURL(tt.address)
240+
require.Equal(t, expectedProxyURL, proxyUrl)
241+
require.Equal(t, tt.expectedError, err)
242+
}
243+
}
244+
245+
cases := []testcase{
246+
{
247+
name: "NoGRPCProxySet",
248+
address: "foo.com:8080",
249+
expectedProxy: "",
250+
expectedError: nil,
251+
},
252+
{
253+
name: "GRPCProxyFoundForAddress",
254+
address: "foo.com:8080",
255+
envVars: []envVar{
256+
{
257+
key: "GRPC_PROXY",
258+
value: "http://my-proxy:8080",
259+
},
260+
},
261+
expectedProxy: "http://my-proxy:8080",
262+
expectedError: nil,
263+
},
264+
{
265+
name: "GRPCNoProxyIncludesAddress",
266+
address: "foo.com:8080",
267+
envVars: []envVar{
268+
{
269+
key: "GRPC_PROXY",
270+
value: "http://my-proxy:8080",
271+
},
272+
{
273+
key: "NO_PROXY",
274+
value: "foo.com:8080",
275+
},
276+
},
277+
expectedProxy: "",
278+
expectedError: nil,
279+
},
280+
}
281+
282+
for _, tt := range cases {
283+
t.Run(tt.name, test(tt))
284+
}
285+
}

0 commit comments

Comments
 (0)