forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bigquery_test.go
165 lines (131 loc) · 3.65 KB
/
bigquery_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package bigquery
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"cloud.google.com/go/bigquery"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
"google.golang.org/api/option"
)
const (
successfulResponse = "{\"kind\": \"bigquery#tableDataInsertAllResponse\"}"
)
var testingHost string
var testDuration = config.Duration(5 * time.Second)
var receivedBody map[string]json.RawMessage
type Row struct {
Tag1 string `json:"tag1"`
Timestamp string `json:"timestamp"`
Value float64 `json:"value"`
}
func TestConnect(t *testing.T) {
srv := localBigQueryServer(t)
testingHost = strings.ReplaceAll(srv.URL, "http://", "")
defer srv.Close()
b := &BigQuery{
Project: "test-project",
Dataset: "test-dataset",
Timeout: testDuration,
}
cerr := b.setUpTestClient()
require.NoError(t, cerr)
berr := b.Connect()
require.NoError(t, berr)
}
func TestWrite(t *testing.T) {
srv := localBigQueryServer(t)
testingHost = strings.ReplaceAll(srv.URL, "http://", "")
defer srv.Close()
b := &BigQuery{
Project: "test-project",
Dataset: "test-dataset",
Timeout: testDuration,
}
mockMetrics := testutil.MockMetrics()
if err := b.setUpTestClient(); err != nil {
require.NoError(t, err)
}
if err := b.Connect(); err != nil {
require.NoError(t, err)
}
if err := b.Write(mockMetrics); err != nil {
require.NoError(t, err)
}
var rows []map[string]json.RawMessage
if err := json.Unmarshal(receivedBody["rows"], &rows); err != nil {
require.NoError(t, err)
}
var row Row
if err := json.Unmarshal(rows[0]["json"], &row); err != nil {
require.NoError(t, err)
}
pt, _ := time.Parse(time.RFC3339, row.Timestamp)
require.Equal(t, mockMetrics[0].Tags()["tag1"], row.Tag1)
require.Equal(t, mockMetrics[0].Time(), pt)
require.Equal(t, mockMetrics[0].Fields()["value"], row.Value)
}
func TestMetricToTableDefault(t *testing.T) {
b := &BigQuery{
Project: "test-project",
Dataset: "test-dataset",
Timeout: testDuration,
warnedOnHyphens: make(map[string]bool),
ReplaceHyphenTo: "_",
Log: testutil.Logger{},
}
otn := "table-with-hyphens"
ntn := b.metricToTable(otn)
require.Equal(t, "table_with_hyphens", ntn)
require.True(t, b.warnedOnHyphens[otn])
}
func TestMetricToTableCustom(t *testing.T) {
log := testutil.Logger{}
b := &BigQuery{
Project: "test-project",
Dataset: "test-dataset",
Timeout: testDuration,
warnedOnHyphens: make(map[string]bool),
ReplaceHyphenTo: "*",
Log: log,
}
otn := "table-with-hyphens"
ntn := b.metricToTable(otn)
require.Equal(t, "table*with*hyphens", ntn)
require.True(t, b.warnedOnHyphens[otn])
}
func (b *BigQuery) setUpTestClient() error {
noAuth := option.WithoutAuthentication()
endpoints := option.WithEndpoint("http://" + testingHost)
ctx := context.Background()
c, err := bigquery.NewClient(ctx, b.Project, noAuth, endpoints)
if err != nil {
return err
}
b.client = c
return nil
}
func localBigQueryServer(t *testing.T) *httptest.Server {
srv := httptest.NewServer(http.NotFoundHandler())
srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/projects/test-project/datasets/test-dataset/tables/test1/insertAll":
decoder := json.NewDecoder(r.Body)
if err := decoder.Decode(&receivedBody); err != nil {
require.NoError(t, err)
}
w.WriteHeader(http.StatusOK)
if _, err := w.Write([]byte(successfulResponse)); err != nil {
require.NoError(t, err)
}
default:
w.WriteHeader(http.StatusNotFound)
}
})
return srv
}