remove debug output and add empty post body test

This commit is contained in:
ncohensm 2016-06-22 17:44:41 -07:00
parent e9d3870778
commit cf08b56460
2 changed files with 32 additions and 21 deletions

View File

@ -106,34 +106,25 @@ func (t *HttpListener) ServeHTTP(res http.ResponseWriter, req *http.Request) {
var path = req.URL.Path[1:] var path = req.URL.Path[1:]
if path == "write" { if path == "write" {
log.Printf("Received write request: [%s]\n", string(body))
var metrics []telegraf.Metric var metrics []telegraf.Metric
if len(body) == 0 { metrics, err = t.parser.Parse(body)
log.Printf("No metrics to parse\n") if err == nil {
t.storeMetrics(metrics)
} else { } else {
metrics, err = t.parser.Parse(body) log.Printf("Problem parsing body: [%s], Error: %s\n", string(body), err)
if err == nil { res.WriteHeader(500)
t.storeMetrics(metrics) res.Write([]byte("ERROR parsing 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(""))
} }
res.WriteHeader(204)
res.Write([]byte(""))
} else if path == "query" { } else if path == "query" {
// Deliver a dummy response to the query endpoint, as some InfluxDB clients test endpoint availability with a 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("Content-Type", "application/json")
res.Header().Set("X-Influxdb-Version", "1.0") res.Header().Set("X-Influxdb-Version", "1.0")
res.WriteHeader(200) res.WriteHeader(200)
res.Write([]byte("{\"results\":[]}")) res.Write([]byte("{\"results\":[]}"))
} else { } else {
// Don't know how to respond to calls to other endpoints // 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.WriteHeader(404)
res.Write([]byte("Not Found")) res.Write([]byte("Not Found"))
} }

View File

@ -22,6 +22,8 @@ cpu_load_short,host=server05 value=12.0 1422568543702900257
cpu_load_short,host=server06 value=12.0 1422568543702900257 cpu_load_short,host=server06 value=12.0 1422568543702900257
` `
badMsg = "blahblahblah: 42\n" badMsg = "blahblahblah: 42\n"
emptyMsg = ""
) )
func newTestHttpListener() (*HttpListener) { func newTestHttpListener() (*HttpListener) {
@ -46,7 +48,7 @@ func TestWriteHTTP(t *testing.T) {
// post single message to listener // post single message to listener
var resp, err = http.Post("http://localhost:8186/write?db=mydb", "", bytes.NewBuffer([]byte(testMsg))) var resp, err = http.Post("http://localhost:8186/write?db=mydb", "", bytes.NewBuffer([]byte(testMsg)))
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, resp.StatusCode, 204) require.EqualValues(t, 204, resp.StatusCode)
time.Sleep(time.Millisecond * 15) time.Sleep(time.Millisecond * 15)
acc.AssertContainsTaggedFields(t, "cpu_load_short", acc.AssertContainsTaggedFields(t, "cpu_load_short",
@ -57,7 +59,7 @@ func TestWriteHTTP(t *testing.T) {
// post multiple message to listener // post multiple message to listener
resp, err = http.Post("http://localhost:8186/write?db=mydb", "", bytes.NewBuffer([]byte(testMsgs))) resp, err = http.Post("http://localhost:8186/write?db=mydb", "", bytes.NewBuffer([]byte(testMsgs)))
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, resp.StatusCode, 204) require.EqualValues(t, 204, resp.StatusCode)
time.Sleep(time.Millisecond * 15) time.Sleep(time.Millisecond * 15)
hostTags := []string{"server02", "server03", hostTags := []string{"server02", "server03",
@ -85,7 +87,25 @@ func TestWriteHTTPInvalid(t *testing.T) {
// post single message to listener // post single message to listener
var resp, err = http.Post("http://localhost:8186/write?db=mydb", "", bytes.NewBuffer([]byte(badMsg))) var resp, err = http.Post("http://localhost:8186/write?db=mydb", "", bytes.NewBuffer([]byte(badMsg)))
require.NoError(t, err) 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) { func TestQueryHTTP(t *testing.T) {
@ -103,6 +123,6 @@ func TestQueryHTTP(t *testing.T) {
// post query to listener // post query to listener
var resp, err = http.Post("http://localhost:8186/query?db=&q=CREATE+DATABASE+IF+NOT+EXISTS+%22mydb%22", "", nil) var resp, err = http.Post("http://localhost:8186/query?db=&q=CREATE+DATABASE+IF+NOT+EXISTS+%22mydb%22", "", nil)
require.NoError(t, err) require.NoError(t, err)
require.EqualValues(t, resp.StatusCode, 200) require.EqualValues(t, 200, resp.StatusCode)
} }