-
Notifications
You must be signed in to change notification settings - Fork 193
/
test_automatic_updates.py
218 lines (166 loc) · 7.67 KB
/
test_automatic_updates.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
# Copyright © 2019 Red Hat, Inc.
#
# This file is part of Bodhi.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""These are tests for the bodhi.server.consumers.automatic_updates module."""
from copy import deepcopy
from unittest import mock
import logging
from fedora_messaging.api import Message
import _pytest.logging
import pytest
from bodhi.server.consumers.automatic_updates import AutomaticUpdateHandler
from bodhi.server.exceptions import BodhiException
from bodhi.server.models import Build, Update, UpdateType, User
from bodhi.tests.server import base
if not hasattr(_pytest.logging.LogCaptureFixture, 'messages'):
# Monkey patch old versions of _pytest.logging.LogCaptureFixture
class MonkeyLogCaptureFixture(_pytest.logging.LogCaptureFixture):
"""Adds 'messages' property to LogCaptureFixture."""
@property
def messages(self):
"""Return a list of format-interpolated log messages."""
return [r.getMessage() for r in self.records]
_pytest.logging.LogCaptureFixture = MonkeyLogCaptureFixture
class TestAutomaticUpdateHandler(base.BasePyTestCase):
"""Test the automatic update handler."""
def setup_method(self, method):
"""Set up environment for each test."""
super().setup_method(method)
self.release = self.create_release('47', create_automatic_updates=True)
msg = {
'build_id': 442562,
'name': 'colord',
'tag_id': 214,
'instance': 's390',
'tag': 'f17-updates-candidate',
'user': 'sharkcz',
'version': '1.3.4',
'owner': 'sharkcz',
'release': '1.fc26',
}
mbody = {
'i': 628,
'timestamp': 1484692585,
'msg_id': '2017-821031da-be3a-4f4b-91df-0baa834ca8a4',
'crypto': 'x509',
'topic': 'org.fedoraproject.prod.buildsys.tag',
'signature': '100% real please trust me',
'msg': msg,
}
self.sample_message = Message(topic='', body=mbody)
self.sample_nvr = f"{msg['name']}-{msg['version']}-{msg['release']}"
self.db_factory = base.TransactionalSessionMaker(self.Session)
self.handler = AutomaticUpdateHandler(self.db_factory)
# Test the main code path.
def test_consume(self):
"""Assert that messages about tagged builds create an update."""
# process the message
self.handler(self.sample_message)
# check if the update exists...
update = self.db.query(Update).filter(
Update.builds.any(Build.nvr == self.sample_nvr)
).first()
# ...and some of its properties
assert update is not None
assert update.type == UpdateType.unspecified
expected_username = base.buildsys.DevBuildsys._build_data['owner_name']
assert update.user and update.user.name == expected_username
# The following tests cover lesser-travelled code paths.
@mock.patch('bodhi.server.consumers.automatic_updates.transactional_session_maker')
def test___init___without_db_factory(self, transactional_session_maker):
"""__init__() should create db_factory if missing."""
handler = AutomaticUpdateHandler()
assert handler.db_factory is transactional_session_maker.return_value
transactional_session_maker.assert_called_once_with()
# Test robustness: malformed messages, unknown koji builds, incomplete
# buildinfo, release missing from the DB
def test_missing_msg(self, caplog):
"""Test 'msg' missing from body."""
caplog.set_level(logging.DEBUG)
msg = deepcopy(self.sample_message)
del msg.body['msg']
self.handler(msg)
assert "Ignoring message without 'msg'." in caplog.messages
def test_missing_mandatory_elems(self):
"""Test tag message without mandatory elements."""
msg = deepcopy(self.sample_message)
for elem in ('build_id', 'name', 'version', 'release'):
del msg.body['msg'][elem]
with pytest.raises(BodhiException):
self.handler(msg)
def test_unknown_koji_build(self):
"""Test tag message about unknown koji build."""
msg = deepcopy(self.sample_message)
msg.body['msg']['release'] += '.youdontknowme'
with pytest.raises(BodhiException):
self.handler(msg)
def test_incomplete_koji_buildinfo_nvr(self):
"""Test koji returning incomplete buildinfo: no nvr."""
msg = deepcopy(self.sample_message)
msg.body['msg']['release'] += '.testmissingnvr'
with pytest.raises(BodhiException):
self.handler(msg)
def test_incomplete_koji_buildinfo_owner(self, caplog):
"""Test koji returning incomplete buildinfo: no owner."""
caplog.set_level(logging.DEBUG)
msg = deepcopy(self.sample_message)
sample_nvr = self.sample_nvr + '.noowner'
msg.body['msg']['release'] += '.noowner'
self.handler(msg)
assert(f"Koji build {sample_nvr} without owner name, creating update anonymously."
in caplog.messages)
def test_missing_user(self, caplog):
"""Test Koji build user missing from DB."""
caplog.set_level(logging.DEBUG)
expected_username = base.buildsys.DevBuildsys._build_data['owner_name']
# ensure user with expected name doesn't exist
self.db.query(User).filter_by(name=expected_username).delete()
self.db.flush()
self.handler(self.sample_message)
assert(f"Creating bodhi user for '{expected_username}'."
in caplog.messages)
def test_existing_user(self, caplog):
"""Test Koji build user existing in DB."""
caplog.set_level(logging.DEBUG)
expected_username = base.buildsys.DevBuildsys._build_data['owner_name']
# ensure user with expected name exists
user = self.db.query(User).filter_by(name=expected_username).first()
if not user:
user = User(name=expected_username)
self.db.add(user)
self.db.flush()
self.handler(self.sample_message)
assert(f"Creating bodhi user for '{expected_username}'."
not in caplog.messages)
# Test messages that should be ignored.
def test_ignored_tag(self, caplog):
"""Test messages re: tags not configured for automatic updates."""
caplog.set_level(logging.DEBUG)
msg = deepcopy(self.sample_message)
bogus_tag = 'thisisntthetagyourelookingfor'
msg.body['msg']['tag'] = bogus_tag
self.handler(msg)
assert any(x.startswith(f"Ignoring build being tagged into '{bogus_tag}'")
for x in caplog.messages)
def test_duplicate_message(self, caplog):
"""Assert that duplicate messages ignore existing build/update."""
caplog.set_level(logging.DEBUG)
self.handler(self.sample_message)
caplog.clear()
self.handler(self.sample_message)
assert (f"Build, active update for {self.sample_nvr} exists already, skipping."
in caplog.messages)