def setUp(self): self.fake_ec2 = flexmock(name='self.fake_ec2') self.fake_ec2.should_receive('get_key_pair') self.fake_ec2.should_receive('create_key_pair').with_args('bookeyname') \ .and_return(KeyPair()) self.fake_ec2.should_receive('get_all_security_groups').and_return([]) self.fake_ec2.should_receive('create_security_group') \ .with_args('boogroup', 'AppScale security group') \ .and_return(SecurityGroup()) self.fake_ec2.should_receive('authorize_security_group') reservation = Reservation() instance = flexmock(name='instance', private_dns_name='private-ip', public_dns_name='public-ip', id='i-id', state='running', key_name='bookeyname') reservation.instances = [instance] self.fake_ec2.should_receive('get_all_instances').and_return([]) \ .and_return([reservation]) self.fake_ec2.should_receive('terminate_instances').and_return( [instance]) self.fake_ec2.should_receive('run_instances') flexmock(boto.ec2) boto.ec2.should_receive('connect_to_region').and_return(self.fake_ec2) (flexmock(utils).should_receive('get_secret').and_return('secret')) (flexmock(utils).should_receive('sleep').and_return()) (flexmock(utils).should_receive('get_random_alphanumeric').and_return( '0000000000')) (flexmock(utils).should_receive('write_key_file').and_return())
def terminate_instances(self, prefix, blocking): i = InfrastructureManager(blocking=blocking) params1 = {'infrastructure': prefix} self.assertRaises(AgentConfigurationException, i.terminate_instances, params1, 'secret') params2 = { 'credentials': { 'a': 'b', 'EC2_URL': 'http://ec2.url.com', 'EC2_ACCESS_KEY': 'access_key', 'EC2_SECRET_KEY': 'secret_key'}, 'infrastructure': prefix, 'instance_ids': ['i-12345'], 'region' : 'my-zone-1', 'keyname': 'bookeyname' } reservation = Reservation() instance = flexmock(name='instance', private_dns_name='private-ip', public_dns_name='public-ip', id='i-id', state='terminated', key_name='bookeyname', ip_address='public-ip', private_ip_address='private-ip') reservation.instances = [instance] self.fake_ec2.should_receive('get_all_instances').and_return([reservation]) flexmock(i).should_receive('_InfrastructureManager__kill_vms') result = i.terminate_instances(params2, 'secret') if not blocking: time.sleep(.1) self.assertTrue(result['success'])
def setUp(self): (flexmock(EC2Connection).should_receive('get_key_pair').and_return( None)) (flexmock(EC2Connection).should_receive('create_key_pair').with_args( 'bookeyname').and_return(KeyPair())) (flexmock(EC2Connection).should_receive( 'get_all_security_groups').and_return([])) (flexmock( EC2Connection).should_receive('create_security_group').with_args( 'boogroup', 'AppScale security group').and_return(SecurityGroup())) (flexmock(EC2Connection).should_receive( 'authorize_security_group').and_return()) reservation = Reservation() instance = Instance() instance.private_dns_name = 'private-ip' instance.public_dns_name = 'public-ip' instance.id = 'i-id' instance._state.name = 'running' instance.key_name = 'bookeyname' reservation.instances = [instance] (flexmock(EC2Connection).should_receive( 'get_all_instances').and_return([]).and_return([reservation])) (flexmock(EC2Connection).should_receive( 'terminate_instances').and_return([instance])) (flexmock(EC2Connection).should_receive('run_instances').and_return()) (flexmock(utils).should_receive('get_secret').and_return('secret')) (flexmock(utils).should_receive('sleep').and_return()) (flexmock(utils).should_receive('get_random_alphanumeric').and_return( '0000000000')) (flexmock(utils).should_receive('write_key_file').and_return())
def setUp(self): (flexmock(EC2Connection) .should_receive('get_key_pair') .and_return(None)) (flexmock(EC2Connection) .should_receive('create_key_pair') .with_args('bookeyname') .and_return(KeyPair())) (flexmock(EC2Connection) .should_receive('get_all_security_groups') .and_return([])) (flexmock(EC2Connection) .should_receive('create_security_group') .with_args('boogroup', 'AppScale security group') .and_return(SecurityGroup())) (flexmock(EC2Connection) .should_receive('authorize_security_group') .and_return()) reservation = Reservation() # the old implementation had a regression where public and private IPs # were getting sorted, and thus public ip1 would point to private ip 2. # to prevent this regression from resurfacing, set up the dns names so # that a sort would mess them up again. instance1 = Instance() instance1.public_dns_name = 'ABC-public-ip1' instance1.private_dns_name = 'DEF-private-ip1' instance1.id = 'i-id1' instance1._state.name = 'running' instance1.key_name = 'bookeyname' instance2 = Instance() instance2.public_dns_name = 'DEF-public-ip2' instance2.private_dns_name = 'ABC-private-ip2' instance2.id = 'i-id2' instance2._state.name = 'running' instance2.key_name = 'bookeyname' reservation.instances = [instance1, instance2] (flexmock(EC2Connection) .should_receive('get_all_instances') .and_return([]) .and_return([reservation])) (flexmock(EC2Connection) .should_receive('run_instances') .and_return()) (flexmock(utils) .should_receive('get_secret') .and_return('secret')) (flexmock(utils) .should_receive('sleep') .and_return()) (flexmock(utils) .should_receive('get_random_alphanumeric') .and_return('0000000000')) (flexmock(utils) .should_receive('write_key_file') .and_return())
def build_pending_reservations(instance_id): instance = Instance() instance.id = instance_id instance.state = 'running' instance.ip_address = '172.16.52.10' instance.private_ip_address = '172.16.52.10' r = Reservation() r.instances = [instance] return [r]
def run_instances(self, ami, *args, **kwargs): self.instances.append("instance with ami %s and key %s and groups %s" % ( ami, kwargs["key_name"], ", ".join(kwargs["security_groups"]) )) instance = Instance() instance.id = 'i-00000302' reservation = Reservation() reservation.instances = [instance] return reservation
def add_instances(self, image_id, count, user_data): new_reservation = Reservation() new_reservation.id = random_reservation_id() for index in range(count): new_instance = Instance( image_id, user_data, ) new_reservation.instances.append(new_instance) self.reservations[new_reservation.id] = new_reservation return new_reservation
def add_instances(self, image_id, count): new_reservation = Reservation() new_reservation.id = random_reservation_id() for index in range(count): new_instance = Instance() new_instance.id = random_instance_id() new_instance.image_id = image_id new_instance._state_name = "pending" new_instance._state_code = 0 new_reservation.instances.append(new_instance) self.reservations[new_reservation.id] = new_reservation return new_reservation
def add_instances(self, image_id, count, user_data, security_group_names): new_reservation = Reservation() new_reservation.id = random_reservation_id() security_groups = [self.get_security_group_from_name(name) for name in security_group_names] for index in range(count): new_instance = Instance( image_id, user_data, security_groups, ) new_reservation.instances.append(new_instance) self.reservations[new_reservation.id] = new_reservation return new_reservation
def setUp(self): (flexmock(EC2Connection) .should_receive('get_key_pair') .and_return(None)) (flexmock(EC2Connection) .should_receive('create_key_pair') .with_args('bookeyname') .and_return(KeyPair())) (flexmock(EC2Connection) .should_receive('get_all_security_groups') .and_return([])) (flexmock(EC2Connection) .should_receive('create_security_group') .with_args('boogroup', 'AppScale security group') .and_return(SecurityGroup())) (flexmock(EC2Connection) .should_receive('authorize_security_group') .and_return()) reservation = Reservation() instance = Instance() instance.private_dns_name = 'private-ip' instance.public_dns_name = 'public-ip' instance.id = 'i-id' instance._state.name = 'running' instance.key_name = 'bookeyname' reservation.instances = [instance] (flexmock(EC2Connection) .should_receive('get_all_instances') .and_return([]) .and_return([reservation])) (flexmock(EC2Connection) .should_receive('terminate_instances') .and_return([instance])) (flexmock(EC2Connection) .should_receive('run_instances') .and_return()) (flexmock(utils) .should_receive('get_secret') .and_return('secret')) (flexmock(utils) .should_receive('sleep') .and_return()) (flexmock(utils) .should_receive('get_random_alphanumeric') .and_return('0000000000')) (flexmock(utils) .should_receive('write_key_file') .and_return())
def add_instances(self, image_id, count, user_data, security_group_names, **kwargs): new_reservation = Reservation() new_reservation.id = random_reservation_id() security_groups = [ self.get_security_group_from_name(name) for name in security_group_names ] security_groups.extend( self.get_security_group_from_id(sg_id) for sg_id in kwargs.pop("security_group_ids", [])) for index in range(count): new_instance = Instance(image_id, user_data, security_groups, **kwargs) new_reservation.instances.append(new_instance) self.reservations[new_reservation.id] = new_reservation return new_reservation
def add_instances(self, image_id, count, user_data, security_group_names, **kwargs): new_reservation = Reservation() new_reservation.id = random_reservation_id() security_groups = [self.get_security_group_from_name(name) for name in security_group_names] security_groups.extend(self.get_security_group_from_id(sg_id) for sg_id in kwargs.pop("security_group_ids", [])) for index in range(count): new_instance = Instance( image_id, user_data, security_groups, **kwargs ) new_reservation.instances.append(new_instance) self.reservations[new_reservation.id] = new_reservation return new_reservation
def test_euca_run_instances(self): i = InfrastructureManager(blocking=True) reservation = Reservation() instance = flexmock(name='instance', private_dns_name='private-ip', public_dns_name='public-ip', id='i-id', state='running', key_name='bookeyname', ip_address='public-ip', private_ip_address='private-ip') new_instance = flexmock(name='new-instance', private_dns_name='new-private-ip', public_dns_name='new-public-ip', id='new-i-id', state='running', key_name='bookeyname', ip_address='new-public-ip', private_ip_address='new-private-ip') reservation.instances = [instance] new_reservation = Reservation() new_reservation.instances = [instance, new_instance] flexmock(EC2Connection).should_receive('get_all_instances').and_return([]) \ .and_return([reservation]).and_return([reservation]) \ .and_return([new_reservation]).and_return([new_reservation]) # first, validate that the run_instances call goes through successfully # and gives the user an operation id full_params = { 'credentials': { 'a': 'b', 'EC2_URL': 'http://testing.appscale.com:8773/foo/bar', 'EC2_ACCESS_KEY': 'access_key', 'EC2_SECRET_KEY': 'secret_key' }, 'group': 'boogroup', 'image_id': 'booid', 'infrastructure': 'euca', 'instance_type': 'booinstance_type', 'keyname': 'bookeyname', 'num_vms': '1', 'use_spot_instances': False, 'zone': 'my-zone-1b', 'autoscale_agent': True, 'IS_VERBOSE': True } id = '0000000000' # no longer randomly generated full_result = {'success': True, 'operation_id': id, 'reason': 'none'} self.assertEquals(full_result, i.run_instances(full_params, 'secret')) # next, look at run_instances internally to make sure it actually is # updating its operation info self.assertEquals(InfrastructureManager.STATE_SUCCESS, i.operation_ids.get(id)['state']) vm_info = i.operation_ids.get(id)['vm_info'] self.assertEquals(['new-public-ip'], vm_info['public_ips']) self.assertEquals(['new-private-ip'], vm_info['private_ips']) self.assertEquals(['new-i-id'], vm_info['instance_ids'])
def setUp(self): fake_ec2 = flexmock(name='fake_ec2') fake_ec2.should_receive('get_key_pair') fake_ec2.should_receive('create_key_pair').with_args('bookeyname') \ .and_return(KeyPair()) fake_ec2.should_receive('get_all_security_groups').and_return([]) fake_ec2.should_receive('create_security_group') \ .with_args('boogroup', 'AppScale security group') \ .and_return(SecurityGroup()) fake_ec2.should_receive('authorize_security_group') reservation = Reservation() instance = Instance() instance.private_dns_name = 'private-ip' instance.public_dns_name = 'public-ip' instance.id = 'i-id' instance._state.name = 'running' instance.key_name = 'bookeyname' reservation.instances = [instance] fake_ec2.should_receive('get_all_instances').and_return([]) \ .and_return([reservation]) fake_ec2.should_receive('terminate_instances').and_return([instance]) fake_ec2.should_receive('request_spot_instances') flexmock(boto.ec2) boto.ec2.should_receive('connect_to_region').and_return(fake_ec2) (flexmock(utils) .should_receive('get_secret') .and_return('secret')) (flexmock(utils) .should_receive('sleep') .and_return()) (flexmock(utils) .should_receive('get_random_alphanumeric') .and_return('0000000000')) (flexmock(utils) .should_receive('write_key_file') .and_return())
def run_instances(self, prefix, blocking, success=True): i = InfrastructureManager(blocking=blocking) reservation = Reservation() instance = flexmock(name='instance', private_dns_name='private-ip', public_dns_name='public-ip', id='i-id', state='running', key_name='bookeyname', ip_address='public-ip', private_ip_address='private-ip') new_instance = flexmock(name='new-instance', private_dns_name='new-private-ip', public_dns_name='new-public-ip', id='new-i-id', state='running', key_name='bookeyname', ip_address='new-public-ip', private_ip_address='new-private-ip') reservation.instances = [instance] new_reservation = Reservation() new_reservation.instances = [instance, new_instance] self.fake_ec2.should_receive('get_all_instances').and_return([]) \ .and_return([reservation]).and_return([new_reservation]) # first, validate that the run_instances call goes through successfully # and gives the user a reservation id full_params = { 'credentials': { 'a': 'b', 'EC2_URL': 'http://testing.appscale.com:8773/foo/bar', 'EC2_ACCESS_KEY': 'access_key', 'EC2_SECRET_KEY': 'secret_key'}, 'group': 'boogroup', 'image_id': 'booid', 'infrastructure': prefix, 'instance_type': 'booinstance_type', 'keyname': 'bookeyname', 'num_vms': '1', 'use_spot_instances': False, 'region' : 'my-zone-1', 'zone' : 'my-zone-1b', 'autoscale_agent': True } id = '0000000000' # no longer randomly generated full_result = { 'success': True, 'reservation_id': id, 'reason': 'none' } if success: self.assertEquals(full_result, i.run_instances(full_params, 'secret')) # next, look at run_instances internally to make sure it actually is # updating its reservation info if not blocking: time.sleep(.1) if success: self.assertEquals(InfrastructureManager.STATE_RUNNING, i.reservations.get(id)['state']) vm_info = i.reservations.get(id)['vm_info'] self.assertEquals(['new-public-ip'], vm_info['public_ips']) self.assertEquals(['new-private-ip'], vm_info['private_ips']) self.assertEquals(['new-i-id'], vm_info['instance_ids']) else: if blocking: self.assertRaises(AgentRuntimeException, i.run_instances, full_params, 'secret')
def test_euca_run_instances(self): i = self.factory.create_agent('euca') reservation = Reservation() instance = flexmock(name='instance', private_dns_name='private-ip', public_dns_name='public-ip', id='i-id', state='running', key_name='bookeyname', ip_address='public-ip', private_ip_address='private-ip') new_instance = flexmock(name='new-instance', private_dns_name='new-private-ip', public_dns_name='new-public-ip', id='new-i-id', state='running', key_name='bookeyname', ip_address='new-public-ip', private_ip_address='new-private-ip') reservation.instances = [instance] new_reservation = Reservation() new_reservation.instances = [instance, new_instance] flexmock(EC2Connection).should_receive('get_all_instances').and_return([]) \ .and_return([reservation]).and_return([reservation]) \ .and_return([new_reservation]).and_return([new_reservation]) # first, validate that the run_instances call goes through successfully # and gives the user an operation id full_params = { 'credentials': { 'a': 'b', 'EC2_URL': 'http://testing.appscale.com:8773/foo/bar', 'EC2_ACCESS_KEY': 'access_key', 'EC2_SECRET_KEY': 'secret_key' }, 'group': 'boogroup', 'image_id': 'booid', 'infrastructure': 'euca', 'instance_type': 'booinstance_type', 'keyname': 'bookeyname', 'num_vms': '1', 'use_spot_instances': False, 'zone': 'my-zone-1b', 'autoscale_agent': True, 'IS_VERBOSE': True } id = '0000000000' # no longer randomly generated agent_run_instances_result = (['new-i-id'], ['new-public-ip'], ['new-private-ip']) security_configured = i.configure_instance_security(full_params) self.assertTupleEqual( agent_run_instances_result, i.run_instances(int(full_params['num_vms']), full_params, security_configured, public_ip_needed=False))
def setUp(self): self.factory = InfrastructureAgentFactory() self.fake_ec2 = flexmock(name='self.fake_ec2') flexmock(boto.ec2) boto.ec2.should_receive('connect_to_region').and_return(self.fake_ec2) reservation = Reservation() instance = flexmock(name='instance', private_dns_name='private-ip', public_dns_name='public-ip', id='i-aabbccee', state='running', key_name='bookeyname', ip_address='public-ip', private_ip_address='private-ip') new_instance = flexmock(name='new-instance', private_dns_name='new-private-ip', public_dns_name='new-public-ip', id='i-aabbccff', state='running', key_name='bookeyname', ip_address='new-public-ip', private_ip_address='new-private-ip') t_aabbccee = flexmock(name='instance', private_dns_name='private-ip', public_dns_name='public-ip', id='i-aabbccee', state='terminated', key_name='bookeyname', ip_address='public-ip', private_ip_address='private-ip') t_aabbccff = flexmock(name='new-instance', private_dns_name='new-private-ip', public_dns_name='new-public-ip', id='i-aabbccff', state='terminated', key_name='bookeyname', ip_address='new-public-ip', private_ip_address='new-private-ip') t_aabbccdd = flexmock(name='instance', private_dns_name='private-ip', public_dns_name='public-ip', id='i-aabbccdd', state='terminated', key_name='bookeyname', ip_address='public-ip', private_ip_address='private-ip') reservation.instances = [instance] new_reservation = Reservation() new_reservation.instances = [instance, new_instance] # For testing wait_for_status self.good_reservation = Reservation() self.good_reservation.instances = [instance, new_instance] self.good_reservations = [self.good_reservation] self.terminated_reservation = Reservation() # Used because we filter for 'terminated' and it should return no instances.. self.empty_reservation = Reservation() self.empty_reservation.instances = [] self.empty_reservations = [self.empty_reservation] self.terminated_reservation.instances = [t_aabbccee, t_aabbccff, t_aabbccdd] self.terminated_reservations = [self.terminated_reservation] self.instance_notfound_body = """ <Response xmlns=""><Errors> <Error> <Code>InvalidInstanceID.NotFound</Code> <Message>The instance ID '{}' does not exist</Message> </Error></Errors> <RequestID>23548a3a-d6c8-4de7-b846-4e072587c582</RequestID></Response> """ self.multiple_instance_notfound_body = """ <Response xmlns=""><Errors> <Error> <Code>InvalidInstanceID.NotFound</Code> <Message>The instance IDs '{}' do not exist</Message> </Error></Errors> <RequestID>23548a3a-d6c8-4de7-b846-4e072587c582</RequestID></Response> """ self.full_params = { 'credentials': { 'a': 'b', 'EC2_URL': 'http://testing.appscale.com:8773/foo/bar', 'EC2_ACCESS_KEY': 'access_key', 'EC2_SECRET_KEY': 'secret_key'}, 'group': 'boogroup', 'image_id': 'booid', 'infrastructure': 'ec2', 'instance_type': 'booinstance_type', 'keyname': 'bookeyname', 'num_vms': '1', 'use_spot_instances': False, 'region': 'my-zone-1', 'zone': 'my-zone-1b', 'autoscale_agent': True, }