OLD | NEW |
---|---|
(Empty) | |
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 """This script tests the installer with a series of test cases specified in | |
Mathieu
2013/08/05 18:46:13
You're doing it right below. One-line description,
| |
6 the config file. For each test case, it checks that the machine states after | |
7 the execution of each command match the expected machine states. | |
8 | |
9 For more details take a look at the design documentation at http://goo.gl/Q0rGM6 | |
10 """ | |
11 | |
12 import json | |
13 import optparse | |
14 import os | |
15 import subprocess | |
16 | |
17 import settings | |
18 import verifier | |
19 | |
20 | |
21 class Config: | |
22 """Describes the machine states, actions, and test cases. | |
23 | |
24 A state is a dictionary where each key is a verifier's name and the | |
25 associated value is the input to that verifier. An action is a shorthand for | |
26 a command. A test is array of alternating state names and action names, | |
27 starting and ending with state names. An instance of this class stores a map | |
28 from state names to state objects, a map from action names to commands, and | |
29 an array of test objects. | |
30 """ | |
31 def __init__(self): | |
32 self.states = {} | |
33 self.actions = {} | |
34 self.tests = [] | |
35 | |
36 | |
37 def MergeProperties(current_property, new_property): | |
Mathieu
2013/08/05 18:46:13
Googling quickly for merging dicts, I found: http:
sukolsak
2013/08/05 20:34:46
No, it wouldn't work here. We are doing it differe
Mathieu
2013/08/05 21:03:26
Thanks for clarifying!
| |
38 """Merges the new Property object into the current Property object | |
Mathieu
2013/08/05 18:46:13
nit: period.
Mathieu
2013/08/05 18:46:13
This makes it seem like Property is a class. How a
sukolsak
2013/08/05 20:34:46
Done.
sukolsak
2013/08/05 20:34:46
Done.
Mathieu
2013/08/05 21:03:26
Please also change the references to "Property obj
sukolsak
2013/08/05 21:57:00
Would changing references to "Property object" in
Mathieu
2013/08/05 22:06:10
Just the comments is fine. Generally, you want to
| |
39 | |
40 Args: | |
41 current_property: The Property object to be modified. | |
42 new_property: The new Property object. | |
43 """ | |
44 for key, value in new_property.iteritems(): | |
45 if key not in current_property: | |
46 current_property[key] = value | |
47 else: | |
48 assert(isinstance(current_property[key], dict) and | |
49 isinstance(value, dict)) | |
50 # This merges two dictionaries together. In case there are properties with | |
51 # the same name, the latter will override the former. | |
52 current_property[key] = dict( | |
53 current_property[key].items() + value.items()) | |
54 | |
55 | |
56 def ParseProperty(directory, property_filename): | |
Mathieu
2013/08/05 18:46:13
I don't think you need this function. See below.
sukolsak
2013/08/05 20:34:46
Done.
| |
57 """Parses a .prop file. | |
58 | |
59 Args: | |
60 property_filename: A Property filename. | |
61 directory: The directory where the Config file and all Property files | |
62 reside in. | |
63 | |
64 Returns: | |
65 A Property object. | |
66 """ | |
67 property_path = os.path.join(directory, property_filename) | |
68 with open(property_path, "r") as property_file: | |
69 return json.load(property_file) | |
70 | |
71 | |
72 def ParseProperties(directory, property_filenames): | |
Mathieu
2013/08/05 18:46:13
could you inline ParseProperty in ParseProperties?
sukolsak
2013/08/05 20:34:46
Done.
| |
73 """Parses an array of .prop files. | |
74 | |
75 Args: | |
76 property_filenames: An array of Property filenames. | |
77 directory: The directory where the Config file and all Property files | |
78 reside in. | |
79 | |
80 Returns: | |
81 A Property object created by merging all Property objects specified in | |
82 the array. | |
83 """ | |
84 current_property = {} | |
85 for property_filename in property_filenames: | |
86 new_property = ParseProperty(directory, property_filename) | |
87 MergeProperties(current_property, new_property) | |
88 return current_property | |
89 | |
90 | |
91 def ParseConfig(config_filename): | |
92 """Parses a .config file. | |
93 | |
94 Args: | |
95 config_filename: A Config filename. | |
96 | |
97 Returns: | |
98 A config object. | |
99 """ | |
100 config = Config() | |
101 | |
102 with open(config_filename, "r") as config_file: | |
103 config_data = json.load(config_file) | |
Mathieu
2013/08/05 18:46:13
as before, I would just have
config_data = json.l
gab
2013/08/05 19:06:20
The idea was that the exception would bubble up th
gab
2013/08/06 14:12:56
ping mathp
Mathieu
2013/08/06 14:16:07
It's fine. I was just mentioning this to make sure
| |
104 directory = os.path.dirname(os.path.abspath(config_filename)) | |
105 | |
106 for state_name, state_property_filenames in config_data["states"]: | |
Mathieu
2013/08/05 18:46:13
single quotes, throughout all your files.
sukolsak
2013/08/05 20:34:46
Done.
| |
107 config.states[state_name] = ParseProperties(directory, | |
108 state_property_filenames) | |
109 for action_name, action_command in config_data["actions"]: | |
110 config.actions[action_name] = action_command | |
111 config.tests = config_data["tests"] | |
Mathieu
2013/08/05 18:46:13
could you bring this at the top after the creation
sukolsak
2013/08/05 20:34:46
Done.
| |
112 return config | |
113 | |
114 | |
115 def VerifyState(config, state): | |
116 """Verifies that the current machine states match the given machine states. | |
117 | |
118 Args: | |
119 config: A Config object. | |
120 state: The current state. | |
121 """ | |
122 # TODO(sukolsak): Think of ways of preserving the log when the test fails but | |
123 # not printing these when the test passes. | |
124 print settings.PRINT_STATE_PREFIX + state | |
125 verifier.Verify(config.states[state]) | |
126 | |
127 | |
128 def RunCommand(command): | |
129 print settings.PRINT_COMMAND_PREFIX + command | |
130 subprocess.call(command, shell=True) | |
131 | |
132 | |
133 def RunResetCommand(): | |
134 print settings.PRINT_COMMAND_PREFIX + "Reset" | |
135 # TODO(sukolsak): Need to figure how exactly we want to reset. | |
136 | |
137 | |
138 def Test(config): | |
139 """Tests the installer using the given Config object. | |
140 | |
141 Args: | |
142 config: A Config object. | |
143 """ | |
144 for test in config.tests: | |
145 print settings.PRINT_TEST_PREFIX + " -> ".join(test) | |
146 | |
147 # A Test object is an array of alternating state names and action names. | |
148 # The array starts and ends with states. Therefore, the length must be odd. | |
149 assert(len(test) % 2 == 1) | |
150 | |
151 RunResetCommand() | |
152 | |
153 current_state = test[0] | |
154 VerifyState(config, current_state) | |
155 # TODO(sukolsak): Quit the test early if VerifyState fails at any point. | |
156 | |
157 for i in range(1, len(test), 2): | |
158 action = test[i] | |
159 RunCommand(config.actions[action]) | |
160 | |
161 current_state = test[i + 1] | |
162 VerifyState(config, current_state) | |
163 | |
164 | |
165 def main(): | |
166 usage = "usage: %prog configfile" | |
167 parser = optparse.OptionParser(usage, description="Test the installer.") | |
168 (options, args) = parser.parse_args() | |
169 if len(args) != 1: | |
170 parser.error("incorrect number of arguments") | |
171 | |
172 config = ParseConfig(args[0]) | |
173 Test(config) | |
174 | |
175 | |
176 if __name__ == "__main__": | |
177 main() | |
OLD | NEW |