Ejemplo n.º 1
0
    def execute(self, state, args):
        self.validate_args(args)
        state.run_config.merge_device_config(state.plugin_cache)
        if args.device:
            device = args.device
            device_config = {}
        else:
            device = state.run_config.device
            device_config = state.run_config.device_config or {}

        if args.output:
            outdir = os.path.basename(args.output)
        else:
            outdir = os.getcwd()

        self.tm = TargetManager(device, device_config, outdir)
        self.tm.initialize()
        self.target = self.tm.target
        self.revent_recorder = ReventRecorder(self.target)
        self.revent_recorder.deploy()

        if args.workload:
            self.workload_record(args)
        elif args.package:
            self.package_record(args)
        else:
            self.manual_record(args)

        self.revent_recorder.remove()
Ejemplo n.º 2
0
    def execute(self, state, args):
        state.run_config.merge_device_config(state.plugin_cache)
        if args.device:
            device = args.device
            device_config = {}
        else:
            device = state.run_config.device
            device_config = state.run_config.device_config or {}

        target_manager = TargetManager(device, device_config, None)
        target_manager.initialize()
        self.target = target_manager.target
        revent_file = self.target.path.join(self.target.working_directory,
                                            os.path.split(args.recording)[1])

        self.logger.info("Pushing file to target")
        self.target.push(args.recording, self.target.working_directory)

        revent_recorder = ReventRecorder(target_manager.target)
        revent_recorder.deploy()

        if args.clear:
            self.target.execute('pm clear {}'.format(args.package))

        if args.package:
            self.logger.info('Starting {}'.format(args.package))
            cmd = 'monkey -p {} -c android.intent.category.LAUNCHER 1'
            self.target.execute(cmd.format(args.package))

        self.logger.info("Starting replay")
        revent_recorder.replay(revent_file)
        self.logger.info("Finished replay")
        revent_recorder.remove()
Ejemplo n.º 3
0
    def execute(self, state, args):
        state.run_config.merge_device_config(state.plugin_cache)
        if args.device:
            device = args.device
            device_config = {}
        else:
            device = state.run_config.device
            device_config = state.run_config.device_config or {}

        target_manager = TargetManager(device, device_config, None)
        target_manager.initialize()
        self.target = target_manager.target
        revent_file = self.target.path.join(self.target.working_directory,
                                            os.path.split(args.recording)[1])

        self.logger.info("Pushing file to target")
        self.target.push(args.recording, self.target.working_directory)

        revent_recorder = ReventRecorder(target_manager.target)
        revent_recorder.deploy()

        if args.clear:
            self.target.execute('pm clear {}'.format(args.package))

        if args.package:
            self.logger.info('Starting {}'.format(args.package))
            cmd = 'monkey -p {} -c android.intent.category.LAUNCHER 1'
            self.target.execute(cmd.format(args.package))

        self.logger.info("Starting replay")
        revent_recorder.replay(revent_file)
        self.logger.info("Finished replay")
        revent_recorder.remove()
Ejemplo n.º 4
0
    def execute(self, state, args):
        self.validate_args(args)
        state.run_config.merge_device_config(state.plugin_cache)
        if args.device:
            device = args.device
            device_config = {}
        else:
            device = state.run_config.device
            device_config = state.run_config.device_config or {}

        if args.output:
            outdir = os.path.basename(args.output)
        else:
            outdir = os.getcwd()

        self.tm = TargetManager(device, device_config, outdir)
        self.tm.initialize()
        self.target = self.tm.target
        self.revent_recorder = ReventRecorder(self.target)
        self.revent_recorder.deploy()

        if args.workload:
            self.workload_record(args)
        elif args.package:
            self.package_record(args)
        else:
            self.manual_record(args)

        self.revent_recorder.remove()
