def setUp(self): template = 'WordPress_Single_Instance.template' stack_paramstr = ';'.join([ 'InstanceType=m1.xlarge', 'DBUsername=dbuser', 'DBPassword='******'OS_PASSWORD'] ]) self.stack = util.Stack(self, template, 'F17', 'x86_64', 'cfntools', stack_paramstr) self.WikiDatabase = util.Instance(self, 'WikiDatabase')
def setUp(self): template = 'AutoScalingMultiAZSample.template' stack_paramstr = ';'.join([ 'InstanceType=m1.small', 'DBUsername=dbuser', 'DBPassword='******'OS_PASSWORD'] ]) self.stack = util.Stack(self, template, 'F17', 'x86_64', 'cfntools', stack_paramstr) self.WebServerGroup0 = util.Instance(self, 'WebServerGroup-0')
def testUpdateStackReplace(self): # Then if we change a template parameter, instance should get replaced physids = self.stack.instance_phys_ids() self.assertEqual(len(physids), 1) print "UPDATE start, instance IDs = %s" % physids self.db_user = '******' self.stack_paramstr = ';'.join(['InstanceType=%s' % self.instance_type, 'DBUsername=%s' % self.db_user, 'DBPassword='******'OS_PASSWORD']]) self.stack.stack_paramstr = self.stack_paramstr self.stack.update() tries = 0 while (tries <= 500): pollids = self.stack.instance_phys_ids() print "Waiting for Instance to be replaced %s/%s %s" %\ (tries, 500, pollids) if (len(pollids) == 2): self.assertTrue(pollids[1] != physids[0]) print "Instance replaced, new ID = %s" % pollids[1] break time.sleep(10) tries += 1 # Check we didn't timeout self.assertTrue(tries < 500) # Now use DescribeStacks to check the parameter is updated parameters = {} parameters['StackName'] = self.stack.stackname response = self.stack.heatclient.describe_stacks(**parameters) prefix = '/DescribeStacksResponse/DescribeStacksResult/Stacks/member' # value for each key, then check the extracted value param_prefix = prefix + '/Parameters/member[ParameterKey="DBUsername"]' lookup_value = self.stack.response_xml_item(response, param_prefix, "ParameterValue") print "Updated, got DBUsername=%s" % lookup_value self.assertEqual(lookup_value, self.db_user) # Now we have to Create a new Instance object and wait for # provisioning to be complete, or the next test will fail self.WikiDatabase = util.Instance(self.inst, self.logical_resource_name) self.WikiDatabase.wait_for_boot() self.WikiDatabase.check_cfntools() self.WikiDatabase.wait_for_provisioning() print "Update completed, instance rebuild complete" self.assertTrue(self.WikiDatabase.file_present ('/etc/wordpress/wp-config.php')) print "Wordpress installation detected after update" print "UpdateStack (Replace) : OK"
def setUp(self): # The HAProxy template somewhat un-usefully load-balances a single # server, so we launch a wordpress stack and stick haproxy in front wp_template = 'WordPress_Single_Instance.template' wp_paramstr = ';'.join([ 'InstanceType=m1.xlarge', 'DBUsername=dbuser', 'DBPassword='******'OS_PASSWORD'] ]) self.stack = util.Stack(self, wp_template, 'F17', 'x86_64', 'cfntools', wp_paramstr) self.WikiDatabase = util.Instance(self, 'WikiDatabase') self.hap_stack = None
def setupAll(cls): print "SETUPALL" template = 'WordPress_Single_Instance.template' stack_paramstr = ';'.join([ 'InstanceType=m1.xlarge', 'DBUsername=dbuser', 'DBPassword='******'OS_PASSWORD'] ]) cls.logical_resource_name = 'WikiDatabase' cls.logical_resource_type = 'AWS::EC2::Instance' # Just to get the assert*() methods class CfnApiFunctions(unittest.TestCase): @unittest.skip('Not a real test case') def runTest(self): pass inst = CfnApiFunctions() cls.stack = util.StackBoto(inst, template, 'F17', 'x86_64', 'cfntools', stack_paramstr) cls.WikiDatabase = util.Instance(inst, cls.logical_resource_name) try: cls.stack.create() cls.WikiDatabase.wait_for_boot() cls.WikiDatabase.check_cfntools() cls.WikiDatabase.wait_for_provisioning() cls.logical_resource_status = "CREATE_COMPLETE" # Save some compiled regexes and strings for response validation cls.time_re = re.compile( "[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$") cls.description_re = re.compile( "^AWS CloudFormation Sample Template") cls.stack_status = "CREATE_COMPLETE" cls.stack_status_reason = "Stack successfully created" cls.stack_timeout = 60 cls.stack_disable_rollback = True # Match the expected format for an instance's physical resource ID cls.phys_res_id_re = re.compile( "^[0-9a-z]*-[0-9a-z]*-[0-9a-z]*-[0-9a-z]*-[0-9a-z]*$") except Exception as ex: print "setupAll failed : %s" % ex cls.stack.cleanup() raise
def test_instance(self): self.stack.create() self.WikiDatabase.wait_for_boot() self.WikiDatabase.check_cfntools() self.WikiDatabase.wait_for_provisioning() # ensure wordpress was installed by checking for expected # configuration file over ssh self.assertTrue( self.WikiDatabase.file_present('/etc/wordpress/wp-config.php')) print "Wordpress installation detected" # Verify the output URL parses as expected, ie check that # the wordpress installation is operational stack_url = self.stack.get_stack_output("WebsiteURL") print "Got stack output WebsiteURL=%s, verifying" % stack_url ver = verify.VerifyStack() self.assertTrue(ver.verify_wordpress(stack_url)) # So wordpress instance is up, we now launch the HAProxy instance # and prove wordpress is accessable via the proxy instance IP hap_stackname = 'hap_teststack' hap_template = 'HAProxy_Single_Instance.template' hap_paramstr = ';'.join( ['InstanceType=m1.xlarge', "Server1=%s:80" % self.WikiDatabase.ip]) self.hap_stack = util.Stack(self, hap_template, 'F17', 'x86_64', 'cfntools', hap_paramstr, stackname=hap_stackname) self.LoadBalancerInstance = util.Instance(self, 'LoadBalancerInstance', hap_stackname) self.hap_stack.create() self.LoadBalancerInstance.wait_for_boot() self.LoadBalancerInstance.check_cfntools() self.LoadBalancerInstance.wait_for_provisioning() # Verify the output URL parses as expected, ie check that # the wordpress installation is operational via the proxy proxy_ip = self.hap_stack.get_stack_output("PublicIp") print "Got haproxy stack output PublicIp=%s, verifying" % proxy_ip proxy_url = "http://%s/wordpress" % proxy_ip self.assertTrue(ver.verify_wordpress(proxy_url))
def test_instance(self): self.stack.create() self.WebServerGroup0.wait_for_boot() self.WebServerGroup0.check_cfntools() self.WebServerGroup0.wait_for_provisioning() # TODO: verify the code below tests the template properly # TODO(sdake) use a util exists function for nonexistent instances (needs dev) # Trigger the load balancer by taking up memory self.WebServerGroup0.exec_command('memhog -r100000 1500m') # Give the load balancer 2 minutes to react sleep(2 * 60) self.WebServerGroup1 = util.Instance(self, 'WebServerGroup-1') # Verify the second instance gets launched self.assertTrue(self.WebServerGroup1.exists()) self.WebServerGroup1.wait_for_boot() self.WebServerGroup1.check_cfntools() self.WebServerGroup1.wait_for_provisioning() # ensure wordpress was installed by checking for expected # configuration file over ssh self.assertTrue( self.WebServerGroup0.file_present('/etc/wordpress/wp-config.php')) print "Wordpress installation detected on WSG0" # ensure wordpress was installed by checking for expected # configuration file over ssh self.assertTrue( self.WebServerGroup1.file_present('/etc/wordpress/wp-config.php')) print "Wordpress installation detected on WSG1" # Verify the output URL parses as expected, ie check that # the wordpress installation is operational stack_url = self.stack.get_stack_output("URL") print "Got stack output WebsiteURL=%s, verifying" % stack_url ver = verify.VerifyStack() self.assertTrue(ver.verify_wordpress(stack_url))
def test_instance(self): self.stack.create() self.WikiDatabase.wait_for_boot() self.WikiDatabase.check_cfntools() self.WikiDatabase.wait_for_provisioning() # ensure wordpress was installed by checking for expected # configuration file over ssh wp_file = '/usr/share/wordpress/wp-config.php' self.assertTrue(self.WikiDatabase.file_present(wp_file)) print "Wordpress installation detected" # Verify the output URL parses as expected, ie check that # the wordpress installation is operational stack_url = self.stack.get_stack_output("WebsiteURL") print "Got stack output WebsiteURL=%s, verifying" % stack_url ver = verify.VerifyStack() self.assertTrue(ver.verify_wordpress(stack_url)) # Save the instance physical resource ID phys_res_ids = self.stack.instance_phys_ids() self.assertEqual(len(phys_res_ids), 1) print "Shutting down instance ID = %s" % phys_res_ids[0] # Now shut down the instance via SSH, and wait for HA to reprovision # note it may not come back on the same IP stdin, stdout, stderr =\ self.WikiDatabase.exec_sudo_command('/sbin/halt') # Now poll the stack events, as the current WikiDatabase instance # should be replaced with a new one # we can then prove the physical resource ID is different, and that # wordpress is accessible on the new WikiDatabase instance tries = 0 while (tries <= 500): pollids = self.stack.instance_phys_ids() print "Waiting for Instance to be replaced %s/%s %s" %\ (tries, 500, pollids) if (len(pollids) == 2): self.assertTrue(pollids[1] != phys_res_ids[0]) print "Instance replaced, new ID = %s" % pollids[1] break time.sleep(10) tries += 1 # Check we didn't timeout self.assertTrue(tries < 500) # Create a new Instance object and wait for boot # It seems to be necessary to close the old WikiDatabase # instance SSH transport, or paramiko throws an exception # when the SSH daemon starts on the new instance if it comes # up on the same IP address, suspect paramiko bug, TBC self.WikiDatabase.close_ssh_client() self.WikiDatabaseNew = util.Instance(self, 'WikiDatabase') self.WikiDatabaseNew.wait_for_boot() self.WikiDatabaseNew.check_cfntools() self.WikiDatabaseNew.wait_for_provisioning() # Re-check wordpress installation as for the first instance self.assertTrue(self.WikiDatabaseNew.file_present(wp_file)) print "Wordpress installation detected on new instance" # Verify the output URL parses as expected, ie check that # the wordpress installation is operational stack_url = self.stack.get_stack_output("WebsiteURL") print "Got stack output WebsiteURL=%s, verifying" % stack_url self.assertTrue(ver.verify_wordpress(stack_url))