Move database classes and functions from kojihub.py to koji/db.py

Move QueryProcessor, InsertProcessor, UpdateProcessor,
BulkInsertProcessor, _applyQueryOpts, get_event, _dml,
_fetchMulti, _fetchSingle, _singleValue, _multiRow, _singleRow

Update koji-sweep-db script to DB Processors

Fixes: https://pagure.io/koji/issue/3466
This commit is contained in:
Jana Cupova 2022-08-18 07:06:08 +02:00 committed by Tomas Kopecek
parent fadda5b755
commit 1cfe6538db
30 changed files with 1170 additions and 1141 deletions

View file

@ -30,9 +30,10 @@ class TestAddHost(unittest.TestCase):
side_effect=self.getUpdate).start()
self.updates = []
self.context = mock.patch('kojihub.context').start()
self.context_db = mock.patch('koji.db.context').start()
# It seems MagicMock will not automatically handle attributes that
# start with "assert"
self.context.session.assertLogin = mock.MagicMock()
self.context_db.session.assertLogin = mock.MagicMock()
self.context.session.assertPerm = mock.MagicMock()
self.context.opts = {'HostPrincipalFormat': '-%s-'}
self.exports = kojihub.RootExports()

View file

@ -20,12 +20,13 @@ class TestAddHostToChannel(unittest.TestCase):
side_effect=self.getInsert).start()
self.inserts = []
self.context = mock.patch('kojihub.context').start()
self.context_db = mock.patch('koji.db.context').start()
# It seems MagicMock will not automatically handle attributes that
# start with "assert"
self.context.session.assertLogin = mock.MagicMock()
self.context_db.session.assertLogin = mock.MagicMock()
self.context.session.assertPerm = mock.MagicMock()
self.context.event_id = 42
self.context.session.user_id = 23
self.context_db.event_id = 42
self.context_db.session.user_id = 23
self.context.opts = {'HostPrincipalFormat': '-%s-'}
self.exports = kojihub.RootExports()
self.get_channel = mock.patch('kojihub.get_channel').start()

View file

