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_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_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_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_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)