def test_fails_if_app_isnt_running(self): # If the user wants to relocate their app to port X, but their app isn't # even running, this should fail. # Assume that the AppController is running but our app isn't. flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_json_location(self.keyname)).and_return(True) fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('get_app_info_map').with_args( 'the secret').and_return(json.dumps({})) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) argv = [ '--keyname', self.keyname, '--appname', self.appid, '--http_port', '80', '--https_port', '443' ] options = ParseArgs(argv, self.function).args self.assertRaises(AppScaleException, AppScaleTools.relocate_app, options)
def test_all_ok(self): # If the user wants to relocate their app to port X, and nothing else # runs on that port, this should succeed. # Assume that the AppController is running, so is our app, and that other # apps are not running on port 80. fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('get_app_info_map').with_args( 'the secret').and_return( json.dumps({ self.appid: { 'nginx': 8080 }, 'a-different-app': { 'nginx': 81 } })) fake_appcontroller.should_receive('relocate_app').with_args( self.appid, 80, 443, 'the secret').and_return("OK") flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) rh = flexmock(RemoteHelper) rh.should_receive('sleep_until_port_is_open').and_return() argv = [ '--keyname', self.keyname, '--appname', self.appid, '--http_port', '80', '--https_port', '443' ] options = ParseArgs(argv, self.function).args AppScaleTools.relocate_app(options)
def test_wait_for_machines_to_finish_loading(self): # mock out reading 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("read").and_return("the secret") builtins.should_receive("open").with_args(secret_key_location, "r").and_return(fake_secret) # mock out getting all the ips in the deployment from the head node fake_soap = flexmock(name="fake_soap") fake_soap.should_receive("get_all_public_ips").with_args("the secret").and_return( json.dumps(["public1", "public2"]) ) role_info = [ {"public_ip": "public1", "private_ip": "private1", "jobs": ["shadow", "db_master"]}, {"public_ip": "public2", "private_ip": "private2", "jobs": ["appengine"]}, ] fake_soap.should_receive("get_role_info").with_args("the secret").and_return(json.dumps(role_info)) # also, let's say that our machines aren't running the first time we ask, # but that they are the second time fake_soap.should_receive("is_done_initializing").with_args("the secret").and_return(False).and_return(True) flexmock(SOAPpy) SOAPpy.should_receive("SOAPProxy").with_args("https://public1:17443").and_return(fake_soap) SOAPpy.should_receive("SOAPProxy").with_args("https://public2:17443").and_return(fake_soap) RemoteHelper.wait_for_machines_to_finish_loading("public1", "bookey")
def setup_appcontroller_mocks(self, public_ip, private_ip): # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('set_parameters').with_args(str, list, ['none'], 'the secret').and_return('OK') fake_appcontroller.should_receive('get_all_public_ips').with_args('the secret') \ .and_return(json.dumps([public_ip])) role_info = [{ 'public_ip' : public_ip, 'private_ip' : private_ip, 'jobs' : ['shadow', 'login'] }] fake_appcontroller.should_receive('get_role_info').with_args('the secret') \ .and_return(json.dumps(role_info)) fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('nothing interesting here') \ .and_return('Database is at not-up-yet') \ .and_return('Database is at {0}'.format(public_ip)) fake_appcontroller.should_receive('is_done_initializing') \ .and_return(False) \ .and_return(True) fake_appcontroller.should_receive('is_initialized').and_return(True) fake_appcontroller.should_receive('does_user_exist').and_return(False) fake_appcontroller.should_receive('set_admin_role').and_return() fake_appcontroller.should_receive('create_user').with_args( '*****@*****.**', str, 'xmpp_user', 'the secret') \ .and_return('true') fake_appcontroller.should_receive('create_user').with_args( 'a@' + public_ip, str, 'xmpp_user', 'the secret') \ .and_return('true') flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://{0}:17443'.format( public_ip)).and_return(fake_appcontroller)
def setup_appcontroller_mocks(self, public_ip, private_ip): # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('set_parameters').\ with_args(str, str, 'the secret').and_return('OK') fake_appcontroller.should_receive('get_all_public_ips').\ with_args('the secret').and_return(json.dumps([public_ip])) role_info = [{ 'public_ip' : public_ip, 'private_ip' : private_ip, 'jobs' : ['shadow', 'login'], 'instance_id': 'i-APPSCALE' }] fake_appcontroller.should_receive('get_role_info').with_args('the secret') \ .and_return(json.dumps(role_info)) fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('nothing interesting here') \ .and_return('Database is at not-up-yet') \ .and_return('Database is at {0}'.format(public_ip)) fake_appcontroller.should_receive('is_done_initializing') \ .and_return(False) \ .and_return(True) fake_appcontroller.should_receive('is_initialized').and_return(True) fake_appcontroller.should_receive('does_user_exist').and_return(False) fake_appcontroller.should_receive('set_admin_role').and_return() fake_appcontroller.should_receive('create_user').with_args( '*****@*****.**', str, 'xmpp_user', 'the secret') \ .and_return('true') fake_appcontroller.should_receive('create_user').with_args( 'a@' + public_ip, str, 'xmpp_user', 'the secret') \ .and_return('true') flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://{0}:17443'.format( public_ip)).and_return(fake_appcontroller)
def test_fails_if_app_isnt_running(self): # If the user wants to relocate their app to port X, but their app isn't # even running, this should fail. # Assume that the AppController is running but our app isn't. flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_json_location( self.keyname)).and_return(True) fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('get_app_info_map').with_args( 'the secret').and_return(json.dumps({})) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) argv = [ '--keyname', self.keyname, '--appname', self.appid, '--http_port', '80', '--https_port', '443' ] options = ParseArgs(argv, self.function).args self.assertRaises(AppScaleException, AppScaleTools.relocate_app, options)
def test_remove_app_but_app_isnt_running(self): # mock out reading from stdin, and assume the user says 'yes' builtins = flexmock(sys.modules['__builtin__']) builtins.should_receive('raw_input').and_return('yes') # mock out reading the secret key builtins.should_call('open') # set the fall-through app_stats_data = { 'apps': { 'pippo': { 'http': 8080, 'language': 'python27', 'total_reqs': 'no_change', 'appservers': 1, 'https': 4380, 'reqs_enqueued': None } } } 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') builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('Database is at public1') fake_appcontroller.should_receive('get_all_stats').with_args( 'the secret').and_return(json.dumps(app_stats_data)) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) # mock out reading the locations.json file, and slip in our own json flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_json_location( self.keyname)).and_return(True) fake_nodes_json = flexmock(name="fake_nodes_json") fake_nodes_json.should_receive('read').and_return( json.dumps({ "node_info": [{ "public_ip": "public1", "private_ip": "private1", "jobs": ["shadow", "login"] }] })) fake_nodes_json.should_receive('write').and_return() builtins.should_receive('open').with_args( LocalState.get_locations_json_location(self.keyname), 'r') \ .and_return(fake_nodes_json) argv = ["--appname", "blargapp", "--keyname", self.keyname] options = ParseArgs(argv, self.function).args self.assertRaises(AppScaleException, AppScaleTools.remove_app, options)
def test_all_ok(self): # If the user wants to relocate their app to port X, and nothing else # runs on that port, this should succeed. # Assume that the AppController is running, so is our app, and that other # apps are not running on port 80. fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('get_app_info_map').with_args( 'the secret').and_return(json.dumps({ self.appid : { 'nginx' : 8080 }, 'a-different-app' : { 'nginx' : 81 } })) fake_appcontroller.should_receive('relocate_app').with_args(self.appid, 80, 443, 'the secret').and_return("OK") flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) argv = [ '--keyname', self.keyname, '--appname', self.appid, '--http_port', '80', '--https_port', '443' ] options = ParseArgs(argv, self.function).args AppScaleTools.relocate_app(options)
def test_appscale_in_two_node_virt_deployment(self): # pretend that the place we're going to put logs into doesn't exist flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args('/tmp/foobaz').and_return( False) # and mock out the mkdir operation flexmock(os) os.should_receive('mkdir').with_args('/tmp/foobaz').and_return() # next, mock out finding the login ip address os.path.should_receive('exists').with_args( LocalState.get_locations_json_location( self.keyname)).and_return(True) fake_nodes_json = flexmock(name="fake_nodes_json") fake_nodes_json.should_receive('read').and_return( json.dumps([{ "public_ip": "public1", "private_ip": "private1", "jobs": ["shadow", "login"] }])) builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') builtins.should_receive('open').with_args( LocalState.get_locations_json_location(self.keyname), 'r') \ .and_return(fake_nodes_json) # 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') builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # and slip in a fake appcontroller to report on the two IP addrs fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('get_all_public_ips').with_args( 'the secret').and_return(json.dumps(['public1', 'public2'])) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) # fake the creation of the log directories locally os.should_receive('mkdir').with_args( '/tmp/foobaz/public1').and_return() os.should_receive('mkdir').with_args( '/tmp/foobaz/public2').and_return() # finally, fake the copying of the log files flexmock(subprocess) subprocess.should_receive('Popen').with_args(re.compile('/var/log/appscale'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) argv = ["--keyname", self.keyname, "--location", "/tmp/foobaz"] options = ParseArgs(argv, self.function).args AppScaleTools.gather_logs(options)
def test_describe_instances_with_two_nodes(self): # mock out writing the secret key to ~/.appscale, as well as reading it # later builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through 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() builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('get_all_public_ips').with_args('the secret') \ .and_return(json.dumps(['public1', 'public2'])) fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('nothing interesting here') \ .and_return('Database is at not-up-yet') \ .and_return('Database is at 1.2.3.4') flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) SOAPpy.should_receive('SOAPProxy').with_args('https://public2:17443') \ .and_return(fake_appcontroller) # mock out reading the locations.json file, and slip in our own json flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_json_location( self.keyname)).and_return(True) fake_nodes_json = flexmock(name="fake_nodes_json") fake_nodes_json.should_receive('read').and_return( json.dumps({ "node_info": [ { "public_ip": "public1", "private_ip": "private1", "jobs": ["shadow", "login"] }, { "public_ip": "public2", "private_ip": "private2", "jobs": ["appengine"] }, ] })) fake_nodes_json.should_receive('write').and_return() builtins.should_receive('open').with_args( LocalState.get_locations_json_location(self.keyname), 'r') \ .and_return(fake_nodes_json) # assume that there are two machines running in our deployment argv = ["--keyname", self.keyname] options = ParseArgs(argv, self.function).args AppScaleTools.describe_instances(options)
def test_remove_app_and_app_is_running(self): # mock out reading from stdin, and assume the user says 'YES' builtins = flexmock(sys.modules['__builtin__']) builtins.should_receive('raw_input').and_return('YES') # mock out reading the secret key builtins.should_call('open') # set the fall-through 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') builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) app_stats_data = {'apps': {'blargapp': {'http': 8080, 'language': 'python27', 'total_reqs': 'no_change', 'appservers': 1, 'https': 4380, 'reqs_enqueued': None}}} # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('Database is at public1') fake_appcontroller.should_receive('stop_app').with_args('blargapp', 'the secret').and_return('OK') fake_appcontroller.should_receive('is_app_running').with_args('blargapp', 'the secret').and_return(True).and_return(True).and_return(False) fake_appcontroller.should_receive('does_app_exist').with_args('blargapp', 'the secret').and_return(True) fake_appcontroller.should_receive('get_all_stats').with_args( 'the secret').and_return(json.dumps(app_stats_data)) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) # mock out reading the locations.json file, and slip in our own json flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_json_location(self.keyname)).and_return(True) fake_nodes_json = flexmock(name="fake_nodes_json") fake_nodes_json.should_receive('read').and_return( json.dumps({"node_info": [{ "public_ip": "public1", "private_ip": "private1", "jobs": ["shadow", "login"] }]})) fake_nodes_json.should_receive('write').and_return() builtins.should_receive('open').with_args( LocalState.get_locations_json_location(self.keyname), 'r') \ .and_return(fake_nodes_json) flexmock(RemoteHelper).should_receive('is_port_open').and_return(False) argv = [ "--appname", "blargapp", "--keyname", self.keyname ] options = ParseArgs(argv, self.function).args AppScaleTools.remove_app(options)
def test_appscale_in_two_node_virt_deployment(self): # pretend that the place we're going to put logs into doesn't exist flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args('/tmp/foobaz').and_return(False) # and mock out the mkdir operation flexmock(os) os.should_receive('mkdir').with_args('/tmp/foobaz').and_return() # next, mock out finding the login ip address os.path.should_receive('exists').with_args( LocalState.get_locations_json_location(self.keyname)).and_return(True) fake_nodes_json = flexmock(name="fake_nodes_json") fake_nodes_json.should_receive('read').and_return(json.dumps([{ "public_ip" : "public1", "private_ip" : "private1", "jobs" : ["shadow", "login"] }])) builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') builtins.should_receive('open').with_args( LocalState.get_locations_json_location(self.keyname), 'r') \ .and_return(fake_nodes_json) # 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') builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # and slip in a fake appcontroller to report on the two IP addrs fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('get_all_public_ips').with_args( 'the secret').and_return(json.dumps(['public1', 'public2'])) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) # fake the creation of the log directories locally os.should_receive('mkdir').with_args('/tmp/foobaz/public1').and_return() os.should_receive('mkdir').with_args('/tmp/foobaz/public2').and_return() # finally, fake the copying of the log files flexmock(subprocess) subprocess.should_receive('Popen').with_args(re.compile('/var/log/appscale'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) argv = [ "--keyname", self.keyname, "--location", "/tmp/foobaz" ] options = ParseArgs(argv, self.function).args AppScaleTools.gather_logs(options)
def test_remove_app_and_app_is_running(self): # mock out reading from stdin, and assume the user says 'YES' builtins = flexmock(sys.modules['__builtin__']) builtins.should_receive('raw_input').and_return('YES') # mock out reading the secret key builtins.should_call('open') # set the fall-through 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') builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('Database is at public1') fake_appcontroller.should_receive('stop_app').with_args('blargapp', 'the secret').and_return('OK') fake_appcontroller.should_receive('is_app_running').with_args('blargapp', 'the secret').and_return(True).and_return(True).and_return(False) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) # mock out reading the locations.json file, and slip in our own json flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_json_location(self.keyname)).and_return(True) fake_nodes_json = flexmock(name="fake_nodes_json") fake_nodes_json.should_receive('read').and_return(json.dumps([{ "public_ip" : "public1", "private_ip" : "private1", "jobs" : ["shadow", "login"] }])) fake_nodes_json.should_receive('write').and_return() builtins.should_receive('open').with_args( LocalState.get_locations_json_location(self.keyname), 'r') \ .and_return(fake_nodes_json) # mock out calls to the UserAppServer and presume that the app does exist fake_userappserver = flexmock(name='fake_uaserver') fake_userappserver.should_receive('get_app_data').with_args( 'blargapp', 'the secret').and_return(json.dumps({ 'hosts' : { '192.168.1.1' : { 'http' : '80', 'https' : '443' }}})) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:4343') \ .and_return(fake_userappserver) argv = [ "--appname", "blargapp", "--keyname", self.keyname ] options = ParseArgs(argv, self.function).args AppScaleTools.remove_app(options)
def test_describe_instances_with_two_nodes(self): # mock out writing the secret key to ~/.appscale, as well as reading it # later builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through 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() builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('get_all_public_ips').with_args('the secret') \ .and_return(json.dumps(['public1', 'public2'])) fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('nothing interesting here') \ .and_return('Database is at not-up-yet') \ .and_return('Database is at 1.2.3.4') flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) SOAPpy.should_receive('SOAPProxy').with_args('https://public2:17443') \ .and_return(fake_appcontroller) # mock out reading the locations.json file, and slip in our own json flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_json_location(self.keyname)).and_return(True) fake_nodes_json = flexmock(name="fake_nodes_json") fake_nodes_json.should_receive('read').and_return(json.dumps( {"node_info": [{ "public_ip": "public1", "private_ip": "private1", "jobs": ["shadow", "login"] }, { "public_ip": "public2", "private_ip": "private2", "jobs": ["appengine"] }, ]})) fake_nodes_json.should_receive('write').and_return() builtins.should_receive('open').with_args( LocalState.get_locations_json_location(self.keyname), 'r') \ .and_return(fake_nodes_json) # assume that there are two machines running in our deployment argv = [ "--keyname", self.keyname ] options = ParseArgs(argv, self.function).args AppScaleTools.describe_instances(options)
def test_reset_password_for_user_that_doesnt_exist(self): # put in a mock for reading the secret file builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through 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') builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # mock out reading the username and new password from the user builtins.should_receive('raw_input').and_return('*****@*****.**') flexmock(getpass) getpass.should_receive('getpass').and_return('the password') # mock out finding the login node's IP address from the json file flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_json_location( self.keyname)).and_return(True) fake_nodes_json = flexmock(name="fake_secret") fake_nodes_json.should_receive('read').and_return( json.dumps([{ 'public_ip': 'public1', 'private_ip': 'private1', 'jobs': ['login', 'db_master'] }])) builtins.should_receive('open').with_args( LocalState.get_locations_json_location(self.keyname), 'r') \ .and_return(fake_nodes_json) # mock out grabbing the userappserver ip from an appcontroller fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('nothing interesting here') \ .and_return('Database is at not-up-yet') \ .and_return('Database is at public1') flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@foo.goo', str, 'the secret').and_return('Error: user does not exist') SOAPpy.should_receive('SOAPProxy').with_args('https://public1:4343') \ .and_return(fake_userappserver) argv = ["--keyname", self.keyname] options = ParseArgs(argv, self.function).args self.assertRaises(SystemExit, AppScaleTools.reset_password, options)
def test_update_local_metadata(self): # mock out getting all the ips in the deployment from the head node fake_soap = flexmock(name='fake_soap') fake_soap.should_receive('get_all_public_ips').with_args('the secret') \ .and_return(json.dumps(['public1'])) role_info = [{ 'public_ip' : 'public1', 'private_ip' : 'private1', 'jobs' : ['shadow', 'db_master'] }] fake_soap.should_receive('get_role_info').with_args('the secret') \ .and_return(json.dumps(role_info)) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_soap) # mock out reading the secret key fake_secret = flexmock(name='fake_secret') fake_secret.should_receive('read').and_return('the secret') builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') builtins.should_receive('open').with_args( LocalState.get_secret_key_location('booscale'), 'r') \ .and_return(fake_secret) # mock out writing the yaml file fake_locations_yaml = flexmock(name='fake_locations_yaml') fake_locations_yaml.should_receive('write').with_args(yaml.dump({ 'load_balancer': 'public1', 'instance_id': 'i-ABCDEFG', 'secret': 'the secret', 'infrastructure': 'ec2', 'group': 'boogroup', 'ips': 'public1', 'table': 'cassandra', 'db_master': 'node-0', 'zone' : 'my-zone-1b' })).and_return() builtins.should_receive('open').with_args( LocalState.get_locations_yaml_location('booscale'), 'w') \ .and_return(fake_locations_yaml) # and mock out writing the json file fake_locations_json = flexmock(name='fake_locations_json') fake_locations_json.should_receive('write').with_args(json.dumps( role_info)).and_return() builtins.should_receive('open').with_args( LocalState.get_locations_json_location('booscale'), 'w') \ .and_return(fake_locations_json) options = flexmock(name='options', table='cassandra', infrastructure='ec2', keyname='booscale', group='boogroup', zone='my-zone-1b') node_layout = NodeLayout(options={ 'min' : 1, 'max' : 1, 'infrastructure' : 'ec2', 'table' : 'cassandra' }) host = 'public1' instance_id = 'i-ABCDEFG' LocalState.update_local_metadata(options, node_layout, host, instance_id)
def test_update_local_metadata(self): # mock out getting all the ips in the deployment from the head node fake_soap = flexmock(name='fake_soap') fake_soap.should_receive('get_all_public_ips').with_args('the secret') \ .and_return(json.dumps(['public1'])) role_info = [{ 'public_ip' : 'public1', 'private_ip' : 'private1', 'jobs' : ['shadow', 'db_master'] }] fake_soap.should_receive('get_role_info').with_args('the secret') \ .and_return(json.dumps(role_info)) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_soap) # mock out reading the secret key fake_secret = flexmock(name='fake_secret') fake_secret.should_receive('read').and_return('the secret') builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') builtins.should_receive('open').with_args( LocalState.get_secret_key_location('booscale'), 'r') \ .and_return(fake_secret) # mock out writing the yaml file fake_locations_yaml = flexmock(name='fake_locations_yaml') fake_locations_yaml.should_receive('write').with_args(yaml.dump({ 'load_balancer': 'public1', 'instance_id': 'i-ABCDEFG', 'secret': 'the secret', 'infrastructure': 'ec2', 'group': 'boogroup', 'ips': 'public1', 'table': 'cassandra', 'db_master': 'node-0', 'zone' : 'my-zone-1b' })).and_return() builtins.should_receive('open').with_args( LocalState.get_locations_yaml_location('booscale'), 'w') \ .and_return(fake_locations_yaml) # and mock out writing the json file fake_locations_json = flexmock(name='fake_locations_json') fake_locations_json.should_receive('write').with_args(json.dumps( role_info)).and_return() builtins.should_receive('open').with_args( LocalState.get_locations_json_location('booscale'), 'w') \ .and_return(fake_locations_json) options = flexmock(name='options', table='cassandra', infrastructure='ec2', keyname='booscale', group='boogroup', zone='my-zone-1b') node_layout = NodeLayout(options={ 'min' : 1, 'max' : 1, 'infrastructure' : 'ec2', 'table' : 'cassandra' }) host = 'public1' instance_id = 'i-ABCDEFG' LocalState.update_local_metadata(options, node_layout, host, instance_id)
def test_remove_app_and_app_is_running(self): # mock out reading from stdin, and assume the user says 'YES' builtins = flexmock(sys.modules['__builtin__']) builtins.should_receive('raw_input').and_return('YES') # mock out reading the secret key builtins.should_call('open') # set the fall-through 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') builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('Database is at public1') fake_appcontroller.should_receive('stop_app').with_args( 'blargapp', 'the secret').and_return('OK') fake_appcontroller.should_receive('is_app_running').with_args( 'blargapp', 'the secret').and_return(True).and_return(True).and_return(False) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) # mock out reading the locations.json file, and slip in our own json flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_json_location( self.keyname)).and_return(True) fake_nodes_json = flexmock(name="fake_nodes_json") fake_nodes_json.should_receive('read').and_return( json.dumps([{ "public_ip": "public1", "private_ip": "private1", "jobs": ["shadow", "login"] }])) fake_nodes_json.should_receive('write').and_return() builtins.should_receive('open').with_args( LocalState.get_locations_json_location(self.keyname), 'r') \ .and_return(fake_nodes_json) # mock out calls to the UserAppServer and presume that the app does exist fake_userappserver = flexmock(name='fake_uaserver') fake_userappserver.should_receive('get_app_data').with_args( 'blargapp', 'the secret').and_return('\nnum_ports:2\n') SOAPpy.should_receive('SOAPProxy').with_args('https://public1:4343') \ .and_return(fake_userappserver) argv = ["--appname", "blargapp", "--keyname", self.keyname] options = ParseArgs(argv, self.function).args AppScaleTools.remove_app(options)
def test_reset_password_for_user_that_exists(self): # put in a mock for reading the secret file builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through 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') builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # mock out reading the username and new password from the user builtins.should_receive('raw_input').and_return('*****@*****.**') flexmock(getpass) getpass.should_receive('getpass').and_return('the password') # mock out finding the login node's IP address from the json file flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_json_location(self.keyname)).and_return(True) fake_nodes_json = flexmock(name="fake_secret") fake_nodes_json.should_receive('read').and_return(json.dumps([{ 'public_ip' : 'public1', 'private_ip' : 'private1', 'jobs' : ['login', 'db_master'] }])) builtins.should_receive('open').with_args( LocalState.get_locations_json_location(self.keyname), 'r') \ .and_return(fake_nodes_json) # mock out grabbing the userappserver ip from an appcontroller fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('nothing interesting here') \ .and_return('Database is at not-up-yet') \ .and_return('Database is at public1') flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@foo.goo', str, 'the secret').and_return('true') SOAPpy.should_receive('SOAPProxy').with_args('https://public1:4343') \ .and_return(fake_userappserver) argv = [ "--keyname", self.keyname ] options = ParseArgs(argv, self.function).args AppScaleTools.reset_password(options)
def test_sensor_app_not_deployed_when_deployment_not_registered(self): # Test sensor app is not deployed when deployment is not registered. flexmock(helper).should_receive('get_deployment_id').and_return(None) flexmock(hermes).should_receive('create_appscale_user').and_return().\ times(0) flexmock(hermes).should_receive('create_xmpp_user').and_return().\ times(0) flexmock(AppControllerClient).should_receive('upload_app').and_return().\ times(0) deploy_sensor_app() # Test sensor app is not deployed when it is already running. flexmock(helper).should_receive('get_deployment_id').and_return( self.DEPLOYMENT_ID) flexmock(appscale_info).should_receive('get_secret').and_return( "fake_secret") flexmock(appscale_info).should_receive('get_db_master_ip').and_return() fake_uaserver = flexmock(name='fake_uaserver') # Assume appscalesensor app already running. fake_uaserver.should_receive('is_app_enabled').with_args( 'appscalesensor', 'fake_secret').and_return("true") flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').and_return(fake_uaserver) flexmock(AppControllerClient).should_receive('upload_app').and_return(). \ times(0) deploy_sensor_app() # Test sensor app is not deployed when the app is not currently running but # there was an error in creating a new user. fake_uaserver.should_receive('is_app_enabled').with_args( 'appscalesensor', 'fake_secret').and_return("false") # Assume error while creating a new user. fake_uaserver.should_receive('does_user_exist').and_return("false") fake_uaserver.should_receive('commit_new_user').and_return("false") flexmock(hermes).should_receive('create_appscale_user').and_return(). \ times(1) flexmock(AppControllerClient).should_receive('upload_app').and_return(). \ times(0) deploy_sensor_app() # Test sensor app is deployed after successfully creating a new user or # with an existing user. fake_uaserver.should_receive('commit_new_user').and_return("true") flexmock(tarfile).should_receive('open').and_return(tarfile.TarFile) flexmock(tarfile.TarFile).should_receive('add').and_return() flexmock(tarfile.TarFile).should_receive('close').and_return() flexmock(appscale_info).should_receive( 'get_appcontroller_client').and_return(AppControllerClient) flexmock(hermes).should_receive('create_appscale_user').and_return( True) flexmock(hermes).should_receive('create_xmpp_user').and_return(True) flexmock(AppControllerClient).should_receive('upload_app').and_return(). \ times(1) deploy_sensor_app()
def test_sensor_app_not_deployed_when_deployment_not_registered(self): # Test sensor app is not deployed when deployment is not registered. flexmock(helper).should_receive('get_deployment_id').and_return(None) flexmock(hermes).should_receive('create_appscale_user').and_return().\ times(0) flexmock(hermes).should_receive('create_xmpp_user').and_return().\ times(0) flexmock(AppControllerClient).should_receive('upload_app').and_return().\ times(0) deploy_sensor_app() # Test sensor app is not deployed when it is already running. flexmock(helper).should_receive('get_deployment_id').and_return( self.DEPLOYMENT_ID) flexmock(appscale_info).should_receive('get_secret').and_return( "fake_secret") flexmock(appscale_info).should_receive('get_db_master_ip').and_return() fake_uaserver = flexmock(name='fake_uaserver') # Assume appscalesensor app already running. fake_uaserver.should_receive('is_app_enabled').with_args( 'appscalesensor', 'fake_secret').and_return("true") flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').and_return(fake_uaserver) flexmock(AppControllerClient).should_receive('upload_app').and_return(). \ times(0) deploy_sensor_app() # Test sensor app is not deployed when the app is not currently running but # there was an error in creating a new user. fake_uaserver.should_receive('is_app_enabled').with_args( 'appscalesensor', 'fake_secret').and_return("false") # Assume error while creating a new user. fake_uaserver.should_receive('does_user_exist').and_return("false") fake_uaserver.should_receive('commit_new_user').and_return("false") flexmock(hermes).should_receive('create_appscale_user').and_return(). \ times(1) flexmock(AppControllerClient).should_receive('upload_app').and_return(). \ times(0) deploy_sensor_app() # Test sensor app is deployed after successfully creating a new user or # with an existing user. fake_uaserver.should_receive('commit_new_user').and_return("true") flexmock(tarfile).should_receive('open').and_return(tarfile.TarFile) flexmock(tarfile.TarFile).should_receive('add').and_return() flexmock(tarfile.TarFile).should_receive('close').and_return() flexmock(appscale_info).should_receive('get_appcontroller_client').and_return( AppControllerClient) flexmock(hermes).should_receive('create_appscale_user').and_return(True) flexmock(hermes).should_receive('create_xmpp_user').and_return(True) flexmock(AppControllerClient).should_receive('upload_app').and_return(). \ times(1) deploy_sensor_app()
def test_get_property(self): # put in a mock for reading the secret file builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through 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') builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # mock out finding the shadow node's IP address from the json file flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_json_location( self.keyname)).and_return(True) fake_nodes_json = flexmock(name="fake_secret") fake_nodes_json.should_receive('read').and_return( json.dumps({ "node_info": [{ 'public_ip': 'public1', 'private_ip': 'private1', 'jobs': ['login', 'shadow'] }] })) builtins.should_receive('open').with_args( LocalState.get_locations_json_location(self.keyname), 'r') \ .and_return(fake_nodes_json) # mock out grabbing the userappserver ip from an appcontroller property_name = "name" property_value = "value" fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('set_property').with_args( property_name, property_value, 'the secret').and_return('OK') flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) argv = [ "--keyname", self.keyname, "--property_name", property_name, "--property_value", property_value ] options = ParseArgs(argv, self.function).args result = AppScaleTools.set_property(options) self.assertEqual(None, result)
def test_update_local_metadata(self): # mock out getting all the ips in the deployment from the head node fake_soap = flexmock(name='fake_soap') fake_soap.should_receive('get_all_public_ips').with_args('the secret') \ .and_return(json.dumps(['public1'])) role_info = [{ 'public_ip': 'public1', 'private_ip': 'private1', 'jobs': ['shadow', 'db_master'] }] fake_soap.should_receive('get_role_info').with_args('the secret') \ .and_return(json.dumps(role_info)) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_soap) # mock out reading the secret key fake_secret = flexmock(name='fake_secret') fake_secret.should_receive('read').and_return('the secret') builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') builtins.should_receive('open').with_args( LocalState.get_secret_key_location('booscale'), 'r') \ .and_return(fake_secret) # Mock out writing the json file. json_location = LocalState.get_locations_json_location('booscale') builtins.should_receive('open').with_args(json_location, 'w')\ .and_return(flexmock(write=lambda *args: None)) options = flexmock(name='options', table='cassandra', infrastructure='ec2', keyname='booscale', group='boogroup', zone='my-zone-1b', EC2_ACCESS_KEY='baz', EC2_SECRET_KEY='baz', EC2_URL='') node_layout = NodeLayout( options={ 'min_machines': 1, 'max_machines': 1, 'infrastructure': 'ec2', 'table': 'cassandra', 'instance_type': 'm1.large' }) LocalState.update_local_metadata(options, 'public1', 'public1')
def test_remove_app_but_app_isnt_running(self): # mock out reading from stdin, and assume the user says 'yes' builtins = flexmock(sys.modules['__builtin__']) builtins.should_receive('raw_input').and_return('yes') # mock out reading the secret key builtins.should_call('open') # set the fall-through 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') builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('Database is at public1') fake_appcontroller.should_receive('is_app_running').with_args('blargapp', 'the secret').and_return(False) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) # mock out reading the locations.json file, and slip in our own json flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_json_location(self.keyname)).and_return(True) fake_nodes_json = flexmock(name="fake_nodes_json") fake_nodes_json.should_receive('read').and_return( json.dumps({"node_info": [{ "public_ip": "public1", "private_ip": "private1", "jobs": ["shadow", "login"] }]})) fake_nodes_json.should_receive('write').and_return() builtins.should_receive('open').with_args( LocalState.get_locations_json_location(self.keyname), 'r') \ .and_return(fake_nodes_json) argv = [ "--appname", "blargapp", "--keyname", self.keyname ] options = ParseArgs(argv, self.function).args self.assertRaises(AppScaleException, AppScaleTools.remove_app, options)
def test_get_property(self): # put in a mock for reading the secret file builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through 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') builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # mock out finding the shadow node's IP address from the json file flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_json_location(self.keyname)).and_return(True) fake_nodes_json = flexmock(name="fake_secret") fake_nodes_json.should_receive('read').and_return( json.dumps({"node_info": [{ 'public_ip': 'public1', 'private_ip': 'private1', 'roles': ['shadow'] }]})) builtins.should_receive('open').with_args( LocalState.get_locations_json_location(self.keyname), 'r') \ .and_return(fake_nodes_json) # mock out grabbing the userappserver ip from an appcontroller property_name = "name" property_value = "value" fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('set_property').with_args(property_name, property_value, 'the secret').and_return('OK') flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) argv = [ "--keyname", self.keyname, "--property_name", property_name, "--property_value", property_value ] options = ParseArgs(argv, self.function).args result = AppScaleTools.set_property(options) self.assertEqual(None, result)
def test_create_user_accounts(self): # mock out reading 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('read').and_return('the secret') builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # mock out reading the locations.json file, and slip in our own json flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_json_location('bookey')).and_return(True) fake_nodes_json = flexmock(name="fake_nodes_json") fake_nodes_json.should_receive('read').and_return( json.dumps({ "node_info": [{ "public_ip": "public1", "private_ip": "private1", "roles": ["shadow"] }] })) builtins.should_receive('open').with_args( LocalState.get_locations_json_location('bookey'), 'r') \ .and_return(fake_nodes_json) # Mock out SOAP interactions with the AppController. fake_appcontroller = flexmock(name="fake_appcontroller") fake_appcontroller.should_receive('does_user_exist').with_args( '*****@*****.**', 'the secret').and_return('false') fake_appcontroller.should_receive('create_user').with_args( '*****@*****.**', str, 'xmpp_user', 'the secret').and_return('true') fake_appcontroller.should_receive('does_user_exist').with_args( 'boo@public1', 'the secret').and_return('false') fake_appcontroller.should_receive('create_user').with_args( 'boo@public1', str, 'xmpp_user', 'the secret').and_return('true') fake_appcontroller.should_receive('get_property').\ with_args('login', 'the secret').and_return('{"login":"******"}') flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@foo.goo', 'password', 'public1', 'bookey')
def test_create_user_accounts(self): # mock out reading 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('read').and_return('the secret') builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # mock out reading the locations.json file, and slip in our own json flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_json_location('bookey')).and_return(True) fake_nodes_json = flexmock(name="fake_nodes_json") fake_nodes_json.should_receive('read').and_return( json.dumps({"node_info": [{ "public_ip": "public1", "private_ip": "private1", "roles": ["shadow"] }]})) builtins.should_receive('open').with_args( LocalState.get_locations_json_location('bookey'), 'r') \ .and_return(fake_nodes_json) # Mock out SOAP interactions with the AppController. fake_appcontroller = flexmock(name="fake_appcontroller") fake_appcontroller.should_receive('does_user_exist').with_args('*****@*****.**', 'the secret').and_return('false') fake_appcontroller.should_receive('create_user').with_args('*****@*****.**', str, 'xmpp_user', 'the secret').and_return('true') fake_appcontroller.should_receive('does_user_exist').with_args('boo@public1', 'the secret').and_return('false') fake_appcontroller.should_receive('create_user').with_args('boo@public1', str, 'xmpp_user', 'the secret').and_return('true') fake_appcontroller.should_receive('get_property').\ with_args('login', 'the secret').and_return('{"login":"******"}') flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@foo.goo', 'password', 'public1', 'bookey')
def test_create_user_accounts(self): # mock out reading 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('read').and_return('the secret') builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # mock out reading the locations.json file, and slip in our own json flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_json_location('bookey')).and_return(True) fake_nodes_json = flexmock(name="fake_nodes_json") fake_nodes_json.should_receive('read').and_return( json.dumps([{ "public_ip": "public1", "private_ip": "private1", "jobs": ["shadow", "login"] }])) builtins.should_receive('open').with_args( LocalState.get_locations_json_location('bookey'), 'r') \ .and_return(fake_nodes_json) # mock out SOAP interactions with the UserAppServer fake_soap = flexmock(name='fake_soap') fake_soap.should_receive('does_user_exist').with_args( '*****@*****.**', 'the secret').and_return('false') fake_soap.should_receive('commit_new_user').with_args( '*****@*****.**', str, 'xmpp_user', 'the secret').and_return('true') fake_soap.should_receive('does_user_exist').with_args( 'boo@public1', 'the secret').and_return('false') fake_soap.should_receive('commit_new_user').with_args( 'boo@public1', str, 'xmpp_user', 'the secret').and_return('true') flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@foo.goo', 'password', 'public1', 'bookey', False)
def setup_appcontroller_mocks(self, public_ip, private_ip): # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name="fake_appcontroller") fake_appcontroller.should_receive("set_parameters").with_args(str, list, ["none"], "the secret").and_return( "OK" ) fake_appcontroller.should_receive("get_all_public_ips").with_args("the secret").and_return( json.dumps([public_ip]) ) role_info = [{"public_ip": public_ip, "private_ip": private_ip, "jobs": ["shadow", "login"]}] fake_appcontroller.should_receive("get_role_info").with_args("the secret").and_return(json.dumps(role_info)) fake_appcontroller.should_receive("status").with_args("the secret").and_return( "nothing interesting here" ).and_return("Database is at not-up-yet").and_return("Database is at {0}".format(public_ip)) fake_appcontroller.should_receive("is_done_initializing").and_return(False).and_return(True) flexmock(SOAPpy) SOAPpy.should_receive("SOAPProxy").with_args("https://{0}:17443".format(public_ip)).and_return( fake_appcontroller )
def test_create_user_accounts(self): # mock out reading 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('read').and_return('the secret') builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # mock out reading the locations.json file, and slip in our own json flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_json_location('bookey')).and_return(True) fake_nodes_json = flexmock(name="fake_nodes_json") fake_nodes_json.should_receive('read').and_return(json.dumps([{ "public_ip" : "public1", "private_ip" : "private1", "jobs" : ["shadow", "login"] }])) builtins.should_receive('open').with_args( LocalState.get_locations_json_location('bookey'), 'r') \ .and_return(fake_nodes_json) # mock out SOAP interactions with the UserAppServer fake_soap = flexmock(name='fake_soap') fake_soap.should_receive('does_user_exist').with_args('*****@*****.**', 'the secret').and_return('false') fake_soap.should_receive('commit_new_user').with_args('*****@*****.**', str, 'xmpp_user', 'the secret').and_return('true') fake_soap.should_receive('does_user_exist').with_args('boo@public1', 'the secret').and_return('false') fake_soap.should_receive('commit_new_user').with_args('boo@public1', str, 'xmpp_user', 'the secret').and_return('true') flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@foo.goo', 'password', 'public1', 'bookey', False)
def test_wait_for_machines_to_finish_loading(self): # mock out reading 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('read').and_return('the secret') builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # mock out getting all the ips in the deployment from the head node fake_soap = flexmock(name='fake_soap') fake_soap.should_receive('get_all_public_ips').with_args('the secret') \ .and_return(json.dumps(['public1', 'public2'])) role_info = [ { 'public_ip' : 'public1', 'private_ip' : 'private1', 'jobs' : ['shadow', 'db_master'] }, { 'public_ip' : 'public2', 'private_ip' : 'private2', 'jobs' : ['appengine'] } ] fake_soap.should_receive('get_role_info').with_args('the secret') \ .and_return(json.dumps(role_info)) # also, let's say that our machines aren't running the first time we ask, # but that they are the second time fake_soap.should_receive('is_done_initializing').with_args('the secret') \ .and_return(False).and_return(True) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_soap) SOAPpy.should_receive('SOAPProxy').with_args('https://public2:17443') \ .and_return(fake_soap) RemoteHelper.wait_for_machines_to_finish_loading('public1', 'bookey')
def setup_uaserver_mocks(self, public_uaserver_address): # mock out calls to the UserAppServer and presume that calls to create new # users succeed fake_userappserver = flexmock(name='fake_userappserver') fake_userappserver.should_receive('does_user_exist').with_args( '*****@*****.**', 'the secret').and_return('false') fake_userappserver.should_receive('does_user_exist').with_args( 'a@' + public_uaserver_address, 'the secret').and_return('false') fake_userappserver.should_receive('commit_new_user').with_args( '*****@*****.**', str, 'xmpp_user', 'the secret') \ .and_return('true') fake_userappserver.should_receive('commit_new_user').with_args( 'a@' + public_uaserver_address, str, 'xmpp_user', 'the secret') \ .and_return('true') fake_userappserver.should_receive('set_cloud_admin_status').with_args( '*****@*****.**', 'true', 'the secret').and_return() fake_userappserver.should_receive('set_capabilities').with_args( '*****@*****.**', UserAppClient.ADMIN_CAPABILITIES, 'the secret').and_return() SOAPpy.should_receive('SOAPProxy').with_args('https://{0}:4343'.format( public_uaserver_address)).and_return(fake_userappserver)
def test_update_local_metadata(self): # mock out getting all the ips in the deployment from the head node fake_soap = flexmock(name='fake_soap') fake_soap.should_receive('get_all_public_ips').with_args('the secret') \ .and_return(json.dumps(['public1'])) role_info = [{ 'public_ip' : 'public1', 'private_ip' : 'private1', 'roles' : ['shadow', 'db_master'] }] fake_soap.should_receive('get_role_info').with_args('the secret') \ .and_return(json.dumps(role_info)) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_soap) # mock out reading the secret key fake_secret = flexmock(name='fake_secret') fake_secret.should_receive('read').and_return('the secret') builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') builtins.should_receive('open').with_args( LocalState.get_secret_key_location('booscale'), 'r') \ .and_return(fake_secret) # Mock out writing the json file. json_location = LocalState.get_locations_json_location('booscale') builtins.should_receive('open').with_args(json_location, 'w')\ .and_return(flexmock(write=lambda *args: None)) options = flexmock(name='options', table='cassandra', infrastructure='ec2', keyname='booscale', group='boogroup', zone='my-zone-1b', EC2_ACCESS_KEY='baz', EC2_SECRET_KEY='baz', EC2_URL='') node_layout = NodeLayout(options={ 'min_machines' : 1, 'max_machines' : 1, 'infrastructure' : 'ec2', 'table' : 'cassandra', 'instance_type': 'm1.large' }) LocalState.update_local_metadata(options, 'public1', 'public1')
def test_get_property(self): # put in a mock for reading the secret file builtins = flexmock(sys.modules["__builtin__"]) builtins.should_call("open") # set the fall-through 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") builtins.should_receive("open").with_args(secret_key_location, "r").and_return(fake_secret) # mock out finding the shadow node's IP address from the json file flexmock(os.path) os.path.should_call("exists") # set the fall-through os.path.should_receive("exists").with_args(LocalState.get_locations_json_location(self.keyname)).and_return( True ) fake_nodes_json = flexmock(name="fake_secret") fake_nodes_json.should_receive("read").and_return( json.dumps({"node_info": [{"public_ip": "public1", "private_ip": "private1", "jobs": ["login", "shadow"]}]}) ) builtins.should_receive("open").with_args(LocalState.get_locations_json_location(self.keyname), "r").and_return( fake_nodes_json ) # mock out grabbing the userappserver ip from an appcontroller property_name = "name" property_value = "value" fake_appcontroller = flexmock(name="fake_appcontroller") fake_appcontroller.should_receive("set_property").with_args( property_name, property_value, "the secret" ).and_return("OK") flexmock(SOAPpy) SOAPpy.should_receive("SOAPProxy").with_args("https://public1:17443").and_return(fake_appcontroller) argv = ["--keyname", self.keyname, "--property_name", property_name, "--property_value", property_value] options = ParseArgs(argv, self.function).args result = AppScaleTools.set_property(options) self.assertEqual(None, result)
def setup_uaserver_mocks(self, public_uaserver_address): # mock out calls to the UserAppServer and presume that calls to create new # users succeed fake_userappserver = flexmock(name='fake_userappserver') fake_userappserver.should_receive('does_user_exist').with_args( '*****@*****.**', 'the secret').and_return('false') fake_userappserver.should_receive('does_user_exist').with_args( 'a@' + public_uaserver_address, 'the secret').and_return('false') fake_userappserver.should_receive('commit_new_user').with_args( '*****@*****.**', str, 'xmpp_user', 'the secret') \ .and_return('true') fake_userappserver.should_receive('commit_new_user').with_args( 'a@' + public_uaserver_address, str, 'xmpp_user', 'the secret') \ .and_return('true') fake_userappserver.should_receive('set_cloud_admin_status').with_args( '*****@*****.**', 'true', 'the secret').and_return() fake_userappserver.should_receive('set_capabilities').with_args( '*****@*****.**', UserAppClient.ADMIN_CAPABILITIES, 'the secret').and_return() SOAPpy.should_receive('SOAPProxy').with_args('https://{0}:4343'.format( public_uaserver_address)).and_return(fake_userappserver)
def setup_uaserver_mocks(self, public_uaserver_address): # mock out calls to the UserAppServer and presume that calls to create new # users succeed fake_userappserver = flexmock(name="fake_userappserver") fake_userappserver.should_receive("does_user_exist").with_args("*****@*****.**", "the secret").and_return("false") fake_userappserver.should_receive("does_user_exist").with_args( "a@" + public_uaserver_address, "the secret" ).and_return("false") fake_userappserver.should_receive("commit_new_user").with_args( "*****@*****.**", str, "xmpp_user", "the secret" ).and_return("true") fake_userappserver.should_receive("commit_new_user").with_args( "a@" + public_uaserver_address, str, "xmpp_user", "the secret" ).and_return("true") fake_userappserver.should_receive("set_cloud_admin_status").with_args( "*****@*****.**", "true", "the secret" ).and_return() fake_userappserver.should_receive("set_capabilities").with_args( "*****@*****.**", UserAppClient.ADMIN_CAPABILITIES, "the secret" ).and_return() SOAPpy.should_receive("SOAPProxy").with_args("https://{0}:4343".format(public_uaserver_address)).and_return( fake_userappserver )
def test_create_user_accounts(self): # mock out reading 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("read").and_return("the secret") builtins.should_receive("open").with_args(secret_key_location, "r").and_return(fake_secret) # mock out reading the locations.json file, and slip in our own json flexmock(os.path) os.path.should_call("exists") # set the fall-through os.path.should_receive("exists").with_args(LocalState.get_locations_json_location("bookey")).and_return(True) fake_nodes_json = flexmock(name="fake_nodes_json") fake_nodes_json.should_receive("read").and_return( json.dumps([{"public_ip": "public1", "private_ip": "private1", "jobs": ["shadow", "login"]}]) ) builtins.should_receive("open").with_args(LocalState.get_locations_json_location("bookey"), "r").and_return( fake_nodes_json ) # mock out SOAP interactions with the UserAppServer fake_soap = flexmock(name="fake_soap") fake_soap.should_receive("does_user_exist").with_args("*****@*****.**", "the secret").and_return("false") fake_soap.should_receive("commit_new_user").with_args("*****@*****.**", str, "xmpp_user", "the secret").and_return( "true" ) fake_soap.should_receive("does_user_exist").with_args("boo@public1", "the secret").and_return("false") fake_soap.should_receive("commit_new_user").with_args("boo@public1", str, "xmpp_user", "the secret").and_return( "true" ) flexmock(SOAPpy) SOAPpy.should_receive("SOAPProxy").with_args("https://*****:*****@foo.goo", "password", "public1", "bookey", False)
def test_all_ok(self): # If the user wants to relocate their app to port X, and nothing else # runs on that port, this should succeed. # Assume that the AppController is running, so is our app, and that other # apps are not running on port 80. fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('get_app_info_map').with_args( 'the secret').and_return(json.dumps({ '{}_default_v1'.format(self.appid) : { 'nginx' : 8080 }, 'a-different-app_default_v1' : { 'nginx' : 81 } })) version_key = '{}_default_v1'.format(self.appid) fake_appcontroller.should_receive('relocate_version').with_args( version_key, 80, 443, 'the secret').and_return('OK') fake_appcontroller.should_receive('get_property').\ and_return('{"login":"******"}') flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) rh = flexmock(RemoteHelper) rh.should_receive('sleep_until_port_is_open').and_return() argv = [ '--keyname', self.keyname, '--appname', self.appid, '--http_port', '80', '--https_port', '443' ] options = ParseArgs(argv, self.function).args AppScaleTools.relocate_app(options)
def test_add_nodes_in_virt_deployment(self): # don't use a 192.168.X.Y IP here, since sometimes we set our virtual # machines to boot with those addresses (and that can mess up our tests). ips_yaml = """ database: 1.2.3.4 zookeeper: 1.2.3.4 appengine: 1.2.3.4 """ # mock out reading the yaml file, and slip in our own fake_yaml_file = flexmock(name='fake_yaml') fake_yaml_file.should_receive('read').and_return(ips_yaml) builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through builtins.should_receive('open').with_args('/tmp/boo.yaml', 'r') \ .and_return(fake_yaml_file) # and pretend we're on a virtualized cluster locations_yaml = yaml.dump({ "infrastructure" : "xen" }) fake_locations_yaml_file = flexmock(name='fake_yaml') fake_locations_yaml_file.should_receive('read').and_return(locations_yaml) builtins.should_receive('open').with_args( LocalState.get_locations_yaml_location(self.keyname), 'r') \ .and_return(fake_locations_yaml_file) # say that the ssh key works flexmock(subprocess) subprocess.should_receive('Popen').with_args(re.compile('ssh'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT, stdin=self.fake_input_file) \ .and_return(self.success) # mock out reading the locations.json file, and slip in our own json flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_json_location(self.keyname)).and_return(True) fake_nodes_json = flexmock(name="fake_nodes_json") fake_nodes_json.should_receive('read').and_return(json.dumps([{ "public_ip" : "public1", "private_ip" : "private1", "jobs" : ["shadow", "login"] }])) builtins.should_receive('open').with_args( LocalState.get_locations_json_location(self.keyname), 'r') \ .and_return(fake_nodes_json) # 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') builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # mock out the SOAP call to the AppController and assume it succeeded json_node_info = json.dumps(yaml.safe_load(ips_yaml)) fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('start_roles_on_nodes') \ .with_args(json_node_info, 'the secret').and_return('OK') flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) argv = [ "--ips", "/tmp/boo.yaml", "--keyname", self.keyname ] options = ParseArgs(argv, self.function).args AppScaleTools.add_instances(options)
def test_upload_tar_gz_app_successfully(self): # mock out generating a random app dir, for later mocks flexmock(uuid) uuid.should_receive('uuid4').and_return('12345678') app_dir = '/tmp/appscale-app-12345678' # add in mocks so that the gzip'ed file gets extracted to /tmp # as well as for removing it later flexmock(os) os.should_receive('mkdir').with_args(app_dir) \ .and_return(True) flexmock(shutil) shutil.should_receive('rmtree').with_args(app_dir).and_return() local_state = flexmock(LocalState) local_state.should_receive('shell')\ .with_args(re.compile('tar zxvf'),False)\ .and_return() # add in mocks so that there is an app.yaml, but with no appid set flexmock(os.path) os.path.should_call('exists') app_yaml_location = AppEngineHelper.get_app_yaml_location(app_dir) os.path.should_receive('exists').with_args(app_yaml_location) \ .and_return(True) # mock out reading the app.yaml file builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through fake_app_yaml = flexmock(name="fake_app_yaml") fake_app_yaml.should_receive('read').and_return(yaml.dump({ 'application' : 'baz', 'runtime' : 'python' })) builtins.should_receive('open').with_args(app_yaml_location, 'r') \ .and_return(fake_app_yaml) # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('Database is at public1') fake_appcontroller.should_receive('done_uploading').with_args('baz', '/var/apps/baz/app/baz.tar.gz', 'the secret').and_return() fake_appcontroller.should_receive('update').with_args(['baz'], 'the secret').and_return() fake_appcontroller.should_receive('is_app_running').with_args('baz', 'the secret').and_return(False).and_return(True) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com', 'the secret').and_return('false') fake_userappserver.should_receive('commit_new_user').with_args( '*****@*****.**', str, 'xmpp_user', 'the secret').and_return('true') fake_userappserver.should_receive('commit_new_user').with_args( 'a@public1', str, 'xmpp_user', 'the secret').and_return('true') fake_userappserver.should_receive('get_app_data').with_args( 'baz', 'the secret').and_return('\n\nnum_ports:0\n') \ .and_return(app_data).and_return(app_data).and_return(app_data) fake_userappserver.should_receive('commit_new_app').with_args( 'baz', '*****@*****.**', 'python', 'the secret').and_return('true') SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com") flexmock(getpass) getpass.should_receive('getpass').and_return('aaaaaa') # mock out making the remote app directory local_state.should_receive('shell')\ .with_args(re.compile('^ssh'),False,5,stdin=re.compile('^mkdir -p'))\ .and_return() # and mock out tarring and copying the app local_state.should_receive('shell')\ .with_args(re.compile('tar -czf'),False)\ .and_return() local_state.should_receive('shell')\ .with_args(re.compile('/tmp/appscale-app-baz.tar.gz'),False,5)\ .and_return() # as well as removing the tar'ed app once we're done copying it flexmock(os) os.should_receive('remove').with_args('/tmp/appscale-app-baz.tar.gz') \ .and_return() # and slap in a mock that says the app comes up after waiting for it # three times fake_socket = flexmock(name='fake_socket') fake_socket.should_receive('connect').with_args(('public1', 8080)).and_raise(Exception).and_raise(Exception) \ .and_return(None) flexmock(socket) socket.should_receive('socket').and_return(fake_socket) argv = [ "--keyname", self.keyname, "--file", self.app_dir + ".tar.gz" ] options = ParseArgs(argv, self.function).args AppScaleTools.upload_app(options)
def test_upload_app_successfully(self): # add in mocks so that there is an app.yaml, but with no appid set flexmock(os.path) os.path.should_call('exists') app_yaml_location = AppEngineHelper.get_app_yaml_location(self.app_dir) os.path.should_receive('exists').with_args(app_yaml_location) \ .and_return(True) # mock out reading the app.yaml file builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through fake_app_yaml = flexmock(name="fake_app_yaml") fake_app_yaml.should_receive('read').and_return( yaml.dump({ 'application': 'baz', 'runtime': 'python27' })) builtins.should_receive('open').with_args(app_yaml_location, 'r') \ .and_return(fake_app_yaml) # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('Database is at public1') fake_appcontroller.should_receive('done_uploading').with_args( 'baz', '/opt/appscale/apps/baz.tar.gz', 'the secret').and_return() fake_appcontroller.should_receive('update').with_args( ['baz'], 'the secret').and_return() fake_appcontroller.should_receive('is_app_running').with_args( 'baz', 'the secret').and_return(False).and_return(True) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com', 'the secret').and_return('false') fake_userappserver.should_receive('does_user_exist').with_args( 'a@public1', 'the secret').and_return('false') fake_userappserver.should_receive('commit_new_user').with_args( '*****@*****.**', str, 'xmpp_user', 'the secret').and_return('true') fake_userappserver.should_receive('commit_new_user').with_args( 'a@public1', str, 'xmpp_user', 'the secret').and_return('true') fake_userappserver.should_receive('get_app_data').with_args( 'baz', 'the secret').and_return('\n\nnum_ports:0\n') \ .and_return(app_data).and_return(app_data).and_return(app_data) fake_userappserver.should_receive('commit_new_app').with_args( 'baz', '*****@*****.**', 'python27', 'the secret').and_return('true') SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com") flexmock(getpass) getpass.should_receive('getpass').and_return('aaaaaa') # mock out making the remote app directory flexmock(subprocess) subprocess.should_receive('Popen').with_args(re.compile('mkdir -p'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) # and mock out tarring and copying the app subprocess.should_receive('Popen').with_args(re.compile('tar -czhf'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) subprocess.should_receive('Popen').with_args(re.compile( '/tmp/appscale-app-baz.tar.gz'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) # as well as removing the tar'ed app once we're done copying it flexmock(os) os.should_receive('remove').with_args('/tmp/appscale-app-baz-1234.tar.gz') \ .and_return() # and slap in a mock that says the app comes up after waiting for it # three times fake_socket = flexmock(name='fake_socket') fake_socket.should_receive('connect').with_args(('public1', 8080)).and_raise(Exception).and_raise(Exception) \ .and_return(None) flexmock(socket) socket.should_receive('socket').and_return(fake_socket) argv = ["--keyname", self.keyname, "--file", self.app_dir] options = ParseArgs(argv, self.function).args (host, port) = AppScaleTools.upload_app(options) self.assertEquals('public1', host) self.assertEquals(8080, port)
def test_upload_app_when_app_exists_on_virt_cluster(self): # we do let you upload an app if it's already running # add in mocks so that there is an app.yaml with an appid set flexmock(os.path) os.path.should_call('exists') app_yaml_location = AppEngineHelper.get_app_yaml_location(self.app_dir) os.path.should_receive('exists').with_args(app_yaml_location) \ .and_return(True) # mock out reading the app.yaml file builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through fake_app_yaml = flexmock(name="fake_app_yaml") fake_app_yaml.should_receive('read').and_return(yaml.dump({ 'application' : 'baz', 'runtime' : 'python27' })) builtins.should_receive('open').with_args(app_yaml_location, 'r') \ .and_return(fake_app_yaml) # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('Database is at public1') fake_appcontroller.should_receive('done_uploading').with_args( 'baz', '/opt/appscale/apps/baz.tar.gz', 'the secret').and_return('OK') fake_appcontroller.should_receive('update').with_args( ['baz'], 'the secret').and_return('OK') flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com', 'the secret').and_return('false') fake_userappserver.should_receive('does_user_exist').with_args( 'a@public1', 'the secret').and_return('false') fake_userappserver.should_receive('commit_new_user').with_args( '*****@*****.**', str, 'xmpp_user', 'the secret').and_return('true') fake_userappserver.should_receive('commit_new_user').with_args( 'a@public1', str, 'xmpp_user', 'the secret').and_return('true') fake_userappserver.should_receive('get_app_data').with_args( 'baz', 'the secret').and_return(app_data) SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com") flexmock(getpass) getpass.should_receive('getpass').and_return('aaaaaa') # mock out making the remote app directory flexmock(subprocess) subprocess.should_receive('Popen').with_args(re.compile('mkdir -p'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) # and mock out tarring and copying the app subprocess.should_receive('Popen').with_args(re.compile('tar -czhf'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) subprocess.should_receive('Popen').with_args(re.compile( '/tmp/appscale-app-baz.tar.gz'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) # as well as removing the tar'ed app once we're done copying it flexmock(os) os.should_receive('remove').with_args('/tmp/appscale-app-baz-1234.tar.gz') \ .and_return() # and slap in a mock that says the app comes up after waiting for it # three times fake_socket = flexmock(name='fake_socket') fake_socket.should_receive('connect').with_args(('public1', 8080)).and_raise(Exception).and_raise(Exception) \ .and_return(None) flexmock(socket) socket.should_receive('socket').and_return(fake_socket) argv = [ "--keyname", self.keyname, "--file", self.app_dir ] options = ParseArgs(argv, self.function).args (host, port) = AppScaleTools.upload_app(options) self.assertEquals('public1', host) self.assertEquals(8080, port)
def test_upload_app_when_app_admin_not_this_user(self): # we don't let you upload an app if the appid is registered to someone else, # so abort # add in mocks so that there is an app.yaml, but with no appid set flexmock(os.path) os.path.should_call('exists') app_yaml_location = AppEngineHelper.get_app_yaml_location(self.app_dir) os.path.should_receive('exists').with_args(app_yaml_location) \ .and_return(True) # mock out reading the app.yaml file builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through fake_app_yaml = flexmock(name="fake_app_yaml") fake_app_yaml.should_receive('read').and_return(yaml.dump({ 'application' : 'baz', 'runtime' : 'python' })) builtins.should_receive('open').with_args(app_yaml_location, 'r') \ .and_return(fake_app_yaml) # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('Database is at public1') flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com', 'the secret').and_return('false') fake_userappserver.should_receive('commit_new_user').with_args( '*****@*****.**', str, 'xmpp_user', 'the secret').and_return('true') fake_userappserver.should_receive('commit_new_user').with_args( 'a@public1', str, 'xmpp_user', 'the secret').and_return('true') fake_userappserver.should_receive('get_app_data').with_args( 'baz', 'the secret').and_return('\n\nnum_ports:0\n\napp_owner:[email protected]') SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com") flexmock(getpass) getpass.should_receive('getpass').and_return('aaaaaa') argv = [ "--keyname", self.keyname, "--file", self.app_dir ] options = ParseArgs(argv, self.function).args self.assertRaises(AppScaleException, AppScaleTools.upload_app, options)
def test_terminate_in_virtual_cluster_and_succeeds(self): # let's say that there is a locations.yaml file, which means appscale is # running, so we should terminate the services on each box flexmock(os.path) os.path.should_call('exists') # set up the fall-through os.path.should_receive('exists').with_args( LocalState.get_secret_key_location(self.keyname)).and_return(True) # mock out reading the locations.yaml file, and pretend that we're on # a virtualized cluster builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') fake_yaml_file = flexmock(name='fake_file') fake_yaml_file.should_receive('read').and_return( yaml.dump({'infrastructure': 'xen'})) builtins.should_receive('open').with_args( LocalState.get_locations_yaml_location(self.keyname), 'r') \ .and_return(fake_yaml_file) # mock out reading the json file, and pretend that we're running in a # two node deployment os.path.should_receive('exists').with_args( LocalState.get_locations_json_location( self.keyname)).and_return(True) fake_json_file = flexmock(name='fake_file') fake_json_file.should_receive('read').and_return( json.dumps([{ 'public_ip': 'public1', 'jobs': ['shadow'] }, { 'public_ip': 'public2', 'jobs': ['appengine'] }])) builtins.should_receive('open').with_args( LocalState.get_locations_json_location(self.keyname), 'r') \ .and_return(fake_json_file) # and slip in a fake secret file fake_secret_file = flexmock(name='fake_file') fake_secret_file.should_receive('read').and_return('the secret') builtins.should_receive('open').with_args( LocalState.get_secret_key_location(self.keyname), 'r') \ .and_return(fake_secret_file) # mock out talking to the appcontroller, and assume that it tells us there # there are still two machines in this deployment fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('get_all_public_ips').with_args('the secret') \ .and_return(json.dumps(['public1', 'public2'])) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) # and mock out the ssh call to kill the remote appcontroller, assuming that # it fails the first time and passes the second flexmock(subprocess) subprocess.should_receive('Popen').with_args(re.compile('controller stop'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.failed).and_return(self.success) # next, mock out our checks to see how the stopping process is going and # assume that it has stopped flexmock(subprocess) subprocess.should_receive('Popen').with_args(re.compile('ps x'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) # finally, mock out removing the yaml file, json file, and secret key from # this machine flexmock(os) os.should_receive('remove').with_args( LocalState.get_locations_yaml_location(self.keyname)).and_return() os.should_receive('remove').with_args( LocalState.get_locations_json_location(self.keyname)).and_return() os.should_receive('remove').with_args( LocalState.get_secret_key_location(self.keyname)).and_return() # also mock out asking the user for confirmation on shutting down # their cloud builtins.should_receive('raw_input').and_return('yes') argv = ["--keyname", self.keyname] options = ParseArgs(argv, self.function).args AppScaleTools.terminate_instances(options)
def test_upload_app_when_app_exists_on_virt_cluster(self): # we do let you upload an app if it's already running # add in mocks so that there is an app.yaml with an appid set flexmock(os.path) os.path.should_call('exists') app_yaml_location = AppEngineHelper.get_app_yaml_location(self.app_dir) os.path.should_receive('exists').with_args(app_yaml_location) \ .and_return(True) # mock out reading the app.yaml file builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through fake_app_yaml = flexmock(name="fake_app_yaml") fake_app_yaml.should_receive('read').and_return( yaml.dump({ 'application': 'baz', 'runtime': 'python27' })) builtins.should_receive('open').with_args(app_yaml_location, 'r') \ .and_return(fake_app_yaml) # Mock out service host and port app_data = { 'owner': '*****@*****.**', 'hosts': { '192.168.1.1': { 'http': 8080, 'https': 4380 } } } app_stats_data = { 'apps': { 'baz': { 'http': 8080, 'language': 'python27', 'total_reqs': 'no_change', 'appservers': 1, 'https': 4380, 'reqs_enqueued': None } } } # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('Database is at public1') fake_appcontroller.should_receive('done_uploading').with_args( 'baz', '/opt/appscale/apps/baz.tar.gz', 'the secret').and_return('OK') fake_appcontroller.should_receive('update').with_args( ['baz'], 'the secret').and_return('OK') fake_appcontroller.should_receive('does_user_exist').with_args( '*****@*****.**', 'the secret').and_return('true') fake_appcontroller.should_receive('does_user_exist').with_args( 'a@public1', 'the secret').and_return('true') fake_appcontroller.should_receive('does_app_exist').with_args( 'baz', 'the secret').and_return(json.dumps(app_data)) fake_appcontroller.should_receive('get_app_data').with_args( 'baz', 'the secret').and_return(json.dumps(app_data)) fake_appcontroller.should_receive('get_all_stats').with_args( 'the secret').and_return(json.dumps(app_stats_data)) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com") flexmock(getpass) getpass.should_receive('getpass').and_return('aaaaaa') # mock out making the remote app directory flexmock(subprocess) subprocess.should_receive('Popen').with_args(re.compile('mkdir -p'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) # and mock out tarring and copying the app subprocess.should_receive('Popen').with_args(re.compile('tar -czhf'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) subprocess.should_receive('Popen').with_args(re.compile( '/tmp/appscale-app-baz.tar.gz'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) # as well as removing the tar'ed app once we're done copying it flexmock(os) os.should_receive('remove').with_args('/tmp/appscale-app-baz-1234.tar.gz') \ .and_return() # and slap in a mock that says the app comes up after waiting for it # three times fake_socket = flexmock(name='fake_socket') fake_socket.should_receive('connect').with_args(('public1', 8080)).and_raise(Exception).and_raise(Exception) \ .and_return(None) flexmock(socket) socket.should_receive('socket').and_return(fake_socket) argv = ["--keyname", self.keyname, "--file", self.app_dir] options = ParseArgs(argv, self.function).args (host, port) = AppScaleTools.upload_app(options) self.assertEquals('public1', host) self.assertEquals(8080, port)
def test_appscale_in_two_node_virt_deployment(self): # pretend that the place we're going to put logs into doesn't exist flexmock(os.path) os.path.should_call('exists') # set the fall-through os.path.should_receive('exists').with_args('/tmp/foobaz').and_return( False) # and mock out the mkdir operation flexmock(os) os.should_receive('mkdir').with_args('/tmp/foobaz').and_return() # next, mock out finding the login ip address os.path.should_receive('exists').with_args( LocalState.get_locations_json_location( self.keyname)).and_return(True) fake_nodes_json = flexmock(name="fake_nodes_json") nodes_info = { "node_info": [ { "public_ip": "public1", "private_ip": "private1", "jobs": [ "load_balancer", "taskqueue_master", "zookeeper", "db_master", "taskqueue", "shadow", "login" ] }, { "public_ip": "public2", "private_ip": "private2", "jobs": ["memcache", "appengine", "zookeeper"] }, { "public_ip": "public3", "private_ip": "private3", "jobs": ["memcache", "appengine"] }, ] } fake_nodes_json.should_receive('read').and_return( json.dumps(nodes_info)) builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') builtins.should_receive('open').with_args( LocalState.get_locations_json_location(self.keyname), 'r') \ .and_return(fake_nodes_json) # 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') builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) # and slip in a fake appcontroller to report on the two IP addrs fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('get_all_public_ips').with_args( 'the secret').and_return( json.dumps(['public1', 'public2', 'public3'])) fake_appcontroller.should_receive('get_role_info').with_args( 'the secret').and_return(json.dumps(nodes_info['node_info'])) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) # fake the creation of the log directories locally flexmock(utils) utils.should_receive('mkdir').with_args( '/tmp/foobaz/symlinks/private-ips') utils.should_receive('mkdir').with_args('/tmp/foobaz/public1') utils.should_receive('mkdir').with_args( '/tmp/foobaz/public1/cassandra') utils.should_receive('mkdir').with_args('/tmp/foobaz/public1/rabbitmq') utils.should_receive('mkdir').with_args('/tmp/foobaz/public2') utils.should_receive('mkdir').with_args( '/tmp/foobaz/public2/cassandra') utils.should_receive('mkdir').with_args('/tmp/foobaz/public2/rabbitmq') utils.should_receive('mkdir').with_args('/tmp/foobaz/public3') utils.should_receive('mkdir').with_args( '/tmp/foobaz/public3/cassandra') utils.should_receive('mkdir').with_args('/tmp/foobaz/public3/rabbitmq') utils.should_receive('mkdir').with_args( '/tmp/foobaz/symlinks/load_balancer') utils.should_receive('mkdir').with_args( '/tmp/foobaz/symlinks/taskqueue_master') utils.should_receive('mkdir').with_args( '/tmp/foobaz/symlinks/zookeeper') utils.should_receive('mkdir').with_args( '/tmp/foobaz/symlinks/db_master') utils.should_receive('mkdir').with_args( '/tmp/foobaz/symlinks/taskqueue') utils.should_receive('mkdir').with_args('/tmp/foobaz/symlinks/shadow') utils.should_receive('mkdir').with_args('/tmp/foobaz/symlinks/login') utils.should_receive('mkdir').with_args( '/tmp/foobaz/symlinks/memcache') utils.should_receive('mkdir').with_args( '/tmp/foobaz/symlinks/appengine') # fake creation of symlink to for friendly navigation links_mapping = { '../../public1': [ '/tmp/foobaz/symlinks/private-ips/private1', '/tmp/foobaz/symlinks/load_balancer/public1', '/tmp/foobaz/symlinks/taskqueue_master/public1', '/tmp/foobaz/symlinks/zookeeper/public1', '/tmp/foobaz/symlinks/db_master/public1', '/tmp/foobaz/symlinks/taskqueue/public1', '/tmp/foobaz/symlinks/shadow/public1', '/tmp/foobaz/symlinks/login/public1', ], '../../public2': [ '/tmp/foobaz/symlinks/private-ips/private2', '/tmp/foobaz/symlinks/zookeeper/public2', '/tmp/foobaz/symlinks/appengine/public2', '/tmp/foobaz/symlinks/memcache/public2', ], '../../public3': [ '/tmp/foobaz/symlinks/private-ips/private3', '/tmp/foobaz/symlinks/appengine/public3', '/tmp/foobaz/symlinks/memcache/public3', ] } for original_dir, expected_links in links_mapping.iteritems(): for expected_link in expected_links: os.should_receive('symlink').with_args(original_dir, expected_link) # finally, fake the copying of the log files flexmock(subprocess) subprocess.should_receive('Popen').with_args(re.compile('/var/log/appscale'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) subprocess.should_receive('Popen').with_args(re.compile('/var/log/kern.log*'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) subprocess.should_receive('Popen').with_args(re.compile('/var/log/monit*'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) subprocess.should_receive('Popen').with_args( re.compile('/var/log/haproxy*'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT).and_return(self.success) subprocess.should_receive('Popen').with_args(re.compile('/var/log/nginx'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) subprocess.should_receive('Popen').with_args( re.compile('/var/log/rabbitmq'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT).and_return(self.success) subprocess.should_receive('Popen').with_args(re.compile('/var/log/syslog*'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) subprocess.should_receive('Popen').with_args(re.compile('/var/log/zookeeper'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) subprocess.should_receive('Popen').with_args( re.compile('/opt/cassandra/cassandra/logs'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT).and_return(self.success) argv = ["--keyname", self.keyname, "--location", "/tmp/foobaz"] options = ParseArgs(argv, self.function).args AppScaleTools.gather_logs(options)
def test_upload_app_when_app_admin_not_this_user(self): # we don't let you upload an app if the appid is registered to someone else, # so abort # add in mocks so that there is an app.yaml, but with no appid set flexmock(os.path) os.path.should_call('exists') app_yaml_location = AppEngineHelper.get_app_yaml_location(self.app_dir) os.path.should_receive('exists').with_args(app_yaml_location) \ .and_return(True) # mock out reading the app.yaml file builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through fake_app_yaml = flexmock(name="fake_app_yaml") fake_app_yaml.should_receive('read').and_return( yaml.dump({ 'application': 'baz', 'runtime': 'python27' })) builtins.should_receive('open').with_args(app_yaml_location, 'r') \ .and_return(fake_app_yaml) # Mock out service host and port app_data = {'owner': '*****@*****.**', 'hosts': {}} app_data_not_admin = {'owner': '*****@*****.**'} # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('Database is at public1') fake_appcontroller.should_receive('does_user_exist').with_args( '*****@*****.**', 'the secret').and_return('true') fake_appcontroller.should_receive('does_user_exist').with_args( 'a@public1', 'the secret').and_return('true') fake_appcontroller.should_receive('does_app_exist').with_args( 'baz', 'the secret').and_return(json.dumps(app_data)) fake_appcontroller.should_receive('get_app_data').with_args( 'baz', 'the secret').and_return(json.dumps(app_data_not_admin)) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com") flexmock(getpass) getpass.should_receive('getpass').and_return('aaaaaa') argv = ["--keyname", self.keyname, "--file", self.app_dir] options = ParseArgs(argv, self.function).args self.assertRaises(AppScaleException, AppScaleTools.upload_app, options)
def test_terminate_in_virtual_cluster_and_succeeds(self): # let's say that there is a locations.yaml file, which means appscale is # running, so we should terminate the services on each box flexmock(os.path) os.path.should_call('exists') # set up the fall-through os.path.should_receive('exists').with_args( LocalState.get_locations_yaml_location(self.keyname)).and_return(True) # mock out reading the locations.yaml file, and pretend that we're on # a virtualized cluster builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') fake_yaml_file = flexmock(name='fake_file') fake_yaml_file.should_receive('read').and_return(yaml.dump({ 'infrastructure' : 'xen' })) builtins.should_receive('open').with_args( LocalState.get_locations_yaml_location(self.keyname), 'r') \ .and_return(fake_yaml_file) # mock out reading the json file, and pretend that we're running in a # two node deployment os.path.should_receive('exists').with_args( LocalState.get_locations_json_location(self.keyname)).and_return(True) fake_json_file = flexmock(name='fake_file') fake_json_file.should_receive('read').and_return(json.dumps([ { 'public_ip' : 'public1', 'jobs' : ['shadow'] }, { 'public_ip' : 'public2', 'jobs' : ['appengine'] } ])) builtins.should_receive('open').with_args( LocalState.get_locations_json_location(self.keyname), 'r') \ .and_return(fake_json_file) # and slip in a fake secret file fake_secret_file = flexmock(name='fake_file') fake_secret_file.should_receive('read').and_return('the secret') builtins.should_receive('open').with_args( LocalState.get_secret_key_location(self.keyname), 'r') \ .and_return(fake_secret_file) # mock out talking to the appcontroller, and assume that it tells us there # there are still two machines in this deployment fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('get_all_public_ips').with_args('the secret') \ .and_return(json.dumps(['public1', 'public2'])) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://public1:17443') \ .and_return(fake_appcontroller) # and mock out the ssh call to kill the remote appcontroller, assuming that # it fails the first time and passes the second flexmock(subprocess) subprocess.should_receive('Popen').with_args(re.compile('controller stop'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.failed).and_return(self.success) # next, mock out our checks to see how the stopping process is going and # assume that it has stopped flexmock(subprocess) subprocess.should_receive('Popen').with_args(re.compile('ps x'), shell=True, stdout=self.fake_temp_file, stderr=subprocess.STDOUT) \ .and_return(self.success) # finally, mock out removing the yaml file, json file, and secret key from # this machine flexmock(os) os.should_receive('remove').with_args( LocalState.get_locations_yaml_location(self.keyname)).and_return() os.should_receive('remove').with_args( LocalState.get_locations_json_location(self.keyname)).and_return() os.should_receive('remove').with_args( LocalState.get_secret_key_location(self.keyname)).and_return() argv = [ "--keyname", self.keyname ] options = ParseArgs(argv, self.function).args AppScaleTools.terminate_instances(options)
def test_upload_php_app_successfully(self): app_dir = '/tmp/appscale-app-1234' # add in mocks so that the gzip'ed file gets extracted to /tmp # as well as for removing it later flexmock(os) os.should_receive('mkdir').with_args(app_dir) \ .and_return(True) flexmock(shutil) shutil.should_receive('rmtree').with_args(app_dir).and_return() local_state = flexmock(LocalState) local_state.should_receive('shell')\ .with_args(re.compile('tar zxvf'),False)\ .and_return() # add in mocks so that there is an app.yaml, but with no appid set flexmock(os.path) os.path.should_call('exists') app_yaml_location = AppEngineHelper.get_app_yaml_location(app_dir) os.path.should_receive('exists').with_args(app_yaml_location) \ .and_return(True) # mock out reading the app.yaml file builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through fake_app_yaml = flexmock(name="fake_app_yaml") fake_app_yaml.should_receive('read').and_return(yaml.dump({ 'application' : 'baz', 'runtime' : 'php' })) builtins.should_receive('open').with_args(app_yaml_location, 'r') \ .and_return(fake_app_yaml) # Mock out service host and port app_data = {'owner' : '*****@*****.**', 'hosts' : {'192.168.1.1' : { 'http' : 8080, 'https' : 4380 }}} app_stats_data = {'apps': {'baz': {'http': 8080, 'language': 'python27', 'total_reqs': 'no_change', 'appservers': 1, 'https': 4380, 'reqs_enqueued': None}}} # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('Database is at public1') fake_appcontroller.should_receive('done_uploading').with_args('baz', '/opt/appscale/apps/baz.tar.gz', 'the secret').and_return() fake_appcontroller.should_receive('update').with_args(['baz'], 'the secret').and_return() fake_appcontroller.should_receive('is_app_running').with_args('baz', 'the secret').and_return(False).and_return(True) fake_appcontroller.should_receive('does_user_exist').with_args( '*****@*****.**', 'the secret').and_return('true') fake_appcontroller.should_receive('does_user_exist').with_args( 'a@public1', 'the secret').and_return('true') fake_appcontroller.should_receive('does_app_exist').with_args( 'baz', 'the secret').and_return(json.dumps(app_data)) fake_appcontroller.should_receive('get_app_data').with_args( 'baz', 'the secret').and_return(json.dumps(app_data)) fake_appcontroller.should_receive('get_all_stats').with_args( 'the secret').and_return(json.dumps(app_stats_data)) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com") flexmock(getpass) getpass.should_receive('getpass').and_return('aaaaaa') # mock out making the remote app directory local_state.should_receive('shell') \ .with_args(re.compile('^ssh'), False, 5, stdin=re.compile('^mkdir -p')) \ .and_return() # and mock out tarring and copying the app local_state.should_receive('shell') \ .with_args(re.compile('tar -czf'), False) \ .and_return() local_state.should_receive('shell') \ .with_args(re.compile('/tmp/appscale-app-baz.tar.gz'), False, 5) \ .and_return() # as well as removing the tar'ed app once we're done copying it flexmock(os) os.should_receive('remove').with_args('/tmp/appscale-app-baz-1234.tar.gz') \ .and_return() os.should_receive('listdir').and_return(['app.yaml','index.py']) # and slap in a mock that says the app comes up after waiting for it # three times fake_socket = flexmock(name='fake_socket') fake_socket.should_receive('connect').with_args(('public1', 8080)).and_raise(Exception).and_raise(Exception) \ .and_return(None) flexmock(socket) socket.should_receive('socket').and_return(fake_socket) argv = [ "--keyname", self.keyname, "--file", self.app_dir + ".tar.gz" ] options = ParseArgs(argv, self.function).args (host, port) = AppScaleTools.upload_app(options) self.assertEquals('public1', host) self.assertEquals(8080, port)
def test_upload_php_app_successfully(self): app_dir = '/tmp/appscale-app-1234' # add in mocks so that the gzip'ed file gets extracted to /tmp # as well as for removing it later flexmock(os) os.should_receive('mkdir').with_args(app_dir) \ .and_return(True) flexmock(shutil) shutil.should_receive('rmtree').with_args(app_dir).and_return() local_state = flexmock(LocalState) local_state.should_receive('shell')\ .with_args(re.compile('tar zxvf'),False)\ .and_return() # add in mocks so that there is an app.yaml, but with no appid set flexmock(os.path) os.path.should_call('exists') app_yaml_location = AppEngineHelper.get_app_yaml_location(app_dir) os.path.should_receive('exists').with_args(app_yaml_location) \ .and_return(True) # mock out reading the app.yaml file builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through fake_app_yaml = flexmock(name="fake_app_yaml") fake_app_yaml.should_receive('read').and_return( yaml.dump({ 'application': 'baz', 'runtime': 'php' })) builtins.should_receive('open').with_args(app_yaml_location, 'r') \ .and_return(fake_app_yaml) # Mock out service host and port app_data = { 'owner': '*****@*****.**', 'hosts': { '192.168.1.1': { 'http': 8080, 'https': 4380 } } } app_stats_data = { 'apps': { 'baz': { 'http': 8080, 'language': 'python27', 'total_reqs': 'no_change', 'appservers': 1, 'https': 4380, 'reqs_enqueued': None } } } remote_tarball = '/opt/appscale/apps/baz.tar.gz' # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('Database is at public1') fake_appcontroller.should_receive('done_uploading').with_args( 'baz', remote_tarball, 'the secret').and_return() fake_appcontroller.should_receive('update').with_args( ['baz'], 'the secret').and_return() fake_appcontroller.should_receive('does_user_exist').with_args( '*****@*****.**', 'the secret').and_return('true') fake_appcontroller.should_receive('does_user_exist').with_args( 'a@public1', 'the secret').and_return('true') fake_appcontroller.should_receive('does_app_exist').with_args( 'baz', 'the secret').and_return(json.dumps(app_data)) fake_appcontroller.should_receive('get_app_data').with_args( 'baz', 'the secret').and_return(json.dumps(app_data)) fake_appcontroller.should_receive('get_all_stats').with_args( 'the secret').and_return(json.dumps(app_stats_data)) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com") flexmock(getpass) getpass.should_receive('getpass').and_return('aaaaaa') # mock out making the remote app directory local_state.should_receive('shell') \ .with_args(re.compile('^ssh'), False, 5, stdin=re.compile('^mkdir -p')) \ .and_return() # and mock out tarring and copying the app local_state.should_receive('shell') \ .with_args(re.compile('tar -czf'), False) \ .and_return() local_state.should_receive('shell') \ .with_args(re.compile('/tmp/appscale-app-baz.tar.gz'), False, 5) \ .and_return() # as well as removing the tar'ed app once we're done copying it flexmock(os) os.should_receive('remove').with_args('/tmp/appscale-app-baz-1234.tar.gz') \ .and_return() os.should_receive('listdir').and_return(['app.yaml', 'index.py']) # and slap in a mock that says the app comes up after waiting for it # three times fake_socket = flexmock(name='fake_socket') fake_socket.should_receive('connect').with_args(('public1', 8080)).and_raise(Exception).and_raise(Exception) \ .and_return(None) flexmock(socket) socket.should_receive('socket').and_return(fake_socket) flexmock(RemoteHelper).should_receive('copy_app_to_host').\ and_return(remote_tarball) argv = ["--keyname", self.keyname, "--file", self.app_dir + ".tar.gz"] options = ParseArgs(argv, self.function).args (host, port) = AppScaleTools.upload_app(options) self.assertEquals('public1', host) self.assertEquals(8080, port)
def test_appscale_in_one_node_cloud_deployment_manual_spot_price(self): # let's say that appscale isn't already running local_state = flexmock(LocalState) local_state.should_receive('ensure_appscale_isnt_running').and_return() local_state.should_receive('make_appscale_directory').and_return() # 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 builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through 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() builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) builtins.should_receive('open').with_args(secret_key_location, 'w') \ .and_return(fake_secret) # mock out interactions with AWS fake_ec2 = flexmock(name='fake_ec2') # first, pretend that our image does exist in EC2 fake_ec2.should_receive('get_image').with_args('ami-ABCDEFG') \ .and_return() # next, assume that our keypair doesn't exist yet fake_ec2.should_receive('get_key_pair').with_args(self.keyname) \ .and_return(None) # same for the security group fake_ec2.should_receive('get_all_security_groups').and_return([]) # mock out creating the keypair fake_key = flexmock(name='fake_key', material='baz') local_state.should_receive('write_key_file').with_args( re.compile(self.keyname), fake_key.material).and_return() fake_ec2.should_receive('create_key_pair').with_args(self.keyname) \ .and_return(fake_key) # and the same for the security group fake_ec2.should_receive('create_security_group').with_args('bazgroup', str).and_return() fake_ec2.should_receive('authorize_security_group').with_args('bazgroup', from_port=1, to_port=65535, ip_protocol='udp', cidr_ip='0.0.0.0/0') fake_ec2.should_receive('authorize_security_group').with_args('bazgroup', from_port=1, to_port=65535, ip_protocol='tcp', cidr_ip='0.0.0.0/0') fake_ec2.should_receive('authorize_security_group').with_args('bazgroup', ip_protocol='icmp', cidr_ip='0.0.0.0/0') # also mock out acquiring a spot instance fake_ec2.should_receive('request_spot_instances').with_args('1.23', 'ami-ABCDEFG', key_name=self.keyname, security_groups=['bazgroup'], instance_type='m1.large', count=1) # assume that there are no instances running initially, and that the # instance we spawn starts as pending, then becomes running no_instances = flexmock(name='no_instances', instances=[]) pending_instance = flexmock(name='pending_instance', state='pending', key_name=self.keyname, id='i-ABCDEFG') pending_reservation = flexmock(name='pending_reservation', instances=[pending_instance]) running_instance = flexmock(name='running_instance', state='running', key_name=self.keyname, id='i-ABCDEFG', public_dns_name='public1', private_dns_name='private1') running_reservation = flexmock(name='running_reservation', instances=[running_instance]) fake_ec2.should_receive('get_all_instances').and_return(no_instances) \ .and_return(pending_reservation).and_return(running_reservation) # finally, inject the mocked EC2 in flexmock(boto) boto.should_receive('connect_ec2').and_return(fake_ec2) # assume that root login is not enabled local_state.should_receive('shell').with_args(re.compile('ssh'), False, 1, stdin='ls').and_return(RemoteHelper.LOGIN_AS_UBUNTU_USER) # assume that we can enable root login local_state.should_receive('shell').with_args(re.compile('ssh'), False, 5, stdin=re.compile('sudo cp')).and_return() # and assume that we can copy over our ssh keys fine local_state.should_receive('shell').with_args(re.compile('scp .*[r|d]sa'), False, 5).and_return() local_state.should_receive('shell').with_args(re.compile('scp .*{0}' .format(self.keyname)), False, 5).and_return() # mock out seeing if the image is appscale-compatible, and assume it is # mock out our attempts to find /etc/appscale and presume it does exist local_state.should_receive('shell').with_args(re.compile('ssh'), False, 5, stdin=re.compile('/etc/appscale')).and_return() # mock out our attempts to find /etc/appscale/version and presume it does # exist local_state.should_receive('shell').with_args(re.compile('ssh'), False, 5, stdin=re.compile('/etc/appscale/{0}' .format(APPSCALE_VERSION))) # put in a mock indicating that the database the user wants is supported local_state.should_receive('shell').with_args(re.compile('ssh'), False, 5, stdin=re.compile('/etc/appscale/{0}/{1}' .format(APPSCALE_VERSION, 'cassandra'))) # mock out generating the private key local_state.should_receive('shell').with_args(re.compile('openssl'), False, stdin=None) # assume that we started god fine local_state.should_receive('shell').with_args(re.compile('ssh'), False, 5, stdin=re.compile('god &')) # and that we copied over the AppController's god file local_state.should_receive('shell').with_args(re.compile('scp'), False, 5, stdin=re.compile('appcontroller.god')) # also, that we started the AppController itself local_state.should_receive('shell').with_args(re.compile('ssh'), False, 5, stdin=re.compile('god load')) # 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) # assume that the AppController comes up on the third attempt fake_socket.should_receive('connect').with_args(('public1', AppControllerClient.PORT)).and_raise(Exception).and_raise(Exception) \ .and_return(None) # same for the UserAppServer fake_socket.should_receive('connect').with_args(('public1', UserAppClient.PORT)).and_raise(Exception).and_raise(Exception) \ .and_return(None) # as well as for the AppDashboard fake_socket.should_receive('connect').with_args(('public1', RemoteHelper.APP_DASHBOARD_PORT)).and_raise(Exception) \ .and_raise(Exception).and_return(None) flexmock(socket) socket.should_receive('socket').and_return(fake_socket) # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('set_parameters').with_args(list, list, ['none'], 'the secret').and_return('OK') fake_appcontroller.should_receive('get_all_public_ips').with_args('the secret') \ .and_return(json.dumps(['public1'])) role_info = [{ 'public_ip' : 'public1', 'private_ip' : 'private1', 'jobs' : ['shadow', 'login'] }] fake_appcontroller.should_receive('get_role_info').with_args('the secret') \ .and_return(json.dumps(role_info)) fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('nothing interesting here') \ .and_return('Database is at not-up-yet') \ .and_return('Database is at public1') fake_appcontroller.should_receive('is_done_initializing') \ .and_return(False) \ .and_return(True) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com', str, 'xmpp_user', 'the secret') \ .and_return('true') fake_userappserver.should_receive('commit_new_user').with_args( 'a@public1', str, 'xmpp_user', 'the secret') \ .and_return('true') fake_userappserver.should_receive('set_cloud_admin_status').with_args( '*****@*****.**', 'true', 'the secret').and_return() fake_userappserver.should_receive('set_capabilities').with_args( '*****@*****.**', UserAppClient.ADMIN_CAPABILITIES, 'the secret').and_return() SOAPpy.should_receive('SOAPProxy').with_args('https://public1:4343') \ .and_return(fake_userappserver) argv = [ "--min", "1", "--max", "1", "--infrastructure", "ec2", "--machine", "ami-ABCDEFG", "--use_spot_instances", "--max_spot_price", "1.23", "--keyname", self.keyname, "--group", "bazgroup", "--test" ] options = ParseArgs(argv, self.function).args AppScaleTools.run_instances(options)
def test_appscale_in_one_node_virt_deployment(self): # let's say that appscale isn't already running local_state = flexmock(LocalState) local_state.should_receive('ensure_appscale_isnt_running').and_return() local_state.should_receive('make_appscale_directory').and_return() rh = flexmock(RemoteHelper) rh.should_receive('copy_deployment_credentials').and_return() # 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 builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through 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() builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) builtins.should_receive('open').with_args(secret_key_location, 'w') \ .and_return(fake_secret) # mock out copying over the keys local_state.should_receive('shell')\ .with_args(re.compile('^scp .*.key'),False,5) # mock out our attempts to find /etc/appscale and presume it does exist local_state.should_receive('shell')\ .with_args(re.compile('^ssh'),False,5,\ stdin=re.compile('ls /etc/appscale'))\ .and_return() # mock out our attempts to find /etc/appscale/version and presume it does # exist local_state.should_receive('shell')\ .with_args(re.compile('^ssh'),False,5,\ stdin=re.compile('ls /etc/appscale/{0}'.format(APPSCALE_VERSION)))\ .and_return() # finally, put in a mock indicating that the database the user wants # is supported local_state.should_receive('shell')\ .with_args(re.compile('^ssh'),False,5,\ stdin=re.compile('ls /etc/appscale/{0}/{1}'\ .format(APPSCALE_VERSION, 'cassandra')))\ .and_return() # mock out generating the private key local_state.should_receive('shell')\ .with_args(re.compile('^openssl'),False,stdin=None)\ .and_return() # mock out removing the old json file local_state = flexmock(LocalState) local_state.should_receive('shell')\ .with_args(re.compile('^ssh'),False,5,stdin=re.compile('rm -rf'))\ .and_return() # assume that we started god fine local_state.should_receive('shell')\ .with_args(re.compile('^ssh'),False,5,stdin=re.compile('god &'))\ .and_return() # and that we copied over the AppController's god file local_state.should_receive('shell')\ .with_args(re.compile('scp .*appcontroller\.god.*'),False,5)\ .and_return() # also, that we started the AppController itself local_state.should_receive('shell')\ .with_args(re.compile('^ssh'),False,5,\ stdin=re.compile('^god load .*appcontroller\.god'))\ .and_return() # assume that the AppController comes up on the third attempt fake_socket = flexmock(name='fake_socket') fake_socket.should_receive('connect').with_args(('1.2.3.4', AppControllerClient.PORT)).and_raise(Exception).and_raise(Exception) \ .and_return(None) # same for the UserAppServer fake_socket.should_receive('connect').with_args(('1.2.3.4', UserAppClient.PORT)).and_raise(Exception).and_raise(Exception) \ .and_return(None) # as well as for the AppDashboard fake_socket.should_receive('connect').with_args(('1.2.3.4', RemoteHelper.APP_DASHBOARD_PORT)).and_raise(Exception) \ .and_raise(Exception).and_return(None) flexmock(socket) socket.should_receive('socket').and_return(fake_socket) # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('set_parameters').with_args(list, list, ['none'], 'the secret').and_return('OK') fake_appcontroller.should_receive('get_all_public_ips')\ .with_args('the secret') \ .and_return(json.dumps(['1.2.3.4'])) role_info = [{ 'public_ip' : '1.2.3.4', 'private_ip' : '1.2.3.4', 'jobs' : ['shadow', 'login'] }] fake_appcontroller.should_receive('get_role_info').with_args('the secret') \ .and_return(json.dumps(role_info)) fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('nothing interesting here') \ .and_return('Database is at not-up-yet') \ .and_return('Database is at 1.2.3.4') fake_appcontroller.should_receive('is_done_initializing') \ .and_return(False) \ .and_return(True) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://1.2.3.4:17443') \ .and_return(fake_appcontroller) # mock out reading the locations.json file, and slip in our own json local_state.should_receive('get_local_nodes_info').and_return(json.loads( json.dumps([{ "public_ip" : "1.2.3.4", "private_ip" : "1.2.3.4", "jobs" : ["shadow", "login"] }]))) # copying over the locations yaml and json files should be fine local_state.should_receive('shell')\ .with_args(re.compile('^scp .*/etc/appscale/locations-bookey.yaml'),\ False,5)\ .and_return() local_state.should_receive('shell')\ .with_args(re.compile('^scp .*/etc/appscale/locations-bookey.json'),\ False,5)\ .and_return() local_state.should_receive('shell')\ .with_args(re.compile('^scp .*/root/.appscale/locations-bookey.json'),\ False,5)\ .and_return() # same for the secret key local_state.should_receive('shell')\ .with_args(re.compile('^scp .*.secret'),False,5)\ .and_return() # mock out calls to the UserAppServer and presume that calls to create new # users succeed fake_userappserver = flexmock(name='fake_appcontroller') fake_userappserver.should_receive('commit_new_user').with_args( '*****@*****.**', str, 'xmpp_user', 'the secret') \ .and_return('true') fake_userappserver.should_receive('commit_new_user').with_args( '[email protected]', str, 'xmpp_user', 'the secret') \ .and_return('true') fake_userappserver.should_receive('set_cloud_admin_status').with_args( '*****@*****.**', 'true', 'the secret').and_return() fake_userappserver.should_receive('set_capabilities').with_args( '*****@*****.**', UserAppClient.ADMIN_CAPABILITIES, 'the secret').and_return() SOAPpy.should_receive('SOAPProxy').with_args('https://1.2.3.4:4343') \ .and_return(fake_userappserver) # don't use a 192.168.X.Y IP here, since sometimes we set our virtual # machines to boot with those addresses (and that can mess up our tests). ips_layout = yaml.safe_load(""" master : 1.2.3.4 database: 1.2.3.4 zookeeper: 1.2.3.4 appengine: 1.2.3.4 """) argv = [ "--ips_layout", base64.b64encode(yaml.dump(ips_layout)), "--keyname", self.keyname, "--test" ] options = ParseArgs(argv, self.function).args AppScaleTools.run_instances(options)
def test_appscale_in_one_node_cloud_deployment_manual_spot_price(self): # let's say that appscale isn't already running local_state = flexmock(LocalState) local_state.should_receive('ensure_appscale_isnt_running').and_return() local_state.should_receive('make_appscale_directory').and_return() # 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 builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through 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() builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) builtins.should_receive('open').with_args(secret_key_location, 'w') \ .and_return(fake_secret) # mock out interactions with AWS fake_ec2 = flexmock(name='fake_ec2') # first, pretend that our image does exist in EC2 fake_ec2.should_receive('get_image').with_args('ami-ABCDEFG') \ .and_return() # next, assume that our keypair doesn't exist yet fake_ec2.should_receive('get_key_pair').with_args(self.keyname) \ .and_return(None) # same for the security group fake_ec2.should_receive('get_all_security_groups').and_return([]) # mock out creating the keypair fake_key = flexmock(name='fake_key', material='baz') local_state.should_receive('write_key_file').with_args( re.compile(self.keyname), fake_key.material).and_return() fake_ec2.should_receive('create_key_pair').with_args(self.keyname) \ .and_return(fake_key) # and the same for the security group fake_ec2.should_receive('create_security_group').with_args('bazgroup', str).and_return() fake_ec2.should_receive('authorize_security_group').with_args('bazgroup', from_port=1, to_port=65535, ip_protocol='udp', cidr_ip='0.0.0.0/0') fake_ec2.should_receive('authorize_security_group').with_args('bazgroup', from_port=1, to_port=65535, ip_protocol='tcp', cidr_ip='0.0.0.0/0') fake_ec2.should_receive('authorize_security_group').with_args('bazgroup', ip_protocol='icmp', cidr_ip='0.0.0.0/0') # also mock out acquiring a spot instance fake_ec2.should_receive('request_spot_instances').with_args('1.23', 'ami-ABCDEFG', key_name=self.keyname, security_groups=['bazgroup'], instance_type='m1.large', count=1) # assume that there are no instances running initially, and that the # instance we spawn starts as pending, then becomes running no_instances = flexmock(name='no_instances', instances=[]) pending_instance = flexmock(name='pending_instance', state='pending', key_name=self.keyname, id='i-ABCDEFG') pending_reservation = flexmock(name='pending_reservation', instances=[pending_instance]) running_instance = flexmock(name='running_instance', state='running', key_name=self.keyname, id='i-ABCDEFG', public_dns_name='public1', private_dns_name='private1') running_reservation = flexmock(name='running_reservation', instances=[running_instance]) fake_ec2.should_receive('get_all_instances').and_return(no_instances) \ .and_return(pending_reservation).and_return(running_reservation) # finally, inject the mocked EC2 in flexmock(boto) boto.should_receive('connect_ec2').and_return(fake_ec2) # assume that root login is not enabled local_state.should_receive('shell').with_args(re.compile('ssh'), False, 1, stdin='ls').and_return(RemoteHelper.LOGIN_AS_UBUNTU_USER) # assume that we can enable root login local_state.should_receive('shell').with_args(re.compile('ssh'), False, 5, stdin=re.compile('sudo cp')).and_return() # and assume that we can copy over our ssh keys fine local_state.should_receive('shell').with_args(re.compile('scp .*[r|d]sa'), False, 5).and_return() local_state.should_receive('shell').with_args(re.compile('scp .*{0}' .format(self.keyname)), False, 5).and_return() # mock out seeing if the image is appscale-compatible, and assume it is # mock out our attempts to find /etc/appscale and presume it does exist local_state.should_receive('shell').with_args(re.compile('ssh'), False, 5, stdin=re.compile('/etc/appscale')).and_return() # mock out our attempts to find /etc/appscale/version and presume it does # exist local_state.should_receive('shell').with_args(re.compile('ssh'), False, 5, stdin=re.compile('/etc/appscale/{0}' .format(APPSCALE_VERSION))) # put in a mock indicating that the database the user wants is supported local_state.should_receive('shell').with_args(re.compile('ssh'), False, 5, stdin=re.compile('/etc/appscale/{0}/{1}' .format(APPSCALE_VERSION, 'cassandra'))) # mock out generating the private key local_state.should_receive('shell').with_args(re.compile('openssl'), False, stdin=None) # assume that we started god fine local_state.should_receive('shell').with_args(re.compile('ssh'), False, 5, stdin=re.compile('god &')) # and that we copied over the AppController's god file local_state.should_receive('shell').with_args(re.compile('scp'), False, 5, stdin=re.compile('appcontroller.god')) # also, that we started the AppController itself local_state.should_receive('shell').with_args(re.compile('ssh'), False, 5, stdin=re.compile('god load')) # 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) # assume that the AppController comes up on the third attempt fake_socket.should_receive('connect').with_args(('public1', AppControllerClient.PORT)).and_raise(Exception).and_raise(Exception) \ .and_return(None) # same for the UserAppServer fake_socket.should_receive('connect').with_args(('public1', UserAppClient.PORT)).and_raise(Exception).and_raise(Exception) \ .and_return(None) # as well as for the AppLoadBalancer fake_socket.should_receive('connect').with_args(('public1', RemoteHelper.APP_LOAD_BALANCER_PORT)).and_raise(Exception) \ .and_raise(Exception).and_return(None) flexmock(socket) socket.should_receive('socket').and_return(fake_socket) # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('set_parameters').with_args(list, list, ['none'], 'the secret').and_return('OK') fake_appcontroller.should_receive('get_all_public_ips').with_args('the secret') \ .and_return(json.dumps(['public1'])) role_info = [{ 'public_ip' : 'public1', 'private_ip' : 'private1', 'jobs' : ['shadow', 'login'] }] fake_appcontroller.should_receive('get_role_info').with_args('the secret') \ .and_return(json.dumps(role_info)) fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('nothing interesting here') \ .and_return('Database is at not-up-yet') \ .and_return('Database is at public1') fake_appcontroller.should_receive('is_done_initializing') \ .and_return(False) \ .and_return(True) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://*****:*****@a.com', str, 'xmpp_user', 'the secret') \ .and_return('true') fake_userappserver.should_receive('commit_new_user').with_args( 'a@public1', str, 'xmpp_user', 'the secret') \ .and_return('true') fake_userappserver.should_receive('set_cloud_admin_status').with_args( '*****@*****.**', 'true', 'the secret').and_return() fake_userappserver.should_receive('set_capabilities').with_args( '*****@*****.**', UserAppClient.ADMIN_CAPABILITIES, 'the secret').and_return() SOAPpy.should_receive('SOAPProxy').with_args('https://public1:4343') \ .and_return(fake_userappserver) argv = [ "--min", "1", "--max", "1", "--infrastructure", "ec2", "--machine", "ami-ABCDEFG", "--use_spot_instances", "--max_spot_price", "1.23", "--keyname", self.keyname, "--group", "bazgroup", "--test" ] options = ParseArgs(argv, self.function).args AppScaleTools.run_instances(options)
def test_appscale_in_one_node_virt_deployment(self): # let's say that appscale isn't already running local_state = flexmock(LocalState) local_state.should_receive('ensure_appscale_isnt_running').and_return() local_state.should_receive('make_appscale_directory').and_return() rh = flexmock(RemoteHelper) rh.should_receive('copy_deployment_credentials').and_return() # 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 builtins = flexmock(sys.modules['__builtin__']) builtins.should_call('open') # set the fall-through 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() builtins.should_receive('open').with_args(secret_key_location, 'r') \ .and_return(fake_secret) builtins.should_receive('open').with_args(secret_key_location, 'w') \ .and_return(fake_secret) # mock out copying over the keys local_state.should_receive('shell')\ .with_args(re.compile('^scp .*.key'),False,5) # mock out our attempts to find /etc/appscale and presume it does exist local_state.should_receive('shell')\ .with_args(re.compile('^ssh'),False,5,\ stdin=re.compile('ls /etc/appscale'))\ .and_return() # mock out our attempts to find /etc/appscale/version and presume it does # exist local_state.should_receive('shell')\ .with_args(re.compile('^ssh'),False,5,\ stdin=re.compile('ls /etc/appscale/{0}'.format(APPSCALE_VERSION)))\ .and_return() # finally, put in a mock indicating that the database the user wants # is supported local_state.should_receive('shell')\ .with_args(re.compile('^ssh'),False,5,\ stdin=re.compile('ls /etc/appscale/{0}/{1}'\ .format(APPSCALE_VERSION, 'cassandra')))\ .and_return() # mock out generating the private key local_state.should_receive('shell')\ .with_args(re.compile('^openssl'),False,stdin=None)\ .and_return() # mock out removing the old json file local_state = flexmock(LocalState) local_state.should_receive('shell')\ .with_args(re.compile('^ssh'),False,5,stdin=re.compile('rm -rf'))\ .and_return() # assume that we started god fine local_state.should_receive('shell')\ .with_args(re.compile('^ssh'),False,5,stdin=re.compile('god &'))\ .and_return() # and that we copied over the AppController's god file local_state.should_receive('shell')\ .with_args(re.compile('scp .*appcontroller\.god.*'),False,5)\ .and_return() # also, that we started the AppController itself local_state.should_receive('shell')\ .with_args(re.compile('^ssh'),False,5,\ stdin=re.compile('^god load .*appcontroller\.god'))\ .and_return() # assume that the AppController comes up on the third attempt fake_socket = flexmock(name='fake_socket') fake_socket.should_receive('connect').with_args(('1.2.3.4', AppControllerClient.PORT)).and_raise(Exception).and_raise(Exception) \ .and_return(None) # same for the UserAppServer fake_socket.should_receive('connect').with_args(('1.2.3.4', UserAppClient.PORT)).and_raise(Exception).and_raise(Exception) \ .and_return(None) # as well as for the AppLoadBalancer fake_socket.should_receive('connect').with_args(('1.2.3.4', RemoteHelper.APP_LOAD_BALANCER_PORT)).and_raise(Exception) \ .and_raise(Exception).and_return(None) flexmock(socket) socket.should_receive('socket').and_return(fake_socket) # mock out the SOAP call to the AppController and assume it succeeded fake_appcontroller = flexmock(name='fake_appcontroller') fake_appcontroller.should_receive('set_parameters').with_args(list, list, ['none'], 'the secret').and_return('OK') fake_appcontroller.should_receive('get_all_public_ips')\ .with_args('the secret') \ .and_return(json.dumps(['1.2.3.4'])) role_info = [{ 'public_ip' : '1.2.3.4', 'private_ip' : '1.2.3.4', 'jobs' : ['shadow', 'login'] }] fake_appcontroller.should_receive('get_role_info').with_args('the secret') \ .and_return(json.dumps(role_info)) fake_appcontroller.should_receive('status').with_args('the secret') \ .and_return('nothing interesting here') \ .and_return('Database is at not-up-yet') \ .and_return('Database is at 1.2.3.4') fake_appcontroller.should_receive('is_done_initializing') \ .and_return(False) \ .and_return(True) flexmock(SOAPpy) SOAPpy.should_receive('SOAPProxy').with_args('https://1.2.3.4:17443') \ .and_return(fake_appcontroller) # mock out reading the locations.json file, and slip in our own json local_state.should_receive('get_local_nodes_info').and_return(json.loads( json.dumps([{ "public_ip" : "1.2.3.4", "private_ip" : "1.2.3.4", "jobs" : ["shadow", "login"] }]))) # copying over the locations yaml and json files should be fine local_state.should_receive('shell')\ .with_args(re.compile('^scp .*/etc/appscale/locations-bookey.yaml'),\ False,5)\ .and_return() local_state.should_receive('shell')\ .with_args(re.compile('^scp .*/etc/appscale/locations-bookey.json'),\ False,5)\ .and_return() local_state.should_receive('shell')\ .with_args(re.compile('^scp .*/root/.appscale/locations-bookey.json'),\ False,5)\ .and_return() # same for the secret key local_state.should_receive('shell')\ .with_args(re.compile('^scp .*.secret'),False,5)\ .and_return() # mock out calls to the UserAppServer and presume that calls to create new # users succeed fake_userappserver = flexmock(name='fake_appcontroller') fake_userappserver.should_receive('commit_new_user').with_args( '*****@*****.**', str, 'xmpp_user', 'the secret') \ .and_return('true') fake_userappserver.should_receive('commit_new_user').with_args( '[email protected]', str, 'xmpp_user', 'the secret') \ .and_return('true') fake_userappserver.should_receive('set_cloud_admin_status').with_args( '*****@*****.**', 'true', 'the secret').and_return() fake_userappserver.should_receive('set_capabilities').with_args( '*****@*****.**', UserAppClient.ADMIN_CAPABILITIES, 'the secret').and_return() SOAPpy.should_receive('SOAPProxy').with_args('https://1.2.3.4:4343') \ .and_return(fake_userappserver) # don't use a 192.168.X.Y IP here, since sometimes we set our virtual # machines to boot with those addresses (and that can mess up our tests). ips_layout = yaml.safe_load(""" master : 1.2.3.4 database: 1.2.3.4 zookeeper: 1.2.3.4 appengine: 1.2.3.4 """) argv = [ "--ips_layout", base64.b64encode(yaml.dump(ips_layout)), "--keyname", self.keyname, "--test" ] options = ParseArgs(argv, self.function).args AppScaleTools.run_instances(options)