From b4eb7d3f920e91d6377d49795069f2b113c3fb1f Mon Sep 17 00:00:00 2001
From: Roman Plessl <roman@plessl.info>
Date: Mon, 14 Sep 2015 12:28:10 +0200
Subject: [PATCH] added more UNIT test cases for covering all parts of the code
 added debug statement for debugging OpenTSDB communication

---
 outputs/opentsdb/opentsdb.go      | 18 +++++++++++++----
 outputs/opentsdb/opentsdb_test.go | 32 +++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/outputs/opentsdb/opentsdb.go b/outputs/opentsdb/opentsdb.go
index 1c29e731d..d44bb837c 100644
--- a/outputs/opentsdb/opentsdb.go
+++ b/outputs/opentsdb/opentsdb.go
@@ -17,6 +17,8 @@ type OpenTSDB struct {
 
 	Host string
 	Port int
+
+	Debug bool
 }
 
 var sampleConfig = `
@@ -29,6 +31,9 @@ var sampleConfig = `
 
 	# Port of the OpenTSDB server in telnet mode
 	port = 4242
+
+	# Debug true - Prints OpenTSDB communication
+	debug = false
 `
 
 type MetricLine struct {
@@ -70,15 +75,20 @@ func (o *OpenTSDB) Write(bp client.BatchPoints) error {
 			Metric:    fmt.Sprintf("%s%s", o.Prefix, pt.Measurement),
 			Timestamp: timeNow.Unix(),
 		}
-		if metricValue, err := buildValue(bp, pt); err == nil {
-			metric.Value = metricValue
+		metricValue, buildError := buildValue(bp, pt);
+		if buildError != nil {
+			fmt.Printf("OpenTSDB: %s\n", buildError.Error())
+			continue
 		}
+		metric.Value = metricValue
 
 		tagsSlice := buildTags(bp.Tags, pt.Tags)
 		metric.Tags = fmt.Sprint(strings.Join(tagsSlice, " "))
 
 		messageLine := fmt.Sprintf("put %s %v %s %s\n", metric.Metric, metric.Timestamp, metric.Value, metric.Tags)
-		fmt.Print(messageLine)
+		if (o.Debug) {
+			fmt.Print(messageLine)
+		}
 		_, err := connection.Write([]byte(messageLine))
 		if err != nil {
 			fmt.Errorf("OpenTSDB: Telnet writing error %s", err.Error())
@@ -115,7 +125,7 @@ func buildValue(bp client.BatchPoints, pt client.Point) (string, error) {
 	case float64:
 		retv = FloatToString(float64(p))
 	default:
-		return retv, fmt.Errorf("undeterminable type for telegraf")
+		return retv, fmt.Errorf("unexpected type %T with value %v for OpenTSDB", v, v)
 	}
 	return retv, nil
 }
diff --git a/outputs/opentsdb/opentsdb_test.go b/outputs/opentsdb/opentsdb_test.go
index 2db064243..e73b1ae2b 100644
--- a/outputs/opentsdb/opentsdb_test.go
+++ b/outputs/opentsdb/opentsdb_test.go
@@ -3,7 +3,9 @@ package opentsdb
 import (
 	"reflect"
 	"testing"
+	"time"
 
+	"github.com/influxdb/influxdb/client"
 	"github.com/influxdb/telegraf/testutil"
 	"github.com/stretchr/testify/require"
 )
@@ -60,4 +62,34 @@ func TestWrite(t *testing.T) {
 	// Verify that we can successfully write data to OpenTSDB
 	err = o.Write(testutil.MockBatchPoints())
 	require.NoError(t, err)
+
+	// Verify postive and negative test cases of writing data
+	var bp client.BatchPoints
+	bp.Time = time.Now()
+	bp.Tags = map[string]string{"testkey": "testvalue"}
+	bp.Points = []client.Point{
+		{
+			Measurement: "justametric.float",
+			Fields:      map[string]interface{}{"value": float64(1.0)},
+		},
+		{
+			Measurement: "justametric.int",
+			Fields:      map[string]interface{}{"value": int64(123456789)},
+		},
+		{
+			Measurement: "justametric.uint",
+			Fields:      map[string]interface{}{"value": uint64(123456789012345)},
+		},
+		{
+			Measurement: "justametric.string",
+			Fields:      map[string]interface{}{"value": "Lorem Ipsum"},
+		},
+		{
+			Measurement: "justametric.anotherfloat",
+			Fields:      map[string]interface{}{"value": float64(42.0)},
+		},
+	}
+	err = o.Write(bp)
+	require.NoError(t, err)
+
 }