@ -17,6 +17,16 @@ class TestCGImporter(unittest.TestCase):
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.context_db = mock.patch('koji.db.context').start()
self.context = mock.patch('kojihub.context').start()
self.get_build = mock.patch('kojihub.get_build').start()
self.get_user = mock.patch('kojihub.get_user').start()
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.new_build').start()
def tearDown(self):
if os.path.exists(self.TMP_PATH):
@ -41,33 +51,30 @@ class TestCGImporter(unittest.TestCase):
f"expected type <class 'str'>", 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))
@mock.patch("koji.pathinfo.work")
def test_get_metadata_missing_json_file(self, work):
work.return_value = os.path.dirname(__file__)
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')
@mock.patch("koji.pathinfo.work")
def test_get_metadata_is_json_file(self, work):
work.return_value = os.path.dirname(__file__)
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)
@mock.patch('kojihub.context')
@mock.patch("koji.pathinfo.work")
def test_assert_cg_access(self, work, context):
work.return_value = os.path.dirname(__file__)
def test_assert_cg_access(self):
self.path_work.return_value = os.path.dirname(__file__)
cursor = mock.MagicMock()
context.session.user_id = 42
context.cnx.cursor.return_value = cursor
self.context.session.user_id = 42
self.context_db.cnx.cursor.return_value = cursor
cursor.fetchall.return_value = [(1, 'foo'), (2, 'bar')]
x = kojihub.CG_Importer()
x.get_metadata('default.json', 'cg_importer_json')
@ -75,17 +82,13 @@ class TestCGImporter(unittest.TestCase):
assert x.cg
assert isinstance(x.cg, int)
@mock.patch("kojihub.get_build")
@mock.patch("kojihub.get_user")
@mock.patch('kojihub.context')
@mock.patch("koji.pathinfo.work")
def test_prep_build(self, work, context, get_user, get_build):
work.return_value = os.path.dirname(__file__)
def test_prep_build(self):
self.path_work.return_value = os.path.dirname(__file__)
cursor = mock.MagicMock()
context.cnx.cursor.return_value = cursor
self.context_db.cnx.cursor.return_value = cursor
cursor.fetchall.return_value = [(1, 'foo'), (2, 'bar')]
get_user.return_value = {'id': 123}
get_build.return_value = {}
self.get_user.return_value = self.userinfo
self.get_build.return_value = {}
x = kojihub.CG_Importer()
x.get_metadata('default.json', 'cg_importer_json')
x.assert_cg_access()
@ -93,84 +96,72 @@ class TestCGImporter(unittest.TestCase):
assert x.buildinfo
assert isinstance(x.buildinfo, dict)
@mock.patch('koji.pathinfo.build')
@mock.patch('os.path.lexists')
@mock.patch('koji.util.rmtree')
def test_check_build_dir(self, rmtree, lexists, build):
def test_check_build_dir(self):
path = '/random_path/random_dir'
build.return_value = path
self.path_build.return_value = path
x = kojihub.CG_Importer()
# directory exists
lexists.return_value = True
self.lexists.return_value = True
with self.assertRaises(koji.GenericError):
x.check_build_dir(delete=False)
rmtree.assert_not_called()
self.rmtree.assert_not_called()
# directory exists + delete
lexists.return_value = True
self.lexists.return_value = True
x.check_build_dir(delete=True)
rmtree.assert_called_once_with(path)
self.rmtree.assert_called_once_with(path)
# directory doesn't exist
rmtree.reset_mock()
lexists.return_value = False
self.rmtree.reset_mock()
self.lexists.return_value = False
x.check_build_dir()
rmtree.assert_not_called()
self.rmtree.assert_not_called()
@mock.patch('kojihub.get_build')
@mock.patch("koji.pathinfo.work")
def test_prep_build_exists(self, work, get_build):
work.return_value = os.path.dirname(__file__)
get_build.return_value = True
def test_prep_build_exists(self):
self.path_work.return_value = os.path.dirname(__file__)
self.get_build.return_value = True
x = kojihub.CG_Importer()
x.get_metadata('default.json', 'cg_importer_json')
with self.assertRaises(GenericError):
x.prep_build()
@mock.patch("kojihub.get_user")
@mock.patch('kojihub.get_build')
@mock.patch('kojihub.new_build')
@mock.patch('kojihub.context')
@mock.patch("koji.pathinfo.work")
def test_get_build(self, work, context, new_build_id, get_build, get_user):
work.return_value = os.path.dirname(__file__)
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')]
context.cnx.cursor.return_value = cursor
new_build_id.return_value = 42
get_build.return_value = False
get_user.return_value = {'id': 123}
self.context_db.cnx.cursor.return_value = cursor
self.new_build.return_value = 42
self.get_build.return_value = False
self.get_user.return_value = self.userinfo
x = kojihub.CG_Importer()
x.get_metadata('default.json', 'cg_importer_json')
x.assert_cg_access()
x.prep_build()
x.prepped_outputs = []
get_build.return_value = {'id': 43, 'package_id': 1,
'package_name': 'testpkg',
'name': 'testpkg', 'version': '1.0.1e',
'release': '42.el7', 'epoch': None,
'nvr': 'testpkg-1.0.1-1.fc24',
'state': 'complete', 'task_id': 1,
'owner_id': 1, 'owner_name': 'jvasallo',
'volume_id': 'id-1212', 'volume_name': 'testvolume',
'creation_event_id': '', 'creation_time': '',
'creation_ts': 424242424242,
'start_time': None, 'start_ts': None,
'completion_time': None, 'completion_ts': None,
'source': 'https://example.com', 'extra': {}
}
new_build_id.return_value = 43
self.get_build.return_value = {'id': 43, 'package_id': 1,
'package_name': 'testpkg',
'name': 'testpkg', 'version': '1.0.1e',
'release': '42.el7', 'epoch': None,
'nvr': 'testpkg-1.0.1-1.fc24',
'state': 'complete', 'task_id': 1,
'owner_id': 1, 'owner_name': 'jvasallo',
'volume_id': 'id-1212', 'volume_name': 'testvolume',
'creation_event_id': '', 'creation_time': '',
'creation_ts': 424242424242,
'start_time': None, 'start_ts': None,
'completion_time': None, 'completion_ts': None,
'source': 'https://example.com', 'extra': {}
}
self.new_build.return_value = 43
x.get_build()
assert x.buildinfo
assert isinstance(x.buildinfo, dict)
@mock.patch("koji.pathinfo.build")
@mock.patch("koji.pathinfo.work")
def test_import_metadata(self, work, build):
work.return_value = os.path.dirname(__file__)
build.return_value = self.TMP_PATH
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.get_metadata('default.json', 'cg_importer_json')
x.import_metadata()
@ -280,23 +271,28 @@ class TestCGReservation(unittest.TestCase):
self.inserts = []
self.updates = []
self.context = mock.patch('kojihub.context').start()
self.context.session.user_id = 123456
self.context_db = mock.patch('koji.db.context').start()
self.context_db.session.user_id = 123456
self.mock_cursor = mock.MagicMock()
self.context.cnx.cursor.return_value = self.mock_cursor
self.context_db.cnx.cursor.return_value = self.mock_cursor
self.get_build = mock.patch('kojihub.get_build').start()
self.get_user = mock.patch('kojihub.get_user').start()
self.userinfo = {'id': 123456, 'name': 'username'}
self.new_build = mock.patch('kojihub.new_build').start()
self.lookup_name = mock.patch('kojihub.lookup_name').start()
self.assert_cg = mock.patch('kojihub.assert_cg').start()
self.get_reservation_token = mock.patch('kojihub.get_reservation_token').start()
self.run_callbacks = mock.patch('koji.plugin.run_callbacks').start()
def tearDown(self):
mock.patch.stopall()
@mock.patch("kojihub.new_build")
@mock.patch("kojihub.get_user")
@mock.patch("kojihub.lookup_name")
@mock.patch("kojihub.assert_cg")
def test_init_build_ok(self, assert_cg, lookup_name, get_user, new_build):
assert_cg.return_value = True
lookup_name.return_value = {'id': 21, 'name': 'cg_name'}
get_user.return_value = {'id': 123456, 'name': 'username'}
new_build.return_value = 654
def test_init_build_ok(self):
self.assert_cg.return_value = True
self.lookup_name.return_value = {'id': 21, 'name': 'cg_name'}
self.get_reservation_token.return_value = None
self.get_user.return_value = self.userinfo
self.new_build.return_value = 654
cg = 'content_generator_name'
self.mock_cursor.fetchone.side_effect = [
[333], # get pkg_id
@ -315,8 +311,8 @@ class TestCGReservation(unittest.TestCase):
kojihub.cg_init_build(cg, data)
lookup_name.assert_called_once_with('content_generator', cg, strict=True)
assert_cg.assert_called_once_with(cg)
self.lookup_name.assert_called_once_with('content_generator', cg, strict=True)
self.assert_cg.assert_called_once_with(cg)
self.assertEqual(1, len(self.inserts))
insert = self.inserts[0]
self.assertEqual(insert.table, 'build_reservations')
@ -324,18 +320,12 @@ class TestCGReservation(unittest.TestCase):
self.assertTrue('token' in insert.data)
self.assertEqual(insert.rawdata, {'created': 'NOW()'})
@mock.patch("koji.plugin.run_callbacks")
@mock.patch("kojihub.get_reservation_token")
@mock.patch("kojihub.lookup_name")
@mock.patch("kojihub.get_build")
@mock.patch("kojihub.assert_cg")
def test_uninit_build_ok(self, assert_cg, get_build, lookup_name, get_reservation_token,
run_callbacks):
assert_cg.return_value = True
def test_uninit_build_ok(self):
self.assert_cg.return_value = True
build_id = 1122
cg_id = 888
cg = 'content_generator_name'
get_build.side_effect = [
self.get_build.side_effect = [
{
'id': build_id,
'state': koji.BUILD_STATES['BUILDING'],
@ -349,18 +339,18 @@ class TestCGReservation(unittest.TestCase):
]
token = 'random_token'
get_reservation_token.return_value = {'build_id': build_id, 'token': token}
lookup_name.return_value = {'name': cg, 'id': cg_id}
self.get_reservation_token.return_value = {'build_id': build_id, 'token': token}
self.lookup_name.return_value = {'name': cg, 'id': cg_id}
kojihub.cg_refund_build(cg, build_id, token)
assert_cg.assert_called_once_with(cg)
get_build.assert_has_calls([
self.assert_cg.assert_called_once_with(cg)
self.get_build.assert_has_calls([
mock.call(build_id, strict=True),
mock.call(build_id, strict=True),
])
get_reservation_token.assert_called_once_with(build_id)
lookup_name.assert_called_once_with('content_generator', cg, strict=True)
self.get_reservation_token.assert_called_once_with(build_id)
self.lookup_name.assert_called_once_with('content_generator', cg, strict=True)
self.assertEqual(len(self.updates), 1)
update = self.updates[0]
@ -369,7 +359,7 @@ class TestCGReservation(unittest.TestCase):
self.assertEqual(update.data['state'], koji.BUILD_STATES['FAILED'])
self.assertEqual(update.rawdata, {'completion_time': 'NOW()'})
run_callbacks.assert_has_calls([
self.run_callbacks.assert_has_calls([
mock.call('preBuildStateChange', attribute='state',
old=koji.BUILD_STATES['BUILDING'],
new=koji.BUILD_STATES['FAILED'],

View file

@ -1,5 +1,4 @@
import copy
import json
import mock
import os
import os.path
@ -53,7 +52,7 @@ class TestCompleteImageBuild(unittest.TestCase):
self.pathinfo = koji.PathInfo(self.tempdir)
mock.patch('koji.pathinfo', new=self.pathinfo).start()
self.hostcalls = kojihub.HostExports()
self.context = mock.patch('kojihub.context').start()
self.context_db = mock.patch('koji.db.context').start()
mock.patch('kojihub.Host').start()
self.Task = mock.patch('kojihub.Task').start()
self.Task.return_value.assertHost = mock.MagicMock()
@ -64,22 +63,22 @@ class TestCompleteImageBuild(unittest.TestCase):
mock.patch('kojihub.lookup_name', new=self.my_lookup_name).start()
mock.patch.object(kojihub.BuildRoot, 'load', new=self.my_buildroot_load).start()
mock.patch('kojihub.import_archive_internal',
new=self.my_import_archive_internal).start()
new=self.my_import_archive_internal).start()
self._dml = mock.patch('kojihub._dml').start()
mock.patch('kojihub.build_notification').start()
mock.patch('kojihub.assert_policy').start()
mock.patch('kojihub.check_volume_policy',
return_value={'id':0, 'name': 'DEFAULT'}).start()
return_value={'id': 0, 'name': 'DEFAULT'}).start()
self.set_up_callbacks()
self.rpms = {}
self.inserts = []
self.updates = []
mock.patch.object(kojihub.InsertProcessor, 'execute',
new=make_insert_grabber(self)).start()
new=make_insert_grabber(self)).start()
mock.patch.object(kojihub.BulkInsertProcessor, '_one_insert',
new=make_bulk_insert_grabber(self)).start()
new=make_bulk_insert_grabber(self)).start()
mock.patch.object(kojihub.UpdateProcessor, 'execute',
new=make_update_grabber(self)).start()
new=make_update_grabber(self)).start()
mock.patch('kojihub.nextval', new=self.my_nextval).start()
self.sequences = {}
@ -140,15 +139,14 @@ class TestCompleteImageBuild(unittest.TestCase):
def my_lookup_name(self, table, info, **kw):
if table == 'btype':
return {
'id': 'BTYPEID:%s' % info,
'name': info,
}
'id': 'BTYPEID:%s' % info,
'name': info,
}
else:
raise Exception("Cannot fake call")
def my_get_archive_type(self, *a, **kw):
return dict.fromkeys(['id', 'name', 'description', 'extensions'],
'ARCHIVETYPE')
return dict.fromkeys(['id', 'name', 'description', 'extensions'], 'ARCHIVETYPE')
@staticmethod
def my_buildroot_load(br, id):
@ -156,14 +154,15 @@ class TestCompleteImageBuild(unittest.TestCase):
br.id = id
br.is_standard = True
br.data = {
'br_type': koji.BR_TYPES['STANDARD'],
'id': id,
}
'br_type': koji.BR_TYPES['STANDARD'],
'id': id,
}
def my_import_archive_internal(self, *a, **kw):
# this is kind of odd, but we need this to fake the archiveinfo
share = {}
old_ip = kojihub.InsertProcessor
def my_ip(table, *a, **kw):
if table == 'archiveinfo':
data = kw['data']
@ -171,6 +170,7 @@ class TestCompleteImageBuild(unittest.TestCase):
share['archiveinfo'] = data
# TODO: need to add id
return old_ip(table, *a, **kw)
def my_ga(archive_id, **kw):
return share['archiveinfo']
with mock.patch('kojihub.InsertProcessor', new=my_ip):
@ -190,17 +190,17 @@ class TestCompleteImageBuild(unittest.TestCase):
def test_complete_image_build(self):
self.set_up_files('import_1')
buildinfo = {
'id': 137,
'task_id': 'TASK_ID',
'name': 'some-image',
'version': '1.2.3.4',
'release': '3',
'epoch': None,
'source': None,
'state': koji.BUILD_STATES['BUILDING'],
'volume_id': 0,
'extra': {},
}
'id': 137,
'task_id': 'TASK_ID',
'name': 'some-image',
'version': '1.2.3.4',
'release': '3',
'epoch': None,
'source': None,
'state': koji.BUILD_STATES['BUILDING'],
'volume_id': 0,
'extra': {},
}
image_info = {'build_id': buildinfo['id']}
self.get_build.return_value = buildinfo
self.get_image_build.return_value = image_info
@ -232,7 +232,7 @@ class TestCompleteImageBuild(unittest.TestCase):
'postImport', # build
'preBuildStateChange', # building -> completed
'postBuildStateChange',
]
]
self.assertEqual(cbtypes, cb_expect)
cb_idx = {}
for c in self.callbacks:
@ -246,10 +246,10 @@ class TestCompleteImageBuild(unittest.TestCase):
cb_idx.setdefault(key, [])
cb_idx[key].append(c[2])
key_expect = [
'postBuildStateChange', 'preBuildStateChange',
'preImport:archive', 'postImport:archive',
'preImport:image', 'postImport:image',
]
'postBuildStateChange', 'preBuildStateChange',
'preImport:archive', 'postImport:archive',
'preImport:image', 'postImport:image',
]
self.assertEqual(set(cb_idx.keys()), set(key_expect))
for key in ['preImport:image']:
callbacks = cb_idx[key]

