Exemplo n.º 1
0
    def test_init(self):
        with mock.patch("gosa.common.network.DBusRunner.get_instance") as m_runner,\
                mock.patch("gosa.common.network.getLogger") as m_logger:
            m_runner.return_value.get_system_bus.return_value.get_object.side_effect = Exception("test error")
            monitor = Monitor()

            # default value is 'online'
            assert monitor.is_online()
            m_logger.return_value.warning.assert_called_with("no network-manager detected: defaulting to state 'online'")
Exemplo n.º 2
0
    def test_old_version(self):
        self.dbus_props.Set("org.freedesktop.NetworkManager", "Version",
                            "0.8.7.0")
        self.dbus_props.Set(
            "org.freedesktop.NetworkManager", "State",
            dbus.UInt32(NM_STATE_DISCONNECTED, variant_level=1))

        monitor = Monitor()
        assert monitor.is_online()
Exemplo n.º 3
0
    def test_init(self):
        with mock.patch("gosa.common.network.DBusRunner.get_instance") as m_runner,\
                mock.patch("gosa.common.network.getLogger") as m_logger:
            m_runner.return_value.get_system_bus.return_value.get_object.side_effect = Exception("test error")
            monitor = Monitor()

            # default value is 'online'
            assert monitor.is_online()
            m_logger.return_value.warning.assert_called_with("no network-manager detected: defaulting to state 'online'")
Exemplo n.º 4
0
    def test_network_offline(self):
        self.dbus_props.Set("org.freedesktop.NetworkManager", "Version", "0.9.7.0")
        self.dbus_props.Set("org.freedesktop.NetworkManager", "State", dbus.UInt32(NM_STATE_CONNECTED_GLOBAL, variant_level=1))

        callback = mock.MagicMock()
        monitor = Monitor(callback)

        assert monitor.is_online()

        self.emit_signal(self.obj_network, 'StateChanged', 'u', [NM_STATE_CONNECTED_SITE])
        assert monitor.is_online()
        callback.assert_called_with(True)
Exemplo n.º 5
0
    def test_network_offline(self):
        self.dbus_props.Set("org.freedesktop.NetworkManager", "Version", "0.9.7.0")
        self.dbus_props.Set("org.freedesktop.NetworkManager", "State", dbus.UInt32(NM_STATE_CONNECTED_GLOBAL, variant_level=1))

        callback = mock.MagicMock()
        monitor = Monitor(callback)

        assert monitor.is_online()

        self.emit_signal(self.obj_network, 'StateChanged', 'u', [NM_STATE_CONNECTED_SITE])
        assert monitor.is_online()
        callback.assert_called_with(True)
Exemplo n.º 6
0
Arquivo: main.py Projeto: peuter/gosa
def mainLoop(env):
    global netstate, dr

    # Enable DBus runner
    dr = DBusRunner()
    dr.start()

    # Do network monitoring
    nm = Monitor(netactivity)
    netactivity(nm.is_online())

    """ Main event loop which will process all registered threads in a loop.
        It will run as long env.active is set to True."""
    try:
        log = logging.getLogger(__name__)

        while True:

            # Check netstate and wait until we're back online
            if not netstate:
                log.info("waiting for network connectivity")
            while not netstate:
                time.sleep(1)

            # Load plugins
            PluginRegistry(component='gosa.client.module')

            # Sleep and slice
            wait = 2
            while True:
                # Threading doesn't seem to work well with python...
                for p in env.threads:

                    # Bail out if we're active in the meanwhile
                    if not env.active:
                        break

                    p.join(wait)

                # No break, go to main loop
                else:
                    continue

                # Break, leave main loop
                break

            # Break, leave main loop
            if not env.reset_requested:
                break

            # Wait for threads to shut down
            for t in env.threads:
                if hasattr(t, 'stop'):
                    t.stop()
                if hasattr(t, 'cancel'):
                    t.cancel()
                t.join(wait)

                #TODO: remove me
                if t.is_alive():
                    try:
                        t._stop()
                    except:
                        print(str(t.getName()) + ' could not be terminated')

            # Lets do an environment reset now
            PluginRegistry.shutdown()

            # Make us active and loop from the beginning
            env.reset_requested = False
            env.active = True

            if not netstate:
                log.info("waiting for network connectivity")
            while not netstate:
                time.sleep(1)

            sleep = randint(30, 60)
            env.log.info("waiting %s seconds to try an MQTT connection recovery" % sleep)
            time.sleep(sleep)

    except Exception as detail:
        log.critical("unexpected error in mainLoop")
        log.exception(detail)
        log.debug(traceback.format_exc())

    except KeyboardInterrupt:
        log.info("console requested shutdown")

    finally:
        shutdown()
Exemplo n.º 7
0
Arquivo: main.py Projeto: peuter/gosa
def mainLoop(env):
    global netstate, dr

    # Enable DBus runner
    dr = DBusRunner()
    dr.start()

    # Do network monitoring
    nm = Monitor(netactivity)
    netactivity(nm.is_online())
    """ Main event loop which will process all registered threads in a loop.
        It will run as long env.active is set to True."""
    try:
        log = logging.getLogger(__name__)

        while True:

            # Check netstate and wait until we're back online
            if not netstate:
                log.info("waiting for network connectivity")
            while not netstate:
                time.sleep(1)

            # Load plugins
            PluginRegistry(component='gosa.client.module')

            # Sleep and slice
            wait = 2
            while True:
                # Threading doesn't seem to work well with python...
                for p in env.threads:

                    # Bail out if we're active in the meanwhile
                    if not env.active:
                        break

                    p.join(wait)

                # No break, go to main loop
                else:
                    continue

                # Break, leave main loop
                break

            # Break, leave main loop
            if not env.reset_requested:
                break

            # Wait for threads to shut down
            for t in env.threads:
                if hasattr(t, 'stop'):
                    t.stop()
                if hasattr(t, 'cancel'):
                    t.cancel()
                t.join(wait)

                #TODO: remove me
                if t.is_alive():
                    try:
                        t._stop()
                    except:
                        print(str(t.getName()) + ' could not be terminated')

            # Lets do an environment reset now
            PluginRegistry.shutdown()

            # Make us active and loop from the beginning
            env.reset_requested = False
            env.active = True

            if not netstate:
                log.info("waiting for network connectivity")
            while not netstate:
                time.sleep(1)

            sleep = randint(30, 60)
            env.log.info(
                "waiting %s seconds to try an MQTT connection recovery" %
                sleep)
            time.sleep(sleep)

    except Exception as detail:
        log.critical("unexpected error in mainLoop")
        log.exception(detail)
        log.debug(traceback.format_exc())

    except KeyboardInterrupt:
        log.info("console requested shutdown")

    finally:
        shutdown()
Exemplo n.º 8
0
    def test_old_version(self):
        self.dbus_props.Set("org.freedesktop.NetworkManager", "Version", "0.8.7.0")
        self.dbus_props.Set("org.freedesktop.NetworkManager", "State", dbus.UInt32(NM_STATE_DISCONNECTED, variant_level=1))

        monitor = Monitor()
        assert monitor.is_online()