Exemple #1
0
def _convert_hi_res_images():
    """Function resizes all the project's hi-resolution images."""
    for root, dirs, files in os.walk(PathManager.get_module_dir()):
        for file_name in files:
            if file_name.endswith('.png'):
                if 'images' in root or 'local_web' in root:
                    if '@' in file_name:
                        logger.debug('Found hi-resolution image at: %s' %
                                     os.path.join(root, file_name))
                        temp = file_name.split('@')
                        name = temp[0]
                        scale = int(temp[1].split('x')[0])
                        new_name = '%s.png' % name
                        img = Image.open(os.path.join(root, file_name))
                        logger.debug('Resizing image from %sx scale' % scale)
                        new_img = img.resize(
                            (img.width / scale, img.height / scale),
                            Image.ANTIALIAS)
                        logger.debug(
                            'Creating newly converted image file at: %s' %
                            os.path.join(root, new_name))
                        new_img.save(os.path.join(root, new_name))
                        logger.debug('Removing unused image at: %s' %
                                     os.path.join(root, file_name))
                        os.remove(os.path.join(root, file_name))
Exemple #2
0
def create_target_json():
    if not use_cached_target_file():
        logging.info('Preparing data for the Control Center.')
        logging.info('This may take a minute.')
        master_target_dir = os.path.join(PathManager.get_module_dir(),
                                         'targets')
        target_list = [
            f for f in os.listdir(master_target_dir)
            if not f.startswith('__') and not f.startswith('.')
        ]

        targets = []
        for item in target_list:
            try:
                app_tests = scan_all_tests(item)
                target_module = importlib.import_module('targets.%s.app' %
                                                        item)
                try:
                    target = target_module.Target()
                    targets.append({
                        'name': target.target_name,
                        'tests': app_tests,
                        'icon': '%s.png' % item,
                        'settings': target.cc_settings
                    })
                except NameError:
                    logger.error('Can\'t find default Target class.')
            except ImportError as e:
                logger.error('Problems importing module \'%s\':\n%s' %
                             (item, e))

        target_json = {'targets': targets}
        target_json_file = os.path.join(args.workdir, 'data', 'targets.json')
        with open(target_json_file, 'w') as f:
            json.dump(target_json, f, sort_keys=True, indent=True)
Exemple #3
0
def convert_test_list(test_list, only_failures=False):
    """Takes a flat list of test objects and paths and converts to an object that can be serialized as JSON.

    :param test_list: List of completed tests
    :param only_failures: If True, only return failed tests
    :return:
    """
    test_root = os.path.join(PathManager.get_module_dir(), 'tests')
    tests = []
    for test in test_list:
        test_failed = True if 'FAILED' in test.outcome or 'ERROR' in test.outcome else False
        original_path = str(test.item.__dict__.get('fspath'))
        target_root = original_path.split(test_root)[1]
        target = target_root.split(os.sep)[1]
        test_path = target_root.split('%s%s%s' % (os.sep, target, os.sep))[1]
        parent = tests
        details = get_test_markers(test.item)
        for module in test_path.split(os.sep):
            test_obj = {'name': module.split('.py')[0]}
            if 'py' not in module:
                module_exists = False
                for objects in parent:
                    if objects['name'] == module:
                        parent = objects['children']
                        module_exists = True
                        break
                if not module_exists:
                    new_parent = test_obj['children'] = []
                    if only_failures and test_failed:
                        parent.append(test_obj)
                    elif not only_failures:
                        parent.append(test_obj)
                    parent = new_parent
            else:
                if test_failed:
                    test_assert = {
                        'error': test.error.lstrip(), 'message': test.message.lstrip(), 'call_stack': test.traceback + '\n\n ',
                        'code': get_failing_code(test.node_name, int(test.line))
                    }
                    test_obj['assert'] = test_assert
                test_obj['result'] = test.outcome
                test_obj['time'] = test.test_duration
                debug_image_directory = os.path.join(PathManager.get_current_run_dir(), test_path.split('.py')[0],
                                                     'debug_images')
                test_obj['debug_image_directory'] = debug_image_directory
                test_obj['debug_images'] = get_image_names(debug_image_directory)
                test_obj['description'] = details.get('description')

                values = {}
                for i in details:
                    if i != 'description':
                        values[i] = details.get(i)
                test_obj['values'] = values
                if only_failures and test_failed:
                    parent.append(test_obj)
                elif not only_failures:
                    parent.append(test_obj)
                parent = tests
    return tests
