This code is duplicated between two methods (and previously was duplicated between three). Let's share it. Signed-off-by: Adam Williamson <awilliam@redhat.com>
236 lines
6.6 KiB
Python
236 lines
6.6 KiB
Python
"""
|
|
Unit tests for `toddlers.utils.bodhi`.
|
|
"""
|
|
|
|
from unittest.mock import Mock
|
|
|
|
import pytest
|
|
|
|
from toddlers.exceptions import BodhiError
|
|
import toddlers.utils.bodhi as bodhi
|
|
|
|
|
|
class TestBodhiSetPagure:
|
|
"""
|
|
Test class for `toddlers.utils.bodhi.set_bodhi` function.
|
|
"""
|
|
|
|
def test_set_bodhi(self):
|
|
"""
|
|
Test initialization of bodhi module.
|
|
"""
|
|
config = {
|
|
"bodhi_url": "https://bodhi.fedoraproject.org",
|
|
}
|
|
bodhi_obj = bodhi.set_bodhi(config)
|
|
|
|
assert bodhi_obj._bodhi_url == config.get("bodhi_url")
|
|
assert bodhi_obj._requests_session
|
|
|
|
def test_set_bodhi_no_bodhi_url(self):
|
|
"""
|
|
Test initialization of bodhi module without required config value.
|
|
"""
|
|
with pytest.raises(
|
|
ValueError, match=r"No bodhi_url found in the configuration file"
|
|
):
|
|
bodhi.set_bodhi({})
|
|
|
|
|
|
class TestBodhiGetActiveBranches:
|
|
"""
|
|
Test class for `toddlers.utils.bodhi.Bodhi.get_active_branches` method.
|
|
"""
|
|
|
|
def setup_method(self):
|
|
"""
|
|
Setup method for the test class.
|
|
"""
|
|
config = {
|
|
"bodhi_url": "https://bodhi.fedoraproject.org",
|
|
}
|
|
self.bodhi = bodhi.set_bodhi(config)
|
|
self.bodhi._requests_session = Mock()
|
|
|
|
def test_get_active_branches(self):
|
|
"""
|
|
Assert that active branches from bodhi are retrieved correctly.
|
|
"""
|
|
response_mock = Mock()
|
|
response_mock.status_code = 200
|
|
branch = "f40"
|
|
data = {"releases": [{"branch": branch}]}
|
|
response_mock.json.return_value = data
|
|
|
|
self.bodhi._requests_session.get.return_value = response_mock
|
|
|
|
result = self.bodhi.get_active_branches()
|
|
|
|
self.bodhi._requests_session.get.assert_called_once()
|
|
|
|
assert result == [branch]
|
|
|
|
def test_get_active_branches_malformed_data(self):
|
|
"""
|
|
Assert that retrieving malformed json from bodhi is handled correctly.
|
|
"""
|
|
response_mock = Mock()
|
|
response_mock.status_code = 200
|
|
data = {}
|
|
response_mock.json.return_value = data
|
|
|
|
self.bodhi._requests_session.get.return_value = response_mock
|
|
|
|
expected_error = "Expected key not found in bodhi response."
|
|
|
|
with pytest.raises(BodhiError, match=expected_error):
|
|
self.bodhi.get_active_branches()
|
|
|
|
self.bodhi._requests_session.get.assert_called_once()
|
|
|
|
def test_get_active_branches_not_ok(self):
|
|
"""
|
|
Assert that failing to retrieve active branches from bodhi is handled correctly.
|
|
"""
|
|
response_mock = Mock()
|
|
response_mock.status_code = 500
|
|
|
|
self.bodhi._requests_session.get.return_value = response_mock
|
|
|
|
expected_error = "Couldn't retrieve active branches."
|
|
|
|
with pytest.raises(BodhiError, match=expected_error):
|
|
self.bodhi.get_active_branches()
|
|
|
|
self.bodhi._requests_session.get.assert_called_once()
|
|
|
|
|
|
class TestBodhiGetBranchInfo:
|
|
"""
|
|
Test class for `toddlers.utils.bodhi.Bodhi.get_branch_info` method
|
|
and methods that use the info.
|
|
"""
|
|
|
|
def setup_method(self):
|
|
"""
|
|
Setup method for the test class.
|
|
"""
|
|
config = {
|
|
"bodhi_url": "https://bodhi.fedoraproject.org",
|
|
}
|
|
self.bodhi = bodhi.set_bodhi(config)
|
|
self.bodhi._requests_session = Mock()
|
|
|
|
def test_get_branch_info(self):
|
|
"""
|
|
Assert that branch info isretrieved correctly from Bodhi.
|
|
"""
|
|
response_mock = Mock()
|
|
response_mock.status_code = 200
|
|
info = {"name": "F42", "long_name": "Fedora 42", "version": "42", "eol": None}
|
|
response_mock.json.return_value = info
|
|
|
|
self.bodhi._requests_session.get.return_value = response_mock
|
|
|
|
result = self.bodhi.get_branch_info("f42")
|
|
|
|
self.bodhi._requests_session.get.assert_called_once()
|
|
|
|
assert result == info
|
|
|
|
def test_get_branch_info_not_ok(self):
|
|
"""
|
|
Assert that failing to retrieve branch info from bodhi is handled correctly.
|
|
"""
|
|
response_mock = Mock()
|
|
response_mock.status_code = 500
|
|
response_mock.headers = {"content-type": "application/json"}
|
|
|
|
self.bodhi._requests_session.get.return_value = response_mock
|
|
|
|
expected_error = "Couldn't get branch info for f41."
|
|
|
|
with pytest.raises(BodhiError, match=expected_error):
|
|
self.bodhi.get_branch_info("f41")
|
|
|
|
self.bodhi._requests_session.get.assert_called_once()
|
|
|
|
@pytest.mark.parametrize(
|
|
("state", "expected"),
|
|
[
|
|
("disabled", False),
|
|
("pending", False),
|
|
("frozen", False),
|
|
("current", True),
|
|
("archived", True),
|
|
],
|
|
)
|
|
def test_is_branch_stable(self, state, expected):
|
|
get_branchinfo_mock = Mock()
|
|
get_branchinfo_mock.return_value = {
|
|
"name": "F42",
|
|
"long_name": "Fedora 42",
|
|
"version": "42",
|
|
"state": state,
|
|
}
|
|
|
|
self.bodhi.get_branch_info = get_branchinfo_mock
|
|
|
|
result = self.bodhi.is_branch_stable("f42")
|
|
|
|
assert result is expected
|
|
|
|
def test_is_branch_stable_defaults(self):
|
|
get_branchinfo_mock = Mock()
|
|
get_branchinfo_mock.return_value = {}
|
|
|
|
self.bodhi.get_branch_info = get_branchinfo_mock
|
|
|
|
result = self.bodhi.is_branch_stable("f41")
|
|
|
|
assert result is True
|
|
|
|
get_branchinfo_mock.side_effect = BodhiError("Couldn't retrieve branch info.")
|
|
result = self.bodhi.is_branch_stable("f41")
|
|
|
|
assert result is True
|
|
|
|
@pytest.mark.parametrize(
|
|
("state", "expected"),
|
|
[
|
|
("disabled", False),
|
|
("pending", False),
|
|
("frozen", True),
|
|
("current", False),
|
|
("archived", False),
|
|
],
|
|
)
|
|
def test_is_branch_frozen(self, state, expected):
|
|
get_branchinfo_mock = Mock()
|
|
get_branchinfo_mock.return_value = {
|
|
"name": "F42",
|
|
"long_name": "Fedora 42",
|
|
"version": "42",
|
|
"state": state,
|
|
}
|
|
|
|
self.bodhi.get_branch_info = get_branchinfo_mock
|
|
|
|
result = self.bodhi.is_branch_frozen("f42")
|
|
|
|
assert result is expected
|
|
|
|
def test_is_branch_frozen_defaults(self):
|
|
get_branchinfo_mock = Mock()
|
|
get_branchinfo_mock.return_value = {}
|
|
|
|
self.bodhi.get_branch_info = get_branchinfo_mock
|
|
|
|
result = self.bodhi.is_branch_frozen("f41")
|
|
|
|
assert result is False
|
|
|
|
get_branchinfo_mock.side_effect = BodhiError("Couldn't retrieve branch info.")
|
|
result = self.bodhi.is_branch_frozen("f41")
|
|
|
|
assert result is False
|