-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_esss_jenkins.py
285 lines (227 loc) · 10.2 KB
/
test_esss_jenkins.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
import pytest
import requests
pytest_plugins = ["errbot.backends.test", "pytester"]
extra_plugin_dir = "."
@pytest.fixture
def testbot(testbot):
from errbot.backends.test import TestPerson
testbot.bot.sender = TestPerson("fry@localhost", nick="fry")
return testbot
@pytest.fixture(autouse=True)
def jenkins_plugin(testbot):
jenkins_plugin = testbot.bot.plugin_manager.get_plugin_obj_by_name("Jenkins")
jenkins_plugin.config = {
"JENKINS_URL": "https://my-server.com/jenkins",
"JENKINS_TOKEN": "jenkins-secret-token",
"JENKINS_USERNAME": "jenkins-user",
"ROCKETCHAT_USER": "rocketchat-user",
"ROCKETCHAT_PASSWORD": "rocketchat-secret-password",
"ROCKETCHAT_DOMAIN": "https://my-server.com/rocketchat",
}
return jenkins_plugin
def test_token(testbot):
testbot.push_message("!jenkins token")
response = testbot.pop_message()
assert "Jenkins API Token not configured" in response
assert "https://my-server.com/jenkins/user/fry/configure" in response
testbot.push_message("!jenkins token secret-token")
response = testbot.pop_message()
assert response == "Token saved."
testbot.push_message("!jenkins token")
response = testbot.pop_message()
assert response == "You API Token is: secret-token (user: fry)"
def test_build_alias(testbot):
from unittest.mock import patch
testbot.push_message("!buildalias")
response = testbot.pop_message()
assert (
"Pass alias name, some search keywords, and an optional set of parameters"
in response
)
testbot.push_message("!buildalias rr30l rocky30 linux --parameters=EXT=20")
response = testbot.pop_message()
assert "Alias registered: rr30l" in response
testbot.push_message("!buildalias")
response = testbot.pop_message()
assert "Existing aliases" in response
assert "rr30l" in response
testbot.push_message("!jenkins token secret-token")
response = testbot.pop_message()
assert response == "Token saved."
jenkins_bot = testbot.bot.plugin_manager.get_plugin_obj_by_name("Jenkins")
with patch.object(jenkins_bot, "_find_all_job_names_filtered") as job_names:
job_names.return_value = []
testbot.push_message("!build rr30l")
response = testbot.pop_message()
assert job_names.call_count == 1
assert "No job found with pattern: ['rocky30', 'linux']" == response
testbot.push_message("!build rr30l 6666")
response = testbot.pop_message()
assert job_names.call_count == 2
assert "No job found with pattern: ['rocky30', 'linux', '6666']" == response
job_names.return_value = ["job_1", "job_2"]
testbot.push_message("!build rr30l 6666")
response = testbot.pop_message()
assert job_names.call_count == 3
assert "Multiple jobs found with pattern" in response
job_names.return_value = ["job_1"]
with patch.object(jenkins_bot, "_post_jenkins_json_request") as post_request:
testbot.push_message("!build rr30l 6666")
response = testbot.pop_message()
assert job_names.call_count == 4
assert post_request.call_count == 1
assert post_request.call_args[0][0].endswith(
"buildWithParameters?{}".format("EXT=20")
)
assert post_request.call_args[0][1] == "fry"
assert "Triggered 1 jobs:" in response
def test_webhook(jenkins_plugin, mocker):
import rocketchat.api
mocker.patch.object(rocketchat.api.RocketChatAPI, "send_message", autospec=True)
class DummyRequest:
pass
request = DummyRequest()
request.params = {
"number": "2",
"job_name": "fett-master-linux64",
"timestamp": "1508516240981",
"builtOn": "dev-ubuntu16.04-linux-sv01-ci01",
"event": "jenkins.job.started",
"userId": "fry",
"url": "job/fett-master-linux64/2/",
}
jenkins_plugin.jenkins(request)
args, kwargs = rocketchat.api.RocketChatAPI.send_message.call_args
assert kwargs == {}
_, text, user = args
assert "Job Started" in text
assert (
"[fett-master-linux64](https://my-server.com/jenkins/job/fett-master-linux64/2/)"
in text
)
assert user == "@fry"
def test_find_string_basic():
from esss_jenkins import filter_jobs_by_find_string
assert filter_jobs_by_find_string(JOBS, "ASIM-501 app win64,linux64".split()) == [
"alfasim-fb-ASIM-501-network-refactorings-part1-app-win64",
"alfasim-fb-ASIM-501-network-refactorings-part1-app-linux64",
]
@pytest.mark.parametrize("tr", [str.lower, str.upper, lambda x: x])
def test_find_string_case_sensitive(tr):
from esss_jenkins import filter_jobs_by_find_string
assert filter_jobs_by_find_string(JOBS, tr("ASIM-501 app win64").split()) == [
"alfasim-fb-ASIM-501-network-refactorings-part1-app-win64"
]
assert filter_jobs_by_find_string(
JOBS, tr("ASIM-501 app win64,linux64").split()
) == [
"alfasim-fb-ASIM-501-network-refactorings-part1-app-win64",
"alfasim-fb-ASIM-501-network-refactorings-part1-app-linux64",
]
assert filter_jobs_by_find_string(JOBS, tr("ASIM-501 win64,linux64").split()) == [
"alfasim-fb-ASIM-501-network-refactorings-part1-app-win64",
"alfasim-fb-ASIM-501-network-refactorings-part1-app-linux64",
"alfasim-fb-ASIM-501-network-refactorings-part1-calc-linux64",
"alfasim-fb-ASIM-501-network-refactorings-part1-synthetic-linux64",
]
assert filter_jobs_by_find_string(JOBS, [tr("eden-win64-27")]) == [
"eden-fb-ASIM-483-remove-dummy-velocity-part5-win64-27",
"eden-win64-27",
]
assert filter_jobs_by_find_string(JOBS, [tr('"eden-win64-27"')]) == [
"eden-win64-27"
]
def test_find_string_long_glob():
from esss_jenkins import filter_jobs_by_find_string
assert filter_jobs_by_find_string(JOBS, '"*rb*kra*"'.split()) == [
"etk-rb-KRA-v2.5.0-win64-27",
"etk-rb-KRA-v2.5.0-win64-35",
]
def test_find_string_glob():
from esss_jenkins import filter_jobs_by_find_string
assert filter_jobs_by_find_string(JOBS, "network-refacto*".split()) == [
"alfasim-fb-ASIM-501-network-refactorings-part1-app-win64",
"alfasim-fb-ASIM-501-network-refactorings-part1-app-win64g",
"alfasim-fb-ASIM-501-network-refactorings-part1-app-linux64",
"alfasim-fb-ASIM-501-network-refactorings-part1-calc-linux64",
"alfasim-fb-ASIM-501-network-refactorings-part1-synthetic-linux64",
"alfasim-fb-ASIM-480-network-refactorings-part1-synthetic-linux64",
]
assert filter_jobs_by_find_string(
JOBS, "network-refacto* win64,linux*".split()
) == [
"alfasim-fb-ASIM-501-network-refactorings-part1-app-win64",
"alfasim-fb-ASIM-501-network-refactorings-part1-app-linux64",
"alfasim-fb-ASIM-501-network-refactorings-part1-calc-linux64",
"alfasim-fb-ASIM-501-network-refactorings-part1-synthetic-linux64",
"alfasim-fb-ASIM-480-network-refactorings-part1-synthetic-linux64",
]
assert filter_jobs_by_find_string(JOBS, "network-refacto* linux*".split()) == [
"alfasim-fb-ASIM-501-network-refactorings-part1-app-linux64",
"alfasim-fb-ASIM-501-network-refactorings-part1-calc-linux64",
"alfasim-fb-ASIM-501-network-refactorings-part1-synthetic-linux64",
"alfasim-fb-ASIM-480-network-refactorings-part1-synthetic-linux64",
]
assert (
filter_jobs_by_find_string(JOBS, "simbr network-refacto* win64,linux*".split())
== []
)
def test_bhist(jenkins_plugin, testbot, mocker, LineMatcher):
settings = jenkins_plugin.load_user_settings("fry")
assert settings["jobs"] == []
testbot.push_message("!bhist")
response = testbot.pop_message()
assert "You never ran anything" in response
settings["jobs"] = [dict(job_name="job-A"), dict(job_name="job-B")]
jenkins_plugin.save_user_settings("fry", settings)
mocker.patch.object(
jenkins_plugin, "_fetch_job_status", autospec=True, return_value="RUNNING"
)
testbot.push_message("!bhist")
response = testbot.pop_message()
matcher = LineMatcher(response.splitlines())
matcher.fnmatch_lines(
["Here you go:", " 0. * job-A*1. * job-B*", " To trigger builds*"]
)
settings = jenkins_plugin.load_user_settings("fry")
assert settings["last_job_listing"] == ["job-A", "job-B"]
@pytest.mark.parametrize(
"expected, return_value",
[
("SUCCESS", {"_class": "hudson.model.FreeStyleBuild", "result": "SUCCESS"}),
("FAILURE", {"_class": "hudson.model.FreeStyleBuild", "result": "FAILURE"}),
("ABORTED", {"_class": "hudson.model.FreeStyleBuild", "result": "ABORTED"}),
("UNSTABLE", {"_class": "hudson.model.FreeStyleBuild", "result": "UNSTABLE"}),
("RUNNING", {"_class": "hudson.model.FreeStyleBuild"}),
],
)
def test_fetch_job_status(jenkins_plugin, mocker, expected, return_value):
mocker.patch.object(
jenkins_plugin, "_get_jenkins_json_request", return_value=return_value
)
assert jenkins_plugin._fetch_job_status("dummy") == expected
def test_fetch_job_status_not_started(jenkins_plugin, mocker):
response_404 = requests.Response()
response_404.status_code = 404
mocker.patch.object(
jenkins_plugin,
"_get_jenkins_json_request",
side_effect=[
jenkins_plugin.ResponseError("Any message", response_404),
{"_class": "hudson.model.FreeStyleBuild"},
],
)
assert jenkins_plugin._fetch_job_status("dummy") == "NOT_STARTED"
JOBS = [
"alfasim-fb-ASIM-501-network-refactorings-part1-app-win64",
"alfasim-fb-ASIM-501-network-refactorings-part1-app-win64g",
"alfasim-fb-ASIM-501-network-refactorings-part1-app-linux64",
"alfasim-fb-ASIM-501-network-refactorings-part1-calc-linux64",
"alfasim-fb-ASIM-501-network-refactorings-part1-synthetic-linux64",
"alfasim-fb-ASIM-480-network-refactorings-part1-synthetic-linux64",
"eden-fb-ASIM-483-remove-dummy-velocity-part5-linux64-27",
"eden-fb-ASIM-483-remove-dummy-velocity-part5-win64-27",
"eden-win64-27",
"etk-rb-KRA-v2.5.0-win64-27",
"etk-rb-KRA-v2.5.0-win64-35",
]