Example #1
0
    def test_managed_redis_server_deletes_redis_dump_when_it_exists(self):
        original_os_remove = os.remove
        original_os_path_exists = os.path.exists

        @test_utils.CallCounter
        def mock_os_remove(path):
            if path == common.REDIS_DUMP_PATH:
                return
            original_os_remove(path)

        def mock_os_path_exists(path):
            if path == common.REDIS_DUMP_PATH:
                return True
            original_os_path_exists(path)

        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(
            self.swap_with_checks(os.path, 'exists', mock_os_path_exists))
        self.exit_stack.enter_context(
            self.swap_with_checks(os, 'remove', mock_os_remove))

        self.exit_stack.enter_context(servers.managed_redis_server())
        self.exit_stack.close()

        self.assertEqual(len(popen_calls), 1)
        self.assertEqual(
            popen_calls[0].program_args,
            '%s %s' % (common.REDIS_SERVER_PATH, common.REDIS_CONF_PATH))
        self.assertEqual(popen_calls[0].kwargs, {'shell': True})
        self.assertEqual(mock_os_remove.times_called, 1)
Example #2
0
    def test_managed_redis_server_throws_exception_when_on_windows_os(self):
        self.exit_stack.enter_context(self.swap_popen())
        self.exit_stack.enter_context(
            self.swap_to_always_return(common, 'is_windows_os', value=True))
        self.exit_stack.enter_context(
            self.swap_to_always_return(common, 'wait_for_port_to_be_in_use'))

        self.assertRaisesRegex(
            Exception,
            'The redis command line interface is not installed because '
            'your machine is on the Windows operating system. The redis '
            'server cannot start.', lambda: self.exit_stack.enter_context(
                servers.managed_redis_server()))
Example #3
0
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)
Example #4
0
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)
Example #5
0
    def test_managed_redis_server(self):
        original_os_remove = os.remove
        original_os_path_exists = os.path.exists

        @test_utils.CallCounter
        def mock_os_remove(path):
            if path == common.REDIS_DUMP_PATH:
                return
            original_os_remove(path)

        def mock_os_path_exists(path):
            if path == common.REDIS_DUMP_PATH:
                return
            original_os_path_exists(path)

        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(
            self.swap_with_checks(os.path, 'exists', mock_os_path_exists))
        self.exit_stack.enter_context(
            self.swap_with_checks(
                subprocess,
                'check_call',
                lambda _: 0,
                expected_args=[([common.REDIS_CLI_PATH, 'shutdown',
                                 'nosave'], )]))
        self.exit_stack.enter_context(
            self.swap_with_checks(os, 'remove', mock_os_remove, called=False))

        self.exit_stack.enter_context(servers.managed_redis_server())

        self.assertEqual(len(popen_calls), 1)
        self.assertEqual(
            popen_calls[0].program_args,
            '%s %s' % (common.REDIS_SERVER_PATH, common.REDIS_CONF_PATH))
        self.assertEqual(popen_calls[0].kwargs, {'shell': True})

        self.exit_stack.close()
Example #6
0
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
 def setUpClass(cls):
     super(RedisCacheServicesUnitTests, cls).setUpClass()
     cls._managed_redis_server = servers.managed_redis_server()
     cls._managed_redis_server.__enter__()