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
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
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: 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 cases = [ 134 {'v1_mount_path': 'home'}, 135 ] 136 failed = self.__test_cases_from_cfg(cases, '_validate_ebs_settings') 137 if failed: 138 raise Exception( 139 'cluster allows invalid ebs settings (cases: %s)' % failed) 140 try: 141 failed = self.__test_cases_from_cfg( 142 [{'v1_device': '/dev/asd'}], '_validate_ebs_settings') 143 raise Exception( 144 'cluster allows invalid ebs settings (cases: %s)' % failed) 145 except exception.InvalidDevice: 146 pass 147 try: 148 failed = self.__test_cases_from_cfg( 149 [{'v1_partition': -1}], '_validate_ebs_settings') 150 raise Exception( 151 'cluster allows invalid ebs settings (cases: %s)' % failed) 152 except exception.InvalidPartition: 153 pass
154
156 assert self.config.permissions.s3.ip_protocol == 'tcp' 157 assert self.config.permissions.s3.cidr_ip == '0.0.0.0/0' 158 cases = [ 159 {'s1_from_port':90, 's1_to_port': 10}, 160 {'s1_from_port':-1}, 161 {'s1_cidr_ip':'asdfasdf'}, 162 ] 163 failed = self.__test_cases_from_cfg(cases, 164 '_validate_permission_settings', 165 cluster_name='c4') 166 if failed: 167 raise Exception( 168 "cluster allows invalid permission settings (cases %s)" % \ 169 failed)
170 171 #def test_image_validation(self): 172 #pass 173 174 #def test_zone_validation(self): 175 #pass 176 177 #def test_platform_validation(self): 178 #pass 179