提交 20b3ac96 编写于 作者: L Lukáš Doktor

examples.tests: Use self.params.get(key, default)

We decided to use params.get(key, default) rather than flat dictionary
in test header. This way should be easier to read to code without the
need to look up and also use different defaults for different namespaces
(or even code branches).
Signed-off-by: NLukáš Doktor <ldoktor@redhat.com>
上级 d886f55b
......@@ -11,6 +11,7 @@ class AbortTest(test.Test):
"""
A test that just calls abort() (and abort).
"""
default_params = {'timeout': 2.0}
def runTest(self):
......
......@@ -15,13 +15,11 @@ class CAbort(test.Test):
A test that calls C standard lib function abort().
"""
default_params = {'source': 'abort.c'}
def setUp(self):
"""
Build 'abort'.
"""
c_file = self.get_data_path(self.params.source)
c_file = self.get_data_path(self.params.get('source', 'abort.c'))
c_file_name = os.path.basename(c_file)
dest_c_file = os.path.join(self.srcdir, c_file_name)
shutil.copy(c_file, dest_c_file)
......
......@@ -15,13 +15,11 @@ class DataDirTest(test.Test):
Test that uses resources from the data dir.
"""
default_params = {'source': 'datadir.c'}
def setUp(self):
"""
Build 'datadir'.
"""
c_file = self.get_data_path(self.params.source)
c_file = self.get_data_path(self.params.get('source', 'datadir.c'))
c_file_name = os.path.basename(c_file)
dest_c_file = os.path.join(self.srcdir, c_file_name)
shutil.copy(c_file, dest_c_file)
......
......@@ -16,13 +16,11 @@ class DoubleFreeTest(test.Test):
Double free test case.
"""
default_params = {'source': 'doublefree.c'}
def setUp(self):
"""
Build 'doublefree'.
"""
c_file = self.get_data_path(self.params.source)
c_file = self.get_data_path(self.params.get('source', 'doublefree.c'))
c_file_name = os.path.basename(c_file)
dest_c_file = os.path.join(self.srcdir, c_file_name)
shutil.copy(c_file, dest_c_file)
......
......@@ -15,16 +15,16 @@ class DoubleFreeTest(test.Test):
10% chance to execute double free exception.
"""
default_params = {'source': 'doublefree.c'}
__binary = None # filename of the compiled program
def setUp(self):
"""
Build 'doublefree'.
"""
c_file = self.get_data_path(self.params.source)
source = self.params.get('source', 'doublefree.c')
c_file = self.get_data_path(source)
shutil.copy(c_file, self.srcdir)
self.__binary = self.params['source'].rsplit('.', 1)[0]
self.__binary = source.rsplit('.', 1)[0]
build.make(self.srcdir,
env={'CFLAGS': '-g -O0'},
extra_args=self.__binary)
......
......@@ -18,16 +18,14 @@ class FioTest(test.Test):
:see: http://freecode.com/projects/fio
"""
default_params = {'fio_tarball': 'fio-2.1.10.tar.bz2',
'fio_job': 'fio-mixed.job'}
def setUp(self):
"""
Build 'fio'.
"""
tarball_path = self.get_data_path(self.params.fio_tarball)
fio_tarball = self.params.get('fio_tarball', 'fio-2.1.10.tar.bz2')
tarball_path = self.get_data_path(fio_tarball)
archive.extract(tarball_path, self.srcdir)
fio_version = self.params.fio_tarball.split('.tar.')[0]
fio_version = fio_tarball.split('.tar.')[0]
self.srcdir = os.path.join(self.srcdir, fio_version)
build.make(self.srcdir)
......@@ -36,7 +34,8 @@ class FioTest(test.Test):
Execute 'fio' with appropriate parameters.
"""
os.chdir(self.srcdir)
cmd = ('./fio %s' % self.get_data_path(self.params.fio_job))
fio_job = self.params.get('fio_job', 'fio-mixed.job')
cmd = ('./fio %s' % self.get_data_path(fio_job))
process.system(cmd)
......
......@@ -10,12 +10,11 @@ class LinuxBuildTest(test.Test):
"""
Execute the Linux Build test.
"""
default_params = {'linux_version': '3.14.5',
'linux_config': 'config'}
def setUp(self):
kernel_version = self.params.linux_version
config_path = self.get_data_path('config')
kernel_version = self.params.get('linux_version', '3.14.5')
linux_config = self.params.get('linux_config', 'config')
config_path = self.get_data_path(linux_config)
self.linux_build = kernel_build.KernelBuild(kernel_version,
config_path,
self.srcdir)
......
......@@ -18,16 +18,16 @@ class PrintVariableTest(test.Test):
3) checks the output
"""
default_params = {'source': 'print_variable.c'}
__binary = None # filename of the compiled program
def setUp(self):
"""
Build 'print_variable'.
"""
c_file = self.get_data_path(self.params.source)
source = self.params.get('source', 'print_variable.c')
c_file = self.get_data_path(source)
shutil.copy(c_file, self.srcdir)
self.__binary = self.params['source'].rsplit('.', 1)[0]
self.__binary = source.rsplit('.', 1)[0]
build.make(self.srcdir,
env={'CFLAGS': '-g -O0'},
extra_args=self.__binary)
......
......@@ -9,18 +9,6 @@ class MultiplexTest(test.Test):
"""
Execute a test that uses provided parameters (for multiplexing testing).
"""
default_params = {'os_type': 'linux',
'gcc_flags': '-O2',
'huge_pages': 'yes',
'numa_balancing': 'yes',
'numa_balancing_migrate_deferred': 'no',
'drive_format': 'virtio_blk',
'nic_model': 'virtio_net',
'enable_msx_vectors': 'yes',
'sync_timeout': 12,
'sync_tries': 3,
'ping_timeout': 10,
'ping_tries': 5}
def setUp(self):
self.compile_code()
......@@ -28,47 +16,57 @@ class MultiplexTest(test.Test):
self.set_numa_balance()
self.assembly_vm()
if self.params.os_type == 'windows':
self.log.info('Preparing VM with Windows (%s)', self.params.win)
if self.params.os_type == 'linux':
self.log.info('Preparing VM with Linux (%s)', self.params.distro)
os_type = self.params.get('os_type', 'linux')
if os_type == 'windows':
self.log.info('Preparing VM with Windows (%s)',
self.params.get('win'))
if os_type == 'linux':
self.log.info('Preparing VM with Linux (%s)',
self.params.get('distro'))
def compile_code(self):
self.log.info('Compile code')
self.log.info('gcc %s %s', self.params.gcc_flags, 'code.c')
self.log.info('gcc %s %s', self.params.get('gcc_flags', '-O2'),
'code.c')
def set_hugepages(self):
if self.params.huge_pages == 'yes':
if self.params.get('huge_pages', 'yes') == 'yes':
self.log.info('Setting hugepages')
def set_numa_balance(self):
if self.params.numa_balance:
self.log.info('Numa balancing: %s', self.params.numa_balance)
if self.params.numa_balancing_migrate_deferred:
self.log.info('Numa balancing migrate deferred: %s',
self.params.numa_balancing_migrate_deferred)
numa_balancing = self.params.get('numa_balancing', 'yes')
numa_migrate = self.params.get('numa_balancing_migrate_deferred', 'no')
if numa_balancing:
self.log.info('Numa balancing: %s', numa_balancing)
if numa_migrate:
self.log.info('Numa balancing migrate deferred: %s', numa_migrate)
def assembly_vm(self):
self.log.info('Assembling VM')
if self.params.drive_format:
self.log.info('Drive format: %s', self.params.drive_format)
if self.params.nic_model:
self.log.info('NIC model: %s', self.params.nic_model)
if self.params.enable_msx_vectors == 'yes':
drive_format = self.params.get('drive_format', 'virtio_blk')
nic_model = self.params.get('nic_model', 'virtio_net')
enable_msx_vectors = self.params.get('enable_msx_vectors', 'yes')
if drive_format:
self.log.info('Drive format: %s', drive_format)
if nic_model:
self.log.info('NIC model: %s', nic_model)
if enable_msx_vectors == 'yes':
self.log.info('Enabling msx vectors')
def runTest(self):
self.log.info('Executing synctest...')
self.log.info('synctest --timeout %s --tries %s',
self.params.sync_timeout,
self.params.sync_tries)
self.params.get('sync_timeout', 12),
self.params.get('sync_tries', 3))
self.log.info('Executing ping test...')
cmdline = 'ping --timeout %s --tries %s' % (self.params.ping_timeout,
self.params.ping_tries)
cmdline = ('ping --timeout %s --tries %s'
% (self.params.get('ping_timeout', 10),
self.params.get('ping_tries', 5)))
if self.params.ping_flags:
cmdline += ' %s' % self.params.ping_flags
ping_flags = self.params.get('ping_flags')
if ping_flags:
cmdline += ' %s' % ping_flags
self.log.info(cmdline)
......
......@@ -15,14 +15,11 @@ class Raise(test.Test):
A test that calls raise() to signals to itself.
"""
default_params = {'source': 'raise.c',
'signal_number': 15}
def setUp(self):
"""
Build 'raise'.
"""
c_file = self.get_data_path(self.params.source)
c_file = self.get_data_path(self.params.get('source', 'raise.c'))
c_file_name = os.path.basename(c_file)
dest_c_file = os.path.join(self.srcdir, c_file_name)
shutil.copy(c_file, dest_c_file)
......@@ -34,10 +31,10 @@ class Raise(test.Test):
"""
Execute 'raise'.
"""
cmd = os.path.join(self.srcdir, 'raise %d' % self.params.signal_number)
signum = self.params.get('signal_number', 15)
cmd = os.path.join(self.srcdir, 'raise %d' % signum)
cmd_result = process.run(cmd, ignore_status=True)
self.log.info(cmd_result)
signum = self.params.signal_number
if signum == 0:
expected_result = 0
self.assertIn("I'm alive!", cmd_result.stdout)
......
......@@ -12,22 +12,20 @@ class SleepTenMin(test.Test):
"""
Sleeps for 10 minutes
"""
default_params = {'sleep_length': 600,
'sleep_cycles': 1,
'sleep_method': 'builtin'}
def runTest(self):
"""
Sleep for length seconds.
"""
cycles = int(self.params.sleep_cycles)
length = int(self.params.sleep_length)
cycles = int(self.params.get('sleep_cycles', 1))
length = int(self.params.get('sleep_length', 600))
method = self.params.get('sleep_method', 'builtin')
for cycle in xrange(0, cycles):
self.log.debug("Sleeping for %.2f seconds", length)
if self.params.sleep_method == 'builtin':
if method == 'builtin':
time.sleep(length)
elif self.params.sleep_method == 'shell':
elif method == 'shell':
os.system("sleep %s" % length)
self.report_state()
......
......@@ -14,20 +14,17 @@ class SyncTest(test.Test):
"""
Execute the synctest test suite.
"""
default_params = {'sync_tarball': 'synctest.tar.bz2',
'sync_length': 100,
'sync_loop': 10,
'debug_symbols': True}
def setUp(self):
"""
Build the synctest suite.
"""
self.cwd = os.getcwd()
tarball_path = self.get_data_path(self.params.sync_tarball)
tarball_path = self.get_data_path(self.params.get('sync_tarball',
'synctest.tar.bz2'))
archive.extract(tarball_path, self.srcdir)
self.srcdir = os.path.join(self.srcdir, 'synctest')
if self.params.debug_symbols:
if self.params.get('debug_symbols', True):
build.make(self.srcdir,
env={'CFLAGS': '-g -O0'},
extra_args='synctest')
......@@ -41,7 +38,8 @@ class SyncTest(test.Test):
os.chdir(self.srcdir)
path = os.path.join(os.getcwd(), 'synctest')
cmd = ('%s %s %s' %
(path, self.params.sync_length, self.params.sync_loop))
(path, self.params.get('sync_length', 100),
self.params.get('sync_loop', 10)))
process.system(cmd)
os.chdir(self.cwd)
......
......@@ -11,16 +11,17 @@ class TimeoutTest(test.Test):
"""
Functional test for avocado. Throw a TestTimeoutError.
"""
default_params = {'timeout': 3.0,
'sleep_time': 5.0}
default_params = {'timeout': 3}
def runTest(self):
"""
This should throw a TestTimeoutError.
"""
sleep_time = self.params.get('sleep_time', 5)
self.log.info('Sleeping for %.2f seconds (2 more than the timeout)',
self.params.sleep_time)
time.sleep(self.params.sleep_time)
sleep_time)
time.sleep(sleep_time)
if __name__ == "__main__":
......
......@@ -23,15 +23,12 @@ class TrinityTest(test.Test):
:param victims_path: Path to victim files (must exist and have some bogus
files inside).
"""
default_params = {'tarball': 'trinity-1.4.tar.bz2',
'victims_path': None,
'stress': None}
def setUp(self):
"""
Build trinity.
"""
tarball_path = self.get_data_path(self.params.tarball)
tarball_path = self.get_data_path(self.params.get('tarball'))
archive.extract(tarball_path, self.srcdir)
self.srcdir = os.path.join(self.srcdir, 'trinity-1.4')
os.chdir(self.srcdir)
......@@ -46,10 +43,10 @@ class TrinityTest(test.Test):
cmd = './trinity -I'
process.run(cmd)
cmd = './trinity'
if self.params.stress:
cmd += " " + self.params.stress
if self.params.victims_path:
cmd += " -V " + self.params.victims_path
if self.params.get('stress'):
cmd += " " + self.params.get('stress')
if self.params.get('victims_path'):
cmd += " -V " + self.params.get('victims_path')
else:
cmd += " -V " + self.victims_path
process.run(cmd)
......
......@@ -11,23 +11,22 @@ class WhiteBoard(test.Test):
"""
Simple test that saves test custom data to the test whiteboard
"""
default_params = {'whiteboard_data_text': 'default whiteboard text',
'whiteboard_data_file': '',
'whiteboard_data_size': '10',
'whiteboard_writes': '1'}
def runTest(self):
if self.params.whiteboard_data_file:
data_file = self.params.get('whiteboard_data_file', '')
data_size = self.params.get('whiteboard_data_size', '10')
if data_file:
self.log.info('Writing data to whiteboard from file: %s',
self.params.whiteboard_data_file)
whiteboard_file = open(self.params.whiteboard_data_file, 'r')
size = int(self.params.whiteboard_data_size)
data_file)
whiteboard_file = open(data_file, 'r')
size = int(data_size)
data = whiteboard_file.read(size)
else:
offset = int(self.params.whiteboard_data_size) - 1
data = self.params.whiteboard_data_text[0:offset]
offset = int(data_size) - 1
data = self.params.get('whiteboard_data_text',
'default whiteboard text')[0:offset]
iterations = int(self.params.whiteboard_writes)
iterations = int(self.params.get('whiteboard_writes', 1))
result = ''
for _ in xrange(0, iterations):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册