Skip to content

Commit 0eff377

Browse files
authored
Merge pull request #568 from oohal/reworks
Reworks
2 parents 969c41b + dc43f79 commit 0eff377

File tree

3 files changed

+107
-74
lines changed

3 files changed

+107
-74
lines changed

OpTestConfiguration.py

+67-51
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from common.OpTestOpenBMC import OpTestOpenBMC
1010
from common.OpTestQemu import OpTestQemu
1111
from common.OpTestMambo import OpTestMambo
12+
from common.OpTestSystem import OpSystemState
1213
import common.OpTestSystem
1314
import common.OpTestHost
1415
from common.OpTestIPMI import OpTestIPMI, OpTestSMCIPMI
@@ -161,6 +162,8 @@ def get_parser():
161162
help="List available suites to run")
162163
tgroup.add_argument("--list-tests", action='store_true',
163164
help="List each test that would have been run")
165+
tgroup.add_argument("--list-all-tests", action='store_true',
166+
help="List all defined tests")
164167
tgroup.add_argument("--run-suite", action='append',
165168
help="Run a test suite(s)")
166169
tgroup.add_argument("--run", action='append',
@@ -413,6 +416,14 @@ def get_parser():
413416
hmcgroup.add_argument(
414417
"--lpar-vios", help="Lpar VIOS to boot before other LPARS", default=None)
415418

419+
misc_group = parser.add_argument_group("Misc")
420+
misc_group.add_argument("--check-ssh-keys", action='store_true', default=False,
421+
help="Check remote host keys when using SSH (auto-yes on new)")
422+
misc_group.add_argument("--known-hosts-file",
423+
help="Specify a custom known_hosts file")
424+
misc_group.add_argument("--accept-unknown-args", default=False, action='store_true',
425+
help="Don't exit if we find unknown command line arguments")
426+
416427
return parser
417428

418429

@@ -455,46 +466,49 @@ def cleanup(self):
455466
# attribute errors thrown in cleanup, e.g. ./op-test -h
456467
self.util.cleanup()
457468

469+
470+
def parse_config_file(self, filename, optional=False):
471+
config = configparser.ConfigParser()
472+
473+
if not os.access(filename, os.R_OK):
474+
if optional:
475+
return {}
476+
raise OSError(errno.ENOENT, os.strerror(errno.ENOENT), filename)
477+
478+
config.read(filename)
479+
480+
if config.has_section('op-test'):
481+
d = dict(config.items('op-test'))
482+
else:
483+
msg = "{} is missing an an [op-test] section header".format(filename)
484+
raise configparser.NoSectionError(msg)
485+
486+
return dict(config.items('op-test'))
487+
458488
def parse_args(self, argv=None):
459489
conf_parser = argparse.ArgumentParser(add_help=False)
490+
conf_parser.add_argument("-c", "--config-file", metavar="FILE")
491+
config_args, _ = conf_parser.parse_known_args(argv)
460492

461-
# We have two parsers so we have correct --help, we need -c in both
462-
conf_parser.add_argument("-c", "--config-file", help="Configuration File",
463-
metavar="FILE")
464-
465-
args, remaining_args = conf_parser.parse_known_args(argv)
466-
defaults = {}
467-
config = configparser.SafeConfigParser()
468-
config.read([os.path.expanduser("~/.op-test-framework.conf")])
469-
if args.config_file:
470-
if os.access(args.config_file, os.R_OK):
471-
config.read([args.config_file])
472-
else:
473-
raise OSError(errno.ENOENT, os.strerror(
474-
errno.ENOENT), args.config_file)
475-
try:
476-
defaults = dict(config.items('op-test'))
477-
except configparser.NoSectionError:
478-
pass
479-
480-
parser = get_parser()
481-
parser.set_defaults(**defaults)
493+
userfile = os.path.expanduser("~/.op-test-framework.conf")
494+
self.defaults = self.parse_config_file(userfile, True)
482495

483-
if defaults.get('qemu_binary'):
484-
qemu_default = defaults['qemu_binary']
496+
# parse specified config file
497+
if config_args.config_file:
498+
cfg = self.parse_config_file(config_args.config_file)
499+
self.defaults.update(cfg)
485500

486-
if defaults.get('mambo_binary'):
487-
mambo_default = defaults['mambo_binary']
488-
if defaults.get('mambo_initial_run_script'):
489-
mambo_default = defaults['mambo_initial_run_script']
501+
parser = get_parser()
502+
parser.set_defaults(**self.defaults)
490503

491-
parser.add_argument("--check-ssh-keys", action='store_true', default=False,
492-
help="Check remote host keys when using SSH (auto-yes on new)")
493-
parser.add_argument("--known-hosts-file",
494-
help="Specify a custom known_hosts file")
504+
# don't parse argv[0]
505+
self.args, self.remaining_args = parser.parse_known_args(argv[1:])
495506

496-
self.args, self.remaining_args = parser.parse_known_args(
497-
remaining_args)
507+
# we use parse_known_args() and generate our own usage because the number
508+
# of args makes the default usage string... unwieldy
509+
if len(self.remaining_args) > 0 and not self.args.accept_unknown_args:
510+
raise Exception("Unknown command line argument(s): {}"
511+
.format(str(self.remaining_args)))
498512

499513
args_dict = vars(self.args)
500514

@@ -510,18 +524,30 @@ def parse_args(self, argv=None):
510524
if args_dict.get(key) is None:
511525
args_dict[key] = default_val[key]
512526

513-
stateMap = {'UNKNOWN': common.OpTestSystem.OpSystemState.UNKNOWN,
514-
'UNKNOWN_BAD': common.OpTestSystem.OpSystemState.UNKNOWN_BAD,
515-
'OFF': common.OpTestSystem.OpSystemState.OFF,
516-
'PETITBOOT': common.OpTestSystem.OpSystemState.PETITBOOT,
517-
'PETITBOOT_SHELL': common.OpTestSystem.OpSystemState.PETITBOOT_SHELL,
518-
'OS': common.OpTestSystem.OpSystemState.OS
519-
}
520527

521528
# Some quick sanity checking
522529
if self.args.known_hosts_file and not self.args.check_ssh_keys:
523530
parser.error("--known-hosts-file requires --check-ssh-keys")
524531

532+
# FIXME: Passing machine_state doesn't work at all with qemu or mambo
533+
if self.args.machine_state == None:
534+
if self.args.bmc_type in ['qemu', 'mambo']:
535+
# Force UNKNOWN_BAD so that we don't try to setup the console early
536+
self.startState = OpSystemState.UNKNOWN_BAD
537+
else:
538+
self.startState = OpSystemState.UNKNOWN
539+
else:
540+
stateMap = {'UNKNOWN': OpSystemState.UNKNOWN,
541+
'UNKNOWN_BAD': OpSystemState.UNKNOWN_BAD,
542+
'OFF': OpSystemState.OFF,
543+
'PETITBOOT': OpSystemState.PETITBOOT,
544+
'PETITBOOT_SHELL': OpSystemState.PETITBOOT_SHELL,
545+
'OS': OpSystemState.OS
546+
}
547+
self.startState = stateMap[self.args.machine_state]
548+
return self.args, self.remaining_args
549+
550+
def do_testing_setup(self):
525551
# Setup some defaults for the output options
526552
# Order of precedence
527553
# 1. cmdline arg
@@ -602,7 +628,7 @@ def parse_args(self, argv=None):
602628
self.atexit_ready = True
603629
# now that we have loggers, dump conf file to help debug later
604630
OpTestLogger.optest_logger_glob.optest_logger.debug(
605-
"conf file defaults={}".format(defaults))
631+
"conf file defaults={}".format(self.defaults))
606632
cmd = "git describe --always"
607633
try:
608634
git_output = subprocess.check_output(cmd.split())
@@ -661,16 +687,6 @@ def parse_args(self, argv=None):
661687
.format(self.args.locker_wait))
662688
time.sleep(60)
663689

