1# Copyright 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Generates test runner factory and tests for performance tests."""
6
7import json
8import fnmatch
9import logging
10import os
11import psutil
12import signal
13import shutil
14import time
15
16from pylib import constants
17from pylib import forwarder
18from pylib.utils import test_environment
19
20import test_runner
21
22
23def Setup(test_options):
24  """Create and return the test runner factory and tests.
25
26  Args:
27    test_options: A PerformanceOptions object.
28
29  Returns:
30    A tuple of (TestRunnerFactory, tests).
31  """
32  # TODO(bulach): remove this once the bot side lands. BUG=318369
33  constants.SetBuildType('Release')
34  if os.path.exists(constants.PERF_OUTPUT_DIR):
35    shutil.rmtree(constants.PERF_OUTPUT_DIR)
36  os.makedirs(constants.PERF_OUTPUT_DIR)
37
38  # Before running the tests, kill any leftover server.
39  test_environment.CleanupLeftoverProcesses()
40  forwarder.Forwarder.UseMultiprocessing()
41
42  if test_options.single_step:
43    # Running a single command, build the tests structure.
44    tests = [['single_step', test_options.single_step]]
45
46  if test_options.steps:
47    with file(test_options.steps, 'r') as f:
48      tests = json.load(f)
49
50  # The list is necessary to keep the steps order, but internally
51  # the format is squashed from a list of lists into a single dict:
52  # [["A", "cmd"], ["B", "cmd"]] into {"A": "cmd", "B": "cmd"}
53  sorted_test_names = [i[0] for i in tests]
54  tests_dict = dict(tests)
55
56  if test_options.test_filter:
57    sorted_test_names = fnmatch.filter(sorted_test_names,
58                                       test_options.test_filter)
59    tests_dict = dict((k, v) for k, v in tests_dict.iteritems()
60                      if k in sorted_test_names)
61
62  flaky_steps = []
63  if test_options.flaky_steps:
64    with file(test_options.flaky_steps, 'r') as f:
65      flaky_steps = json.load(f)
66
67  def TestRunnerFactory(device, shard_index):
68    return test_runner.TestRunner(
69        test_options, device, tests_dict, flaky_steps)
70
71  return (TestRunnerFactory, sorted_test_names)
72