Skip to content

Commit a868dc5

Browse files
authored
Add code coverage (#361)
* fix nil-panics when running only this test file * increase test covergage * avoid duplicate metrics collector registration attempted issues
1 parent 73bdd51 commit a868dc5

File tree

1 file changed

+77
-3
lines changed

1 file changed

+77
-3
lines changed

proxy_test.go

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"bytes"
55
"crypto/tls"
66
"fmt"
7-
"golang.org/x/time/rate"
87
"io"
98
"math/rand"
109
"net"
10+
"os"
1111
"regexp"
1212
"runtime"
1313
"strings"
@@ -16,6 +16,8 @@ import (
1616
"testing"
1717
"time"
1818

19+
"golang.org/x/time/rate"
20+
1921
"github.com/contentsquare/chproxy/cache"
2022

2123
"net/http"
@@ -36,10 +38,44 @@ const max_concurrent_goroutines = 256
3638
const heavyRequestDuration = time.Millisecond * 512
3739
const defaultUsername = "default"
3840
const (
39-
okResponse = "1"
41+
okResponse = "1"
42+
badGatewayResponse = "]: cannot reach 127.0.0.1:"
4043
)
44+
const testCacheDir = "./test-cache-data"
4145

4246
var goodCfg = &config.Config{
47+
Server: config.Server{
48+
Metrics: config.Metrics{
49+
Namespace: "proxy_test"},
50+
},
51+
Clusters: []config.Cluster{
52+
{
53+
Name: "cluster",
54+
Scheme: "http",
55+
Replicas: []config.Replica{
56+
{
57+
Nodes: []string{"localhost:8123"},
58+
},
59+
},
60+
ClusterUsers: []config.ClusterUser{
61+
{
62+
Name: "web",
63+
},
64+
},
65+
},
66+
},
67+
Users: []config.User{
68+
{
69+
Name: defaultUsername,
70+
ToCluster: "cluster",
71+
ToUser: "web",
72+
},
73+
},
74+
ParamGroups: []config.ParamGroup{
75+
{Name: "param_test", Params: []config.Param{{Key: "param_key", Value: "param_value"}}},
76+
},
77+
}
78+
var goodCfgWithCache = &config.Config{
4379
Clusters: []config.Cluster{
4480
{
4581
Name: "cluster",
@@ -61,11 +97,23 @@ var goodCfg = &config.Config{
6197
Name: defaultUsername,
6298
ToCluster: "cluster",
6399
ToUser: "web",
100+
Cache: "file_system_cache",
64101
},
65102
},
66103
ParamGroups: []config.ParamGroup{
67104
{Name: "param_test", Params: []config.Param{{Key: "param_key", Value: "param_value"}}},
68105
},
106+
Caches: []config.Cache{
107+
{
108+
Name: "file_system_cache",
109+
Mode: "file_system",
110+
FileSystem: config.FileSystemCacheConfig{
111+
Dir: testCacheDir,
112+
MaxSize: config.ByteSize(1024 * 1024),
113+
},
114+
Expire: config.Duration(1000 * 60 * 60),
115+
},
116+
},
69117
}
70118

71119
func newConfiguredProxy(cfg *config.Config) (*reverseProxy, error) {
@@ -79,7 +127,7 @@ func init() {
79127
// we need to initiliaze prometheus metrics
80128
// otherwise the calls the proxy.applyConfig will fail
81129
// due to memory issues if someone only runs proxy_test
82-
initMetrics(goodCfg)
130+
registerMetrics(goodCfg)
83131
}
84132

85133
func TestNewReverseProxy(t *testing.T) {
@@ -270,6 +318,28 @@ func TestReverseProxy_ServeHTTP1(t *testing.T) {
270318
expStatusCode int
271319
f func(p *reverseProxy) *http.Response
272320
}{
321+
{
322+
cfg: goodCfg,
323+
name: "Bad gatway response without cache",
324+
expResponse: badGatewayResponse,
325+
expStatusCode: http.StatusBadGateway,
326+
f: func(p *reverseProxy) *http.Response {
327+
req := httptest.NewRequest("GET", fmt.Sprintf("%s/badGateway?query=SELECT123456", fakeServer.URL), nil)
328+
return makeCustomRequest(p, req)
329+
},
330+
},
331+
{
332+
cfg: goodCfgWithCache,
333+
name: "Bad gatway response with cache",
334+
expResponse: badGatewayResponse,
335+
expStatusCode: http.StatusBadGateway,
336+
f: func(p *reverseProxy) *http.Response {
337+
req := httptest.NewRequest("GET", fmt.Sprintf("%s/badGateway?query=SELECT123456", fakeServer.URL), nil)
338+
// cleaning the cache to be sure it will be a cache miss although the query isn't supposed to be cached
339+
os.RemoveAll(testCacheDir)
340+
return makeCustomRequest(p, req)
341+
},
342+
},
273343
{
274344
cfg: goodCfg,
275345
name: "Ok response",
@@ -900,6 +970,10 @@ var (
900970
fmt.Fprintln(w, okResponse)
901971
return
902972
}
973+
if r.URL.Path == "/badGateway" {
974+
w.WriteHeader(http.StatusBadGateway)
975+
return
976+
}
903977

904978
body, err := io.ReadAll(r.Body)
905979
if err != nil {

0 commit comments

Comments
 (0)