2023-01-24 06:14:34 +00:00
|
|
|
package prometheusimport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/app/vmagent/remotewrite"
|
2023-01-27 07:21:03 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fs"
|
2023-01-24 06:14:34 +00:00
|
|
|
"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()
|
2023-02-23 02:58:44 +00:00
|
|
|
req := httptest.NewRequest(http.MethodPost, "/insert/0/api/v1/import/prometheus", bytes.NewBufferString(`{"foo":"bar"}
|
2023-01-24 06:14:34 +00:00
|
|
|
go_memstats_alloc_bytes_total 1`))
|
|
|
|
if err := InsertHandler(nil, req); err != nil {
|
2024-07-11 13:53:40 +00:00
|
|
|
t.Fatalf("unxepected error %s", err)
|
2023-01-24 06:14:34 +00:00
|
|
|
}
|
2023-01-24 06:22:05 +00:00
|
|
|
expectedMsg := "cannot unmarshal Prometheus line"
|
|
|
|
if !strings.Contains(testOutput.String(), expectedMsg) {
|
2024-07-11 13:53:40 +00:00
|
|
|
t.Fatalf("output %q should contain %q", testOutput.String(), expectedMsg)
|
2023-01-24 06:14:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2023-01-27 07:21:03 +00:00
|
|
|
tmpDataDir := flag.Lookup("remoteWrite.tmpDataPath").Value.String()
|
|
|
|
fs.MustRemoveAll(tmpDataDir)
|
|
|
|
|
2023-01-24 06:14:34 +00:00
|
|
|
}
|