def setUp(self): self.keyname = "boobazblargfoo" self.function = "appscale-add-keypair" # mock out any writing to stdout flexmock(AppScaleLogger) AppScaleLogger.should_receive('log').and_return() # mock out all sleeping flexmock(time) time.should_receive('sleep').and_return() # throw some default mocks together for when invoking via shell succeeds # and when it fails self.fake_temp_file = flexmock(name='fake_temp_file') self.fake_temp_file.should_receive('seek').with_args(0).and_return() self.fake_temp_file.should_receive('read').and_return('boo out') self.fake_temp_file.should_receive('close').and_return() flexmock(tempfile) tempfile.should_receive('NamedTemporaryFile').and_return(self.fake_temp_file) self.success = flexmock(name='success', returncode=0) self.success.should_receive('wait').and_return(0) self.failed = flexmock(name='success', returncode=1) self.failed.should_receive('wait').and_return(1)
def setUp(self): self.keyname = "boobazblargfoo" self.function = "appscale-terminate-instances" # mock out any writing to stdout flexmock(AppScaleLogger) AppScaleLogger.should_receive("log").and_return() AppScaleLogger.should_receive("verbose").and_return() # mock out all sleeping flexmock(time) time.should_receive("sleep").and_return() local_state = flexmock(LocalState) local_state.should_receive("shell").and_return("") # throw some default mocks together for when invoking via shell succeeds # and when it fails self.fake_temp_file = flexmock(name="fake_temp_file") self.fake_temp_file.should_receive("read").and_return("boo out") self.fake_temp_file.should_receive("close").and_return() self.fake_temp_file.should_receive("seek").with_args(0).and_return() flexmock(tempfile) tempfile.should_receive("NamedTemporaryFile").and_return(self.fake_temp_file) self.success = flexmock(name="success", returncode=0) self.success.should_receive("wait").and_return(0) self.failed = flexmock(name="failed", returncode=1) self.failed.should_receive("wait").and_return(1) # throw in some mocks that assume our EC2 environment variables are set for credential in EC2Agent.REQUIRED_EC2_CREDENTIALS: os.environ[credential] = "baz"
def setUp(self): self.keyname = "boobazblargfoo" self.function = "appscale-gather-logs" # mock out any writing to stdout flexmock(AppScaleLogger) AppScaleLogger.should_receive('log').and_return() # mock out all sleeping flexmock(time) time.should_receive('sleep').and_return() # throw some default mocks together for when invoking via shell succeeds # and when it fails self.fake_temp_file = flexmock(name='fake_temp_file') self.fake_temp_file.should_receive('read').and_return('boo out') self.fake_temp_file.should_receive('close').and_return() self.fake_temp_file.should_receive('seek').with_args(0).and_return() flexmock(tempfile) tempfile.should_receive('NamedTemporaryFile').and_return( self.fake_temp_file) self.success = flexmock(name='success', returncode=0) self.success.should_receive('wait').and_return(0) self.failed = flexmock(name='success', returncode=1) self.failed.should_receive('wait').and_return(1)
def setUp(self): self.keyname = "boobazblargfoo" self.function = "appscale-gather-logs" # mock out any writing to stdout flexmock(AppScaleLogger) AppScaleLogger.should_receive("log").and_return() # mock out all sleeping flexmock(time) time.should_receive("sleep").and_return() # throw some default mocks together for when invoking via shell succeeds # and when it fails self.fake_temp_file = flexmock(name="fake_temp_file") self.fake_temp_file.should_receive("read").and_return("boo out") self.fake_temp_file.should_receive("close").and_return() self.fake_temp_file.should_receive("seek").with_args(0).and_return() flexmock(tempfile) tempfile.should_receive("NamedTemporaryFile").and_return(self.fake_temp_file) self.success = flexmock(name="success", returncode=0) self.success.should_receive("wait").and_return(0) self.failed = flexmock(name="success", returncode=1) self.failed.should_receive("wait").and_return(1)
def setUp(self): self.keyname = "boobazblargfoo" self.function = "appscale-upload-app" self.app_dir = "/tmp/baz/gbaz" # mock out the check to make sure our app is a directory flexmock(os.path) os.path.should_call('isdir') os.path.should_receive('isdir').with_args( self.app_dir).and_return(True) # mock out any writing to stdout flexmock(AppScaleLogger) AppScaleLogger.should_receive('log').and_return() AppScaleLogger.should_receive('success').and_return() # mock out all sleeping flexmock(time) time.should_receive('sleep').and_return() local_state = flexmock(LocalState) local_state.should_receive('shell').and_return() # throw some default mocks together for when invoking via shell succeeds # and when it fails self.fake_temp_file = flexmock(name='fake_temp_file') self.fake_temp_file.should_receive('read').and_return('boo out') self.fake_temp_file.should_receive('close').and_return() self.fake_temp_file.should_receive('seek').with_args(0).and_return() flexmock(tempfile) tempfile.should_receive('NamedTemporaryFile').and_return( self.fake_temp_file) self.success = flexmock(name='success', returncode=0) self.success.should_receive('wait').and_return(0) self.failed = flexmock(name='success', returncode=1) self.failed.should_receive('wait').and_return(1) # mock out generating a random app dir, for later mocks flexmock(uuid) uuid.should_receive('uuid4').and_return('1234')
def setUp(self): self.keyname = "boobazblargfoo" self.function = "appscale-upload-app" self.app_dir = "/tmp/baz/gbaz" # mock out the check to make sure our app is a directory flexmock(os.path) os.path.should_call('isdir') os.path.should_receive('isdir').with_args(self.app_dir).and_return(True) # mock out any writing to stdout flexmock(AppScaleLogger) AppScaleLogger.should_receive('log').and_return() AppScaleLogger.should_receive('success').and_return() # mock out all sleeping flexmock(time) time.should_receive('sleep').and_return() local_state = flexmock(LocalState) local_state.should_receive('shell').and_return() # throw some default mocks together for when invoking via shell succeeds # and when it fails self.fake_temp_file = flexmock(name='fake_temp_file') self.fake_temp_file.should_receive('read').and_return('boo out') self.fake_temp_file.should_receive('close').and_return() self.fake_temp_file.should_receive('seek').with_args(0).and_return() flexmock(tempfile) tempfile.should_receive('NamedTemporaryFile').and_return(self.fake_temp_file) self.success = flexmock(name='success', returncode=0) self.success.should_receive('wait').and_return(0) self.failed = flexmock(name='success', returncode=1) self.failed.should_receive('wait').and_return(1) # mock out generating a random app dir, for later mocks flexmock(uuid) uuid.should_receive('uuid4').and_return('1234')
def setUp(self): self.keyname = "boobazblargfoo" self.function = "appscale-run-instances" # mock out any writing to stdout flexmock(AppScaleLogger) AppScaleLogger.should_receive('log').and_return() AppScaleLogger.should_receive('success').and_return() # mock out all sleeping flexmock(time) time.should_receive('sleep').and_return() # throw some default mocks together for when invoking via shell succeeds # and when it fails self.fake_temp_file = flexmock(name='fake_temp_file') self.fake_temp_file.should_receive('seek').with_args(0).and_return() self.fake_temp_file.should_receive('read').and_return('boo out') self.fake_temp_file.should_receive('close').and_return() self.fake_input_file = flexmock(name='fake_input_file') self.fake_input_file.should_receive('seek').with_args(0).and_return() self.fake_input_file.should_receive('write').and_return() self.fake_input_file.should_receive('read').and_return('boo out') self.fake_input_file.should_receive('close').and_return() flexmock(tempfile) tempfile.should_receive('NamedTemporaryFile')\ .and_return(self.fake_temp_file) tempfile.should_receive('TemporaryFile').and_return(self.fake_input_file) self.success = flexmock(name='success', returncode=0) self.success.should_receive('wait').and_return(0) self.failed = flexmock(name='failed', returncode=1) self.failed.should_receive('wait').and_return(1) # throw in some mocks that assume our EC2 environment variables are set for credential in EC2Agent.REQUIRED_EC2_CREDENTIALS: os.environ[credential] = "baz"
def setUp(self): self.keyname = "boobazblargfoo" self.group = "bazgroup" self.function = "appscale-run-instances" # mock out any writing to stdout flexmock(AppScaleLogger) AppScaleLogger.should_receive('log').and_return() AppScaleLogger.should_receive('success').and_return() # mock out all sleeping flexmock(time) time.should_receive('sleep').and_return() # pretend we have an appscalefile at the right location, and that it # specifies the keyname and group appscalefile_path = os.getcwd() + os.sep + "AppScalefile" flexmock(os.path) os.path.should_call('exists') os.path.should_receive('exists').with_args(appscalefile_path) \ .and_return(True) appscalefile_contents = """ keyname: {0} group: {1} """.format(self.keyname, self.group) self.builtins = flexmock(sys.modules['__builtin__']) self.builtins.should_call('open') # set the fall-through fake_appscalefile = flexmock(name="fake_appscalefile") fake_appscalefile.should_receive('read').and_return(appscalefile_contents) fake_appscalefile.should_receive('write').and_return() self.builtins.should_receive('open').with_args(appscalefile_path, 'r') \ .and_return(fake_appscalefile) self.builtins.should_receive('open').with_args(appscalefile_path, 'w') \ .and_return(fake_appscalefile) # throw some default mocks together for when invoking via shell succeeds # and when it fails self.fake_temp_file = flexmock(name='fake_temp_file') self.fake_temp_file.should_receive('seek').with_args(0).and_return() self.fake_temp_file.should_receive('read').and_return('boo out') self.fake_temp_file.should_receive('close').and_return() self.fake_input_file = flexmock(name='fake_input_file') self.fake_input_file.should_receive('seek').with_args(0).and_return() self.fake_input_file.should_receive('write').and_return() self.fake_input_file.should_receive('read').and_return('boo out') self.fake_input_file.should_receive('close').and_return() flexmock(tempfile) tempfile.should_receive('NamedTemporaryFile')\ .and_return(self.fake_temp_file) tempfile.should_receive('TemporaryFile').and_return(self.fake_input_file) self.success = flexmock(name='success', returncode=0) self.success.should_receive('wait').and_return(0) self.failed = flexmock(name='failed', returncode=1) self.failed.should_receive('wait').and_return(1) # throw in some mocks that assume our EC2 environment variables are set for credential in EC2Agent.REQUIRED_EC2_CREDENTIALS: os.environ[credential] = "baz" # mock out interactions with AWS self.fake_ec2 = flexmock(name='fake_ec2') # And add in mocks for libraries most of the tests mock out self.local_state = flexmock(LocalState)
def setUp(self): # mock out all logging, since it clutters our output flexmock(AppScaleLogger) AppScaleLogger.should_receive('log').and_return() # mock out all sleeps, as they aren't necessary for unit testing flexmock(time) time.should_receive('sleep').and_return() # set up some fake options so that we don't have to generate them via # ParseArgs self.options = flexmock(infrastructure='ec2', group='boogroup', machine='ami-ABCDEFG', instance_type='m1.large', keyname='bookey', table='cassandra', verbose=False, test=False, use_spot_instances=False, zone='my-zone-1b', static_ip=None, replication=None, appengine=None, autoscale=None, user_commands=[], flower_password='', max_memory='400', ips=FOUR_NODE_CLOUD) self.my_id = "12345" self.node_layout = NodeLayout(self.options) # set up phony AWS credentials for each test # ones that test not having them present can # remove them for credential in EucalyptusAgent.REQUIRED_EC2_CREDENTIALS: os.environ[credential] = "baz" os.environ['EC2_URL'] = "http://boo" # mock out calls to EC2 # begin by assuming that our ssh keypair doesn't exist, and thus that we # need to create it key_contents = "key contents here" fake_key = flexmock(name="fake_key", material=key_contents) fake_key.should_receive('save').with_args( os.environ['HOME'] + '/.appscale').and_return(None) fake_ec2 = flexmock(name="fake_ec2") fake_ec2.should_receive('get_key_pair').with_args('bookey') \ .and_return(None) fake_ec2.should_receive('create_key_pair').with_args('bookey') \ .and_return(fake_key) # mock out writing the secret key builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through secret_key_location = LocalState.LOCAL_APPSCALE_PATH + "bookey.secret" fake_secret = flexmock(name="fake_secret") fake_secret.should_receive('write').and_return() builtins.should_receive('open').with_args(secret_key_location, 'w') \ .and_return(fake_secret) # also, mock out the keypair writing and chmod'ing ssh_key_location = LocalState.LOCAL_APPSCALE_PATH + "bookey.key" fake_file = flexmock(name="fake_file") fake_file.should_receive('write').with_args(key_contents).and_return() builtins.should_receive('open').with_args(ssh_key_location, 'w') \ .and_return(fake_file) flexmock(os) os.should_receive('chmod').with_args(ssh_key_location, 0600).and_return() # next, assume there are no security groups up at first, but then it gets # created. udp_rule = flexmock(from_port=1, to_port=65535, ip_protocol='udp') tcp_rule = flexmock(from_port=1, to_port=65535, ip_protocol='tcp') icmp_rule = flexmock(from_port=-1, to_port=-1, ip_protocol='icmp') group = flexmock(name='boogroup', rules=[tcp_rule, udp_rule, icmp_rule]) fake_ec2.should_receive( 'get_all_security_groups').with_args().and_return([]) fake_ec2.should_receive('get_all_security_groups').with_args( 'boogroup').and_return([group]) # and then assume we can create and open our security group fine fake_ec2.should_receive('create_security_group').with_args( 'boogroup', 'AppScale security group').and_return() fake_ec2.should_receive('authorize_security_group').and_return() # next, add in mocks for run_instances # the first time around, let's say that no machines are running # the second time around, let's say that our machine is pending # and that it's up the third time around fake_pending_instance = flexmock(state='pending') fake_pending_reservation = flexmock(instances=fake_pending_instance) fake_running_instance = flexmock(state='running', key_name='bookey', id='i-12345678', ip_address=IP_1, private_ip_address=IP_1) fake_running_reservation = flexmock(instances=fake_running_instance) fake_ec2.should_receive('get_all_instances').and_return([]) \ .and_return([]) \ .and_return([fake_pending_reservation]) \ .and_return([fake_running_reservation]) # next, assume that our run_instances command succeeds fake_ec2.should_receive('run_instances').and_return() # finally, inject our mocked EC2 flexmock(boto.ec2) boto.ec2.should_receive('connect_to_region').and_return(fake_ec2) # assume that ssh comes up on the third attempt fake_socket = flexmock(name='fake_socket') fake_socket.should_receive('connect').with_args(('public1', RemoteHelper.SSH_PORT)).and_raise(Exception).and_raise(Exception) \ .and_return(None) flexmock(socket) socket.should_receive('socket').and_return(fake_socket) # throw some default mocks together for when invoking via shell succeeds # and when it fails self.fake_temp_file = flexmock(name='fake_temp_file') self.fake_temp_file.should_receive('seek').with_args(0).and_return() self.fake_temp_file.should_receive('read').and_return('boo out') self.fake_temp_file.should_receive('close').and_return() flexmock(tempfile) tempfile.should_receive('NamedTemporaryFile')\ .and_return(self.fake_temp_file) self.success = flexmock(name='success', returncode=0) self.success.should_receive('wait').and_return(0) self.failed = flexmock(name='success', returncode=1) self.failed.should_receive('wait').and_return(1) # assume that root login isn't already enabled local_state = flexmock(LocalState) local_state.should_receive('shell') \ .with_args(re.compile('^ssh .*root'), False, 1, stdin='ls') \ .and_return( 'Please login as the user "ubuntu" rather than the user "root"') # and assume that we can ssh in as ubuntu to enable root login local_state = flexmock(LocalState) local_state.should_receive('shell')\ .with_args(re.compile('^ssh .*ubuntu'),False,5)\ .and_return() # also assume that we can scp over our ssh keys local_state.should_receive('shell')\ .with_args(re.compile('scp .*/root/.ssh/id_'),False,5)\ .and_return() local_state.should_receive('shell')\ .with_args(re.compile('scp .*/root/.appscale/bookey.key'),False,5)\ .and_return()
def setUp(self): # mock out all logging, since it clutters our output flexmock(AppScaleLogger) AppScaleLogger.should_receive('log').and_return() # mock out all sleeps, as they aren't necessary for unit testing flexmock(time) time.should_receive('sleep').and_return() # set up some fake options so that we don't have to generate them via # ParseArgs self.options = flexmock(infrastructure='ec2', group='boogroup', machine='ami-ABCDEFG', instance_type='m1.large', keyname='bookey', table='cassandra', verbose=False, test=False, use_spot_instances=False, zone='my-zone-1b', static_ip=None) self.my_id = "12345" self.node_layout = NodeLayout(self.options) # set up phony AWS credentials for each test # ones that test not having them present can # remove them for credential in EucalyptusAgent.REQUIRED_EC2_CREDENTIALS: os.environ[credential] = "baz" os.environ['EC2_URL'] = "http://boo" # mock out calls to EC2 # begin by assuming that our ssh keypair doesn't exist, and thus that we # need to create it key_contents = "key contents here" fake_key = flexmock(name="fake_key", material=key_contents) fake_key.should_receive('save').with_args(os.environ['HOME']+'/.appscale').and_return(None) fake_ec2 = flexmock(name="fake_ec2") fake_ec2.should_receive('get_key_pair').with_args('bookey') \ .and_return(None) fake_ec2.should_receive('create_key_pair').with_args('bookey') \ .and_return(fake_key) # mock out writing the secret key builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through secret_key_location = LocalState.LOCAL_APPSCALE_PATH + "bookey.secret" fake_secret = flexmock(name="fake_secret") fake_secret.should_receive('write').and_return() builtins.should_receive('open').with_args(secret_key_location, 'w') \ .and_return(fake_secret) # also, mock out the keypair writing and chmod'ing ssh_key_location = LocalState.LOCAL_APPSCALE_PATH + "bookey.key" fake_file = flexmock(name="fake_file") fake_file.should_receive('write').with_args(key_contents).and_return() builtins.should_receive('open').with_args(ssh_key_location, 'w') \ .and_return(fake_file) flexmock(os) os.should_receive('chmod').with_args(ssh_key_location, 0600).and_return() # next, assume there are no security groups up at first, but then it gets # created. udp_rule = flexmock(from_port=1, to_port=65535, ip_protocol='udp') tcp_rule = flexmock(from_port=1, to_port=65535, ip_protocol='tcp') icmp_rule = flexmock(from_port=-1, to_port=-1, ip_protocol='icmp') group = flexmock(name='boogroup', rules=[tcp_rule, udp_rule, icmp_rule]) fake_ec2.should_receive('get_all_security_groups').with_args().and_return([]) fake_ec2.should_receive('get_all_security_groups').with_args('boogroup').and_return([group]) # and then assume we can create and open our security group fine fake_ec2.should_receive('create_security_group').with_args('boogroup', 'AppScale security group').and_return() fake_ec2.should_receive('authorize_security_group').and_return() # next, add in mocks for run_instances # the first time around, let's say that no machines are running # the second time around, let's say that our machine is pending # and that it's up the third time around fake_pending_instance = flexmock(state='pending') fake_pending_reservation = flexmock(instances=fake_pending_instance) fake_running_instance = flexmock(state='running', key_name='bookey', id='i-12345678', public_dns_name='public1', private_dns_name='private1') fake_running_reservation = flexmock(instances=fake_running_instance) fake_ec2.should_receive('get_all_instances').and_return([]) \ .and_return([]) \ .and_return([fake_pending_reservation]) \ .and_return([fake_running_reservation]) # next, assume that our run_instances command succeeds fake_ec2.should_receive('run_instances').and_return() # finally, inject our mocked EC2 flexmock(boto.ec2) boto.ec2.should_receive('connect_to_region').and_return(fake_ec2) # assume that ssh comes up on the third attempt fake_socket = flexmock(name='fake_socket') fake_socket.should_receive('connect').with_args(('public1', RemoteHelper.SSH_PORT)).and_raise(Exception).and_raise(Exception) \ .and_return(None) flexmock(socket) socket.should_receive('socket').and_return(fake_socket) # throw some default mocks together for when invoking via shell succeeds # and when it fails self.fake_temp_file = flexmock(name='fake_temp_file') self.fake_temp_file.should_receive('seek').with_args(0).and_return() self.fake_temp_file.should_receive('read').and_return('boo out') self.fake_temp_file.should_receive('close').and_return() flexmock(tempfile) tempfile.should_receive('NamedTemporaryFile')\ .and_return(self.fake_temp_file) self.success = flexmock(name='success', returncode=0) self.success.should_receive('wait').and_return(0) self.failed = flexmock(name='success', returncode=1) self.failed.should_receive('wait').and_return(1) # assume that root login isn't already enabled local_state = flexmock(LocalState) local_state.should_receive('shell') \ .with_args(re.compile('^ssh .*root'), False, 1, stdin='ls') \ .and_return(RemoteHelper.LOGIN_AS_UBUNTU_USER) # and assume that we can ssh in as ubuntu to enable root login local_state = flexmock(LocalState) local_state.should_receive('shell')\ .with_args(re.compile('^ssh .*ubuntu'),False,5)\ .and_return() # also assume that we can scp over our ssh keys local_state.should_receive('shell')\ .with_args(re.compile('scp .*/root/.ssh/id_'),False,5)\ .and_return() local_state.should_receive('shell')\ .with_args(re.compile('scp .*/root/.appscale/bookey.key'),False,5)\ .and_return()
def setUp(self): # mock out all logging, since it clutters our output flexmock(AppScaleLogger) AppScaleLogger.should_receive('log').and_return() # mock out all sleeps, as they aren't necessary for unit testing flexmock(time) time.should_receive('sleep').and_return() # set up some fake options so that we don't have to generate them via # ParseArgs self.options = flexmock(infrastructure='ec2', group='boogroup', machine='ami-ABCDEFG', instance_type='m1.large', keyname='bookey', table='cassandra', verbose=False) self.node_layout = NodeLayout(self.options) # mock out calls to EC2 # begin by assuming that our ssh keypair doesn't exist, and thus that we # need to create it key_contents = "key contents here" fake_key = flexmock(name="fake_key", material=key_contents) fake_ec2 = flexmock(name="fake_ec2") fake_ec2.should_receive('get_key_pair').with_args('bookey') \ .and_return(None) fake_ec2.should_receive('create_key_pair').with_args('bookey') \ .and_return(fake_key) # mock out writing the secret key builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through secret_key_location = LocalState.LOCAL_APPSCALE_PATH + "bookey.secret" fake_secret = flexmock(name="fake_secret") fake_secret.should_receive('write').and_return() builtins.should_receive('open').with_args(secret_key_location, 'w') \ .and_return(fake_secret) # also, mock out the keypair writing and chmod'ing ssh_key_location = LocalState.LOCAL_APPSCALE_PATH + "bookey.key" fake_file = flexmock(name="fake_file") fake_file.should_receive('write').with_args(key_contents).and_return() builtins.should_receive('open').with_args(ssh_key_location, 'w') \ .and_return(fake_file) flexmock(os) os.should_receive('chmod').with_args(ssh_key_location, 0600).and_return() # next, assume there are no security groups up yet fake_ec2.should_receive('get_all_security_groups').and_return([]) # and then assume we can create and open our security group fine fake_ec2.should_receive('create_security_group').with_args('boogroup', 'AppScale security group').and_return() fake_ec2.should_receive('authorize_security_group').and_return() # next, add in mocks for run_instances # the first time around, let's say that no machines are running # the second time around, let's say that our machine is pending # and that it's up the third time around fake_pending_instance = flexmock(state='pending') fake_pending_reservation = flexmock(instances=fake_pending_instance) fake_running_instance = flexmock(state='running', key_name='bookey', id='i-12345678', public_dns_name='public1', private_dns_name='private1') fake_running_reservation = flexmock(instances=fake_running_instance) fake_ec2.should_receive('get_all_instances').and_return([]) \ .and_return([fake_pending_reservation]) \ .and_return([fake_running_reservation]) # next, assume that our run_instances command succeeds fake_ec2.should_receive('run_instances').and_return() # finally, inject our mocked EC2 flexmock(boto) boto.should_receive('connect_ec2').with_args('baz', 'baz').and_return(fake_ec2) # assume that ssh comes up on the third attempt fake_socket = flexmock(name='fake_socket') fake_socket.should_receive('connect').with_args(('public1', RemoteHelper.SSH_PORT)).and_raise(Exception).and_raise(Exception) \ .and_return(None) flexmock(socket) socket.should_receive('socket').and_return(fake_socket) # throw some default mocks together for when invoking via shell succeeds # and when it fails self.fake_temp_file = flexmock(name='fake_temp_file') self.fake_temp_file.should_receive('read').and_return('boo out') self.fake_temp_file.should_receive('close').and_return() flexmock(tempfile) tempfile.should_receive('TemporaryFile').and_return(self.fake_temp_file) self.success = flexmock(name='success', returncode=0) self.success.should_receive('wait').and_return(0) self.failed = flexmock(name='success', returncode=1) self.failed.should_receive('wait').and_return(1) # and assume that we can ssh in as ubuntu to enable root login, but that # it fails the first time flexmock(subprocess) subprocess.should_receive('Popen').with_args(re.compile('ubuntu'), \ shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.failed).and_return(self.success) # also assume that we can scp over our ssh keys, but that it fails the first # time subprocess.should_receive('Popen').with_args(re.compile('/root/.ssh/id_'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.failed).and_return(self.success) subprocess.should_receive('Popen').with_args(re.compile( '/root/.appscale/bookey.key'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.failed).and_return(self.success)
def setUp(self): # mock out all logging, since it clutters our output flexmock(AppScaleLogger) AppScaleLogger.should_receive("log").and_return() # mock out all sleeps, as they aren't necessary for unit testing flexmock(time) time.should_receive("sleep").and_return() # set up some fake options so that we don't have to generate them via # ParseArgs self.options = flexmock( infrastructure="ec2", group="boogroup", machine="ami-ABCDEFG", instance_type="m1.large", keyname="bookey", table="cassandra", verbose=False, test=False, use_spot_instances=False, zone="my-zone-1b", ) self.my_id = "12345" self.node_layout = NodeLayout(self.options) # set up phony AWS credentials for each test # ones that test not having them present can # remove them for credential in EucalyptusAgent.REQUIRED_EC2_CREDENTIALS: os.environ[credential] = "baz" os.environ["EC2_URL"] = "http://boo" # mock out calls to EC2 # begin by assuming that our ssh keypair doesn't exist, and thus that we # need to create it key_contents = "key contents here" fake_key = flexmock(name="fake_key", material=key_contents) fake_key.should_receive("save").with_args(os.environ["HOME"] + "/.appscale").and_return(None) fake_ec2 = flexmock(name="fake_ec2") fake_ec2.should_receive("get_key_pair").with_args("bookey").and_return(None) fake_ec2.should_receive("create_key_pair").with_args("bookey").and_return(fake_key) # mock out writing the secret key builtins = flexmock(sys.modules["__builtin__"]) builtins.should_call("open") # set the fall-through secret_key_location = LocalState.LOCAL_APPSCALE_PATH + "bookey.secret" fake_secret = flexmock(name="fake_secret") fake_secret.should_receive("write").and_return() builtins.should_receive("open").with_args(secret_key_location, "w").and_return(fake_secret) # also, mock out the keypair writing and chmod'ing ssh_key_location = LocalState.LOCAL_APPSCALE_PATH + "bookey.key" fake_file = flexmock(name="fake_file") fake_file.should_receive("write").with_args(key_contents).and_return() builtins.should_receive("open").with_args(ssh_key_location, "w").and_return(fake_file) flexmock(os) os.should_receive("chmod").with_args(ssh_key_location, 0600).and_return() # next, assume there are no security groups up at first, but then it gets # created. udp_rule = flexmock(from_port=1, to_port=65535, ip_protocol="udp") tcp_rule = flexmock(from_port=1, to_port=65535, ip_protocol="tcp") icmp_rule = flexmock(from_port=-1, to_port=-1, ip_protocol="icmp") group = flexmock(name="boogroup", rules=[tcp_rule, udp_rule, icmp_rule]) fake_ec2.should_receive("get_all_security_groups").with_args().and_return([]) fake_ec2.should_receive("get_all_security_groups").with_args("boogroup").and_return([group]) # and then assume we can create and open our security group fine fake_ec2.should_receive("create_security_group").with_args("boogroup", "AppScale security group").and_return() fake_ec2.should_receive("authorize_security_group").and_return() # next, add in mocks for run_instances # the first time around, let's say that no machines are running # the second time around, let's say that our machine is pending # and that it's up the third time around fake_pending_instance = flexmock(state="pending") fake_pending_reservation = flexmock(instances=fake_pending_instance) fake_running_instance = flexmock( state="running", key_name="bookey", id="i-12345678", public_dns_name="public1", private_dns_name="private1" ) fake_running_reservation = flexmock(instances=fake_running_instance) fake_ec2.should_receive("get_all_instances").and_return([]).and_return([fake_pending_reservation]).and_return( [fake_running_reservation] ) # next, assume that our run_instances command succeeds fake_ec2.should_receive("run_instances").and_return() # finally, inject our mocked EC2 flexmock(boto) boto.should_receive("connect_ec2").and_return(fake_ec2) # assume that ssh comes up on the third attempt fake_socket = flexmock(name="fake_socket") fake_socket.should_receive("connect").with_args(("public1", RemoteHelper.SSH_PORT)).and_raise( Exception ).and_raise(Exception).and_return(None) flexmock(socket) socket.should_receive("socket").and_return(fake_socket) # throw some default mocks together for when invoking via shell succeeds # and when it fails self.fake_temp_file = flexmock(name="fake_temp_file") self.fake_temp_file.should_receive("seek").with_args(0).and_return() self.fake_temp_file.should_receive("read").and_return("boo out") self.fake_temp_file.should_receive("close").and_return() flexmock(tempfile) tempfile.should_receive("NamedTemporaryFile").and_return(self.fake_temp_file) self.success = flexmock(name="success", returncode=0) self.success.should_receive("wait").and_return(0) self.failed = flexmock(name="success", returncode=1) self.failed.should_receive("wait").and_return(1) # assume that root login isn't already enabled local_state = flexmock(LocalState) local_state.should_receive("shell").with_args(re.compile("^ssh .*root"), False, 1, stdin="ls").and_return( "Please login as the ubuntu user rather than root user." ) # and assume that we can ssh in as ubuntu to enable root login local_state = flexmock(LocalState) local_state.should_receive("shell").with_args(re.compile("^ssh .*ubuntu"), False, 5).and_return() # also assume that we can scp over our ssh keys local_state.should_receive("shell").with_args(re.compile("scp .*/root/.ssh/id_"), False, 5).and_return() local_state.should_receive("shell").with_args( re.compile("scp .*/root/.appscale/bookey.key"), False, 5 ).and_return()