def test_start_server_removes_elasticsearch_data(self): check_function_calls = {'shutil_rmtree_is_called': False} old_os_path_exists = os.path.exists def mock_os_remove_files(file_path): # pylint: disable=unused-argument check_function_calls['shutil_rmtree_is_called'] = True def mock_os_path_exists(file_path): # pylint: disable=unused-argument if file_path == common.ES_PATH_DATA_DIR: return True return old_os_path_exists(file_path) self.exit_stack.enter_context(self.swap_popen()) self.exit_stack.enter_context( self.swap_to_always_return(subprocess, 'call', value=scripts_test_utils.PopenStub())) self.exit_stack.enter_context( self.swap(shutil, 'rmtree', mock_os_remove_files)) self.exit_stack.enter_context( self.swap(os.path, 'exists', mock_os_path_exists)) self.exit_stack.enter_context( self.swap_to_always_return(common, 'wait_for_port_to_be_in_use')) self.exit_stack.enter_context( servers.managed_elasticsearch_dev_server()) self.exit_stack.close() self.assertTrue(check_function_calls['shutil_rmtree_is_called'])
def main(args=None): """Runs lighthouse checks and deletes reports.""" # TODO(#11549): Move this to top of the file. import contextlib2 parsed_args = _PARSER.parse_args(args=args) if parsed_args.mode == LIGHTHOUSE_MODE_ACCESSIBILITY: lighthouse_mode = LIGHTHOUSE_MODE_ACCESSIBILITY server_mode = SERVER_MODE_DEV elif parsed_args.mode == LIGHTHOUSE_MODE_PERFORMANCE: lighthouse_mode = LIGHTHOUSE_MODE_PERFORMANCE server_mode = SERVER_MODE_PROD else: raise Exception('Invalid parameter passed in: \'%s\', please choose' 'from \'accessibility\' or \'performance\'' % parsed_args.mode) if lighthouse_mode == LIGHTHOUSE_MODE_PERFORMANCE: python_utils.PRINT('Building files in production mode.') # We are using --source_maps here, so that we have at least one CI check # that builds using source maps in prod env. This is to ensure that # there are no issues while deploying oppia. build.main(args=['--prod_env', '--source_maps']) elif lighthouse_mode == LIGHTHOUSE_MODE_ACCESSIBILITY: build.main(args=[]) run_webpack_compilation() with contextlib2.ExitStack() as stack: stack.enter_context( common.inplace_replace_file_context( common.CONSTANTS_FILE_PATH, '"ENABLE_ACCOUNT_DELETION": .*', '"ENABLE_ACCOUNT_DELETION": true,')) stack.enter_context(servers.managed_redis_server()) stack.enter_context(servers.managed_elasticsearch_dev_server()) if constants.EMULATOR_MODE: stack.enter_context(servers.managed_firebase_auth_emulator()) stack.enter_context( servers.managed_dev_appserver(APP_YAML_FILENAMES[server_mode], port=GOOGLE_APP_ENGINE_PORT, clear_datastore=True, log_level='critical', skip_sdk_update_check=True)) run_lighthouse_puppeteer_script() run_lighthouse_checks(lighthouse_mode)
def test_managed_elasticsearch_dev_server(self): popen_calls = self.exit_stack.enter_context(self.swap_popen()) self.exit_stack.enter_context(self.swap_to_always_return( common, 'wait_for_port_to_be_in_use')) self.exit_stack.enter_context( servers.managed_elasticsearch_dev_server()) self.exit_stack.close() self.assertEqual( popen_calls[0].program_args, '%s/bin/elasticsearch -q' % common.ES_PATH) self.assertEqual(popen_calls[0].kwargs, { 'shell': True, 'env': {'ES_PATH_CONF': common.ES_PATH_CONFIG_DIR}, })
def main(args=None): """Runs lighthouse checks and deletes reports.""" parsed_args = _PARSER.parse_args(args=args) if parsed_args.mode == LIGHTHOUSE_MODE_ACCESSIBILITY: lighthouse_mode = LIGHTHOUSE_MODE_ACCESSIBILITY server_mode = SERVER_MODE_DEV elif parsed_args.mode == LIGHTHOUSE_MODE_PERFORMANCE: lighthouse_mode = LIGHTHOUSE_MODE_PERFORMANCE server_mode = SERVER_MODE_PROD else: raise Exception('Invalid parameter passed in: \'%s\', please choose' 'from \'accessibility\' or \'performance\'' % parsed_args.mode) if lighthouse_mode == LIGHTHOUSE_MODE_PERFORMANCE: python_utils.PRINT('Building files in production mode.') build.main(args=['--prod_env']) elif lighthouse_mode == LIGHTHOUSE_MODE_ACCESSIBILITY: build.main(args=[]) run_webpack_compilation() with contextlib.ExitStack() as stack: stack.enter_context( common.inplace_replace_file_context( common.CONSTANTS_FILE_PATH, '"ENABLE_ACCOUNT_DELETION": .*', '"ENABLE_ACCOUNT_DELETION": true,')) stack.enter_context(servers.managed_redis_server()) stack.enter_context(servers.managed_elasticsearch_dev_server()) if constants.EMULATOR_MODE: stack.enter_context(servers.managed_firebase_auth_emulator()) stack.enter_context(servers.managed_cloud_datastore_emulator()) stack.enter_context( servers.managed_dev_appserver(APP_YAML_FILENAMES[server_mode], port=GOOGLE_APP_ENGINE_PORT, log_level='critical', skip_sdk_update_check=True)) run_lighthouse_puppeteer_script() run_lighthouse_checks(lighthouse_mode, parsed_args.shard)
def run_tests(args): """Run the scripts to start end-to-end tests.""" if is_oppia_server_already_running(): sys.exit(1) install_third_party_libraries(args.skip_install) with contextlib.ExitStack() as stack: dev_mode = not args.prod_env if args.skip_build: build.modify_constants(prod_env=args.prod_env) else: build_js_files(dev_mode, source_maps=args.source_maps) stack.callback(build.set_constants_to_default) stack.enter_context(servers.managed_redis_server()) stack.enter_context(servers.managed_elasticsearch_dev_server()) if constants.EMULATOR_MODE: stack.enter_context(servers.managed_firebase_auth_emulator()) stack.enter_context( servers.managed_cloud_datastore_emulator(clear_datastore=True)) app_yaml_path = 'app.yaml' if args.prod_env else 'app_dev.yaml' stack.enter_context( servers.managed_dev_appserver( app_yaml_path, port=GOOGLE_APP_ENGINE_PORT, log_level=args.server_log_level, # Automatic restart can be disabled since we don't expect code # changes to happen while the e2e tests are running. automatic_restart=False, skip_sdk_update_check=True, env={ **os.environ, 'PORTSERVER_ADDRESS': common.PORTSERVER_SOCKET_FILEPATH, })) stack.enter_context( servers.managed_webdriver_server( chrome_version=args.chrome_driver_version)) proc = stack.enter_context( servers.managed_protractor_server( suite_name=args.suite, dev_mode=dev_mode, debug_mode=args.debug_mode, sharding_instances=args.sharding_instances, stdout=subprocess.PIPE)) print('Servers have come up.\n' 'Note: If ADD_SCREENSHOT_REPORTER is set to true in ' 'core/tests/protractor.conf.js, you can view screenshots of the ' 'failed tests in ../protractor-screenshots/') output_lines = [] while True: # Keep reading lines until an empty string is returned. Empty # strings signal that the process has ended. for line in iter(proc.stdout.readline, b''): if isinstance(line, str): # Although our unit tests always provide unicode strings, # the actual server needs this failsafe since it can output # non-unicode strings. line = line.encode('utf-8') # pragma: no cover output_lines.append(line.rstrip()) # Replaces non-ASCII characters with '?'. common.write_stdout_safe(line.decode('ascii', errors='replace')) # The poll() method returns None while the process is running, # otherwise it returns the return code of the process (an int). if proc.poll() is not None: break return output_lines, proc.returncode