forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
372 lines (325 loc) · 9.95 KB
/
server.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package helpers
import (
"bytes"
"common"
"configuration"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"syscall"
"time"
influxdb "github.com/influxdb/influxdb-go"
. "launchpad.net/gocheck"
)
type Server struct {
p *os.Process
configFile string
sslOnly bool
apiPort int
sslApiPort int
args []string
}
func NewSslServer(configFile string, c *C) *Server {
return newServerCommon(configFile, true, true, c)
}
func NewServerWithArgs(configFile string, c *C, args ...string) *Server {
return newServerCommon(configFile, false, false, c, args...)
}
func NewServer(configFile string, c *C) *Server {
return newServerCommon(configFile, true, false, c)
}
func newServerCommon(configFile string, deleteData, ssl bool, c *C, args ...string) *Server {
config := configuration.LoadConfiguration("../../" + configFile)
s := &Server{configFile: configFile, apiPort: config.ApiHttpPort, sslApiPort: config.ApiHttpSslPort, sslOnly: ssl, args: args}
if deleteData {
c.Assert(os.RemoveAll(config.DataDir), IsNil)
c.Assert(os.RemoveAll(config.WalDir), IsNil)
c.Assert(os.RemoveAll(config.RaftDir), IsNil)
}
err := s.Start()
c.Assert(err, IsNil)
s.WaitForServerToStart()
return s
}
func (self *Server) WaitForServerToStart() {
url := fmt.Sprintf("http://localhost:%d/ping", self.apiPort)
if self.sslOnly {
url = fmt.Sprintf("https://localhost:%d/ping", self.sslApiPort)
}
client := http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
for i := 0; i < 600; i++ {
resp, err := client.Get(url)
if err != nil {
// wait 100 milliseconds before retrying to give the server a
// chance to startup
time.Sleep(100 * time.Millisecond)
continue
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
panic(resp)
}
// wait for the server to be marked up by other servers
time.Sleep(500 * time.Millisecond)
return
}
// wait for other servers to send heartbeat and detect that the
// server is up
panic("server didn't start")
}
func (self *Server) WaitForServerToSync() {
for i := 0; i < 600; i++ {
resp, err := http.Get(fmt.Sprintf("http://localhost:%d/sync?u=root&p=root", self.apiPort))
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return
}
if resp.StatusCode != http.StatusInternalServerError {
panic(resp)
}
// wait 100 millisecond to give the servers chance to catch up
time.Sleep(100 * time.Millisecond)
}
panic("servers didn't sync")
}
// optional db
func (self *Server) GetClient(db string, c *C) *influxdb.Client {
return self.GetClientWithUser(db, "", "", c)
}
func (self *Server) GetClientWithUser(db, username, password string, c *C) *influxdb.Client {
client, err := influxdb.NewClient(&influxdb.ClientConfig{
Host: fmt.Sprintf("localhost:%d", self.apiPort),
Username: username,
Password: password,
Database: db,
})
c.Assert(err, IsNil)
return client
}
func (self *Server) WriteData(data interface{}, c *C, precision ...influxdb.TimePrecision) {
client := self.GetClient("db1", c)
var series []*influxdb.Series
switch x := data.(type) {
case string:
c.Assert(json.Unmarshal([]byte(x), &series), IsNil)
case []*influxdb.Series:
series = x
default:
c.Fatalf("Unknown type: %T", x)
}
var err error
if len(precision) == 0 {
err = client.WriteSeries(series)
} else {
err = client.WriteSeriesWithTimePrecision(series, precision[0])
}
c.Assert(err, IsNil)
self.WaitForServerToSync()
}
func (self *Server) RunQuery(query string, precision influxdb.TimePrecision, c *C) []*influxdb.Series {
return self.RunQueryAsUser(query, precision, "user", "pass", true, c)
}
func (self *Server) RunQueryAsRoot(query string, precision influxdb.TimePrecision, c *C) []*influxdb.Series {
return self.RunQueryAsUser(query, precision, "root", "root", true, c)
}
func (self *Server) RunQueryAsUser(query string, precision influxdb.TimePrecision, username, password string, isValid bool, c *C) []*influxdb.Series {
client := self.GetClientWithUser("db1", username, password, c)
series, err := client.Query(query, precision)
if isValid {
c.Assert(err, IsNil)
} else {
c.Assert(err, NotNil)
}
return series
}
func (self *Server) Start() error {
if self.p != nil {
return fmt.Errorf("Server is already running with pid %d", self.p.Pid)
}
fmt.Printf("Starting server")
dir, err := os.Getwd()
if err != nil {
return err
}
root := filepath.Join(dir, "..", "..")
filename := filepath.Join(root, "daemon")
if self.configFile == "" {
self.configFile = "src/integration/test_config_single.toml"
}
args := []string{filename, "-config", self.configFile}
args = append(args, self.args...)
p, err := os.StartProcess(filename, args, &os.ProcAttr{
Dir: root,
Env: os.Environ(),
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
})
if err != nil {
return err
}
self.p = p
return nil
}
func (self *Server) ApiPort() int {
return self.apiPort
}
func (self *Server) Stop() {
if self.p == nil {
return
}
self.p.Signal(syscall.SIGTERM)
self.p.Wait()
self.p = nil
}
func (self *Server) SetSslOnly(sslOnly bool) {
self.sslOnly = sslOnly
}
func (self *Server) DoesWalExist() error {
config := configuration.LoadConfiguration("../../" + self.configFile)
_, err := os.Stat(filepath.Join(config.WalDir, "log.1"))
return err
}
func ResultsToSeriesCollection(results []*common.SerializedSeries) *SeriesCollection {
return &SeriesCollection{Members: results}
}
type SeriesCollection struct {
Members []*common.SerializedSeries
}
func (self *SeriesCollection) GetSeries(name string, c *C) *Series {
for _, s := range self.Members {
if s.Name == name {
return &Series{s}
}
}
c.Fatalf("Couldn't find series '%s' in:\n", name, self)
return nil
}
type Series struct {
*common.SerializedSeries
}
func (self *Series) GetValueForPointAndColumn(pointIndex int, columnName string, c *C) interface{} {
columnIndex := -1
for index, name := range self.Columns {
if name == columnName {
columnIndex = index
}
}
if columnIndex == -1 {
c.Errorf("Couldn't find column '%s' in series:\n", columnName, self)
return nil
}
if pointIndex > len(self.Points)-1 {
c.Errorf("Fewer than %d points in series '%s':\n", pointIndex+1, self.Name, self)
}
return self.Points[pointIndex][columnIndex]
}
type Point struct {
Values []interface{}
}
func (self *Server) Query(database, query string, onlyLocal bool, c *C) *SeriesCollection {
return self.QueryWithUsername(database, query, onlyLocal, c, "paul", "pass")
}
func (self *Server) QueryAsRoot(database, query string, onlyLocal bool, c *C) *SeriesCollection {
return self.QueryWithUsername(database, query, onlyLocal, c, "root", "root")
}
func (self *Server) GetResponse(database, query, username, password string, onlyLocal bool, c *C) *http.Response {
encodedQuery := url.QueryEscape(query)
fullUrl := fmt.Sprintf("http://localhost:%d/db/%s/series?u=%s&p=%s&q=%s", self.apiPort, database, username, password, encodedQuery)
if onlyLocal {
fullUrl = fullUrl + "&force_local=true"
}
resp, err := http.Get(fullUrl)
c.Assert(err, IsNil)
return resp
}
func (self *Server) GetErrorBody(database, query, username, password string, onlyLocal bool, c *C) (string, int) {
resp := self.GetResponse(database, query, username, password, onlyLocal, c)
defer resp.Body.Close()
c.Assert(resp.StatusCode, Not(Equals), http.StatusOK)
bytes, err := ioutil.ReadAll(resp.Body)
c.Assert(err, IsNil)
return string(bytes), resp.StatusCode
}
func (self *Server) QueryWithUsername(database, query string, onlyLocal bool, c *C, username, password string) *SeriesCollection {
resp := self.GetResponse(database, query, username, password, onlyLocal, c)
defer resp.Body.Close()
c.Assert(resp.StatusCode, Equals, http.StatusOK)
body, err := ioutil.ReadAll(resp.Body)
c.Assert(err, IsNil)
var js []*common.SerializedSeries
err = json.Unmarshal(body, &js)
if err != nil {
fmt.Println("NOT JSON: ", string(body))
}
c.Assert(err, IsNil)
return ResultsToSeriesCollection(js)
}
func (self *Server) VerifyForbiddenQuery(database, query string, onlyLocal bool, c *C, username, password string) string {
encodedQuery := url.QueryEscape(query)
fullUrl := fmt.Sprintf("http://localhost:%d/db/%s/series?u=%s&p=%s&q=%s", self.apiPort, database, username, password, encodedQuery)
if onlyLocal {
fullUrl = fullUrl + "&force_local=true"
}
resp, err := http.Get(fullUrl)
c.Assert(err, IsNil)
defer resp.Body.Close()
c.Assert(resp.StatusCode, Equals, http.StatusForbidden)
body, err := ioutil.ReadAll(resp.Body)
c.Assert(err, IsNil)
return string(body)
}
func (self *Server) Post(url, data string, c *C) *http.Response {
err := self.Request("POST", url, data, c)
return err
}
func (self *Server) Delete(url, body string, c *C) *http.Response {
err := self.Request("DELETE", url, body, c)
return err
}
func (self *Server) PostGetBody(url, data string, c *C) []byte {
resp := self.Request("POST", url, data, c)
body, err := ioutil.ReadAll(resp.Body)
c.Assert(err, IsNil)
return body
}
func (self *Server) Get(url string, c *C) []byte {
resp := self.Request("GET", url, "", c)
body, err := ioutil.ReadAll(resp.Body)
c.Assert(err, IsNil)
return body
}
func (self *Server) Request(method, url, data string, c *C) *http.Response {
fullUrl := fmt.Sprintf("http://localhost:%d%s", self.apiPort, url)
req, err := http.NewRequest(method, fullUrl, bytes.NewBufferString(data))
c.Assert(err, IsNil)
resp, err := http.DefaultClient.Do(req)
c.Assert(err, IsNil)
return resp
}
func (self *Server) RemoveAllContinuousQueries(db string, c *C) {
client := self.GetClient(db, c)
queries, err := client.GetContinuousQueries()
c.Assert(err, IsNil)
for _, q := range queries {
c.Assert(client.DeleteContinuousQueries(int(q["id"].(float64))), IsNil)
}
}
func (self *Server) AssertContinuousQueryCount(db string, count int, c *C) {
client := self.GetClient(db, c)
queries, err := client.GetContinuousQueries()
c.Assert(err, IsNil)
c.Assert(queries, HasLen, count)
}