blob: 5646a901b35791db24dea1fa1c12b8ea62fed0cc [file] [log] [blame]
Alex Miller9979b5a2012-11-01 17:36:12 -07001# Copyright (c) 2013 The Chromium OS 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
6import collections
7import logging
8
9import common
10from autotest_lib.client.common_lib import global_config
11from autotest_lib.server.cros.dynamic_suite import reimager
12from autotest_lib.site_utils import suite_preprocessor
13
14
15default_num = global_config.global_config.get_config_value(
16 "CROS", "sharding_factor", default=1, type=int)
17
18
19def NumOfTask(task):
20 """
21 Take a run and produce the number of machines it will run across.
22
23 This seems easy, because |task.num| exists. However, it is |None| for any
24 suite that doesn't specify |num|. This is needed so that we know when to pass
25 in a num, and when not to because the server can override the default
26 sharding factor locally. Therefore, we need to compensate for this when doing
27 our local analysis here and provide the default sharding factor if we see
28 |num| is None.
29
30 @param task The task to get the |num| for.
31 @return |num| for this task.
32 """
33 if task.num is None:
34 return default_num
35 else:
Alex Miller4a163282013-01-15 18:18:54 -080036 return int(task.num)
Alex Miller9979b5a2012-11-01 17:36:12 -070037
38
39# TODO(milleral): crosbug.com/37623
40# DEPENDENCIES-related code needs to be refactored a bit so that trying to
41# gather all the dependencies and analyze them doesn't require reaching into
42# random pieces of code across the codebase nor reaching into other object's
43# private methods.
44
45def CheckDependencies(tasks):
46 """
47 Iterate through all of the tasks that suite_scheduler will process, and warn
48 if any of the tasks are set to run a suite with a |num| such that the suite
49 will not be able to satisfy all of its HostSpecs. This can happen when a
50 new test is added to a suite that increases the overall number of
51 HostSpecs, and |num| was not bumped up accordingly.
52
53 If the default sharding_factor is ever changed in the shadow_config on the
54 server, this sanity check will no longer give correct results.
55
56 @param tasks The list of tasks to check.
57 @return 0 if no problems are found
58 1 if problems are found
59 """
60 test_deps = suite_preprocessor.calculate_dependencies(common.autotest_dir)
61
62 by_suite = collections.defaultdict(list)
63 for task in tasks:
64 by_suite.setdefault(task.suite, []).append(task)
65
66 corrections = []
67 for suitename, control_deps in test_deps.items():
68 if not suitename:
69 continue
Vadim Bendebury4d6981b2013-01-08 13:01:40 -080070 imager = reimager.OsReimager(common.autotest_dir, 'none')
Alex Miller9979b5a2012-11-01 17:36:12 -070071
72 # Figure out what kind of hosts we need to grab.
73 per_test_specs = imager._build_host_specs_from_dependencies(
74 None, None, control_deps).values()
75
76 hostspecs = len(set([x for x in per_test_specs]))
77
78 for task in by_suite[suitename]:
79 if hostspecs > NumOfTask(task):
80 corrections.append((task.name, hostspecs))
81
82 for c in corrections:
83 # Failures to parse a config entry result in a logging.warn(),
84 # so let's keep the output the same across different errors
85 logging.warn("Increase %s to |num: %d|", c[0], c[1])
86
87 return 1 if corrections else 0