def test_can_select_device_loaded_from_config_dict(moler_config, device_factory):
    ConnectionObserver.get_unraised_exceptions(True)

    @MolerTest.raise_background_exceptions(check_steps_end=True)
    def can_select_device_loaded_from_config_dict(moler_config, device_factory):
        conn_config = {
            'LOGGER': {
                'PATH': '/tmp/',
                'RAW_LOG': True,
                'DATE_FORMAT': '%d %H:%M:%S'
            },
            'DEVICES': {
                'UNIX_LOCAL': {
                    'DEVICE_CLASS': 'moler.device.unixlocal.UnixLocal',
                    'INITIAL_STATE': 'UNIX_LOCAL'
                }
            }
        }
        moler_config.load_config(config=conn_config, config_type='dict')

        device = device_factory.get_device(name='UNIX_LOCAL')

        assert device.__module__ == 'moler.device.unixlocal'
        assert device.__class__.__name__ == 'UnixLocal'

        device.__del__()

        MolerTest.steps_end()
    can_select_device_loaded_from_config_dict(moler_config, device_factory)
예제 #2
0
def connection_observer_major_base_class(request):
    module_name, class_name = request.param.rsplit('.', 1)
    module = importlib.import_module('moler.{}'.format(module_name))
    klass = getattr(module, class_name)
    yield klass
    # remove exceptions collected inside ConnectionObserver
    ConnectionObserver.get_unraised_exceptions(remove=True)
예제 #3
0
def test_moler_test_exception_no_exception():
    ConnectionObserver.get_unraised_exceptions()
    from moler.cmd.unix.ls import Ls
    cmd = Ls(connection=None)
    cmd.set_exception("wrong exception")
    with pytest.raises(MolerStatusException):
        moler_test_not_raise_exception_when_no_steps_end_for_global_method()
예제 #4
0
def connection_observer(observer_runner):
    from moler.threaded_moler_connection import ThreadedMolerConnection
    moler_conn = ThreadedMolerConnection()
    observer = NetworkDownDetector(connection=moler_conn, runner=observer_runner)
    yield observer
    # remove exceptions collected inside ConnectionObserver
    ConnectionObserver.get_unraised_exceptions(remove=True)
예제 #5
0
def test_exception_in_observer_is_ignored_if_no_result_called_nor_decorator_on_function(do_nothing_connection_observer,
                                                                                        ObserverExceptionClass):
    def function_using_observer():
        observer = do_nothing_connection_observer
        observer.set_exception(ObserverExceptionClass("some error inside observer"))

    function_using_observer()  # should not raise so test should pass
    ConnectionObserver.get_unraised_exceptions()
예제 #6
0
def async_runner(request):
    module_name, class_name = request.param.rsplit('.', 1)
    module = importlib.import_module('moler.{}'.format(module_name))
    runner_class = getattr(module, class_name)
    runner = runner_class()
    yield runner
    # remove exceptions collected inside ConnectionObserver
    ConnectionObserver.get_unraised_exceptions(remove=True)
    runner.shutdown()
예제 #7
0
def observer_runner(request):
    module_name, class_name = request.param.rsplit('.', 1)
    module = importlib.import_module('moler.{}'.format(module_name))
    runner_class = getattr(module, class_name)
    runner = runner_class()
    # NOTE: AsyncioRunner given here will start without running event loop
    yield runner
    # remove exceptions collected inside ConnectionObserver
    ConnectionObserver.get_unraised_exceptions(remove=True)
    runner.shutdown()
예제 #8
0
def do_nothing_connection_observer():
    from moler.connection_observer import ConnectionObserver

    class DoNothingObserver(ConnectionObserver):
        def data_received(self, data, recv_time):  # we need to overwrite it since it is @abstractmethod
            pass  # ignore incoming data

    observer = DoNothingObserver()

    ConnectionObserver.get_unraised_exceptions()
    yield observer
    ConnectionObserver.get_unraised_exceptions()
