forked from instana/python-sensor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_mysql-python.py
More file actions
249 lines (187 loc) · 7.4 KB
/
Copy pathtest_mysql-python.py
File metadata and controls
249 lines (187 loc) · 7.4 KB
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
from __future__ import absolute_import
import logging
import os
import sys
from unittest import SkipTest
from nose.tools import assert_equals
from instana.singletons import tracer
if sys.version_info < (3, 0):
import MySQLdb
else:
raise SkipTest("MySQL-python supported on Python 2.7 only")
logger = logging.getLogger(__name__)
if 'MYSQL_HOST' in os.environ:
mysql_host = os.environ['MYSQL_HOST']
elif 'TRAVIS_MYSQL_HOST' in os.environ:
mysql_host = os.environ['TRAVIS_MYSQL_HOST']
else:
mysql_host = '127.0.0.1'
if 'MYSQL_PORT' in os.environ:
mysql_port = int(os.environ['MYSQL_PORT'])
else:
mysql_port = 3306
if 'MYSQL_DB' in os.environ:
mysql_db = os.environ['MYSQL_DB']
else:
mysql_db = "travis_ci_test"
if 'MYSQL_USER' in os.environ:
mysql_user = os.environ['MYSQL_USER']
else:
mysql_user = "root"
if 'MYSQL_PW' in os.environ:
mysql_pw = os.environ['MYSQL_PW']
elif 'TRAVIS_MYSQL_PASS' in os.environ:
mysql_pw = os.environ['TRAVIS_MYSQL_PASS']
else:
mysql_pw = ''
create_table_query = 'CREATE TABLE IF NOT EXISTS users(id serial primary key, \
name varchar(40) NOT NULL, email varchar(40) NOT NULL)'
create_proc_query = """
CREATE PROCEDURE test_proc(IN t VARCHAR(255))
BEGIN
SELECT name FROM users WHERE name = t;
END
"""
db = MySQLdb.connect(host=mysql_host, port=mysql_port,
user=mysql_user, passwd=mysql_pw,
db=mysql_db)
cursor = db.cursor()
cursor.execute(create_table_query)
while cursor.nextset() is not None:
pass
cursor.execute('DROP PROCEDURE IF EXISTS test_proc')
while cursor.nextset() is not None:
pass
cursor.execute(create_proc_query)
while cursor.nextset() is not None:
pass
cursor.close()
db.close()
class TestMySQLPython:
def setUp(self):
logger.warn("MySQL connecting: %s:<pass>@%s:3306/%s", mysql_user, mysql_host, mysql_db)
self.db = MySQLdb.connect(host=mysql_host, port=mysql_port,
user=mysql_user, passwd=mysql_pw,
db=mysql_db)
self.cursor = self.db.cursor()
self.recorder = tracer.recorder
self.recorder.clear_spans()
tracer.cur_ctx = None
def tearDown(self):
""" Do nothing for now """
return None
def test_vanilla_query(self):
self.cursor.execute("""SELECT * from users""")
result = self.cursor.fetchone()
assert_equals(3, len(result))
spans = self.recorder.queued_spans()
assert_equals(0, len(spans))
def test_basic_query(self):
result = None
with tracer.start_active_span('test'):
result = self.cursor.execute("""SELECT * from users""")
self.cursor.fetchone()
assert(result >= 0)
spans = self.recorder.queued_spans()
assert_equals(2, len(spans))
db_span = spans[0]
test_span = spans[1]
assert_equals("test", test_span.data.sdk.name)
assert_equals(test_span.t, db_span.t)
assert_equals(db_span.p, test_span.s)
assert_equals(None, db_span.error)
assert_equals(None, db_span.ec)
assert_equals(db_span.n, "mysql")
assert_equals(db_span.data.mysql.db, mysql_db)
assert_equals(db_span.data.mysql.user, mysql_user)
assert_equals(db_span.data.mysql.stmt, 'SELECT * from users')
assert_equals(db_span.data.mysql.host, "%s:3306" % mysql_host)
def test_basic_insert(self):
result = None
with tracer.start_active_span('test'):
result = self.cursor.execute(
"""INSERT INTO users(name, email) VALUES(%s, %s)""",
('beaker', 'beaker@muppets.com'))
assert_equals(1, result)
spans = self.recorder.queued_spans()
assert_equals(2, len(spans))
db_span = spans[0]
test_span = spans[1]
assert_equals("test", test_span.data.sdk.name)
assert_equals(test_span.t, db_span.t)
assert_equals(db_span.p, test_span.s)
assert_equals(None, db_span.error)
assert_equals(None, db_span.ec)
assert_equals(db_span.n, "mysql")
assert_equals(db_span.data.mysql.db, mysql_db)
assert_equals(db_span.data.mysql.user, mysql_user)
assert_equals(db_span.data.mysql.stmt, 'INSERT INTO users(name, email) VALUES(%s, %s)')
assert_equals(db_span.data.mysql.host, "%s:3306" % mysql_host)
def test_executemany(self):
result = None
with tracer.start_active_span('test'):
result = self.cursor.executemany("INSERT INTO users(name, email) VALUES(%s, %s)",
[('beaker', 'beaker@muppets.com'), ('beaker', 'beaker@muppets.com')])
self.db.commit()
assert_equals(2, result)
spans = self.recorder.queued_spans()
assert_equals(2, len(spans))
db_span = spans[0]
test_span = spans[1]
assert_equals("test", test_span.data.sdk.name)
assert_equals(test_span.t, db_span.t)
assert_equals(db_span.p, test_span.s)
assert_equals(None, db_span.error)
assert_equals(None, db_span.ec)
assert_equals(db_span.n, "mysql")
assert_equals(db_span.data.mysql.db, mysql_db)
assert_equals(db_span.data.mysql.user, mysql_user)
assert_equals(db_span.data.mysql.stmt, 'INSERT INTO users(name, email) VALUES(%s, %s)')
assert_equals(db_span.data.mysql.host, "%s:3306" % mysql_host)
def test_call_proc(self):
result = None
with tracer.start_active_span('test'):
result = self.cursor.callproc('test_proc', ('beaker',))
assert(result)
spans = self.recorder.queued_spans()
assert_equals(2, len(spans))
db_span = spans[0]
test_span = spans[1]
assert_equals("test", test_span.data.sdk.name)
assert_equals(test_span.t, db_span.t)
assert_equals(db_span.p, test_span.s)
assert_equals(None, db_span.error)
assert_equals(None, db_span.ec)
assert_equals(db_span.n, "mysql")
assert_equals(db_span.data.mysql.db, mysql_db)
assert_equals(db_span.data.mysql.user, mysql_user)
assert_equals(db_span.data.mysql.stmt, 'test_proc')
assert_equals(db_span.data.mysql.host, "%s:3306" % mysql_host)
def test_error_capture(self):
result = None
span = None
try:
with tracer.start_active_span('test'):
result = self.cursor.execute("""SELECT * from blah""")
self.cursor.fetchone()
except Exception:
pass
finally:
if span:
span.finish()
assert(result is None)
spans = self.recorder.queued_spans()
assert_equals(2, len(spans))
db_span = spans[0]
test_span = spans[1]
assert_equals("test", test_span.data.sdk.name)
assert_equals(test_span.t, db_span.t)
assert_equals(db_span.p, test_span.s)
assert_equals(True, db_span.error)
assert_equals(1, db_span.ec)
assert_equals(db_span.data.mysql.error, '(1146, "Table \'%s.blah\' doesn\'t exist")' % mysql_db)
assert_equals(db_span.n, "mysql")
assert_equals(db_span.data.mysql.db, mysql_db)
assert_equals(db_span.data.mysql.user, mysql_user)
assert_equals(db_span.data.mysql.stmt, 'SELECT * from blah')
assert_equals(db_span.data.mysql.host, "%s:3306" % mysql_host)