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

Source Code for Module starcluster.tests.test_cluster_validation

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