• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import logging, os, shutil, glob, ConfigParser
2from autotest_lib.client.common_lib import error
3from autotest_lib.client.virt import virt_utils, virt_env_process
4
5
6def run_unittest(test, params, env):
7    """
8    KVM RHEL-6 style unit test:
9    1) Resume a stopped VM
10    2) Wait for VM to terminate
11    3) If qemu exited with code = 0, the unittest passed. Otherwise, it failed
12    4) Collect all logs generated
13
14    @param test: kvm test object
15    @param params: Dictionary with the test parameters
16    @param env: Dictionary with test environment
17    """
18    unittest_dir = os.path.join(test.bindir, 'unittests')
19    if not os.path.isdir(unittest_dir):
20        raise error.TestError("No unittest dir %s available (did you run the "
21                              "build test first?)" % unittest_dir)
22    os.chdir(unittest_dir)
23    unittest_list = glob.glob('*.flat')
24    if not unittest_list:
25        raise error.TestError("No unittest files available (did you run the "
26                              "build test first?)")
27    logging.debug('Flat file list: %s', unittest_list)
28
29    unittest_cfg = os.path.join(unittest_dir, 'unittests.cfg')
30    parser = ConfigParser.ConfigParser()
31    parser.read(unittest_cfg)
32    test_list = parser.sections()
33
34    if not test_list:
35        raise error.TestError("No tests listed on config file %s" %
36                              unittest_cfg)
37    logging.debug('Unit test list: %s', test_list)
38
39    if params.get('test_list'):
40        test_list = params.get('test_list').split()
41        logging.info('Original test list overriden by user')
42        logging.info('User defined unit test list: %s', test_list)
43
44    nfail = 0
45    tests_failed = []
46
47    timeout = int(params.get('unittest_timeout', 600))
48
49    extra_params_original = params['extra_params']
50
51    for t in test_list:
52        logging.info('Running %s', t)
53
54        flat_file = None
55        if parser.has_option(t, 'file'):
56            flat_file = parser.get(t, 'file')
57
58        if flat_file is None:
59            nfail += 1
60            tests_failed.append(t)
61            logging.error('Unittest config file %s has section %s but no '
62                          'mandatory option file', unittest_cfg, t)
63            continue
64
65        if flat_file not in unittest_list:
66            nfail += 1
67            tests_failed.append(t)
68            logging.error('Unittest file %s referenced in config file %s but '
69                          'was not find under the unittest dir', flat_file,
70                          unittest_cfg)
71            continue
72
73        smp = None
74        if parser.has_option(t, 'smp'):
75            smp = int(parser.get(t, 'smp'))
76            params['smp'] = smp
77
78        extra_params = None
79        if parser.has_option(t, 'extra_params'):
80            extra_params = parser.get(t, 'extra_params')
81            params['extra_params'] += ' %s' % extra_params
82
83        vm_name = params.get("main_vm")
84        params['kernel'] = os.path.join(unittest_dir, flat_file)
85        testlog_path = os.path.join(test.debugdir, "%s.log" % t)
86
87        try:
88            try:
89                vm_name = params.get('main_vm')
90                virt_env_process.preprocess_vm(test, params, env, vm_name)
91                vm = env.get_vm(vm_name)
92                vm.create()
93                vm.monitor.cmd("cont")
94                logging.info("Waiting for unittest %s to complete, timeout %s, "
95                             "output in %s", t, timeout,
96                             vm.get_testlog_filename())
97                if not virt_utils.wait_for(vm.is_dead, timeout):
98                    raise error.TestFail("Timeout elapsed (%ss)" % timeout)
99                # Check qemu's exit status
100                status = vm.process.get_status()
101                if status != 0:
102                    nfail += 1
103                    tests_failed.append(t)
104                    logging.error("Unit test %s failed", t)
105            except Exception, e:
106                nfail += 1
107                tests_failed.append(t)
108                logging.error('Exception happened during %s: %s', t, str(e))
109        finally:
110            try:
111                shutil.copy(vm.get_testlog_filename(), testlog_path)
112                logging.info("Unit test log collected and available under %s",
113                             testlog_path)
114            except (NameError, IOError):
115                logging.error("Not possible to collect logs")
116
117        # Restore the extra params so other tests can run normally
118        params['extra_params'] = extra_params_original
119
120    if nfail != 0:
121        raise error.TestFail("Unit tests failed: %s" % " ".join(tests_failed))
122