• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import re, string, logging
2from autotest_lib.client.common_lib import error
3from autotest_lib.client.virt import kvm_monitor, virt_vm
4
5
6def run_physical_resources_check(test, params, env):
7    """
8    Check physical resources assigned to KVM virtual machines:
9    1) Log into the guest
10    2) Verify whether cpu counts ,memory size, nics' model,
11       count and drives' format & count, drive_serial, UUID
12       reported by the guest OS matches what has been assigned
13       to the VM (qemu command line)
14    3) Verify all MAC addresses for guest NICs
15
16    @param test: KVM test object.
17    @param params: Dictionary with the test parameters.
18    @param env: Dictionary with test environment.
19    """
20    # Define a function for checking number of hard drivers & NICs
21    def check_num(devices, info_cmd, check_str):
22        f_fail = []
23        expected_num = params.objects(devices).__len__()
24        o = ""
25        try:
26            o = vm.monitor.info(info_cmd)
27        except kvm_monitor.MonitorError, e:
28            fail_log =  e + "\n"
29            fail_log += "info/query monitor command failed (%s)" % info_cmd
30            f_fail.append(fail_log)
31            logging.error(fail_log)
32
33        actual_num = string.count(o, check_str)
34        if expected_num != actual_num:
35            fail_log =  "%s number mismatch:\n" % str(devices)
36            fail_log += "    Assigned to VM: %d\n" % expected_num
37            fail_log += "    Reported by OS: %d" % actual_num
38            f_fail.append(fail_log)
39            logging.error(fail_log)
40        return expected_num, f_fail
41
42    # Define a function for checking hard drives & NICs' model
43    def chk_fmt_model(device, fmt_model, info_cmd, regexp):
44        f_fail = []
45        devices = params.objects(device)
46        for chk_device in devices:
47            expected = params.object_params(chk_device).get(fmt_model)
48            if not expected:
49                expected = "rtl8139"
50            o = ""
51            try:
52                o = vm.monitor.info(info_cmd)
53            except kvm_monitor.MonitorError, e:
54                fail_log = e + "\n"
55                fail_log += "info/query monitor command failed (%s)" % info_cmd
56                f_fail.append(fail_log)
57                logging.error(fail_log)
58
59            device_found = re.findall(regexp, o)
60            logging.debug("Found devices: %s", device_found)
61            found = False
62            for fm in device_found:
63                if expected in fm:
64                    found = True
65
66            if not found:
67                fail_log =  "%s model mismatch:\n" % str(device)
68                fail_log += "    Assigned to VM: %s\n" % expected
69                fail_log += "    Reported by OS: %s" % device_found
70                f_fail.append(fail_log)
71                logging.error(fail_log)
72        return f_fail
73
74    # Define a function to verify UUID & Serial number
75    def verify_device(expect, name, verify_cmd):
76        f_fail = []
77        if verify_cmd:
78            actual = session.cmd_output(verify_cmd)
79            if not string.upper(expect) in actual:
80                fail_log =  "%s mismatch:\n" % name
81                fail_log += "    Assigned to VM: %s\n" % string.upper(expect)
82                fail_log += "    Reported by OS: %s" % actual
83                f_fail.append(fail_log)
84                logging.error(fail_log)
85        return f_fail
86
87
88    vm = env.get_vm(params["main_vm"])
89    vm.verify_alive()
90    timeout = int(params.get("login_timeout", 360))
91    session = vm.wait_for_login(timeout=timeout)
92
93    logging.info("Starting physical resources check test")
94    logging.info("Values assigned to VM are the values we expect "
95                 "to see reported by the Operating System")
96    # Define a failure counter, as we want to check all physical
97    # resources to know which checks passed and which ones failed
98    n_fail = []
99
100    # We will check HDs with the image name
101    image_name = virt_vm.get_image_filename(params, test.bindir)
102
103    # Check cpu count
104    logging.info("CPU count check")
105    expected_cpu_nr = int(params.get("smp"))
106    actual_cpu_nr = vm.get_cpu_count()
107    if expected_cpu_nr != actual_cpu_nr:
108        fail_log =  "CPU count mismatch:\n"
109        fail_log += "    Assigned to VM: %s \n" % expected_cpu_nr
110        fail_log += "    Reported by OS: %s" % actual_cpu_nr
111        n_fail.append(fail_log)
112        logging.error(fail_log)
113
114    # Check memory size
115    logging.info("Memory size check")
116    expected_mem = int(params.get("mem"))
117    actual_mem = vm.get_memory_size()
118    if actual_mem != expected_mem:
119        fail_log =  "Memory size mismatch:\n"
120        fail_log += "    Assigned to VM: %s\n" % expected_mem
121        fail_log += "    Reported by OS: %s\n" % actual_mem
122        n_fail.append(fail_log)
123        logging.error(fail_log)
124
125
126    logging.info("Hard drive count check")
127    _, f_fail = check_num("images", "block", image_name)
128    n_fail.extend(f_fail)
129
130    logging.info("NIC count check")
131    _, f_fail = check_num("nics", "network", "model=")
132    n_fail.extend(f_fail)
133
134    logging.info("NICs model check")
135    f_fail = chk_fmt_model("nics", "nic_model", "network", "model=(.*),")
136    n_fail.extend(f_fail)
137
138    logging.info("Drive format check")
139    f_fail = chk_fmt_model("images", "drive_format",
140                           "block", "(.*)\: .*%s" % image_name)
141    n_fail.extend(f_fail)
142
143    logging.info("Network card MAC check")
144    o = ""
145    try:
146        o = vm.monitor.info("network")
147    except kvm_monitor.MonitorError, e:
148        fail_log =  e + "\n"
149        fail_log += "info/query monitor command failed (network)"
150        n_fail.append(fail_log)
151        logging.error(fail_log)
152    found_mac_addresses = re.findall("macaddr=(\S+)", o)
153    logging.debug("Found MAC adresses: %s", found_mac_addresses)
154
155    num_nics = len(params.objects("nics"))
156    for nic_index in range(num_nics):
157        mac = vm.get_mac_address(nic_index)
158        if not string.lower(mac) in found_mac_addresses:
159            fail_log =  "MAC address mismatch:\n"
160            fail_log += "    Assigned to VM (not found): %s" % mac
161            n_fail.append(fail_log)
162            logging.error(fail_log)
163
164    logging.info("UUID check")
165    if vm.get_uuid():
166        f_fail = verify_device(vm.get_uuid(), "UUID",
167                               params.get("catch_uuid_cmd"))
168        n_fail.extend(f_fail)
169
170    logging.info("Hard Disk serial number check")
171    catch_serial_cmd = params.get("catch_serial_cmd")
172    f_fail = verify_device(params.get("drive_serial"), "Serial",
173                           catch_serial_cmd)
174    n_fail.extend(f_fail)
175
176    if n_fail:
177        session.close()
178        raise error.TestFail("Physical resources check test "
179                             "reported %s failures:\n%s" %
180                             (len(n_fail), "\n".join(n_fail)))
181
182    session.close()
183