starcluster.tests.test_cluster_validation

Package starcluster :: Package tests :: Module test_cluster_validation |
[hide private]
[frames] | no frames]

# Source Code for Module starcluster.tests.test_cluster_validation

      1  #!/usr/bin/env python
      2  import os
      3  import tempfile
      4
      5  from starcluster import exception
      6  from starcluster.cluster import Cluster
      7  from starcluster.tests import StarClusterTest
      8
      9
     10 -class TestClusterValidation(StarClusterTest):
     11
     12 -    def test_aws_credentials_validation(self):
     13          cluster = self.config.get_cluster_template('c1')
     14          try:
     15              cluster._validate_credentials()
     16          except exception.ClusterValidationError:
     17              pass
     18          else:
     19              raise Exception("cluster allows invalid aws credentials")
     20
     21 -    def test_plugin_loading(self):
     22          # default test template should have valid plugins by default
     23          # make them invalid
     24          cases = [
     25              {'p1_class': 'None'},
     26              {'p1_class':'unittest.TestCase'},
     27          ]
     28          for case in cases:
     29              cfg = self.get_custom_config(**case)
     30              try:
     31                  cfg.get_cluster_template('c1')
     32              except exception.PluginError:
     33                  pass
     34              else:
     35                  raise Exception(
     36                      'cluster allows non-valid plugin setup class (case: %s)' %
     37                      case)
     38
     39 -    def test_cluster_size_validation(self):
     40          cases = [
     41              {'c1_size': -1},
     42              {'c1_size': 0},
     43          ]
     44          for case in cases:
     45              cfg = self.get_custom_config(**case)
     46              try:
     47                  cluster = cfg.get_cluster_template('c1')
     48                  cluster._validate_cluster_size()
     49              except exception.ClusterValidationError:
     50                  pass
     51              else:
     52                  raise Exception('cluster allows invalid size (case: %s)' \
     53                                  % case)
     54
     55 -    def test_shell_validation(self):
     56          cases = [
     57              {'cluster_shell': ''},
     58              {'cluster_shell': 'nosh'},
     59              {'cluster_shell': 2},
     60          ]
     61          failed = self.__test_cases_from_cluster(
     62              cases, '_validate_shell_setting')
     63          if failed:
     64              raise Exception('cluster allows invalid cluster shell (cases: %s)'
     65                             % failed)
     66
     67 -    def test_keypair_validation(self):
     68          tmpfile = tempfile.NamedTemporaryFile()
     69          tmp_file = tmpfile.name
     70          tmpfile.close()
     71          tmpdir = tempfile.mkdtemp()
     72          cases = [{'k1_location': tmp_file}, {'k1_location': tmpdir}]
     73          for case in cases:
     74              cfg = self.get_custom_config(**case)
     75              cluster = cfg.get_cluster_template('c1')
     76              try:
     77                  cluster._validate_keypair()
     78              except exception.ClusterValidationError:
     79                  pass
     80              else:
     81                  raise Exception('cluster allows invalid key_location')
     82          os.rmdir(tmpdir)
     83
     84 -    def __test_cases_from_cfg(self, cases, test, cluster_name='c1'):
     85          """
     86          Tests all cases by loading a cluster template from the test config.
     87          This method will write a custom test config for each case using its
     88          settings.
     89          """
     90          failed = []
     91          for case in cases:
     92              cfg = self.get_custom_config(**case)
     93              cluster = cfg.get_cluster_template(cluster_name)
     94              try:
     95                  getattr(cluster, test)()
     96              except exception.ClusterValidationError, e:
     97                  print "case: %s, error: %s" % (str(case), e)
     98                  continue
     99              else:
    100                  failed.append(case)
    101          return failed
    102
    103 -    def __test_cases_from_cluster(self, cases, test):
    104          """
    105          Tests all cases by manually loading a cluster template using the
    106          Cluster class. All settings for a case are passed in as constructor
    107          keywords.  Avoids the config module by using the Cluster object
    108          directly to create a test case.
    109          """
    110          failed = []
    111          for case in cases:
    112              cluster = Cluster(**case)
    113              try:
    114                  getattr(cluster, test)()
    115              except exception.ClusterValidationError:
    116                  continue
    117              else:
    118                  failed.append(case)
    119          return failed
    120
    121 -    def test_instance_type_validation(self):
    122          cases = [
    123              {'node_instance_type': 'asdf'},
    124              {'master_instance_type': 'fdsa', 'node_instance_type': 'm1.small'},
    125          ]
    126          failed = self.__test_cases_from_cluster(cases,
    127                                                  "_validate_instance_types")
    128          if failed:
    129              raise Exception(
    130                  'cluster allows invalid instance type settings (cases: %s)' % \
    131                  failed)
    132
    133 -    def test_ebs_validation(self):
    134          try:
    135              failed = self.__test_cases_from_cfg(
    136                  [{'v1_device': '/dev/asd'}], '_validate_ebs_settings')
    137              raise Exception(
    138                  'cluster allows invalid ebs settings (cases: %s)' % failed)
    139          except exception.InvalidDevice:
    140              pass
    141          try:
    142              failed = self.__test_cases_from_cfg(
    143                  [{'v1_partition': -1}], '_validate_ebs_settings')
    144              raise Exception(
    145                  'cluster allows invalid ebs settings (cases: %s)' % failed)
    146          except exception.InvalidPartition:
    147              pass
    148          cases = [
    149              {'v1_mount_path': 'home'},
    150              {'v1_mount_path': '/home', 'v2_mount_path': '/home'},
    151              {'v4_id': 'vol-abcdefg', 'v5_id': 'vol-abcdefg',
    152               'v4_partition': 2, 'v5_partition': 2, 'c1_vols': 'v4, v5'},
    153              {'v1_id': 'vol-abcdefg', 'v2_id': 'vol-gfedcba',
    154               'v1_device': '/dev/sdd', 'v2_device': '/dev/sdd',
    155               'c1_vols': 'v1, v2'},
    156              {'v1_id': 'vol-abcdefg', 'v2_id': 'vol-abcdefg',
    157               'v1_device': '/dev/sdz', 'v2_device': '/dev/sdd',
    158               'c1_vols': 'v1, v2'}
    159          ]
    160          failed = self.__test_cases_from_cfg(cases, '_validate_ebs_settings')
    161          if failed:
    162              raise Exception(
    163                  'cluster allows invalid ebs settings (cases: %s)' % failed)
    164
    165          cases = [
    166              {'v4_id': 'vol-abcdefg', 'v5_id': 'vol-abcdefg',
    167               'v4_partition': 1, 'v5_partition': 2, 'c1_vols': 'v4, v5'},
    168          ]
    169          passed = self.__test_cases_from_cfg(cases, '_validate_ebs_settings')
    170          if len(passed) != len(cases):
    171              raise Exception("validation fails on valid cases: %s" %
    172                              str(passed))
    173
    174 -    def test_permission_validation(self):
    175          assert self.config.permissions.s3.ip_protocol == 'tcp'
    176          assert self.config.permissions.s3.cidr_ip == '0.0.0.0/0'
    177          cases = [
    178              {'s1_from_port':90, 's1_to_port': 10},
    179              {'s1_from_port':-1},
    180              {'s1_cidr_ip':'asdfasdf'},
    181          ]
    182          failed = self.__test_cases_from_cfg(cases,
    183                                              '_validate_permission_settings',
    184                                              cluster_name='c4')
    185          if failed:
    186              raise Exception(
    187                  "cluster allows invalid permission settings (cases %s)" % \
    188                  failed)
    189
    190      #def test_image_validation(self):
    191          #pass
    192
    193      #def test_zone_validation(self):
    194          #pass
    195
    196      #def test_platform_validation(self):
    197          #pass
    198