mirror of
https://pagure.io/fm-orchestrator.git
synced 2026-04-02 10:20:31 +08:00
Use a context manager to patch sys.modules
When not using the context manager, it causes the pytest process to not close after the tests have completed when the tests are run using Python 3.
This commit is contained in:
@@ -202,7 +202,6 @@ class TestBuild:
|
||||
with io.open(path.join(dir_path, "modulemd.i686.txt"), encoding="utf-8") as mmd:
|
||||
assert len(mmd.read()) == 254
|
||||
|
||||
@patch.dict("sys.modules", krbV=Mock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_tag_cg_build(self, ClientSession):
|
||||
""" Test that the CG build is tagged. """
|
||||
@@ -210,7 +209,8 @@ class TestBuild:
|
||||
koji_session.getUser.return_value = GET_USER_RV
|
||||
koji_session.getTag.return_value = {"id": 123}
|
||||
|
||||
self.cg._tag_cg_build()
|
||||
with patch.dict("sys.modules", krbV=Mock()):
|
||||
self.cg._tag_cg_build()
|
||||
|
||||
koji_session.getTag.assert_called_once_with(self.cg.module.cg_build_koji_tag)
|
||||
koji_session.tagBuild.assert_called_once_with(123, "nginx-0-2.10e50d06")
|
||||
@@ -218,7 +218,6 @@ class TestBuild:
|
||||
# tagBuild requires logging into a session in advance.
|
||||
koji_session.krb_login.assert_called_once()
|
||||
|
||||
@patch.dict("sys.modules", krbV=Mock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_tag_cg_build_fallback_to_default_tag(self, ClientSession):
|
||||
""" Test that the CG build is tagged to default tag. """
|
||||
@@ -226,7 +225,8 @@ class TestBuild:
|
||||
koji_session.getUser.return_value = GET_USER_RV
|
||||
koji_session.getTag.side_effect = [{}, {"id": 123}]
|
||||
|
||||
self.cg._tag_cg_build()
|
||||
with patch.dict("sys.modules", krbV=Mock()):
|
||||
self.cg._tag_cg_build()
|
||||
|
||||
assert koji_session.getTag.mock_calls == [
|
||||
call(self.cg.module.cg_build_koji_tag),
|
||||
@@ -237,7 +237,6 @@ class TestBuild:
|
||||
# tagBuild requires logging into a session in advance.
|
||||
koji_session.krb_login.assert_called_once()
|
||||
|
||||
@patch.dict("sys.modules", krbV=Mock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_tag_cg_build_no_tag_set(self, ClientSession):
|
||||
""" Test that the CG build is not tagged when no tag set. """
|
||||
@@ -246,13 +245,13 @@ class TestBuild:
|
||||
koji_session.getTag.side_effect = [{}, {"id": 123}]
|
||||
|
||||
self.cg.module.cg_build_koji_tag = None
|
||||
self.cg._tag_cg_build()
|
||||
with patch.dict("sys.modules", krbV=Mock()):
|
||||
self.cg._tag_cg_build()
|
||||
|
||||
koji_session.tagBuild.assert_not_called()
|
||||
# tagBuild requires logging into a session in advance.
|
||||
koji_session.krb_login.assert_called_once()
|
||||
|
||||
@patch.dict("sys.modules", krbV=Mock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_tag_cg_build_no_tag_available(self, ClientSession):
|
||||
""" Test that the CG build is not tagged when no tag available. """
|
||||
@@ -260,7 +259,8 @@ class TestBuild:
|
||||
koji_session.getUser.return_value = GET_USER_RV
|
||||
koji_session.getTag.side_effect = [{}, {}]
|
||||
|
||||
self.cg._tag_cg_build()
|
||||
with patch.dict("sys.modules", krbV=Mock()):
|
||||
self.cg._tag_cg_build()
|
||||
|
||||
koji_session.tagBuild.assert_not_called()
|
||||
# tagBuild requires logging into a session in advance.
|
||||
@@ -963,7 +963,6 @@ class TestBuild:
|
||||
requires.append("%s:%s" % (name, stream))
|
||||
assert "%s:%s" % (mmd.get_module_name(), mmd.get_stream_name()) in requires
|
||||
|
||||
@patch.dict("sys.modules", krbV=Mock())
|
||||
@patch("koji.ClientSession")
|
||||
@patch("module_build_service.builder.KojiContentGenerator.KojiContentGenerator._tag_cg_build")
|
||||
@patch("module_build_service.builder.KojiContentGenerator.KojiContentGenerator._load_koji_tag")
|
||||
@@ -971,7 +970,8 @@ class TestBuild:
|
||||
""" Tests whether build is still tagged even if there's an exception in CGImport """
|
||||
cl_session.return_value.CGImport = Mock(
|
||||
side_effect=koji.GenericError("Build already exists asdv"))
|
||||
self.cg.koji_import()
|
||||
with patch.dict("sys.modules", krbV=Mock()):
|
||||
self.cg.koji_import()
|
||||
tagger.assert_called()
|
||||
|
||||
def test_fill_in_rpms_list_debuginfo_deps(self):
|
||||
|
||||
@@ -414,7 +414,6 @@ class TestKojiBuilder:
|
||||
expected_calls = [mock.call(1, "foo"), mock.call(2, "foo"), mock.call(1, "bar")]
|
||||
assert mock_session.untagBuild.mock_calls == expected_calls
|
||||
|
||||
@patch.dict("sys.modules", krbV=MagicMock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_get_build_weights(self, ClientSession):
|
||||
session = ClientSession.return_value
|
||||
@@ -431,7 +430,8 @@ class TestKojiBuilder:
|
||||
],
|
||||
]
|
||||
|
||||
weights = KojiModuleBuilder.get_build_weights(["httpd", "apr"])
|
||||
with patch.dict("sys.modules", krbV=MagicMock()):
|
||||
weights = KojiModuleBuilder.get_build_weights(["httpd", "apr"])
|
||||
assert weights == {"httpd": 2, "apr": 2}
|
||||
|
||||
expected_calls = [mock.call(456), mock.call(789)]
|
||||
@@ -440,7 +440,6 @@ class TestKojiBuilder:
|
||||
# getLoggedInUser requires to a logged-in session
|
||||
session.krb_login.assert_called_once()
|
||||
|
||||
@patch.dict("sys.modules", krbV=MagicMock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_get_build_weights_no_task_id(self, ClientSession):
|
||||
session = ClientSession.return_value
|
||||
@@ -455,14 +454,14 @@ class TestKojiBuilder:
|
||||
]
|
||||
session.getAverageBuildDuration.return_value = None
|
||||
|
||||
weights = KojiModuleBuilder.get_build_weights(["httpd", "apr"])
|
||||
with patch.dict("sys.modules", krbV=MagicMock()):
|
||||
weights = KojiModuleBuilder.get_build_weights(["httpd", "apr"])
|
||||
assert weights == {"httpd": 2, "apr": 1.5}
|
||||
|
||||
expected_calls = [mock.call(456)]
|
||||
assert session.getTaskDescendents.mock_calls == expected_calls
|
||||
session.krb_login.assert_called_once()
|
||||
|
||||
@patch.dict("sys.modules", krbV=MagicMock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_get_build_weights_no_build(self, ClientSession):
|
||||
session = ClientSession.return_value
|
||||
@@ -477,14 +476,14 @@ class TestKojiBuilder:
|
||||
]
|
||||
session.getAverageBuildDuration.return_value = None
|
||||
|
||||
weights = KojiModuleBuilder.get_build_weights(["httpd", "apr"])
|
||||
with patch.dict("sys.modules", krbV=MagicMock()):
|
||||
weights = KojiModuleBuilder.get_build_weights(["httpd", "apr"])
|
||||
assert weights == {"httpd": 2, "apr": 1.5}
|
||||
|
||||
expected_calls = [mock.call(456)]
|
||||
assert session.getTaskDescendents.mock_calls == expected_calls
|
||||
session.krb_login.assert_called_once()
|
||||
|
||||
@patch.dict("sys.modules", krbV=MagicMock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_get_build_weights_listBuilds_failed(self, ClientSession):
|
||||
session = ClientSession.return_value
|
||||
@@ -492,7 +491,8 @@ class TestKojiBuilder:
|
||||
session.multiCall.side_effect = [[[1], [2]], []]
|
||||
session.getAverageBuildDuration.return_value = None
|
||||
|
||||
weights = KojiModuleBuilder.get_build_weights(["httpd", "apr"])
|
||||
with patch.dict("sys.modules", krbV=MagicMock()):
|
||||
weights = KojiModuleBuilder.get_build_weights(["httpd", "apr"])
|
||||
assert weights == {"httpd": 1.5, "apr": 1.5}
|
||||
|
||||
expected_calls = [
|
||||
@@ -504,7 +504,6 @@ class TestKojiBuilder:
|
||||
assert session.listBuilds.mock_calls == expected_calls
|
||||
session.krb_login.assert_called_once()
|
||||
|
||||
@patch.dict("sys.modules", krbV=MagicMock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_get_build_weights_getPackageID_failed(self, ClientSession):
|
||||
session = ClientSession.return_value
|
||||
@@ -512,7 +511,8 @@ class TestKojiBuilder:
|
||||
session.multiCall.side_effect = [[], []]
|
||||
session.getAverageBuildDuration.return_value = None
|
||||
|
||||
weights = KojiModuleBuilder.get_build_weights(["httpd", "apr"])
|
||||
with patch.dict("sys.modules", krbV=MagicMock()):
|
||||
weights = KojiModuleBuilder.get_build_weights(["httpd", "apr"])
|
||||
assert weights == {"httpd": 1.5, "apr": 1.5}
|
||||
|
||||
expected_calls = [mock.call("httpd"), mock.call("apr")]
|
||||
@@ -520,12 +520,12 @@ class TestKojiBuilder:
|
||||
|
||||
session.krb_login.assert_called_once()
|
||||
|
||||
@patch.dict("sys.modules", krbV=MagicMock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_get_build_weights_getLoggedInUser_failed(self, ClientSession):
|
||||
session = ClientSession.return_value
|
||||
session.getAverageBuildDuration.return_value = None
|
||||
weights = KojiModuleBuilder.get_build_weights(["httpd", "apr"])
|
||||
with patch.dict("sys.modules", krbV=MagicMock()):
|
||||
weights = KojiModuleBuilder.get_build_weights(["httpd", "apr"])
|
||||
assert weights == {"httpd": 1.5, "apr": 1.5}
|
||||
session.krb_login.assert_called_once()
|
||||
|
||||
@@ -905,51 +905,51 @@ class TestKojiBuilder:
|
||||
else:
|
||||
mock_koji_cg.koji_import.assert_not_called()
|
||||
|
||||
@patch.dict("sys.modules", krbV=MagicMock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_ensure_builder_use_a_logged_in_koji_session(self, ClientSession):
|
||||
module_build = module_build_service.common.models.ModuleBuild.get_by_id(db_session, 2)
|
||||
builder = KojiModuleBuilder(db_session, "owner", module_build, conf, "module-tag", [])
|
||||
with patch.dict("sys.modules", krbV=MagicMock()):
|
||||
builder = KojiModuleBuilder(db_session, "owner", module_build, conf, "module-tag", [])
|
||||
builder.koji_session.krb_login.assert_called_once()
|
||||
|
||||
@patch.dict("sys.modules", krbV=MagicMock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_get_module_build_arches(self, ClientSession):
|
||||
module_build = module_build_service.common.models.ModuleBuild.get_by_id(db_session, 2)
|
||||
arches = "x86_64 i686 ppc64le aarch64 s390x"
|
||||
session = ClientSession.return_value
|
||||
session.getTag.return_value = {"arches": arches}
|
||||
ret = KojiModuleBuilder.get_module_build_arches(module_build)
|
||||
with patch.dict("sys.modules", krbV=MagicMock()):
|
||||
ret = KojiModuleBuilder.get_module_build_arches(module_build)
|
||||
assert " ".join(ret) == arches
|
||||
|
||||
@patch.dict("sys.modules", krbV=MagicMock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_get_module_build_arches_with_archless_tag(self, ClientSession):
|
||||
module_build = module_build_service.common.models.ModuleBuild.get_by_id(db_session, 2)
|
||||
session = ClientSession.return_value
|
||||
session.getTag.return_value = {"arches": ""}
|
||||
ret = KojiModuleBuilder.get_module_build_arches(module_build)
|
||||
with patch.dict("sys.modules", krbV=MagicMock()):
|
||||
ret = KojiModuleBuilder.get_module_build_arches(module_build)
|
||||
assert ret == []
|
||||
|
||||
@patch.dict("sys.modules", krbV=MagicMock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_get_module_build_arches_without_tag(self, ClientSession):
|
||||
module_build = module_build_service.common.models.ModuleBuild.get_by_id(db_session, 2)
|
||||
module_build.koji_tag = None
|
||||
session = ClientSession.return_value
|
||||
ret = KojiModuleBuilder.get_module_build_arches(module_build)
|
||||
with patch.dict("sys.modules", krbV=MagicMock()):
|
||||
ret = KojiModuleBuilder.get_module_build_arches(module_build)
|
||||
assert ret == []
|
||||
session.getTag.assert_not_called()
|
||||
session.assert_not_called()
|
||||
|
||||
@patch.dict("sys.modules", krbV=MagicMock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_get_module_build_arches_with_unknown_tag(self, ClientSession):
|
||||
module_build = module_build_service.common.models.ModuleBuild.get_by_id(db_session, 2)
|
||||
session = ClientSession.return_value
|
||||
session.getTag.return_value = None
|
||||
with pytest.raises(ValueError, match="Unknown Koji tag .*"):
|
||||
KojiModuleBuilder.get_module_build_arches(module_build)
|
||||
with patch.dict("sys.modules", krbV=MagicMock()):
|
||||
KojiModuleBuilder.get_module_build_arches(module_build)
|
||||
|
||||
|
||||
class TestGetDistTagSRPM:
|
||||
|
||||
@@ -126,7 +126,6 @@ class TestPoller:
|
||||
|
||||
assert len(start_build_component.mock_calls) == expected_build_calls
|
||||
|
||||
@patch.dict("sys.modules", krbV=mock.MagicMock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_retrigger_new_repo_on_failure(self, ClientSession, create_builder, dbg):
|
||||
"""
|
||||
@@ -148,12 +147,12 @@ class TestPoller:
|
||||
module_build.new_repo_task_id = 123456
|
||||
db_session.commit()
|
||||
|
||||
producer.retrigger_new_repo_on_failure()
|
||||
with patch.dict("sys.modules", krbV=mock.MagicMock()):
|
||||
producer.retrigger_new_repo_on_failure()
|
||||
|
||||
koji_session.newRepo.assert_called_once_with(
|
||||
"module-testmodule-master-20170219191323-c40c156c-build")
|
||||
|
||||
@patch.dict("sys.modules", krbV=mock.MagicMock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_trigger_new_repo_when_succeeded(self, ClientSession, create_builder, dbg):
|
||||
"""
|
||||
@@ -176,7 +175,8 @@ class TestPoller:
|
||||
module_build.new_repo_task_id = 123456
|
||||
db_session.commit()
|
||||
|
||||
producer.retrigger_new_repo_on_failure()
|
||||
with patch.dict("sys.modules", krbV=mock.MagicMock()):
|
||||
producer.retrigger_new_repo_on_failure()
|
||||
|
||||
module_build = models.ModuleBuild.get_by_id(db_session, 3)
|
||||
|
||||
@@ -208,7 +208,6 @@ class TestPoller:
|
||||
for component in components:
|
||||
assert component.state is None
|
||||
|
||||
@patch.dict("sys.modules", krbV=mock.MagicMock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_old_build_targets_are_not_associated_with_any_module_builds(
|
||||
self, ClientSession, create_builder, dbg
|
||||
@@ -220,11 +219,11 @@ class TestPoller:
|
||||
{"dest_tag_name": "module-yyy-2"},
|
||||
]
|
||||
|
||||
producer.delete_old_koji_targets()
|
||||
with patch.dict("sys.modules", krbV=mock.MagicMock()):
|
||||
producer.delete_old_koji_targets()
|
||||
|
||||
koji_session.deleteBuildTarget.assert_not_called()
|
||||
|
||||
@patch.dict("sys.modules", krbV=mock.MagicMock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_dont_delete_base_module_build_target(
|
||||
self, ClientSession, create_builder, dbg
|
||||
@@ -238,10 +237,11 @@ class TestPoller:
|
||||
# If module build's name is one of base module names, build target
|
||||
# should not be deleted.
|
||||
with patch.object(conf, "base_module_names", new=[module_build.name]):
|
||||
producer.delete_old_koji_targets()
|
||||
koji_session.deleteBuildTarget.assert_not_called()
|
||||
with patch.dict("sys.modules", krbV=mock.MagicMock()):
|
||||
producer.delete_old_koji_targets()
|
||||
|
||||
koji_session.deleteBuildTarget.assert_not_called()
|
||||
|
||||
@patch.dict("sys.modules", krbV=mock.MagicMock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_dont_delete_build_target_for_unfinished_module_builds(
|
||||
self, ClientSession, create_builder, dbg
|
||||
@@ -258,11 +258,11 @@ class TestPoller:
|
||||
module_build.state = state
|
||||
db_session.commit()
|
||||
|
||||
producer.delete_old_koji_targets()
|
||||
with patch.dict("sys.modules", krbV=mock.MagicMock()):
|
||||
producer.delete_old_koji_targets()
|
||||
|
||||
koji_session.deleteBuildTarget.assert_not_called()
|
||||
|
||||
@patch.dict("sys.modules", krbV=mock.MagicMock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_only_delete_build_target_with_allowed_koji_tag_prefix(
|
||||
self, ClientSession, create_builder, dbg
|
||||
@@ -290,12 +290,12 @@ class TestPoller:
|
||||
|
||||
with patch.object(conf, "koji_tag_prefixes", new=["module", "another-prefix"]):
|
||||
with patch.object(conf, "koji_target_delete_time", new=60):
|
||||
producer.delete_old_koji_targets()
|
||||
with patch.dict("sys.modules", krbV=mock.MagicMock()):
|
||||
producer.delete_old_koji_targets()
|
||||
|
||||
koji_session.deleteBuildTarget.assert_called_once_with(1)
|
||||
koji_session.krb_login.assert_called_once()
|
||||
|
||||
@patch.dict("sys.modules", krbV=mock.MagicMock())
|
||||
@patch("koji.ClientSession")
|
||||
def test_cant_delete_build_target_if_not_reach_delete_time(
|
||||
self, ClientSession, create_builder, dbg
|
||||
@@ -317,9 +317,10 @@ class TestPoller:
|
||||
]
|
||||
|
||||
with patch.object(conf, "koji_tag_prefixes", new=["module"]):
|
||||
# Use default koji_target_delete_time in config. That time is long
|
||||
# enough for test.
|
||||
producer.delete_old_koji_targets()
|
||||
with patch.dict("sys.modules", krbV=mock.MagicMock()):
|
||||
# Use default koji_target_delete_time in config. That time is long
|
||||
# enough for test.
|
||||
producer.delete_old_koji_targets()
|
||||
|
||||
koji_session.deleteBuildTarget.assert_not_called()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user