From cf08b56460628272ae16c421d566cd027f79e70c Mon Sep 17 00:00:00 2001 From: ncohensm Date: Wed, 22 Jun 2016 17:44:41 -0700 Subject: [PATCH] remove debug output and add empty post body test --- plugins/inputs/http_listener/http_listener.go | 25 ++++++----------- .../http_listener/http_listener_test.go | 28 ++++++++++++++++--- 2 files changed, 32 insertions(+), 21 deletions(-) diff --git a/plugins/inputs/http_listener/http_listener.go b/plugins/inputs/http_listener/http_listener.go index 9d8f7582c..a60af1fc3 100644 --- a/plugins/inputs/http_listener/http_listener.go +++ b/plugins/inputs/http_listener/http_listener.go @@ -106,34 +106,25 @@ func (t *HttpListener) ServeHTTP(res http.ResponseWriter, req *http.Request) { var path = req.URL.Path[1:] if path == "write" { - log.Printf("Received write request: [%s]\n", string(body)) - var metrics []telegraf.Metric - if len(body) == 0 { - log.Printf("No metrics to parse\n") + metrics, err = t.parser.Parse(body) + if err == nil { + t.storeMetrics(metrics) } else { - metrics, err = t.parser.Parse(body) - if err == nil { - t.storeMetrics(metrics) - log.Printf("Persisted %d metrics\n", len(metrics)) - } else { - log.Printf("Problem parsing body: [%s], Error: %s\n", string(body), err) - res.WriteHeader(500) - res.Write([]byte("ERROR parsing metrics")) - } - res.WriteHeader(204) - res.Write([]byte("")) + log.Printf("Problem parsing body: [%s], Error: %s\n", string(body), err) + res.WriteHeader(500) + res.Write([]byte("ERROR parsing metrics")) } + res.WriteHeader(204) + res.Write([]byte("")) } else if path == "query" { // Deliver a dummy response to the query endpoint, as some InfluxDB clients test endpoint availability with a query - log.Printf("Received query request: [%s]\n", string(body)) res.Header().Set("Content-Type", "application/json") res.Header().Set("X-Influxdb-Version", "1.0") res.WriteHeader(200) res.Write([]byte("{\"results\":[]}")) } else { // Don't know how to respond to calls to other endpoints - log.Printf("Received unknown %s request: [%s]\n", path, string(body)) res.WriteHeader(404) res.Write([]byte("Not Found")) } diff --git a/plugins/inputs/http_listener/http_listener_test.go b/plugins/inputs/http_listener/http_listener_test.go index fdf1354c2..1a00b8d2e 100644 --- a/plugins/inputs/http_listener/http_listener_test.go +++ b/plugins/inputs/http_listener/http_listener_test.go @@ -22,6 +22,8 @@ cpu_load_short,host=server05 value=12.0 1422568543702900257 cpu_load_short,host=server06 value=12.0 1422568543702900257 ` badMsg = "blahblahblah: 42\n" + + emptyMsg = "" ) func newTestHttpListener() (*HttpListener) { @@ -46,7 +48,7 @@ func TestWriteHTTP(t *testing.T) { // post single message to listener var resp, err = http.Post("http://localhost:8186/write?db=mydb", "", bytes.NewBuffer([]byte(testMsg))) require.NoError(t, err) - require.EqualValues(t, resp.StatusCode, 204) + require.EqualValues(t, 204, resp.StatusCode) time.Sleep(time.Millisecond * 15) acc.AssertContainsTaggedFields(t, "cpu_load_short", @@ -57,7 +59,7 @@ func TestWriteHTTP(t *testing.T) { // post multiple message to listener resp, err = http.Post("http://localhost:8186/write?db=mydb", "", bytes.NewBuffer([]byte(testMsgs))) require.NoError(t, err) - require.EqualValues(t, resp.StatusCode, 204) + require.EqualValues(t, 204, resp.StatusCode) time.Sleep(time.Millisecond * 15) hostTags := []string{"server02", "server03", @@ -85,7 +87,25 @@ func TestWriteHTTPInvalid(t *testing.T) { // post single message to listener var resp, err = http.Post("http://localhost:8186/write?db=mydb", "", bytes.NewBuffer([]byte(badMsg))) require.NoError(t, err) - require.EqualValues(t, resp.StatusCode, 500) + require.EqualValues(t, 500, resp.StatusCode) +} + +func TestWriteHTTPEmpty(t *testing.T) { + time.Sleep(time.Millisecond * 250) + + listener := newTestHttpListener() + listener.parser, _ = parsers.NewInfluxParser() + + acc := &testutil.Accumulator{} + require.NoError(t, listener.Start(acc)) + defer listener.Stop() + + time.Sleep(time.Millisecond * 25) + + // post single message to listener + var resp, err = http.Post("http://localhost:8186/write?db=mydb", "", bytes.NewBuffer([]byte(emptyMsg))) + require.NoError(t, err) + require.EqualValues(t, 204, resp.StatusCode) } func TestQueryHTTP(t *testing.T) { @@ -103,6 +123,6 @@ func TestQueryHTTP(t *testing.T) { // post query to listener var resp, err = http.Post("http://localhost:8186/query?db=&q=CREATE+DATABASE+IF+NOT+EXISTS+%22mydb%22", "", nil) require.NoError(t, err) - require.EqualValues(t, resp.StatusCode, 200) + require.EqualValues(t, 200, resp.StatusCode) }