Skip to contentSkip to conversation

KVM test: introduce Params, a dict-like object containing test parameters

PATCH New Michael Goldish 1 participant 4 messages Dec 27, 2010 KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 KVM test: introduce Params, a dict-like object containing test parameters

This object should replace get_sub_dict_names() and get_sub_dict().

---
 client/tests/kvm/kvm_utils.py |   38 +++++++++++++++++++++++++++++++++++++-
 1 files changed, 37 insertions(+), 1 deletions(-)
client/tests/kvm/kvm_utils.py +37 -1
index 57c9951..1e32e30 100644
@@ -5,7 +5,7 @@ KVM test utility functions.
 """
 
 import time, string, random, socket, os, signal, re, logging, commands, cPickle
-import fcntl, shelve, ConfigParser, rss_file_transfer, threading, sys
+import fcntl, shelve, ConfigParser, rss_file_transfer, threading, sys, UserDict
 from autotest_lib.client.bin import utils, os_dep
 from autotest_lib.client.common_lib import error, logging_config
 import kvm_subprocess
@@ -97,6 +97,42 @@ def get_sub_dict_names(dict, keyword):
         return []
 
 
+class Params(UserDict.IterableUserDict):
+    """
+    A dict-like object passed to every test.
+    """
+    def objects(self, key):
+        """
+        Return the names of objects defined using a given key.
+
+        @param key: The name of the key whose value lists the objects
+                (e.g. 'nics').
+        """
+        return self.get(key, "").split()
+
+
+    def object_params(self, obj_name):
+        """
+        Return a dict-like object containing the parameters of an individual
+        object.
+
+        This method behaves as follows: the suffix '_' + obj_name is removed
+        from all key names that have it.  Other key names are left unchanged.
+        The values of keys with the suffix overwrite the values of their
+        suffixless versions.
+
+        @param obj_name: The name of the object (objects are listed by the
+                objects() method).
+        """
+        suffix = "_" + obj_name
+        new_dict = self.copy()
+        for key in self:
+            if key.endswith(suffix):
+                new_key = key.split(suffix)[0]
+                new_dict[new_key] = self[key]
+        return new_dict
+
+
 # Functions related to MAC/IP addresses
 
 def _open_mac_pool(lock_mode):
Signature · 1 line
1.7.3.4
#1 lore raw

PATCH 2/4 KVM test: kvm.py: convert the params dict to a kvm_utils.Params object

Also bump the env version so old env files will not be used.

---
 client/tests/kvm/kvm.py |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
client/tests/kvm/kvm.py +4 -1
index dc00edd..ceee88f 100644
@@ -21,9 +21,12 @@ class kvm(test.test):
             (Online doc - Getting started with KVM testing)
     """
     version = 1
-    env_version = 0
+    env_version = 1
 
     def run_once(self, params):
+        # Convert params to a Params object
+        params = kvm_utils.Params(params)
+
         # Report the parameters we've received and write them as keyvals
         logging.debug("Test parameters:")
         keys = params.keys()
Signature · 1 line
1.7.3.4
#2 lore raw

PATCH 3/4 KVM test: use the new Params methods instead of get_sub_dict_.*()

In reply to Michael Goldish
---
 client/tests/kvm/kvm_preprocessing.py              |   14 ++---
 client/tests/kvm/kvm_vm.py                         |   58 ++++++++++----------
 client/tests/kvm/tests/pci_hotplug.py              |    4 +-
 client/tests/kvm/tests/physical_resources_check.py |    8 ++--
 client/tests/kvm/tests/unittest.py                 |    4 +-
 client/tests/kvm/tests/whql_submission.py          |   30 +++++-----
 6 files changed, 57 insertions(+), 61 deletions(-)
client/tests/kvm/kvm_preprocessing.py +6 -8
index 90382cc..25c4e5f 100644
@@ -183,13 +183,11 @@ def process(test, params, env, image_func, vm_func):
     @param vm_func: A function to call for each VM.
     """
     # Get list of VMs specified for this test
-    vm_names = kvm_utils.get_sub_dict_names(params, "vms")
-    for vm_name in vm_names:
-        vm_params = kvm_utils.get_sub_dict(params, vm_name)
+    for vm_name in params.objects("vms"):
+        vm_params = params.object_params(vm_name)
         # Get list of images specified for this VM
-        image_names = kvm_utils.get_sub_dict_names(vm_params, "images")
-        for image_name in image_names:
-            image_params = kvm_utils.get_sub_dict(vm_params, image_name)
+        for image_name in vm_params.objects("images"):
+            image_params = vm_params.object_params(image_name)
             # Call image_func for each image
             image_func(test, image_params)
         # Call vm_func for each vm
@@ -226,7 +224,7 @@ def preprocess(test, params, env):
                 env["tcpdump"].get_output()))
 
     # Destroy and remove VMs that are no longer needed in the environment
-    requested_vms = kvm_utils.get_sub_dict_names(params, "vms")
+    requested_vms = params.objects("vms")
     for key in env.keys():
         vm = env[key]
         if not kvm_utils.is_vm(vm):
@@ -372,7 +370,7 @@ def postprocess_on_error(test, params, env):
     @param params: A dict containing all VM and image parameters.
     @param env: The environment (a dict-like object).
     """