예제 #9
0
def test_connection_observer_is_done_after_setting_exception(
        do_nothing_connection_observer__for_major_base_class):
    connection_observer = do_nothing_connection_observer__for_major_base_class
    assert not connection_observer.done()
    # We start feeding connection-observer with data coming from connection
    # till it decides "ok, I've found something what indicates error-condition"
    #                  or it catches exception of it's internal processing
    # and it internally sets the exception
    connection_observer.set_exception(IndexError())
    assert connection_observer.done()

    ConnectionObserver.get_unraised_exceptions()
예제 #10
0
def test_exception_in_observer_is_raised_if_no_result_called_but_parameterless_decorator_on_function(
        do_nothing_connection_observer, ObserverExceptionClass):
    from moler.util.moler_test import MolerTest
    exc = ObserverExceptionClass("some error inside observer")

    @MolerTest.raise_background_exceptions
    def function_using_observer():
        observer = do_nothing_connection_observer
        observer.set_exception(exc)

    with pytest.raises(ExecutionException) as err:
        function_using_observer()
    ConnectionObserver.get_unraised_exceptions()
예제 #11
0
def test_calling_result_on_exception_broken_connection_observer_raises_that_exception(
        do_nothing_connection_observer__for_major_base_class):
    connection_observer = do_nothing_connection_observer__for_major_base_class
    # We start feeding connection-observer with data coming from connection
    # till it decides "ok, I've found something what indicates error-condition"
    #                  or it catches exception of it's internal processing
    # and it internally sets the exception
    index_err = IndexError()
    connection_observer.set_exception(index_err)
    with pytest.raises(IndexError) as error:
        connection_observer.result()
    assert error.value == index_err

    ConnectionObserver.get_unraised_exceptions()
예제 #12
0
def failing_net_down_detector(fail_on_data, fail_by_raising, runner):
    from moler.threaded_moler_connection import ThreadedMolerConnection

    class FailingNetworkDownDetector(NetworkDownDetector):
        def data_received(self, data, recv_time):
            if data == fail_on_data:
                raise fail_by_raising
            return super(FailingNetworkDownDetector, self).data_received(data, recv_time)

    moler_conn = ThreadedMolerConnection()
    failing_detector = FailingNetworkDownDetector(connection=moler_conn, runner=runner)
    yield failing_detector
    # remove exceptions collected inside ConnectionObserver
    ConnectionObserver.get_unraised_exceptions(remove=True)
예제 #13
0
def test_exception_in_observer_is_raised_if_no_result_called_but_decorator_on_class(
        do_nothing_connection_observer, ObserverExceptionClass):
    from moler.util.moler_test import MolerTest
    exc = ObserverExceptionClass("some error inside observer")

    @MolerTest.raise_background_exceptions()
    class MyTest(object):
        def method_using_observer(self):
            observer = do_nothing_connection_observer
            observer.set_exception(exc)

    with pytest.raises(ExecutionException) as err:
        MyTest().method_using_observer()
    ConnectionObserver.get_unraised_exceptions()
예제 #14
0
def test_exception_in_observer_is_raised_when_result_is_called_after_set_exception(
        do_nothing_connection_observer, ObserverExceptionClass):
    exc = ObserverExceptionClass("some error inside observer")

    def function_using_observer():
        observer = do_nothing_connection_observer
        # for real usage observer should be started to run background thread that will set_exception()
        # but for unit tests we just call it (simulating background thread)
        observer.set_exception(exc)
        observer.result()

    with pytest.raises(ObserverExceptionClass) as err:
        function_using_observer()
    assert err.value == exc
    ConnectionObserver.get_unraised_exceptions()
예제 #15
0
def test_exception_in_observer_is_raised_if_no_result_called_but_decorator_on_method(
        do_nothing_connection_observer, ObserverExceptionClass):
    from moler.util.moler_test import MolerTest
    exc = ObserverExceptionClass("some error inside observer")

    class MyTest(object):
        @MolerTest.raise_background_exceptions()
        # @MolerTest.raise_background_exceptions  # doesn't work since it is created by python and given class as first argument
        #                                               # compare with syntax of @pytest.fixture  @pytest.yield_fixture
        def method_using_observer(self):
            observer = do_nothing_connection_observer
            observer.set_exception(exc)

    with pytest.raises(ExecutionException) as err:
        MyTest().method_using_observer()
    ConnectionObserver.get_unraised_exceptions()
