mirror of
https://github.com/prymitive/karma
synced 2026-08-28 11:17:28 +00:00
Merge pull request #1614 from prymitive/gocovmerge
fix(tests): replace gocovmerge with a simple bash script
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/pmezard/go-difflib/difflib"
|
||||
|
||||
"github.com/prymitive/karma/internal/models"
|
||||
)
|
||||
|
||||
func TestSortByStartsAt(t *testing.T) {
|
||||
type testCaseT struct {
|
||||
groups []models.APIAlertGroup
|
||||
sortReverse bool
|
||||
sorted []models.APIAlertGroup
|
||||
}
|
||||
|
||||
g1 := models.AlertGroup{
|
||||
ID: "1",
|
||||
LatestStartsAt: time.Date(2020, time.January, 1, 0, 0, 0, 1, time.UTC),
|
||||
}
|
||||
g2 := models.AlertGroup{
|
||||
ID: "2",
|
||||
LatestStartsAt: time.Date(2020, time.January, 1, 0, 0, 0, 2, time.UTC),
|
||||
}
|
||||
g3 := models.AlertGroup{
|
||||
ID: "3",
|
||||
LatestStartsAt: time.Date(2020, time.January, 1, 0, 0, 0, 3, time.UTC),
|
||||
}
|
||||
|
||||
testCases := []testCaseT{
|
||||
{
|
||||
groups: []models.APIAlertGroup{},
|
||||
sorted: []models.APIAlertGroup{},
|
||||
},
|
||||
{
|
||||
groups: []models.APIAlertGroup{},
|
||||
sorted: []models.APIAlertGroup{},
|
||||
sortReverse: true,
|
||||
},
|
||||
{
|
||||
groups: []models.APIAlertGroup{
|
||||
{AlertGroup: g1},
|
||||
{AlertGroup: g2},
|
||||
{AlertGroup: g3},
|
||||
},
|
||||
sorted: []models.APIAlertGroup{
|
||||
{AlertGroup: g1},
|
||||
{AlertGroup: g2},
|
||||
{AlertGroup: g3},
|
||||
},
|
||||
},
|
||||
{
|
||||
groups: []models.APIAlertGroup{
|
||||
{AlertGroup: g1},
|
||||
{AlertGroup: g2},
|
||||
{AlertGroup: g3},
|
||||
},
|
||||
sortReverse: true,
|
||||
sorted: []models.APIAlertGroup{
|
||||
{AlertGroup: g3},
|
||||
{AlertGroup: g2},
|
||||
{AlertGroup: g1},
|
||||
},
|
||||
},
|
||||
{
|
||||
groups: []models.APIAlertGroup{
|
||||
{AlertGroup: g2},
|
||||
{AlertGroup: g3},
|
||||
{AlertGroup: g1},
|
||||
},
|
||||
sorted: []models.APIAlertGroup{
|
||||
{AlertGroup: g1},
|
||||
{AlertGroup: g2},
|
||||
{AlertGroup: g3},
|
||||
},
|
||||
},
|
||||
{
|
||||
groups: []models.APIAlertGroup{
|
||||
{AlertGroup: g2},
|
||||
{AlertGroup: g3},
|
||||
{AlertGroup: g1},
|
||||
},
|
||||
sortReverse: true,
|
||||
sorted: []models.APIAlertGroup{
|
||||
{AlertGroup: g3},
|
||||
{AlertGroup: g2},
|
||||
{AlertGroup: g1},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
sort.Slice(testCase.groups, func(i, j int) bool {
|
||||
return sortByStartsAt(i, j, testCase.groups, testCase.sortReverse)
|
||||
})
|
||||
|
||||
gotJSON, _ := json.MarshalIndent(testCase.groups, "", " ")
|
||||
expectedJSON, _ := json.MarshalIndent(testCase.sorted, "", " ")
|
||||
|
||||
if string(gotJSON) != string(expectedJSON) {
|
||||
diff := difflib.UnifiedDiff{
|
||||
A: difflib.SplitLines(string(expectedJSON)),
|
||||
B: difflib.SplitLines(string(gotJSON)),
|
||||
FromFile: "Expected",
|
||||
ToFile: "Response",
|
||||
Context: 3,
|
||||
}
|
||||
text, err := difflib.GetUnifiedDiffString(diff)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Errorf("Sort result mismatch:\n%s", text)
|
||||
}
|
||||
}
|
||||
}
|
||||
+28
-6
@@ -991,6 +991,7 @@ func TestVerifyAllGroups(t *testing.T) {
|
||||
for _, version := range mock.ListAllMocks() {
|
||||
t.Logf("Testing API using mock files from Alertmanager %s", version)
|
||||
mockAlerts(version)
|
||||
apiCache.Flush()
|
||||
r := ginTestEngine()
|
||||
req := httptest.NewRequest("GET", "/alerts.json", nil)
|
||||
resp := httptest.NewRecorder()
|
||||
@@ -1085,12 +1086,13 @@ func TestVerifyAllGroups(t *testing.T) {
|
||||
}
|
||||
|
||||
type sortTest struct {
|
||||
filter string
|
||||
sortOrder string
|
||||
sortLabel string
|
||||
sortReverse string
|
||||
expectedLabel string
|
||||
expectedValues []string
|
||||
defaultSortReverse bool
|
||||
filter string
|
||||
sortOrder string
|
||||
sortLabel string
|
||||
sortReverse string
|
||||
expectedLabel string
|
||||
expectedValues []string
|
||||
}
|
||||
|
||||
var sortTests = []sortTest{
|
||||
@@ -1182,6 +1184,24 @@ var sortTests = []sortTest{
|
||||
expectedLabel: "job",
|
||||
expectedValues: []string{"node_ping", "node_ping", "node_ping", "node_exporter", "node_exporter", "node_exporter"},
|
||||
},
|
||||
{
|
||||
defaultSortReverse: true,
|
||||
filter: "q=@receiver=by-cluster-service",
|
||||
sortOrder: "label",
|
||||
sortLabel: "job",
|
||||
sortReverse: "a",
|
||||
expectedLabel: "job",
|
||||
expectedValues: []string{"node_ping", "node_ping", "node_ping", "node_exporter", "node_exporter", "node_exporter"},
|
||||
},
|
||||
{
|
||||
defaultSortReverse: false,
|
||||
filter: "q=@receiver=by-cluster-service",
|
||||
sortOrder: "label",
|
||||
sortLabel: "job",
|
||||
sortReverse: "2",
|
||||
expectedLabel: "job",
|
||||
expectedValues: []string{"node_exporter", "node_exporter", "node_exporter", "node_ping", "node_ping", "node_ping"},
|
||||
},
|
||||
}
|
||||
|
||||
func TestSortOrder(t *testing.T) {
|
||||
@@ -1200,6 +1220,8 @@ func TestSortOrder(t *testing.T) {
|
||||
r := ginTestEngine()
|
||||
|
||||
for _, testCase := range sortTests {
|
||||
apiCache.Flush()
|
||||
config.Config.Grid.Sorting.Reverse = testCase.defaultSortReverse
|
||||
uri := fmt.Sprintf(
|
||||
"/alerts.json?sortOrder=%s&sortLabel=%s&sortReverse=%s&%s",
|
||||
testCase.sortOrder,
|
||||
|
||||
+8
-4
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"mime"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
@@ -372,15 +373,18 @@ func main() {
|
||||
go Tick()
|
||||
|
||||
listen := fmt.Sprintf("%s:%d", config.Config.Listen.Address, config.Config.Listen.Port)
|
||||
listener, err := net.Listen("tcp", listen)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Infof("Listening on %s", listener.Addr())
|
||||
|
||||
httpServer := &http.Server{
|
||||
Addr: listen,
|
||||
Handler: router,
|
||||
}
|
||||
|
||||
go func() {
|
||||
if err := httpServer.ListenAndServe(); err != nil {
|
||||
log.Infof("Listening on %s", listen)
|
||||
}
|
||||
_ = httpServer.Serve(listener)
|
||||
}()
|
||||
|
||||
quit := make(chan os.Signal, 1)
|
||||
|
||||
@@ -273,6 +273,7 @@ func TestGrids(t *testing.T) {
|
||||
r := ginTestEngine()
|
||||
// re-run a few times to test the cache
|
||||
for i := 1; i <= 3; i++ {
|
||||
apiCache.Flush()
|
||||
req := httptest.NewRequest("GET", "/alerts.json?gridLabel="+testCase.gridLabel+testCase.requestQuery, nil)
|
||||
resp := httptest.NewRecorder()
|
||||
r.ServeHTTP(resp, req)
|
||||
|
||||
@@ -22,7 +22,6 @@ require (
|
||||
github.com/go-openapi/validate v0.19.7
|
||||
github.com/golangci/golangci-lint v1.24.0
|
||||
github.com/google/go-cmp v0.4.0
|
||||
github.com/hansboder/gocovmerge v0.0.0-20190813150856-3552c2006da5
|
||||
github.com/hansrodtang/randomcolor v0.0.0-20160512071917-d27108b3d7a5
|
||||
github.com/jarcoal/httpmock v1.0.5
|
||||
github.com/knadh/koanf v0.9.1
|
||||
|
||||
@@ -263,8 +263,6 @@ github.com/gostaticanalysis/analysisutil v0.0.0-20190318220348-4088753ea4d3/go.m
|
||||
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
|
||||
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
|
||||
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
|
||||
github.com/hansboder/gocovmerge v0.0.0-20190813150856-3552c2006da5 h1:PKFOvrFdBuYn8F0GD0itonb8M/DFiSrDYfF54DUIE3M=
|
||||
github.com/hansboder/gocovmerge v0.0.0-20190813150856-3552c2006da5/go.mod h1:YuynvyiaysiM6eitHFN8mL+lRHEbZFTHG+hVXX1xZf8=
|
||||
github.com/hansrodtang/randomcolor v0.0.0-20160512071917-d27108b3d7a5 h1:9WT/rQ2tZI0TBZhBsA/dYU1bdI2QKaUzQ3X6YDwaLUY=
|
||||
github.com/hansrodtang/randomcolor v0.0.0-20160512071917-d27108b3d7a5/go.mod h1:XvzgNvkcPBKMxeywFlPioejZ5ulXi/GMLZDoBw915FA=
|
||||
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
|
||||
|
||||
+1
-3
@@ -21,10 +21,8 @@ cmd/karma/bindata_assetfs.go: $(GOBIN)/go-bindata-assetfs $(SOURCES_JS) ui/build
|
||||
$(NAME): go.mod go.sum cmd/karma/bindata_assetfs.go $(SOURCES_GO)
|
||||
$(GO) build -ldflags "-X main.version=$(VERSION)" ./cmd/karma
|
||||
|
||||
$(GOBIN)/gocovmerge: go.mod go.sum
|
||||
$(GO) install github.com/hansboder/gocovmerge
|
||||
.PHONY: test-go
|
||||
test-go: $(GOBIN)/gocovmerge
|
||||
test-go:
|
||||
@rm -f profile.*
|
||||
$(ENV) ./scripts/test-unit.sh
|
||||
$(ENV) ./scripts/test-main.sh
|
||||
|
||||
@@ -3,7 +3,12 @@
|
||||
set -o errexit
|
||||
set -o pipefail
|
||||
|
||||
gocovmerge profile.* | grep -vE '^github.com/prymitive/karma/cmd/karma/bindata_assetfs.go:' > coverage.txt
|
||||
echo "mode: set" > coverage.txt
|
||||
cat profile.* \
|
||||
| grep -v mode: \
|
||||
| grep -vE '^github.com/prymitive/karma/cmd/karma/bindata_assetfs.go:' \
|
||||
| sort -r \
|
||||
| awk '{if($1 != last) {print $0;last=$1}}' >> coverage.txt
|
||||
rm -f profile.*
|
||||
|
||||
go tool cover -func coverage.txt | tail -n 1 | awk '{print $3}'
|
||||
|
||||
@@ -24,6 +24,7 @@ go test \
|
||||
ALERTMANAGER_URI=http://localhost \
|
||||
ALERTMANAGER_INTERVAL=1s \
|
||||
LISTEN_ADDRESS=127.0.0.1 \
|
||||
LISTEN_PORT=0 \
|
||||
LOG_LEVEL=fatal \
|
||||
LOG_CONFIG=false \
|
||||
./karma.test \
|
||||
|
||||
Reference in New Issue
Block a user