/
uplift_tests.py
365 lines (304 loc) · 12.7 KB
/
uplift_tests.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
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
import unittest
import os
import json
from mock import patch
import tempfile
import copy
import gaia_uplift.git as git
import gaia_uplift.bzapi as bzapi
import gaia_uplift.uplift as subject
import gaia_uplift.configuration as c
class BugSkipping(unittest.TestCase):
def setUp(self):
subject.skip_bugs_file = 'test.skip_bugs'
if os.path.exists(subject.skip_bugs_file):
os.unlink(subject.skip_bugs_file)
def test_add_int_bug_to_skip(self):
bugid = 12345
subject.skip_bug(bugid)
with open(subject.skip_bugs_file) as f:
self.assertEqual([bugid], json.load(f))
def test_add_str_bug_to_skip(self):
bugid = 12345
subject.skip_bug(str(bugid))
with open(subject.skip_bugs_file) as f:
self.assertEqual([bugid], json.load(f))
def test_add_bug_twice(self):
bugid = 12345
subject.skip_bug(bugid)
subject.skip_bug(bugid)
with open(subject.skip_bugs_file) as f:
self.assertEqual([bugid], json.load(f))
def test_is_skippable_not_skipable(self):
bugid = 12345
self.assertFalse(subject.is_skipable(bugid))
def test_is_skippable_skipable(self):
bugid = 12345
subject.skip_bug(12345)
self.assertTrue(subject.is_skipable(bugid))
class FindBugsTest(unittest.TestCase):
def test_find_bugs(self):
with patch('gaia_uplift.bzapi.search') as search, \
patch('gaia_uplift.bzapi.parse_bugzilla_query') as parse:
search.return_value = ['123456']
parse.return_value = ['my_butt']
actual = subject.find_bugs(['snow_storm'])
self.assertEqual(['123456'], actual)
parse.assert_called_once_with('snow_storm')
search.assert_called_once_with('my_butt')
def test_find_bugs_only_skipable(self):
with patch('gaia_uplift.bzapi.search') as search, \
patch('gaia_uplift.bzapi.parse_bugzilla_query') as parse, \
patch('gaia_uplift.uplift.is_skipable') as is_skipable:
search.return_value = ['123456']
parse.return_value = ['my_butt']
is_skipable.return_value = True
actual = subject.find_bugs(['snow_storm'])
self.assertEqual([], actual)
parse.assert_called_once_with('snow_storm')
is_skipable.assert_called_once_with('123456')
search.assert_called_once_with('my_butt')
class OrderCommits(unittest.TestCase):
def test_order_commits(self):
with patch('gaia_uplift.git.sort_commits') as sort:
expected = range(5)
requirements = dict([(x, {'commits': [x]}) for x in expected])
sort.return_value = expected
actual = subject.order_commits(None, requirements)
self.assertEqual(expected, actual)
sort.assert_called_once_with(None, expected, 'master')
def test_order_commits_no_commits(self):
with patch('gaia_uplift.git.sort_commits') as sort:
iterations = range(5)
expected = [x for x in iterations if x % 2 == 0]
requirements = dict([(x, {'commits': [x]}) if x % 2 == 0 else (x, {}) for x in iterations])
sort.return_value = expected
actual = subject.order_commits(None, requirements)
self.assertEqual(expected, actual)
sort.assert_called_once_with(None, expected, 'master')
class UpliftCommit(unittest.TestCase):
def test_success_two_branches(self):
with patch('gaia_uplift.git.cherry_pick') as cherry_pick:
branches = ['v1', 'v2']
cherry_pick.side_effect = ["%ssha" % x for x in branches]
actual = subject.uplift_commit(None, 'commit', branches)
expected = {
'success': {'v1': 'v1sha', 'v2': 'v2sha'},
'failure': []
}
self.assertEqual(expected, actual)
def test_one_fail_one_pass(self):
with patch('gaia_uplift.git.cherry_pick') as cherry_pick:
branches = ['v1', 'v2']
cherry_pick.side_effect = ['v1sha', git.GitError]
actual = subject.uplift_commit(None, 'commit', branches)
expected = {
'success': {'v1': 'v1sha'},
'failure': ['v2']
}
self.assertEqual(expected, actual)
def test_failure_two_branches(self):
with patch('gaia_uplift.git.cherry_pick') as cherry_pick:
branches = ['v1', 'v2']
cherry_pick.side_effect = [git.GitError, git.GitError]
actual = subject.uplift_commit(None, 'commit', branches)
expected = {
'success': {},
'failure': ['v1', 'v2']
}
self.assertEqual(expected, actual)
def test_noop(self):
with patch('gaia_uplift.git.cherry_pick') as cherry_pick:
commit = 'abc123'
cherry_pick.return_value = commit
actual = subject.uplift_commit(None, commit, ['v1'])
expected = {
'success': {'v1': commit},
'failure': []
}
self.assertEqual(expected, actual)
class Push(unittest.TestCase):
def test_success(self):
with patch('gaia_uplift.git.push') as push, \
patch('gaia_uplift.util.ask_yn') as ask_yn:
ask_yn.return_value = True
expected = {'url': None,
'branches': { 'master': ("a"*40, "b"*40) }
}
push.return_value = expected
actual = subject.push(None)
self.assertEqual(expected, actual)
def test_no_push(self):
with patch('gaia_uplift.git.push') as push, \
patch('gaia_uplift.util.ask_yn') as ask_yn:
preview = {'url': None,
'branches': { 'master': ("a"*40, "b"*40) }
}
push.return_value = preview
ask_yn.return_value = False
self.assertEqual(None, subject.push(None))
def test_abject_failure(self):
with patch('gaia_uplift.git.push') as push, \
patch('gaia_uplift.util.ask_yn') as ask_yn:
preview = {'url': None,
'branches': {'master': ("a"*40, "b"*40) }
}
ask_yn.return_value = False
push.side_effect = git.PushFailure
with self.assertRaises(git.PushFailure):
subject.push(None)
def test_recovery(self):
with patch('gaia_uplift.git.push') as push, \
patch('gaia_uplift.util.ask_yn') as ask_yn:
ask_yn.return_value = True
expected = {'url': None,
'branches': { 'master': ("a"*40, "b"*40) }
}
push.side_effect = [expected, git.PushFailure, expected]
actual = subject.push(None)
self.assertEqual(expected, actual)
class BuildUpliftRequirements(unittest.TestCase):
def setUp(self):
self.old_config = c.json_file
self.config = os.path.join(os.path.dirname(__file__), 'uplift_tests_config.json')
c.change_file(self.config)
def tearDown(self):
c.change_file(self.old_config)
def test_new(self):
old_rf = subject.requirements_file
subject.requirements_file = os.devnull
with patch('gaia_uplift.uplift.find_bugs') as find_bugs, \
patch('gaia_uplift.uplift.is_skipable') as is_skipable, \
patch('gaia_uplift.bzapi.fetch_complete_bug') as fetch_bug, \
patch('gaia_uplift.util.ask_yn') as ask_yn:
# This is basically a directory which we can use for the fake
# bug data fetching
bug_data = {
'1': {
'v1-status': 'fixed',
'v2-status': 'verified',
'v3-status': 'purplemonkeydishwasher',
'blocking': 'v1',
'summary': 'bug1'
},
'2': {
'v3-status': '---',
'blocking': 'v3',
'summary': 'bug2'
},
'3': {
'v3-status': '---',
'blocking': '-',
'summary': 'bug3'
},
'4': {
'v3-status': '---',
'summary': 'bug4',
'attachments': [{'flags': [{'name': 'approval-v3', 'status': '+'}]}]
},
'5': {
'v3-status': '---',
'summary': 'bug5',
'attachments': [{'flags': [{'name': 'approval-v3','status': '-'}]}]
},
}
def fetch_bugs_gen(x):
return bug_data[x]
# Fake returns
ask_yn.return_value = False
is_skipable.return_value = False
find_bugs.return_value = bug_data.keys()
fetch_bug.side_effect = fetch_bugs_gen
expected = {
'1': {
'needed_on': [u'v3'],
'already_fixed_on': [u'v1', u'v2'],
'summary': 'bug1'
},
'2': {
'needed_on': [u'v3'],
'already_fixed_on': [],
'summary': 'bug2'
},
'4': {
'needed_on': [u'v3'],
'already_fixed_on': [],
'summary': 'bug4'
},
}
actual = subject.build_uplift_requirements(None)
self.assertEqual(expected, actual)
subject.requirements_file = old_rf
def test_existing(self):
with patch('gaia_uplift.util.ask_yn') as ask_yn, \
patch('gaia_uplift.util.read_json') as read_json:
expected = 'lemonmeranguepie'
read_json.return_value = expected
ask_yn.return_value = True
actual = subject.build_uplift_requirements(None)
self.assertEqual(expected, actual)
class Uplift(unittest.TestCase):
def setUp(self):
self.old_config = c.json_file
self.config = os.path.join(os.path.dirname(__file__), 'uplift_tests_config.json')
c.change_file(self.config)
def tearDown(self):
c.change_file(self.old_config)
def test_success(self):
with patch('gaia_uplift.uplift.order_commits') as order_commits, \
patch('gaia_uplift.uplift.uplift_commit') as uplift_commit, \
patch('gaia_uplift.git.sort_commits') as sort_commits, \
patch('gaia_uplift.git.create_gaia') as create_gaia:
uplift_outcome = {
'success': {'v3': '321dcba'},
'failure': []
}
requirements = {
'1': {
'needed_on': [u'v3'],
'already_fixed_on': [],
'summary': "success",
'commits': ["abcd123"]
}
}
expected = copy.deepcopy(requirements)
expected['1']['uplift_status'] = {
'abcd123': uplift_outcome
}
expected['1']['flags_to_set'] = {
'v3-status': 'fixed'
}
# Set up mocks
uplift_commit.return_value = uplift_outcome
order_commits.return_value = ['abcd123']
sort_commits.return_value = ['abcd123']
actual = subject.uplift(None, None, requirements)
self.assertEqual(expected, actual)
def test_failure(self):
with patch('gaia_uplift.uplift.order_commits') as order_commits, \
patch('gaia_uplift.uplift.uplift_commit') as uplift_commit, \
patch('gaia_uplift.git.sort_commits') as sort_commits, \
patch('gaia_uplift.git.create_gaia') as create_gaia:
uplift_outcome = {
'success': {},
'failure': ['v3']
}
requirements = {
'1': {
'needed_on': [u'v3'],
'already_fixed_on': [],
'summary': "success",
'commits': ["abcd123"]
}
}
expected = copy.deepcopy(requirements)
expected['1']['uplift_status'] = {
'abcd123': uplift_outcome
}
expected['1']['flags_to_set'] = {}
# Set up mocks
uplift_commit.return_value = uplift_outcome
order_commits.return_value = ['abcd123']
sort_commits.return_value = ['abcd123']
actual = subject.uplift(None, None, requirements)
self.assertEqual(expected, actual)