Revision 4e771a95

b/lib/cmdlib/cluster.py
56 56
  GetWantedInstances, MergeAndVerifyHvState, MergeAndVerifyDiskState, \
57 57
  GetUpdatedIPolicy, ComputeNewInstanceViolations, GetUpdatedParams, \
58 58
  CheckOSParams, CheckHVParams, AdjustCandidatePool, CheckNodePVs, \
59
  ComputeIPolicyInstanceViolation, AnnotateDiskParams, SupportsOob
59
  ComputeIPolicyInstanceViolation, AnnotateDiskParams, SupportsOob, \
60
  CheckIpolicyVsDiskTemplates
60 61

  
61 62
import ganeti.masterd.instance
62 63

  
......
789 790
      enabled_disk_templates = cluster.enabled_disk_templates
790 791
    return (enabled_disk_templates, new_enabled_disk_templates)
791 792

  
792
  @staticmethod
793
  def _CheckIpolicyVsDiskTemplates(ipolicy, enabled_disk_templates):
794
    """Checks ipolicy disk templates against enabled disk tempaltes.
795

  
796
    @type ipolicy: dict
797
    @param ipolicy: the new ipolicy
798
    @type enabled_disk_templates: list of string
799
    @param enabled_disk_templates: list of enabled disk templates on the
800
      cluster
801
    @rtype: errors.OpPrereqError
802
    @raises: exception if there is at least one allowed disk template that
803
      is not also enabled.
804

  
805
    """
806
    assert constants.IPOLICY_DTS in ipolicy
807
    allowed_disk_templates = ipolicy[constants.IPOLICY_DTS]
808
    not_enabled = []
809
    for allowed_disk_template in allowed_disk_templates:
810
      if not allowed_disk_template in enabled_disk_templates:
811
        not_enabled.append(allowed_disk_template)
812
    if not_enabled:
813
      raise errors.OpPrereqError("The following disk template are allowed"
814
                                 " by the ipolicy, but not enabled on the"
815
                                 " cluster: %s" % utils.CommaJoin(not_enabled))
816

  
817 793
  def _CheckIpolicy(self, cluster, enabled_disk_templates):
818 794
    """Checks the ipolicy.
819 795

  
......
829 805
      self.new_ipolicy = GetUpdatedIPolicy(cluster.ipolicy, self.op.ipolicy,
830 806
                                           group_policy=False)
831 807

  
832
      self._CheckIpolicyVsDiskTemplates(self.new_ipolicy,
833
                                        enabled_disk_templates)
808
      CheckIpolicyVsDiskTemplates(self.new_ipolicy,
809
                                  enabled_disk_templates)
834 810

  
835 811
      all_instances = self.cfg.GetAllInstancesInfo().values()
836 812
      violations = set()
......
850 826
                        " violate them: %s",
851 827
                        utils.CommaJoin(utils.NiceSort(violations)))
852 828
    else:
853
      self._CheckIpolicyVsDiskTemplates(cluster.ipolicy,
854
                                        enabled_disk_templates)
829
      CheckIpolicyVsDiskTemplates(cluster.ipolicy,
830
                                  enabled_disk_templates)
855 831

  
856 832
  def CheckPrereq(self):
857 833
    """Check prerequisites.
b/lib/cmdlib/common.py
1116 1116
                               " enabled in this cluster. Enabled disk"
1117 1117
                               " templates are: %s" % (storage_type,
1118 1118
                               ",".join(cluster.enabled_disk_templates)))
1119

  
1120

  
1121
def CheckIpolicyVsDiskTemplates(ipolicy, enabled_disk_templates):
1122
  """Checks ipolicy disk templates against enabled disk tempaltes.
1123

  
1124
  @type ipolicy: dict
1125
  @param ipolicy: the new ipolicy
1126
  @type enabled_disk_templates: list of string
1127
  @param enabled_disk_templates: list of enabled disk templates on the
1128
    cluster
1129
  @raises errors.OpPrereqError: if there is at least one allowed disk
1130
    template that is not also enabled.
1131

  
1132
  """
1133
  assert constants.IPOLICY_DTS in ipolicy
1134
  allowed_disk_templates = ipolicy[constants.IPOLICY_DTS]
1135
  not_enabled = set(allowed_disk_templates) - set(enabled_disk_templates)
1136
  if not_enabled:
1137
    raise errors.OpPrereqError("The following disk template are allowed"
1138
                               " by the ipolicy, but not enabled on the"
1139
                               " cluster: %s" % utils.CommaJoin(not_enabled))
b/test/py/ganeti.cmdlib.cluster_unittest.py
32 32
import mock
33 33

  
34 34

  
35
class TestCheckIpolicy(unittest.TestCase):
36

  
37
  def setUp(self):
38
    unittest.TestCase.setUp(self)
39

  
40
  def testAllTemplatesEnabled(self):
41
    allowed_disk_templates = [constants.DT_PLAIN]
42
    ipolicy = {constants.IPOLICY_DTS: allowed_disk_templates}
43
    enabled_disk_templates = [constants.DT_PLAIN, constants.DT_DRBD8]
44
    cluster.LUClusterSetParams._CheckIpolicyVsDiskTemplates(
45
        ipolicy, enabled_disk_templates)
46

  
47
  def testSomeTemplatesUnenabled(self):
48
    allowed_disk_templates = [constants.DT_PLAIN, constants.DT_DISKLESS]
49
    ipolicy = {constants.IPOLICY_DTS: allowed_disk_templates}
50
    enabled_disk_templates = [constants.DT_PLAIN, constants.DT_DRBD8]
51
    self.assertRaises(
52
        errors.OpPrereqError, 
53
        cluster.LUClusterSetParams._CheckIpolicyVsDiskTemplates,
54
        ipolicy, enabled_disk_templates)
55

  
56

  
57 35
class TestCheckFileStoragePath(unittest.TestCase):
58 36

  
59 37
  def setUp(self):
b/test/py/ganeti.cmdlib.common_unittest.py
1
#!/usr/bin/python
2
#
3

  
4
# Copyright (C) 2013 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

  
21

  
22
"""Script for unittesting the cmdlib module 'common'"""
23

  
24

  
25
import unittest
26

  
27
from ganeti.cmdlib import common
28
from ganeti import constants
29
from ganeti import errors
30

  
31
import testutils
32

  
33

  
34
class TestCheckIpolicy(unittest.TestCase):
35

  
36
  def testAllTemplatesEnabled(self):
37
    allowed_disk_templates = [constants.DT_PLAIN]
38
    ipolicy = {constants.IPOLICY_DTS: allowed_disk_templates}
39
    enabled_disk_templates = [constants.DT_PLAIN, constants.DT_DRBD8]
40
    common.CheckIpolicyVsDiskTemplates(
41
        ipolicy, enabled_disk_templates)
42

  
43
  def testSomeTemplatesUnenabled(self):
44
    allowed_disk_templates = [constants.DT_PLAIN, constants.DT_DISKLESS]
45
    ipolicy = {constants.IPOLICY_DTS: allowed_disk_templates}
46
    enabled_disk_templates = [constants.DT_PLAIN, constants.DT_DRBD8]
47
    self.assertRaises(
48
        errors.OpPrereqError,
49
        common.CheckIpolicyVsDiskTemplates,
50
        ipolicy, enabled_disk_templates)
51

  
52

  
53
if __name__ == "__main__":
54
  testutils.GanetiTestProgram()

Also available in: Unified diff