From 791f62eff98cca199d86609e7f660d32b0f91a55 Mon Sep 17 00:00:00 2001 From: Brent Baude Date: Fri, 17 Feb 2017 10:43:45 -0600 Subject: [PATCH] Add base integration test for dbus Define the methodology for testing dbus in integrations tests. Using a decorator, this allows us to automatically define and test dbus tests. This is only the base test to exercise the testing. Closes: #901 Approved by: rhatdan --- test.sh | 27 +++++++++++-- tests/integration/test_dbus.py | 70 ++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 4 deletions(-) create mode 100755 tests/integration/test_dbus.py diff --git a/test.sh b/test.sh index 73e7583..987b939 100755 --- a/test.sh +++ b/test.sh @@ -128,7 +128,21 @@ make_docker_images () { make_docker_images -# Python unit tests. +if [ ! -n "${PYTHON+ }" ]; then + if hash python3 > /dev/null 2>&1 /dev/null; then + PYTHON=$(hash -t python3) + elif type python3 > /dev/null 2>&1; then + PYTHON=$(type python3 | awk '{print $3}') + elif hash python2 > /dev/null 2>&1; then + PYTHON=$(hash -t python2) + elif type python2 > /dev/null 2>&1; then + PYTHON=$(type python2 | awk '{print $3}') + else + PYTHON='/usr/bin/python' + fi +fi + + echo "UNIT TESTS:" COVERAGE_BIN=${COVERAGE_BIN-"/usr/bin/coverage"} @@ -195,10 +209,13 @@ fi if [ ! -n "${TEST_UNIT+ }" ]; then - for tf in `find ./tests/integration/ -name test_*.sh`; do + for tf in `find ./tests/integration/ -name test_*`; do + bn=$(basename "$tf") + extension="${bn##*.}" if [ -n "${TEST_INTEGRATION+ }" ]; then - tfbn=$(basename "$tf" .sh) + tfbn="${bn%.*}" + tfbn="${tfbn#test_}" if [[ " $TEST_INTEGRATION " != *" $tfbn "* ]]; then continue @@ -213,7 +230,9 @@ if [ ! -n "${TEST_UNIT+ }" ]; then printf "Running %-40.40s" "$(basename ${tf})...." printf "\n==== ${tf} ====\n" >> ${LOG} - + if [ "${extension}" == "py" ]; then + tf="$PYTHON ${tf}" + fi if ${tf} &>> ${LOG}; then printf "PASS\n"; else diff --git a/tests/integration/test_dbus.py b/tests/integration/test_dbus.py new file mode 100755 index 0000000..fe40836 --- /dev/null +++ b/tests/integration/test_dbus.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python +#pylint: skip-file + +import dbus +import sys +try: + from slip.dbus import polkit +except ImportError: + sys.exit(77) +import dbus.service +import dbus.mainloop.glib + + +# Throw this error to signify at test failure +class AtomicIntegrationTestError(Exception): + def __init__(self, test_name): + super(AtomicIntegrationTestError, self).__init__("Test '{}' Failed...".format(test_name)) + + +# Wrapper class for the decorator +def integration_test(func): + def _integration_test(self): + return func(self) + return _integration_test + + +class TestDBus(): + + def __init__(self): + self.bus = dbus.SystemBus() + self.dbus_object = self.bus.get_object("org.atomic", "/org/atomic/object") + + # Add this decorator to define the method as something that should be + # tested + @integration_test + @polkit.enable_proxy + def test_scan_list(self): + results = self.dbus_object.ScanList() + assert(isinstance(results, dbus.String)) + + + +if __name__ == '__main__': + + def get_test_methods(_tb): + """ + Returns the test methods from above that have the integration_test decorator + :param _tb: TestDbus instance + """ + + test_method_names = [x for x in dir(_tb) if not x.startswith('__')] + test_methods = [] + for method in test_method_names: + _method = getattr(_tb, method) + if callable(_method) and _method.__getattribute__('__name__') == '_integration_test': + test_methods.append(method) + return test_methods + + tb = TestDBus() + test_methods = get_test_methods(tb) + + for test in reversed(test_methods): + _tb = getattr(tb, test) + try: + _tb() + except: + raise AtomicIntegrationTestError(test) + + print("Test '{}' passed.".format(test)) +