def test_appscale_in_one_node_cloud_deployment_auto_spot_price(self): # let's say that appscale isn't already running local_appscale_path = os.path.expanduser("~") + os.sep + ".appscale" + \ os.sep + self.keyname + ".key" self.local_state.should_receive( 'ensure_appscale_isnt_running').and_return() self.local_state.should_receive('make_appscale_directory').and_return() self.local_state.should_receive('get_key_path_from_name').and_return( local_appscale_path) # mock out talking to logs.appscale.com fake_connection = flexmock(name='fake_connection') fake_connection.should_receive('request').with_args( 'POST', '/upload', str, AppScaleLogger.HEADERS).and_return() flexmock(httplib) httplib.should_receive('HTTPConnection').with_args('logs.appscale.com') \ .and_return(fake_connection) # mock out generating the secret key flexmock(uuid) uuid.should_receive('uuid4').and_return('the secret') # mock out writing the secret key to ~/.appscale, as well as reading it # later secret_key_location = AppScaleState.ssh_key(self.keyname) fake_secret = flexmock(name="fake_secret") fake_secret.should_receive('read').and_return('the secret') fake_secret.should_receive('write').and_return() self.builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) self.builtins.should_receive('open').with_args(secret_key_location, 'w') \ .and_return(fake_secret) # mock out writing the secret key to ~/.appscale, as well as reading it # later secret_key_location = LocalState.get_secret_key_location(self.keyname) fake_secret = flexmock(name="fake_secret") fake_secret.should_receive('read').and_return('the secret') fake_secret.should_receive('write').and_return() self.builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) self.builtins.should_receive('open').with_args(secret_key_location, 'w') \ .and_return(fake_secret) self.setup_ec2_mocks() # slip in some fake spot instance info fake_entry = flexmock(name='fake_entry', price=1) self.fake_ec2.should_receive('get_spot_price_history').with_args( start_time=str, end_time=str, product_description='Linux/UNIX', instance_type='m3.medium', availability_zone='my-zone-1b').and_return([fake_entry]) # also mock out acquiring a spot instance self.fake_ec2.should_receive('request_spot_instances').with_args( '1.1', 'ami-ABCDEFG', key_name=self.keyname, network_interfaces=None, security_groups=[self.group], instance_type='m3.medium', count=1, placement='my-zone-1b') # Don't write local metadata files. flexmock(LocalState).should_receive('update_local_metadata') # assume that root login is not enabled self.local_state.should_receive('shell').with_args( re.compile('ssh'), None, 5, stdin='ls').and_return( 'Please login as the user "ubuntu" rather than the user "root"' ) # assume that we can enable root login self.local_state.should_receive('shell').with_args( re.compile('ssh'), None, 5, stdin='sudo touch /root/.ssh/authorized_keys').and_return() self.local_state.should_receive('shell').with_args( re.compile('ssh'), None, 5, stdin='sudo chmod 600 /root/.ssh/authorized_keys').and_return() self.local_state.should_receive('shell').with_args( re.compile('ssh'), None, 5, stdin='mktemp').and_return() self.local_state.should_receive('shell').with_args( re.compile('ssh'), None, 5, stdin=re.compile( 'sudo sort -u ~/.ssh/authorized_keys /root/.ssh/authorized_keys -o ' )).and_return() self.local_state.should_receive('shell').with_args( re.compile('ssh'), None, 5, stdin=re.compile( 'sudo sed -n ' '\'\/\.\*Please login\/d; w\/root\/\.ssh\/authorized_keys\' ') ).and_return() self.local_state.should_receive('shell').with_args( re.compile('ssh'), None, 5, stdin=re.compile('rm -f ')).and_return() self.local_state.should_receive('shell').with_args( re.compile('ssh'), None, 5, stdin=re.compile('rm -rf ')).and_return() # and assume that we can copy over our ssh keys fine self.local_state.should_receive('shell').\ with_args(re.compile('scp .*[r|d]sa'), None, 5).and_return() self.local_state.should_receive('shell').\ with_args(re.compile('scp .*{0}'.format(self.keyname)), None, 5).\ and_return() self.setup_appscale_compatibility_mocks() # mock out generating the private key self.local_state.should_receive('shell').with_args( re.compile('openssl'), stdin=None) self.local_state.should_receive('shell').with_args( re.compile('^ssh'), None, 5, stdin='systemctl start appscale-controller') self.setup_socket_mocks('elastic-ip') self.setup_appcontroller_mocks('elastic-ip', 'private1') # mock out reading the locations.json file, and slip in our own json self.local_state.should_receive('get_local_nodes_info').and_return( json.loads( json.dumps([{ "public_ip": "elastic-ip", "private_ip": "private1", "roles": ["shadow"] }]))) # copying over the locations yaml and json files should be fine self.local_state.should_receive('shell').with_args( re.compile('scp'), None, 5, stdin=re.compile('locations-{0}'.format(self.keyname))) # same for the secret key self.local_state.should_receive('shell').with_args( re.compile('scp'), None, 5, stdin=re.compile('{0}.secret'.format(self.keyname))) flexmock(RemoteHelper).should_receive('copy_deployment_credentials') flexmock(AppControllerClient) AppControllerClient.should_receive('does_user_exist').and_return(True) AppControllerClient.should_receive('get_property').\ and_return({'login': '******'}) # Let's mock the call to describe_instances when checking for old # instances to re-use, and then to start the headnode. pending_instance = flexmock(name='pending_instance', state='pending', key_name=self.keyname, id='i-ABCDEFG') pending_reservation = flexmock(name='pending_reservation', instances=[pending_instance]) no_instances = flexmock(name='no_instances', instances=[]) running_instance = flexmock(name='running_instance', state='running', key_name=self.keyname, id='i-ABCDEFG', ip_address='public1', private_ip_address='private1') running_reservation = flexmock(name='running_reservation', instances=[running_instance]) self.fake_ec2.should_receive('get_all_instances') \ .and_return(no_instances) \ .and_return(pending_reservation) \ .and_return(running_reservation) argv = [ "--min", "1", "--max", "1", "--infrastructure", "ec2", "--machine", "ami-ABCDEFG", "--instance_type", "m3.medium", "--use_spot_instances", "--keyname", self.keyname, "--group", self.group, "--test", "--zone", "my-zone-1b", "--static_ip", "elastic-ip", "--EC2_ACCESS_KEY", "baz", "--EC2_SECRET_KEY", "baz" ] options = ParseArgs(argv, self.function).args AppScaleTools.run_instances(options)
def test_ssh_key(self): actual = AppScaleState.ssh_key(self.default_keyname) expected = os.path.join(self.default_configdir, "{0}.key".format(self.default_keyname)) self.assertEqual(expected, actual)