testing/talos/mach_commands.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/talos/mach_commands.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,150 @@
     1.4 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.5 +# License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 +# file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 +
     1.8 +# Integrates Talos mozharness with mach
     1.9 +
    1.10 +from __future__ import print_function, unicode_literals
    1.11 +
    1.12 +import os
    1.13 +import sys
    1.14 +import json
    1.15 +import which
    1.16 +import socket
    1.17 +
    1.18 +from mozbuild.base import (
    1.19 +    MozbuildObject,
    1.20 +    MachCommandBase
    1.21 +)
    1.22 +
    1.23 +from mach.decorators import (
    1.24 +    CommandArgument,
    1.25 +    CommandProvider,
    1.26 +    Command,
    1.27 +)
    1.28 +
    1.29 +class TalosRunner(MozbuildObject):
    1.30 +    def run_test(self, suite, repo, rev):
    1.31 +        """
    1.32 +        We want to do couple of things before running Talos
    1.33 +        1. Clone mozharness
    1.34 +        2. Make config for Talos Mozharness
    1.35 +        3. Run mozharness
    1.36 +        """
    1.37 +
    1.38 +        print("Running Talos test suite %s" % suite)
    1.39 +        self.init_variables(suite, repo, rev)
    1.40 +        self.clone_mozharness()
    1.41 +        self.make_config()
    1.42 +        self.write_config()
    1.43 +        self.make_args()
    1.44 +        return self.run_mozharness()
    1.45 +
    1.46 +    def init_variables(self, suite, repo, rev):
    1.47 +        self.suite = suite
    1.48 +        self.mozharness_repo = repo
    1.49 +        self.mozharness_rev = rev
    1.50 +
    1.51 +        self.talos_dir = os.path.join(self.topsrcdir, 'testing', 'talos')
    1.52 +        self.mozharness_dir = os.path.join(self.topobjdir, 'mozharness')
    1.53 +        self.config_dir = os.path.join(self.mozharness_dir, 'configs', 'talos')
    1.54 +        self.talos_json = os.path.join(self.talos_dir, 'talos.json')
    1.55 +        self.config_filename = 'in_tree_conf.json'
    1.56 +        self.config_file_path = os.path.join(self.config_dir,
    1.57 +                                             self.config_filename)
    1.58 +        self.binary_path = self.get_binary_path()
    1.59 +        self.virtualenv_script = os.path.join(self.topsrcdir, 'python',
    1.60 +                                              'virtualenv', 'virtualenv.py')
    1.61 +        self.virtualenv_path = os.path.join(self.mozharness_dir, 'venv')
    1.62 +        self.python_interp = sys.executable
    1.63 +
    1.64 +    def clone_mozharness(self):
    1.65 +        """Clones mozharness into topobjdir/mozharness
    1.66 +           using mercurial. If mozharness is already cloned,
    1.67 +           it updates it to the latest version"""
    1.68 +        try:
    1.69 +            mercurial = which.which('hg')
    1.70 +        except which.WhichError as e:
    1.71 +            print("You don't have hg in your PATH: {0}".format(e))
    1.72 +            raise e
    1.73 +        clone_cmd = [mercurial, 'clone', '-r', self.mozharness_rev,
    1.74 +                     self.mozharness_repo, self.mozharness_dir]
    1.75 +        pull_cmd = [mercurial, 'pull', '-r', self.mozharness_rev, '-u']
    1.76 +
    1.77 +        dot_hg = os.path.join(self.mozharness_dir, '.hg')
    1.78 +        if os.path.exists(dot_hg):
    1.79 +            self.run_process(args=pull_cmd, cwd=self.mozharness_dir)
    1.80 +        else:
    1.81 +            self.run_process(args=clone_cmd)
    1.82 +
    1.83 +    def make_config(self):
    1.84 +        self.config = {
    1.85 +            'talos_json': self.talos_json,
    1.86 +            'binary_path': self.binary_path,
    1.87 +            'log_name': 'talos',
    1.88 +            'virtualenv_path': self.virtualenv_path,
    1.89 +            'pypi_url': 'http://pypi.python.org/simple',
    1.90 +            'use_talos_json': True,
    1.91 +            'base_work_dir': self.mozharness_dir,
    1.92 +            'exes': {
    1.93 +                'python': self.python_interp,
    1.94 +                'virtualenv': [self.python_interp, self.virtualenv_script]
    1.95 +            },
    1.96 +            'title': socket.gethostname(),
    1.97 +            'default_actions': [
    1.98 +                'clone-talos',
    1.99 +                'create-virtualenv',
   1.100 +                'run-tests',
   1.101 +            ],
   1.102 +            'python_webserver': True
   1.103 +        }
   1.104 +
   1.105 +    def make_args(self):
   1.106 +        self.args = {
   1.107 +            'config': {
   1.108 +                'suite': self.suite,
   1.109 +                'use_talos_json': True
   1.110 +            },
   1.111 +           'initial_config_file': self.config_file_path,
   1.112 +       }
   1.113 +
   1.114 +    def write_config(self):
   1.115 +        try:
   1.116 +            config_file = open(self.config_file_path, 'wb')
   1.117 +            config_file.write(json.dumps(self.config))
   1.118 +        except IOError as e:
   1.119 +            err_str = "Error writing to Talos Mozharness config file {0}:{1}"
   1.120 +            print(err_str.format(self.config_file_path, str(e)))
   1.121 +            raise e
   1.122 +
   1.123 +    def run_mozharness(self):
   1.124 +        sys.path.insert(0, self.mozharness_dir)
   1.125 +        from mozharness.mozilla.testing.talos import Talos
   1.126 +        talos_mh = Talos(config=self.args['config'],
   1.127 +                         initial_config_file=self.args['initial_config_file'])
   1.128 +        return talos_mh.run()
   1.129 +
   1.130 +@CommandProvider
   1.131 +class MachCommands(MachCommandBase):
   1.132 +    mozharness_repo = 'https://hg.mozilla.org/build/mozharness'
   1.133 +    mozharness_rev = 'production'
   1.134 +
   1.135 +    @Command('talos-test', category='testing',
   1.136 +             description='Run talos tests.')
   1.137 +    @CommandArgument('suite', help='Talos test suite to run. Valid suites are '
   1.138 +                                   'chromez, dirtypaint, dromaeojs, other,'
   1.139 +                                   'svgr, rafx, tpn, tp5o, xperf.')
   1.140 +    @CommandArgument('--repo', default=mozharness_repo,
   1.141 +                     help='The mozharness repository to clone from. '
   1.142 +                          'Defaults to http://hg.mozilla.org/build/mozharness')
   1.143 +    @CommandArgument('--rev', default=mozharness_rev,
   1.144 +                     help='The mozharness revision to clone. Defaults to '
   1.145 +                          'production')
   1.146 +    def run_talos_test(self, suite, repo=None, rev=None):
   1.147 +        talos = self._spawn(TalosRunner)
   1.148 +
   1.149 +        try:
   1.150 +            return talos.run_test(suite, repo, rev)
   1.151 +        except Exception as e:
   1.152 +            print(str(e))
   1.153 +            return 1

mercurial