Ejemplo n.º 1
0
def test_client_health():
    """Test getting plugin health via the client."""

    c = client.PluginUnixClient('foo/bar/test.sock')
    c.grpc.Health = mock_health

    resp = c.health()

    assert isinstance(resp, synse_grpc.api.PluginHealth)
Ejemplo n.º 2
0
def test_client_metainfo():
    """Test getting plugin metainfo via the client."""

    c = client.PluginUnixClient('foo/bar/test.sock')
    c.grpc.Metainfo = mock_metainfo

    resp = c.metainfo()

    assert isinstance(resp, synse_grpc.api.Metadata)
Ejemplo n.º 3
0
def test_client_write():
    """Test writing via the client."""

    c = client.PluginUnixClient('foo/bar/test.sock')
    c.grpc.Write = mock_write

    resp = c.write('rack-1', 'vec', '12345', [client.WriteData()])

    assert isinstance(resp, synse_grpc.api.Transactions)
Ejemplo n.º 4
0
def test_client_test():
    """Test that a plugin is reachable."""

    c = client.PluginUnixClient('foo/bar/test.sock')
    c.grpc.Test = mock_test

    resp = c.test()

    assert isinstance(resp, synse_grpc.api.Status)
Ejemplo n.º 5
0
def test_client_init():
    """Verify the client initializes as expected."""

    c = client.PluginUnixClient('foo/bar/test-cli.sock')

    assert c.address == 'foo/bar/test-cli.sock'
    assert c.type == 'unix'
    assert isinstance(c.channel, grpc.Channel)
    assert isinstance(c.grpc, synse_grpc.grpc.PluginStub)
Ejemplo n.º 6
0
def test_client_transaction():
    """Test checking a transaction via the client."""

    c = client.PluginUnixClient('foo/bar/test.sock')
    c.grpc.Transaction = mock_transaction

    resp = c.transaction('abcdef')

    assert isinstance(resp, list)
    assert isinstance(resp[0], synse_grpc.api.WriteResponse)
Ejemplo n.º 7
0
def test_client_read_cached():
    """Test reading plugin cache via the client."""

    c = client.PluginUnixClient('foo/bar/test.sock')
    c.grpc.ReadCached = mock_read_cached

    resp = [x for x in c.read_cached()]

    assert len(resp) == 1
    assert isinstance(resp[0], synse_grpc.api.DeviceReading)
Ejemplo n.º 8
0
def test_client_capabilities():
    """Test getting plugin capabilities via the client."""

    c = client.PluginUnixClient('foo/bar/test.sock')
    c.grpc.Capabilities = mock_capabilities

    resp = c.capabilities()

    assert isinstance(resp, list)
    assert len(resp) == 1
    assert isinstance(resp[0], synse_grpc.api.DeviceCapability)
Ejemplo n.º 9
0
def test_client_devices():
    """Test getting device info via the client."""

    c = client.PluginUnixClient('foo/bar/test.sock')
    c.grpc.Devices = mock_device_info

    resp = c.devices()

    assert isinstance(resp, list)
    assert len(resp) == 1
    assert isinstance(resp[0], synse_grpc.api.Device)
Ejemplo n.º 10
0
def test_client_read():
    """Test reading via the client."""

    c = client.PluginUnixClient('foo/bar/test.sock')
    c.grpc.Read = mock_read

    resp = c.read('rack-1', 'vec', '12345')

    assert isinstance(resp, list)
    assert len(resp) == 1
    assert isinstance(resp[0], synse_grpc.api.Reading)
Ejemplo n.º 11
0
def register_plugin(address, protocol):
    """Register a plugin. If a plugin with the given address already exists,
    it will not be re-registered, but its ID will still be returned.

    If a plugin fails to register, None is returned.

    Args:
        address (str): The address of the plugin to register.
        protocol (str): The protocol that the plugin uses. This should
            be one of 'unix', 'tcp'.

    Returns:
        str: The ID of the plugin that was registered.
        None: The given address failed to resolve, so no plugin
            was registered.

    Raises:
        ValueError: An invalid protocol is specified. The protocol must
            be one of: 'unix', 'tcp'

    """
    plugin = Plugin.manager.get_by_address(address)
    if plugin:
        logger.debug(_('{} is already registered').format(plugin))
        return plugin.id()

    # The client does not exist, so we must register it. This means we need to
    # connect with it to (a) make sure its reachable, and (b) get its metadata
    # in order to properly create a new Plugin model for it.
    if protocol == 'tcp':
        plugin_client = client.PluginTCPClient(address)
    elif protocol == 'unix':
        plugin_client = client.PluginUnixClient(address)
    else:
        raise ValueError(_('Invalid protocol specified for registration: {}').format(protocol))

    try:
        status = plugin_client.test()
        if not status.ok:
            logger.warning(_('gRPC Test response was not OK: {}').format(address))
            return None
    except Exception as e:
        logger.warning(_('Failed to reach plugin at address {}: {}').format(address, e))
        return None

    # If we made it here, we were successful in establishing communication
    # with the plugin. Now, we should get its metainfo and create a Plugin
    # instance with it.
    try:
        meta = plugin_client.metainfo()
    except Exception as e:
        logger.warning(_('Failed to get plugin metadata at address {}: {}').format(address, e))
        return None

    plugin = Plugin(
        metadata=meta,
        address=address,
        plugin_client=plugin_client
    )

    logger.debug(_('Registered new plugin: {}').format(plugin))
    return plugin.id()