View file

@ -1,5 +1,4 @@
import copy
import json
import mock
import os
import os.path
@ -23,6 +22,7 @@ class TestCompleteMavenBuild(unittest.TestCase):
mock.patch('koji.pathinfo', new=self.pathinfo).start()
self.hostcalls = kojihub.HostExports()
self.context = mock.patch('kojihub.context').start()
self.context_db = mock.patch('koji.db.context').start()
self.context.opts = {'EnableMaven': True}
mock.patch('kojihub.Host').start()
self.Task = mock.patch('kojihub.Task').start()
@ -33,13 +33,13 @@ class TestCompleteMavenBuild(unittest.TestCase):
mock.patch('kojihub.lookup_name', new=self.my_lookup_name).start()
mock.patch.object(kojihub.BuildRoot, 'load', new=self.my_buildroot_load).start()
mock.patch('kojihub.import_archive_internal',
new=self.my_import_archive_internal).start()
mock.patch('kojihub._dml').start()
mock.patch('kojihub._fetchSingle').start()
new=self.my_import_archive_internal).start()
mock.patch('koji.db._dml').start()
mock.patch('koji.db._fetchSingle').start()
mock.patch('kojihub.build_notification').start()
mock.patch('kojihub.assert_policy').start()
mock.patch('kojihub.check_volume_policy',
return_value={'id':0, 'name': 'DEFAULT'}).start()
return_value={'id': 0, 'name': 'DEFAULT'}).start()
self.set_up_callbacks()
def tearDown(self):
@ -65,7 +65,7 @@ class TestCompleteMavenBuild(unittest.TestCase):
shutil.copy(src, dst)
self.maven_data = data
files = open(datadir + '/files', 'rt', encoding='utf-8').readlines()
files = [l.strip() for l in files]
files = [file.strip() for file in files]
self.expected_files = files
def my_lookup_name(self, table, info, **kw):
@ -80,18 +80,20 @@ class TestCompleteMavenBuild(unittest.TestCase):
br.id = id
br.is_standard = True
br.data = {
'br_type': koji.BR_TYPES['STANDARD'],
'id': id,
}
'br_type': koji.BR_TYPES['STANDARD'],
'id': id,
}
def my_import_archive_internal(self, *a, **kw):
# this is kind of odd, but we need this to fake the archiveinfo
share = {}
def my_ip(table, *a, **kw):
if table == 'archiveinfo':
share['archiveinfo'] = kw['data']
# TODO: need to add id
return mock.MagicMock()
def my_ga(archive_id, **kw):
return share['archiveinfo']
with mock.patch('kojihub.InsertProcessor', new=my_ip):
@ -141,7 +143,7 @@ class TestCompleteMavenBuild(unittest.TestCase):
'postImport',
'preBuildStateChange', # building -> completed
'postBuildStateChange',
]
]
self.assertEqual(cbtypes, cb_expect)
cb_idx = {}
@ -157,7 +159,12 @@ class TestCompleteMavenBuild(unittest.TestCase):
key = cbtype
cb_idx.setdefault(key, [])
cb_idx[key].append(c[2])
key_expect = ['postBuildStateChange', 'preBuildStateChange', 'preImport:archive', 'postImport:archive']
key_expect = [
'postBuildStateChange',
'preBuildStateChange',
'preImport:archive',
'postImport:archive'
]
self.assertEqual(set(cb_idx.keys()), set(key_expect))
# in this case, pre and post data is similar
for key in ['preImport:archive', 'postImport:archive']:

View file

@ -15,6 +15,7 @@ class TestCreateMavenBuild(unittest.TestCase):
self.exports = kojihub.RootExports()
self.session = mock.MagicMock()
self.context = mock.patch('kojihub.context').start()
self.context_db = mock.patch('koji.db.context').start()
self.context.session.assertPerm = mock.MagicMock()
self.InsertProcessor = mock.patch('kojihub.InsertProcessor',
side_effect=self.getInsert).start()

View file

@ -28,10 +28,11 @@ class TestCreateTag(unittest.TestCase):
self.verify_name_internal = mock.patch('kojihub.verify_name_internal').start()
self.writeInheritanceData = mock.patch('kojihub._writeInheritanceData').start()
self.context = mock.patch('kojihub.context').start()
self.context_db = mock.patch('koji.db.context').start()
# It seems MagicMock will not automatically handle attributes that
# start with "assert"
self.context.session.assertPerm = mock.MagicMock()
self.context.session.assertLogin = mock.MagicMock()
self.context_db.session.assertLogin = mock.MagicMock()
def tearDown(self):
mock.patch.stopall()
@ -46,8 +47,8 @@ class TestCreateTag(unittest.TestCase):
self.get_tag.side_effect = [None, {'id': 1, 'name': 'parent-tag'}]
self.get_tag_id.return_value = 99
self.verify_name_internal.return_value = None
self.context.event_id = 42
self.context.session.user_id = 23
self.context_db.event_id = 42
self.context_db.session.user_id = 23
self.writeInheritanceData.return_value = None
kojihub.create_tag('newtag', parent='parent-tag')
@ -73,8 +74,8 @@ class TestCreateTag(unittest.TestCase):
self.get_tag.return_value = None
self.get_tag_id.return_value = 99
self.verify_name_internal.return_value = None
self.context.event_id = 42
self.context.session.user_id = 23
self.context_db.event_id = 42
self.context_db.session.user_id = 23
with self.assertRaises(koji.GenericError):
kojihub.create_tag('newtag', arches=u'ěšč')

View file

@ -20,10 +20,11 @@ class TestDeleteTag(unittest.TestCase):
self.updates = []
self.get_tag = mock.patch('kojihub.get_tag').start()
self.context = mock.patch('kojihub.context').start()
self.context_db = mock.patch('koji.db.context').start()
# It seems MagicMock will not automatically handle attributes that
# start with "assert"
self.context.session.assertPerm = mock.MagicMock()
self.context.session.assertLogin = mock.MagicMock()
self.context_db.session.assertLogin = mock.MagicMock()
def tearDown(self):
mock.patch.stopall()
@ -37,8 +38,8 @@ class TestDeleteTag(unittest.TestCase):
def test_good_tag(self):
self.get_tag.return_value = {'id': 'TAGID'}
self.context.event_id = "12345"
self.context.session.user_id = "42"
self.context_db.event_id = "12345"
self.context_db.session.user_id = "42"
data = {'revoker_id': '42', 'revoke_event': '12345'}
kojihub.delete_tag('goodtag')
for u in self.updates:

