Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver006 committed Dec 9, 2020
1 parent 9ee0d43 commit 4bb4bd4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ test:
TEST_REDIS_CLUSTER_SLAVE_URI="redis://redis-cluster:7005" \
TEST_TILE38_URI="redis://tile38:9851" \
TEST_REDIS_SENTINEL_URI="redis://redis-sentinel:26379" \
go test -v -covermode=atomic -cover -race -coverprofile=coverage.txt ./...
go test -v -covermode=atomic -cover -race -coverprofile=coverage.txt -p 1 ./...



Expand Down
94 changes: 63 additions & 31 deletions exporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,14 @@ func resetSlowLog(t *testing.T, addr string) error {
return nil
}

func downloadURL(t *testing.T, url string) string {
log.Debugf("downloadURL() %s", url)
resp, err := http.Get(url)
func downloadURL(t *testing.T, u string) string {
_, res := downloadURLWithStatusCode(t, u)
return res
}

func downloadURLWithStatusCode(t *testing.T, u string) (int, string) {
log.Debugf("downloadURL() %s", u)
resp, err := http.Get(u)
if err != nil {
t.Fatal(err)
}
Expand All @@ -173,7 +178,8 @@ func downloadURL(t *testing.T, url string) string {
if err != nil {
t.Fatal(err)
}
return string(body)
// t.Logf("http code: %d", resp.StatusCode)
return resp.StatusCode, string(body)
}

func TestLatencySpike(t *testing.T) {
Expand Down Expand Up @@ -1048,46 +1054,55 @@ func TestHTTPScrapeMetricsEndpoints(t *testing.T) {
}

for _, tst := range []struct {
addr string
ck string
csk string
cs string
css string
cntk string
pwd string
target string
name string
addr string
ck string
csk string
cs string
css string
cntk string
pwd string
scrape bool
target string
wantStatusCode int
}{
{addr: testRedisIPAddress, csk: csk, css: css, cntk: cntk},
{addr: testRedisHostname, csk: csk, css: css, cntk: cntk},
{addr: os.Getenv("TEST_REDIS_URI"), ck: csk, cs: css, cntk: cntk},
{addr: os.Getenv("TEST_REDIS_URI"), csk: csk, css: css, cntk: cntk},
{pwd: "", target: os.Getenv("TEST_REDIS_URI"), ck: csk, cs: css, cntk: cntk},
{pwd: "", target: os.Getenv("TEST_REDIS_URI"), csk: csk, css: css, cntk: cntk},
{pwd: "redis-password", target: os.Getenv("TEST_PWD_REDIS_URI"), ck: csk, cs: css, cntk: cntk},
{pwd: "redis-password", target: os.Getenv("TEST_PWD_REDIS_URI"), csk: csk, cs: css, cntk: cntk},
{name: "ip-addr", addr: testRedisIPAddress, csk: csk, css: css, cntk: cntk},
{name: "hostname", addr: testRedisHostname, csk: csk, css: css, cntk: cntk},

{name: "check-keys", addr: os.Getenv("TEST_REDIS_URI"), ck: csk, cs: css, cntk: cntk},
{name: "check-single-keys", addr: os.Getenv("TEST_REDIS_URI"), csk: csk, css: css, cntk: cntk},

{name: "addr-no-prefix", addr: strings.TrimPrefix(os.Getenv("TEST_REDIS_URI"), "redis://"), csk: csk, css: css, cntk: cntk},

{name: "scrape-target-no-prefix", pwd: "", scrape: true, target: strings.TrimPrefix(os.Getenv("TEST_REDIS_URI"), "redis://"), ck: csk, cs: css, cntk: cntk},

{name: "scrape-ck", pwd: "", scrape: true, target: os.Getenv("TEST_REDIS_URI"), ck: csk, cs: css, cntk: cntk},
{name: "scrape-csk", pwd: "", scrape: true, target: os.Getenv("TEST_REDIS_URI"), csk: csk, css: css, cntk: cntk},

{name: "scrape-pwd-ck", pwd: "redis-password", scrape: true, target: os.Getenv("TEST_PWD_REDIS_URI"), ck: csk, cs: css, cntk: cntk},
{name: "scrape-pwd-csk", pwd: "redis-password", scrape: true, target: os.Getenv("TEST_PWD_REDIS_URI"), csk: csk, cs: css, cntk: cntk},

{name: "error-scrape-no-target", wantStatusCode: 400, scrape: true, target: ""},
} {
name := fmt.Sprintf("addr:[%s]___target:[%s]___pwd:[%s]", tst.addr, tst.target, tst.pwd)
t.Run(name, func(t *testing.T) {
t.Run(tst.name, func(t *testing.T) {
options := Options{
Namespace: "test",
Password: tst.pwd,
LuaScript: []byte(`return {"a", "11", "b", "12", "c", "13"}`),
Registry: prometheus.NewRegistry(),
}

if tst.target == "" {
options.CheckSingleKeys = tst.csk
options.CheckKeys = tst.ck
options.CheckSingleStreams = tst.css
options.CheckStreams = tst.cs
options.CountKeys = tst.cntk
}
options.CheckSingleKeys = tst.csk
options.CheckKeys = tst.ck
options.CheckSingleStreams = tst.css
options.CheckStreams = tst.cs
options.CountKeys = tst.cntk

e, _ := NewRedisExporter(tst.addr, options)
ts := httptest.NewServer(e)

u := ts.URL
if tst.target != "" {
if tst.scrape {
u += "/scrape"
v := url.Values{}
v.Add("target", tst.target)
Expand All @@ -1104,6 +1119,23 @@ func TestHTTPScrapeMetricsEndpoints(t *testing.T) {
u += "/metrics"
}

wantStatusCode := 200
if tst.wantStatusCode != 0 {
wantStatusCode = tst.wantStatusCode
}

gotStatusCode, body := downloadURLWithStatusCode(t, u)

if gotStatusCode != wantStatusCode {
t.Fatalf("got status code: %d wanted: %d", gotStatusCode, wantStatusCode)
return
}

// if we expected an error we can stop here
if wantStatusCode != 200 {
return
}

wants := []string{
// metrics
`test_connected_clients`,
Expand Down Expand Up @@ -1145,9 +1177,9 @@ func TestHTTPScrapeMetricsEndpoints(t *testing.T) {
`stream_group_messages_pending`,
`stream_group_consumer_messages_pending`,
`stream_group_consumer_idle_seconds`,
`test_up 1`,
}

body := downloadURL(t, u)
for _, want := range wants {
if !strings.Contains(body, want) {
t.Errorf("url: %s want metrics to include %q, have:\n%s", u, want, body)
Expand Down

0 comments on commit 4bb4bd4

Please sign in to comment.