TDengine/tools/keeper/api/zabbix_test.go
guozhenwei 07a943655b
test: add keeper test cases (#33319)
* test: add collect test case

* test: add zabbix test

* test: add adapter2 test

* test: add audit test

* test: add checkhealth test

* test: add common test

* test: add gen_metric test

* test: add command test

* test: add connector test

* test: add log test

* test: add web test

* test: add builder test

* test: add handle test

* test: add util test

* test: add audit test

* test: add adapter2 test

* test: modify test case
2025-10-23 18:10:30 +08:00

71 lines
1.4 KiB
Go

package api
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
)
func TestZabbixInit(t *testing.T) {
gin.SetMode(gin.TestMode)
z := &Zabbix{}
r := gin.New()
z.Init(r)
check := func(path string) {
req := httptest.NewRequest(http.MethodGet, path, nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("%s status = %d, want 200", path, w.Code)
}
var resp struct {
Data json.RawMessage `json:"data"`
}
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("%s unmarshal response error: %v", path, err)
}
// Accept both null or empty array
if string(resp.Data) != "null" {
var arr []any
if err := json.Unmarshal(resp.Data, &arr); err != nil {
t.Fatalf("%s data is not array/null: %s", path, string(resp.Data))
}
if len(arr) != 0 {
t.Fatalf("%s data length = %d, want 0", path, len(arr))
}
}
}
check("/zabbix/float")
check("/zabbix/string")
}
func TestZabbix_sortLabel(t *testing.T) {
z := &Zabbix{}
if out := z.sortLabel(nil); out != "" {
t.Fatalf("expected empty string, got %q", out)
}
if out := z.sortLabel(map[string]string{}); out != "" {
t.Fatalf("expected empty string, got %q", out)
}
in := map[string]string{
"c": "3",
"a": "1",
"b": "2",
}
out := z.sortLabel(in)
want := "a=1_b=2_c=3"
if out != want {
t.Fatalf("sortLabel mismatch, want %q, got %q", want, out)
}
}