suite_runner.py revision b47bff4d3336c5fe5593a95963c0f3dc20a02f68
1#!/usr/bin/python
2
3# Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import os
8import time
9
10from utils import command_executer
11
12
13class SuiteRunner(object):
14  """ This defines the interface from crosperf to test script.
15  """
16  def __init__(self, logger_to_use=None):
17    self._logger = logger_to_use
18    self._ce = command_executer.GetCommandExecuter(self._logger)
19    self._ct = command_executer.CommandTerminator()
20
21  def Run(self, machine, label, benchmark, test_args):
22    if benchmark.suite == "telemetry":
23      return self.Telemetry_Run(machine, label, benchmark)
24    elif benchmark.use_test_that:
25      return self.Test_That_Run(machine, label, benchmark, test_args)
26    else:
27      return self.Pyauto_Run(machine, label, benchmark, test_args)
28
29  def RebootMachine(self, machine_name, chromeos_root):
30    command ="reboot && exit"
31    self._ce.CrosRunCommand(command, machine=machine_name,
32                      chromeos_root=chromeos_root)
33    time.sleep(60)
34
35
36  def Pyauto_Run(self, machine, label, benchmark, test_args):
37    """Run the run_remote_test."""
38    options = ""
39    if label.board:
40      options += " --board=%s" % label.board
41    if test_args:
42      options += " %s" % test_args
43    command = "rm -rf /usr/local/autotest/results/*"
44    self._ce.CrosRunCommand(command, machine=machine, username="root",
45                            chromeos_root=label.chromeos_root)
46
47    self.RebootMachine(machine, label.chromeos_root)
48
49    command = ("./run_remote_tests.sh --remote=%s %s %s" %
50               (machine, options, benchmark.test_name))
51    return self._ce.ChrootRunCommand(label.chromeos_root,
52                                     command,
53                                     True,
54                                     self._ct)
55
56  def Test_That_Run(self, machine, label, benchmark, test_args):
57    """Run the test_that test.."""
58    options = ""
59    if label.board:
60      options += " --board=%s" % label.board
61    if test_args:
62      options += " %s" % test_args
63    command = "rm -rf /usr/local/autotest/results/*"
64    self._ce.CrosRunCommand(command, machine=machine, username="root",
65                            chromeos_root=label.chromeos_root)
66
67    self.RebootMachine(machine, label.chromeos_root)
68
69    command = ("/usr/bin/test_that %s %s %s" %
70               (options, machine, benchmark.test_name))
71    return self._ce.ChrootRunCommand(label.chromeos_root,
72                                     command,
73                                     True,
74                                     self._ct)
75
76  def Telemetry_Run(self, machine, label, benchmark):
77    if not os.path.isdir(label.chrome_src):
78      self._logger.LogFatal("Cannot find chrome src dir to"
79                                        " run telemetry.")
80    rsa_key = os.path.join(label.chromeos_root,
81        "src/scripts/mod_for_test_scripts/ssh_keys/testing_rsa")
82
83    cmd = ("cd {0} && "
84           "./tools/perf/run_measurement "
85           "--browser=cros-chrome "
86           "--output-format=csv "
87           "--remote={1} "
88           "--identity {2} "
89           "{3} {4}".format(label.chrome_src, machine,
90                            rsa_key,
91                            benchmark.test_name,
92                            benchmark.test_args))
93    return self._ce.RunCommand(cmd, return_output=True,
94                               print_to_console=False)
95
96  def Terminate(self):
97    self._ct.Terminate()
98
99
100class MockSuiteRunner(object):
101  def __init__(self):
102    pass
103
104  def Run(self, *args):
105    return ["", "", 0]
106