Ejemplo n.º 5
0
    def execute(self, config_manager, output):
        """
        Execute the run specified by an agenda. Optionally, selectors may be
        used to only execute a subset of the specified agenda.

        Params::

            :state: a ``ConfigManager`` containing processed configuration
            :output: an initialized ``RunOutput`` that will be used to
                     store the results.

        """
        signal.connect(self._error_signalled_callback, signal.ERROR_LOGGED)
        signal.connect(self._warning_signalled_callback, signal.WARNING_LOGGED)

        self.logger.info('Initializing run')
        self.logger.debug('Finalizing run configuration.')
        config = config_manager.finalize()
        output.write_config(config)

        self.target_manager = TargetManager(config.run_config.device,
                                            config.run_config.device_config,
                                            output.basepath)

        self.logger.info('Initializing execution context')
        context = ExecutionContext(config_manager, self.target_manager, output)

        try:
            self.do_execute(context)
        except KeyboardInterrupt as e:
            context.run_output.status = Status.ABORTED
            log.log_error(e, self.logger)
            context.write_output()
            raise
        except Exception as e:
            context.run_output.status = Status.FAILED
            log.log_error(e, self.logger)
            context.write_output()
            raise
        finally:
            context.finalize()
            self.execute_postamble(context, output)
            signal.send(signal.RUN_COMPLETED, self, context)
Ejemplo n.º 6
0
    def execute(self, config_manager, output):
        """
        Execute the run specified by an agenda. Optionally, selectors may be
        used to only selecute a subset of the specified agenda.

        Params::

            :state: a ``ConfigManager`` containing processed configuration
            :output: an initialized ``RunOutput`` that will be used to
                     store the results.

        """
        signal.connect(self._error_signalled_callback, signal.ERROR_LOGGED)
        signal.connect(self._warning_signalled_callback, signal.WARNING_LOGGED)

        self.logger.info('Initializing run')
        self.logger.debug('Finalizing run configuration.')
        config = config_manager.finalize()
        output.write_config(config)

        self.target_manager = TargetManager(config.run_config.device,
                                            config.run_config.device_config,
                                            output.basepath)

        self.logger.info('Initializing execution context')
        context = ExecutionContext(config_manager, self.target_manager, output)

        try:
            self.do_execute(context)
        except KeyboardInterrupt as e:
            context.run_output.status = 'ABORTED'
            log.log_error(e, self.logger)
            context.write_output()
            raise
        except Exception as e:
            context.run_output.status = 'FAILED'
            log.log_error(e, self.logger)
            context.write_output()
            raise
        finally:
            context.finalize()
            self.execute_postamble(context, output)
            signal.send(signal.RUN_COMPLETED, self, context)
