class TestServerProfile(unittest.TestCase):
    def setUp(self):
        self.cfg = Config()
        self.profile = ServerProfile(self.cfg)

    def test_basics(self):
        self.cfg.load(TEST_CONFIG)
        self.assertEqual(self.profile.host, "localhost")
        self.assertEqual(self.profile.port, 6666)
        self.assertEqual(self.profile.current_profile, "default")

        self.profile.current_profile = "other"

        self.assertEqual(self.profile.host, "werkstatt")
        self.assertEqual(self.profile.port, 6666)
        self.assertEqual(self.profile.current_profile, "other")

    def test_add_server(self):
        # Test setter shortly
        self.profile.host = "werkzeug"
        self.assertEqual(self.profile.host, "werkzeug")

        self.profile.add_server("zeroconf", "Zeroconf", "new_host", port=7777)
        self.assertEqual(self.profile.host, "werkzeug")
        self.profile.current_profile = "zeroconf"
        self.assertEqual(self.profile.host, "new_host")
        self.assertEqual(self.profile.port, 7777)

        # Different port this time, try to overwrite it.
        self.profile.add_server("zeroconf", "Zeroconf", "new_host", port=9999)
        self.assertEqual(self.profile.port, 9999)
Exemple #2
0
def boot_base(verbosity=logging.INFO, protocol_machine='idle', host=None, port=None):
    '''Initialize the basic services.

    This is basically a helper to spare us a lot of init work in tests.

    Following things are done:

        - Prepare the Filesystem.
        - Initialize a pretty root logger.
        - Load the config.
        - Initialize the Plugin System.
        - Instance the client and connect to it.
        - Find out to which host/port we should connect (config and NetworProvider).
        - Make everything available under the 'g' variable;

    :protocol_machine: The pm to use for the Client. Please know what you do.
    :verbosity: Verbosity to use during bootup. You can adjust this using ``logger.getLogger(None).setLevel(logger.YourLevel)`` later.
    :returns: True if erverything worked and the client is connected.
    '''
    # prepare filesystem
    _create_file_structure()

    # All logger will inherit from the root logger,
    # so we just customize it.
    _create_logger(verbosity=verbosity)

    global LOGGER
    LOGGER = _create_logger('boot', verbosity=verbosity)

    # Set up Config
    config = cfg.Config()
    try:
        with open(g.CONFIG_FILE, 'r') as f:
            config.load(f.read())
    except FileNotFoundError:
        pass

    config.add_defaults(CONFIG_DEFAULTS)
    config.add_defaults({
        'paths': {
            'config_home': g.CONFIG_DIR,
            'cache_home': g.CACHE_DIR
        }
    })

    # Make it known.
    g.register('config', config)

    # Logging signals
    client = core.Client(protocol_machine=protocol_machine)
    g.register('client', client)

    # Redirect GLib Errors to the error signal (needs to know what client's signals)
    core.register_external_logs(client)

    # register auto-logging
    client.signal_add('connectivity', _connectivity_logger)
    client.signal_add('logging', _logging_logger)

    # Initialize the Plugin System
    psys = PluginSystem(config=g.config, extra_plugin_paths=[g.USER_PLUGIN_DIR])
    g.register('psys', psys)

    # Acquire Config Defaults for all Plugins
    for plugin in psys.list_plugin_info_by_category('ConfigDefaults'):
        name = plugin.name
        config.add_defaults({
            'plugins': {
                name: plugin.plugin_object.get_config_defaults()
            }
        })

    # do the Heartbeat stuff
    g.register('heartbeat', Heartbeat(client))

    # Make use of the Server Profiles in the config
    # Note: This needs the Pluginsystem
    server_profile = ServerProfile(config)
    g.register('server_profile', server_profile)
    server_profile.trigger_scan(psys)

    # Go into the hot phase...
    if not host and not port:
        host, port = _find_out_host_and_port()
    error = client.connect(host, port)
    if error is not None:
        logging.error("Connection Trouble: " + error)

    return client.is_connected
 def setUp(self):
     self.cfg = Config()
     self.profile = ServerProfile(self.cfg)