예제 #1
0
    def test_getAzraelServiceHosts_inside_docker(self, m_isInsideDocker):
        """
        Inside a container the host names for the services must match their
        service name. For instance, the 'Leonard' service must run on a host
        called 'Leonard' (not case sensitive).
        """
        default_services = {
            'clerk': ('127.0.0.1', 5555),
            'database': ('127.0.0.1', 27017),
            'leonard': ('127.0.0.1', 5556),
            'rabbitmq': ('127.0.0.1', 5672),
            'webapi': ('127.0.0.1', 8080),
        }

        # Make getAzraelServiceHosts believe we are outside a container. In
        # that case all services are assumed to run on localhost.
        m_isInsideDocker.return_value = False
        assert m_isInsideDocker.call_count == 0
        ret = azutils.getAzraelServiceHosts(etchosts=None)
        assert m_isInsideDocker.call_count == 1

        # Make getAzraelServiceHosts believe we are inside a container.
        m_isInsideDocker.return_value = True

        # If the specified /etc/hosts file does not exists then we must get the
        # default values.
        m_isInsideDocker.reset_mock()
        ret = azutils.getAzraelServiceHosts(etchosts=None)
        assert m_isInsideDocker.call_count == 1
        assert ret == default_services
        ret = azutils.getAzraelServiceHosts(etchosts='/does_not_exist.txt')
        assert m_isInsideDocker.call_count == 2
        assert ret == default_services

        # Create a dummy hosts file and mess with the upper/lower case of the
        # strings (host names are case insensitive).
        lines = '\n'.join([
            '127.0.0.1       localhost',
            '127.0.1.2       foo',
            '127.0.1.3       wEbApI',
            '127.0.1.4       Clerk alias1 alias2',
        ]) + '\n'

        # This time the hosts for 'webapi' and 'clerk' must reflect the
        # values in the hosts file.
        with mock.patch.object(builtins, 'open',
                               mock.mock_open(read_data=lines)) as m_open:
            assert m_open.call_count == 0
            ret = azutils.getAzraelServiceHosts(etchosts='/etc/hosts')
            assert m_open.call_count == 1

        assert ret == {
            'clerk': ('127.0.1.4', 5555),
            'database': ('127.0.0.1', 27017),
            'leonard': ('127.0.0.1', 5556),
            'rabbitmq': ('127.0.0.1', 5672),
            'webapi': ('127.0.1.3', 8080),
        }
예제 #2
0
    def __init__(self,
                 addr_clerk: str = None,
                 port_clerk: int = None,
                 port_webapi: int = None):
        super().__init__()

        # Use default values for the omitted connection parameters.
        azService = azutils.getAzraelServiceHosts('/etc/hosts')
        if addr_clerk is None:
            addr_clerk = azService['clerk'].ip
        if port_clerk is None:
            port_clerk = azService['clerk'].port
        if port_webapi is None:
            port_webapi = azService['webapi'].port

        # Store the connection parameters in instance variables.
        self.addr_clerk = addr_clerk
        self.port_clerk = port_clerk
        self.port_webapi = port_webapi

        # Create a Class-specific logger.
        name = '.'.join([__name__, self.__class__.__name__])
        self.logit = logging.getLogger(name)

        # Create ZeroMQ sockets and connect them to Clerk.
        self.ctx = zmq.Context()
        self.sock_cmd = self.ctx.socket(zmq.REQ)
        self.sock_cmd.linger = 0
        addr = 'tcp://{}:{}'.format(self.addr_clerk, self.port_clerk)
        self.logit.info('Connecing to <{}>'.format(addr))
        self.sock_cmd.connect(addr)
예제 #3
0
파일: client.py 프로젝트: olitheolix/azrael
    def __init__(self, addr_clerk: str=None,
                 port_clerk: int=None,
                 port_webapi: int=None):
        super().__init__()

        # Use default values for the omitted connection parameters.
        azService = azutils.getAzraelServiceHosts('/etc/hosts')
        if addr_clerk is None:
            addr_clerk = azService['clerk'].ip
        if port_clerk is None:
            port_clerk = azService['clerk'].port
        if port_webapi is None:
            port_webapi = azService['webapi'].port

        # Store the connection parameters in instance variables.
        self.addr_clerk = addr_clerk
        self.port_clerk = port_clerk
        self.port_webapi = port_webapi

        # Create a Class-specific logger.
        name = '.'.join([__name__, self.__class__.__name__])
        self.logit = logging.getLogger(name)

        # Create ZeroMQ sockets and connect them to Clerk.
        self.ctx = zmq.Context()
        self.sock_cmd = self.ctx.socket(zmq.REQ)
        self.sock_cmd.linger = 0
        addr = 'tcp://{}:{}'.format(self.addr_clerk, self.port_clerk)
        self.logit.info('Connecing to <{}>'.format(addr))
        self.sock_cmd.connect(addr)
예제 #4
0
    def test_getAzraelServiceHosts_outside_docker(self, m_getenv):
        """
        When outside a container all services must point to localhost.
        """
        # Mock os.getenv to ensure it tells getAzraelServiceHosts that we are
        # outside a container.
        m_getenv.return_value = None

        ret = azutils.getAzraelServiceHosts(etchosts=None)
        assert ret == {
            'clerk': ('127.0.0.1', 5555),
            'database': ('127.0.0.1', 27017),
            'rabbitmq': ('127.0.0.1', 5672),
            'webapi': ('127.0.0.1', 8080),
            'leonard': ('127.0.0.1', 5556),
        }
        m_getenv.assert_called_with('INSIDEDOCKER', None)
예제 #5
0
fileHandler = logging.FileHandler(log_file, mode='a')
fileHandler.setLevel(logging.DEBUG)
fileHandler.setFormatter(formatter)
fileHandler.setLevel(logging.DEBUG)

# Install the handler.
logger.addHandler(fileHandler)

del console, logFormat, formatter, fileHandler


# ---------------------------------------------------------------------------
# Global variables.
# ---------------------------------------------------------------------------
# Determine the host/ip addresses of all the services.
azService = azutils.getAzraelServiceHosts('/etc/hosts')


# WebServer URLs for the model- templates and instances. These *must not* include
# the trailing slash.
url_templates = '/templates'
url_instances = '/instances'
assert not url_templates.endswith('/') and not url_templates.endswith('/')


def getMongoClient(timeout: float=10):
    """
    Return a connected `MongoClient` instance.

    This is a convenience method that automatically connects to the correct
    host on the correct address.