Skip to content

Commit

Permalink
query: e2e: Test --web.external-prefix
Browse files Browse the repository at this point in the history
Signed-off-by: Prem Kumar <prmsrswt@gmail.com>
  • Loading branch information
onprem committed Jun 24, 2020
1 parent e5d1cc7 commit fb86080
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 10 deletions.
2 changes: 1 addition & 1 deletion test/e2e/compact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ func TestCompactWithStoreGateway(t *testing.T) {
testutil.Ok(t, str.WaitSumMetrics(e2e.Equals(0), "thanos_blocks_meta_sync_failures_total"))
testutil.Ok(t, str.WaitSumMetrics(e2e.Equals(0), "thanos_blocks_meta_modified"))

q, err := e2ethanos.NewQuerier(s.SharedDir(), "1", []string{str.GRPCNetworkEndpoint()}, nil, nil, "")
q, err := e2ethanos.NewQuerier(s.SharedDir(), "1", []string{str.GRPCNetworkEndpoint()}, nil, nil, "", "")
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(q))

Expand Down
39 changes: 39 additions & 0 deletions test/e2e/e2ethanos/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
package e2ethanos

import (
"net/http"
"net/http/httputil"
"net/url"
"os/exec"
"strings"
"testing"

"github.com/cortexproject/cortex/integration/e2e"
Expand All @@ -19,3 +23,38 @@ func CleanScenario(t *testing.T, s *e2e.Scenario) func() {
s.Close()
}
}

func singleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}

// NewSingleHostReverseProxy is almost same as httputil.NewSingleHostReverseProxy
// but it performs a url path rewrite
func NewSingleHostReverseProxy(target *url.URL, externalPrefix string) *httputil.ReverseProxy {
targetQuery := target.RawQuery
director := func(req *http.Request) {
req.URL.Scheme = target.Scheme
req.URL.Host = target.Host
req.URL.Path = singleJoiningSlash(target.Path, strings.TrimPrefix(req.URL.Path, "/"+externalPrefix))

if targetQuery == "" || req.URL.RawQuery == "" {
req.URL.RawQuery = targetQuery + req.URL.RawQuery
} else {
req.URL.RawQuery = targetQuery + "&" + req.URL.RawQuery
}

if _, ok := req.Header["User-Agent"]; !ok {
// explicitly disable User-Agent so it's not set to default value
req.Header.Set("User-Agent", "")
}
}
return &httputil.ReverseProxy{Director: director}
}
6 changes: 5 additions & 1 deletion test/e2e/e2ethanos/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func NewPrometheusWithSidecar(sharedDir string, netName string, name string, con
return prom, sidecar, nil
}

