forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logzio_test.go
94 lines (78 loc) · 1.94 KB
/
logzio_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package logzio
import (
"bytes"
"compress/gzip"
"encoding/json"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
"io"
"net/http"
"net/http/httptest"
"testing"
)
const (
testToken = "123456789"
testURL = "https://logzio.com"
)
func TestConnetWithoutToken(t *testing.T) {
l := &Logzio{
URL: testURL,
Log: testutil.Logger{},
}
err := l.Connect()
require.Error(t, err)
}
func TestParseMetric(t *testing.T) {
l := &Logzio{}
for _, tm := range testutil.MockMetrics() {
lm := l.parseMetric(tm)
require.Equal(t, tm.Fields(), lm.Metric[tm.Name()])
require.Equal(t, logzioType, lm.Type)
require.Equal(t, tm.Tags(), lm.Dimensions)
require.Equal(t, tm.Time(), lm.Time)
}
}
func TestBadStatusCode(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer ts.Close()
l := &Logzio{
Token: testToken,
URL: ts.URL,
Log: testutil.Logger{},
}
err := l.Connect()
require.NoError(t, err)
err = l.Write(testutil.MockMetrics())
require.Error(t, err)
}
func TestWrite(t *testing.T) {
tm := testutil.TestMetric(float64(3.14), "test1")
var body bytes.Buffer
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gz, err := gzip.NewReader(r.Body)
require.NoError(t, err)
_, err = io.Copy(&body, gz)
require.NoError(t, err)
var lm Metric
err = json.Unmarshal(body.Bytes(), &lm)
require.NoError(t, err)
require.Equal(t, tm.Fields(), lm.Metric[tm.Name()])
require.Equal(t, logzioType, lm.Type)
require.Equal(t, tm.Tags(), lm.Dimensions)
require.Equal(t, tm.Time(), lm.Time)
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
l := &Logzio{
Token: testToken,
URL: ts.URL,
Log: testutil.Logger{},
}
err := l.Connect()
require.NoError(t, err)
err = l.Write([]telegraf.Metric{tm})
require.NoError(t, err)
}