async def test_get_metainfo_cache_partial_failure(plugin_context, clear_caches): """Get the metainfo cache when some plugins fail to respond.""" # create & register new plugins p = plugin.Plugin('foo', 'localhost:9999', 'tcp') p.client.metainfo = mock_client_metainfo p = plugin.Plugin('bar', 'localhost:9998', 'tcp') p.client.metainfo = mock_client_metainfo_fail # override to induce failure meta = await cache.get_metainfo_cache() assert isinstance(meta, dict) assert len(meta) == 1 # two plugins registered, but only one successful
async def test_get_device_info_cache_exist(patch_register_plugins, plugin_context, clear_caches): """Get the existing device info cache.""" # create & register new plugin p = plugin.Plugin(metadata=api.Metadata(name='foo', tag='vaporio/foo'), address='localhost:9999', plugin_client=client.PluginTCPClient('localhost:9999')) p.client.devices = mock_client_devices # this is the first time we ask for cache # it's not there yet so we build one first_meta = await cache.get_device_info_cache() assert isinstance(first_meta, dict) assert 'rack-1-vec-12345' in first_meta assert first_meta['rack-1-vec-12345'] == mock_get_device_info_cache( )['rack-1-vec-12345'] # this is the second time we ask for it # it should be there this time and return right away second_meta = await cache.get_device_info_cache() assert isinstance(second_meta, dict) assert 'rack-1-vec-12345' in second_meta assert second_meta['rack-1-vec-12345'] == mock_get_device_info_cache( )['rack-1-vec-12345']
def mock_plugin(): """Convenience fixture to create a test plugin. We create a TCP based plugin for ease of testing. """ p = plugin.Plugin(metadata=api.Metadata(name='test-plug'), address='localhost:9999', plugin_client=PluginTCPClient(address='localhost:9999')) yield p
def test_plugin_path_not_exist(): """Create a plugin when the socket doesn't exist.""" p = plugin.Plugin( metadata=api.Metadata(name='test'), address='some/nonexistent/path', plugin_client=PluginUnixClient(address='some/nonexistent/path')) assert p.protocol == 'unix' assert p.name == 'test'
def test_plugin_tco_ok(): """Create a TCP plugin successfully""" p = plugin.Plugin('test', 'localhost:9999', 'tcp') assert p.name == 'test' assert p.addr == 'localhost:9999' assert p.mode == 'tcp' assert p.client is not None assert isinstance(p.client, SynseInternalClient)
async def test_get_device_info_cache_partial_failure(patch_register_plugins, plugin_context, clear_caches): """Get the device info cache when some plugins fail to respond.""" # create & register new plugins p = plugin.Plugin(metadata=api.Metadata(name='foo', tag='vaporio/foo'), address='localhost:9999', plugin_client=client.PluginTCPClient('localhost:9999')) p.client.devices = mock_client_devices p = plugin.Plugin(metadata=api.Metadata(name='bar', tag='vaporio/bar'), address='localhost:9998', plugin_client=client.PluginTCPClient('localhost:9998')) p.client.devices = mock_client_device_info_fail # override to induce failure meta = await cache.get_device_info_cache() assert isinstance(meta, dict) assert len(meta) == 1 # two plugins registered, but only one successful
async def test_get_metainfo_cache_empty(plugin_context, clear_caches): """Get the empty metainfo cache.""" # create & register new plugin p = plugin.Plugin('foo', 'localhost:9999', 'tcp') p.client.metainfo = mock_client_metainfo_empty meta = await cache.get_metainfo_cache() assert isinstance(meta, dict) assert len(meta) == 0
def mock_plugin(): """Convenience fixture to create a test plugin. We create a TCP based plugin for ease of testing. """ p = plugin.Plugin( name='test-plug', address='localhost:9999', mode='tcp' ) yield p
def _mock(): return 'test', plugin.Plugin( metadata=api.Metadata( name='test', tag='vaporio/test' ), address='localhost:5001', plugin_client=PluginTCPClient( address='localhost:5001' ), )
def make_plugin(setup): """Fixture to create and register a plugin for testing.""" # make a dummy plugin for the tests to use if 'foo' not in plugin.Plugin.manager.plugins: plugin.Plugin('foo', 'tmp/foo', 'unix') yield if 'foo' in plugin.Plugin.manager.plugins: del plugin.Plugin.manager.plugins['foo']
async def test_get_metainfo_cache_total_failure(plugin_context, clear_caches): """Get the metainfo cache when all plugins fail to respond.""" # create & register new plugin p = plugin.Plugin('foo', 'localhost:9999', 'tcp') p.client.metainfo = mock_client_metainfo_fail # override to induce failure try: await cache.get_metainfo_cache() except errors.SynseError as e: assert e.error_id == errors.INTERNAL_API_FAILURE
def make_plugin(): """Fixture to create and register a plugin for testing.""" # make a dummy plugin for the tests to use if 'foo' not in plugin.Plugin.manager.plugins: plugin.Plugin('foo', 'localhost:9999', 'tcp') yield if 'foo' in plugin.Plugin.manager.plugins: del plugin.Plugin.manager.plugins['foo']
async def test_get_device_info_cache_empty(plugin_context, clear_caches): """Get the empty device info cache.""" # create & register new plugin p = plugin.Plugin(metadata=api.Metadata(name='foo', tag='vaporio/foo'), address='localhost:9999', plugin_client=client.PluginTCPClient('localhost:9999')) p.client.devices = mock_client_device_info_empty meta = await cache.get_device_info_cache() assert isinstance(meta, dict) assert len(meta) == 0
def test_plugin_tcp_ok(): """Create a TCP plugin successfully""" p = plugin.Plugin(metadata=api.Metadata(name='test'), address='localhost:9999', plugin_client=PluginTCPClient(address='localhost:9999')) assert p.name == 'test' assert p.address == 'localhost:9999' assert p.protocol == 'tcp' assert p.client is not None assert isinstance(p.client, PluginClient)
async def test_get_metainfo_cache_ok(plugin_context, clear_caches): """Get the metainfo cache.""" # create & register new plugin p = plugin.Plugin('foo', 'localhost:9999', 'tcp') p.client.metainfo = mock_client_metainfo meta = await cache.get_metainfo_cache() assert isinstance(meta, dict) assert 'rack-1-vec-12345' in meta assert meta['rack-1-vec-12345'] == mock_get_metainfo_cache( )['rack-1-vec-12345']
def test_plugin_unix_ok(tmpsocket): """Create a UNIX plugin successfully""" _, path = tmpsocket.add('test') p = plugin.Plugin(metadata=api.Metadata(name='test'), address=path, plugin_client=PluginUnixClient(address=path)) assert p.name == 'test' assert p.address == path assert p.protocol == 'unix' assert p.client is not None assert isinstance(p.client, PluginClient)
def test_plugin_unix_ok(): """Create a UNIX plugin successfully""" # create a file in the tmp dir for the test path = os.path.join(data_dir, 'test') open(path, 'w').close() p = plugin.Plugin('test', path, 'unix') assert p.name == 'test' assert p.addr == path assert p.mode == 'unix' assert p.client is not None assert isinstance(p.client, SynseInternalClient)
async def test_get_device_info_cache_total_failure(plugin_context, clear_caches): """Get the device info cache when all plugins fail to respond.""" # create & register new plugin p = plugin.Plugin(metadata=api.Metadata(name='foo', tag='vaporio/foo'), address='localhost:9999', plugin_client=client.PluginTCPClient('localhost:9999')) p.client.devices = mock_client_device_info_fail # override to induce failure try: await cache.get_device_info_cache() except errors.SynseError as e: assert e.error_id == errors.INTERNAL_API_FAILURE
async def test_get_device_info_cache_ok(patch_register_plugins, plugin_context, clear_caches): """Get the device info cache.""" # create & register new plugin p = plugin.Plugin(metadata=api.Metadata(name='foo', tag='vaporio/foo'), address='localhost:9999', plugin_client=client.PluginTCPClient('localhost:9999')) p.client.devices = mock_client_devices meta = await cache.get_device_info_cache() assert isinstance(meta, dict) assert 'rack-1-vec-12345' in meta assert meta['rack-1-vec-12345'] == mock_get_device_info_cache( )['rack-1-vec-12345']
def make_plugin(): """Fixture to create and register a plugin for testing.""" plugin_id = 'vaporio/foo+tcp@localhost:9999' # make a dummy plugin for the tests to use if plugin_id not in plugin.Plugin.manager.plugins: plugin.Plugin(metadata=api.Metadata(name='foo', tag='vaporio/foo'), address='localhost:9999', plugin_client=PluginTCPClient('localhost:9999')) yield if plugin_id in plugin.Plugin.manager.plugins: del plugin.Plugin.manager.plugins[plugin_id]
def make_plugin(setup): """Fixture to create and register a plugin for testing.""" plugin_id = 'vaporio/foo+unix@tmp/foo' # make a dummy plugin for the tests to use if plugin_id not in plugin.Plugin.manager.plugins: plugin.Plugin(metadata=api.Metadata(name='foo', tag='vaporio/foo'), address='tmp/foo', plugin_client=PluginUnixClient('tmp/foo')) yield if plugin_id in plugin.Plugin.manager.plugins: del plugin.Plugin.manager.plugins[plugin_id]
def add_plugin(): """Add a test plugin to the plugin manager.""" p = plugin.Plugin( metadata=api.Metadata( name='test', tag='vaporio/test', ), address='localhost:5001', plugin_client=PluginTCPClient( address='localhost:5001', ), ) yield plugin.Plugin.manager.remove(p.id())
async def test_get_resource_info_cache_empty(plugin_context, clear_caches): """Get the empty info cache.""" # create & register new plugin with empty metainfo cache p = plugin.Plugin('foo', 'localhost:9999', 'tcp') p.client.metainfo = mock_client_metainfo_empty meta_cache = await cache.get_metainfo_cache() assert isinstance(meta_cache, dict) assert len(meta_cache) == 0 # because metainfo cache is empty and info cache is built upon metainfo's # scan cache should also be empty info_cache = await cache.get_resource_info_cache() assert isinstance(info_cache, dict) assert len(info_cache) == 0
async def test_get_resource_info_cache_empty(plugin_context, clear_caches): """Get the empty info cache.""" # create & register new plugin with empty device info cache p = plugin.Plugin(metadata=api.Metadata(name='foo', tag='vaporio/foo'), address='localhost:9999', plugin_client=client.PluginTCPClient('localhost:9999')) p.client.devices = mock_client_device_info_empty meta_cache = await cache.get_device_info_cache() assert isinstance(meta_cache, dict) assert len(meta_cache) == 0 # because device info cache is empty and info cache is built upon device info's # scan cache should also be empty info_cache = await cache.get_resource_info_cache() assert isinstance(info_cache, dict) assert len(info_cache) == 0
async def test_get_metainfo_cache_exist(plugin_context, clear_caches): """Get the existing metainfo cache.""" # create & register new plugin p = plugin.Plugin('foo', 'localhost:9999', 'tcp') p.client.metainfo = mock_client_metainfo # this is the first time we ask for cache # it's not there yet so we build one first_meta = await cache.get_metainfo_cache() assert isinstance(first_meta, dict) assert 'rack-1-vec-12345' in first_meta assert first_meta['rack-1-vec-12345'] == mock_get_metainfo_cache( )['rack-1-vec-12345'] # this is the second time we ask for it # it should be there this time and return right away second_meta = await cache.get_metainfo_cache() assert isinstance(second_meta, dict) assert 'rack-1-vec-12345' in second_meta assert second_meta['rack-1-vec-12345'] == mock_get_metainfo_cache( )['rack-1-vec-12345']
async def test_read_cached_command_2(monkeypatch, patch_get_device_info, clear_manager): """Read the plugin cache for multiple plugins.""" # Add plugins to the manager (this is done by the constructor) plugin.Plugin( metadata=api.Metadata( name='foo', tag='vaporio/foo', ), address='localhost:5001', plugin_client=PluginTCPClient( address='localhost:5001', ), ) plugin.Plugin( metadata=api.Metadata( name='bar', tag='vaporio/bar', ), address='localhost:5002', plugin_client=PluginTCPClient( address='localhost:5002', ), ) # monkeypatch the read_cached method so it yields some data def _mock(*args, **kwargs): readings = [ api.DeviceReading( rack='rack', board='board', device='device', reading=api.Reading( timestamp='2018-10-18T16:43:18+00:00', type='temperature', int64_value=10, ) ), api.DeviceReading( rack='rack', board='board', device='device', reading=api.Reading( timestamp='2018-10-18T16:43:18+00:00', type='humidity', int64_value=30, ) ) ] for r in readings: yield r monkeypatch.setattr(PluginClient, 'read_cached', _mock) assert len(plugin.Plugin.manager.plugins) == 2 results = [i async for i in read_cached()] # two plugins with two patched readings each assert len(results) == 4 assert results[0].data['type'] == 'temperature' assert results[1].data['type'] == 'humidity' assert results[2].data['type'] == 'temperature' assert results[3].data['type'] == 'humidity'
def test_plugin_invalid_mode(): """Create a plugin when the mode is invalid.""" with pytest.raises(errors.PluginStateError): plugin.Plugin('test', 'some/addr', 'foo')
def test_plugin_path_not_exist(): """Create a plugin when the socket doesn't exist.""" with pytest.raises(errors.PluginStateError): plugin.Plugin('test', 'some/nonexistent/path', 'unix')