forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfluxdb_test.py
executable file
·135 lines (121 loc) · 4.34 KB
/
influxdb_test.py
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
#!/usr/bin/env python
# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Test influxdb.py."""
import BaseHTTPServer
import threading
import re
import unittest
import influxdb
class TestInfluxPoint(unittest.TestCase):
def test_from_dict(self):
def check(sample, measurement, tags, fields, time):
point = influxdb.Point.from_dict(sample)
self.assertEqual(point.measurement, measurement)
self.assertEqual(point.tags, tags)
self.assertEqual(point.fields, fields)
self.assertEqual(point.time, time)
check(
{
'measurement': 'metric',
'tags': {'style': 'stylish'},
'fields': {'baseball': 'diamond', 'basketball': False},
'time': 42
},
'metric',
{'style': 'stylish'},
{'baseball': 'diamond', 'basketball': False},
42,
)
check(
{
'measurement': 'metric',
'tags': {},
'fields': {'num': 2.7},
},
'metric',
{},
{'num': 2.7},
None,
)
# Check that objects that don't meet the InfluxPoint spec are unchanged.
sample = {
'measurement': 'metric',
'tags': {'tag': 'value'},
'notfields': 'something',
}
self.assertEqual(influxdb.Point.from_dict(sample), sample)
def test_serialize(self):
def check(measurement, tags, fields, time, expected):
point = influxdb.Point(measurement, tags, fields, time)
self.assertEqual(point.serialize(), expected)
check(
'metric',
{'type': 'good'},
{'big?': True, 'size': 20},
42,
'metric,type=good big?=True,size=20 42',
)
check(
'measure with spaces',
{'tag,with,comma': 'tagval=with=equals'},
{',,': 20.2, 'string': 'yarn'},
None,
r'measure\ with\ spaces,tag\,with\,comma=tagval\=with\=equals \,\,=20.2,string="yarn"',
)
check(
'measure with spaces',
{'tag,with,comma': 'tagval=with=equals'},
{',,': 20.2, 'string': 'yarn'},
None,
r'measure\ with\ spaces,tag\,with\,comma=tagval\=with\=equals \,\,=20.2,string="yarn"',
)
class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_POST(self): # pylint: disable=invalid-name
if not self.path.startswith('/write'):
raise ValueError(
'path should start with \'/write\', but is \'%s\'' % self.path
)
body = self.rfile.read(int(self.headers.getheader('content-length')))
new_ids = [int(match) for match in re.findall(r'id=(\d+)', body)]
self.server.received = self.server.received.union(new_ids)
self.send_response(201)
class TestInfluxPusher(unittest.TestCase):
def setUp(self):
self.port = 8000
self.written = 0
self.test_server = BaseHTTPServer.HTTPServer(
('', self.port),
RequestHandler,
)
self.test_server.received = set()
thread = threading.Thread(target=self.test_server.serve_forever)
thread.start()
def tearDown(self):
self.test_server.shutdown()
for num in xrange(self.written):
self.assertIn(num, self.test_server.received)
def test_push(self):
points = [influxdb.Point('metric', {}, {'id': num}, None)
for num in xrange(110)]
pusher = influxdb.Pusher(
'localhost:%d' % self.port,
None,
'username',
'pass123',
)
pusher.push(points, 'mydb')
self.written = 110
if __name__ == '__main__':
unittest.main()