From 41edab7fa27a592f4a8dc44f0015124977409021 Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Apr 30 2018 15:35:25 +0000 Subject: host history tests --- diff --git a/hub/kojihub.py b/hub/kojihub.py index c1b6a22..bd82d46 100644 --- a/hub/kojihub.py +++ b/hub/kojihub.py @@ -4545,7 +4545,7 @@ def get_host(hostInfo, strict=False, event=None): } clauses = [eventCondition(event, table='host_config')] - if isinstance(hostInfo, int) or isinstance(hostInfo, long): + if isinstance(hostInfo, (int, long)): clauses.append("id = %(hostInfo)i") elif isinstance(hostInfo, str): clauses.append("name = %(hostInfo)s") @@ -10613,7 +10613,7 @@ class RootExports(object): #host entry hostID = _singleValue("SELECT nextval('host_id_seq')", strict=True) insert = "INSERT INTO host (id, user_id, name) VALUES (%(hostID)i, %(userID)i, %(hostname)s" - _dml(insert, locals()) + _dml(insert, dslice(locals(), ('hostID', 'userID', 'hostname'))) insert = InsertProcessor('host_config') insert.set(host_id=hostID, arches=" ".join(arches)) @@ -10623,7 +10623,7 @@ class RootExports(object): #host_channels entry insert = """INSERT INTO host_channels (host_id, channel_id) VALUES (%(hostID)i, %(default_channel)i)""" - _dml(insert, locals()) + _dml(insert, dslice(locals(), ('hostID', 'default_channel'))) return hostID def enableHost(self, hostname): diff --git a/tests/test_hub/test_add_host.py b/tests/test_hub/test_add_host.py new file mode 100644 index 0000000..455a53f --- /dev/null +++ b/tests/test_hub/test_add_host.py @@ -0,0 +1,79 @@ +import unittest +import mock + +import koji +import kojihub + +UP = kojihub.UpdateProcessor +IP = kojihub.InsertProcessor + + +class TestAddHost(unittest.TestCase): + def getInsert(self, *args, **kwargs): + insert = IP(*args, **kwargs) + insert.execute = mock.MagicMock() + self.inserts.append(insert) + return insert + + def getUpdate(self, *args, **kwargs): + update = UP(*args, **kwargs) + update.execute = mock.MagicMock() + self.updates.append(update) + return update + + def setUp(self): + self.InsertProcessor = mock.patch('kojihub.InsertProcessor', + side_effect=self.getInsert).start() + self.inserts = [] + self.UpdateProcessor = mock.patch('kojihub.UpdateProcessor', + side_effect=self.getUpdate).start() + self.updates = [] + self.context = mock.patch('kojihub.context').start() + # It seems MagicMock will not automatically handle attributes that + # start with "assert" + self.context.session.assertLogin = mock.MagicMock() + self.context.session.assertPerm = mock.MagicMock() + self.context.opts = {'HostPrincipalFormat': '-%s-'} + self.exports = kojihub.RootExports() + + def tearDown(self): + mock.patch.stopall() + + @mock.patch('kojihub._dml') + @mock.patch('kojihub.get_host') + @mock.patch('kojihub._singleValue') + def test_add_host_exists(self, _singleValue, get_host, _dml): + get_host.return_value = {'id': 123} + with self.assertRaises(koji.GenericError): + self.exports.addHost('hostname', ['i386', 'x86_64']) + _dml.assert_not_called() + get_host.assert_called_once_with('hostname') + _singleValue.assert_not_called() + + @mock.patch('kojihub._dml') + @mock.patch('kojihub.get_host') + @mock.patch('kojihub._singleValue') + def test_add_host_valid(self, _singleValue, get_host, _dml): + get_host.return_value = {} + _singleValue.side_effect = [333, 12] + self.context.session.createUser.return_value = 456 + + r = self.exports.addHost('hostname', ['i386', 'x86_64']) + self.assertEqual(r, 12) + + self.context.session.assertPerm.assert_called_once_with('admin') + kojihub.get_host.assert_called_once_with('hostname') + self.context.session.createUser.assert_called_once_with('hostname', + usertype=koji.USERTYPES['HOST'], krb_principal='-hostname-') + self.assertEqual(_singleValue.call_count, 2) + _singleValue.assert_has_calls([ + mock.call("SELECT id FROM channels WHERE name = 'default'"), + mock.call("SELECT nextval('host_id_seq')", strict=True) + ]) + self.assertEqual(_dml.call_count, 2) + _dml.assert_has_calls([ + mock.call("INSERT INTO host (id, user_id, name) VALUES (%(hostID)i, %(userID)i, %(hostname)s", + {'hostID': 12, 'userID': 456, 'hostname': 'hostname'}), + mock.call("""INSERT INTO host_channels (host_id, channel_id)\n VALUES (%(hostID)i, %(default_channel)i)""", + {'hostID': 12, 'default_channel': 333}) + ]) diff --git a/tests/test_hub/test_edit_host.py b/tests/test_hub/test_edit_host.py new file mode 100644 index 0000000..6759a5a --- /dev/null +++ b/tests/test_hub/test_edit_host.py @@ -0,0 +1,127 @@ +import unittest +import mock + +import koji +import kojihub + +UP = kojihub.UpdateProcessor +IP = kojihub.InsertProcessor + + +class TestSetHostEnabled(unittest.TestCase): + def getInsert(self, *args, **kwargs): + insert = IP(*args, **kwargs) + insert.execute = mock.MagicMock() + self.inserts.append(insert) + return insert + + def getUpdate(self, *args, **kwargs): + update = UP(*args, **kwargs) + update.execute = mock.MagicMock() + self.updates.append(update) + return update + + def setUp(self): + self.InsertProcessor = mock.patch('kojihub.InsertProcessor', + side_effect=self.getInsert).start() + self.inserts = [] + self.UpdateProcessor = mock.patch('kojihub.UpdateProcessor', + side_effect=self.getUpdate).start() + self.updates = [] + self.context = mock.patch('kojihub.context').start() + # It seems MagicMock will not automatically handle attributes that + # start with "assert" + self.context.session.assertLogin = mock.MagicMock() + self.context.session.assertPerm = mock.MagicMock() + self.exports = kojihub.RootExports() + + def tearDown(self): + mock.patch.stopall() + + def test_edit_host_missing(self): + kojihub.get_host = mock.MagicMock() + kojihub.get_host.side_effect = koji.GenericError + with self.assertRaises(koji.GenericError): + self.exports.editHost('hostname') + kojihub.get_host.assert_called_once_with('hostname', strict=True) + self.assertEqual(self.inserts, []) + self.assertEqual(self.updates, []) + + def test_edit_host_valid(self): + kojihub.get_host = mock.MagicMock() + kojihub.get_host.return_value = { + 'id': 123, + 'user_id': 234, + 'name': 'hostname', + 'arches': ['x86_64'], + 'capacity': 100.0, + 'description': 'description', + 'comment': 'comment', + 'enabled': False, + } + self.context.event_id = 42 + self.context.session.user_id = 23 + + r = self.exports.editHost('hostname', arches=['x86_64', 'i386'], + capacity=12.0, comment='comment_new', non_existing_kw='bogus') + + self.assertTrue(r) + kojihub.get_host.assert_called_once_with('hostname', strict=True) + + # revoke + self.assertEqual(len(self.updates), 1) + values = kojihub.get_host.return_value + clauses = ['host_id = %(id)i', 'active = TRUE'] + revoke_data = { + 'revoke_event': 42, + 'revoker_id': 23 + } + revoke_rawdata = {'active': 'NULL'} + update = self.updates[0] + self.assertEqual(update.table, 'host_config') + self.assertEqual(update.values, values) + self.assertEqual(update.clauses, clauses) + self.assertEqual(update.data, revoke_data) + self.assertEqual(update.rawdata, revoke_rawdata) + + # insert + self.assertEqual(len(self.inserts), 1) + insert = self.inserts[0] + #data = kojihub.get_host.return_value + data = { + 'create_event': 42, + 'creator_id': 23, + 'host_id': 123, + 'arches': ['x86_64', 'i386'], + 'capacity': 12.0, + 'comment': 'comment_new', + 'description': 'description', + 'enabled': False, + } + rawdata = {} + self.assertEqual(insert.table, 'host_config') + self.assertEqual(insert.data, data) + self.assertEqual(insert.rawdata, rawdata) + + def test_edit_host_no_change(self): + kojihub.get_host = mock.MagicMock() + kojihub.get_host.return_value = { + 'id': 123, + 'user_id': 234, + 'name': 'hostname', + 'arches': ['x86_64'], + 'capacity': 100.0, + 'description': 'description', + 'comment': 'comment', + 'enabled': False, + } + self.context.event_id = 42 + self.context.session.user_id = 23 + + r = self.exports.editHost('hostname') + + self.assertFalse(r) + kojihub.get_host.assert_called_once_with('hostname', strict=True) + + self.assertEqual(len(self.updates), 0) + self.assertEqual(len(self.inserts), 0) diff --git a/tests/test_hub/test_get_host.py b/tests/test_hub/test_get_host.py new file mode 100644 index 0000000..b3e0b8a --- /dev/null +++ b/tests/test_hub/test_get_host.py @@ -0,0 +1,95 @@ +import unittest +import mock + +import koji +import kojihub + +QP = kojihub.QueryProcessor + + +class TestSetHostEnabled(unittest.TestCase): + def getQuery(self, *args, **kwargs): + query = QP(*args, **kwargs) + query.execute = mock.MagicMock() + query.executeOne = mock.MagicMock() + self.queries.append(query) + return query + + def setUp(self): + self.QueryProcessor = mock.patch('kojihub.QueryProcessor', + side_effect=self.getQuery).start() + self.queries = [] + self.context = mock.patch('kojihub.context').start() + # It seems MagicMock will not automatically handle attributes that + # start with "assert" + self.exports = kojihub.RootExports() + + def tearDown(self): + mock.patch.stopall() + + def test_get_host_by_name(self): + self.exports.getHost('hostname') + + self.assertEqual(len(self.queries), 1) + query = self.queries[0] + columns = ['host.id', 'host.user_id', 'host.name', 'host.ready', + 'host.task_load', 'host_config.arches', + 'host_config.capacity', 'host_config.description', + 'host_config.comment', 'host_config.enabled'] + joins = ['host ON host.id = host_config.host_id'] + aliases = ['id', 'user_id', 'name', 'ready', 'task_load', + 'arches', 'capacity', 'description', 'comment', 'enabled'] + clauses = ['(host_config.active = TRUE)', 'name = %(hostInfo)s'] + values = {'hostInfo': 'hostname'} + self.assertEqual(query.tables, ['host_config']) + self.assertEqual(query.joins, joins) + self.assertEqual(set(query.columns), set(columns)) + self.assertEqual(set(query.aliases), set(aliases)) + self.assertEqual(query.clauses, clauses) + self.assertEqual(query.values, values) + + def test_get_host_by_id_event(self): + self.exports.getHost(123, event=345) + + self.assertEqual(len(self.queries), 1) + query = self.queries[0] + columns = ['host.id', 'host.user_id', 'host.name', 'host.ready', + 'host.task_load', 'host_config.arches', + 'host_config.capacity', 'host_config.description', + 'host_config.comment', 'host_config.enabled'] + joins = ['host ON host.id = host_config.host_id'] + aliases = ['id', 'user_id', 'name', 'ready', 'task_load', + 'arches', 'capacity', 'description', 'comment', 'enabled'] + clauses = ['(host_config.create_event <= 345 AND ( host_config.revoke_event IS NULL OR 345 < host_config.revoke_event ))', + 'id = %(hostInfo)i'] + values = {'hostInfo': 123} + self.assertEqual(query.tables, ['host_config']) + self.assertEqual(query.joins, joins) + self.assertEqual(set(query.columns), set(columns)) + self.assertEqual(set(query.aliases), set(aliases)) + self.assertEqual(query.clauses, clauses) + self.assertEqual(query.values, values) + + def getQueryMissing(self, *args, **kwargs): + q = self.getQuery(*args, **kwargs) + q.executeOne.return_value = [] + return q + + def test_get_host_missing(self): + self.QueryProcessor.side_effect = self.getQueryMissing + + r = self.exports.getHost(123) + self.assertEqual(r, None) + + with self.assertRaises(koji.GenericError): + self.exports.getHost(123, strict=True) + + self.assertEqual(len(self.queries), 2) + + self.QueryProcessor.side_effect = self.getQuery + + def test_get_host_invalid_hostinfo(self): + with self.assertRaises(koji.GenericError): + self.exports.getHost({'host_id': 567}) + + self.assertEqual(len(self.queries), 0) diff --git a/tests/test_hub/test_set_host_enabled.py b/tests/test_hub/test_set_host_enabled.py new file mode 100644 index 0000000..5307d01 --- /dev/null +++ b/tests/test_hub/test_set_host_enabled.py @@ -0,0 +1,147 @@ +import unittest +import mock + +import koji +import kojihub + +UP = kojihub.UpdateProcessor +IP = kojihub.InsertProcessor + + +class TestSetHostEnabled(unittest.TestCase): + def getInsert(self, *args, **kwargs): + insert = IP(*args, **kwargs) + insert.execute = mock.MagicMock() + self.inserts.append(insert) + return insert + + def getUpdate(self, *args, **kwargs): + update = UP(*args, **kwargs) + update.execute = mock.MagicMock() + self.updates.append(update) + return update + + def setUp(self): + self.InsertProcessor = mock.patch('kojihub.InsertProcessor', + side_effect=self.getInsert).start() + self.inserts = [] + self.UpdateProcessor = mock.patch('kojihub.UpdateProcessor', + side_effect=self.getUpdate).start() + self.updates = [] + self.context = mock.patch('kojihub.context').start() + # It seems MagicMock will not automatically handle attributes that + # start with "assert" + self.context.session.assertLogin = mock.MagicMock() + self.context.session.assertPerm = mock.MagicMock() + self.exports = kojihub.RootExports() + + def tearDown(self): + mock.patch.stopall() + + def test_enableHost_missing(self): + # non-existing hostname + kojihub.get_host = mock.MagicMock() + kojihub.get_host.return_value = {} + with self.assertRaises(koji.GenericError): + self.exports.enableHost('hostname') + self.assertEqual(self.updates, []) + self.assertEqual(self.inserts, []) + kojihub.get_host.assert_called_once_with('hostname') + + def test_enableHost_valid(self): + kojihub.get_host = mock.MagicMock() + kojihub.get_host.return_value = { + 'id': 123, + 'user_id': 234, + 'name': 'hostname', + 'arches': ['x86_64'], + 'capacity': 100.0, + 'description': 'description', + 'comment': 'comment', + 'enabled': False, + } + self.context.event_id = 42 + self.context.session.user_id = 23 + + self.exports.enableHost('hostname') + + kojihub.get_host.assert_called_once_with('hostname') + # revoke + self.assertEqual(len(self.updates), 1) + values = kojihub.get_host.return_value + clauses = ['host_id = %(id)i', 'active = TRUE'] + revoke_data = { + 'revoke_event': 42, + 'revoker_id': 23 + } + revoke_rawdata = {'active': 'NULL'} + update = self.updates[0] + self.assertEqual(update.table, 'host_config') + self.assertEqual(update.values, values) + self.assertEqual(update.clauses, clauses) + self.assertEqual(update.data, revoke_data) + self.assertEqual(update.rawdata, revoke_rawdata) + + # insert + insert = self.inserts[0] + data = kojihub.get_host.return_value + data['create_event'] = 42 + data['creator_id'] = 23 + data['enabled'] = True + data['host_id'] = data['id'] + del data['id'] + rawdata = {} + self.assertEqual(insert.table, 'host_config') + self.assertEqual(insert.data, data) + self.assertEqual(insert.rawdata, rawdata) + + self.assertEqual(len(self.inserts), 1) + + def test_disableHost_valid(self): + kojihub.get_host = mock.MagicMock() + kojihub.get_host.return_value = { + 'id': 123, + 'user_id': 234, + 'name': 'hostname', + 'arches': ['x86_64'], + 'capacity': 100.0, + 'description': 'description', + 'comment': 'comment', + 'enabled': True, + } + self.context.event_id = 42 + self.context.session.user_id = 23 + + self.exports.disableHost('hostname') + + kojihub.get_host.assert_called_once_with('hostname') + # revoke + self.assertEqual(len(self.updates), 1) + values = kojihub.get_host.return_value + clauses = ['host_id = %(id)i', 'active = TRUE'] + revoke_data = { + 'revoke_event': 42, + 'revoker_id': 23 + } + revoke_rawdata = {'active': 'NULL'} + update = self.updates[0] + self.assertEqual(update.table, 'host_config') + self.assertEqual(update.values, values) + self.assertEqual(update.clauses, clauses) + self.assertEqual(update.data, revoke_data) + self.assertEqual(update.rawdata, revoke_rawdata) + + # insert + insert = self.inserts[0] + data = kojihub.get_host.return_value + data['create_event'] = 42 + data['creator_id'] = 23 + data['enabled'] = False + data['host_id'] = data['id'] + del data['id'] + rawdata = {} + self.assertEqual(insert.table, 'host_config') + self.assertEqual(insert.data, data) + self.assertEqual(insert.rawdata, rawdata) + + self.assertEqual(len(self.inserts), 1)