Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code coverage #361

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading