Repository: VictoriaMetrics/VictoriaMetrics
Pull request #413 (2020-04-12 11:25:07)

[vmalert] add tests to webserver

codecov[bot]: # [Codecov](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/413?src=pr&el=h1) Report
> Merging [#413](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/413?src=pr&el=desc) into [master](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/commit/7c9405f53d258febcabc6b98c66b00e33a5f7163&el=desc) will **increase** coverage by `0.27%`.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/413/graphs/tree.svg?width=650&height=150&src=pr&token=YND8dYVcXn)](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/413?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #413      +/-   ##
==========================================
+ Coverage   70.93%   71.20%   +0.27%     
==========================================
  Files         131      131              
  Lines       20476    20471       -5     
==========================================
+ Hits        14524    14576      +52     
+ Misses       4846     4778      -68     
- Partials     1106     1117      +11     
```


| [Impacted Files](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/413?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [app/vmalert/web.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/413/diff?src=pr&el=tree#diff-YXBwL3ZtYWxlcnQvd2ViLmdv) | `72.41% <ø> (+72.41%)` | :arrow_up: |
| [app/vmselect/promql/aggr\_incremental.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/413/diff?src=pr&el=tree#diff-YXBwL3Ztc2VsZWN0L3Byb21xbC9hZ2dyX2luY3JlbWVudGFsLmdv) | `94.11% <0.00%> (-4.42%)` | :arrow_down: |
| [lib/mergeset/table.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/413/diff?src=pr&el=tree#diff-bGliL21lcmdlc2V0L3RhYmxlLmdv) | `68.32% <0.00%> (-1.00%)` | :arrow_down: |
| [lib/storage/index\_db.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/413/diff?src=pr&el=tree#diff-bGliL3N0b3JhZ2UvaW5kZXhfZGIuZ28=) | `75.74% <0.00%> (-0.84%)` | :arrow_down: |
| [lib/storage/partition.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/413/diff?src=pr&el=tree#diff-bGliL3N0b3JhZ2UvcGFydGl0aW9uLmdv) | `71.63% <0.00%> (+0.47%)` | :arrow_up: |
| [lib/fs/dir\_remover.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/413/diff?src=pr&el=tree#diff-bGliL2ZzL2Rpcl9yZW1vdmVyLmdv) | `26.66% <0.00%> (+23.33%)` | :arrow_up: |
| [app/vmalert/rule.go](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/413/diff?src=pr&el=tree#diff-YXBwL3ZtYWxlcnQvcnVsZS5nbw==) | `81.13% <0.00%> (+23.58%)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/413?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/413?src=pr&el=footer). Last update [7c9405f...50e9d67](https://codecov.io/gh/VictoriaMetrics/VictoriaMetrics/pull/413?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

hagen1778: LGTM! I'd really appreciate linter comments to be fixed as well, even if they aren't related to this PR.

Patch:
diff --git a/app/vmalert/web.go b/app/vmalert/web.go
index 0977984e3..60453dabc 100644
--- a/app/vmalert/web.go
+++ b/app/vmalert/web.go
@@ -58,13 +58,14 @@ func (rh *requestHandler) handler(w http.ResponseWriter, r *http.Request) bool {
 	}
 }
 
+type listAlertsResponse struct {
+	Data struct {
+		Alerts []*apiAlert `json:"alerts"`
+	} `json:"data"`
+	Status string `json:"status"`
+}
+
 func (rh *requestHandler) list() ([]byte, error) {
-	type listAlertsResponse struct {
-		Data struct {
-			Alerts []*apiAlert `json:"alerts"`
-		} `json:"data"`
-		Status string `json:"status"`
-	}
 	lr := listAlertsResponse{Status: "success"}
 	for _, g := range rh.groups {
 		for _, r := range g.Rules {


Test patch:
diff --git a/app/vmalert/web_test.go b/app/vmalert/web_test.go
new file mode 100644
index 000000000..e20394d32
--- /dev/null
+++ b/app/vmalert/web_test.go
@@ -0,0 +1,72 @@
+package main
+
+import (
+	"encoding/json"
+	"net/http"
+	"net/http/httptest"
+	"reflect"
+	"testing"
+
+	"github.com/VictoriaMetrics/VictoriaMetrics/app/vmalert/notifier"
+)
+
+func TestHandler(t *testing.T) {
+	rule := &Rule{
+		Name: "alert",
+		alerts: map[uint64]*notifier.Alert{
+			0: {},
+		},
+	}
+	rh := &requestHandler{
+		groups: []Group{{
+			Name:  "group",
+			Rules: []*Rule{rule},
+		}},
+	}
+	getResp := func(url string, to interface{}, code int) {
+		t.Helper()
+		resp, err := http.Get(url)
+		if err != nil {
+			t.Errorf("unexpected err %s", err)
+		}
+		if code != resp.StatusCode {
+			t.Errorf("unexpected status code %d want %d", resp.StatusCode, code)
+		}
+		defer func() {
+			if err := resp.Body.Close(); err != nil {
+				t.Errorf("err closing body %s", err)
+			}
+		}()
+		if to != nil {
+			if err = json.NewDecoder(resp.Body).Decode(to); err != nil {
+				t.Errorf("unexpected err %s", err)
+			}
+		}
+	}
+	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { rh.handler(w, r) }))
+	defer ts.Close()
+	t.Run("/api/v1/alerts", func(t *testing.T) {
+		lr := listAlertsResponse{}
+		getResp(ts.URL+"/api/v1/alerts", &lr, 200)
+		if length := len(lr.Data.Alerts); length != 1 {
+			t.Errorf("expected 1 alert got %d", length)
+		}
+	})
+	t.Run("/api/v1/group/0/status", func(t *testing.T) {
+		alert := &apiAlert{}
+		getResp(ts.URL+"/api/v1/group/0/status", alert, 200)
+		expAlert := rule.newAlertAPI(*rule.alerts[0])
+		if !reflect.DeepEqual(alert, expAlert) {
+			t.Errorf("expected %v is equal to %v", alert, expAlert)
+		}
+	})
+	t.Run("/api/v1/group/1/status", func(t *testing.T) {
+		getResp(ts.URL+"/api/v1/group/1/status", nil, 404)
+	})
+	t.Run("/api/v1/unknown-group/0/status", func(t *testing.T) {
+		getResp(ts.URL+"/api/v1/unknown-group/0/status", nil, 404)
+	})
+	t.Run("/", func(t *testing.T) {
+		getResp(ts.URL, nil, 200)
+	})
+}