This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
test_git.py
557 lines (436 loc) · 19 KB
/
test_git.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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# -*- coding: utf-8; -*-
import time, copy
import io, pytest
import ruamel.yaml as yaml
from pytest import mark
from pytest import fixture, yield_fixture
from textwrap import dedent
from functools import partial
from repo_fixture import repo_fixture
from utils import is_created_by_jenkinsautojobs
from jenkins_autojobs import git, main
from jenkins import Jenkins
#-----------------------------------------------------------------------------
# Fixtures and shortcuts.
cmd = partial(git._main, ['jenkins-makejobs-git'])
@yield_fixture(scope='module')
def repo():
r = repo_fixture('git')
yield r
r.clean()
@fixture(scope='module')
def jenkins():
print('Looking for a running Jenkins instance')
webapi = Jenkins('http://127.0.0.1:60888')
print('Removing all jobs ...')
for job in webapi.jobs:
webapi.job_delete(job.name)
print('Creating test jobs ...')
configxml = 'master-job-git-config.xml'
configxml = open(configxml).read().encode('utf8')
webapi.job_create('master-job-git', configxml)
return webapi
@fixture(scope='module')
def view(request, jenkins):
if jenkins.view_exists('Tests'):
jenkins.view_delete('Tests')
configxml = 'test-view-config.xml'
jenkins.view_create('Tests', open(configxml).read().encode('utf8'))
request.addfinalizer(lambda: jenkins.view_delete('Tests'))
return 'Tests'
@fixture(scope='function')
def config(jenkins, repo):
base = u'''
jenkins: %s
repo: %s
template: master-job-git
namesep: '-'
namefmt: '{shortref}'
overwrite: true
enable: 'sticky'
sanitize:
'@!?#&|\^_$%%*': '_'
substitute:
'@@JOB_NAME@@': '{shortref}'
ignore:
- 'refs/heads/feature/.*-nobuild'
refs:
- 'refs/heads/feature/(.*)'
- 'refs/heads/scratch/(.*)':
'namefmt': '{shortref}'
'''
base = dedent(base) % (jenkins.url, repo.url)
base = yaml.load(io.StringIO(base))
return base
@fixture(scope='function', autouse=True)
def cleanup(request, jenkins):
def finalize():
jobs = (job for job in jenkins.jobs if job.name != 'master-job-git')
for job in jobs:
jenkins.job_delete(job.name)
request.addfinalizer(finalize)
#-----------------------------------------------------------------------------
params = [
['feature/one/two', '{shortref}', '.', 'feature.one.two'],
['feature/one/two', 'test-{shortref}', '-', 'test-feature-one-two'],
['feature/one/two', 'test.{ref}', '-', 'test.refs-heads-feature-one-two'],
]
@mark.parametrize(['branch', 'namefmt', 'namesep', 'expected'], params)
def test_namefmt_namesep_global(config, jenkins, repo, branch, namefmt, namesep, expected):
config['namefmt'] = namefmt
config['namesep'] = namesep
with repo.branch(branch):
cmd(config)
assert jenkins.job_exists(expected)
#-----------------------------------------------------------------------------
params = [
['feature/one/two', '{shortref}', '.', 'feature.one.two'],
['feature/one/two', 'test-{shortref}', '-', 'test-feature-one-two'],
['feature/one/two', 'test.{ref}', '-', 'test.refs-heads-feature-one-two'],
]
@mark.parametrize(['branch', 'namefmt', 'namesep', 'expected'], params)
def test_namefmt_namesep_global(config, jenkins, repo, branch, namefmt, namesep, expected):
config['namefmt'] = namefmt
config['namesep'] = namesep
with repo.branch(branch):
cmd(config)
assert jenkins.job_exists(expected)
#-----------------------------------------------------------------------------
params = [
['scratch/one/two', '{shortref}', '.', 'scratch.one.two'],
['scratch/one/two', 'test.{ref}', '_', 'test.refs_heads_scratch_one_two'],
['scratch/one/two', 'test.{shortref}', '_', 'test.scratch_one_two'],
]
@mark.parametrize(['branch', 'namefmt', 'namesep', 'expected'], params)
def test_namefmt_namesep_inherit(config, jenkins, repo, branch, namefmt, namesep, expected):
config['refs'] = [{
'refs/heads/%s' % branch : {
'namesep': namesep,
'namefmt': namefmt,
}
}]
with repo.branch(branch):
cmd(config)
assert jenkins.job_exists(expected)
#-----------------------------------------------------------------------------
params = [
['feature/one@two', '{shortref}', {'#@': 'X'}, '.', 'feature.oneXtwo'],
['feature/one#two', 'test-{shortref}', {'#@': '_'}, '-', 'test-feature-one_two'],
['feature/one@#two','test.{ref}', {'#@': '_'}, '-', 'test.refs-heads-feature-one__two'],
]
@mark.parametrize(['branch', 'namefmt', 'sanitize', 'namesep', 'expected'], params)
def test_namefmt_sanitize_global(config, jenkins, repo, branch, namefmt, sanitize, namesep, expected):
config['namefmt'] = namefmt
config['namesep'] = namesep
config['sanitize'] = sanitize
with repo.branch(branch):
cmd(config)
assert jenkins.job_exists(expected)
#-----------------------------------------------------------------------------
params = [
['feature/one#two', '{shortref}', {'#': '_'}, '.', 'feature.one_two'],
['feature/one#two@three','test.{ref}', {'#@': '_'}, '-', 'test.refs-heads-feature-one_two_three'],
]
@mark.parametrize(['branch', 'namefmt', 'sanitize', 'namesep', 'expected'], params)
def test_namefmt_sanitize_inherit(config, jenkins, repo, branch, namefmt, sanitize, namesep, expected):
config['namefmt'] = namefmt
config['namesep'] = namesep
config['sanitize'] = {'#': 'X'}
config['refs'] = [{
'refs/heads/%s' % branch : {
'sanitize' : sanitize,
}}]
with repo.branch(branch):
cmd(config)
assert jenkins.job_exists(expected)
#-----------------------------------------------------------------------------
params = [
['scratch/one/two/three', 'refs/heads/scratch/(.*)/(.*)/', '{0}-wip-{1}', 'one-wip-two'],
['scratch/one/two/three', 'refs/heads/scratch/(.*)/.*/(.*)', '{1}.{0}', 'three.one'],
['wip/alpha/beta/gamma', 'refs/heads/wip/(.*)/.*/(.*)', 'test-{1}.{0}', 'test-gamma.alpha'],
]
@mark.parametrize(['branch', 'regex', 'namefmt', 'expected'], params)
def test_namefmt_groups_inherit(config, jenkins, repo, branch, regex, namefmt, expected):
config['namefmt'] = '.'
config['refs'] = [{ regex: {'namefmt' : namefmt, }}]
with repo.branch(branch):
cmd(config)
assert jenkins.job_exists(expected)
#-----------------------------------------------------------------------------
params = [
['feature/one/two', {'@@JOB_NAME@@' : '{shortref}'}, 'feature-one-two', 'feature-one-two'],
['feature/two/three', {'@@JOB_NAME@@' : 'one-{ref}'}, 'feature-two-three', 'one-refs-heads-feature-two-three'],
]
@mark.parametrize(['branch', 'sub', 'ejob', 'expected'], params)
def test_substitute(config, jenkins, repo, branch, sub, ejob, expected):
test_substitute.cleanup_jobs = [ejob]
config['substitute'] = sub
with repo.branch(branch):
cmd(config)
assert jenkins.job_exists(ejob)
assert expected in jenkins.job_info(ejob)['description'].splitlines()
def test_substitute_reposub(config, jenkins, repo):
config['substitute'] = {'@@JOB_NAME@@': '{repo-orig}'}
with repo.branch('feature/reposub'):
cmd(config)
assert jenkins.job_exists('feature-reposub')
assert config['repo'] in jenkins.job_info('feature-reposub')['description'].splitlines()
#-----------------------------------------------------------------------------
def test_substitute_groups(config, jenkins, repo):
config['refs'] = ['refs/heads/(?P<type>(?:feature|release))/(.*)/(.*)']
with repo.branch('feature/one/two.three') as name:
cmd(config)
assert jenkins.job_exists('feature-one-two.three')
config['substitute'] = {'@@JOB_NAME@@' : '{2}'}
config['refs'] = ['refs/heads/release-((?:(\d+\.){1,2})[1-9]+\d*)-(.*)']
with repo.branch('release-0.7.4-wheezy') as name:
cmd(config)
assert 'wheezy' in jenkins.job_info(name)['description'].splitlines()
config['refs'] = ['refs/heads/feature-(\d\d)-(?P<name>\w+)-(\d)']
config['substitute'] = {'@@JOB_NAME@@' : '{0}-{name}-{2}'}
with repo.branch('feature-55-random-1') as name:
cmd(config)
assert '55-random-1' in jenkins.job_info(name)['description'].splitlines()
#-----------------------------------------------------------------------------
params = [
['wip/one/two', ['wip/.*']],
['feature/zetta-nobuild', ['.*-nobuild']]
]
@mark.parametrize(['branch', 'ignores'], params)
def test_ignore(config, jenkins, repo, branch, ignores):
job = branch.replace('/', config['namesep'])
config['ignore'] = ignores
with repo.branch(branch):
cmd(config)
assert not jenkins.job_exists(job)
#-----------------------------------------------------------------------------
params = [
['feature/config-one', 'origin/refs/heads/feature/config-one', 'feature/config-one']
]
@mark.parametrize(['branch', 'name', 'local'], params)
def test_configxml_global(config, jenkins, repo, branch, name, local):
job = branch.replace('/', config['namesep'])
test_configxml_global.cleanup_jobs = [job]
with repo.branch(branch):
cmd(config)
assert jenkins.job_exists(job)
configxml = jenkins.job_config_etree(job)
scm_el = configxml.xpath('scm[@class="hudson.plugins.git.GitSCM"]')[0]
el = scm_el.xpath('//hudson.plugins.git.BranchSpec/name')[0]
assert el.text == local
assert scm_el.xpath('//localBranch')[0].text == local
#-----------------------------------------------------------------------------
def test_overwrite_global(config, jenkins, repo, capsys):
config['overwrite'] = False
config['namefmt'] = 'samename'
with repo.branch('feature/one'):
cmd(config)
assert jenkins.job_exists('samename')
with repo.branch('feature/one'):
cmd(config)
assert jenkins.job_exists('samename')
# :todo: find better way
out, err = capsys.readouterr()
assert 'create job' not in out
#-----------------------------------------------------------------------------
def test_enable_true(config, jenkins, repo,):
jenkins.job_disable('master-job-git')
config['enable'] = True
with repo.branch('feature/enable-true'):
cmd(config)
assert jenkins.job_exists('feature-enable-true')
#-----------------------------------------------------------------------------
def test_enable_false(config, jenkins, repo,):
jenkins.job_enable('master-job-git')
config['enable'] = False
with repo.branch('feature/enable-false'):
cmd(config)
assert jenkins.job_exists('feature-enable-false')
assert not jenkins.job_enabled('feature-enable-false')
#-----------------------------------------------------------------------------
def test_enable_template(config, jenkins, repo,):
config['enable'] = 'template'
jenkins.job_enable('master-job-git')
with repo.branch('feature/enable-template-one'):
cmd(config)
assert jenkins.job_exists('feature-enable-template-one')
assert jenkins.job_enabled('feature-enable-template-one')
jenkins.job_disable('master-job-git')
with repo.branch('feature/enable-template-two'):
cmd(config)
assert jenkins.job_exists('feature-enable-template-two')
assert not jenkins.job_enabled('feature-enable-template-two')
#-----------------------------------------------------------------------------
def test_enable_sticky(config, jenkins, repo):
config['enable'] = 'sticky'
config['overwrite'] = True
# First run inherits the enabled state of the template job.
jenkins.job_disable('master-job-git')
with repo.branch('feature/enable-sticky-one'):
cmd(config)
assert not jenkins.job_enabled('feature-enable-sticky-one')
# If child job is enabled, it will remain enabled.
jenkins.job_enable('feature-enable-sticky-one')
cmd(config)
assert jenkins.job_enabled('feature-enable-sticky-one')
jenkins.job_disable('feature-enable-sticky-one')
cmd(config)
assert not jenkins.job_enabled('feature-enable-sticky-one')
#-------------------------------------------------------------------------
jenkins.job_enable('master-job-git')
with repo.branch('feature/enable-sticky-two'):
cmd(config)
assert jenkins.job_enabled('feature-enable-sticky-two')
# If child job is disabled, it will remain disabled.
jenkins.job_disable('feature-enable-sticky-two')
cmd(config)
assert not jenkins.job_enabled('feature-enable-sticky-two')
#-----------------------------------------------------------------------------
def test_inheritance_order(config, jenkins, repo):
config['refs'] = [
{ 'refs/heads/feature/bravo/(.*)' : {'namefmt' : 'one-{shortref}'} },
{ 'refs/heads/feature/(.*)' : {'namefmt' : 'two-{shortref}'} },
]
with repo.branch('feature/bravo/four'):
cmd(config)
assert jenkins.job_exists('one-feature-bravo-four')
#-----------------------------------------------------------------------------
def test_missing_template(config, jenkins, repo):
config['template'] = 'does-not-exist'
with pytest.raises(SystemExit):
cmd(config)
#-----------------------------------------------------------------------------
@mark.parametrize('tag_method', ['element', 'description'])
def test_cleanup(config, jenkins, repo, tag_method):
config['cleanup'] = True
config['tag-method'] = tag_method
with repo.branches('feature/one', 'feature/two'):
cmd(config)
assert jenkins.job_exists('feature-one')
assert jenkins.job_exists('feature-two')
assert is_created_by_jenkinsautojobs(jenkins.job('feature-one'), tag_method)
with repo.branch('feature/one'):
cmd(config)
assert not jenkins.job_exists('feature-two')
#-----------------------------------------------------------------------------
@mark.parametrize('tag_method', ['element', 'description'])
def test_failing_git_cleanup(config, jenkins, repo, tag_method):
config['cleanup'] = True
config['tag-method'] = tag_method
with repo.branches('feature/one', 'feature/two'):
cmd(config)
assert jenkins.job_exists('feature-one')
assert jenkins.job_exists('feature-two')
assert is_created_by_jenkinsautojobs(jenkins.job('feature-one'), tag_method)
config['repo'] = '/tmp/should-never-exist-zxcv-123-zxcv-1asfmn'
with pytest.raises(SystemExit):
cmd(config)
# feature-{one,two} should not be removed if command fails
assert jenkins.job_exists('feature-one')
assert jenkins.job_exists('feature-two')
#-----------------------------------------------------------------------------
def test_tag_element(config, jenkins, repo):
config['tag'] = 'group1'
config['tag-method'] = 'element'
config['refs'] = [
{'refs/heads/feature/(.*)': {'namefmt': '{shortref}'}},
{'refs/heads/test': {'tag': 'group2'}},
]
with repo.branch('feature/one'):
cmd(config)
jobc = jenkins.job('feature-one').config
assert main.get_autojobs_tags(jobc, 'element') == ['group1']
assert '<tag>group1</tag>' in jobc
with repo.branch('test'):
cmd(config)
assert '<tag>group2</tag>' in jenkins.job('test').config
def test_tag_description(config, jenkins, repo):
config['tag'] = 'group1'
config['tag-method'] = 'description'
config['refs'] = [
{'refs/heads/feature/(.*)': {'namefmt': '{shortref}'}},
{'refs/heads/test': {'tag': 'group2'}},
]
with repo.branch('feature/one'):
cmd(config)
job = jenkins.job('feature-one')
desc = job.config_etree.xpath('/project/description/text()')[0]
assert '\n(created by jenkins-autojobs)' in desc
assert '\n(jenkins-autojobs-tag: group1)' in desc
assert main.get_autojobs_tags(job.config, 'description') == ['group1']
with repo.branch('test'):
cmd(config)
job = jenkins.job('test')
desc = job.config_etree.xpath('/project/description/text()')[0]
assert '\n(jenkins-autojobs-tag: group2)' in desc
assert main.get_autojobs_tags(job.config, 'description') == ['group2']
#-----------------------------------------------------------------------------
def test_cleanup_filter_regex(config, jenkins, repo):
config['cleanup'] = True
config['cleanup-filters'] = {
'jobs': ['.*one']
}
with repo.branches('feature/one', 'feature/one1', 'feature/two'):
cmd(copy.deepcopy(config))
with repo.branch('feature/one'):
cmd(copy.deepcopy(config))
assert not jenkins.job_exists('feature-one1')
assert jenkins.job_exists('feature-two')
def test_cleanup_filter_view(config, jenkins, view, repo):
config['cleanup'] = True
with repo.branches('feature/three'):
cmd(copy.deepcopy(config))
config['view'] = [view]
config['cleanup-filters'] = {
'views': [view]
}
with repo.branches('feature/one', 'feature/one1', 'feature/two'):
cmd(copy.deepcopy(config))
with repo.branch('feature/one'):
cmd(copy.deepcopy(config))
assert not jenkins.job_exists('feature-one1')
assert not jenkins.job_exists('feature-two')
assert jenkins.job_exists('feature-three')
#-----------------------------------------------------------------------------
@mark.parametrize('tag_method', ['element', 'description'])
def test_cleanup_tags(config, jenkins, repo, tag_method):
config['cleanup'] = 'group1'
config['tag-method'] = tag_method
config['refs'] = [
{'refs/heads/group1/(.*)': {'tag': 'group1'}},
{'refs/heads/group2/(.*)': {'tag': 'group2'}},
]
with repo.branches('group1/one', 'group1/two', 'group2/three'):
cmd(config)
assert jenkins.job_exists('group1-one')
assert jenkins.job_exists('group1-two')
assert jenkins.job_exists('group2-three')
cmd(config)
assert not jenkins.job_exists('group1-one')
assert not jenkins.job_exists('group1-two')
assert jenkins.job_exists('group2-three')
config['cleanup'] = 'group2'
with repo.branches('group1/one'):
cmd(config)
assert not jenkins.job_exists('group2-three')
#-----------------------------------------------------------------------------
def test_views(config, jenkins, view, repo):
config['namefmt'] = '{shortref}'
config['view'] = [view, 'All'] # All should be filtered out in main()
with repo.branches('feature/one'):
cmd(config)
assert '<string>feature-one</string>' in jenkins.view_config(view)
#-----------------------------------------------------------------------------
def test_views_nonexist(config, jenkins, repo):
config['namefmt'] = '{shortref}'
config['view'] = ['abc', 'zxc']
with pytest.raises(SystemExit):
cmd(config)
#-----------------------------------------------------------------------------
@mark.slow
def test_build_on_create(config, jenkins, repo):
config['build-on-create'] = True
with repo.branches('feature/one'):
cmd(config)
time.sleep(10)
assert len(jenkins.job('feature-one').builds) == 1