예제 #16
0
    def _check_exceptions_occured(caught_exception=None):
        unraised_exceptions = ConnectionObserver.get_unraised_exceptions(True)
        occured_exceptions = list()
        for unraised_exception in unraised_exceptions:
            occured_exceptions.append(unraised_exception)
            MolerTest.error(
                "Unhandled exception: '{}'".format(unraised_exception))
        if caught_exception:
            occured_exceptions.append(caught_exception)

        was_error_in_last_execution = MolerTest._was_error
        err_msg = ""

        if was_error_in_last_execution:
            err_msg += "There were error messages in Moler execution. Please check Moler logs for details.\n"
        if len(occured_exceptions) > 0:
            err_msg += "There were unhandled exceptions in Moler.\n"
            for exc in occured_exceptions:
                try:
                    import traceback
                    exc_traceback = ' '.join(
                        traceback.format_tb(exc.__traceback__))
                    err_msg += "{}{}".format(exc_traceback, repr(exc))
                except AttributeError:
                    err_msg += repr(exc)
        if err_msg:
            MolerTest.error(err_msg)
            MolerTest._was_error = False
            raise MolerStatusException(err_msg, occured_exceptions)
예제 #17
0
파일: moler_test.py 프로젝트: nokia/moler
    def _prepare_err_msg(cls, caught_exception):
        was_error_in_last_execution = cls._was_error
        err_msg = ""
        get_traceback = False

        unraised_exceptions = ConnectionObserver.get_unraised_exceptions(True)
        occured_exceptions = list()
        for unraised_exception in unraised_exceptions:
            occured_exceptions.append(unraised_exception)
        if caught_exception:
            occured_exceptions.append(caught_exception)

        if was_error_in_last_execution:
            err_msg += "Moler caught some error messages during execution. Please check Moler logs for details.\n"
        if len(occured_exceptions) > 0:
            err_msg += "There were unhandled exceptions from test caught by Moler.\n"
            for i, exc in enumerate(occured_exceptions, 1):
                if hasattr(exc, '__traceback__'):
                    exc_traceback = ' '.join(
                        traceback.format_tb(exc.__traceback__))
                    err_msg += "  ({}) {}{}\n".format(i, exc_traceback,
                                                      repr(exc))
                else:
                    get_traceback = True
            if get_traceback:
                err_msg += "  {}\n".format(cls._get_tracebacks())

        if len(cls._list_of_errors) > 0:
            err_msg += "Moler caught some error messages during execution:\n"

            for i, msg in enumerate(cls._list_of_errors, 1):
                err_msg += "  {}) >>{}<<\n".format(i, msg)

        return err_msg
예제 #18
0
    def _prepare_err_msg(caught_exception):
        was_error_in_last_execution = MolerTest._was_error
        err_msg = ""

        unraised_exceptions = ConnectionObserver.get_unraised_exceptions(True)
        occured_exceptions = list()
        for unraised_exception in unraised_exceptions:
            occured_exceptions.append(unraised_exception)
        if caught_exception:
            occured_exceptions.append(caught_exception)

        if was_error_in_last_execution:
            err_msg += "There were error messages in Moler execution. Please check Moler logs for details.\n"
        if len(occured_exceptions) > 0:
            err_msg += "There were unhandled exceptions in Moler.\n"
            for i, exc in enumerate(occured_exceptions, 1):
                try:
                    import traceback
                    exc_traceback = ' '.join(
                        traceback.format_tb(exc.__traceback__))
                    err_msg += "  {}) {}{}\n".format(i, exc_traceback,
                                                     repr(exc))
                except AttributeError:
                    err_msg += repr(exc)

        if len(MolerTest._list_of_errors) > 0:
            err_msg += "There were error messages in Moler execution:\n"

            for i, msg in enumerate(MolerTest._list_of_errors, 1):
                err_msg += "  {}) >>{}<<\n".format(i, msg)

        return err_msg