Ejemplo n.º 7
0
class Executor(object):
    """
    The ``Executor``'s job is to set up the execution context and pass to a
    ``Runner`` along with a loaded run specification. Once the ``Runner`` has
    done its thing, the ``Executor`` performs some final reporting before
    returning.

    The initial context set up involves combining configuration from various
    sources, loading of required workloads, loading and installation of
    instruments and output processors, etc. Static validation of the combined
    configuration is also performed.

    """

    # pylint: disable=R0915

    def __init__(self):
        self.logger = logging.getLogger('executor')
        self.error_logged = False
        self.warning_logged = False
        self.target_manager = None

    def execute(self, config_manager, output):
        """
        Execute the run specified by an agenda. Optionally, selectors may be
        used to only execute a subset of the specified agenda.

        Params::

            :state: a ``ConfigManager`` containing processed configuration
            :output: an initialized ``RunOutput`` that will be used to
                     store the results.

        """
        signal.connect(self._error_signalled_callback, signal.ERROR_LOGGED)
        signal.connect(self._warning_signalled_callback, signal.WARNING_LOGGED)

        self.logger.info('Initializing run')
        self.logger.debug('Finalizing run configuration.')
        config = config_manager.finalize()
        output.write_config(config)

        self.target_manager = TargetManager(config.run_config.device,
                                            config.run_config.device_config,
                                            output.basepath)

        self.logger.info('Initializing execution context')
        context = ExecutionContext(config_manager, self.target_manager, output)

        try:
            self.do_execute(context)
        except KeyboardInterrupt as e:
            context.run_output.status = Status.ABORTED
            log.log_error(e, self.logger)
            context.write_output()
            raise
        except Exception as e:
            context.run_output.status = Status.FAILED
            log.log_error(e, self.logger)
            context.write_output()
            raise
        finally:
            context.finalize()
            self.execute_postamble(context, output)
            signal.send(signal.RUN_COMPLETED, self, context)

    def do_execute(self, context):
        self.logger.info('Connecting to target')
        context.tm.initialize()

        if context.cm.run_config.reboot_policy.perform_initial_reboot:
            self.logger.info('Performing initial reboot.')
            attempts = context.cm.run_config.max_retries
            while attempts:
                try:
                    self.target_manager.reboot(context)
                except TargetError as e:
                    if attempts:
                        attempts -= 1
                    else:
                        raise e
                else:
                    break

        context.output.set_target_info(self.target_manager.get_target_info())

        self.logger.info('Generating jobs')
        context.cm.generate_jobs(context)
        context.write_job_specs()
        context.output.write_state()

        self.logger.info('Installing instruments')
        for instrument in context.cm.get_instruments(
                self.target_manager.target):
            instrumentation.install(instrument, context)
        instrumentation.validate()

        self.logger.info('Installing output processors')
        pm = ProcessorManager()
        for proc in context.cm.get_processors():
            pm.install(proc, context)
        pm.validate()

        context.write_config()

        self.logger.info('Starting run')
        runner = Runner(context, pm)
        signal.send(signal.RUN_STARTED, self, context)
        runner.run()

    def execute_postamble(self, context, output):
        self.logger.info('Done.')
        duration = format_duration(output.info.duration)
        self.logger.info('Run duration: {}'.format(duration))
        num_ran = context.run_state.num_completed_jobs
        status_summary = 'Ran a total of {} iterations: '.format(num_ran)

        counter = context.run_state.get_status_counts()
        parts = []
        for status in reversed(Status.levels):
            if status in counter:
                parts.append('{} {}'.format(counter[status], status))
        self.logger.info('{}{}'.format(status_summary, ', '.join(parts)))

        self.logger.info('Results can be found in {}'.format(output.basepath))

        if self.error_logged:
            self.logger.warning('There were errors during execution.')
            self.logger.warning('Please see {}'.format(output.logfile))
        elif self.warning_logged:
            self.logger.warning('There were warnings during execution.')
            self.logger.warning('Please see {}'.format(output.logfile))

    def _error_signalled_callback(self, _):
        self.error_logged = True
        signal.disconnect(self._error_signalled_callback, signal.ERROR_LOGGED)

    def _warning_signalled_callback(self, _):
        self.warning_logged = True
        signal.disconnect(self._warning_signalled_callback,
                          signal.WARNING_LOGGED)

    def __str__(self):
        return 'executor'

    __repr__ = __str__
