This repository has been archived by the owner on Feb 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
test_datazilla_request.py
246 lines (195 loc) · 8.53 KB
/
test_datazilla_request.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
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
import unittest
from mock import patch
from dzclient import DatazillaRequest, DatazillaResult, DatazillaResultsCollection
class DatazillaRequestTest(unittest.TestCase):
def test_init_with_date(self):
"""Can provide test date on instantiation."""
req = DatazillaRequest(
'http', 'host', 'project', 'key', 'secret', test_date=12345)
self.assertEqual(req.test_date, 12345)
def test_add_datazilla_result(self):
"""Can add a DatazillaResult to a DatazillaRequest."""
req = DatazillaRequest(
'http', 'host', 'project', 'key', 'secret'
)
res = DatazillaResult({'suite': {'test': [1, 2, 3]}})
req.add_datazilla_result(res)
self.assertEqual(req.results.results, res.results)
def test_add_second_datazilla_result(self):
"""Adding a second DatazillaResult joins their results."""
req = DatazillaRequest(
'http', 'host', 'project', 'key', 'secret'
)
res1 = DatazillaResult({'suite1': {'test': [1]}})
res2 = DatazillaResult({'suite2': {'test': [2]}})
req.add_datazilla_result(res1)
req.add_datazilla_result(res2)
self.assertEqual(
req.results.results,
{'suite1': {'test': [1]}, 'suite2': {'test': [2]}},
)
def test_create_from_results_collection(self):
collection = DatazillaResultsCollection(machine_name='localhost',
os='linux')
test_date = collection.test_date
req = DatazillaRequest.create(
'http', 'host', 'project', 'key', 'secret', collection
)
self.assertEqual(req.machine_name, 'localhost')
self.assertEqual(req.os, 'linux')
self.assertEqual(req.test_date, test_date)
self.assertEqual(req.host, 'host')
self.assertEqual(req.project, 'project')
self.assertEqual(req.oauth_key, 'key')
self.assertEqual(req.oauth_secret, 'secret')
def test_datasets(self):
"""Tests dataset creation for submission to datazilla"""
req = DatazillaRequest(
protocol='http',
host='host',
project='project',
oauth_key='key',
oauth_secret='secret',
machine_name='qm-pxp01',
os='linux',
os_version='Ubuntu 11.10',
platform='x86_64',
build_name='Firefox',
version='14.0a2',
revision='785345035a3b',
branch='Mozilla-Aurora',
id='20120228122102',
)
results = {'suite1': {'test1': [1]}, 'suite2': {'test2': [2]}}
req.add_datazilla_result(DatazillaResult(results))
datasets = req.datasets()
self.assertEqual(len(datasets), 2)
for dataset in datasets:
self.assertEqual(set(dataset.keys()), set(['results', 'test_build', 'test_machine', 'testrun']))
self.assertEqual(dataset['test_build'],
{'branch': 'Mozilla-Aurora',
'id': '20120228122102',
'name': 'Firefox',
'revision': '785345035a3b',
'version': '14.0a2'})
self.assertEqual(dataset['test_machine'],
{'name': 'qm-pxp01',
'os': 'linux',
'osversion': 'Ubuntu 11.10',
'platform': 'x86_64'})
self.assertEqual(set(dataset['testrun'].keys()), set(['suite', 'date']))
suite = dataset['testrun']['suite']
self.assertTrue(suite in results)
self.assertEqual(dataset['results'], results[suite])
@patch.object(DatazillaRequest, 'send')
def test_submit(self, mock_send):
"""Submits blob of JSON data for each test suite."""
req = DatazillaRequest(
protocol='http',
host='host',
project='project',
oauth_key='key',
oauth_secret='secret',
machine_name='qm-pxp01',
os='linux',
os_version='Ubuntu 11.10',
platform='x86_64',
build_name='Firefox',
version='14.0a2',
revision='785345035a3b',
branch='Mozilla-Aurora',
id='20120228122102',
)
req.add_datazilla_result(
DatazillaResult(
{'suite1': {'test1': [1]}, 'suite2': {'test2': [2]}}
)
)
req.submit()
self.assertEqual(mock_send.call_count, 2)
data1 = mock_send.call_args_list[0][0][0]
data2 = mock_send.call_args_list[1][0][0]
results = sorted([data1.pop('results'), data2.pop('results')])
self.assertEqual(results, sorted([{'test1': [1]}, {'test2': [2]}]))
suites = sorted(
[data1['testrun'].pop('suite'), data2['testrun'].pop('suite')])
self.assertEqual(suites, sorted(['suite1', 'suite2']))
# aside from results and suite, datasets are the same
self.assertEqual(data1, data2)
self.assertEqual(
data1['test_build'],
{
'branch': 'Mozilla-Aurora',
'id': '20120228122102',
'name': 'Firefox',
'revision': '785345035a3b',
'version': '14.0a2',
},
)
self.assertEqual(
data1['test_machine'],
{
'name': 'qm-pxp01',
'os': 'linux',
'osversion': 'Ubuntu 11.10',
'platform': 'x86_64',
},
)
self.assertEqual(data1['testrun']['date'], req.test_date)
@patch("dzclient.client.oauth.generate_nonce")
@patch("dzclient.client.oauth.time.time")
@patch("dzclient.client.httplib.HTTPConnection")
def test_send(self,
mock_HTTPConnection, mock_time, mock_generate_nonce):
"""Can send data to the server."""
mock_time.return_value = 1342229050
mock_generate_nonce.return_value = "46810593"
protocol = 'http'
host = "datazilla.mozilla.org"
project = "project"
key = "oauth-key"
secret = "oauth-secret"
req = DatazillaRequest(protocol, host, project, key, secret)
mock_conn = mock_HTTPConnection.return_value
mock_request = mock_conn.request
mock_response = mock_conn.getresponse.return_value
response = req.send({"some": "data"})
self.assertEqual(mock_HTTPConnection.call_count, 1)
self.assertEqual(mock_HTTPConnection.call_args[0][0], host)
self.assertEqual(mock_response, response)
self.assertEqual(mock_request.call_count, 1)
method, path, data, header = mock_request.call_args[0]
self.assertEqual(method, "POST")
self.assertEqual(path, "/project/api/load_test")
self.assertEqual(data, 'oauth_body_hash=2jmj7l5rSw0yVb%2FvlWAYkK%2FYBwk%3D&oauth_nonce=46810593&oauth_timestamp=1342229050&oauth_consumer_key=oauth-key&oauth_signature_method=HMAC-SHA1&oauth_version=1.0&oauth_token=&user=project&oauth_signature=mKpovMfgWJqlcVKSdcTCbw4gfaM%3D&data=%257B%2522some%2522%253A%2520%2522data%2522%257D')
self.assertEqual(
header['Content-type'],
'application/x-www-form-urlencoded',
)
@patch("dzclient.client.httplib.HTTPConnection")
def test_send_without_oauth(self, mock_HTTPConnection):
"""Can send data to the server without oauth"""
protocol = 'http'
host = "datazilla.mozilla.org"
project = "project"
key = None
secret = None
req = DatazillaRequest(protocol, host, project, key, secret)
mock_conn = mock_HTTPConnection.return_value
mock_request = mock_conn.request
mock_response = mock_conn.getresponse.return_value
response = req.send({"some": "data"})
self.assertEqual(mock_HTTPConnection.call_count, 1)
self.assertEqual(mock_HTTPConnection.call_args[0][0], host)
self.assertEqual(mock_response, response)
self.assertEqual(mock_request.call_count, 1)
method, path, data, header = mock_request.call_args[0]
self.assertEqual(method, "POST")
self.assertEqual(path, "/project/api/load_test")
self.assertEqual(data, 'data=%257B%2522some%2522%253A%2520%2522data%2522%257D')
self.assertEqual(
header['Content-type'],
'application/x-www-form-urlencoded',
)
if __name__ == '__main__':
unittest.main()