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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
import mock
import unittest
from jenkinsapi.plugins import Plugins
from jenkinsapi.utils.requester import Requester
from jenkinsapi.jenkins import Jenkins, JenkinsBase, Job
from jenkinsapi.custom_exceptions import JenkinsAPIException, UnknownJob, BadURL
class TestJenkins(unittest.TestCase):
DATA = {}
@mock.patch.object(Jenkins, '_poll')
def setUp(self, _poll):
_poll.return_value = self.DATA
self.J = Jenkins('http://localhost:8080',
username='foouser', password='foopassword')
@mock.patch.object(Jenkins, '_poll')
def test_clone(self, _poll):
_poll.return_value = self.DATA
JJ = self.J._clone()
self.assertNotEquals(id(JJ), id(self.J))
self.assertEquals(JJ, self.J)
def test_stored_passwords(self):
self.assertEquals(self.J.requester.password, 'foopassword')
self.assertEquals(self.J.requester.username, 'foouser')
@mock.patch.object(Jenkins, '_poll')
def test_reload(self, _poll):
mock_requester = Requester(username='foouser', password='foopassword')
mock_requester.get_url = mock.MagicMock(return_value='')
J = Jenkins('http://localhost:8080/',
username='foouser', password='foopassword',
requester=mock_requester)
J.poll()
@mock.patch.object(JenkinsBase, '_poll')
@mock.patch.object(Jenkins, '_poll')
@mock.patch.object(Job, '_poll')
def test_get_jobs(self, _base_poll, _poll, _job_poll):
_poll.return_value = {
'jobs': [
{'name': 'job_one', 'url': 'http://localhost:8080/job_one', 'color': 'blue'},
{'name': 'job_two', 'url': 'http://localhost:8080/job_two', 'color': 'blue'},
]
}
_base_poll.return_value = _poll.return_value
_job_poll.return_value = {}
J = Jenkins('http://localhost:8080/',
username='foouser', password='foopassword')
for idx, (job_name, job) in enumerate(J.get_jobs()):
self.assertEquals(job_name, _poll.return_value['jobs'][idx]['name'])
self.assertTrue(isinstance(job, Job))
self.assertEquals(job.name, _poll.return_value['jobs'][idx]['name'])
self.assertEquals(job.baseurl, _poll.return_value['jobs'][idx]['url'])
@mock.patch.object(JenkinsBase, '_poll')
@mock.patch.object(Jenkins, '_poll')
@mock.patch.object(Job, '_poll')
def test_lazy_loading(self, _base_poll, _poll, _job_poll):
_poll.return_value = {
'jobs': [
{'name': 'job_one', 'url': 'http://localhost:8080/job_one', 'color': 'blue'},
{'name': 'job_two', 'url': 'http://localhost:8080/job_two', 'color': 'blue'},
]
}
_base_poll.return_value = _poll.return_value
_job_poll.return_value = {}
J = Jenkins('http://localhost:8080/',
username='foouser', password='foopassword', lazy=True)
self.assertEquals(J._data, None)
for idx, (job_name, job) in enumerate(J.get_jobs()):
self.assertEquals(job_name, _poll.return_value['jobs'][idx]['name'])
self.assertTrue(isinstance(job, Job))
self.assertEquals(job.name, _poll.return_value['jobs'][idx]['name'])
self.assertEquals(job.baseurl, _poll.return_value['jobs'][idx]['url'])
@mock.patch.object(JenkinsBase, '_poll')
@mock.patch.object(Jenkins, '_poll')
@mock.patch.object(Job, '_poll')
def test_get_jobs_info(self, _base_poll, _poll, _job_poll):
_poll.return_value = {
'jobs': [
{'name': 'job_one', 'url': 'http://localhost:8080/job_one', 'color': 'blue'},
{'name': 'job_two', 'url': 'http://localhost:8080/job_two', 'color': 'blue'},
]
}
_base_poll.return_value = _poll.return_value
_job_poll.return_value = {}
J = Jenkins('http://localhost:8080/',
username='foouser', password='foopassword')
for idx, (url, job_name) in enumerate(J.get_jobs_info()):
self.assertEquals(job_name, _poll.return_value['jobs'][idx]['name'])
self.assertEquals(url, _poll.return_value['jobs'][idx]['url'])
@mock.patch.object(JenkinsBase, '_poll')
@mock.patch.object(Jenkins, '_poll')
@mock.patch.object(Job, '_poll')
def test_get_jobs_list(self, _base_poll, _poll, _job_poll):
_poll.return_value = {
'jobs': [
{'name': 'job_one', 'url': 'http://localhost:8080/job_one', 'color': 'blue'},
{'name': 'job_two', 'url': 'http://localhost:8080/job_two', 'color': 'blue'},
]
}
_base_poll.return_value = _poll.return_value
_job_poll.return_value = {}
J = Jenkins('http://localhost:8080/',
username='foouser', password='foopassword')
for idx, job_name in enumerate(J.get_jobs_list()):
self.assertEquals(job_name, _poll.return_value['jobs'][idx]['name'])
@mock.patch.object(JenkinsBase, '_poll')
@mock.patch.object(Jenkins, '_poll')
@mock.patch.object(Job, '_poll')
def test_get_job(self, _base_poll, _poll, _job_poll):
_poll.return_value = {
'jobs': [
{'name': 'job_one', 'url': 'http://localhost:8080/job_one', 'color': 'blue'},
{'name': 'job_two', 'url': 'http://localhost:8080/job_two', 'color': 'blue'},
]
}
_base_poll.return_value = _poll.return_value
_job_poll.return_value = {}
J = Jenkins('http://localhost:8080/',
username='foouser', password='foopassword')
job = J.get_job('job_one')
self.assertTrue(isinstance(job, Job))
self.assertEquals(job.name, _poll.return_value['jobs'][0]['name'])
self.assertEquals(job.baseurl, _poll.return_value['jobs'][0]['url'])
@mock.patch.object(JenkinsBase, '_poll')
@mock.patch.object(Jenkins, '_poll')
@mock.patch.object(Job, '_poll')
def test_get_job_that_does_not_exist(self, _base_poll, _poll, _job_poll):
_poll.return_value = {
'jobs': [
{'name': 'job_one', 'url': 'http://localhost:8080/job_one', 'color': 'blue'},
{'name': 'job_two', 'url': 'http://localhost:8080/job_two', 'color': 'blue'},
]
}
_base_poll.return_value = _poll.return_value
_job_poll.return_value = {}
J = Jenkins('http://localhost:8080/',
username='foouser', password='foopassword')
with self.assertRaises(UnknownJob):
job = J.get_job('job_three')
@mock.patch.object(JenkinsBase, '_poll')
@mock.patch.object(Jenkins, '_poll')
@mock.patch.object(Job, '_poll')
def test_has_job(self, _base_poll, _poll, _job_poll):
_poll.return_value = {
'jobs': [
{'name': 'job_one', 'url': 'http://localhost:8080/job_one', 'color': 'blue'},
{'name': 'job_two', 'url': 'http://localhost:8080/job_two', 'color': 'blue'},
]
}
_base_poll.return_value = _poll.return_value
_job_poll.return_value = {}
J = Jenkins('http://localhost:8080/',
username='foouser', password='foopassword')
job = J.has_job('job_one')
self.assertTrue(job)
@mock.patch.object(JenkinsBase, '_poll')
@mock.patch.object(Jenkins, '_poll')
@mock.patch.object(Job, '_poll')
def test_has_no_job(self, _base_poll, _poll, _job_poll):
_poll.return_value = {
'jobs': [
{'name': 'job_one', 'url': 'http://localhost:8080/job_one', 'color': 'blue'},
{'name': 'job_two', 'url': 'http://localhost:8080/job_two', 'color': 'blue'},
]
}
_base_poll.return_value = _poll.return_value
_job_poll.return_value = {}
J = Jenkins('http://localhost:8080/',
username='foouser', password='foopassword')
job = J.has_job('inexistant_job')
self.assertFalse(job)
@mock.patch.object(JenkinsBase, '_poll')
@mock.patch.object(Jenkins, '_poll')
@mock.patch.object(Job, '_poll')
def test_create_dup_job(self, _base_poll, _poll, _job_poll):
_poll.return_value = {
'jobs': [
{'name': 'job_one', 'url': 'http://localhost:8080/job_one', 'color': 'blue'},
{'name': 'job_two', 'url': 'http://localhost:8080/job_two', 'color': 'blue'},
]
}
_base_poll.return_value = _poll.return_value
_job_poll.return_value = {}
J = Jenkins('http://localhost:8080/',
username='foouser', password='foopassword')
job = J.create_job('job_one', None)
self.assertTrue(isinstance(job, Job))
self.assertTrue(job.baseurl == 'http://localhost:8080/job_one')
self.assertTrue(job.name == 'job_one')
# Here we're going to test function, which is going to modify
# Jenkins internal data. It calls for data once to check
# if job already there, then calls again to see if job hs been created.
# So we need to create mock function, which
# will return different value per each call
# Define what we will return
create_job_returns = [
# This will be returned when job is not yet created
{
'jobs': [
{'name': 'job_one', 'url': 'http://localhost:8081/job_one', 'color': 'blue'},
{'name': 'job_one', 'url': 'http://localhost:8080/job_one', 'color': 'blue'},
]
},
# This to simulate that the job has been created
{
'jobs': [
{'name': 'job_one', 'url': 'http://localhost:8080/job_one', 'color': 'blue'},
{'name': 'job_two', 'url': 'http://localhost:8080/job_two', 'color': 'blue'},
{'name': 'job_new', 'url': 'http://localhost:8080/job_new', 'color': 'blue'},
]
}
]
# Mock function
def second_call_poll():
return TestJenkins.create_job_returns.pop(0)
# Patch Jenkins with mock function
@mock.patch.object(Jenkins, '_poll', side_effect=second_call_poll)
@mock.patch.object(Job, '_poll')
def test_create_new_job(self, _poll, _job_poll):
_job_poll.return_value = {}
mock_requester = Requester(username='foouser', password='foopassword')
mock_requester.post_xml_and_confirm_status = mock.MagicMock(return_value='')
J = Jenkins('http://localhost:8080/',
username='foouser', password='foopassword',
requester=mock_requester)
job = J.create_job('job_new', None)
self.assertTrue(isinstance(job, Job))
self.assertTrue(job.baseurl == 'http://localhost:8080/job_new')
self.assertTrue(job.name == 'job_new')
@mock.patch.object(JenkinsBase, '_poll')
@mock.patch.object(Jenkins, '_poll')
@mock.patch.object(Job, '_poll')
def test_create_new_job_fail(self, _base_poll, _poll, _job_poll):
_job_poll.return_value = {}
_poll.return_value = {
'jobs': [
{'name': 'job_one', 'url': 'http://localhost:8080/job_one', 'color': 'blue'},
{'name': 'job_one', 'url': 'http://localhost:8080/job_one', 'color': 'blue'},
]
}
_base_poll.return_value = _poll.return_value
mock_requester = Requester(username='foouser', password='foopassword')
mock_requester.post_xml_and_confirm_status = mock.MagicMock(return_value='')
J = Jenkins('http://localhost:8080/',
username='foouser', password='foopassword',
requester=mock_requester)
with self.assertRaises(JenkinsAPIException) as ar:
J.create_job('job_new', None)
self.assertEquals(str(ar.exception), 'Cannot create job job_new')
@mock.patch.object(JenkinsBase, '_poll')
@mock.patch.object(Jenkins, '_poll')
@mock.patch.object(Job, '_poll')
def test_get_jenkins_obj_from_url(self, _base_poll, _poll, _job_poll):
_job_poll.return_value = {}
_poll.return_value = {
'jobs': [
{'name': 'job_one', 'url': 'http://localhost:8080/job_one', 'color': 'blue'},
{'name': 'job_one', 'url': 'http://localhost:8080/job_one', 'color': 'blue'},
]
}
_base_poll.return_value = _poll.return_value
mock_requester = Requester(username='foouser', password='foopassword')
mock_requester.post_xml_and_confirm_status = mock.MagicMock(return_value='')
J = Jenkins('http://localhost:8080/',
username='foouser', password='foopassword',
requester=mock_requester)
new_jenkins = J.get_jenkins_obj_from_url('http://localhost:8080/')
self.assertEquals(new_jenkins, J)
new_jenkins = J.get_jenkins_obj_from_url('http://localhost:8080/foo')
self.assertNotEquals(new_jenkins, J)
@mock.patch.object(JenkinsBase, '_poll')
@mock.patch.object(Jenkins, '_poll')
@mock.patch.object(Job, '_poll')
def test_get_jenkins_obj(self, _base_poll, _poll, _job_poll):
_job_poll.return_value = {}
_poll.return_value = {
'jobs': [
{'name': 'job_one', 'url': 'http://localhost:8080/job_one', 'color': 'blue'},
{'name': 'job_one', 'url': 'http://localhost:8080/job_one', 'color': 'blue'},
]
}
_base_poll.return_value = _poll.return_value
mock_requester = Requester(username='foouser', password='foopassword')
mock_requester.post_xml_and_confirm_status = mock.MagicMock(return_value='')
J = Jenkins('http://localhost:8080/',
username='foouser', password='foopassword',
requester=mock_requester)
new_jenkins = J.get_jenkins_obj()
self.assertEquals(new_jenkins, J)
@mock.patch.object(JenkinsBase, '_poll')
@mock.patch.object(Jenkins, '_poll')
def test_get_version(self, _base_poll, _poll):
class MockResponse(object):
def __init__(self):
self.headers = {}
self.headers['X-Jenkins'] = '1.542'
mock_requester = Requester(username='foouser', password='foopassword')
mock_requester.get_and_confirm_status = mock.MagicMock(return_value=MockResponse())
J = Jenkins('http://localhost:8080/',
username='foouser', password='foopassword',
requester=mock_requester)
self.assertEquals('1.542', J.version)
@mock.patch.object(JenkinsBase, '_poll')
@mock.patch.object(Jenkins, '_poll')
def test_get_version_nonexistent(self, _base_poll, _poll):
class MockResponse(object):
def __init__(self):
self.headers = {}
base_url = 'http://localhost:8080'
mock_requester = Requester(username='foouser', password='foopassword')
mock_requester.get_and_confirm_status = mock.MagicMock(return_value=MockResponse())
J = Jenkins(base_url,
username='foouser', password='foopassword',
requester=mock_requester)
self.assertEquals('0.0', J.version)
@mock.patch.object(JenkinsBase, 'get_data')
def test_get_master_data(self, _base_poll):
base_url = 'http://localhost:808'
_base_poll.return_value = {
"busyExecutors" : 59,
"totalExecutors" : 75
}
j = Jenkins(base_url,
username='foouser', password='foopassword')
data = j.get_master_data()
self.assertEquals(data['busyExecutors'], 59)
self.assertEquals(data['totalExecutors'], 75)
class TestJenkinsURLs(unittest.TestCase):
@mock.patch.object(Jenkins, '_poll')
def testNoSlash(self, _poll):
_poll.return_value = {}
J = Jenkins('http://localhost:8080',
username='foouser', password='foopassword')
self.assertEquals(J.get_create_url(), 'http://localhost:8080/createItem')
@mock.patch.object(Jenkins, '_poll')
def testWithSlash(self, _poll):
_poll.return_value = {}
J = Jenkins('http://localhost:8080/',
username='foouser', password='foopassword')
self.assertEquals(J.get_create_url(), 'http://localhost:8080/createItem')
@mock.patch.object(Jenkins, '_poll')
@mock.patch.object(Plugins, '_poll')
def test_has_plugin(self, _p_poll, _poll):
_poll.return_value = {}
_p_poll.return_value = {
'plugins': [
{
'deleted': False, 'hasUpdate': True, 'downgradable': False,
'dependencies': [{}, {}, {}, {}],
'longName': 'Jenkins Subversion Plug-in', 'active': True,
'shortName': 'subversion', 'backupVersion': None,
'url': 'http://wiki.jenkins-ci.org/display/JENKINS/Subversion+Plugin',
'enabled': True, 'pinned': False, 'version': '1.45',
'supportsDynamicLoad': 'MAYBE', 'bundled': True
}
]
}
J = Jenkins('http://localhost:8080/',
username='foouser', password='foopassword')
self.assertTrue(J.has_plugin('subversion'))
if __name__ == '__main__':
unittest.main()
|