664-
if self.args.machine_state == None:
665-
if self.args.bmc_type in ['qemu', 'mambo']:
666-
# Force UNKNOWN_BAD so that we don't try to setup the console early
667-
self.startState = common.OpTestSystem.OpSystemState.UNKNOWN_BAD
668-
else:
669-
self.startState = common.OpTestSystem.OpSystemState.UNKNOWN
670-
else:
671-
self.startState = stateMap[self.args.machine_state]
672-
return self.args, self.remaining_args
673-
674690
def get_suffix(self):
675691
# Grab the suffix, if not given use current time
676692
if (self.args.suffix):

op-test

+34-16
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ import re
112112
import signal
113113
import logging
114114
import OpTestLogger
115-
import importlib
116115
# op-test is the parent logger
117116
optestlog = logging.getLogger(OpTestLogger.optest_logger_glob.parent_logger)
118117

@@ -149,9 +148,39 @@ signal.signal(signal.SIGSEGV, optest_handler)
149148
signal.signal(signal.SIGTERM, optest_handler)
150149
signal.signal(signal.SIGUSR1, optest_handler)
151150
signal.signal(signal.SIGUSR2, optest_handler)
152-
args, remaining_args = OpTestConfiguration.conf.parse_args(sys.argv)
153-
OpTestConfiguration.conf.objs()
154151

152+
# parse config options and create log files, take the host lock, etc.
153+
try:
154+
args, remaining_args = OpTestConfiguration.conf.parse_args(sys.argv)
155+
except Exception as e:
156+
print("Config error: {}".format(e));
157+
sys.exit(1)
158+
159+
def print_tests(s):
160+
for test in s:
161+
if isinstance(test, unittest.TestSuite):
162+
print_tests(test)
163+
else:
164+
test_id = test.id()
165+
print('{0:40}'.format(test_id))
166+
167+
if OpTestConfiguration.conf.args.list_all_tests:
168+
print_tests(unittest.TestLoader().discover('testcases', '*.py'))
169+
exit(0)
170+
171+
# create loggers, take hostlocks, etc
172+
try:
173+
OpTestConfiguration.conf.do_testing_setup()
174+
except Exception as e:
175+
print()
176+
print("Test setup error! {}".format(e));
177+
print()
178+
print(traceback.format_exc())
179+
print()
180+
OpTestConfiguration.conf.util.cleanup()
181+
sys.exit(1)
182+
183+
OpTestConfiguration.conf.objs()
155184