-    params.update(kvm_utils.get_sub_dict(params, "on_error"))
+    params.update(params.object_params("on_error"))
 
 
 def _update_address_cache(address_cache, line):
client/tests/kvm/kvm_vm.py +28 -30

152 lines not on this page · raw message

client/tests/kvm/tests/pci_hotplug.py +2 -2
index 727ee62..27b81de 100644
@@ -51,7 +51,7 @@ def run_pci_hotplug(test, params, env):
         if test_type == "nic":
             pci_add_cmd = "pci_add pci_addr=auto nic model=%s" % tested_model
         elif test_type == "block":
-            image_params = kvm_utils.get_sub_dict(params, "stg")
+            image_params = params.object_params("stg")
             image_filename = kvm_vm.get_image_filename(image_params,
                                                        test.bindir)
             pci_add_cmd = ("pci_add pci_addr=auto storage file=%s,if=%s" %
@@ -73,7 +73,7 @@ def run_pci_hotplug(test, params, env):
             pci_add_cmd = "device_add id=%s,driver=%s" % (id, tested_model)
 
         elif test_type == "block":
-            image_params = kvm_utils.get_sub_dict(params, "stg")
+            image_params = params.object_params("stg")
             image_filename = kvm_vm.get_image_filename(image_params,
                                                        test.bindir)
             if tested_model == "virtio":
client/tests/kvm/tests/physical_resources_check.py +4 -4
index 0630ac8..3234da7 100644
@@ -51,7 +51,7 @@ def run_physical_resources_check(test, params, env):
     # Define a function for checking number of hard drivers & NICs
     def check_num(devices, info_cmd, check_str):
         f_fail = 0
-        expected_num = kvm_utils.get_sub_dict_names(params, devices).__len__()
+        expected_num = params.objects(devices).__len__()
         try:
             o = vm.monitor.info(info_cmd)
         except kvm_monitor.MonitorError, e:
@@ -78,9 +78,9 @@ def run_physical_resources_check(test, params, env):
     # Define a function for checking hard drives & NICs' model
     def chk_fmt_model(device, fmt_model, info_cmd, str):
         f_fail = 0
-        devices = kvm_utils.get_sub_dict_names(params, device)
+        devices = params.objects(device)
         for chk_device in devices:
-            expected = kvm_utils.get_sub_dict(params, chk_device).get(fmt_model)
+            expected = params.object_params(chk_device).get(fmt_model)
             if not expected:
                 expected = "rtl8139"
             try:
@@ -123,7 +123,7 @@ def run_physical_resources_check(test, params, env):
     found_mac_addresses = re.findall("macaddr=(\S+)", o)
     logging.debug("Found MAC adresses: %s" % found_mac_addresses)
 
-    num_nics = len(kvm_utils.get_sub_dict_names(params, "nics"))
+    num_nics = len(params.objects("nics"))
     for nic_index in range(num_nics):
         mac = vm.get_mac_address(nic_index)
         if not string.lower(mac) in found_mac_addresses:
client/tests/kvm/tests/unittest.py +2 -2
index 54e5f73..67686e3 100644
@@ -36,8 +36,8 @@ def run_unittest(test, params, env):
                               unittest_cfg)
     logging.debug('Unit test list: %s' % test_list)
 
-    if params.get('test_list', None):
-        test_list = kvm_utils.get_sub_dict_names(params, 'test_list')
+    if params.get('test_list'):
+        test_list = params.get('test_list').split()
         logging.info('Original test list overriden by user')
         logging.info('User defined unit test list: %s' % test_list)
 
client/tests/kvm/tests/whql_submission.py +15 -15
index 2d01e30..ae71942 100644
@@ -21,13 +21,13 @@ def run_whql_submission(test, params, env):
     # Log into all client VMs
     vms = []
     sessions = []
-    for vm_name in kvm_utils.get_sub_dict_names(params, "vms"):
+    for vm_name in params.objects("vms"):
         vms.append(kvm_test_utils.get_living_vm(env, vm_name))
         sessions.append(kvm_test_utils.wait_for_login(vms[-1], 0, 240))
 
     # Make sure all NICs of all client VMs are up
     for vm in vms:
-        nics = kvm_utils.get_sub_dict_names(vm.params, "nics")
+        nics = vm.params.objects("nics")
         for nic_index in range(len(nics)):
             s = kvm_test_utils.wait_for_login(vm, nic_index, 600)
             s.close()