Ejemplo n.º 8
0
class RecordCommand(Command):

    name = 'record'
    description = '''
    Performs a revent recording

    This command helps making revent recordings. It will automatically
    deploy revent and has options to automatically open apps and record
    specified stages of a workload.

    Revent allows you to record raw inputs such as screen swipes or button presses.
    This can be useful for recording inputs for workloads such as games that don't
    have XML UI layouts that can be used with UIAutomator. As a drawback from this,
    revent recordings are specific to the device type they were recorded on.

    WA uses two parts to the names of revent recordings in the format,
    {device_name}.{suffix}.revent.

     - device_name can either be specified manually with the ``-d`` argument or
       it can be automatically determined. On Android device it will be obtained
       from ``build.prop``, on Linux devices it is obtained from ``/proc/device-tree/model``.
     - suffix is used by WA to determine which part of the app execution the
       recording is for, currently these are either ``setup``, ``run``, ``extract_results``
       or ``teardown``. All stages are optional for recording and these should
       be specified with the ``-s``, ``-r``, ``-e`` or ``-t`` arguments respectively,
       or optionally ``-a`` to indicate all stages should be recorded.
    '''

    def __init__(self, **kwargs):
        super(RecordCommand, self).__init__(**kwargs)
        self.tm = None
        self.target = None
        self.revent_recorder = None

    def initialize(self, context):
        self.parser.add_argument('-d',
                                 '--device',
                                 metavar='DEVICE',
                                 help='''
                                 Specify the device on which to run. This will
                                 take precedence over the device (if any)
                                 specified in configuration.
                                 ''')
        self.parser.add_argument('-o',
                                 '--output',
                                 help='Specify the output file',
                                 metavar='FILE')
        self.parser.add_argument('-s',
                                 '--setup',
                                 help='Record a recording for setup stage',
                                 action='store_true')
        self.parser.add_argument('-r',
                                 '--run',
                                 help='Record a recording for run stage',
                                 action='store_true')
        self.parser.add_argument(
            '-e',
            '--extract_results',
            help='Record a recording for extract_results stage',
            action='store_true')
        self.parser.add_argument('-t',
                                 '--teardown',
                                 help='Record a recording for teardown stage',
                                 action='store_true')
        self.parser.add_argument('-a',
                                 '--all',
                                 help='Record recordings for available stages',
                                 action='store_true')

        # Need validation
        self.parser.add_argument('-C',
                                 '--clear',
                                 help='Clear app cache before launching it',
                                 action='store_true')
        group = self.parser.add_mutually_exclusive_group(required=False)
        group.add_argument('-p',
                           '--package',
                           help='Android package to launch before recording')
        group.add_argument('-w',
                           '--workload',
                           help='Name of a revent workload (mostly games)')

    def validate_args(self, args):
        if args.clear and not (args.package or args.workload):
            self.logger.error(
                "Package/Workload must be specified if you want to clear cache"
            )
            sys.exit()
        if args.workload and args.output:
            self.logger.error("Output file cannot be specified with Workload")
            sys.exit()
        if not args.workload and (args.setup or args.extract_results
                                  or args.teardown or args.all):
            self.logger.error(
                "Cannot specify a recording stage without a Workload")
            sys.exit()
        if args.workload and not any([
                args.all, args.teardown, args.extract_results, args.run,
                args.setup
        ]):
            self.logger.error(
                "Please specify which workload stages you wish to record")
            sys.exit()

    def execute(self, state, args):
        self.validate_args(args)
        state.run_config.merge_device_config(state.plugin_cache)
        if args.device:
            device = args.device
            device_config = {}
        else:
            device = state.run_config.device
            device_config = state.run_config.device_config or {}

        if args.output:
            outdir = os.path.basename(args.output)
        else:
            outdir = os.getcwd()

        self.tm = TargetManager(device, device_config, outdir)
        self.tm.initialize()
        self.target = self.tm.target
        self.revent_recorder = ReventRecorder(self.target)
        self.revent_recorder.deploy()

        if args.workload:
            self.workload_record(args)
        elif args.package:
            self.package_record(args)
        else:
            self.manual_record(args)

        self.revent_recorder.remove()

    def record(self, revent_file, name, output_path):
        msg = 'Press Enter when you are ready to record {}...'
        self.logger.info(msg.format(name))
        raw_input('')
        self.revent_recorder.start_record(revent_file)
        msg = 'Press Enter when you have finished recording {}...'
        self.logger.info(msg.format(name))
        raw_input('')
        self.revent_recorder.stop_record()

        if not os.path.isdir(output_path):
            os.makedirs(output_path)

        revent_file_name = self.target.path.basename(revent_file)
        host_path = os.path.join(output_path, revent_file_name)
        if os.path.exists(host_path):
            msg = 'Revent file \'{}\' already exists, overwrite? [y/n]'
            self.logger.info(msg.format(revent_file_name))
            if input('') == 'y':
                os.remove(host_path)
            else:
                msg = 'Did not pull and overwrite \'{}\''
                self.logger.warning(msg.format(revent_file_name))
                return
        msg = 'Pulling \'{}\' from device'
        self.logger.info(msg.format(self.target.path.basename(revent_file)))
        self.target.pull(revent_file,
                         output_path,
                         as_root=self.target.is_rooted)

    def manual_record(self, args):
        output_path, file_name = self._split_revent_location(args.output)
        revent_file = self.target.get_workpath(file_name)
        self.record(revent_file, '', output_path)
        msg = 'Recording is available at: \'{}\''
        self.logger.info(msg.format(os.path.join(output_path, file_name)))

    def package_record(self, args):
        if self.target.os != 'android' and self.target.os != 'chromeos':
            raise ConfigError('Target does not appear to be running Android')
        if self.target.os == 'chromeos' and not self.target.supports_android:
            raise ConfigError('Target does not appear to support Android')
        if args.clear:
            self.target.execute('pm clear {}'.format(args.package))
        self.logger.info('Starting {}'.format(args.package))
        cmd = 'monkey -p {} -c android.intent.category.LAUNCHER 1'
        self.target.execute(cmd.format(args.package))

        output_path, file_name = self._split_revent_location(args.output)
        revent_file = self.target.get_workpath(file_name)
        self.record(revent_file, '', output_path)
        msg = 'Recording is available at: \'{}\''
        self.logger.info(msg.format(os.path.join(output_path, file_name)))

    def workload_record(self, args):
        context = LightContext(self.tm)
        setup_revent = '{}.setup.revent'.format(self.target.model)
        run_revent = '{}.run.revent'.format(self.target.model)
        extract_results_revent = '{}.extract_results.revent'.format(
            self.target.model)
        teardown_file_revent = '{}.teardown.revent'.format(self.target.model)
        setup_file = self.target.get_workpath(setup_revent)
        run_file = self.target.get_workpath(run_revent)
        extract_results_file = self.target.get_workpath(extract_results_revent)
        teardown_file = self.target.get_workpath(teardown_file_revent)

        self.logger.info('Deploying {}'.format(args.workload))
        workload = pluginloader.get_workload(args.workload, self.target)
        # Setup apk if android workload
        if hasattr(workload, 'apk'):
            workload.apk.initialize(context)
            workload.apk.setup(context)
            sleep(workload.loading_time)

        output_path = os.path.join(workload.dependencies_directory,
                                   'revent_files')
        if args.setup or args.all:
            self.record(setup_file, 'SETUP', output_path)
        if args.run or args.all:
            self.record(run_file, 'RUN', output_path)
        if args.extract_results or args.all:
            self.record(extract_results_file, 'EXTRACT_RESULTS', output_path)
        if args.teardown or args.all:
            self.record(teardown_file, 'TEARDOWN', output_path)
        self.logger.info('Tearing down {}'.format(args.workload))
        workload.teardown(context)
        self.logger.info(
            'Recording(s) are available at: \'{}\''.format(output_path))

    def _split_revent_location(self, output):
        output_path = None
        file_name = None
        if output:
            output_path, file_name, = os.path.split(output)

        if not file_name:
            file_name = '{}.revent'.format(self.target.model)
        if not output_path:
            output_path = os.getcwd()

        return output_path, file_name