예제 #19
0
def test_exception_in_observer_is_raised_if_no_result_called_but_parameterless_decorator_on_staticmethod(
        do_nothing_connection_observer, ObserverExceptionClass):
    from moler.util.moler_test import MolerTest
    exc = ObserverExceptionClass("some error inside observer")

    with pytest.raises(ExecutionException) as err:

        class MyTest(object):
            # TODO: Add later
            @MolerTest.raise_background_exceptions
            @staticmethod
            def method_using_observer():
                observer = do_nothing_connection_observer
                observer.set_exception(exc)

        MyTest.method_using_observer()
    ConnectionObserver.get_unraised_exceptions()
예제 #20
0
def test_connection_observer_exception_do_not_remove():
    ConnectionObserver.get_unraised_exceptions(True)
    time.sleep(0.1)
    from moler.cmd.unix.ls import Ls
    from moler.exceptions import CommandTimeout
    from moler.exceptions import WrongUsage
    cmd = Ls(None)
    none_exceptions = ConnectionObserver.get_unraised_exceptions(True)
    assert 0 == len(none_exceptions)
    cmd.set_exception(CommandTimeout(cmd, 0.1))
    cmd._is_done = True
    active_exceptions = ConnectionObserver.get_unraised_exceptions(False)
    assert 1 == len(active_exceptions)
    cmd = Ls(None)
    ctoe = CommandTimeout(cmd, 0.1)
    cwue = WrongUsage(cmd, "Another exception")
    cmd.set_exception(ctoe)
    cmd._is_done = True
    cmd.set_exception(cwue)
    active_exceptions = ConnectionObserver.get_unraised_exceptions(False)
    assert ctoe == active_exceptions[1]
    assert 2 == len(active_exceptions)
    active_exceptions = ConnectionObserver.get_unraised_exceptions(True)
    assert 2 == len(active_exceptions)
    none_exceptions = ConnectionObserver.get_unraised_exceptions(True)
    assert 0 == len(none_exceptions)
예제 #21
0
def test_exception_in_observer_is_raised_if_no_result_called_but_parameterless_decorator_on_derived_class(
        do_nothing_connection_observer, ObserverExceptionClass):
    from moler.util.moler_test import MolerTest
    exc = ObserverExceptionClass("some error inside observer")

    class MyTestBase(object):
        def method_using_observer(self):
            observer = do_nothing_connection_observer
            observer.set_exception(exc)

    @MolerTest.raise_background_exceptions
    class MyTest(MyTestBase):
        def method_of_derived_class(self):
            pass

    with pytest.raises(MolerStatusException) as err:
        MyTest().method_using_observer()
    ConnectionObserver.get_unraised_exceptions()
예제 #22
0
def test_log_error_in_next_test_when_previous_set_exception(
        do_nothing_connection_observer, ObserverExceptionClass):
    exc = ObserverExceptionClass("some error inside observer")

    def function_using_observer_and_set_exception():
        observer = do_nothing_connection_observer
        # for real usage observer should be started to run background thread that will set_exception()
        # but for unit tests we just call it (simulating background thread)
        observer.set_exception(exc)

    @MolerTest.raise_background_exceptions(check_steps_end=True)
    def function_using_observer():
        observer = do_nothing_connection_observer
        # for real usage observer should be started to run background thread that will set_exception()
        # but for unit tests we just call it (simulating background thread)
        observer.result()
        MolerTest.steps_end()

    function_using_observer_and_set_exception()

    with pytest.raises(ExecutionException) as err:
        function_using_observer()
    assert "some error inside observer" in str(err.value)
    ConnectionObserver.get_unraised_exceptions()
