Chromium Code Reviews| 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 | |
| 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 argparse | |
| 13 import json | |
| 14 import os | |
| 15 import settings | |
| 16 import subprocess | |
| 17 import verifier | |
| 18 | |
| 19 | |
| 20 class Config: | |
| 21 """Describes the machine states, actions, and test cases.""" | |
|
robertshield
2013/07/31 18:28:53
Please provide here a very brief description of a
sukolsak
2013/07/31 19:35:36
Done.
| |
| 22 | |
| 23 def __init__(self): | |
| 24 self.states = {} | |
| 25 self.actions = {} | |
| 26 self.tests = [] | |
| 27 | |
| 28 | |
| 29 def MergeProperties(current_property, new_property): | |
| 30 """Merges the new Property object into the current Property object | |
|
robertshield
2013/07/31 18:28:53
Is this actually a "Property" object, or is it rat
sukolsak
2013/07/31 19:35:36
It is a Property object. It is also a dictionary.
| |
| 31 | |
| 32 Args: | |
| 33 current_property: The Property object to be modified. | |
| 34 new_property: The new Property object. | |
| 35 """ | |
| 36 for key, value in new_property.iteritems(): | |
| 37 if key not in current_property: | |
| 38 current_property[key] = value | |
| 39 else: | |
| 40 assert(isinstance(current_property[key], dict) and | |
| 41 isinstance(value, dict)) | |
| 42 # This merges two dictionaries together. In case there are properties with | |
| 43 # the same name, the latter will override the former. | |
| 44 current_property[key] = dict( | |
| 45 current_property[key].items() + value.items()) | |
| 46 | |
| 47 | |
| 48 def ParseProperty(directory, property_filename): | |
| 49 """Parses a .prop file. | |
| 50 | |
| 51 Args: | |
| 52 property_filename: A Property filename. | |
| 53 directory: The directory where the Config file and all Property files | |
| 54 reside in. | |
| 55 | |
| 56 Returns: | |
| 57 A Property object. | |
| 58 """ | |
| 59 property_path = os.path.join(directory, property_filename) | |
| 60 with open(property_path, "r") as property_file: | |
| 61 return json.load(property_file) | |
| 62 | |
| 63 | |
| 64 def ParseProperties(directory, property_filenames): | |
| 65 """Parses an array of .prop files. | |
| 66 | |
| 67 Args: | |
| 68 property_filenames: An array of Property filenames. | |
| 69 directory: The directory where the Config file and all Property files | |
| 70 reside in. | |
| 71 | |
| 72 Returns: | |
| 73 A Property object created by merging all Property objects specified in | |
| 74 the array. | |
| 75 """ | |
| 76 current_property = {} | |
| 77 for property_filename in property_filenames: | |
| 78 new_property = ParseProperty(directory, property_filename) | |
| 79 MergeProperties(current_property, new_property) | |
| 80 return current_property | |
| 81 | |
| 82 | |
| 83 def ParseConfig(config_filename): | |
| 84 """Parses a .config file. | |
| 85 | |
| 86 Args: | |
| 87 config_filename: A Config filename. | |
| 88 | |
| 89 Returns: | |
| 90 A config object. | |
| 91 """ | |
| 92 config = Config() | |
| 93 | |
| 94 with open(config_filename, "r") as config_file: | |
| 95 config_data = json.load(config_file) | |
| 96 directory = os.path.dirname(os.path.abspath(config_filename)) | |
| 97 | |
| 98 for state_name, state_property_filenames in config_data["states"]: | |
| 99 config.states[state_name] = ParseProperties(directory, | |
| 100 state_property_filenames) | |
| 101 for action_name, action_command in config_data["actions"]: | |
| 102 config.actions[action_name] = action_command | |
| 103 config.tests = config_data["tests"] | |
| 104 return config | |
| 105 | |
| 106 | |
| 107 def VerifyState(config, state): | |
| 108 """Verifies that the current machine states match the given machine states. | |
| 109 | |
| 110 Args: | |
| 111 config: A Config object. | |
| 112 state: The current state. | |
| 113 """ | |
| 114 print settings.PRINT_STATE_PREFIX + state | |
|
robertshield
2013/07/31 18:28:53
These are good to have for now, but when this star
sukolsak
2013/07/31 19:35:36
Done.
| |
| 115 verifier.Verify(config.states[state]) | |
| 116 | |
| 117 | |
| 118 def RunCommand(command): | |
| 119 print settings.PRINT_COMMAND_PREFIX + command | |
| 120 subprocess.call(command, shell=True) | |
| 121 | |
| 122 | |
| 123 def RunResetCommand(): | |
| 124 print settings.PRINT_COMMAND_PREFIX + "Reset" | |
| 125 # TODO(sukolsak): Need to figure how exactly we want to reset. | |
| 126 | |
| 127 | |
| 128 def Test(config): | |
| 129 """Tests the installer using the given Config object. | |
| 130 | |
| 131 Args: | |
| 132 config: A Config object. | |
| 133 """ | |
| 134 for test in config.tests: | |
| 135 print settings.PRINT_TEST_PREFIX + " -> ".join(test) | |
| 136 | |
| 137 # A Test object is an array of alternating states and actions. | |
| 138 # The array starts and ends with states. Therefore, the length must be odd. | |
| 139 assert(len(test) % 2 == 1) | |
| 140 | |
| 141 RunResetCommand() | |
| 142 | |
| 143 current_state = test[0] | |
| 144 VerifyState(config, current_state) | |
| 145 # TODO(sukolsak): Quit the test early if VerifyState fails at any point. | |
| 146 | |
| 147 for i in range(1, len(test), 2): | |
| 148 action = test[i] | |
| 149 RunCommand(config.actions[action]) | |
| 150 | |
| 151 current_state = test[i + 1] | |
| 152 VerifyState(config, current_state) | |
| 153 | |
| 154 | |
| 155 def main(): | |
| 156 parser = argparse.ArgumentParser(description="Test the installer.") | |
| 157 parser.add_argument("config_filename", help="the config file") | |
| 158 args = parser.parse_args() | |
| 159 | |
| 160 config = ParseConfig(args.config_filename) | |
| 161 Test(config) | |
| 162 | |
| 163 | |
| 164 if __name__ == "__main__": | |
| 165 main() | |
| OLD | NEW |