package warp10 import ( "fmt" "testing" "time" "github.com/stretchr/testify/require" "github.com/influxdata/telegraf/testutil" ) type ErrorTest struct { Message string Expected string } func TestWriteWarp10(t *testing.T) { w := Warp10{ Prefix: "unit.test", WarpURL: "http://localhost:8090", Token: "WRITE", } var now = time.Now() payload := w.GenWarp10Payload(testutil.MockMetrics(), now) require.Exactly(t, fmt.Sprintf("%d// unit.testtest1.value{source=telegraf,tag1=value1} 1.000000\n", now.UnixNano()/1000), payload) } func TestHandleWarp10Error(t *testing.T) { w := Warp10{ Prefix: "unit.test", WarpURL: "http://localhost:8090", Token: "WRITE", } tests := [...]*ErrorTest{ { Message: ` Error 500 io.warp10.script.WarpScriptException: Invalid token.

HTTP ERROR 500

Problem accessing /api/v0/update. Reason:

    io.warp10.script.WarpScriptException: Invalid token.

`, Expected: fmt.Sprintf("Invalid token"), }, { Message: ` Error 500 io.warp10.script.WarpScriptException: Token Expired.

HTTP ERROR 500

Problem accessing /api/v0/update. Reason:

    io.warp10.script.WarpScriptException: Token Expired.

`, Expected: fmt.Sprintf("Token Expired"), }, { Message: ` Error 500 io.warp10.script.WarpScriptException: Token revoked.

HTTP ERROR 500

Problem accessing /api/v0/update. Reason:

    io.warp10.script.WarpScriptException: Token revoked.

`, Expected: fmt.Sprintf("Token revoked"), }, { Message: ` Error 500 io.warp10.script.WarpScriptException: Write token missing.

HTTP ERROR 500

Problem accessing /api/v0/update. Reason:

    io.warp10.script.WarpScriptException: Write token missing.

`, Expected: "Write token missing", }, { Message: `Error 503: server unavailable`, Expected: "Error 503: server unavailable", }, } for _, handledError := range tests { payload := w.HandleError(handledError.Message, 511) require.Exactly(t, handledError.Expected, payload) } }