This repository has been archived by the owner on Jan 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_cit.py
266 lines (214 loc) · 10.2 KB
/
test_cit.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
from __future__ import with_statement
import cit # must be imported first to install submodules on PYTHONPATH
from jenkinsapi.jenkins import Jenkins
import StringIO
import hashlib
import mock
import os
import pytest
import time
import xml.etree.ElementTree as ET
import yaml
import sys
JENKINS_URL = 'http://localhost:8080'
JOB_TEST_PREFIX = 'cit-test-job-'
#===================================================================================================
# jenkins
#===================================================================================================
@pytest.fixture
def tmp_job_name(request, tmpdir):
config = file(os.path.join(os.path.dirname(__file__), 'test_config.xml')).read()
jenkins = Jenkins(JENKINS_URL)
# create job using base config
hasher = hashlib.sha1(str(time.time()))
job_name = '%s%s' % (JOB_TEST_PREFIX, hasher.hexdigest())
job = jenkins.create_job(job_name, config)
job.update_config(config)
# configure local cit information using this job as base
cit_config = {
'jobs' : [{
'source-job' : job_name,
'feature-branch-job' : job_name + '-$name',
}],
}
cit_config_file = str(tmpdir.join('.cit.yaml'))
yaml.dump(cit_config, file(cit_config_file, 'w'))
def delete_test_jobs():
'''
finalizer for this fixture that removes left-over test jobs from the live jenkins server.
'''
jenkins = Jenkins(JENKINS_URL)
for name, job in jenkins.get_jobs():
if name.startswith(JOB_TEST_PREFIX):
jenkins.delete_job(name)
request.addfinalizer(delete_test_jobs)
return job_name
#===================================================================================================
# change_cwd
#===================================================================================================
@pytest.fixture
def change_cwd(tmpdir):
'''
creates a suitable source directory to ensure our configuration suite is being found and loaded
correctly.
:return: working directory, as a sub-directory of the given temp dir
'''
os.makedirs(str(tmpdir.join('.git')))
cwd = str(tmpdir.join('src', 'plk'))
os.makedirs(cwd)
os.chdir(cwd)
#===================================================================================================
# global_config_file
#===================================================================================================
@pytest.fixture
def global_config_file(tmpdir):
'''
fixture that initializes a config file in the given temp directory. Useful to test cit
commands when it has already been correctly configured.
'''
global_config_file = str(tmpdir.join('citconfig.yaml'))
global_config = {'jenkins' : {'url' : JENKINS_URL}}
yaml.dump(global_config, file(global_config_file, 'w'))
return global_config_file
#===================================================================================================
# TestFeatureBranchCommands
#===================================================================================================
class TestFeatureBranchCommands(object):
'''
Class that groups all tests for feature branch commands that also require a
jenkins instance to be executed.
'''
pytestmark = pytest.mark.skipif('not config.option.jenkins_available')
@pytest.mark.usefixtures('change_cwd')
@pytest.mark.parametrize('branch', ['new-feature', None])
def test_fb_add(self, tmp_job_name, global_config_file, branch):
'''
test "fb.add" command
:param branch:
parametrized to test adding passing a branch name in the command line and without
(which means "use current branch as branch name")
'''
with mock.patch('cit.get_git_branch', autospec=True) as mock_get_git_branch:
mock_get_git_branch.return_value = 'new-feature'
with mock.patch('cit.get_git_user', autospec=True) as mock_get_git_user:
mock_get_git_user.return_value = ('anonymous', 'anonymous@somewhere.com')
with mock.patch('cit.get_global_config_file', autospec=True) as mock_get_global_config_file:
mock_get_global_config_file.return_value = str(global_config_file)
argv = ['fb.add']
if branch:
argv.append(branch)
assert cit.app.main(argv) is None
branch = 'new-feature'
jenkins = Jenkins(JENKINS_URL)
new_job_name = tmp_job_name + '-' + branch
assert jenkins.has_job(new_job_name), "no job %s found. available: %s" % (new_job_name, jenkins.get_jobs_list())
config_xml = jenkins.get_job(new_job_name).get_config()
# ensure we configured branch correctly
config = ET.fromstring(config_xml)
branches_elements = list(config.findall('.//branches'))
assert len(branches_elements) == 1
branches_elem = branches_elements[0]
name_elem = branches_elem.findall('hudson.plugins.git.BranchSpec/name')[0]
assert name_elem.text == branch
# ensure we have updated the display name with the feature branch's name
display_name_elements = list(config.findall('.//displayName'))
assert len(display_name_elements) == 1
display_name_element = display_name_elements[0]
assert display_name_element.text == '%s SS win32' % branch
# ensure we have set the user email recipient
recipient_elements = list(config.findall('.//hudson.tasks.Mailer/recipients'))
assert len(recipient_elements) == 1
recipient_element = recipient_elements[0]
assert recipient_element.text == 'anonymous@somewhere.com'
# ensure we don't have build parameters anymore
params = list(config.findall('.//hudson.model.ParametersDefinitionProperty'))
assert len(params) == 0
# ensure no build is triggered after the job
build_triggers = list(config.findall('.//hudson.tasks.BuildTrigger'))
assert len(build_triggers) == 0
@pytest.mark.usefixtures('change_cwd')
@pytest.mark.parametrize('branch', ['new-feature', None])
def test_fb_rm(self, tmp_job_name, global_config_file, branch):
'''
test "fb.rm" command
:param branch:
parametrized to test removing passing a branch name in the command line and without
(which means "use current branch as branch name")
'''
jenkins = Jenkins(JENKINS_URL)
new_job_name = tmp_job_name + '-new-feature'
jenkins.copy_job(tmp_job_name, new_job_name)
jenkins = Jenkins(JENKINS_URL)
assert jenkins.has_job(new_job_name), "no job %s found. available: %s" % (new_job_name, jenkins.get_jobs_list())
with mock.patch('cit.get_git_branch', autospec=True) as mock_get_git_branch:
mock_get_git_branch.return_value = 'new-feature'
with mock.patch('cit.get_global_config_file', autospec=True) as mock_get_global_config_file:
mock_get_global_config_file.return_value = str(global_config_file)
argv = ['fb.rm']
if branch:
argv.append(branch)
assert cit.app.main(argv) is None
jenkins = Jenkins(JENKINS_URL)
assert not jenkins.has_job(new_job_name), "job %s found! available: %s" % (new_job_name, jenkins.get_jobs_list())
#===================================================================================================
# test_cit_init
#===================================================================================================
@pytest.mark.usefixtures('change_cwd')
def test_cit_init(tmpdir):
input_lines = [
'project_win32',
'project_$name_win32',
'project_win64',
'project_$name_win64',
'',
]
stdin = StringIO.StringIO('\n'.join(input_lines))
try:
sys.stdin = stdin
assert cit.app.main(['fb.init']) is None
finally:
sys.stdin = sys.__stdin__
cit_file = tmpdir.join('.cit.yaml')
assert cit_file.ensure()
contents = cit_file.read()
obtained = yaml.load(contents)
expected = {
'jobs' : [
{
'source-job': 'project_win32',
'feature-branch-job' : 'project_$name_win32',
},
{
'source-job': 'project_win64',
'feature-branch-job' : 'project_$name_win64',
},
]
}
assert obtained == expected
#===================================================================================================
# test_cit_install
#===================================================================================================
@pytest.mark.usefixtures('change_cwd')
def test_cit_install(tmpdir):
global_config_file = tmpdir.join('citconfig.yaml')
with mock.patch('cit.get_global_config_file', autospec=True) as mock_get_global_config_file:
mock_get_global_config_file.return_value = str(global_config_file)
input_lines = [
'localhost:8080',
'',
]
stdin = StringIO.StringIO('\n'.join(input_lines))
sys.stdin = stdin
try:
assert cit.app.main(['install']) is None
finally:
sys.stdin = sys.__stdin__
assert global_config_file.ensure()
contents = global_config_file.read()
obtained = yaml.load(contents)
assert obtained == {'jenkins' : {'url' : 'http://localhost:8080'}}
#===================================================================================================
# main
#===================================================================================================
if __name__ == '__main__':
pytest.main()