From 8d34971fd663f5c155a432b82a6b3463e4972f2c Mon Sep 17 00:00:00 2001 From: Jakub Kadlcik Date: Feb 19 2020 08:00:40 +0000 Subject: rpmbuild: add tests that we properly cleanup tmp directories Merges: #1262 --- diff --git a/rpmbuild/tests/test_base.py b/rpmbuild/tests/test_base.py index 019c6a9..f392c77 100644 --- a/rpmbuild/tests/test_base.py +++ b/rpmbuild/tests/test_base.py @@ -28,3 +28,9 @@ class TestProvider(TestCase): mock.call.__enter__().write('%__urlhelper_localopts --proto -all,+https,+ftps\n'), ] rpmmacros.assert_has_calls(calls, any_order=True) + + @mock.patch('copr_rpmbuild.providers.base.os.mkdir') + @mock.patch('copr_rpmbuild.providers.base.Provider.create_rpmmacros') + def test_workdir_in_outdir(self, mock_create_rpmmacros, mock_mkdir): + provider = Provider(self.source_json, self.resultdir, self.config) + assert provider.workdir == "/path/to/resultdir/obtain-sources" diff --git a/rpmbuild/tests/test_main.py b/rpmbuild/tests/test_main.py new file mode 100644 index 0000000..b1b1594 --- /dev/null +++ b/rpmbuild/tests/test_main.py @@ -0,0 +1,42 @@ +import os +import pytest +import unittest +import shutil + +from main import produce_srpm +from copr_rpmbuild.helpers import SourceType + +try: + from unittest import mock +except ImportError: + # Python 2 version depends on mock + import mock + + +class TestTmpCleanup(unittest.TestCase): + + config = {} + resultdir = "/path/to/non/existing/directory" + task = {"source_type": SourceType.UPLOAD, + "source_json": {"url": "http://foo.ex/somepackage.spec"}} + + @mock.patch("copr_rpmbuild.providers.spec.UrlProvider.produce_srpm") + @mock.patch("main.shutil.rmtree", wraps=shutil.rmtree) + def test_produce_srpm_cleanup(self, mock_rmtree, mock_produce_srpm): + # Just to be sure, we are starting from zero + assert mock_rmtree.call_count == 0 + + # Test that we cleanup after successful build + produce_srpm(self.task, self.config, self.resultdir) + args, _ = mock_rmtree.call_args + assert mock_rmtree.call_count == 1 + assert args[0].startswith("/tmp/copr-rpmbuild-") + + # Test that we cleanup after unsuccessful build + mock_produce_srpm.side_effect = RuntimeError("Jeeez, something failed") + with pytest.raises(RuntimeError): + produce_srpm(self.task, self.config, self.resultdir) + + args, _ = mock_rmtree.call_args + assert mock_rmtree.call_count == 2 + assert args[0].startswith("/tmp/copr-rpmbuild-")