View file

@ -30,9 +30,10 @@ class TestEditHost(unittest.TestCase):
side_effect=self.getUpdate).start()
self.updates = []
self.context = mock.patch('kojihub.context').start()
self.context_db = mock.patch('koji.db.context').start()
# It seems MagicMock will not automatically handle attributes that
# start with "assert"
self.context.session.assertLogin = mock.MagicMock()
self.context_db.session.assertLogin = mock.MagicMock()
self.context.session.assertPerm = mock.MagicMock()
self.exports = kojihub.RootExports()
self.get_host = mock.patch('kojihub.get_host').start()
@ -94,8 +95,8 @@ class TestEditHost(unittest.TestCase):
def test_edit_host_valid(self):
kojihub.get_host = mock.MagicMock()
kojihub.get_host.return_value = self.hostinfo
self.context.event_id = 42
self.context.session.user_id = 23
self.context_db.event_id = 42
self.context_db.session.user_id = 23
r = self.exports.editHost('hostname', arches='x86_64 i386', capacity=12.0,
comment='comment_new', non_existing_kw='bogus')
@ -140,8 +141,8 @@ class TestEditHost(unittest.TestCase):
def test_edit_host_no_change(self):
kojihub.get_host = mock.MagicMock()
kojihub.get_host.return_value = self.hostinfo
self.context.event_id = 42
self.context.session.user_id = 23
self.context_db.event_id = 42
self.context_db.session.user_id = 23
r = self.exports.editHost('hostname')

View file

@ -36,9 +36,10 @@ class TestEditTag(unittest.TestCase):
self.get_perm_id = mock.patch('kojihub.get_perm_id').start()
self.verify_name_internal = mock.patch('kojihub.verify_name_internal').start()
self.context = mock.patch('kojihub.context').start()
self.context_db = mock.patch('koji.db.context').start()
# It seems MagicMock will not automatically handle attributes that
# start with "assert"
self.context.session.assertLogin = mock.MagicMock()
self.context_db.session.assertLogin = mock.MagicMock()
def tearDown(self):
mock.patch.stopall()
@ -66,8 +67,8 @@ class TestEditTag(unittest.TestCase):
'exD': 4}}
self._singleValue.return_value = None
self.verify_name_internal.return_value = None
self.context.event_id = 42
self.context.session.user_id = 23
self.context_db.event_id = 42
self.context_db.session.user_id = 23
# no1 invoke
kwargs = {
'perm': None,

View file

@ -7,6 +7,7 @@ QP = kojihub.QueryProcessor
IP = kojihub.InsertProcessor
UP = kojihub.UpdateProcessor
class TestGrouplist(unittest.TestCase):
def getQuery(self, *args, **kwargs):
query = QP(*args, **kwargs)
@ -40,6 +41,7 @@ class TestGrouplist(unittest.TestCase):
def setUp(self):
self.context = mock.patch('kojihub.context').start()
self.context_db = mock.patch('koji.db.context').start()
self.get_tag = mock.patch('kojihub.get_tag').start()
self.lookup_tag = mock.patch('kojihub.lookup_tag').start()
self.lookup_group = mock.patch('kojihub.lookup_group').start()
@ -47,38 +49,40 @@ class TestGrouplist(unittest.TestCase):
# It seems MagicMock will not automatically handle attributes that
# start with "assert"
self.context.session.assertPerm = mock.MagicMock()
self.context.session.assertLogin = mock.MagicMock()
self.context_db.session.assertLogin = mock.MagicMock()
self.QueryProcessor = mock.patch('kojihub.QueryProcessor',
side_effect=self.getQuery).start()
side_effect=self.getQuery).start()
self.queries = []
self.InsertProcessor = mock.patch('kojihub.InsertProcessor',
side_effect=self.getInsert).start()
side_effect=self.getInsert).start()
self.inserts = []
self.UpdateProcessor = mock.patch('kojihub.UpdateProcessor',
side_effect=self.getUpdate).start()
side_effect=self.getUpdate).start()
self.updates = []
self.tag = 'tag'
self.group = 'group'
self.taginfo = {'name': self.tag, 'id': 1}
self.groupinfo = {'name': self.group, 'id': 2}
def tearDown(self):
mock.patch.stopall()
def test_grplist_add(self):
tag = 'tag'
group = 'group'
self.get_tag.return_value = {'name': 'tag', 'id': 'tag_id'}
self.lookup_group.return_value = {'name': 'group', 'id': 'group_id'}
self.get_tag.return_value = self.taginfo
self.lookup_group.return_value = self.groupinfo
self.get_tag_groups.return_value = {}
self.context.event_id = 42
self.context.session.user_id = 24
self.context_db.event_id = 42
self.context_db.session.user_id = 24
kojihub.grplist_add(tag, group)
kojihub.grplist_add(self.tag, self.group)
# what was called
self.context.session.assertPerm.assert_called_once_with('tag')
self.get_tag.assert_called_once_with(tag, strict=True)
self.lookup_group.assert_called_once_with(group, create=True)
self.get_tag_groups.assert_called_with('tag_id', inherit=True,
incl_pkgs=False, incl_reqs=False)
self.get_tag.assert_called_once_with(self.tag, strict=True)
self.lookup_group.assert_called_once_with(self.group, create=True)
self.get_tag_groups.assert_called_with(self.taginfo['id'], inherit=True, incl_pkgs=False,
incl_reqs=False)
# db
# revoke
self.assertEqual(len(self.updates), 1)
@ -96,8 +100,8 @@ class TestGrouplist(unittest.TestCase):
'uservisible': True,
'create_event': 42,
'creator_id': 24,
'tag_id': 'tag_id',
'group_id': 'group_id',
'tag_id': self.taginfo['id'],
'group_id': self.groupinfo['id'],
'blocked': False,
}
self.assertEqual(insert.table, 'group_config')
@ -107,37 +111,35 @@ class TestGrouplist(unittest.TestCase):
def test_grplist_add_no_admin(self):
self.context.session.assertPerm.side_effect = koji.GenericError
with self.assertRaises(koji.GenericError):
kojihub.grplist_add('tag', 'group')
self.context.session.assertPerm.assert_called_once_with('tag')
kojihub.grplist_add(self.tag, self.group)
self.context.session.assertPerm.assert_called_once_with(self.tag)
self.assertEqual(len(self.inserts), 0)
self.assertEqual(len(self.updates), 0)
def test_grplist_add_no_tag(self):
self.get_tag.side_effect = koji.GenericError
with self.assertRaises(koji.GenericError):
kojihub.grplist_add('tag', 'group')
self.context.session.assertPerm.assert_called_once_with('tag')
kojihub.grplist_add(self.tag, self.group)
self.context.session.assertPerm.assert_called_once_with(self.tag)
self.assertEqual(len(self.inserts), 0)
self.assertEqual(len(self.updates), 0)
def test_grplist_block(self):
# identical with test_grplist_add except blocked=True
tag = 'tag'
group = 'group'
self.get_tag.return_value = {'name': 'tag', 'id': 'tag_id'}
self.lookup_group.return_value = {'name': 'group', 'id': 'group_id'}
self.get_tag.return_value = self.taginfo
self.lookup_group.return_value = self.groupinfo
self.get_tag_groups.return_value = {}
self.context.event_id = 42
self.context.session.user_id = 24
self.context_db.event_id = 42
self.context_db.session.user_id = 24
kojihub.grplist_block(tag, group)
kojihub.grplist_block(self.tag, self.group)
# what was called
self.context.session.assertPerm.assert_called_once_with('tag')
self.get_tag.assert_called_once_with(tag, strict=True)
self.lookup_group.assert_called_once_with(group, create=True)
self.get_tag_groups.assert_called_with('tag_id', inherit=True,
incl_pkgs=False, incl_reqs=False)
self.get_tag.assert_called_once_with(self.tag, strict=True)
self.lookup_group.assert_called_once_with(self.group, create=True)
self.get_tag_groups.assert_called_with(self.taginfo['id'], inherit=True, incl_pkgs=False,
incl_reqs=False)
# db
# revoke
self.assertEqual(len(self.updates), 1)
@ -155,8 +157,8 @@ class TestGrouplist(unittest.TestCase):
'uservisible': True,
'create_event': 42,
'creator_id': 24,
'tag_id': 'tag_id',
'group_id': 'group_id',
'tag_id': self.taginfo['id'],
'group_id': self.groupinfo['id'],
'blocked': True,
}
self.assertEqual(insert.table, 'group_config')
@ -164,19 +166,17 @@ class TestGrouplist(unittest.TestCase):
self.assertEqual(insert.rawdata, {})
def test_grplist_remove(self):
tag = 'tag'
group = 'group'
self.get_tag.return_value = {'name': 'tag', 'id': 'tag_id'}
self.lookup_group.return_value = {'name': 'group', 'id': 'group_id'}
self.context.event_id = 42
self.context.session.user_id = 24
self.get_tag.return_value = self.taginfo
self.lookup_group.return_value = self.groupinfo
self.context_db.event_id = 42
self.context_db.session.user_id = 24
kojihub.grplist_remove(tag, group)
kojihub.grplist_remove(self.tag, self.group)
# what was called
self.context.session.assertPerm.assert_called_once_with('tag')
self.get_tag.assert_called_once_with(tag, strict=True)
self.lookup_group.assert_called_once_with(group, strict=True)
self.context.session.assertPerm.assert_called_once_with(self.tag)
self.get_tag.assert_called_once_with(self.tag, strict=True)
self.lookup_group.assert_called_once_with(self.group, strict=True)
# db
self.assertEqual(len(self.queries), 1)
@ -197,7 +197,7 @@ class TestGrouplist(unittest.TestCase):
self.reset_db_processors()
with mock.patch('kojihub.QueryProcessor', side_effect=self.getEmptyQuery):
with self.assertRaises(koji.GenericError) as cm:
kojihub.grplist_remove(tag, group)
kojihub.grplist_remove(self.tag, self.group)
self.assertEqual(len(self.queries), 1)
self.assertEqual(len(self.inserts), 0)
@ -209,7 +209,7 @@ class TestGrouplist(unittest.TestCase):
self.reset_db_processors()
with mock.patch('kojihub.QueryProcessor',
side_effect=self.getEmptyQuery):
kojihub.grplist_remove(tag, group, force=True)
kojihub.grplist_remove(self.tag, self.group, force=True)
self.assertEqual(len(self.queries), 0)
self.assertEqual(len(self.inserts), 0)
@ -217,27 +217,23 @@ class TestGrouplist(unittest.TestCase):
def test_grplist_unblock(self):
# identical with test_grplist_add except blocked=True
tag = 'tag'
group = 'group'
self.lookup_tag.return_value = {'name': 'tag', 'id': 'tag_id'}
self.lookup_group.return_value = {'name': 'group', 'id': 'group_id'}
#self.context.event_id = 42
#self.context.session.user_id = 24
self.lookup_tag.return_value = self.taginfo
self.lookup_group.return_value = self.groupinfo
# will fail for non-blocked group
with self.assertRaises(koji.GenericError):
kojihub.grplist_unblock(tag, group)
kojihub.grplist_unblock(self.tag, self.group)
# what was called
self.context.session.assertPerm.assert_called_once_with('tag')
self.lookup_tag.assert_called_once_with(tag, strict=True)
self.lookup_group.assert_called_once_with(group, strict=True)
self.lookup_tag.assert_called_once_with(self.tag, strict=True)
self.lookup_group.assert_called_once_with(self.group, strict=True)
# db
self.assertEqual(len(self.queries), 1)
query = self.queries[0]
self.assertEqual(query.tables, ['group_config'])
self.assertEqual(query.joins,None)
self.assertEqual(query.joins, None)
self.assertEqual(query.clauses,
['active = TRUE', 'group_id=%(grp_id)s', 'tag_id=%(tag_id)s'])
self.assertEqual(len(self.updates), 0)
@ -260,7 +256,7 @@ class TestGrouplist(unittest.TestCase):
}
self.get_tag_groups.return_value = {1: group}
r = kojihub.readTagGroups('tag')
r = kojihub.readTagGroups(self.tag)
self.assertEqual(r, [{'name': 'a', 'packagelist': [], 'grouplist': [], 'blocked': False}])
def test_readTagGroups_blocked(self):
@ -273,10 +269,10 @@ class TestGrouplist(unittest.TestCase):
self.get_tag_groups.return_value = {1: group.copy()}
# without blocked
r = kojihub.readTagGroups('tag')
r = kojihub.readTagGroups(self.tag)
self.assertEqual(r, [])
# with blocked
self.get_tag_groups.return_value = {1: group.copy()}
r = kojihub.readTagGroups('tag', incl_blocked=True)
r = kojihub.readTagGroups(self.tag, incl_blocked=True)
self.assertEqual(r, [{'name': 'a', 'packagelist': [], 'grouplist': [], 'blocked': True}])

