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 | |
6 specified in the config file. For each test case, it checks that | |
7 the machine states after the execution of each command match the | |
8 expected machine states. | |
9 | |
10 Read the design documentation at http://goo.gl/Q0rGM6 | |
gab
2013/07/31 13:45:16
s/Read/For more details take a look at
sukolsak
2013/07/31 18:20:30
Done.
| |
11 """ | |
12 | |
13 import argparse | |
14 import json | |
15 import os | |
16 import settings | |
17 import subprocess | |
18 import verifier | |
19 | |
20 | |
21 class Config: | |
22 """Describes the machine states, actions, and test cases.""" | |
23 | |
24 def __init__(self): | |
25 self.states = {} | |
26 self.actions = {} | |
27 self.tests = [] | |
28 | |
29 | |
30 def MergeProperties(current_property, new_property): | |
31 """Merges the new Property object into the current Property object | |
32 | |
33 Args: | |
34 current_property: The Property object to be modified. | |
35 new_property: The new Property object. | |
36 """ | |
37 for key, value in new_property.iteritems(): | |
38 if key not in current_property: | |
39 current_property[key] = value | |
40 else: | |
41 assert(isinstance(current_property[key], dict) and | |
42 isinstance(value, dict)) | |
gab
2013/07/31 13:45:16
fix indent (i.e., wrap to '(' above)
sukolsak
2013/07/31 18:20:30
Done.
| |
43 current_property[key] = dict( | |
44 current_property[key].items() + value.items()) | |
gab
2013/07/31 13:45:16
Does this merge (i.e., override) properties with t
sukolsak
2013/07/31 18:20:30
Yes. Comment added.
gab
2013/08/01 20:30:41
Sounds good, can you make it clear in the spec the
sukolsak
2013/08/02 22:59:55
Done.
| |
45 | |
46 | |
47 def ParseProperty(property_filename, directory): | |
gab
2013/07/31 13:45:16
I suggest flipping the arguments here since native
sukolsak
2013/07/31 18:20:30
Done.
| |
48 """Parses a .prop file. | |
49 | |
50 Args: | |
51 property_filename: A Property filename. | |
52 directory: The directory where the Config file and | |
53 all Property files reside in. | |
54 | |
55 Returns: | |
56 A Property object. | |
57 """ | |
58 property_path = os.path.join(directory, property_filename) | |
59 with open(property_path, "r") as property_file: | |
gab
2013/07/31 13:45:16
Any failure handling required here? Or will an exc
sukolsak
2013/07/31 18:20:30
The exception IOError will be bubbled back up and
| |
60 return json.load(property_file) | |
61 | |
62 | |
63 def ParseProperties(property_filenames, directory): | |
gab
2013/07/31 13:45:16
Same comment here about argument order
sukolsak
2013/07/31 18:20:30
Done.
| |
64 """Parses an array of .prop files. | |
65 | |
66 Args: | |
67 property_filenames: An array of Property filenames. | |
68 directory: The directory where the Config file and | |
69 all Property files reside in. | |
70 | |
71 Returns: | |
72 A Property object created by merging all Property objects | |
73 specified in the array. | |
74 """ | |
75 current_property = {} | |
76 for property_filename in property_filenames: | |
77 new_property = ParseProperty(property_filename, directory) | |
78 MergeProperties(current_property, new_property) | |
79 return current_property | |
80 | |
81 | |
82 def ParseConfig(config_filename): | |
83 """Parses a .config file. | |
84 | |
85 Args: | |
86 config_filename: A Config filename. | |
87 | |
88 Returns: | |
89 A config object. | |
90 """ | |
91 config = Config() | |
92 | |
93 with open(config_filename, "r") as config_file: | |
gab
2013/07/31 13:45:16
Does config_filename need to be absolute?
Either
sukolsak
2013/07/31 18:20:30
It can be relative or absolute. I think the open o
| |
94 config_data = json.load(config_file) | |
95 directory = os.path.dirname(os.path.abspath(config_filename)) | |
96 | |
97 for state_name, state_property_filenames in config_data["states"]: | |
98 config.states[state_name] = ParseProperties(state_property_filenames, | |
99 directory) | |
gab
2013/07/31 13:45:16
fix indent
sukolsak
2013/07/31 18:20:30
Done.
| |
100 for action_name, action_command in config_data["actions"]: | |
101 config.actions[action_name] = action_command | |
102 config.tests = config_data["tests"] | |
103 return config | |
104 | |
105 | |
106 def VerifyState(config, state): | |
107 """Verifies that the current machine states match the given machine states. | |
108 | |
109 Args: | |
110 config: A Config object. | |
111 state: The current state. | |
112 """ | |
113 print settings.PRINT_STATE_PREFIX + state | |
114 verifier.Verify(config.states[state]) | |
115 | |
116 | |
117 def RunCommand(command): | |
118 print settings.PRINT_COMMAND_PREFIX + command | |
119 subprocess.call(command, shell=True) | |
120 | |
121 | |
122 def RunResetCommand(): | |
123 print settings.PRINT_COMMAND_PREFIX + "Reset" | |
124 # TODO(sukolsak): Need to figure how exactly we want to reset. | |
125 | |
126 | |
127 def Test(config): | |
128 """Tests the installer using the given Config object. | |
129 | |
130 Args: | |
131 config: A Config object. | |
132 """ | |
133 for test in config.tests: | |
134 print settings.PRINT_TEST_PREFIX + " -> ".join(test) | |
135 | |
136 # A Test object is an array of alternating states and actions. | |
137 # The array starts and ends with states. Therefore, the length must be odd. | |
138 assert(len(test) % 2 == 1) | |
139 | |
140 RunResetCommand() | |
141 | |
142 current_state = test[0] | |
143 VerifyState(config, current_state) | |
gab
2013/07/31 13:45:16
Add a TODO to quit the test early if VerifyState f
sukolsak
2013/07/31 18:20:30
Done.
| |
144 | |
145 for i in range(1, len(test), 2): | |
146 action = test[i] | |
147 RunCommand(config.actions[action]) | |
148 | |
149 current_state = test[i + 1] | |
150 VerifyState(config, current_state) | |
151 | |
152 | |
153 def main(): | |
154 parser = argparse.ArgumentParser(description="Test the installer.") | |
155 parser.add_argument("config_filename", help="the config file") | |
156 args = parser.parse_args() | |
157 | |
158 config = ParseConfig(args.config_filename) | |
159 Test(config) | |
160 | |
161 | |
162 if __name__ == "__main__": | |
163 main() | |
OLD | NEW |