def test_ec2_terminate_instances_all_not_found(self): """ Test out all not found, should return true """ # Uncomment to get logging from the agent, helpful for debugging logging.basicConfig(level=logging.DEBUG) l = logging.getLogger('appscale.agents.ec_agent') l.setLevel(logging.DEBUG) ec2 = self.factory.create_agent('ec2') instance_ids = ['i-aabbccdd', 'i-aabbccee', 'i-aabbccff'] self.full_params['instance_ids'] = instance_ids # First instance ID doesn't exist, second call will succeed (self.fake_ec2.should_receive('terminate_instances') .and_raise(EC2ResponseError(400, 'no reason', self.multiple_instance_notfound_body.format(instance_ids[:]))) .and_return(True)) # _wait_for_status should return true. (self.fake_ec2.should_receive('get_all_reservations') .and_return(self.terminated_reservations)) instance_ids = set(self.full_params[ec2.PARAM_INSTANCE_IDS]) status_filters = {"instance-state-name": 'terminated', "key-name": self.full_params[ec2.PARAM_KEYNAME]} conn = ec2.open_connection(self.full_params) result = ec2._EC2Agent__terminate_instances(instance_ids, conn, status_filters, max_attempts=1) self.assertTrue(result)
def test_ec2_terminate_instances_keeps_running(self): """ Test out case where all instances keep running. boto.terminate_instances throws InstanceIDInvalid.NotFound We recover and keep waiting for the other 2 instances, which keep running and do *not* terminate. Will take about 120 seconds to run (timeout) Should return False """ ec2 = self.factory.create_agent('ec2') instance_ids = ['i-aabbccdd', 'i-aabbccee', 'i-aabbccff'] self.full_params['instance_ids'] = instance_ids # First instance ID doesn't exist, second call will succeed (self.fake_ec2.should_receive('terminate_instances') .and_raise(EC2ResponseError(400, 'no reason', self.instance_notfound_body.format(instance_ids[0]))) .and_return(True)) # _wait_for_status should return true. (self.fake_ec2.should_receive('get_all_reservations') .and_return(self.empty_reservations)) instance_ids = set(self.full_params[ec2.PARAM_INSTANCE_IDS]) status_filters = {"instance-state-name": 'terminated', "key-name": self.full_params[ec2.PARAM_KEYNAME]} conn = ec2.open_connection(self.full_params) result = ec2._EC2Agent__terminate_instances(instance_ids, conn, status_filters, max_attempts=1) self.assertFalse(result)
def test_ec2_terminate_instances(self): """ Test out a successful terminate instances """ # Uncomment to get logging from the agent, helpful for debugging #logging.basicConfig(level=logging.DEBUG) #l = logging.getLogger('appscale.agents.ec_agent') #l.setLevel(logging.DEBUG) ec2 = self.factory.create_agent('ec2') (self.fake_ec2.should_receive('terminate_instances') .and_return(True)) self.full_params['instance_ids'] = ['i-aabbccdd', 'i-aabbccee', 'i-aabbccff'] (self.fake_ec2.should_receive('get_all_reservations') .and_return(self.terminated_reservations)) instance_ids = set(self.full_params[ec2.PARAM_INSTANCE_IDS]) status_filters = {"instance-state-name": 'terminated', "key-name": self.full_params[ec2.PARAM_KEYNAME]} conn = ec2.open_connection(self.full_params) result = ec2._EC2Agent__terminate_instances(instance_ids, conn, status_filters, max_attempts=1) self.assertTrue(result)
def test_ec2_wait_for_status_change_keeps_on_running(self): """ Case where all instances keep running and never reach 'terminated' Should return False """ filters = {'instance-state-name': 'terminated', 'key-name': self.full_params['keyname']} ec2 = self.factory.create_agent('ec2') conn = ec2.open_connection(self.full_params) # First element is missing instance_ids = ['i-aabbccdd', 'i-aabbccee', 'i-aabbccff'] (self.fake_ec2.should_receive('get_all_reservations') .and_raise(EC2ResponseError(400, 'no reason', self.instance_notfound_body.format(instance_ids[1]))) .and_return(self.empty_reservations) .and_return(self.empty_reservations) .and_return(self.empty_reservations) .and_return(self.empty_reservations) ) # Last two won't reach terminated state result = ec2.wait_for_status_change(instance_ids, conn, filters, max_wait_time=3, poll_interval=1) self.assertFalse(result)
def test_ec2_wait_for_status_change_invalid_filters(self): """ Simple test case if an invalid filter is passed" """ ec2 = self.factory.create_agent('ec2') conn = ec2.open_connection(self.full_params) with self.assertRaisesRegexp(InvalidFilter, 'instance-state-name is missing from filter'): ec2.wait_for_status_change(['i-aabbccdd'], conn, filters={})
def test_ec2_wait_for_status_change_already_terminated(self): """ Terminate an instance and the cloud returns InvalidInstanceID.NotFound This should be reported as a success """ filters = {'instance-state-name': 'terminated', 'key-name': self.full_params['keyname']} ec2 = self.factory.create_agent('ec2') conn = ec2.open_connection(self.full_params) instance_ids = ['i-aabbccdd'] self.fake_ec2.should_receive('get_all_reservations').and_raise( EC2ResponseError(400, "no reason", self.instance_notfound_body.format(instance_ids[0]))) result = ec2.wait_for_status_change(instance_ids, conn, filters, max_wait_time=10, poll_interval=1) self.assertTrue(result)
def test_ec2_wait_for_status_change_successfully_terminated(self): """ The first instance id in the list is marked as not found. Note: self.terminated_reservations does return the instance id, which shouldn't have an impact """ filters = {'instance-state-name': 'terminated', 'key-name': self.full_params['keyname']} ec2 = self.factory.create_agent('ec2') conn = ec2.open_connection(self.full_params) instance_ids = ['i-aabbccdd', 'i-aabbccee', 'i-aabbccff'] (self.fake_ec2.should_receive('get_all_reservations') .and_return(self.terminated_reservations)) result = ec2.wait_for_status_change(instance_ids, conn, filters, max_wait_time=10, poll_interval=1) self.assertTrue(result)
def test_ec2_wait_for_status_change_stopped_not_found(self): """ When waiting for the 'stopped' state, if an instance is not found it is considered to be an error and the method will throw an InstanceIDNotFound exception. """ filters = {'instance-state-name': 'stopped', 'key-name': self.full_params['keyname']} ec2 = self.factory.create_agent('ec2') conn = ec2.open_connection(self.full_params) # Throw an invalid id when stopping an instance. wait_for_status should raise an # exception. (self.fake_ec2.should_receive('get_all_reservations') .and_raise(EC2ResponseError(400, "no reason", self.instance_notfound_body.format('i-aabbccdd')))) with self.assertRaises(InstanceIDNotFound): ec2.wait_for_status_change(['i-aabbccdd'], conn, filters, 5, 1)
def test_ec2_wait_for_status_change_all_not_found_terminated(self): """ Case where all instances are not found Should return True """ filters = {'instance-state-name': 'terminated', 'key-name': self.full_params['keyname']} ec2 = self.factory.create_agent('ec2') conn = ec2.open_connection(self.full_params) instance_ids = ['i-aabbccdd', 'i-aabbccee', 'i-aabbccff'] # Raise an exception for each instance id in serial (self.fake_ec2.should_receive('get_all_reservations') .and_raise(EC2ResponseError(400, "no reason", self.instance_notfound_body.format(instance_ids[0]))) .and_raise(EC2ResponseError(400, "no reason", self.instance_notfound_body.format(instance_ids[1]))) .and_raise(EC2ResponseError(400, "no reason", self.instance_notfound_body.format(instance_ids[2])))) result = ec2.wait_for_status_change(instance_ids, conn, filters, max_wait_time=5, poll_interval=1) self.assertTrue(result)
def test_ec2_wait_for_status_change_multiple_not_found_different_order(self): """ Multiple instance ids not found, first one is found Should return True """ filters = {'instance-state-name': 'terminated', 'key-name': self.full_params['keyname']} ec2 = self.factory.create_agent('ec2') conn = ec2.open_connection(self.full_params) instance_ids = ['i-aabbccdd', 'i-aabbccee', 'i-aabbccff'] # Raise exception for the first two instance ids. missing_ids = ','.join(instance_ids[1:]) (self.fake_ec2.should_receive('get_all_reservations') .and_raise(EC2ResponseError(400, "no reason", self.multiple_instances_not_found.format(missing_ids))) .and_return(self.terminated_reservations)) result = ec2.wait_for_status_change(instance_ids, conn, filters, max_wait_time=5, poll_interval=1) self.assertTrue(result)
def test_ec2_terminate_instances_invalid_id(self): """ Test out an invalid instance id """ ec2 = self.factory.create_agent('ec2') instance_ids = ['i-aabbccdd', 'i-aabbccee', 'i-aabbccff'] self.full_params['instance_ids'] = instance_ids # First instance ID doesn't exist, second call will succeed (self.fake_ec2.should_receive('terminate_instances') .and_raise(EC2ResponseError(400, 'no reason', self.instance_notfound_body.format(instance_ids[0]))) .and_return(True)) # _wait_for_status should return true. (self.fake_ec2.should_receive('get_all_reservations') .and_return(self.terminated_reservations)) instance_ids = set(self.full_params[ec2.PARAM_INSTANCE_IDS]) status_filters = {"instance-state-name": 'terminated', "key-name": self.full_params[ec2.PARAM_KEYNAME]} conn = ec2.open_connection(self.full_params) result = ec2._EC2Agent__terminate_instances(instance_ids, conn, status_filters, max_attempts=1) self.assertTrue(result)