test_cg_importer.py: avoid creating temp files in checkout
This commit is contained in:
parent
20b4b1b17d
commit
14863332fd
2 changed files with 17 additions and 20 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
import mock
|
import mock
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
import tempfile
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
import koji
|
import koji
|
||||||
|
|
@ -12,12 +13,13 @@ UP = kojihub.UpdateProcessor
|
||||||
|
|
||||||
|
|
||||||
class TestCGImporter(unittest.TestCase):
|
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):
|
def setUp(self):
|
||||||
if not os.path.exists(self.TMP_PATH):
|
self.tempdir = tempfile.mkdtemp()
|
||||||
os.mkdir(self.TMP_PATH)
|
self.pathinfo = koji.PathInfo(self.tempdir)
|
||||||
self.path_work = mock.patch('koji.pathinfo.work').start()
|
mock.patch('koji.pathinfo', new=self.pathinfo).start()
|
||||||
self.context_db = mock.patch('kojihub.db.context').start()
|
self.context_db = mock.patch('kojihub.db.context').start()
|
||||||
self.context = mock.patch('kojihub.kojihub.context').start()
|
self.context = mock.patch('kojihub.kojihub.context').start()
|
||||||
self.get_build = mock.patch('kojihub.kojihub.get_build').start()
|
self.get_build = mock.patch('kojihub.kojihub.get_build').start()
|
||||||
|
|
@ -25,7 +27,6 @@ class TestCGImporter(unittest.TestCase):
|
||||||
self.userinfo = {'id': 123}
|
self.userinfo = {'id': 123}
|
||||||
self.rmtree = mock.patch('koji.util.rmtree').start()
|
self.rmtree = mock.patch('koji.util.rmtree').start()
|
||||||
self.lexists = mock.patch('os.path.lexists').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.new_build = mock.patch('kojihub.kojihub.new_build').start()
|
||||||
self.buildinfo = {
|
self.buildinfo = {
|
||||||
'id': 43,
|
'id': 43,
|
||||||
|
|
@ -52,11 +53,14 @@ class TestCGImporter(unittest.TestCase):
|
||||||
'source': 'https://example.com',
|
'source': 'https://example.com',
|
||||||
'extra': {},
|
'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):
|
def tearDown(self):
|
||||||
if os.path.exists(self.TMP_PATH):
|
mock.patch.stopall()
|
||||||
shutil.rmtree(self.TMP_PATH)
|
shutil.rmtree(self.tempdir)
|
||||||
|
|
||||||
def test_basic_instantiation(self):
|
def test_basic_instantiation(self):
|
||||||
kojihub.CG_Importer() # No exception!
|
kojihub.CG_Importer() # No exception!
|
||||||
|
|
@ -77,27 +81,23 @@ class TestCGImporter(unittest.TestCase):
|
||||||
f"expected type <class 'str'>", str(ex.exception))
|
f"expected type <class 'str'>", str(ex.exception))
|
||||||
|
|
||||||
def test_get_metadata_is_none(self):
|
def test_get_metadata_is_none(self):
|
||||||
self.path_work.return_value = os.path.dirname(__file__)
|
|
||||||
x = kojihub.CG_Importer()
|
x = kojihub.CG_Importer()
|
||||||
with self.assertRaises(GenericError) as ex:
|
with self.assertRaises(GenericError) as ex:
|
||||||
x.get_metadata(None, '')
|
x.get_metadata(None, '')
|
||||||
self.assertEqual('No such file: metadata.json', str(ex.exception))
|
self.assertEqual('No such file: metadata.json', str(ex.exception))
|
||||||
|
|
||||||
def test_get_metadata_missing_json_file(self):
|
def test_get_metadata_missing_json_file(self):
|
||||||
self.path_work.return_value = os.path.dirname(__file__)
|
|
||||||
x = kojihub.CG_Importer()
|
x = kojihub.CG_Importer()
|
||||||
with self.assertRaises(GenericError):
|
with self.assertRaises(GenericError):
|
||||||
x.get_metadata('missing.json', 'cg_importer_json')
|
x.get_metadata('missing.json', 'cg_importer_json')
|
||||||
|
|
||||||
def test_get_metadata_is_json_file(self):
|
def test_get_metadata_is_json_file(self):
|
||||||
self.path_work.return_value = os.path.dirname(__file__)
|
|
||||||
x = kojihub.CG_Importer()
|
x = kojihub.CG_Importer()
|
||||||
x.get_metadata('default.json', 'cg_importer_json')
|
x.get_metadata('default.json', 'cg_importer_json')
|
||||||
assert x.raw_metadata
|
assert x.raw_metadata
|
||||||
assert isinstance(x.raw_metadata, str)
|
assert isinstance(x.raw_metadata, str)
|
||||||
|
|
||||||
def test_assert_cg_access(self):
|
def test_assert_cg_access(self):
|
||||||
self.path_work.return_value = os.path.dirname(__file__)
|
|
||||||
cursor = mock.MagicMock()
|
cursor = mock.MagicMock()
|
||||||
self.context.session.user_id = 42
|
self.context.session.user_id = 42
|
||||||
self.context_db.cnx.cursor.return_value = cursor
|
self.context_db.cnx.cursor.return_value = cursor
|
||||||
|
|
@ -109,7 +109,6 @@ class TestCGImporter(unittest.TestCase):
|
||||||
assert isinstance(x.cg, int)
|
assert isinstance(x.cg, int)
|
||||||
|
|
||||||
def test_prep_build(self):
|
def test_prep_build(self):
|
||||||
self.path_work.return_value = os.path.dirname(__file__)
|
|
||||||
cursor = mock.MagicMock()
|
cursor = mock.MagicMock()
|
||||||
self.context_db.cnx.cursor.return_value = cursor
|
self.context_db.cnx.cursor.return_value = cursor
|
||||||
cursor.fetchall.return_value = [(1, 'foo'), (2, 'bar')]
|
cursor.fetchall.return_value = [(1, 'foo'), (2, 'bar')]
|
||||||
|
|
@ -123,11 +122,12 @@ class TestCGImporter(unittest.TestCase):
|
||||||
assert isinstance(x.buildinfo, dict)
|
assert isinstance(x.buildinfo, dict)
|
||||||
|
|
||||||
def test_check_build_dir(self):
|
def test_check_build_dir(self):
|
||||||
path = '/random_path/random_dir'
|
# path = '/random_path/random_dir'
|
||||||
self.path_build.return_value = path
|
|
||||||
|
|
||||||
x = kojihub.CG_Importer()
|
x = kojihub.CG_Importer()
|
||||||
x.log_warning = mock.MagicMock()
|
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
|
# directory exists
|
||||||
self.lexists.return_value = True
|
self.lexists.return_value = True
|
||||||
|
|
@ -139,8 +139,8 @@ class TestCGImporter(unittest.TestCase):
|
||||||
# directory exists + delete
|
# directory exists + delete
|
||||||
self.lexists.return_value = True
|
self.lexists.return_value = True
|
||||||
x.check_build_dir(delete=True)
|
x.check_build_dir(delete=True)
|
||||||
self.rmtree.assert_called_once_with(path)
|
self.rmtree.assert_called_once_with(build_dir)
|
||||||
x.log_warning.assert_called_once_with("Deleting build directory: /random_path/random_dir")
|
x.log_warning.assert_called_once_with("Deleting build directory: %s" % build_dir)
|
||||||
x.log_warning.reset_mock()
|
x.log_warning.reset_mock()
|
||||||
|
|
||||||
# directory doesn't exist
|
# directory doesn't exist
|
||||||
|
|
@ -151,7 +151,6 @@ class TestCGImporter(unittest.TestCase):
|
||||||
x.log_warning.assert_not_called()
|
x.log_warning.assert_not_called()
|
||||||
|
|
||||||
def test_prep_build_exists(self):
|
def test_prep_build_exists(self):
|
||||||
self.path_work.return_value = os.path.dirname(__file__)
|
|
||||||
self.get_build.return_value = self.buildinfo
|
self.get_build.return_value = self.buildinfo
|
||||||
x = kojihub.CG_Importer()
|
x = kojihub.CG_Importer()
|
||||||
x.get_metadata('default.json', 'cg_importer_json')
|
x.get_metadata('default.json', 'cg_importer_json')
|
||||||
|
|
@ -159,7 +158,6 @@ class TestCGImporter(unittest.TestCase):
|
||||||
x.prep_build()
|
x.prep_build()
|
||||||
|
|
||||||
def test_get_build(self):
|
def test_get_build(self):
|
||||||
self.path_work.return_value = os.path.dirname(__file__)
|
|
||||||
cursor = mock.MagicMock()
|
cursor = mock.MagicMock()
|
||||||
cursor.fetchall.return_value = [(1, 'foo'), (2, 'bar')]
|
cursor.fetchall.return_value = [(1, 'foo'), (2, 'bar')]
|
||||||
self.context_db.cnx.cursor.return_value = cursor
|
self.context_db.cnx.cursor.return_value = cursor
|
||||||
|
|
@ -178,9 +176,8 @@ class TestCGImporter(unittest.TestCase):
|
||||||
assert isinstance(x.buildinfo, dict)
|
assert isinstance(x.buildinfo, dict)
|
||||||
|
|
||||||
def test_import_metadata(self):
|
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 = kojihub.CG_Importer()
|
||||||
|
x.buildinfo = self.buildinfo
|
||||||
x.get_metadata('default.json', 'cg_importer_json')
|
x.get_metadata('default.json', 'cg_importer_json')
|
||||||
x.import_metadata()
|
x.import_metadata()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue