From 14863332fd62a76a95e992c8e6483369f9bf7640 Mon Sep 17 00:00:00 2001 From: Mike McLean Date: Jan 22 2024 18:41:45 +0000 Subject: test_cg_importer.py: avoid creating temp files in checkout --- diff --git a/tests/test_hub/cg_importer_json/default.json b/tests/test_hub/cg_importer_json/default.json deleted file mode 100644 index 03eb402..0000000 --- a/tests/test_hub/cg_importer_json/default.json +++ /dev/null @@ -1,95 +0,0 @@ -{"metadata_version": 0, - "build": {"name": "rhel-server-docker", - "version": "7.1", - "release": "4", - "source": "git://git.engineering.redhat.com/users/vpavlin/tdl_templates.git#a14f145244", - "extra": {}, - "start_time": 1423148398, - "end_time": 1423148828, - "owner": "koji"}, - "buildroots": [{"id": 1, - "host": {"os": "rhel-7", - "arch": "x86_64"}, - "content_generator": {"name": "osbs", - "version": "0.2"}, - "container": {"type": "docker", - "arch": "x86_64"}, - "tools": [{"name": "docker", - "version": "1.5.0"}], - "components": [{"type": "rpm", - "name": "glibc", - "version": "2.17", - "release": "75.el7", - "epoch": null, - "arch": "x86_64", - "sigmd5": "a1b2c3...", - "signature": "fd431d51"}, - {"type": "rpm", - "name": "openssl", - "version": "1.0.1e", - "release": "42.el7", - "epoch": null, - "arch": "x86_64", - "sigmd5": "d4e5f6...", - "signature": "fd431d51"}, - {"type": "rpm", - "name": "bind-libs", - "version": "9.9.4", - "release": "18.el7", - "epoch": 32, - "arch": "x86_64", - "sigmd5": "987abc...", - "signature": null}, - {"type": "rpm", - "name": "python-urllib3", - "version": "1.5", - "release": "8.el7", - "epoch": null, - "arch": "noarch", - "sigmd5": "123hgf...", - "signature": null}, - {"type": "file", - "filename": "jboss-eap-6.3.3-full-build.zip", - "filesize": 12345678, - "checksum": "5ec2f29c4e1c2e2aa6552836e236a158", - "checksum_type": "md5"}, - {"type": "kojifile", - "filename": "jboss-eap-6.4.10-1.win6.src.zip", - "filesize": 21670719, - "checksum": "215cf04db6bdbefb9644f001995bd550", - "checksum_type": "md5", - "archive_id": 1, - "nvr": "jboss-eap-6.4.10-1.win6"} - ], - "extra": {"osbs": {"build_id": 12345, - "builder_image_id": 67890}} - }], - "output": [{"buildroot_id": 1, - "filename": "rhel-server-docker-7.1-4.x86_64.tar.xz", - "filesize": 34440656, - "arch": "x86_64", - "checksum_type": "md5", - "checksum": "275ae42a45cfedbdb0c0a1acc0b55a1b", - "type": "docker-image", - "components": "", - "extra": {"docker": {"id": "987654...", - "parent_id": "a1b2c3...", - "repositories": ["repository.example.com/username/imagename:tagname", - "repository.example.com/username/imagename:latest"]}}}, - {"buildroot_id": 1, - "filename": "checkout.log", - "filesize": 85724, - "arch": "noarch", - "checksum_type": "md5", - "checksum": "a1b2c3...", - "type": "log"}, - {"buildroot_id": 1, - "filename": "os-indirection.log", - "filesize": 27189, - "arch": "noarch", - "checksum_type": "md5", - "checksum": "d4f5g6...", - "type": "log"} - ] - -} diff --git a/tests/test_hub/data/cg_importer/default.json b/tests/test_hub/data/cg_importer/default.json new file mode 100644 index 0000000..03eb402 --- /dev/null +++ b/tests/test_hub/data/cg_importer/default.json @@ -0,0 +1,95 @@ +{"metadata_version": 0, + "build": {"name": "rhel-server-docker", + "version": "7.1", + "release": "4", + "source": "git://git.engineering.redhat.com/users/vpavlin/tdl_templates.git#a14f145244", + "extra": {}, + "start_time": 1423148398, + "end_time": 1423148828, + "owner": "koji"}, + "buildroots": [{"id": 1, + "host": {"os": "rhel-7", + "arch": "x86_64"}, + "content_generator": {"name": "osbs", + "version": "0.2"}, + "container": {"type": "docker", + "arch": "x86_64"}, + "tools": [{"name": "docker", + "version": "1.5.0"}], + "components": [{"type": "rpm", + "name": "glibc", + "version": "2.17", + "release": "75.el7", + "epoch": null, + "arch": "x86_64", + "sigmd5": "a1b2c3...", + "signature": "fd431d51"}, + {"type": "rpm", + "name": "openssl", + "version": "1.0.1e", + "release": "42.el7", + "epoch": null, + "arch": "x86_64", + "sigmd5": "d4e5f6...", + "signature": "fd431d51"}, + {"type": "rpm", + "name": "bind-libs", + "version": "9.9.4", + "release": "18.el7", + "epoch": 32, + "arch": "x86_64", + "sigmd5": "987abc...", + "signature": null}, + {"type": "rpm", + "name": "python-urllib3", + "version": "1.5", + "release": "8.el7", + "epoch": null, + "arch": "noarch", + "sigmd5": "123hgf...", + "signature": null}, + {"type": "file", + "filename": "jboss-eap-6.3.3-full-build.zip", + "filesize": 12345678, + "checksum": "5ec2f29c4e1c2e2aa6552836e236a158", + "checksum_type": "md5"}, + {"type": "kojifile", + "filename": "jboss-eap-6.4.10-1.win6.src.zip", + "filesize": 21670719, + "checksum": "215cf04db6bdbefb9644f001995bd550", + "checksum_type": "md5", + "archive_id": 1, + "nvr": "jboss-eap-6.4.10-1.win6"} + ], + "extra": {"osbs": {"build_id": 12345, + "builder_image_id": 67890}} + }], + "output": [{"buildroot_id": 1, + "filename": "rhel-server-docker-7.1-4.x86_64.tar.xz", + "filesize": 34440656, + "arch": "x86_64", + "checksum_type": "md5", + "checksum": "275ae42a45cfedbdb0c0a1acc0b55a1b", + "type": "docker-image", + "components": "", + "extra": {"docker": {"id": "987654...", + "parent_id": "a1b2c3...", + "repositories": ["repository.example.com/username/imagename:tagname", + "repository.example.com/username/imagename:latest"]}}}, + {"buildroot_id": 1, + "filename": "checkout.log", + "filesize": 85724, + "arch": "noarch", + "checksum_type": "md5", + "checksum": "a1b2c3...", + "type": "log"}, + {"buildroot_id": 1, + "filename": "os-indirection.log", + "filesize": 27189, + "arch": "noarch", + "checksum_type": "md5", + "checksum": "d4f5g6...", + "type": "log"} + ] + +} diff --git a/tests/test_hub/test_cg_importer.py b/tests/test_hub/test_cg_importer.py index d2328e6..6da3288 100644 --- a/tests/test_hub/test_cg_importer.py +++ b/tests/test_hub/test_cg_importer.py @@ -1,6 +1,7 @@ import mock import os import shutil +import tempfile import unittest import koji @@ -12,12 +13,13 @@ UP = kojihub.UpdateProcessor class TestCGImporter(unittest.TestCase): - TMP_PATH = os.path.join(os.path.dirname(__file__), 'tmptest') + + DATADIR = os.path.join(os.path.dirname(__file__), 'data', 'cg_importer') def setUp(self): - if not os.path.exists(self.TMP_PATH): - os.mkdir(self.TMP_PATH) - self.path_work = mock.patch('koji.pathinfo.work').start() + self.tempdir = tempfile.mkdtemp() + self.pathinfo = koji.PathInfo(self.tempdir) + mock.patch('koji.pathinfo', new=self.pathinfo).start() self.context_db = mock.patch('kojihub.db.context').start() self.context = mock.patch('kojihub.kojihub.context').start() self.get_build = mock.patch('kojihub.kojihub.get_build').start() @@ -25,7 +27,6 @@ class TestCGImporter(unittest.TestCase): self.userinfo = {'id': 123} self.rmtree = mock.patch('koji.util.rmtree').start() self.lexists = mock.patch('os.path.lexists').start() - self.path_build = mock.patch('koji.pathinfo.build').start() self.new_build = mock.patch('kojihub.kojihub.new_build').start() self.buildinfo = { 'id': 43, @@ -52,11 +53,14 @@ class TestCGImporter(unittest.TestCase): 'source': 'https://example.com', 'extra': {}, } + upload_dir = os.path.join(self.pathinfo.work(), 'cg_importer_json') + os.makedirs(upload_dir) + shutil.copy("%s/default.json" % self.DATADIR, upload_dir) def tearDown(self): - if os.path.exists(self.TMP_PATH): - shutil.rmtree(self.TMP_PATH) + mock.patch.stopall() + shutil.rmtree(self.tempdir) def test_basic_instantiation(self): kojihub.CG_Importer() # No exception! @@ -77,27 +81,23 @@ class TestCGImporter(unittest.TestCase): f"expected type ", str(ex.exception)) def test_get_metadata_is_none(self): - self.path_work.return_value = os.path.dirname(__file__) x = kojihub.CG_Importer() with self.assertRaises(GenericError) as ex: x.get_metadata(None, '') self.assertEqual('No such file: metadata.json', str(ex.exception)) def test_get_metadata_missing_json_file(self): - self.path_work.return_value = os.path.dirname(__file__) x = kojihub.CG_Importer() with self.assertRaises(GenericError): x.get_metadata('missing.json', 'cg_importer_json') def test_get_metadata_is_json_file(self): - self.path_work.return_value = os.path.dirname(__file__) x = kojihub.CG_Importer() x.get_metadata('default.json', 'cg_importer_json') assert x.raw_metadata assert isinstance(x.raw_metadata, str) def test_assert_cg_access(self): - self.path_work.return_value = os.path.dirname(__file__) cursor = mock.MagicMock() self.context.session.user_id = 42 self.context_db.cnx.cursor.return_value = cursor @@ -109,7 +109,6 @@ class TestCGImporter(unittest.TestCase): assert isinstance(x.cg, int) def test_prep_build(self): - self.path_work.return_value = os.path.dirname(__file__) cursor = mock.MagicMock() self.context_db.cnx.cursor.return_value = cursor cursor.fetchall.return_value = [(1, 'foo'), (2, 'bar')] @@ -123,11 +122,12 @@ class TestCGImporter(unittest.TestCase): assert isinstance(x.buildinfo, dict) def test_check_build_dir(self): - path = '/random_path/random_dir' - self.path_build.return_value = path + # path = '/random_path/random_dir' x = kojihub.CG_Importer() x.log_warning = mock.MagicMock() + x.buildinfo = self.buildinfo + build_dir = "%s/vol/testvolume/packages/testpkg/1.0.1e/42.el7" % self.tempdir # directory exists self.lexists.return_value = True @@ -139,8 +139,8 @@ class TestCGImporter(unittest.TestCase): # directory exists + delete self.lexists.return_value = True x.check_build_dir(delete=True) - self.rmtree.assert_called_once_with(path) - x.log_warning.assert_called_once_with("Deleting build directory: /random_path/random_dir") + self.rmtree.assert_called_once_with(build_dir) + x.log_warning.assert_called_once_with("Deleting build directory: %s" % build_dir) x.log_warning.reset_mock() # directory doesn't exist @@ -151,7 +151,6 @@ class TestCGImporter(unittest.TestCase): x.log_warning.assert_not_called() def test_prep_build_exists(self): - self.path_work.return_value = os.path.dirname(__file__) self.get_build.return_value = self.buildinfo x = kojihub.CG_Importer() x.get_metadata('default.json', 'cg_importer_json') @@ -159,7 +158,6 @@ class TestCGImporter(unittest.TestCase): x.prep_build() def test_get_build(self): - self.path_work.return_value = os.path.dirname(__file__) cursor = mock.MagicMock() cursor.fetchall.return_value = [(1, 'foo'), (2, 'bar')] self.context_db.cnx.cursor.return_value = cursor @@ -178,9 +176,8 @@ class TestCGImporter(unittest.TestCase): assert isinstance(x.buildinfo, dict) def test_import_metadata(self): - self.path_work.return_value = os.path.dirname(__file__) - self.path_build.return_value = self.TMP_PATH x = kojihub.CG_Importer() + x.buildinfo = self.buildinfo x.get_metadata('default.json', 'cg_importer_json') x.import_metadata()