156185
class SystemAccessSuite():
157186
'''
@@ -837,8 +866,6 @@ try:
837866
OpTestConfiguration.conf.util.cleanup()
838867
exit(0)
839868

840-
importlib.reload(sys)
841-
842869
t = unittest.TestSuite()
843870

844871
if OpTestConfiguration.conf.args.run_suite:
@@ -860,18 +887,9 @@ try:
860887
t.addTest(suites['default'].suite())
861888

862889
if OpTestConfiguration.conf.args.list_tests:
863-
print('{0:40}'.format('Test'))
890+
print('{0:40}'.format('Tests'))
864891
print('{0:40}'.format('----------'))
865-
866-
def print_suite(s):
867-
for test in s:
868-
if isinstance(test, unittest.TestSuite):
869-
print_suite(test)
870-
else:
871-
test_id = test.id()
872-
print('{0:40}'.format(test_id))
873-
874-
print_suite(t)
892+
print_tests(t)
875893
OpTestConfiguration.conf.util.cleanup()
876894
exit(0)
877895

testcases/OpTestInbandUsbInterface.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@
5151

5252
import OpTestConfiguration
5353
from common.OpTestSystem import OpSystemState
54-
from testcases.OpTestInbandIPMI import BasicInbandIPMI, OpTestInbandIPMI, ExperimentalInbandIPMI
55-
from testcases.OpTestInbandIPMI import SkirootBasicInbandIPMI, SkirootFullInbandIPMI
54+
import testcases.OpTestInbandIPMI as ib
5655

5756

5857
def experimental_suite():
@@ -71,7 +70,7 @@ def skiroot_full_suite():
7170
return unittest.defaultTestLoader.loadTestsFromTestCase(SkirootInbandUSB)
7271

7372

74-
class BasicInbandUSB(BasicInbandIPMI):
73+
class BasicInbandUSB(ib.BasicInbandIPMI):
7574
def setUp(self, ipmi_method=BMC_CONST.IPMITOOL_USB):
7675
self.bmc_type = OpTestConfiguration.conf.args.bmc_type
7776
if "FSP" in self.bmc_type:
@@ -81,7 +80,7 @@ def setUp(self, ipmi_method=BMC_CONST.IPMITOOL_USB):
8180
super(BasicInbandUSB, self).setUp(ipmi_method=ipmi_method)
8281

8382

84-
class InbandUSB(OpTestInbandIPMI):
83+
class InbandUSB(ib.OpTestInbandIPMI):
8584
def setUp(self, ipmi_method=BMC_CONST.IPMITOOL_USB):
8685
self.bmc_type = OpTestConfiguration.conf.args.bmc_type
8786
if "FSP" in self.bmc_type:
@@ -91,7 +90,7 @@ def setUp(self, ipmi_method=BMC_CONST.IPMITOOL_USB):
9190
super(InbandUSB, self).setUp(ipmi_method=ipmi_method)
9291

9392

94-
class SkirootBasicInbandUSB(SkirootBasicInbandIPMI):
93+
class SkirootBasicInbandUSB(ib.SkirootBasicInbandIPMI):
9594
def setUp(self, ipmi_method=BMC_CONST.IPMITOOL_USB):
9695
self.bmc_type = OpTestConfiguration.conf.args.bmc_type
9796
if "FSP" in self.bmc_type:
@@ -101,7 +100,7 @@ def setUp(self, ipmi_method=BMC_CONST.IPMITOOL_USB):
101100
super(SkirootBasicInbandUSB, self).setUp(ipmi_method=ipmi_method)
102101

103102

104-
class SkirootInbandUSB(SkirootFullInbandIPMI):
103+
class SkirootInbandUSB(ib.SkirootFullInbandIPMI):
105104
def setUp(self, ipmi_method=BMC_CONST.IPMITOOL_USB):
106105
self.bmc_type = OpTestConfiguration.conf.args.bmc_type
107106
if "FSP" in self.bmc_type:
@@ -111,7 +110,7 @@ def setUp(self, ipmi_method=BMC_CONST.IPMITOOL_USB):
111110
super(SkirootInbandUSB, self).setUp(ipmi_method=ipmi_method)
112111

113112

114-
class ExperimentalInbandUSB(ExperimentalInbandIPMI):
113+
class ExperimentalInbandUSB(ib.ExperimentalInbandIPMI):
115114
def setUp(self, ipmi_method=BMC_CONST.IPMITOOL_USB):
116115
self.bmc_type = OpTestConfiguration.conf.args.bmc_type
117116
if "FSP" in self.bmc_type:

0 commit comments

Comments
 (0)