Ejemplo n.º 9
0
class RecordCommand(Command):

    name = 'record'
    description = '''
    Performs a revent recording

    This command helps making revent recordings. It will automatically
    deploy revent and has options to automatically open apps and record
    specified stages of a workload.

    Revent allows you to record raw inputs such as screen swipes or button presses.
    This can be useful for recording inputs for workloads such as games that don't
    have XML UI layouts that can be used with UIAutomator. As a drawback from this,
    revent recordings are specific to the device type they were recorded on.

    WA uses two parts to the names of revent recordings in the format,
    {device_name}.{suffix}.revent.

     - device_name can either be specified manually with the ``-d`` argument or
       it can be automatically determined. On Android device it will be obtained
       from ``build.prop``, on Linux devices it is obtained from ``/proc/device-tree/model``.
     - suffix is used by WA to determine which part of the app execution the
       recording is for, currently these are either ``setup``, ``run``, ``extract_results``
       or ``teardown``. All stages are optional for recording and these should
       be specified with the ``-s``, ``-r``, ``-e`` or ``-t`` arguments respectively,
       or optionally ``-a`` to indicate all stages should be recorded.
    '''

    def __init__(self, **kwargs):
        super(RecordCommand, self).__init__(**kwargs)
        self.tm = None
        self.target = None
        self.revent_recorder = None

    def initialize(self, context):
        self.parser.add_argument('-d', '--device', metavar='DEVICE',
                                 help='''
                                 Specify the device on which to run. This will
                                 take precedence over the device (if any)
                                 specified in configuration.
                                 ''')
        self.parser.add_argument('-o', '--output', help='Specify the output file', metavar='FILE')
        self.parser.add_argument('-s', '--setup', help='Record a recording for setup stage',
                                 action='store_true')
        self.parser.add_argument('-r', '--run', help='Record a recording for run stage',
                                 action='store_true')
        self.parser.add_argument('-e', '--extract_results', help='Record a recording for extract_results stage',
                                 action='store_true')
        self.parser.add_argument('-t', '--teardown', help='Record a recording for teardown stage',
                                 action='store_true')
        self.parser.add_argument('-a', '--all', help='Record recordings for available stages',
                                 action='store_true')

        # Need validation
        self.parser.add_argument('-C', '--clear', help='Clear app cache before launching it',
                                 action='store_true')
        group = self.parser.add_mutually_exclusive_group(required=False)
        group.add_argument('-p', '--package', help='Android package to launch before recording')
        group.add_argument('-w', '--workload', help='Name of a revent workload (mostly games)')

    def validate_args(self, args):
        if args.clear and not (args.package or args.workload):
            self.logger.error("Package/Workload must be specified if you want to clear cache")
            sys.exit()
        if args.workload and args.output:
            self.logger.error("Output file cannot be specified with Workload")
            sys.exit()
        if not args.workload and (args.setup or args.extract_results or
                                  args.teardown or args.all):
            self.logger.error("Cannot specify a recording stage without a Workload")
            sys.exit()
        if args.workload and not any([args.all, args.teardown, args.extract_results, args.run, args.setup]):
            self.logger.error("Please specify which workload stages you wish to record")
            sys.exit()

    def execute(self, state, args):
        self.validate_args(args)
        state.run_config.merge_device_config(state.plugin_cache)
        if args.device:
            device = args.device
            device_config = {}
        else:
            device = state.run_config.device
            device_config = state.run_config.device_config or {}

        if args.output:
            outdir = os.path.basename(args.output)
        else:
            outdir = os.getcwd()

        self.tm = TargetManager(device, device_config, outdir)
        self.tm.initialize()
        self.target = self.tm.target
        self.revent_recorder = ReventRecorder(self.target)
        self.revent_recorder.deploy()

        if args.workload:
            self.workload_record(args)
        elif args.package:
            self.package_record(args)
        else:
            self.manual_record(args)

        self.revent_recorder.remove()

    def record(self, revent_file, name, output_path):
        msg = 'Press Enter when you are ready to record {}...'
        self.logger.info(msg.format(name))
        raw_input('')
        self.revent_recorder.start_record(revent_file)
        msg = 'Press Enter when you have finished recording {}...'
        self.logger.info(msg.format(name))
        raw_input('')
        self.revent_recorder.stop_record()

        if not os.path.isdir(output_path):
            os.makedirs(output_path)

        revent_file_name = self.target.path.basename(revent_file)
        host_path = os.path.join(output_path, revent_file_name)
        if os.path.exists(host_path):
            msg = 'Revent file \'{}\' already exists, overwrite? [y/n]'
            self.logger.info(msg.format(revent_file_name))
            if input('') == 'y':
                os.remove(host_path)
            else:
                msg = 'Did not pull and overwrite \'{}\''
                self.logger.warning(msg.format(revent_file_name))
                return
        msg = 'Pulling \'{}\' from device'
        self.logger.info(msg.format(self.target.path.basename(revent_file)))
        self.target.pull(revent_file, output_path, as_root=self.target.is_rooted)

    def manual_record(self, args):
        output_path, file_name = self._split_revent_location(args.output)
        revent_file = self.target.get_workpath(file_name)
        self.record(revent_file, '', output_path)
        msg = 'Recording is available at: \'{}\''
        self.logger.info(msg.format(os.path.join(output_path, file_name)))

    def package_record(self, args):
        if self.target.os != 'android' and self.target.os != 'chromeos':
            raise ConfigError('Target does not appear to be running Android')
        if self.target.os == 'chromeos' and not self.target.supports_android:
            raise ConfigError('Target does not appear to support Android')
        if args.clear:
            self.target.execute('pm clear {}'.format(args.package))
        self.logger.info('Starting {}'.format(args.package))
        cmd = 'monkey -p {} -c android.intent.category.LAUNCHER 1'
        self.target.execute(cmd.format(args.package))

        output_path, file_name = self._split_revent_location(args.output)
        revent_file = self.target.get_workpath(file_name)
        self.record(revent_file, '', output_path)
        msg = 'Recording is available at: \'{}\''
        self.logger.info(msg.format(os.path.join(output_path, file_name)))

    def workload_record(self, args):
        context = LightContext(self.tm)
        setup_revent = '{}.setup.revent'.format(self.target.model)
        run_revent = '{}.run.revent'.format(self.target.model)
        extract_results_revent = '{}.extract_results.revent'.format(self.target.model)
        teardown_file_revent = '{}.teardown.revent'.format(self.target.model)
        setup_file = self.target.get_workpath(setup_revent)
        run_file = self.target.get_workpath(run_revent)
        extract_results_file = self.target.get_workpath(extract_results_revent)
        teardown_file = self.target.get_workpath(teardown_file_revent)

        self.logger.info('Deploying {}'.format(args.workload))
        workload = pluginloader.get_workload(args.workload, self.target)
        # Setup apk if android workload
        if hasattr(workload, 'apk'):
            workload.apk.initialize(context)
            workload.apk.setup(context)
            sleep(workload.loading_time)

        output_path = os.path.join(workload.dependencies_directory,
                                   'revent_files')
        if args.setup or args.all:
            self.record(setup_file, 'SETUP', output_path)
        if args.run or args.all:
            self.record(run_file, 'RUN', output_path)
        if args.extract_results or args.all:
            self.record(extract_results_file, 'EXTRACT_RESULTS', output_path)
        if args.teardown or args.all:
            self.record(teardown_file, 'TEARDOWN', output_path)
        self.logger.info('Tearing down {}'.format(args.workload))
        workload.teardown(context)
        self.logger.info('Recording(s) are available at: \'{}\''.format(output_path))

    def _split_revent_location(self, output):
        output_path = None
        file_name = None
        if output:
            output_path, file_name, = os.path.split(output)

        if not file_name:
            file_name = '{}.revent'.format(self.target.model)
        if not output_path:
            output_path = os.getcwd()

        return output_path, file_name