Exemple #4
0
def init_control_center():
    copy_tree(
        os.path.join(PathManager.get_module_dir(), 'src', 'control_center',
                     'assets'),
        get_core_args().workdir)
    targets_dir = os.path.join(PathManager.get_module_dir(), 'targets')

    exclude_dirs = {'__pycache__'}
    for path, dirs, files in PathManager.sorted_walk(targets_dir):
        [dirs.remove(d) for d in list(dirs) if d in exclude_dirs]
        for target in dirs:
            src = os.path.join(targets_dir, target, 'icon.png')
            dest = os.path.join(get_core_args().workdir, 'images',
                                '%s.png' % target)
            try:
                shutil.copyfile(src, dest)
            except FileNotFoundError:
                logger.warning('Could not find icon file for target: %s' %
                               target)
        break
    create_target_json()
Exemple #5
0
def load_target(target: str = None):
    """Checks if provided target exists."""
    if target is None:
        logger.warning('No target provided. Launching Firefox target by default')
        target = core_args.target

    target_dir = os.path.join(PathManager.get_module_dir(), 'targets', target)
    if os.path.exists(target_dir):
        logger.debug('%s target module found!' % target)
        return True

    logger.critical('Iris doesn\'t contain %s target module' % target)
    return False
Exemple #6
0
    def _get_staged_profile(profile_name, path):
        """
        Internal-only method used to extract a given profile.
        :param profile_name:
        :param path:
        :return:
        """
        staged_profiles = os.path.join(PathManager.get_module_dir(), 'targets',
                                       'firefox', 'firefox_app', 'profiles')

        sz_bin = find_executable('7z')
        logger.debug('Using 7zip executable at "%s"' % sz_bin)

        zipped_profile = os.path.join(staged_profiles,
                                      '%s.zip' % profile_name.value)

        cmd = [
            sz_bin, 'x', '-y', '-bd',
            '-o%s' % staged_profiles, zipped_profile
        ]
        logger.debug('Unzipping profile with command "%s"' % ' '.join(cmd))
        try:
            output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            logger.error('7zip failed: %s' % repr(e.output))
            raise Exception('Unable to unzip profile.')
        logger.debug('7zip succeeded: %s' % repr(output))

        from_directory = os.path.join(staged_profiles, profile_name.value)
        to_directory = path
        logger.debug('Creating new profile: %s' % to_directory)

        dir_util.copy_tree(from_directory, to_directory)

        try:
            shutil.rmtree(from_directory)
        except WindowsError:
            logger.debug(
                'Error, can\'t remove orphaned directory, leaving in place.')

        resource_fork_folder = os.path.join(staged_profiles, '__MACOSX')
        if os.path.exists(resource_fork_folder):
            try:
                shutil.rmtree(resource_fork_folder)
            except WindowsError:
                logger.debug(
                    'Error, can\'t remove orphaned directory, leaving in place.'
                )

        return to_directory
Exemple #7
0
def _load_all_patterns(application: str) -> list:
    """Function returns a list with all the project's Patterns."""
    if get_core_args().resize:
        _convert_hi_res_images()
    result_list = []
    for root, dirs, files in os.walk(PathManager.get_module_dir()):
        for file_name in files:
            if file_name.endswith('.png'):
                if application in root and (PathManager.get_images_path() in root or 'common' in root):
                    pattern_name, pattern_scale = _parse_name(file_name)
                    pattern_path = os.path.join(root, file_name)
                    pattern = {'name': pattern_name, 'path': pattern_path, 'scale': pattern_scale}
                    result_list.append(pattern)
    return result_list
Exemple #8
0
def scan_all_tests(target):
    logging.info('Gathering test info for \'%s\'...' % target)
    master_test_root = os.path.join(PathManager.get_module_dir(), 'tests')
    test_root = os.path.join(master_test_root, target)
    base_props = ['description']
    tests = []

    my_plugin = TestCollector()
    pytest.main(['--collect-only', '-s', '-p', 'no:terminal', test_root],
                plugins=[my_plugin])

    for test in my_plugin.get_collected_items():
        original_path = str(test.__dict__.get('fspath'))
        target_root = original_path.split(master_test_root)[1]
        test_path = target_root.split('%s%s%s' % (os.sep, target, os.sep))[1]
        parent = tests
        details = get_test_markers(test)
        for module in test_path.split(os.sep):
            test_obj = {'name': module.split('.py')[0]}
            if 'py' not in module:
                module_exists = False
                for objects in parent:
                    if objects['name'] == module:
                        parent = objects['children']
                        module_exists = True
                        break
                if not module_exists:
                    new_parent = test_obj['children'] = []
                    parent.append(test_obj)
                    parent = new_parent
            else:
                for prop in base_props:
                    if details.get(prop) is not None:
                        test_obj[prop] = details.get(prop)
                    else:
                        test_obj[prop] = ''
                test_obj['file'] = original_path
                values = {}
                for i in details:
                    if i not in base_props:
                        values[i] = details.get(i)
                if details.get('platform') is None:
                    values['platform'] = 'all'
                if details.get('locale') is None:
                    values['locale'] = 'all'
                test_obj['values'] = values
                parent.append(test_obj)
                parent = tests
    return tests
