def test_wait_for_app(self): port = 20000 ip = '127.0.0.1' testing.disable_logging() fake_opener = flexmock( open=lambda url, timeout: flexmock(code=200, headers=flexmock(headers=[]))) flexmock(urllib2).should_receive('build_opener').and_return(fake_opener) flexmock(appscale_info).should_receive('get_private_ip').and_return(ip) instance_manager = InstanceManager( None, None, None, None, None, None, None, None, None) instance_manager._private_ip = ip instance_started = yield instance_manager._wait_for_app(port) self.assertEqual(True, instance_started) current_time = time.time() flexmock(time).should_receive('time').and_return(current_time).\ and_return(current_time + START_APP_TIMEOUT + 1) response = Future() response.set_result(None) flexmock(gen).should_receive('sleep').and_return(response) fake_opener.should_receive('open').and_raise(IOError) instance_started = yield instance_manager._wait_for_app(port) self.assertEqual(False, instance_started)
def test_start_app_failed_copy_java(self): version_details = {'runtime': 'java', 'revision': 1, 'deployment': {'zip': {'sourceUrl': 'source.tar.gz'}}} version_manager = flexmock(version_details=version_details, project_id='test', revision_key='test_default_v1_1', version_key='test_default_v1') instance_manager = InstanceManager( None, None, None, None, None, None, None, None, None) instance_manager._projects_manager = { 'test': {'default': {'v1': version_manager}}} instance_manager._deployment_config = flexmock( get_config=lambda x: {'default_max_appserver_memory': 400}) source_manager = flexmock() response = Future() response.set_result(None) source_manager.should_receive('ensure_source').\ with_args('test_default_v1_1', 'source.tar.gz', 'java').\ and_return(response) instance_manager._source_manager = source_manager flexmock(utils).should_receive('find_web_inf').\ and_return('/path/to/dir/WEB-INF') flexmock(monit_app_configuration).should_receive('create_config_file').\ and_raise(IOError) with self.assertRaises(IOError): yield instance_manager._start_instance(version_manager, 20000)
def test_wait_for_app(self): port = 20000 ip = '127.0.0.1' testing.disable_logging() fake_opener = flexmock(open=lambda url, timeout: flexmock( code=200, headers=flexmock(headers=[]))) flexmock(urllib2).should_receive('build_opener').and_return( fake_opener) flexmock(appscale_info).should_receive('get_private_ip').and_return(ip) instance_manager = InstanceManager(None, None, None, None, None, None, None, None, None) instance_manager._private_ip = ip instance_started = yield instance_manager._wait_for_app(port) self.assertEqual(True, instance_started) current_time = time.time() flexmock(monotonic).should_receive('monotonic').and_return(current_time).\ and_return(current_time + START_APP_TIMEOUT + 1) response = Future() response.set_result(None) flexmock(gen).should_receive('sleep').and_return(response) fake_opener.should_receive('open').and_raise(IOError) instance_started = yield instance_manager._wait_for_app(port) self.assertEqual(False, instance_started)
def main(): file_io.set_logging_format() logging.getLogger().setLevel(logging.INFO) zk_ips = appscale_info.get_zk_node_ips() zk_client = KazooClient(hosts=','.join(zk_ips)) zk_client.start() deployment_config = DeploymentConfig(zk_client) projects_manager = GlobalProjectsManager(zk_client) thread_pool = ThreadPoolExecutor(MAX_BACKGROUND_WORKERS) source_manager = SourceManager(zk_client, thread_pool) source_manager.configure_automatic_fetch(projects_manager) monit_operator = MonitOperator() options.define('private_ip', appscale_info.get_private_ip()) options.define('syslog_server', appscale_info.get_headnode_ip()) options.define('db_proxy', appscale_info.get_db_proxy()) options.define('tq_proxy', appscale_info.get_tq_proxy()) options.define('secret', appscale_info.get_secret()) routing_client = RoutingClient(zk_client, options.private_ip, options.secret) instance_manager = InstanceManager(zk_client, monit_operator, routing_client, projects_manager, deployment_config, source_manager, options.syslog_server, thread_pool, options.private_ip) instance_manager.start() logger.info('Starting AppManager') io_loop = IOLoop.current() io_loop.run_sync(instance_manager.populate_api_servers) io_loop.start()
def test_stop_app_instance(self): version_key = 'test_default_v1' port = 20000 flexmock(misc).should_receive('is_app_name_valid').and_return(False) instance_manager = InstanceManager(None, None, None, None, None, None, None, None, None) flexmock(misc).should_receive('is_app_name_valid').and_return(True) response = Future() response.set_result(None) instance_manager._routing_client = flexmock( unregister_instance=lambda instance: response) flexmock(ServiceOperator).should_receive('stop_async').\ with_args('appscale-instance-run@test_default_v1-20000').\ and_return(response) response = Future() response.set_result(None) flexmock(instance_manager).should_receive('_clean_old_sources').\ and_return(response) instance_manager._service_operator = flexmock( stop_async=lambda service: response) yield instance_manager._stop_app_instance( instance.Instance('_'.join([version_key, 'revid']), port))
def main(): file_io.set_logging_format() logging.getLogger().setLevel(logging.INFO) zk_ips = appscale_info.get_zk_node_ips() zk_client = KazooClient(hosts=','.join(zk_ips)) zk_client.start() deployment_config = DeploymentConfig(zk_client) projects_manager = GlobalProjectsManager(zk_client) thread_pool = ThreadPoolExecutor(MAX_BACKGROUND_WORKERS) source_manager = SourceManager(zk_client, thread_pool) source_manager.configure_automatic_fetch(projects_manager) monit_operator = MonitOperator() options.define('private_ip', appscale_info.get_private_ip()) options.define('syslog_server', appscale_info.get_headnode_ip()) options.define('db_proxy', appscale_info.get_db_proxy()) options.define('load_balancer_ip', appscale_info.get_load_balancer_ips()[0]) options.define('tq_proxy', appscale_info.get_tq_proxy()) options.define('secret', appscale_info.get_secret()) routing_client = RoutingClient(zk_client, options.private_ip, options.secret) instance_manager = InstanceManager( zk_client, monit_operator, routing_client, projects_manager, deployment_config, source_manager, options.syslog_server, thread_pool, options.private_ip) instance_manager.start() logger.info('Starting AppManager') io_loop = IOLoop.current() io_loop.run_sync(instance_manager.populate_api_servers) io_loop.start()
def test_stop_app_instance(self): version_key = 'test_default_v1' port = 20000 flexmock(misc).should_receive('is_app_name_valid').and_return(False) instance_manager = InstanceManager( None, None, None, None, None, None, None, None, None) flexmock(misc).should_receive('is_app_name_valid').and_return(True) response = Future() response.set_result(None) instance_manager._routing_client = flexmock( unregister_instance=lambda instance: response) flexmock(MonitOperator).should_receive('send_command_sync').\ with_args('app___test_default_v1-20000', 'unmonitor').\ and_raise(HTTPError) unmonitor_future = Future() unmonitor_future.set_exception(HTTPError(500)) flexmock(instance_manager).should_receive('_unmonitor_and_terminate').\ and_return(unmonitor_future) entries_response = Future() entries_response.set_result(['app___test_default_v1_revid-20000']) instance_manager._monit_operator = flexmock( get_entries=lambda: entries_response, reload=lambda x: response) with self.assertRaises(HTTPError): yield instance_manager._stop_app_instance( instance.Instance('_'.join([version_key, 'revid']), port)) flexmock(MonitOperator).should_receive('send_command_sync').\ with_args('app___test_default_v1-20000', 'unmonitor') flexmock(os).should_receive('remove') flexmock(monit_interface).should_receive('safe_monit_run') response = Future() response.set_result(None) flexmock(MonitOperator).should_receive('reload').\ and_return(response) response = Future() response.set_result(None) flexmock(instance_manager).should_receive('_clean_old_sources').\ and_return(response) response = Future() response.set_result(None) flexmock(instance_manager).should_receive('_unmonitor_and_terminate').\ and_return(response) yield instance_manager._stop_app_instance( instance.Instance('_'.join([version_key, 'revid']), port))
def test_start_app_failed_copy_java(self): version_details = { 'runtime': 'java', 'revision': 1, 'deployment': { 'zip': { 'sourceUrl': 'source.tar.gz' } }, 'appscaleExtensions': { 'httpPort': '8080' } } version_manager = flexmock(version_details=version_details, project_id='test', revision_key='test_default_v1_1', version_key='test_default_v1') instance_manager = InstanceManager(None, None, None, None, None, None, None, None, None) instance_manager._login_server = '192.168.33.10' instance_manager._projects_manager = { 'test': { 'default': { 'v1': version_manager } } } instance_manager._deployment_config = flexmock( get_config=lambda x: {'default_max_appserver_memory': 400}) source_manager = flexmock() response = Future() response.set_result(None) source_manager.should_receive('ensure_source').\ with_args('test_default_v1_1', 'source.tar.gz', 'java').\ and_return(response) instance_manager._source_manager = source_manager flexmock(instance).should_receive('find_web_inf'). \ and_return('/path/to/dir/WEB-INF') response = Future() response.set_result((19999, [])) flexmock(instance_manager).should_receive('_ensure_api_server'). \ and_return(response) flexmock(file_io).should_receive('write').and_raise(IOError) with self.assertRaises(IOError): yield instance_manager._start_instance(version_manager, 20000)
def test_start_app_goodconfig_python(self): testing.disable_logging() version_details = { 'runtime': 'python27', 'revision': 1, 'deployment': { 'zip': { 'sourceUrl': 'source.tar.gz' } } } version_manager = flexmock(version_details=version_details, project_id='test', revision_key='test_default_v1_1', version_key='test_default_v1') projects_manager = {'test': {'default': {'v1': version_manager}}} deployment_config = flexmock( get_config=lambda x: {'default_max_appserver_memory': 400}) source_manager = flexmock() response = Future() response.set_result(None) source_manager.should_receive('ensure_source').\ with_args('test_default_v1_1', 'source.tar.gz', 'python27').\ and_return(response) instance_manager = InstanceManager(None, None, None, projects_manager, deployment_config, source_manager, None, None, None) instance_manager._login_server = '192.168.33.10' flexmock(monit_app_configuration).should_receive('create_config_file').\ and_return('fakeconfig') response = Future() response.set_result(None) flexmock(instance_manager).should_receive('_ensure_api_server').\ and_return(response) response = Future() response.set_result(None) flexmock(MonitOperator).should_receive('reload').\ and_return(response) response = Future() response.set_result(None) flexmock(MonitOperator).should_receive('send_command_retry_process').\ with_args('app___test_default_v1_1-20000', 'start').\ and_return(response) response = Future() response.set_result(None) flexmock(instance_manager).should_receive('_add_routing').\ and_return(response) flexmock(instance_manager).should_receive('_wait_for_app').\ and_return(True) flexmock(os).should_receive('popen').\ and_return(flexmock(read=lambda: '12345\n')) flexmock(file_io).should_receive('write').\ and_return() flexmock(utils).should_receive("setup_logrotate").and_return() instance_manager._zk_client = flexmock() instance_manager._zk_client.should_receive('ensure_path') instance_manager._monit_operator = flexmock( reload=lambda x: response, send_command_retry_process=lambda watch, cmd: response) yield instance_manager._start_instance(version_manager, 20000)
def test_start_app_goodconfig_java(self): testing.disable_logging() version_details = { 'runtime': 'java', 'revision': 1, 'deployment': { 'zip': { 'sourceUrl': 'source.tar.gz' } } } version_manager = flexmock(version_details=version_details, project_id='test', revision_key='test_default_v1_1', version_key='test_default_v1') instance_manager = InstanceManager(None, None, None, None, None, None, None, None, None) instance_manager._projects_manager = { 'test': { 'default': { 'v1': version_manager } } } instance_manager._deployment_config = flexmock( get_config=lambda x: {'default_max_appserver_memory': 400}) source_manager = flexmock() response = Future() response.set_result(None) source_manager.should_receive('ensure_source').\ with_args('test_default_v1_1', 'source.tar.gz', 'java').\ and_return(response) instance_manager._source_manager = source_manager start_cmd = ( '/root/appscale/AppServer_Java/appengine-java-repacked/bin/' 'dev_appserver.sh --port 20000') flexmock(instance).should_receive('create_java_start_cmd').\ and_return(start_cmd) flexmock(monit_app_configuration).should_receive('create_config_file').\ and_return('fakeconfig') response = Future() response.set_result(None) flexmock(instance_manager).should_receive('_ensure_api_server').\ and_return(response) response = Future() response.set_result(None) flexmock(MonitOperator).should_receive('reload').\ and_return(response) response = Future() response.set_result(None) flexmock(MonitOperator).should_receive('send_command_retry_process').\ with_args('app___test_default_v1_1-20000', 'start').\ and_return(response) response = Future() response.set_result(None) flexmock(instance_manager).should_receive('_add_routing').\ and_return(response) flexmock(instance).should_receive('create_java_app_env').\ and_return({}) flexmock(instance_manager_module).should_receive('create_java_start_cmd').\ and_return('/root/appscale/AppServer_Java/appengine-java-repacked/bin/' 'dev_appserver.sh --port 20000') flexmock(instance_manager).should_receive('_wait_for_app').\ and_return(True) flexmock(os).should_receive('popen').\ and_return(flexmock(read=lambda: '0\n')) flexmock(file_io).should_receive('write').and_return() flexmock(subprocess).should_receive('call').and_return(0) flexmock(utils).should_receive("setup_logrotate").and_return() flexmock(os).should_receive('listdir').and_return([]) instance_manager._zk_client = flexmock() instance_manager._zk_client.should_receive('ensure_path') response = Future() response.set_result(None) instance_manager._monit_operator = flexmock( reload=lambda x: response, send_command_retry_process=lambda watch, cmd: response) yield instance_manager._start_instance(version_manager, 20000)
def test_start_app_goodconfig_python(self): testing.disable_logging() version_details = {'runtime': 'python27', 'revision': 1, 'deployment': {'zip': {'sourceUrl': 'source.tar.gz'}}} version_manager = flexmock(version_details=version_details, project_id='test', revision_key='test_default_v1_1', version_key='test_default_v1') projects_manager = { 'test': {'default': {'v1': version_manager}}} deployment_config = flexmock( get_config=lambda x: {'default_max_appserver_memory': 400}) source_manager = flexmock() response = Future() response.set_result(None) source_manager.should_receive('ensure_source').\ with_args('test_default_v1_1', 'source.tar.gz', 'python27').\ and_return(response) instance_manager = InstanceManager( None, None, None, projects_manager, deployment_config, source_manager, None, None, None) instance_manager._login_server = '192.168.33.10' flexmock(monit_app_configuration).should_receive('create_config_file').\ and_return('fakeconfig') response = Future() response.set_result(None) flexmock(instance_manager).should_receive('_ensure_api_server').\ and_return(response) response = Future() response.set_result(None) flexmock(MonitOperator).should_receive('reload').\ and_return(response) response = Future() response.set_result(None) flexmock(MonitOperator).should_receive('send_command_retry_process').\ with_args('app___test_default_v1_1-20000', 'start').\ and_return(response) response = Future() response.set_result(None) flexmock(instance_manager).should_receive('_add_routing').\ and_return(response) flexmock(instance_manager).should_receive('_wait_for_app').\ and_return(True) flexmock(os).should_receive('popen').\ and_return(flexmock(read=lambda: '12345\n')) flexmock(file_io).should_receive('write').\ and_return() flexmock(utils).should_receive("setup_logrotate").and_return() instance_manager._zk_client = flexmock() instance_manager._zk_client.should_receive('ensure_path') instance_manager._monit_operator = flexmock( reload=lambda x: response, send_command_retry_process=lambda watch, cmd: response) yield instance_manager._start_instance(version_manager, 20000)
def test_start_app_goodconfig_java(self): testing.disable_logging() version_details = {'runtime': 'java', 'revision': 1, 'deployment': {'zip': {'sourceUrl': 'source.tar.gz'}}} version_manager = flexmock(version_details=version_details, project_id='test', revision_key='test_default_v1_1', version_key='test_default_v1') instance_manager = InstanceManager( None, None, None, None, None, None, None, None, None) instance_manager._projects_manager = { 'test': {'default': {'v1': version_manager}}} instance_manager._deployment_config = flexmock( get_config=lambda x: {'default_max_appserver_memory': 400}) source_manager = flexmock() response = Future() response.set_result(None) source_manager.should_receive('ensure_source').\ with_args('test_default_v1_1', 'source.tar.gz', 'java').\ and_return(response) instance_manager._source_manager = source_manager start_cmd = ('/root/appscale/AppServer_Java/appengine-java-repacked/bin/' 'dev_appserver.sh --port 20000') flexmock(instance).should_receive('create_java_start_cmd').\ and_return(start_cmd) flexmock(monit_app_configuration).should_receive('create_config_file').\ and_return('fakeconfig') response = Future() response.set_result(None) flexmock(instance_manager).should_receive('_ensure_api_server').\ and_return(response) response = Future() response.set_result(None) flexmock(MonitOperator).should_receive('reload').\ and_return(response) response = Future() response.set_result(None) flexmock(MonitOperator).should_receive('send_command_retry_process').\ with_args('app___test_default_v1_1-20000', 'start').\ and_return(response) response = Future() response.set_result(None) flexmock(instance_manager).should_receive('_add_routing').\ and_return(response) flexmock(instance).should_receive('create_java_app_env').\ and_return({}) flexmock(instance_manager_module).should_receive('create_java_start_cmd').\ and_return('/root/appscale/AppServer_Java/appengine-java-repacked/bin/' 'dev_appserver.sh --port 20000') flexmock(instance_manager).should_receive('_wait_for_app').\ and_return(True) flexmock(os).should_receive('popen').\ and_return(flexmock(read=lambda: '0\n')) flexmock(file_io).should_receive('write').and_return() flexmock(subprocess).should_receive('call').and_return(0) flexmock(utils).should_receive("setup_logrotate").and_return() flexmock(os).should_receive('listdir').and_return([]) instance_manager._zk_client = flexmock() instance_manager._zk_client.should_receive('ensure_path') response = Future() response.set_result(None) instance_manager._monit_operator = flexmock( reload=lambda x: response, send_command_retry_process=lambda watch, cmd: response) yield instance_manager._start_instance(version_manager, 20000)
def test_start_app_goodconfig_python(self): testing.disable_logging() version_details = { 'runtime': 'python27', 'revision': 1, 'deployment': { 'zip': { 'sourceUrl': 'source.tar.gz' } }, 'appscaleExtensions': { 'httpPort': '8080' } } version_manager = flexmock(version_details=version_details, project_id='test', revision_key='test_default_v1_1', version_key='test_default_v1') projects_manager = {'test': {'default': {'v1': version_manager}}} deployment_config = flexmock( get_config=lambda x: {'default_max_appserver_memory': 400}) source_manager = flexmock() response = Future() response.set_result(None) source_manager.should_receive('ensure_source').\ with_args('test_default_v1_1', 'source.tar.gz', 'python27').\ and_return(response) instance_manager = InstanceManager(None, None, None, projects_manager, deployment_config, source_manager, None, None, None) instance_manager._login_server = '192.168.33.10' response = Future() response.set_result((19999, [])) flexmock(instance_manager).should_receive('_ensure_api_server').\ and_return(response) flexmock(file_io).should_receive('write').and_return() response = Future() response.set_result(None) flexmock(ServiceOperator).should_receive('start_async').\ and_return(response) response = Future() response.set_result(None) flexmock(instance_manager).should_receive('_add_routing').\ and_return(response) flexmock(instance_manager).should_receive('_wait_for_app').\ and_return(True) flexmock(os).should_receive('popen').\ and_return(flexmock(read=lambda: '12345\n')) flexmock(file_io).should_receive('write').\ and_return() flexmock(utils).should_receive("setup_logrotate").and_return() instance_manager._zk_client = flexmock() instance_manager._zk_client.should_receive('ensure_path') instance_manager._service_operator = flexmock( start_async=lambda service, wants, properties: response) yield instance_manager._start_instance(version_manager, 20000)