Skip to content

Commit

Permalink
Moved additional tests to server
Browse files Browse the repository at this point in the history
Signed-off-by: naveensrinivasan <172697+naveensrinivasan@users.noreply.github.com>
  • Loading branch information
naveensrinivasan committed Dec 6, 2022
1 parent 1a2c4fc commit acf2b7f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 74 deletions.
76 changes: 76 additions & 0 deletions cmd/rekor-server/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@
package main

import (
"bufio"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"regexp"
"strconv"
"testing"

"github.com/sigstore/rekor/pkg/util"
Expand Down Expand Up @@ -50,3 +55,74 @@ func TestDuplicates(t *testing.T) {
out = util.RunCli(t, "upload", "--artifact", artifactPath, "--signature", sigPath, "--public-key", pubPath)
util.OutputContains(t, out, "Created entry at")
}

// Smoke test to ensure we're publishing and recording metrics when an API is
// called.
// TODO: use a more robust test approach here e.g. prometheus client-based?
// TODO: cover all endpoints to make sure none are dropped.
func TestMetricsCounts(t *testing.T) {
latencyMetric := "rekor_latency_by_api_count{method=\"GET\",path=\"/api/v1/log\"}"
qpsMetric := "rekor_qps_by_api{code=\"200\",method=\"GET\",path=\"/api/v1/log\"}"

latencyCount, err := getRekorMetricCount(latencyMetric, t)
if err != nil {
t.Fatal(err)
}

qpsCount, err := getRekorMetricCount(qpsMetric, t)
if err != nil {
t.Fatal(err)
}

resp, err := http.Get("http://localhost:3000/api/v1/log")
if err != nil {
t.Fatal(err)
}
resp.Body.Close()

latencyCount2, err := getRekorMetricCount(latencyMetric, t)
if err != nil {
t.Fatal(err)
}

qpsCount2, err := getRekorMetricCount(qpsMetric, t)
if err != nil {
t.Fatal(err)
}

if latencyCount2-latencyCount != 1 {
t.Error("rekor_latency_by_api_count did not increment")
}

if qpsCount2-qpsCount != 1 {
t.Error("rekor_qps_by_api did not increment")
}
}
func getRekorMetricCount(metricLine string, t *testing.T) (int, error) {
re, err := regexp.Compile(fmt.Sprintf("^%s.*([0-9]+)$", regexp.QuoteMeta(metricLine)))
if err != nil {
return 0, err
}

resp, err := http.Get("http://localhost:2112/metrics")
if err != nil {
return 0, err
}
defer resp.Body.Close()

scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
match := re.FindStringSubmatch(scanner.Text())
if len(match) != 2 {
continue
}

result, err := strconv.Atoi(match[1])
if err != nil {
return 0, nil
}
t.Log("Matched metric line: " + scanner.Text())
return result, nil
}
return 0, nil
}
74 changes: 0 additions & 74 deletions tests/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package e2e

import (
"bufio"
"bytes"
"context"
"crypto"
Expand All @@ -31,7 +30,6 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -663,78 +661,6 @@ func TestSearchValidateTreeID(t *testing.T) {
}
}

func getRekorMetricCount(metricLine string, t *testing.T) (int, error) {
re, err := regexp.Compile(fmt.Sprintf("^%s.*([0-9]+)$", regexp.QuoteMeta(metricLine)))
if err != nil {
return 0, err
}

resp, err := http.Get("http://localhost:2112/metrics")
if err != nil {
return 0, err
}
defer resp.Body.Close()

scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
match := re.FindStringSubmatch(scanner.Text())
if len(match) != 2 {
continue
}

result, err := strconv.Atoi(match[1])
if err != nil {
return 0, nil
}
t.Log("Matched metric line: " + scanner.Text())
return result, nil
}
return 0, nil
}

// Smoke test to ensure we're publishing and recording metrics when an API is
// called.
// TODO: use a more robust test approach here e.g. prometheus client-based?
// TODO: cover all endpoints to make sure none are dropped.
func TestMetricsCounts(t *testing.T) {
latencyMetric := "rekor_latency_by_api_count{method=\"GET\",path=\"/api/v1/log\"}"
qpsMetric := "rekor_qps_by_api{code=\"200\",method=\"GET\",path=\"/api/v1/log\"}"

latencyCount, err := getRekorMetricCount(latencyMetric, t)
if err != nil {
t.Fatal(err)
}

qpsCount, err := getRekorMetricCount(qpsMetric, t)
if err != nil {
t.Fatal(err)
}

resp, err := http.Get("http://localhost:3000/api/v1/log")
if err != nil {
t.Fatal(err)
}
resp.Body.Close()

latencyCount2, err := getRekorMetricCount(latencyMetric, t)
if err != nil {
t.Fatal(err)
}

qpsCount2, err := getRekorMetricCount(qpsMetric, t)
if err != nil {
t.Fatal(err)
}

if latencyCount2-latencyCount != 1 {
t.Error("rekor_latency_by_api_count did not increment")
}

if qpsCount2-qpsCount != 1 {
t.Error("rekor_qps_by_api did not increment")
}
}

// TestSearchLogQuerySingleShard provides coverage testing on the searchLogQuery endpoint within a single shard
func TestSearchLogQuerySingleShard(t *testing.T) {

Expand Down

0 comments on commit acf2b7f

Please sign in to comment.