Chromium Code Reviews| Index: chrome/test/mini_installer/test_installer.py |
| diff --git a/chrome/test/mini_installer/test_installer.py b/chrome/test/mini_installer/test_installer.py |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d0b310dc7a5b6fac30b29c97e5e4b5b54014b8b5 |
| --- /dev/null |
| +++ b/chrome/test/mini_installer/test_installer.py |
| @@ -0,0 +1,165 @@ |
| +# Copyright 2013 The Chromium Authors. All rights reserved. |
| +# Use of this source code is governed by a BSD-style license that can be |
| +# found in the LICENSE file. |
| + |
| +"""This script tests the installer with a series of test cases specified in |
| +the config file. For each test case, it checks that the machine states after |
| +the execution of each command match the expected machine states. |
| + |
| +For more details take a look at the design documentation at http://goo.gl/Q0rGM6 |
| +""" |
| + |
| +import argparse |
| +import json |
| +import os |
| +import settings |
| +import subprocess |
| +import verifier |
| + |
| + |
| +class Config: |
| + """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.
|
| + |
| + def __init__(self): |
| + self.states = {} |
| + self.actions = {} |
| + self.tests = [] |
| + |
| + |
| +def MergeProperties(current_property, new_property): |
| + """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.
|
| + |
| + Args: |
| + current_property: The Property object to be modified. |
| + new_property: The new Property object. |
| + """ |
| + for key, value in new_property.iteritems(): |
| + if key not in current_property: |
| + current_property[key] = value |
| + else: |
| + assert(isinstance(current_property[key], dict) and |
| + isinstance(value, dict)) |
| + # This merges two dictionaries together. In case there are properties with |
| + # the same name, the latter will override the former. |
| + current_property[key] = dict( |
| + current_property[key].items() + value.items()) |
| + |
| + |
| +def ParseProperty(directory, property_filename): |
| + """Parses a .prop file. |
| + |
| + Args: |
| + property_filename: A Property filename. |
| + directory: The directory where the Config file and all Property files |
| + reside in. |
| + |
| + Returns: |
| + A Property object. |
| + """ |
| + property_path = os.path.join(directory, property_filename) |
| + with open(property_path, "r") as property_file: |
| + return json.load(property_file) |
| + |
| + |
| +def ParseProperties(directory, property_filenames): |
| + """Parses an array of .prop files. |
| + |
| + Args: |
| + property_filenames: An array of Property filenames. |
| + directory: The directory where the Config file and all Property files |
| + reside in. |
| + |
| + Returns: |
| + A Property object created by merging all Property objects specified in |
| + the array. |
| + """ |
| + current_property = {} |
| + for property_filename in property_filenames: |
| + new_property = ParseProperty(directory, property_filename) |
| + MergeProperties(current_property, new_property) |
| + return current_property |
| + |
| + |
| +def ParseConfig(config_filename): |
| + """Parses a .config file. |
| + |
| + Args: |
| + config_filename: A Config filename. |
| + |
| + Returns: |
| + A config object. |
| + """ |
| + config = Config() |
| + |
| + with open(config_filename, "r") as config_file: |
| + config_data = json.load(config_file) |
| + directory = os.path.dirname(os.path.abspath(config_filename)) |
| + |
| + for state_name, state_property_filenames in config_data["states"]: |
| + config.states[state_name] = ParseProperties(directory, |
| + state_property_filenames) |
| + for action_name, action_command in config_data["actions"]: |
| + config.actions[action_name] = action_command |
| + config.tests = config_data["tests"] |
| + return config |
| + |
| + |
| +def VerifyState(config, state): |
| + """Verifies that the current machine states match the given machine states. |
| + |
| + Args: |
| + config: A Config object. |
| + state: The current state. |
| + """ |
| + 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.
|
| + verifier.Verify(config.states[state]) |
| + |
| + |
| +def RunCommand(command): |
| + print settings.PRINT_COMMAND_PREFIX + command |
| + subprocess.call(command, shell=True) |
| + |
| + |
| +def RunResetCommand(): |
| + print settings.PRINT_COMMAND_PREFIX + "Reset" |
| + # TODO(sukolsak): Need to figure how exactly we want to reset. |
| + |
| + |
| +def Test(config): |
| + """Tests the installer using the given Config object. |
| + |
| + Args: |
| + config: A Config object. |
| + """ |
| + for test in config.tests: |
| + print settings.PRINT_TEST_PREFIX + " -> ".join(test) |
| + |
| + # A Test object is an array of alternating states and actions. |
| + # The array starts and ends with states. Therefore, the length must be odd. |
| + assert(len(test) % 2 == 1) |
| + |
| + RunResetCommand() |
| + |
| + current_state = test[0] |
| + VerifyState(config, current_state) |
| + # TODO(sukolsak): Quit the test early if VerifyState fails at any point. |
| + |
| + for i in range(1, len(test), 2): |
| + action = test[i] |
| + RunCommand(config.actions[action]) |
| + |
| + current_state = test[i + 1] |
| + VerifyState(config, current_state) |
| + |
| + |
| +def main(): |
| + parser = argparse.ArgumentParser(description="Test the installer.") |
| + parser.add_argument("config_filename", help="the config file") |
| + args = parser.parse_args() |
| + |
| + config = ParseConfig(args.config_filename) |
| + Test(config) |
| + |
| + |
| +if __name__ == "__main__": |
| + main() |