fix(test): refactor autocomplete tests to be more DRY

Too much duplication there, both loops can be merged
This commit is contained in:
Łukasz Mierzwa
2018-08-21 09:35:49 +01:00
parent 129ffb7e17
commit 0147734769
+85 -106
View File
@@ -3,134 +3,113 @@ package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/prymitive/unsee/internal/mock"
)
type labelTest struct {
Term string
Results []string
type requestTest struct {
PathSuffix string
StatusCode int
Results []string
}
var labelTests = []labelTest{
type autocompleteTest struct {
PathPrefix string
Tests []requestTest
}
var autocompleteTests = []autocompleteTest{
{
Term: "a",
Results: []string{"alertname", "instance"},
PathPrefix: "/labelNames.json",
Tests: []requestTest{
{
PathSuffix: "",
StatusCode: 200,
Results: []string{"alertname", "cluster", "instance", "job"},
},
{
PathSuffix: "?term=",
StatusCode: 200,
Results: []string{"alertname", "cluster", "instance", "job"},
},
{
PathSuffix: "?term=a",
StatusCode: 200,
Results: []string{"alertname", "instance"},
},
{
PathSuffix: "?term=alertname",
StatusCode: 200,
Results: []string{"alertname"},
},
{
PathSuffix: "?term=1234567890",
StatusCode: 200,
Results: []string{},
},
},
},
{
Term: "alertname",
Results: []string{"alertname"},
},
{
Term: "1234567890",
Results: []string{},
},
{
Term: "",
Results: []string{"alertname", "cluster", "instance", "job"},
PathPrefix: "/labelValues.json",
Tests: []requestTest{
{
PathSuffix: "?name=",
StatusCode: 400,
Results: []string{},
},
{
PathSuffix: "?name=foobar",
StatusCode: 200,
Results: []string{},
},
{
PathSuffix: "?name=alertname",
StatusCode: 200,
Results: []string{"Free_Disk_Space_Too_Low", "HTTP_Probe_Failed", "Host_Down", "Memory_Usage_Too_High"},
},
{
PathSuffix: "?name=cluster",
StatusCode: 200,
Results: []string{"dev", "prod", "staging"},
},
},
},
}
func TestKnownLabelNames(t *testing.T) {
func TestLabelAutocomplete(t *testing.T) {
mockConfig()
for _, version := range mock.ListAllMocks() {
t.Logf("Testing known labels using mock files from Alertmanager %s", version)
t.Logf("Testing labels autocomplete using mock files from Alertmanager %s", version)
mockAlerts(version)
r := ginTestEngine()
// repeat test a few times to test cached responses
for i := 1; i <= 3; i++ {
req := httptest.NewRequest("GET", "/labelNames.json", nil)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
for _, testVariant := range autocompleteTests {
for _, testCase := range testVariant.Tests {
// repeat each test a few times to test cached responses
for i := 1; i <= 3; i++ {
url := fmt.Sprintf("%s%s", testVariant.PathPrefix, testCase.PathSuffix)
req := httptest.NewRequest("GET", url, nil)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Errorf("Invalid status code for request without any query: %d", resp.Code)
}
if resp.Code != testCase.StatusCode {
t.Errorf("GET %s returned status %d, expected %d", url, resp.Code, testCase.StatusCode)
}
for _, testCase := range labelTests {
url := fmt.Sprintf("/labelNames.json?term=%s", testCase.Term)
req := httptest.NewRequest("GET", url, nil)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
if resp.Code < 300 {
ur := []string{}
err := json.Unmarshal(resp.Body.Bytes(), &ur)
if err != nil {
t.Errorf("Failed to unmarshal response: %s", err)
}
if resp.Code != http.StatusOK {
t.Errorf("GET %s returned status %d", url, resp.Code)
}
ur := []string{}
err := json.Unmarshal(resp.Body.Bytes(), &ur)
if err != nil {
t.Errorf("Failed to unmarshal response: %s", err)
}
if len(ur) != len(testCase.Results) {
t.Errorf("Invalid number of label names for %s, got %d, expected %d", url, len(ur), len(testCase.Results))
t.Errorf("Results: %s", ur)
}
}
}
}
}
type valueTest struct {
Name string
Results []string
}
var valueTests = []valueTest{
{
Name: "foobar",
Results: []string{},
},
{
Name: "alertname",
Results: []string{"Free_Disk_Space_Too_Low", "HTTP_Probe_Failed", "Host_Down", "Memory_Usage_Too_High"},
},
{
Name: "cluster",
Results: []string{"dev", "prod", "staging"},
},
}
func TestKnownLabelValues(t *testing.T) {
mockConfig()
for _, version := range mock.ListAllMocks() {
t.Logf("Testing known label values using mock files from Alertmanager %s", version)
mockAlerts(version)
r := ginTestEngine()
// repeat test a few times to test cached responses
for i := 1; i <= 3; i++ {
req := httptest.NewRequest("GET", "/labelValues.json", nil)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
if resp.Code != http.StatusBadRequest {
t.Errorf("Invalid status code for request without any query: %d", resp.Code)
}
for _, testCase := range valueTests {
url := fmt.Sprintf("/labelValues.json?name=%s", testCase.Name)
req := httptest.NewRequest("GET", url, nil)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
if resp.Code != http.StatusOK {
t.Errorf("GET %s returned status %d", url, resp.Code)
}
ur := []string{}
err := json.Unmarshal(resp.Body.Bytes(), &ur)
if err != nil {
t.Errorf("Failed to unmarshal response: %s", err)
}
if len(ur) != len(testCase.Results) {
t.Errorf("Invalid number of label values for %s, got %d, expected %d", url, len(ur), len(testCase.Results))
t.Errorf("Results: %s", ur)
if len(ur) != len(testCase.Results) {
t.Errorf("Invalid number of responses for %s, got %d, expected %d", url, len(ur), len(testCase.Results))
t.Errorf("Results: %s", ur)
}
}
}
}
}