mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-21 14:44:00 +00:00
7c97cef95c
Consistently using t.Fatal* simplifies the test code and makes it less fragile, since it is common error
to forget to make proper cleanup after t.Error* call. Also t.Error* calls do not provide any practical
benefits when some tests fail. They just clutter test output with additional noise information,
which do not help in fixing failing tests most of the time.
This is a follow-up for a9525da8a4
60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
package prometheusimport
|
|
|
|
import (
|
|
"bytes"
|
|
"flag"
|
|
"log"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/remotewrite"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fs"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/protoparser/common"
|
|
)
|
|
|
|
var (
|
|
srv *httptest.Server
|
|
testOutput *bytes.Buffer
|
|
)
|
|
|
|
func TestInsertHandler(t *testing.T) {
|
|
setUp()
|
|
defer tearDown()
|
|
req := httptest.NewRequest(http.MethodPost, "/insert/0/api/v1/import/prometheus", bytes.NewBufferString(`{"foo":"bar"}
|
|
go_memstats_alloc_bytes_total 1`))
|
|
if err := InsertHandler(nil, req); err != nil {
|
|
t.Fatalf("unxepected error %s", err)
|
|
}
|
|
expectedMsg := "cannot unmarshal Prometheus line"
|
|
if !strings.Contains(testOutput.String(), expectedMsg) {
|
|
t.Fatalf("output %q should contain %q", testOutput.String(), expectedMsg)
|
|
}
|
|
}
|
|
|
|
func setUp() {
|
|
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
w.WriteHeader(204)
|
|
}))
|
|
flag.Parse()
|
|
remoteWriteFlag := "remoteWrite.url"
|
|
if err := flag.Lookup(remoteWriteFlag).Value.Set(srv.URL); err != nil {
|
|
log.Fatalf("unable to set %q with value %q, err: %v", remoteWriteFlag, srv.URL, err)
|
|
}
|
|
logger.Init()
|
|
common.StartUnmarshalWorkers()
|
|
remotewrite.Init()
|
|
testOutput = &bytes.Buffer{}
|
|
logger.SetOutputForTests(testOutput)
|
|
}
|
|
|
|
func tearDown() {
|
|
common.StopUnmarshalWorkers()
|
|
srv.Close()
|
|
logger.ResetOutputForTest()
|
|
tmpDataDir := flag.Lookup("remoteWrite.tmpDataPath").Value.String()
|
|
fs.MustRemoveAll(tmpDataDir)
|
|
|
|
}
|