From 25413fb769fcc46d6dead30cb933761442d23d84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Thu, 9 Apr 2020 09:17:15 +0100 Subject: [PATCH 1/4] fix(tests): replace gocovmerge with a simple bash script --- go.mod | 1 - go.sum | 2 -- make/go.mk | 4 +--- scripts/gocovmerge.sh | 7 ++++++- tools.go | 1 - 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index cc83da1f0..5f2e1a0f6 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 322f45631..b00680b36 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/make/go.mk b/make/go.mk index 4013a83d4..e654ad601 100644 --- a/make/go.mk +++ b/make/go.mk @@ -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 diff --git a/scripts/gocovmerge.sh b/scripts/gocovmerge.sh index 6789adb9e..ded009fec 100755 --- a/scripts/gocovmerge.sh +++ b/scripts/gocovmerge.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}' diff --git a/tools.go b/tools.go index 90ef30c33..cb6ac3bec 100644 --- a/tools.go +++ b/tools.go @@ -10,5 +10,4 @@ import ( _ "github.com/elazarl/go-bindata-assetfs" _ "github.com/go-bindata/go-bindata/v3" _ "github.com/golangci/golangci-lint/pkg/commands" - _ "github.com/hansboder/gocovmerge" ) From 397dedd897a678e3e0f9e5c5495d3700fd8869e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Thu, 9 Apr 2020 09:44:21 +0100 Subject: [PATCH 2/4] fix(backend): report listening address in logs --- cmd/karma/main.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/cmd/karma/main.go b/cmd/karma/main.go index 12e4a8252..e717c19cc 100644 --- a/cmd/karma/main.go +++ b/cmd/karma/main.go @@ -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) From 01f51cfeda6a6b2cffdbf579a39b66d45106b374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Thu, 9 Apr 2020 09:44:27 +0100 Subject: [PATCH 3/4] fix(tests): use auto port for test-main --- scripts/test-main.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/test-main.sh b/scripts/test-main.sh index c728ec6ac..f4635f234 100755 --- a/scripts/test-main.sh +++ b/scripts/test-main.sh @@ -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 \ From 9eb3c4b94185e7b89c1ec8e82917b21996649427 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Thu, 9 Apr 2020 11:25:37 +0100 Subject: [PATCH 4/4] fix(tests): add more test coverage --- cmd/karma/alerts_test.go | 119 +++++++++++++++++++++++++++++++++++++++ cmd/karma/api_test.go | 34 +++++++++-- cmd/karma/views_test.go | 1 + 3 files changed, 148 insertions(+), 6 deletions(-) create mode 100644 cmd/karma/alerts_test.go diff --git a/cmd/karma/alerts_test.go b/cmd/karma/alerts_test.go new file mode 100644 index 000000000..fbd9a737e --- /dev/null +++ b/cmd/karma/alerts_test.go @@ -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) + } + } +} diff --git a/cmd/karma/api_test.go b/cmd/karma/api_test.go index 2fe726665..fe67aafa9 100644 --- a/cmd/karma/api_test.go +++ b/cmd/karma/api_test.go @@ -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, diff --git a/cmd/karma/views_test.go b/cmd/karma/views_test.go index 9a145ed72..6f9951f33 100644 --- a/cmd/karma/views_test.go +++ b/cmd/karma/views_test.go @@ -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)