def test_connection_observer_exception_do_not_remove():
    ConnectionObserver.get_unraised_exceptions(True)
    time.sleep(0.1)
    from moler.cmd.unix.ls import Ls
    from moler.exceptions import CommandTimeout
    cmd = Ls(None)
    none_exceptions = ConnectionObserver.get_unraised_exceptions(True)
    assert 0 == len(none_exceptions)
    cmd.set_exception(CommandTimeout(cmd, 0.1))
    active_exceptions = ConnectionObserver.get_unraised_exceptions(False)
    assert 1 == len(active_exceptions)
    cmd = Ls(None)
    cmd.set_exception(CommandTimeout(cmd, 0.1))
    active_exceptions = ConnectionObserver.get_unraised_exceptions(False)
    assert 2 == len(active_exceptions)
    active_exceptions = ConnectionObserver.get_unraised_exceptions(True)
    assert 2 == len(active_exceptions)
    none_exceptions = ConnectionObserver.get_unraised_exceptions(True)
    assert 0 == len(none_exceptions)
예제 #24
0
    def _final_check(caught_exception=None, check_steps_end=True):
        exceptions = ConnectionObserver.get_unraised_exceptions(True)
        unhandled_exceptions = list()
        for exception in exceptions:
            unhandled_exceptions.append(exception)
            MolerTest.error("Unhandled exception: '{}'".format(exception))
        if caught_exception:
            unhandled_exceptions.append(caught_exception)

        was_error_in_last_execution = MolerTest._was_error
        err_msg = ""

        if check_steps_end and not MolerTest._was_steps_end:
            err_msg += "Method steps_end() was not called.\n"
        if was_error_in_last_execution:
            err_msg += "There were error messages in Moler execution. Please check Moler logs for details.\n"
        if len(unhandled_exceptions) > 0:
            err_msg += "There were unhandled exceptions in Moler.\n"
        if err_msg or len(unhandled_exceptions) > 0:
            MolerTest.error(err_msg)
            MolerTest._was_error = False
            raise MolerStatusException(err_msg, unhandled_exceptions)
def test_connection_observer_one_exception():
    ConnectionObserver.get_unraised_exceptions(True)
    time.sleep(0.1)
    from moler.cmd.unix.ls import Ls
    from moler.exceptions import CommandTimeout
    cmd = Ls(None)
    none_exceptions = ConnectionObserver.get_unraised_exceptions(True)
    assert 0 == len(none_exceptions)
    cmd.set_exception(CommandTimeout(cmd, 0.1))
    active_exceptions = ConnectionObserver.get_unraised_exceptions(True)
    assert 1 == len(active_exceptions)
    try:
        cmd.result()
    except CommandTimeout:
        pass
    none_exceptions = ConnectionObserver.get_unraised_exceptions(True)
    assert 0 == len(none_exceptions)
    none_exceptions = ConnectionObserver.get_unraised_exceptions(True)
    assert 0 == len(none_exceptions)
예제 #26
0
def test_moler_test_not_raise_exception_when_no_steps_end(moler_test):
    ConnectionObserver.get_unraised_exceptions()
    moler_test.test_not_raise_exception_when_no_steps_end()
예제 #27
0
def test_moler_test_raise_exception_when_log_error_raise_exception_set(
        moler_test_se):
    ConnectionObserver.get_unraised_exceptions()
    with pytest.raises(MolerStatusException):
        moler_test_se.test_raise_exception_when_log_error_raise_exception_set()
예제 #28
0
def test_moler_test_test_raise_exception_when_not_call_steps_end(
        moler_test_se):
    ConnectionObserver.get_unraised_exceptions()
    with pytest.raises(MolerStatusException):
        moler_test_se.test_raise_exception_when_not_call_steps_end()
예제 #29
0
def test_moler_test_wrapper():
    ConnectionObserver.get_unraised_exceptions()
    decorated = moler_test_raise_exception_when_no_steps_end_for_global_method
    ret = MolerTest._wrapper(decorated, False)
    assert decorated == ret
예제 #30
0
def test_moler_test_raise_exception_when_not_callable_passed():
    ConnectionObserver.get_unraised_exceptions()
    var = "no callable"
    with pytest.raises(MolerStatusException):
        MolerTest._decorate(var)