View file

@ -22,7 +22,7 @@ class TestImportBuild(unittest.TestCase):
self.check_volume_policy = mock.patch('kojihub.check_volume_policy').start()
self.new_typed_build = mock.patch('kojihub.new_typed_build').start()
self._dml = mock.patch('kojihub._dml').start()
self._dml = mock.patch('koji.db._dml').start()
self._singleValue = mock.patch('kojihub._singleValue').start()
self.get_build = mock.patch('kojihub.get_build').start()
self.add_rpm_sig = mock.patch('kojihub.add_rpm_sig').start()
@ -31,6 +31,7 @@ class TestImportBuild(unittest.TestCase):
self.import_rpm = mock.patch('kojihub.import_rpm').start()
self.QueryProcessor = mock.patch('kojihub.QueryProcessor').start()
self.context = mock.patch('kojihub.context').start()
self.context_db = mock.patch('koji.db.context').start()
self.new_package = mock.patch('kojihub.new_package').start()
self.get_rpm_header = mock.patch('koji.get_rpm_header').start()
self.pathinfo_work = mock.patch('koji.pathinfo.work').start()

View file

@ -10,20 +10,22 @@ import kojihub
class TestImportImageInternal(unittest.TestCase):
def setUp(self):
self.tempdir = tempfile.mkdtemp()
self.context_db = mock.patch('koji.db.context').start()
self.Task = mock.patch('kojihub.Task').start()
self.get_build = mock.patch('kojihub.get_build').start()
self.get_archive_type = mock.patch('kojihub.get_archive_type').start()
self.path_work = mock.patch('koji.pathinfo.work').start()
self.import_archive = mock.patch('kojihub.import_archive').start()
self.build = mock.patch('koji.pathinfo.build').start()
self.get_rpm = mock.patch('kojihub.get_rpm').start()
def tearDown(self):
shutil.rmtree(self.tempdir)
@mock.patch('koji.pathinfo.work')
@mock.patch('kojihub.import_archive')
@mock.patch('kojihub.get_archive_type')
@mock.patch('kojihub.get_build')
@mock.patch('kojihub.Task')
@mock.patch('kojihub.context')
def test_basic(self, context, Task, get_build, get_archive_type, import_archive, work):
def test_basic(self):
task = mock.MagicMock()
task.assertHost = mock.MagicMock()
Task.return_value = task
self.Task.return_value = task
imgdata = {
'arch': 'x86_64',
'task_id': 1,
@ -34,32 +36,24 @@ class TestImportImageInternal(unittest.TestCase):
],
}
cursor = mock.MagicMock()
context.cnx.cursor.return_value = cursor
context.session.host_id = 42
get_build.return_value = {
self.context_db.cnx.cursor.return_value = cursor
self.context_db.session.host_id = 42
self.get_build.return_value = {
'id': 2,
'name': 'name',
'version': 'version',
'release': 'release',
}
get_archive_type.return_value = 4
work.return_value = self.tempdir
self.get_archive_type.return_value = 4
self.path_work.return_value = self.tempdir
os.makedirs(self.tempdir + "/tasks/1/1")
kojihub.importImageInternal(task_id=1, build_info=get_build.return_value, imgdata=imgdata)
kojihub.importImageInternal(
task_id=1, build_info=self.get_build.return_value, imgdata=imgdata)
@mock.patch('kojihub.get_rpm')
@mock.patch('koji.pathinfo.build')
@mock.patch('koji.pathinfo.work')
@mock.patch('kojihub.import_archive')
@mock.patch('kojihub.get_archive_type')
@mock.patch('kojihub.get_build')
@mock.patch('kojihub.Task')
@mock.patch('kojihub.context')
def test_with_rpm(self, context, Task, get_build, get_archive_type, import_archive, build,
work, get_rpm):
def test_with_rpm(self):
task = mock.MagicMock()
task.assertHost = mock.MagicMock()
Task.return_value = task
self.Task.return_value = task
rpm = {
# 'location': 'foo',
'id': 6,
@ -87,14 +81,14 @@ class TestImportImageInternal(unittest.TestCase):
'id': 2
}
cursor = mock.MagicMock()
context.cnx.cursor.return_value = cursor
context.session.host_id = 42
get_build.return_value = build_info
get_rpm.return_value = rpm
get_archive_type.return_value = 4
work.return_value = self.tempdir
build.return_value = self.tempdir
import_archive.return_value = {
self.context_db.cnx.cursor.return_value = cursor
self.context_db.session.host_id = 42
self.get_build.return_value = build_info
self.get_rpm.return_value = rpm
self.get_archive_type.return_value = 4
self.path_work.return_value = self.tempdir
self.build.return_value = self.tempdir
self.import_archive.return_value = {
'id': 9,
'filename': self.tempdir + '/foo.archive',
}

View file

@ -22,6 +22,7 @@ class TestImportRPM(unittest.TestCase):
pass
self.context = mock.patch('kojihub.context').start()
self.context.session.assertPerm = mock.MagicMock()
self.context_db = mock.patch('koji.db.context').start()
self.cursor = mock.MagicMock()
self.rpm_header_retval = {
@ -40,7 +41,7 @@ class TestImportRPM(unittest.TestCase):
self.get_build = mock.patch('kojihub.get_build').start()
self.get_rpm_header = mock.patch('koji.get_rpm_header').start()
self.new_typed_build = mock.patch('kojihub.new_typed_build').start()
self._dml = mock.patch('kojihub._dml').start()
self._dml = mock.patch('koji.db._dml').start()
self._singleValue = mock.patch('kojihub._singleValue').start()
self.os_path_exists = mock.patch('os.path.exists').start()
self.os_path_basename = mock.patch('os.path.basename').start()
@ -172,7 +173,7 @@ class TestImportRPM(unittest.TestCase):
def test_non_exist_build(self):
self.cursor.fetchone.return_value = None
self.context.cnx.cursor.return_value = self.cursor
self.context_db.cnx.cursor.return_value = self.cursor
retval = copy.copy(self.rpm_header_retval)
retval.update({
'filename': 'name-version-release.arch.rpm',

View file

@ -16,6 +16,7 @@ class DummyExports(object):
class TestMulticall(unittest.TestCase):
def test_multicall(self):
self.context_db = mock.patch('koji.db.context').start()
kojixmlrpc.kojihub = mock.MagicMock()
kojixmlrpc.context.opts = mock.MagicMock()
kojixmlrpc.context.session = mock.MagicMock()

View file

@ -20,37 +20,43 @@ class TestRemoveHostFromChannel(unittest.TestCase):
side_effect=self.getUpdate).start()
self.updates = []
self.context = mock.patch('kojihub.context').start()
self.context_db = mock.patch('koji.db.context').start()
# It seems MagicMock will not automatically handle attributes that
# start with "assert"
self.context.session.assertLogin = mock.MagicMock()
self.context_db.session.assertLogin = mock.MagicMock()
self.context.session.assertPerm = mock.MagicMock()
self.context.event_id = 42
self.context.session.user_id = 23
self.context_db.event_id = 42
self.context_db.session.user_id = 23
self.context.opts = {'HostPrincipalFormat': '-%s-'}
self.exports = kojihub.RootExports()
self.list_channels = mock.patch('kojihub.list_channels').start()
self.get_channel_id = mock.patch('kojihub.get_channel_id').start()
self.get_host = mock.patch('kojihub.get_host').start()
self.hostname = 'hostname'
self.hostinfo = {'id': 123, 'name': self.hostname}
self.channel_id = 234
self.channelname = 'channelname'
self.list_channels_output = [{'id': self.channel_id, 'name': self.channelname}]
def tearDown(self):
mock.patch.stopall()
@mock.patch('kojihub.list_channels')
@mock.patch('kojihub.get_channel_id')
@mock.patch('kojihub.get_host')
def test_valid(self, get_host, get_channel_id, list_channels):
get_host.return_value = {'id': 123, 'name': 'hostname'}
get_channel_id.return_value = 234
list_channels.return_value = [{'id': 234, 'name': 'channelname'}]
def test_valid(self):
self.get_host.return_value = self.hostinfo
self.get_channel_id.return_value = self.channel_id
self.list_channels.return_value = self.list_channels_output
kojihub.remove_host_from_channel('hostname', 'channelname')
kojihub.remove_host_from_channel(self.hostname, self.channelname)
get_host.assert_called_once_with('hostname')
get_channel_id.assert_called_once_with('channelname')
list_channels.assert_called_once_with(123)
self.get_host.assert_called_once_with(self.hostname)
self.get_channel_id.assert_called_once_with(self.channelname)
self.list_channels.assert_called_once_with(self.hostinfo['id'])
self.assertEqual(len(self.updates), 1)
update = self.updates[0]
values = {
'host_id': 123,
'channel_id': 234,
'host_id': self.hostinfo['id'],
'channel_id': self.channel_id,
}
clauses = [
'host_id = %(host_id)i AND channel_id = %(channel_id)i',
@ -60,45 +66,36 @@ class TestRemoveHostFromChannel(unittest.TestCase):
self.assertEqual(update.values, values)
self.assertEqual(update.clauses, clauses)
@mock.patch('kojihub.list_channels')
@mock.patch('kojihub.get_channel_id')
@mock.patch('kojihub.get_host')
def test_wrong_host(self, get_host, get_channel_id, list_channels):
get_host.return_value = None
def test_wrong_host(self):
self.get_host.return_value = None
with self.assertRaises(koji.GenericError):
kojihub.remove_host_from_channel('hostname', 'channelname')
kojihub.remove_host_from_channel(self.hostname, self.channelname)
get_host.assert_called_once_with('hostname')
self.get_host.assert_called_once_with(self.hostname)
self.assertEqual(len(self.updates), 0)
@mock.patch('kojihub.list_channels')
@mock.patch('kojihub.get_channel_id')
@mock.patch('kojihub.get_host')
def test_wrong_channel(self, get_host, get_channel_id, list_channels):
get_host.return_value = {'id': 123, 'name': 'hostname'}
get_channel_id.return_value = None
list_channels.return_value = [{'id': 234, 'name': 'channelname'}]
def test_wrong_channel(self):
self.get_host.return_value = self.hostinfo
self.get_channel_id.return_value = None
self.list_channels.return_value = self.list_channels_output
with self.assertRaises(koji.GenericError):
kojihub.remove_host_from_channel('hostname', 'channelname')
kojihub.remove_host_from_channel(self.hostname, self.channelname)
get_host.assert_called_once_with('hostname')
get_channel_id.assert_called_once_with('channelname')
self.get_host.assert_called_once_with(self.hostname)
self.get_channel_id.assert_called_once_with(self.channelname)
self.assertEqual(len(self.updates), 0)
@mock.patch('kojihub.list_channels')
@mock.patch('kojihub.get_channel_id')
@mock.patch('kojihub.get_host')
def test_missing_record(self, get_host, get_channel_id, list_channels):
get_host.return_value = {'id': 123, 'name': 'hostname'}
get_channel_id.return_value = 234
list_channels.return_value = []
def test_missing_record(self):
self.get_host.return_value = self.hostinfo
self.get_channel_id.return_value = self.channel_id
self.list_channels.return_value = []
with self.assertRaises(koji.GenericError):
kojihub.remove_host_from_channel('hostname', 'channelname')
kojihub.remove_host_from_channel(self.hostname, self.channelname)
get_host.assert_called_once_with('hostname')
get_channel_id.assert_called_once_with('channelname')
list_channels.assert_called_once_with(123)
self.get_host.assert_called_once_with(self.hostname)
self.get_channel_id.assert_called_once_with(self.channelname)
self.list_channels.assert_called_once_with(self.hostinfo['id'])
self.assertEqual(len(self.updates), 0)

View file

@ -29,9 +29,10 @@ class TestSetHostEnabled(unittest.TestCase):
side_effect=self.getUpdate).start()
self.updates = []
self.context = mock.patch('kojihub.context').start()
self.context_db = mock.patch('koji.db.context').start()
# It seems MagicMock will not automatically handle attributes that
# start with "assert"
self.context.session.assertLogin = mock.MagicMock()
self.context_db.session.assertLogin = mock.MagicMock()
self.context.session.assertPerm = mock.MagicMock()
self.exports = kojihub.RootExports()
@ -61,8 +62,8 @@ class TestSetHostEnabled(unittest.TestCase):
'enabled': False,
}
kojihub.get_host.return_value = hostinfo
self.context.event_id = 42
self.context.session.user_id = 23
self.context_db.event_id = 42
self.context_db.session.user_id = 23
self.exports.enableHost('hostname')
@ -113,8 +114,8 @@ class TestSetHostEnabled(unittest.TestCase):
'enabled': True,
}
kojihub.get_host.return_value = hostinfo
self.context.event_id = 42
self.context.session.user_id = 23
self.context_db.event_id = 42
self.context_db.session.user_id = 23
self.exports.disableHost('hostname')

View file

@ -50,32 +50,37 @@ class TestTagBuild(unittest.TestCase):
self.check_tag_access = mock.patch('kojihub.check_tag_access').start()
self.writeInheritanceData = mock.patch('kojihub.writeInheritanceData').start()
self.context = mock.patch('kojihub.context').start()
self.context_db = mock.patch('koji.db.context').start()
# It seems MagicMock will not automatically handle attributes that
# start with "assert"
self.context.session.assertPerm = mock.MagicMock()
self.context.session.assertLogin = mock.MagicMock()
def tearDown(self):
mock.patch.stopall()
def test_simple_tag(self):
self.check_tag_access.return_value = (True, False, "")
self.get_build.return_value = {
self.context_db.session.assertLogin = mock.MagicMock()
self.buildinfo = {
'id': 1,
'name': 'name',
'version': 'version',
'release': 'release',
'state': koji.BUILD_STATES['COMPLETE'],
}
self.get_tag.return_value = {
self.taginfo = {
'id': 777,
'name': 'tag',
}
self.get_user.return_value = {
self.userinfo = {
'id': 999,
'name': 'user',
}
self.context.event_id = 42
self.event_id = 42
def tearDown(self):
mock.patch.stopall()
def test_simple_tag(self):
self.check_tag_access.return_value = (True, False, "")
self.get_build.return_value = self.buildinfo
self.get_tag.return_value = self.taginfo
self.get_user.return_value = self.userinfo
self.context_db.event_id = self.event_id
# set return for the already tagged check
self.query_executeOne.return_value = None
@ -91,10 +96,10 @@ class TestTagBuild(unittest.TestCase):
insert = self.inserts[0]
self.assertEqual(insert.table, 'tag_listing')
values = {
'build_id': 1,
'create_event': 42,
'creator_id': 999,
'tag_id': 777
'build_id': self.buildinfo['id'],
'create_event': self.event_id,
'creator_id': self.userinfo['id'],
'tag_id': self.taginfo['id']
}
self.assertEqual(insert.data, values)
self.assertEqual(insert.rawdata, {})
@ -102,30 +107,18 @@ class TestTagBuild(unittest.TestCase):
def test_simple_tag_with_user(self):
self.check_tag_access.return_value = (True, False, "")
self.get_build.return_value = {
'id': 1,
'name': 'name',
'version': 'version',
'release': 'release',
'state': koji.BUILD_STATES['COMPLETE'],
}
self.get_tag.return_value = {
'id': 777,
'name': 'tag',
}
self.get_user.return_value = {
'id': 999,
'name': 'user',
}
self.context.event_id = 42
self.get_build.return_value = self.buildinfo
self.get_tag.return_value = self.taginfo
self.get_user.return_value = self.userinfo
self.context_db.event_id = self.event_id
# set return for the already tagged check
self.query_executeOne.return_value = None
# call it
kojihub._tag_build('sometag', 'name-version-release', user_id=999)
kojihub._tag_build('sometag', 'name-version-release', user_id=self.userinfo['id'])
self.get_tag.called_once_with('sometag', strict=True)
self.get_user.called_one_with(999, strict=True)
self.get_user.called_one_with(self.userinfo['id'], strict=True)
self.get_build.called_once_with('name-version-release', strict=True)
self.context.session.assertPerm.assert_not_called()
@ -134,10 +127,10 @@ class TestTagBuild(unittest.TestCase):
insert = self.inserts[0]
self.assertEqual(insert.table, 'tag_listing')
values = {
'build_id': 1,
'create_event': 42,
'creator_id': 999,
'tag_id': 777
'build_id': self.buildinfo['id'],
'create_event': self.event_id,
'creator_id': self.userinfo['id'],
'tag_id': self.taginfo['id']
}
self.assertEqual(insert.data, values)
self.assertEqual(insert.rawdata, {})
@ -145,22 +138,10 @@ class TestTagBuild(unittest.TestCase):
def test_simple_untag(self):
self.check_tag_access.return_value = (True, False, "")
self.get_build.return_value = {
'id': 1,
'name': 'name',
'version': 'version',
'release': 'release',
'state': koji.BUILD_STATES['COMPLETE'],
}
self.get_tag.return_value = {
'id': 777,
'name': 'tag',
}
self.get_user.return_value = {
'id': 999,
'name': 'user',
}
self.context.event_id = 42
self.get_build.return_value = self.buildinfo
self.get_tag.return_value = self.taginfo
self.get_user.return_value = self.userinfo
self.context_db.event_id = self.event_id
# set return for the already tagged check
self.query_executeOne.return_value = None
@ -177,8 +158,8 @@ class TestTagBuild(unittest.TestCase):
update = self.updates[0]
self.assertEqual(update.table, 'tag_listing')
values = {
'build_id': 1,
'tag_id': 777
'build_id': self.buildinfo['id'],
'tag_id': self.taginfo['id']
}
data = {
'revoke_event': 42,
@ -191,30 +172,18 @@ class TestTagBuild(unittest.TestCase):
def test_simple_untag_with_user(self):
self.check_tag_access.return_value = (True, False, "")
self.get_build.return_value = {
'id': 1,
'name': 'name',
'version': 'version',
'release': 'release',
'state': koji.BUILD_STATES['COMPLETE'],
}
self.get_tag.return_value = {
'id': 777,
'name': 'tag',
}
self.get_user.return_value = {
'id': 999,
'name': 'user',
}
self.context.event_id = 42
self.get_build.return_value = self.buildinfo
self.get_tag.return_value = self.taginfo
self.get_user.return_value = self.userinfo
self.context_db.event_id = self.event_id
# set return for the already tagged check
self.query_executeOne.return_value = None
# call it
kojihub._untag_build('sometag', 'name-version-release', user_id=999)
kojihub._untag_build('sometag', 'name-version-release', user_id=self.userinfo['id'])
self.get_tag.called_once_with('sometag', strict=True)
self.get_user.called_one_with(999, strict=True)
self.get_user.called_one_with(self.userinfo['id'], strict=True)
self.get_build.called_once_with('name-version-release', strict=True)
self.context.session.assertPerm.assert_not_called()
self.assertEqual(len(self.inserts), 0)
@ -224,8 +193,8 @@ class TestTagBuild(unittest.TestCase):
update = self.updates[0]
self.assertEqual(update.table, 'tag_listing')
values = {
'build_id': 1,
'tag_id': 777
'build_id': self.buildinfo['id'],
'tag_id': self.taginfo['id']
}
data = {
'revoke_event': 42,

View file

@ -32,12 +32,13 @@ class TestGrouplist(unittest.TestCase):
def setUp(self):
self.context = mock.patch('kojihub.context').start()
self.context_db = mock.patch('koji.db.context').start()
self.get_user = mock.patch('kojihub.get_user').start()
self.verify_name_internal = mock.patch('kojihub.verify_name_internal').start()
# It seems MagicMock will not automatically handle attributes that
# start with "assert"
self.context.session.assertPerm = mock.MagicMock()
self.context.session.assertLogin = mock.MagicMock()
self.context_db.session.assertLogin = mock.MagicMock()
self.QueryProcessor = mock.patch('kojihub.QueryProcessor',
side_effect=self.getQuery).start()

View file

@ -6,6 +6,12 @@ import kojihub
class TestInsertProcessor(unittest.TestCase):
def setUp(self):
self.context_db = mock.patch('koji.db.context').start()
def tearDown(self):
mock.patch.stopall()
def test_basic_instantiation(self):
proc = kojihub.InsertProcessor('sometable')
actual = str(proc)
@ -18,43 +24,40 @@ class TestInsertProcessor(unittest.TestCase):
expected = 'INSERT INTO sometable (foo) VALUES (%(foo)s)'
self.assertEqual(actual, expected)
@mock.patch('kojihub.context')
def test_simple_execution_with_iterate(self, context):
def test_simple_execution_with_iterate(self):
cursor = mock.MagicMock()
context.cnx.cursor.return_value = cursor
self.context_db.cnx.cursor.return_value = cursor
proc = kojihub.InsertProcessor('sometable', data={'foo': 'bar'})
proc.execute()
cursor.execute.assert_called_once_with(
'INSERT INTO sometable (foo) VALUES (%(foo)s)',
{'foo': 'bar'}, log_errors=True)
@mock.patch('kojihub.context')
def test_make_create(self, context):
def test_make_create(self,):
cursor = mock.MagicMock()
context.cnx.cursor.return_value = cursor
context.session.assertLogin = mock.MagicMock()
self.context_db.cnx.cursor.return_value = cursor
self.context_db.session.assertLogin = mock.MagicMock()
proc = kojihub.InsertProcessor('sometable', data={'foo': 'bar'})
proc.make_create(event_id=1, user_id=2)
self.assertEqual(proc.data['create_event'], 1)
self.assertEqual(proc.data['creator_id'], 2)
proc.make_create(user_id=2)
self.assertEqual(proc.data['create_event'], context.event_id)
self.assertEqual(proc.data['create_event'], self.context_db.event_id)
self.assertEqual(proc.data['creator_id'], 2)
proc.make_create(event_id=1)
self.assertEqual(proc.data['create_event'], 1)
self.assertEqual(proc.data['creator_id'], context.session.user_id)
self.assertEqual(proc.data['creator_id'], self.context_db.session.user_id)
proc.make_create()
self.assertEqual(proc.data['create_event'], context.event_id)
self.assertEqual(proc.data['creator_id'], context.session.user_id)
self.assertEqual(proc.data['create_event'], self.context_db.event_id)
self.assertEqual(proc.data['creator_id'], self.context_db.session.user_id)
@mock.patch('kojihub.context')
def test_dup_check(self, context):
def test_dup_check(self):
cursor = mock.MagicMock()
context.cnx.cursor.return_value = cursor
context.session.assertLogin = mock.MagicMock()
self.context_db.cnx.cursor.return_value = cursor
self.context_db.session.assertLogin = mock.MagicMock()
proc = kojihub.InsertProcessor('sometable', data={'foo': 'bar'})
proc.dup_check()
@ -76,10 +79,9 @@ class TestInsertProcessor(unittest.TestCase):
result = proc.dup_check()
self.assertEqual(result, None)
@mock.patch('kojihub.context')
def test_raw_data(self, context):
def test_raw_data(self):
cursor = mock.MagicMock()
context.cnx.cursor.return_value = cursor
self.context_db.cnx.cursor.return_value = cursor
proc = kojihub.InsertProcessor('sometable', rawdata={'foo': '\'bar\''})
result = proc.dup_check()
self.assertEqual(result, None)
@ -89,6 +91,12 @@ class TestInsertProcessor(unittest.TestCase):
class TestBulkInsertProcessor(unittest.TestCase):
def setUp(self):
self.context_db = mock.patch('koji.db.context').start()
def tearDown(self):
mock.patch.stopall()
def test_basic_instantiation(self):
proc = kojihub.BulkInsertProcessor('sometable')
actual = str(proc)
@ -106,10 +114,9 @@ class TestBulkInsertProcessor(unittest.TestCase):
actual = str(proc)
self.assertEqual(actual, expected)
@mock.patch('kojihub.context')
def test_simple_execution(self, context):
def test_simple_execution(self):
cursor = mock.MagicMock()
context.cnx.cursor.return_value = cursor
self.context_db.cnx.cursor.return_value = cursor
proc = kojihub.BulkInsertProcessor('sometable', data=[{'foo': 'bar'}])
proc.execute()
cursor.execute.assert_called_once_with(
@ -128,10 +135,9 @@ class TestBulkInsertProcessor(unittest.TestCase):
log_errors=True
)
@mock.patch('kojihub.context')
def test_bulk_execution(self, context):
def test_bulk_execution(self):
cursor = mock.MagicMock()
context.cnx.cursor.return_value = cursor
self.context_db.cnx.cursor.return_value = cursor
proc = kojihub.BulkInsertProcessor('sometable', data=[{'foo': 'bar1'}])
proc.add_record(foo='bar2')
@ -166,10 +172,9 @@ class TestBulkInsertProcessor(unittest.TestCase):
str(proc)
self.assertEqual(cm.exception.args[0], 'Missing value foo2 in BulkInsert')
@mock.patch('kojihub.context')
def test_batch_execution(self, context):
def test_batch_execution(self):
cursor = mock.MagicMock()
context.cnx.cursor.return_value = cursor
self.context_db.cnx.cursor.return_value = cursor
proc = kojihub.BulkInsertProcessor('sometable', data=[{'foo': 'bar1'}], batch=2)
proc.add_record(foo='bar2')
@ -185,10 +190,9 @@ class TestBulkInsertProcessor(unittest.TestCase):
mock.call('INSERT INTO sometable (foo) VALUES (%(foo0)s)',
{'foo0': 'bar3'}, log_errors=True))
@mock.patch('kojihub.context')
def test_no_batch_execution(self, context):
def test_no_batch_execution(self):
cursor = mock.MagicMock()
context.cnx.cursor.return_value = cursor
self.context_db.cnx.cursor.return_value = cursor
proc = kojihub.BulkInsertProcessor('sometable', data=[{'foo': 'bar1'}], batch=0)
proc.add_record(foo='bar2')

View file

@ -29,6 +29,7 @@ class TestQueryProcessor(unittest.TestCase):
)
self.original_chunksize = kojihub.QueryProcessor.iterchunksize
kojihub.QueryProcessor.iterchunksize = 2
self.context_db = mock.patch('koji.db.context').start()
def tearDown(self):
kojihub.QueryProcessor.iterchunksize = self.original_chunksize
@ -71,19 +72,17 @@ class TestQueryProcessor(unittest.TestCase):
" ORDER BY something OFFSET 10 LIMIT 3"
self.assertEqual(actual, expected)
@mock.patch('kojihub.context')
def test_simple_with_execution(self, context):
def test_simple_with_execution(self):
cursor = mock.MagicMock()
context.cnx.cursor.return_value = cursor
self.context_db.cnx.cursor.return_value = cursor
proc = kojihub.QueryProcessor(**self.simple_arguments)
proc.execute()
cursor.execute.assert_called_once_with(
'\nSELECT something\n FROM awesome\n\n\n \n \n\n \n', {})
@mock.patch('kojihub.context')
def test_simple_count_with_execution(self, context):
def test_simple_count_with_execution(self):
cursor = mock.MagicMock()
context.cnx.cursor.return_value = cursor
self.context_db.cnx.cursor.return_value = cursor
cursor.fetchall.return_value = [('some count',)]
args = self.simple_arguments.copy()
args['opts'] = {'countOnly': True}
@ -103,10 +102,9 @@ class TestQueryProcessor(unittest.TestCase):
' FROM awesome\n\n\n GROUP BY id\n \n\n \n) numrows', {})
self.assertEqual(results, 'some count')
@mock.patch('kojihub.context')
def test_simple_execution_with_iterate(self, context):
def test_simple_execution_with_iterate(self):
cursor = mock.MagicMock()
context.cnx.cursor.return_value = cursor
self.context_db.cnx.cursor.return_value = cursor
cursor.fetchall.return_value = [
('value number 1',),
('value number 2',),

View file

@ -8,7 +8,8 @@ import kojihub
class TestSavepoint(unittest.TestCase):
def setUp(self):
self.dml = mock.patch('kojihub._dml').start()
self.dml = mock.patch('koji.db._dml').start()
self.context_db = mock.patch('koji.db.context').start()
def tearDown(self):
mock.patch.stopall()

View file

@ -21,10 +21,10 @@ class TestUpdateProcessor(unittest.TestCase):
expected = {'data.foo': 'bar'}
self.assertEqual(actual, expected)
@mock.patch('kojihub.context')
def test_simple_execution_with_iterate(self, context):
@mock.patch('koji.db.context')
def test_simple_execution_with_iterate(self, context_db):
cursor = mock.MagicMock()
context.cnx.cursor.return_value = cursor
context_db.cnx.cursor.return_value = cursor
proc = kojihub.UpdateProcessor('sometable', data={'foo': 'bar'})
proc.execute()
cursor.execute.assert_called_once_with(