Skip to content

Commit

Permalink
Add code coverage (#361)
Browse files Browse the repository at this point in the history
* fix nil-panics when running only this test file

* increase test covergage

* avoid duplicate metrics collector registration attempted issues
  • Loading branch information
mga-chka authored Sep 26, 2023
1 parent 73bdd51 commit a868dc5
Showing 1 changed file with 77 additions and 3 deletions.
80 changes: 77 additions & 3 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"bytes"
"crypto/tls"
"fmt"
"golang.org/x/time/rate"
"io"
"math/rand"
"net"
"os"
"regexp"
"runtime"
"strings"
Expand All @@ -16,6 +16,8 @@ import (
"testing"
"time"

"golang.org/x/time/rate"

"github.com/contentsquare/chproxy/cache"

"net/http"
Expand All @@ -36,10 +38,44 @@ const max_concurrent_goroutines = 256
const heavyRequestDuration = time.Millisecond * 512
const defaultUsername = "default"
const (
okResponse = "1"
okResponse = "1"
badGatewayResponse = "]: cannot reach 127.0.0.1:"
)
const testCacheDir = "./test-cache-data"

var goodCfg = &config.Config{
Server: config.Server{
Metrics: config.Metrics{
Namespace: "proxy_test"},
},
Clusters: []config.Cluster{
{
Name: "cluster",
Scheme: "http",
Replicas: []config.Replica{
{
Nodes: []string{"localhost:8123"},
},
},
ClusterUsers: []config.ClusterUser{
{
Name: "web",
},
},
},
},
Users: []config.User{
{
Name: defaultUsername,
ToCluster: "cluster",
ToUser: "web",
},
},
ParamGroups: []config.ParamGroup{
{Name: "param_test", Params: []config.Param{{Key: "param_key", Value: "param_value"}}},
},
}
var goodCfgWithCache = &config.Config{
Clusters: []config.Cluster{
{
Name: "cluster",
Expand All @@ -61,11 +97,23 @@ var goodCfg = &config.Config{
Name: defaultUsername,
ToCluster: "cluster",
ToUser: "web",
Cache: "file_system_cache",
},
},
ParamGroups: []config.ParamGroup{
{Name: "param_test", Params: []config.Param{{Key: "param_key", Value: "param_value"}}},
},
Caches: []config.Cache{
{
Name: "file_system_cache",
Mode: "file_system",
FileSystem: config.FileSystemCacheConfig{
Dir: testCacheDir,
MaxSize: config.ByteSize(1024 * 1024),
},
Expire: config.Duration(1000 * 60 * 60),
},
},
}

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

func TestNewReverseProxy(t *testing.T) {
Expand Down Expand Up @@ -270,6 +318,28 @@ func TestReverseProxy_ServeHTTP1(t *testing.T) {
expStatusCode int
f func(p *reverseProxy) *http.Response
}{
{
cfg: goodCfg,
name: "Bad gatway response without cache",
expResponse: badGatewayResponse,
expStatusCode: http.StatusBadGateway,
f: func(p *reverseProxy) *http.Response {
req := httptest.NewRequest("GET", fmt.Sprintf("%s/badGateway?query=SELECT123456", fakeServer.URL), nil)
return makeCustomRequest(p, req)
},
},
{
cfg: goodCfgWithCache,
name: "Bad gatway response with cache",
expResponse: badGatewayResponse,
expStatusCode: http.StatusBadGateway,
f: func(p *reverseProxy) *http.Response {
req := httptest.NewRequest("GET", fmt.Sprintf("%s/badGateway?query=SELECT123456", fakeServer.URL), nil)
// cleaning the cache to be sure it will be a cache miss although the query isn't supposed to be cached
os.RemoveAll(testCacheDir)
return makeCustomRequest(p, req)
},
},
{
cfg: goodCfg,
name: "Ok response",
Expand Down Expand Up @@ -900,6 +970,10 @@ var (
fmt.Fprintln(w, okResponse)
return
}
if r.URL.Path == "/badGateway" {
w.WriteHeader(http.StatusBadGateway)
return
}

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

0 comments on commit a868dc5

Please sign in to comment.