forked from influxdata/influxdb-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
205 lines (181 loc) · 6.58 KB
/
client_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2020-2021 InfluxData, Inc. All rights reserved.
// Use of this source code is governed by MIT
// license that can be found in the LICENSE file.
package influxdb2
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/influxdata/influxdb-client-go/v2/domain"
http2 "github.com/influxdata/influxdb-client-go/v2/internal/http"
iwrite "github.com/influxdata/influxdb-client-go/v2/internal/write"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestUrls(t *testing.T) {
urls := []struct {
serverURL string
serverAPIURL string
writeURLPrefix string
}{
{"http://host:8086", "http://host:8086/api/v2/", "http://host:8086/api/v2/write"},
{"http://host:8086/", "http://host:8086/api/v2/", "http://host:8086/api/v2/write"},
{"http://host:8086/path", "http://host:8086/path/api/v2/", "http://host:8086/path/api/v2/write"},
{"http://host:8086/path/", "http://host:8086/path/api/v2/", "http://host:8086/path/api/v2/write"},
{"http://host:8086/path1/path2/path3", "http://host:8086/path1/path2/path3/api/v2/", "http://host:8086/path1/path2/path3/api/v2/write"},
{"http://host:8086/path1/path2/path3/", "http://host:8086/path1/path2/path3/api/v2/", "http://host:8086/path1/path2/path3/api/v2/write"},
}
for _, url := range urls {
t.Run(url.serverURL, func(t *testing.T) {
c := NewClient(url.serverURL, "x")
ci := c.(*clientImpl)
assert.Equal(t, url.serverURL, ci.serverURL)
assert.Equal(t, url.serverAPIURL, ci.httpService.ServerAPIURL())
ws := iwrite.NewService("org", "bucket", ci.httpService, c.Options().WriteOptions())
wu := ws.WriteURL()
assert.Equal(t, url.writeURLPrefix+"?bucket=bucket&org=org&precision=ns", wu)
})
}
}
func TestWriteAPIManagement(t *testing.T) {
data := []struct {
org string
bucket string
expectedCout int
}{
{"o1", "b1", 1},
{"o1", "b2", 2},
{"o1", "b1", 2},
{"o2", "b1", 3},
{"o2", "b2", 4},
{"o1", "b2", 4},
{"o1", "b3", 5},
{"o2", "b2", 5},
}
c := NewClient("http://localhost", "x").(*clientImpl)
for i, d := range data {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
w := c.WriteAPI(d.org, d.bucket)
assert.NotNil(t, w)
assert.Len(t, c.writeAPIs, d.expectedCout)
wb := c.WriteAPIBlocking(d.org, d.bucket)
assert.NotNil(t, wb)
assert.Len(t, c.syncWriteAPIs, d.expectedCout)
})
}
c.Close()
assert.Len(t, c.writeAPIs, 0)
assert.Len(t, c.syncWriteAPIs, 0)
}
func TestUserAgent(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-time.After(100 * time.Millisecond)
if r.Header.Get("User-Agent") == http2.UserAgent {
w.WriteHeader(http.StatusNoContent)
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer server.Close()
c := NewClient(server.URL, "x")
up, err := c.Ping(context.Background())
require.NoError(t, err)
assert.True(t, up)
err = c.WriteAPIBlocking("o", "b").WriteRecord(context.Background(), "a,a=a a=1i")
assert.NoError(t, err)
}
func TestServerError429(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-time.After(100 * time.Millisecond)
w.Header().Set("Retry-After", "1")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusTooManyRequests)
_, _ = w.Write([]byte(`{"code":"too many requests", "message":"exceeded rate limit"}`))
}))
defer server.Close()
c := NewClient(server.URL, "x")
err := c.WriteAPIBlocking("o", "b").WriteRecord(context.Background(), "a,a=a a=1i")
require.Error(t, err)
assert.Equal(t, "too many requests: exceeded rate limit", err.Error())
}
func TestServerOnPath(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/proxy/0:0/influx/api/v2/write" {
w.WriteHeader(http.StatusNoContent)
} else {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(fmt.Sprintf(`{"code":"internal server error", "message":"%s"}`, r.URL.Path)))
}
}))
defer server.Close()
c := NewClient(server.URL+"/proxy/0:0/influx/", "x")
err := c.WriteAPIBlocking("o", "b").WriteRecord(context.Background(), "a,a=a a=1i")
require.NoError(t, err)
}
func TestServerErrorNonJSON(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-time.After(100 * time.Millisecond)
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(`internal server error`))
}))
defer server.Close()
c := NewClient(server.URL, "x")
//Test non JSON error in custom code
err := c.WriteAPIBlocking("o", "b").WriteRecord(context.Background(), "a,a=a a=1i")
require.Error(t, err)
assert.Equal(t, "500 Internal Server Error: internal server error", err.Error())
// Test non JSON error from generated code
params := &domain.GetBucketsParams{}
b, err := c.APIClient().GetBuckets(context.Background(), params)
assert.Nil(t, b)
require.Error(t, err)
assert.Equal(t, "500 Internal Server Error: internal server error", err.Error())
}
func TestServerErrorInflux1_8(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Influxdb-Error", "bruh moment")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(404)
_, _ = w.Write([]byte(`{"error": "bruh moment"}`))
}))
defer server.Close()
c := NewClient(server.URL, "x")
err := c.WriteAPIBlocking("o", "b").WriteRecord(context.Background(), "a,a=a a=1i")
require.Error(t, err)
assert.Equal(t, "404 Not Found: bruh moment", err.Error())
}
func TestServerErrorEmptyBody(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(404)
}))
defer server.Close()
c := NewClient(server.URL, "x")
err := c.WriteAPIBlocking("o", "b").WriteRecord(context.Background(), "a,a=a a=1i")
require.Error(t, err)
assert.Equal(t, "Unexpected status code 404", err.Error())
}
func TestReadyFail(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte(`<html></html>`))
}))
defer server.Close()
c := NewClient(server.URL, "x")
r, err := c.Ready(context.Background())
assert.Error(t, err)
assert.Nil(t, r)
}
func TestHealthFail(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte(`<html></html>`))
}))
defer server.Close()
c := NewClient(server.URL, "x")
h, err := c.Health(context.Background())
assert.Error(t, err)
assert.Nil(t, h)
}