Exemple #9
0
def load_app(app: str = None):
    """Checks if provided application exists."""
    if app is None:
        logger.warning(
            'No application provided. Launching Firefox application by default'
        )
        app = core_args.application

    app_dir = os.path.join(PathManager.get_module_dir(), 'targets', app)
    if os.path.exists(app_dir):
        logger.debug('%s application module found!' % app)
        return True

    logger.critical('Iris doesn\'t contain %s application module' % app)
    return False
Exemple #10
0
 def __init__(self):
     BaseTarget.__init__(self)
     global target_args
     target_args = self.get_target_args()
     self.target_name = 'Firefox'
     self.process_list = []
     self.cc_settings = [{
         'name':
         'firefox',
         'type':
         'list',
         'label':
         'Firefox',
         'value':
         ['local', 'latest', 'latest-esr', 'latest-beta', 'nightly'],
         'default':
         'beta'
     }, {
         'name': 'locale',
         'type': 'list',
         'label': 'Locale',
         'value': OSHelper.LOCALES,
         'default': 'en-US'
     }, {
         'name': 'mouse',
         'type': 'list',
         'label': 'Mouse speed',
         'value': ['0.0', '0.5', '1.0', '2.0'],
         'default': '0.5'
     }, {
         'name': 'highlight',
         'type': 'checkbox',
         'label': 'Debug using highlighting'
     }, {
         'name': 'override',
         'type': 'checkbox',
         'label': 'Run disabled tests'
     }, {
         'name': 'email',
         'type': 'checkbox',
         'label': 'Email results'
     }, {
         'name': 'report',
         'type': 'checkbox',
         'label': 'Create TestRail report'
     }]
     self.local_web_root = os.path.join(PathManager.get_module_dir(),
                                        'targets', 'firefox', 'local_web')
Exemple #11
0
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import logging
import os.path
from configparser import ConfigParser

from src.core.util.path_manager import PathManager

logger = logging.getLogger(__name__)

config_file = os.path.join(PathManager.get_module_dir(), 'config.ini')
config = ConfigParser()


def get_config_section(section):
    """Returns all properties of a section as a dict or None if section does not exist."""
    if os.path.isfile(config_file):
        try:
            config.read(config_file)
            if config.has_section(section):
                result = dict(config.items(section))
                return result

        except EOFError:
            logger.warning('Config file error.')
        return None
    logger.warning('Config file not found.')
    return None
Exemple #12
0
def create_run_log(app):
    args = get_core_args()
    meta = {
        'run_id':
        PathManager.get_run_id(),
        'platform':
        OSHelper.get_os().value,
        'config':
        '%s, %s-bit, %s' % (OSHelper.get_os().value, OSHelper.get_os_bits(),
                            OSHelper.get_processor()),
        'locale':
        args.locale,
        'args':
        ' '.join(sys.argv),
        'params':
        vars(args),
        'log':
        os.path.join(PathManager.get_current_run_dir(), 'iris_log.log')
    }
    values = {}
    for i in app.values:
        values[i] = app.values[i]
    meta['values'] = values

    repo = git.Repo(PathManager.get_module_dir())
    meta['iris_version'] = 2.0
    meta['iris_repo'] = repo.working_tree_dir
    meta['iris_branch'] = repo.active_branch.name
    meta['iris_branch_head'] = repo.head.object.hexsha
    meta['python_version'] = get_python_version()

    failed = 0
    passed = 0
    skipped = 0
    errors = 0

    for test in app.completed_tests:
        if test.outcome == 'FAILED':
            failed = failed + 1
        if test.outcome == 'PASSED':
            passed = passed + 1
        if test.outcome == 'SKIPPED':
            skipped = skipped + 1
        if test.outcome == 'ERROR':
            errors = errors + 1

    logger.debug('Updating runs.json with completed run data.')
    meta['total'] = len(app.completed_tests)
    meta['passed'] = passed
    meta['failed'] = failed
    meta['skipped'] = skipped
    meta['errors'] = errors
    meta['start_time'] = app.start_time
    meta['end_time'] = app.end_time
    meta['total_time'] = app.end_time - app.start_time

    tests = {
        'all_tests': convert_test_list(app.completed_tests),
        'failed_tests': convert_test_list(app.completed_tests,
                                          only_failures=True)
    }

    run_file = os.path.join(PathManager.get_current_run_dir(), 'run.json')
    run_file_data = {'meta': meta, 'tests': tests}

    with open(run_file, 'w') as f:
        json.dump(run_file_data, f, sort_keys=True, indent=True)