From 9dfe1d3956346228cacb519e33ff21b6acdfce4f Mon Sep 17 00:00:00 2001 From: christophe kalenzaga Date: Tue, 26 Sep 2023 10:42:10 +0200 Subject: [PATCH 1/3] fix nil-panics when running only this test file --- proxy_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxy_test.go b/proxy_test.go index 167352dc..8ef0d4a1 100644 --- a/proxy_test.go +++ b/proxy_test.go @@ -79,7 +79,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) { From 2b15f601071f0ed20e168b05aef3a3634cd48291 Mon Sep 17 00:00:00 2001 From: christophe kalenzaga Date: Tue, 26 Sep 2023 10:47:37 +0200 Subject: [PATCH 2/3] increase test covergage --- proxy_test.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/proxy_test.go b/proxy_test.go index 8ef0d4a1..b9016698 100644 --- a/proxy_test.go +++ b/proxy_test.go @@ -4,10 +4,10 @@ import ( "bytes" "crypto/tls" "fmt" - "golang.org/x/time/rate" "io" "math/rand" "net" + "os" "regexp" "runtime" "strings" @@ -16,6 +16,8 @@ import ( "testing" "time" + "golang.org/x/time/rate" + "github.com/contentsquare/chproxy/cache" "net/http" @@ -36,8 +38,10 @@ 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{ Clusters: []config.Cluster{ @@ -67,6 +71,46 @@ var goodCfg = &config.Config{ {Name: "param_test", Params: []config.Param{{Key: "param_key", Value: "param_value"}}}, }, } +var goodCfgWithCache = &config.Config{ + 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", + 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) { p := newReverseProxy(&cfg.ConnectionPool) @@ -270,6 +314,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", @@ -900,6 +966,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 { From f7bdd5b1f9a8756d9f068d2d5f82debaa8c9291e Mon Sep 17 00:00:00 2001 From: christophe kalenzaga Date: Tue, 26 Sep 2023 11:19:18 +0200 Subject: [PATCH 3/3] avoid duplicate metrics collector registration attempted issues --- proxy_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/proxy_test.go b/proxy_test.go index b9016698..7c276ef0 100644 --- a/proxy_test.go +++ b/proxy_test.go @@ -44,6 +44,10 @@ const ( const testCacheDir = "./test-cache-data" var goodCfg = &config.Config{ + Server: config.Server{ + Metrics: config.Metrics{ + Namespace: "proxy_test"}, + }, Clusters: []config.Cluster{ { Name: "cluster",