root / qa / qa_config.py @ c54784d9
History | View | Annotate | Download (2.8 kB)
1 | c68d1f43 | Michael Hanselmann | #
|
---|---|---|---|
2 | c68d1f43 | Michael Hanselmann | #
|
3 | c68d1f43 | Michael Hanselmann | |
4 | cec9845c | Michael Hanselmann | # Copyright (C) 2007 Google Inc.
|
5 | cec9845c | Michael Hanselmann | #
|
6 | cec9845c | Michael Hanselmann | # This program is free software; you can redistribute it and/or modify
|
7 | cec9845c | Michael Hanselmann | # it under the terms of the GNU General Public License as published by
|
8 | cec9845c | Michael Hanselmann | # the Free Software Foundation; either version 2 of the License, or
|
9 | cec9845c | Michael Hanselmann | # (at your option) any later version.
|
10 | cec9845c | Michael Hanselmann | #
|
11 | cec9845c | Michael Hanselmann | # This program is distributed in the hope that it will be useful, but
|
12 | cec9845c | Michael Hanselmann | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | cec9845c | Michael Hanselmann | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | cec9845c | Michael Hanselmann | # General Public License for more details.
|
15 | cec9845c | Michael Hanselmann | #
|
16 | cec9845c | Michael Hanselmann | # You should have received a copy of the GNU General Public License
|
17 | cec9845c | Michael Hanselmann | # along with this program; if not, write to the Free Software
|
18 | cec9845c | Michael Hanselmann | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | cec9845c | Michael Hanselmann | # 02110-1301, USA.
|
20 | cec9845c | Michael Hanselmann | |
21 | cec9845c | Michael Hanselmann | |
22 | cec9845c | Michael Hanselmann | """QA configuration.
|
23 | cec9845c | Michael Hanselmann |
|
24 | cec9845c | Michael Hanselmann | """
|
25 | cec9845c | Michael Hanselmann | |
26 | cec9845c | Michael Hanselmann | |
27 | cec9845c | Michael Hanselmann | import yaml |
28 | cec9845c | Michael Hanselmann | |
29 | cec9845c | Michael Hanselmann | import qa_error |
30 | cec9845c | Michael Hanselmann | |
31 | cec9845c | Michael Hanselmann | |
32 | cec9845c | Michael Hanselmann | cfg = None
|
33 | cec9845c | Michael Hanselmann | options = None
|
34 | cec9845c | Michael Hanselmann | |
35 | cec9845c | Michael Hanselmann | |
36 | cec9845c | Michael Hanselmann | def Load(path): |
37 | cec9845c | Michael Hanselmann | """Loads the passed configuration file.
|
38 | cec9845c | Michael Hanselmann |
|
39 | cec9845c | Michael Hanselmann | """
|
40 | cec9845c | Michael Hanselmann | global cfg
|
41 | cec9845c | Michael Hanselmann | |
42 | cec9845c | Michael Hanselmann | f = open(path, 'r') |
43 | cec9845c | Michael Hanselmann | try:
|
44 | cec9845c | Michael Hanselmann | cfg = yaml.load(f.read()) |
45 | cec9845c | Michael Hanselmann | finally:
|
46 | cec9845c | Michael Hanselmann | f.close() |
47 | cec9845c | Michael Hanselmann | |
48 | cec9845c | Michael Hanselmann | Validate() |
49 | cec9845c | Michael Hanselmann | |
50 | cec9845c | Michael Hanselmann | |
51 | cec9845c | Michael Hanselmann | def Validate(): |
52 | cec9845c | Michael Hanselmann | if len(cfg['nodes']) < 1: |
53 | cec9845c | Michael Hanselmann | raise qa_error.Error("Need at least one node") |
54 | cec9845c | Michael Hanselmann | if len(cfg['instances']) < 1: |
55 | cec9845c | Michael Hanselmann | raise qa_error.Error("Need at least one instance") |
56 | cec9845c | Michael Hanselmann | |
57 | cec9845c | Michael Hanselmann | |
58 | cec9845c | Michael Hanselmann | def get(name, default=None): |
59 | cec9845c | Michael Hanselmann | return cfg.get(name, default)
|
60 | cec9845c | Michael Hanselmann | |
61 | cec9845c | Michael Hanselmann | |
62 | cec9845c | Michael Hanselmann | def TestEnabled(test): |
63 | cec9845c | Michael Hanselmann | """Returns True if the given test is enabled."""
|
64 | cec9845c | Michael Hanselmann | return cfg.get('tests', {}).get(test, False) |
65 | cec9845c | Michael Hanselmann | |
66 | cec9845c | Michael Hanselmann | |
67 | cec9845c | Michael Hanselmann | def GetMasterNode(): |
68 | cec9845c | Michael Hanselmann | return cfg['nodes'][0] |
69 | cec9845c | Michael Hanselmann | |
70 | cec9845c | Michael Hanselmann | |
71 | cec9845c | Michael Hanselmann | def AcquireInstance(): |
72 | cec9845c | Michael Hanselmann | """Returns an instance which isn't in use.
|
73 | cec9845c | Michael Hanselmann |
|
74 | cec9845c | Michael Hanselmann | """
|
75 | cec9845c | Michael Hanselmann | # Filter out unwanted instances
|
76 | cec9845c | Michael Hanselmann | tmp_flt = lambda inst: not inst.get('_used', False) |
77 | cec9845c | Michael Hanselmann | instances = filter(tmp_flt, cfg['instances']) |
78 | cec9845c | Michael Hanselmann | del tmp_flt
|
79 | cec9845c | Michael Hanselmann | |
80 | cec9845c | Michael Hanselmann | if len(instances) == 0: |
81 | cec9845c | Michael Hanselmann | raise qa_error.OutOfInstancesError("No instances left") |
82 | cec9845c | Michael Hanselmann | |
83 | cec9845c | Michael Hanselmann | inst = instances[0]
|
84 | cec9845c | Michael Hanselmann | inst['_used'] = True |
85 | cec9845c | Michael Hanselmann | return inst
|
86 | cec9845c | Michael Hanselmann | |
87 | cec9845c | Michael Hanselmann | |
88 | cec9845c | Michael Hanselmann | def ReleaseInstance(inst): |
89 | cec9845c | Michael Hanselmann | inst['_used'] = False |
90 | cec9845c | Michael Hanselmann | |
91 | cec9845c | Michael Hanselmann | |
92 | cec9845c | Michael Hanselmann | def AcquireNode(exclude=None): |
93 | cec9845c | Michael Hanselmann | """Returns the least used node.
|
94 | cec9845c | Michael Hanselmann |
|
95 | cec9845c | Michael Hanselmann | """
|
96 | cec9845c | Michael Hanselmann | master = GetMasterNode() |
97 | cec9845c | Michael Hanselmann | |
98 | cec9845c | Michael Hanselmann | # Filter out unwanted nodes
|
99 | cec9845c | Michael Hanselmann | # TODO: Maybe combine filters
|
100 | cec9845c | Michael Hanselmann | if exclude is None: |
101 | cec9845c | Michael Hanselmann | nodes = cfg['nodes'][:]
|
102 | 4b62db14 | Michael Hanselmann | elif isinstance(exclude, (list, tuple)): |
103 | 4b62db14 | Michael Hanselmann | nodes = filter(lambda node: node not in exclude, cfg['nodes']) |
104 | cec9845c | Michael Hanselmann | else:
|
105 | cec9845c | Michael Hanselmann | nodes = filter(lambda node: node != exclude, cfg['nodes']) |
106 | cec9845c | Michael Hanselmann | |
107 | cec9845c | Michael Hanselmann | tmp_flt = lambda node: node.get('_added', False) or node == master |
108 | cec9845c | Michael Hanselmann | nodes = filter(tmp_flt, nodes)
|
109 | cec9845c | Michael Hanselmann | del tmp_flt
|
110 | cec9845c | Michael Hanselmann | |
111 | cec9845c | Michael Hanselmann | if len(nodes) == 0: |
112 | cec9845c | Michael Hanselmann | raise qa_error.OutOfNodesError("No nodes left") |
113 | cec9845c | Michael Hanselmann | |
114 | cec9845c | Michael Hanselmann | # Get node with least number of uses
|
115 | cec9845c | Michael Hanselmann | def compare(a, b): |
116 | cec9845c | Michael Hanselmann | result = cmp(a.get('_count', 0), b.get('_count', 0)) |
117 | cec9845c | Michael Hanselmann | if result == 0: |
118 | cec9845c | Michael Hanselmann | result = cmp(a['primary'], b['primary']) |
119 | cec9845c | Michael Hanselmann | return result
|
120 | cec9845c | Michael Hanselmann | |
121 | cec9845c | Michael Hanselmann | nodes.sort(cmp=compare) |
122 | cec9845c | Michael Hanselmann | |
123 | cec9845c | Michael Hanselmann | node = nodes[0]
|
124 | cec9845c | Michael Hanselmann | node['_count'] = node.get('_count', 0) + 1 |
125 | cec9845c | Michael Hanselmann | return node
|
126 | cec9845c | Michael Hanselmann | |
127 | cec9845c | Michael Hanselmann | |
128 | cec9845c | Michael Hanselmann | def ReleaseNode(node): |
129 | cec9845c | Michael Hanselmann | node['_count'] = node.get('_count', 0) - 1 |