From 4e771a9573902972ca9679e0c27276ccb4f15c50 Mon Sep 17 00:00:00 2001 From: Helga Velroyen Date: Wed, 7 Aug 2013 13:50:27 +0200 Subject: [PATCH] Move Ipolicy utility function to cmdlib/common.py Since the check of consistency between an ipolicy and the list of enabled disk templates will not only be needed on cluster modification, but also on group modification, we move the respective function and its unit tests to the 'common' module. Signed-off-by: Helga Velroyen Reviewed-by: Thomas Thrainer --- lib/cmdlib/cluster.py | 36 ++++--------------- lib/cmdlib/common.py | 21 +++++++++++ test/py/ganeti.cmdlib.cluster_unittest.py | 22 ------------ test/py/ganeti.cmdlib.common_unittest.py | 54 +++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 52 deletions(-) create mode 100755 test/py/ganeti.cmdlib.common_unittest.py diff --git a/lib/cmdlib/cluster.py b/lib/cmdlib/cluster.py index 88448a7..2b93e93 100644 --- a/lib/cmdlib/cluster.py +++ b/lib/cmdlib/cluster.py @@ -56,7 +56,8 @@ from ganeti.cmdlib.common import ShareAll, RunPostHook, \ GetWantedInstances, MergeAndVerifyHvState, MergeAndVerifyDiskState, \ GetUpdatedIPolicy, ComputeNewInstanceViolations, GetUpdatedParams, \ CheckOSParams, CheckHVParams, AdjustCandidatePool, CheckNodePVs, \ - ComputeIPolicyInstanceViolation, AnnotateDiskParams, SupportsOob + ComputeIPolicyInstanceViolation, AnnotateDiskParams, SupportsOob, \ + CheckIpolicyVsDiskTemplates import ganeti.masterd.instance @@ -789,31 +790,6 @@ class LUClusterSetParams(LogicalUnit): enabled_disk_templates = cluster.enabled_disk_templates return (enabled_disk_templates, new_enabled_disk_templates) - @staticmethod - def _CheckIpolicyVsDiskTemplates(ipolicy, enabled_disk_templates): - """Checks ipolicy disk templates against enabled disk tempaltes. - - @type ipolicy: dict - @param ipolicy: the new ipolicy - @type enabled_disk_templates: list of string - @param enabled_disk_templates: list of enabled disk templates on the - cluster - @rtype: errors.OpPrereqError - @raises: exception if there is at least one allowed disk template that - is not also enabled. - - """ - assert constants.IPOLICY_DTS in ipolicy - allowed_disk_templates = ipolicy[constants.IPOLICY_DTS] - not_enabled = [] - for allowed_disk_template in allowed_disk_templates: - if not allowed_disk_template in enabled_disk_templates: - not_enabled.append(allowed_disk_template) - if not_enabled: - raise errors.OpPrereqError("The following disk template are allowed" - " by the ipolicy, but not enabled on the" - " cluster: %s" % utils.CommaJoin(not_enabled)) - def _CheckIpolicy(self, cluster, enabled_disk_templates): """Checks the ipolicy. @@ -829,8 +805,8 @@ class LUClusterSetParams(LogicalUnit): self.new_ipolicy = GetUpdatedIPolicy(cluster.ipolicy, self.op.ipolicy, group_policy=False) - self._CheckIpolicyVsDiskTemplates(self.new_ipolicy, - enabled_disk_templates) + CheckIpolicyVsDiskTemplates(self.new_ipolicy, + enabled_disk_templates) all_instances = self.cfg.GetAllInstancesInfo().values() violations = set() @@ -850,8 +826,8 @@ class LUClusterSetParams(LogicalUnit): " violate them: %s", utils.CommaJoin(utils.NiceSort(violations))) else: - self._CheckIpolicyVsDiskTemplates(cluster.ipolicy, - enabled_disk_templates) + CheckIpolicyVsDiskTemplates(cluster.ipolicy, + enabled_disk_templates) def CheckPrereq(self): """Check prerequisites. diff --git a/lib/cmdlib/common.py b/lib/cmdlib/common.py index e126769..73132c2 100644 --- a/lib/cmdlib/common.py +++ b/lib/cmdlib/common.py @@ -1116,3 +1116,24 @@ def CheckStorageTypeEnabled(cluster, storage_type): " enabled in this cluster. Enabled disk" " templates are: %s" % (storage_type, ",".join(cluster.enabled_disk_templates))) + + +def CheckIpolicyVsDiskTemplates(ipolicy, enabled_disk_templates): + """Checks ipolicy disk templates against enabled disk tempaltes. + + @type ipolicy: dict + @param ipolicy: the new ipolicy + @type enabled_disk_templates: list of string + @param enabled_disk_templates: list of enabled disk templates on the + cluster + @raises errors.OpPrereqError: if there is at least one allowed disk + template that is not also enabled. + + """ + assert constants.IPOLICY_DTS in ipolicy + allowed_disk_templates = ipolicy[constants.IPOLICY_DTS] + not_enabled = set(allowed_disk_templates) - set(enabled_disk_templates) + if not_enabled: + raise errors.OpPrereqError("The following disk template are allowed" + " by the ipolicy, but not enabled on the" + " cluster: %s" % utils.CommaJoin(not_enabled)) diff --git a/test/py/ganeti.cmdlib.cluster_unittest.py b/test/py/ganeti.cmdlib.cluster_unittest.py index ff44ede..7667362 100755 --- a/test/py/ganeti.cmdlib.cluster_unittest.py +++ b/test/py/ganeti.cmdlib.cluster_unittest.py @@ -32,28 +32,6 @@ import testutils import mock -class TestCheckIpolicy(unittest.TestCase): - - def setUp(self): - unittest.TestCase.setUp(self) - - def testAllTemplatesEnabled(self): - allowed_disk_templates = [constants.DT_PLAIN] - ipolicy = {constants.IPOLICY_DTS: allowed_disk_templates} - enabled_disk_templates = [constants.DT_PLAIN, constants.DT_DRBD8] - cluster.LUClusterSetParams._CheckIpolicyVsDiskTemplates( - ipolicy, enabled_disk_templates) - - def testSomeTemplatesUnenabled(self): - allowed_disk_templates = [constants.DT_PLAIN, constants.DT_DISKLESS] - ipolicy = {constants.IPOLICY_DTS: allowed_disk_templates} - enabled_disk_templates = [constants.DT_PLAIN, constants.DT_DRBD8] - self.assertRaises( - errors.OpPrereqError, - cluster.LUClusterSetParams._CheckIpolicyVsDiskTemplates, - ipolicy, enabled_disk_templates) - - class TestCheckFileStoragePath(unittest.TestCase): def setUp(self): diff --git a/test/py/ganeti.cmdlib.common_unittest.py b/test/py/ganeti.cmdlib.common_unittest.py new file mode 100755 index 0000000..626afe6 --- /dev/null +++ b/test/py/ganeti.cmdlib.common_unittest.py @@ -0,0 +1,54 @@ +#!/usr/bin/python +# + +# Copyright (C) 2013 Google Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301, USA. + + +"""Script for unittesting the cmdlib module 'common'""" + + +import unittest + +from ganeti.cmdlib import common +from ganeti import constants +from ganeti import errors + +import testutils + + +class TestCheckIpolicy(unittest.TestCase): + + def testAllTemplatesEnabled(self): + allowed_disk_templates = [constants.DT_PLAIN] + ipolicy = {constants.IPOLICY_DTS: allowed_disk_templates} + enabled_disk_templates = [constants.DT_PLAIN, constants.DT_DRBD8] + common.CheckIpolicyVsDiskTemplates( + ipolicy, enabled_disk_templates) + + def testSomeTemplatesUnenabled(self): + allowed_disk_templates = [constants.DT_PLAIN, constants.DT_DISKLESS] + ipolicy = {constants.IPOLICY_DTS: allowed_disk_templates} + enabled_disk_templates = [constants.DT_PLAIN, constants.DT_DRBD8] + self.assertRaises( + errors.OpPrereqError, + common.CheckIpolicyVsDiskTemplates, + ipolicy, enabled_disk_templates) + + +if __name__ == "__main__": + testutils.GanetiTestProgram() -- 1.7.10.4