Ejemplo n.º 10
0
class Executor(object):
    """
    The ``Executor``'s job is to set up the execution context and pass to a
    ``Runner`` along with a loaded run specification. Once the ``Runner`` has
    done its thing, the ``Executor`` performs some final reporting before
    returning.

    The initial context set up involves combining configuration from various
    sources, loading of requided workloads, loading and installation of
    instruments and output processors, etc. Static validation of the combined
    configuration is also performed.

    """
    # pylint: disable=R0915

    def __init__(self):
        self.logger = logging.getLogger('executor')
        self.error_logged = False
        self.warning_logged = False
        self.target_manager = None

    def execute(self, config_manager, output):
        """
        Execute the run specified by an agenda. Optionally, selectors may be
        used to only selecute a subset of the specified agenda.

        Params::

            :state: a ``ConfigManager`` containing processed configuration
            :output: an initialized ``RunOutput`` that will be used to
                     store the results.

        """
        signal.connect(self._error_signalled_callback, signal.ERROR_LOGGED)
        signal.connect(self._warning_signalled_callback, signal.WARNING_LOGGED)

        self.logger.info('Initializing run')
        self.logger.debug('Finalizing run configuration.')
        config = config_manager.finalize()
        output.write_config(config)

        self.target_manager = TargetManager(config.run_config.device,
                                            config.run_config.device_config,
                                            output.basepath)

        self.logger.info('Initializing execution context')
        context = ExecutionContext(config_manager, self.target_manager, output)

        try:
            self.do_execute(context)
        except KeyboardInterrupt as e:
            context.run_output.status = 'ABORTED'
            log.log_error(e, self.logger)
            context.write_output()
            raise
        except Exception as e:
            context.run_output.status = 'FAILED'
            log.log_error(e, self.logger)
            context.write_output()
            raise
        finally:
            context.finalize()
            self.execute_postamble(context, output)
            signal.send(signal.RUN_COMPLETED, self, context)

    def do_execute(self, context):
        self.logger.info('Connecting to target')
        context.tm.initialize()

        if context.cm.run_config.reboot_policy.perform_initial_reboot:
            self.logger.info('Performing initial reboot.')
            attempts = context.cm.run_config.max_retries
            while attempts:
                try:
                    self.target_manager.reboot()
                except TargetError as e:
                    if attempts:
                        attempts -= 1
                    else:
                        raise e
                else:
                    break

        context.output.set_target_info(self.target_manager.get_target_info())

        self.logger.info('Generating jobs')
        context.cm.generate_jobs(context)
        context.write_job_specs()
        context.output.write_state()

        self.logger.info('Installing instruments')
        for instrument in context.cm.get_instruments(self.target_manager.target):
            instrumentation.install(instrument, context)
        instrumentation.validate()

        self.logger.info('Installing output processors')
        pm = ProcessorManager()
        for proc in context.cm.get_processors():
            pm.install(proc, context)
        pm.validate()

        context.write_config()

        self.logger.info('Starting run')
        runner = Runner(context, pm)
        signal.send(signal.RUN_STARTED, self, context)
        runner.run()

    def execute_postamble(self, context, output):
        self.logger.info('Done.')
        duration = format_duration(output.info.duration)
        self.logger.info('Run duration: {}'.format(duration))
        num_ran = context.run_state.num_completed_jobs
        status_summary = 'Ran a total of {} iterations: '.format(num_ran)

        counter = context.run_state.get_status_counts()
        parts = []
        for status in reversed(Status.levels):
            if status in counter:
                parts.append('{} {}'.format(counter[status], status))
        self.logger.info(status_summary + ', '.join(parts))

        self.logger.info('Results can be found in {}'.format(output.basepath))

        if self.error_logged:
            self.logger.warning('There were errors during execution.')
            self.logger.warning('Please see {}'.format(output.logfile))
        elif self.warning_logged:
            self.logger.warning('There were warnings during execution.')
            self.logger.warning('Please see {}'.format(output.logfile))

    def _error_signalled_callback(self, _):
        self.error_logged = True
        signal.disconnect(self._error_signalled_callback, signal.ERROR_LOGGED)

    def _warning_signalled_callback(self, _):
        self.warning_logged = True
        signal.disconnect(self._warning_signalled_callback, signal.WARNING_LOGGED)

    def __str__(self):
        return 'executor'

    __repr__ = __str__