@@ -77,7 +77,7 @@ def run_whql_submission(test, params, env):
 
     # Check the NICs again
     for vm in vms:
-        nics = kvm_utils.get_sub_dict_names(vm.params, "nics")
+        nics = vm.params.objects("nics")
         for nic_index in range(len(nics)):
             s = kvm_test_utils.wait_for_login(vm, nic_index, 600)
             s.close()
@@ -122,8 +122,8 @@ def run_whql_submission(test, params, env):
 
     # Set submission DeviceData
     find_prompt("DeviceData name:")
-    for dd in kvm_utils.get_sub_dict_names(params, "device_data"):
-        dd_params = kvm_utils.get_sub_dict(params, dd)
+    for dd in params.objects("device_data"):
+        dd_params = params.object_params(dd)
         if dd_params.get("dd_name") and dd_params.get("dd_data"):
             server_session.sendline(dd_params.get("dd_name"))
             server_session.sendline(dd_params.get("dd_data"))
@@ -131,29 +131,29 @@ def run_whql_submission(test, params, env):
 
     # Set submission descriptors
     find_prompt("Descriptor path:")
-    for desc in kvm_utils.get_sub_dict_names(params, "descriptors"):
-        desc_params = kvm_utils.get_sub_dict(params, desc)
+    for desc in params.objects("descriptors"):
+        desc_params = params.object_params(desc)
         if desc_params.get("desc_path"):
             server_session.sendline(desc_params.get("desc_path"))
     server_session.sendline()
 
     # Set machine dimensions for each client machine
-    for vm_name in kvm_utils.get_sub_dict_names(params, "vms"):
-        vm_params = kvm_utils.get_sub_dict(params, vm_name)
+    for vm_name in params.objects("vms"):
+        vm_params = params.object_params(vm_name)
         find_prompt(r"Dimension name\b.*:")
-        for dp in kvm_utils.get_sub_dict_names(vm_params, "dimensions"):
-            dp_params = kvm_utils.get_sub_dict(vm_params, dp)
+        for dp in vm_params.objects("dimensions"):
+            dp_params = vm_params.object_params(dp)
             if dp_params.get("dim_name") and dp_params.get("dim_value"):
                 server_session.sendline(dp_params.get("dim_name"))
                 server_session.sendline(dp_params.get("dim_value"))
         server_session.sendline()
 
     # Set extra parameters for tests that require them (e.g. NDISTest)
-    for vm_name in kvm_utils.get_sub_dict_names(params, "vms"):
-        vm_params = kvm_utils.get_sub_dict(params, vm_name)
+    for vm_name in params.objects("vms"):
+        vm_params = params.object_params(vm_name)
         find_prompt(r"Parameter name\b.*:")
-        for dp in kvm_utils.get_sub_dict_names(vm_params, "device_params"):
-            dp_params = kvm_utils.get_sub_dict(vm_params, dp)
+        for dp in vm_params.objects("device_params"):
+            dp_params = vm_params.object_params(dp)
             if dp_params.get("dp_name") and dp_params.get("dp_regex"):
                 server_session.sendline(dp_params.get("dp_name"))
                 server_session.sendline(dp_params.get("dp_regex"))
Signature · 1 line
1.7.3.4
#3 lore raw

PATCH 4/4 KVM test: remove get_sub_dict_names() and get_sub_dict()

In reply to Michael Goldish
---
 client/tests/kvm/kvm_utils.py |   35 -----------------------------------
 1 files changed, 0 insertions(+), 35 deletions(-)
client/tests/kvm/kvm_utils.py +0 -35
index 1e32e30..2e4ba92 100644
@@ -62,41 +62,6 @@ def load_env(filename, version):
         return default
 
 
-def get_sub_dict(dict, name):
-    """
-    Return a "sub-dict" corresponding to a specific object.
-
-    Operate on a copy of dict: for each key that ends with the suffix
-    "_" + name, strip the suffix from the key, and set the value of
-    the stripped key to that of the key. Return the resulting dict.
-
-    @param name: Suffix of the key we want to set the value.
-    """
-    suffix = "_" + name
-    new_dict = dict.copy()
-    for key in dict.keys():
-        if key.endswith(suffix):
-            new_key = key.split(suffix)[0]
-            new_dict[new_key] = dict[key]
-    return new_dict
-
-
-def get_sub_dict_names(dict, keyword):
-    """
-    Return a list of "sub-dict" names that may be extracted with get_sub_dict.
-
-    This function may be modified to change the behavior of all functions that
-    deal with multiple objects defined in dicts (e.g. VMs, images, NICs).
-
-    @param keyword: A key in dict (e.g. "vms", "images", "nics").
-    """
-    names = dict.get(keyword)
-    if names:
-        return names.split()
-    else:
-        return []
-
-
 class Params(UserDict.IterableUserDict):
     """
     A dict-like object passed to every test.
Signature · 1 line
1.7.3.4