From 7a8ac21ab88b9689c718963620f4ef4c56d2cec6 Mon Sep 17 00:00:00 2001 From: Yu Ming Zhu Date: Nov 20 2018 09:07:08 +0000 Subject: hub: [set_host_enabled] add force option by default force=False if force is True, the old item in host_config will be revoked and new one will be create, otherwise if host has been enabled/disabled, error raised. --- diff --git a/hub/kojihub.py b/hub/kojihub.py index a1a1fbc..4ce865c 100644 --- a/hub/kojihub.py +++ b/hub/kojihub.py @@ -2073,11 +2073,15 @@ def readTagGroups(tag, event=None, inherit=True, incl_pkgs=True, incl_reqs=True, else: return [x for x in groups if not x['blocked']] -def set_host_enabled(hostname, enabled=True): +def set_host_enabled(hostname, enabled=True, force=False): context.session.assertPerm('admin') host = get_host(hostname) if not host: raise koji.GenericError('host does not exist: %s' % hostname) + if not force and host['enabled'] == enabled: + str_enabled = 'enabled' if enabled else 'disabled' + raise koji.GenericError('host: %s has already been %s' + % (host['name'], str_enabled)) update = UpdateProcessor('host_config', values=host, clauses=['host_id = %(id)i']) update.make_revoke() @@ -10817,13 +10821,13 @@ class RootExports(object): return hostID - def enableHost(self, hostname): + def enableHost(self, hostname, force=False): """Mark a host as enabled""" - set_host_enabled(hostname, True) + set_host_enabled(hostname, True, force=force) - def disableHost(self, hostname): + def disableHost(self, hostname, force=False): """Mark a host as disabled""" - set_host_enabled(hostname, False) + set_host_enabled(hostname, False, force=force) getHost = staticmethod(get_host) editHost = staticmethod(edit_host) diff --git a/tests/test_hub/test_edit_host.py b/tests/test_hub/test_edit_host.py index a4b61d3..0d46bd1 100644 --- a/tests/test_hub/test_edit_host.py +++ b/tests/test_hub/test_edit_host.py @@ -12,7 +12,7 @@ UP = kojihub.UpdateProcessor IP = kojihub.InsertProcessor -class TestSetHostEnabled(unittest.TestCase): +class TestEditHost(unittest.TestCase): def getInsert(self, *args, **kwargs): insert = IP(*args, **kwargs) insert.execute = mock.MagicMock() diff --git a/tests/test_hub/test_set_host_enabled.py b/tests/test_hub/test_set_host_enabled.py index b239080..6923c71 100644 --- a/tests/test_hub/test_set_host_enabled.py +++ b/tests/test_hub/test_set_host_enabled.py @@ -104,6 +104,36 @@ class TestSetHostEnabled(unittest.TestCase): self.assertEqual(len(self.inserts), 1) + def test_enableHost_enabled(self): + kojihub.get_host = mock.MagicMock() + hostinfo = { + 'id': 123, + 'user_id': 234, + 'name': 'hostname', + 'arches': ['x86_64'], + 'capacity': 100.0, + 'description': 'description', + 'comment': 'comment', + 'enabled': True, + } + kojihub.get_host.return_value = hostinfo + self.context.event_id = 42 + self.context.session.user_id = 23 + + with self.assertRaises(koji.GenericError) as cm: + self.exports.enableHost('hostname') + kojihub.get_host.assert_called_once_with('hostname') + self.assertEqual(self.updates, []) + self.assertEqual(self.inserts, []) + self.assertEqual(cm.exception.args[0], + 'host: hostname has already been enabled') + + kojihub.get_host.reset_mock() + self.exports.enableHost('hostname', force=True) + kojihub.get_host.assert_called_once_with('hostname') + self.assertEqual(len(self.updates), 1) + self.assertEqual(len(self.inserts), 1) + def test_disableHost_valid(self): kojihub.get_host = mock.MagicMock() hostinfo = { @@ -155,3 +185,33 @@ class TestSetHostEnabled(unittest.TestCase): self.assertEqual(insert.rawdata, rawdata) self.assertEqual(len(self.inserts), 1) + + def test_disableHost_disabled(self): + kojihub.get_host = mock.MagicMock() + hostinfo = { + 'id': 123, + 'user_id': 234, + 'name': 'hostname', + 'arches': ['x86_64'], + 'capacity': 100.0, + 'description': 'description', + 'comment': 'comment', + 'enabled': False, + } + kojihub.get_host.return_value = hostinfo + self.context.event_id = 42 + self.context.session.user_id = 23 + + with self.assertRaises(koji.GenericError) as cm: + self.exports.disableHost('hostname') + kojihub.get_host.assert_called_once_with('hostname') + self.assertEqual(self.updates, []) + self.assertEqual(self.inserts, []) + self.assertEqual(cm.exception.args[0], + 'host: hostname has already been disabled') + + kojihub.get_host.reset_mock() + self.exports.disableHost('hostname', force=True) + kojihub.get_host.assert_called_once_with('hostname') + self.assertEqual(len(self.updates), 1) + self.assertEqual(len(self.inserts), 1)