summaryrefslogtreecommitdiff
path: root/cleopatre/devkit/tests/fw_wd/run-test
diff options
context:
space:
mode:
authorNicolas Schodet2011-12-08 17:28:15 +0100
committerNicolas Schodet2011-12-20 15:49:31 +0100
commitb8fe0c67430ec147317fd994d702d2328471b99a (patch)
tree4fbbe84a1e5deddbc5dba46b1c96381d7134d8e4 /cleopatre/devkit/tests/fw_wd/run-test
parent6aaa2d5c07911b9e04b63cff40049e92ba93abf7 (diff)
cleo/app/fw_wd: refactor fwwatchd code, add tests
Diffstat (limited to 'cleopatre/devkit/tests/fw_wd/run-test')
-rwxr-xr-xcleopatre/devkit/tests/fw_wd/run-test224
1 files changed, 224 insertions, 0 deletions
diff --git a/cleopatre/devkit/tests/fw_wd/run-test b/cleopatre/devkit/tests/fw_wd/run-test
new file mode 100755
index 0000000000..5008933233
--- /dev/null
+++ b/cleopatre/devkit/tests/fw_wd/run-test
@@ -0,0 +1,224 @@
+#!/usr/bin/python
+"""Test FW watch daemon."""
+from shutil import copy, rmtree
+from os import symlink, mkdir, chmod
+import sys
+import subprocess
+import gzip
+
+busybox_path = '../common/busybox/busybox'
+fw_wd_path = '../../../application/fw_wd'
+
+trace_content = """\
+Test trace line 1
+Test trace line 2
+Test trace line 3
+Test trace line 4
+Test trace line 5
+"""
+
+trace_files_content = {
+'/etc/hpav.info': """\
+hpav.info file line 1
+hpav.info file line 2
+""",
+'/proc/uptime': """\
+uptime line 1
+""",
+}
+
+fw_wd_stub = """\
+#!/bin/sh
+test -f /tmp/fw_wd_run && echo "fw_wd stop test" && exit 1
+touch /tmp/fw_wd_run
+echo "fw_wd run"
+exit %(retcode)s
+"""
+
+reboot_stub = """\
+#!/bin/sh
+echo "reboot run"
+exit %(retcode)s
+"""
+
+default_content = """\
+REBOOT=%(reboot)s
+TRACE_ENABLED=%(trace_enabled)s
+TRACE_FOLDER="/usr/local/trace/"
+TRACE_FILES="%(trace_files)s"
+TRACE_CMDS="%(trace_cmds)s"
+"""
+
+chroot_env = {
+ 'PATH': '/bin:/sbin:/usr/bin:/usr/sbin',
+ 'USER': 'testuser',
+ 'SHELL': '/bin/sh',
+ }
+
+trace_cmds = [ [ ], [ 'echo choucroute' ] ]
+trace_cmds_result = [ [ ], [ 'choucroute\n' ] ]
+
+class TestParams:
+ def __init__(self, test_name, **kw):
+ self.test_name = test_name
+ self.fw_wd_retcode = 0
+ self.reboot = True
+ self.reboot_retcode = 0
+ self.trace_enabled = True
+ self.trace_present = True
+ self.trace_files = [ '/proc/uptime', '/etc/hpav.info' ]
+ self.trace_cmds_test = False
+ self.trace_dir = True
+ self.old_traces = [ ]
+ self.__dict__.update(kw)
+
+def chroot_call(root_path, command):
+ """Call a command in the chrooted environment, raise error on any
+ problem."""
+ subprocess.check_call([ 'fakechroot', '/usr/sbin/chroot', root_path ] +
+ command)
+
+def create_file(root_path, filename, content, executable=False):
+ """Create a text file in rootfs with given content. Give execution
+ permissions if requested."""
+ f = open(root_path + filename, 'w')
+ f.write(content)
+ f.close()
+ if executable:
+ chmod(root_path + filename, 0755)
+
+def create_rootfs(root_path, test_params):
+ """Create a full rootfs from given test parameters. Clean up if rootfs is
+ existent."""
+ # Create basic rootfs.
+ rmtree(root_path, ignore_errors=True)
+ mkdir(root_path)
+ for p in [ '/bin', '/sbin', '/usr', '/usr/bin', '/usr/sbin', '/usr/local',
+ '/proc', '/proc/self', '/dev', '/etc', '/etc/default', '/tmp' ]:
+ mkdir(root_path + p)
+ copy(busybox_path, root_path + '/bin')
+ symlink('/bin/busybox', root_path + '/proc/self/exe')
+ chroot_call(root_path, [ '/bin/busybox', '--install', '-s' ])
+ copy(fw_wd_path + '/fwwatchd', root_path + '/usr/bin')
+ # Create test stubs.
+ create_file(root_path, '/usr/bin/fw_wd',
+ fw_wd_stub % { 'retcode': test_params.fw_wd_retcode },
+ executable=True)
+ create_file(root_path, '/sbin/reboot',
+ reboot_stub % { 'retcode': test_params.reboot_retcode },
+ executable=True)
+ if test_params.trace_present:
+ create_file(root_path, '/dev/trace', trace_content)
+ for f in test_params.trace_files:
+ create_file(root_path, f, trace_files_content[f])
+ if test_params.trace_dir:
+ mkdir(root_path + '/usr/local/trace')
+ for i in test_params.old_traces:
+ create_file(root_path, '/usr/local/trace/trace_%d.gz' % i,
+ 'Old trace %d' % i)
+ # Create default file.
+ def bool_value(b):
+ return 'true' if b else 'false'
+ def list_value(l):
+ return ':'.join(l)
+ create_file(root_path, '/etc/default/fwwatchd', default_content % {
+ 'reboot': bool_value(test_params.reboot),
+ 'trace_enabled': bool_value(test_params.trace_enabled),
+ 'trace_files': list_value(test_params.trace_files),
+ 'trace_cmds': list_value(trace_cmds[test_params.trace_cmds_test]),
+ })
+
+def check(root_path, test_params, stdout):
+ """Check test result."""
+ # Check stdout.
+ if not test_params.trace_enabled:
+ expected_stdout = ''
+ else:
+ expected_stdout = 'fw_wd run\n'
+ if test_params.fw_wd_retcode == 0:
+ if test_params.reboot:
+ expected_stdout += 'reboot run\n'
+ if test_params.reboot_retcode == 0:
+ expected_stdout += 'fw_wd stop test\n'
+ if expected_stdout != stdout:
+ print test_params.test_name, "failed (%s != %s)" % (repr(stdout),
+ repr(expected_stdout))
+ return False
+ # Check trace file.
+ expected_trace = ''
+ expected_last_head_trace = ''
+ if test_params.trace_enabled and test_params.fw_wd_retcode == 0:
+ if test_params.trace_present:
+ expected_last_head_trace = \
+ '\n'.join(trace_content.split('\n')[0:3]) + '\n'
+ expected_trace += expected_last_head_trace + trace_content
+ for f in test_params.trace_files:
+ expected_trace += "==== %s ====\n" % f
+ expected_trace += trace_files_content[f]
+ expected_trace += "\n"
+ for cmd, result in zip(trace_cmds[test_params.trace_cmds_test],
+ trace_cmds_result[test_params.trace_cmds_test]):
+ expected_trace += "==== %s ====\n" % cmd
+ expected_trace += result
+ expected_trace += "\n"
+ expected_trace_nb = max(test_params.old_traces + [ 0 ] ) + 1
+ expected_file_name = '/usr/local/trace/trace_%d.gz' % expected_trace_nb
+ try:
+ f = gzip.open(root_path + expected_file_name)
+ if not expected_trace:
+ print test_params.test_name, \
+ "failed (%s exists)" % expected_file_name
+ return False
+ else:
+ trace = f.read()
+ if expected_trace != trace:
+ print test_params.test_name, "failed"
+ print "expected trace:\n", expected_trace
+ print "trace:\n", trace
+ return False
+ f.close()
+ except IOError:
+ if expected_trace:
+ print test_params.test_name, \
+ "failed (can not open %s)" % expected_file_name
+ return False
+ return True
+
+def run_test(root_path, test_params):
+ """Run FW watch daemon in a chrooted environment and check result. Return
+ a number of failed test."""
+ create_rootfs('root', test_params)
+ stdout = subprocess.Popen([ 'fakechroot', '/usr/sbin/chroot', root_path,
+ '/usr/bin/fwwatchd' ], stdout=subprocess.PIPE, stderr=subprocess.PIPE,
+ env=chroot_env).communicate()[0]
+ result = check('root', test_params, stdout)
+ return 0 if result else 1
+
+if __name__ == '__main__':
+ failed = 0
+ tp = TestParams("default params")
+ failed += run_test('root', tp)
+ # Watchdog test.
+ tp = TestParams("fw_wd fail", fw_wd_retcode=1)
+ failed += run_test('root', tp)
+ # Trace tests.
+ tp = TestParams("trace disabled", trace_enabled=False)
+ failed += run_test('root', tp)
+ tp = TestParams("trace not available", trace_present=False)
+ failed += run_test('root', tp)
+ tp = TestParams("no trace files", trace_files=[ ])
+ failed += run_test('root', tp)
+ tp = TestParams("trace commands", trace_cmds_test=True)
+ failed += run_test('root', tp)
+ tp = TestParams("no trace dir", trace_dir=False)
+ failed += run_test('root', tp)
+ tp = TestParams("old traces", old_traces=[ 1, 2, 8 ])
+ failed += run_test('root', tp)
+ # Reboot tests.
+ tp = TestParams("no reboot", reboot=False)
+ failed += run_test('root', tp)
+ tp = TestParams("reboot fail", reboot_retcode=1)
+ failed += run_test('root', tp)
+ # Bye.
+ print "failed %d tests" % failed
+ sys.exit (1 if failed else 0)