func NewQuerier(sharedDir, name string, storeAddresses, fileSDStoreAddresses, ruleAddresses []string, routePrefix string) (*Service, error) {
func NewQuerier(sharedDir, name string, storeAddresses, fileSDStoreAddresses, ruleAddresses []string, routePrefix, externalPrefix string) (*Service, error) {
const replicaLabel = "replica"

args := e2e.BuildArgs(map[string]string{
Expand Down Expand Up @@ -163,6 +163,10 @@ func NewQuerier(sharedDir, name string, storeAddresses, fileSDStoreAddresses, ru
args = append(args, "--web.route-prefix="+routePrefix)
}

if externalPrefix != "" {
args = append(args, "--web.external-prefix="+externalPrefix)
}

querier := NewService(
fmt.Sprintf("querier-%v", name),
DefaultImage(),
Expand Down
58 changes: 57 additions & 1 deletion test/e2e/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package e2e_test
import (
"context"
"fmt"
"net/http/httptest"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -100,7 +101,7 @@ func TestQuery(t *testing.T) {
testutil.Ok(t, s.StartAndWaitReady(prom1, sidecar1, prom2, sidecar2, prom3, sidecar3, prom4, sidecar4))

// Querier. Both fileSD and directly by flags.
q, err := e2ethanos.NewQuerier(s.SharedDir(), "1", []string{sidecar1.GRPCNetworkEndpoint(), sidecar2.GRPCNetworkEndpoint(), receiver.GRPCNetworkEndpoint()}, []string{sidecar3.GRPCNetworkEndpoint(), sidecar4.GRPCNetworkEndpoint()}, nil, "")
q, err := e2ethanos.NewQuerier(s.SharedDir(), "1", []string{sidecar1.GRPCNetworkEndpoint(), sidecar2.GRPCNetworkEndpoint(), receiver.GRPCNetworkEndpoint()}, []string{sidecar3.GRPCNetworkEndpoint(), sidecar4.GRPCNetworkEndpoint()}, nil, "", "")
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(q))

Expand Down Expand Up @@ -179,6 +180,7 @@ func TestQueryRoutePrefix(t *testing.T) {
nil,
nil,
"test",
"",
)
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(q))
Expand Down Expand Up @@ -213,6 +215,60 @@ func TestQueryRoutePrefix(t *testing.T) {
testutil.Ok(t, err)
}

func TestQueryExternalPrefix(t *testing.T) {
t.Parallel()

s, err := e2e.NewScenario("e2e_test_query_external_prefix")
testutil.Ok(t, err)
t.Cleanup(e2ethanos.CleanScenario(t, s))

externalPrefix := "thanos"

q, err := e2ethanos.NewQuerier(
s.SharedDir(), "1",
nil,
nil,
nil,
"",
externalPrefix,
)
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(q))

querierURL := urlParse(t, "http://"+q.HTTPEndpoint())

querierProxy := httptest.NewServer(e2ethanos.NewSingleHostReverseProxy(querierURL, externalPrefix))

ctx, cancel := chromedp.NewContext(context.Background())
t.Cleanup(cancel)

var networkErrors []string

// Listen for failed network requests and push them to an array.
chromedp.ListenTarget(ctx, func(ev interface{}) {
switch ev := ev.(type) {
case *network.EventLoadingFailed:
networkErrors = append(networkErrors, ev.ErrorText)
}
})

err = chromedp.Run(ctx,
network.Enable(),
chromedp.Navigate(querierProxy.URL+"/thanos/graph"),
chromedp.WaitVisible(`body`),
)
testutil.Ok(t, err)

err = func() error {
if len(networkErrors) > 0 {
return fmt.Errorf("some network requests failed: %s", strings.Join(networkErrors, "; "))
}
return nil
}()

testutil.Ok(t, err)
}

func urlParse(t *testing.T, addr string) *url.URL {
u, err := url.Parse(addr)
testutil.Ok(t, err)
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/receive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestReceive(t *testing.T) {
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(prom1, prom2, prom3))

q, err := e2ethanos.NewQuerier(s.SharedDir(), "1", []string{r1.GRPCNetworkEndpoint(), r2.GRPCNetworkEndpoint(), r3.GRPCNetworkEndpoint()}, nil, nil, "")
q, err := e2ethanos.NewQuerier(s.SharedDir(), "1", []string{r1.GRPCNetworkEndpoint(), r2.GRPCNetworkEndpoint(), r3.GRPCNetworkEndpoint()}, nil, nil, "", "")
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(q))

Expand Down Expand Up @@ -140,7 +140,7 @@ func TestReceive(t *testing.T) {
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(prom1))

q, err := e2ethanos.NewQuerier(s.SharedDir(), "1", []string{r1.GRPCNetworkEndpoint(), r2.GRPCNetworkEndpoint(), r3.GRPCNetworkEndpoint()}, nil, nil, "")
q, err := e2ethanos.NewQuerier(s.SharedDir(), "1", []string{r1.GRPCNetworkEndpoint(), r2.GRPCNetworkEndpoint(), r3.GRPCNetworkEndpoint()}, nil, nil, "", "")
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(q))

Expand Down Expand Up @@ -212,7 +212,7 @@ func TestReceive(t *testing.T) {
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(prom1))

q, err := e2ethanos.NewQuerier(s.SharedDir(), "1", []string{r1.GRPCNetworkEndpoint(), r2.GRPCNetworkEndpoint()}, nil, nil, "")
q, err := e2ethanos.NewQuerier(s.SharedDir(), "1", []string{r1.GRPCNetworkEndpoint(), r2.GRPCNetworkEndpoint()}, nil, nil, "", "")
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(q))

Expand Down
6 changes: 3 additions & 3 deletions test/e2e/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func TestRule_AlertmanagerHTTPClient(t *testing.T) {
{
EndpointsConfig: http_util.EndpointsConfig{
StaticAddresses: func() []string {
q, err := e2ethanos.NewQuerier(s.SharedDir(), "1", nil, nil, nil, "")
q, err := e2ethanos.NewQuerier(s.SharedDir(), "1", nil, nil, nil, "", "")
testutil.Ok(t, err)
return []string{q.NetworkHTTPEndpointFor(s.NetworkName())}
}(),
Expand All @@ -271,7 +271,7 @@ func TestRule_AlertmanagerHTTPClient(t *testing.T) {
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(r))

q, err := e2ethanos.NewQuerier(s.SharedDir(), "1", []string{r.GRPCNetworkEndpoint()}, nil, nil, "")
q, err := e2ethanos.NewQuerier(s.SharedDir(), "1", []string{r.GRPCNetworkEndpoint()}, nil, nil, "", "")
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(q))

Expand Down Expand Up @@ -351,7 +351,7 @@ func TestRule(t *testing.T) {
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(r))

q, err := e2ethanos.NewQuerier(s.SharedDir(), "1", []string{r.GRPCNetworkEndpoint()}, nil, nil, "")
q, err := e2ethanos.NewQuerier(s.SharedDir(), "1", []string{r.GRPCNetworkEndpoint()}, nil, nil, "", "")
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(q))

Expand Down
1 change: 1 addition & 0 deletions test/e2e/rules_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func TestRulesAPI_Fanout(t *testing.T) {
nil,
[]string{sidecar1.GRPCNetworkEndpoint(), sidecar2.GRPCNetworkEndpoint(), r1.GRPCNetworkEndpoint(), r2.GRPCNetworkEndpoint()},
"",
"",
)

testutil.Ok(t, err)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/store_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestStoreGateway(t *testing.T) {
// Ensure bucket UI.
ensureGETStatusCode(t, http.StatusOK, "http://"+path.Join(s1.HTTPEndpoint(), "loaded"))

q, err := e2ethanos.NewQuerier(s.SharedDir(), "1", []string{s1.GRPCNetworkEndpoint()}, nil, nil, "")
q, err := e2ethanos.NewQuerier(s.SharedDir(), "1", []string{s1.GRPCNetworkEndpoint()}, nil, nil, "", "")
testutil.Ok(t, err)
testutil.Ok(t, s.StartAndWaitReady(q))

Expand Down

0 comments on commit fb86080

Please sign in to comment.