araldo / test / server_test.py

""" Tests for module araldo.server
"""

import pytest
from optparse import OptionError
from mock import Mock, patch, call
from araldo.server import (
    start_server, setup_logging, setup_sending,
    setup_signals, sig_handler, setup_plugins)
from gevent.queue import Queue
from araldo import server
from araldo.plugin_manager import PluginManager
from araldo.config import Config


@pytest.fixture
def queue():
    return Queue()


@pytest.fixture
def conf1_name():
    return "testfiles/conf1.yaml"


@pytest.fixture
def config(conf1_name):
    return Config.create(conf1_name)


@pytest.fixture
def plugin_manager(config):
    return PluginManager(config)


def test_server(plugin_manager, queue):
    conf_mock = Mock()
    start_mock = Mock()
    start_server(9000, conf_mock, queue, plugin_manager, start_mock)
    assert start_mock.called


def test_setup_logging(config):
    setup_logging(config)
    #assert len(logger.handlers) > 0


def test_setup_sending(queue, plugin_manager):
    config = Config.get()
    config.settings()["routes"] = {}
    setup_sending(config, queue, plugin_manager)


def test_setup_signals():
    with patch.object(server.signal, "signal"):
        setup_signals()


def test_setup_plugins(conf1_name):
    config = Config(conf1_name)
    logger = Mock()
    setup_plugins(logger, config)


def test_sig_handler():
    with patch.object(server.sys, "exit") as ex:
        sig_handler(None, None)
        assert ex.called


def test_parse_args_explicit_conf_long_option():
    with patch.object(server.os.path, "exists") as ex:
        ex.return_value = True
        parsre, options, args = server.parse_args(["--config", "myconf.yaml"])
        assert options.config == "myconf.yaml"


def test_parse_args_explicit_conf():
    with patch.object(server.os.path, "exists") as ex:
        ex.return_value = True
        parser, options, args = server.parse_args(["-c", "myconf.yaml"])
        assert options.config == "myconf.yaml"


def test_parse_args_implicit_conf():
    parser, options, args = server.parse_args([])
    assert options.config == "araldo.yaml"


def test_nonexistant_config_file():
    parser, options, args = server.parse_args([])
    with pytest.raises(OptionError):
        with patch.object(server.os.path, "exists") as ex:
            ex.return_value = False
            server.parse_args(["-c", "nonexistant.yaml"])
            assert ex.call_args == call("nonexistant.yaml")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.