def test_register_service_bails_when_no_ipc(self): address = "ipc:///tmp/nmos-nodefacade.dummy.for.test" srv_type = "dummy_type" UUT = Facade(srv_type, address=address) self.mocks['nmosnode.facade.Proxy'].side_effect = Exception href = "http://dummy.example.com" proxy_path = "http://dummyproxy.example.com" UUT.register_service(href, proxy_path) self.assertIsNone(UUT.ipc) self.assertFalse(UUT.srv_registered)
def assert_method_calls_remote_method_or_bails(self, method_name, remote_method_name, params, registered=True, ipc=True, raises=False, presetup=None, extra_check=None): getattr(self.mocks['nmosnode.facade.Proxy'].return_value, remote_method_name).reset_mock() getattr(self.mocks['nmosnode.facade.Proxy'].return_value, remote_method_name).side_effect = None self.mocks['nmosnode.facade.Proxy'].side_effect = None address = "ipc:///tmp/nmos-nodefacade.dummy.for.test" srv_type = "dummy_type" UUT = Facade(srv_type, address=address) if presetup is not None: presetup(UUT) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_register.return_value = FAC_SUCCESS if registered: href = "http://dummy.example.com" proxy_path = "http://dummyproxy.example.com" UUT.register_service(href, proxy_path) if not ipc: UUT.ipc = None self.mocks['nmosnode.facade.Proxy'].side_effect = Exception if raises: getattr(self.mocks['nmosnode.facade.Proxy'].return_value, remote_method_name).side_effect = Exception getattr(UUT, method_name)(*params) if registered and ipc: callparams = [srv_type, mock.ANY] + list(params) getattr(self.mocks['nmosnode.facade.Proxy'].return_value, remote_method_name).assert_called_once_with(*callparams) elif not registered or not ipc: getattr(self.mocks['nmosnode.facade.Proxy'].return_value, remote_method_name).assert_not_called() self.assertTrue(UUT.reregister) if raises: self.assertTrue(UUT.reregister) if extra_check is not None: extra_check(UUT)
def test_heartbeat_service_bails_when_no_ipc(self): address = "ipc:///tmp/nmos-nodefacade.dummy.for.test" srv_type = "dummy_type" UUT = Facade(srv_type, address=address) self.mocks['nmosnode.facade.Proxy'].side_effect = Exception with mock.patch.object(UUT, 'reregister_all') as reregister_all: UUT.heartbeat_service() self.assertFalse(UUT.srv_registered) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_heartbeat.assert_not_called( ) reregister_all.assert_not_called()
def test_heartbeat_service_reregisters_on_failure(self): address = "ipc:///tmp/nmos-nodefacade.dummy.for.test" srv_type = "dummy_type" UUT = Facade(srv_type, address=address) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_heartbeat.return_value = FAC_OTHERERROR with mock.patch.object(UUT, 'reregister_all') as reregister_all: UUT.heartbeat_service() self.assertFalse(UUT.srv_registered) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_heartbeat.assert_called_once_with( srv_type, mock.ANY) reregister_all.assert_called_once_with()
def test_unregister_service_bails_when_unregister_raises(self): address = "ipc:///tmp/nmos-nodefacade.dummy.for.test" srv_type = "dummy_type" UUT = Facade(srv_type, address=address) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_unregister.side_effect = Exception UUT.srv_registered = True UUT.unregister_service() self.assertIsNone(UUT.ipc) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_unregister.assert_called_once_with( srv_type, mock.ANY) self.assertTrue(UUT.srv_registered)
def test_register_service(self): address = "ipc:///tmp/nmos-nodefacade.dummy.for.test" srv_type = "dummy_type" UUT = Facade(srv_type, address=address) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_register.return_value = FAC_SUCCESS href = "http://dummy.example.com" proxy_path = "http://dummyproxy.example.com" UUT.register_service(href, proxy_path) self.assertIsNotNone(UUT.ipc) UUT.ipc.srv_register.assert_called_once_with( srv_type, "urn:x-ipstudio:service:" + srv_type, mock.ANY, href, proxy_path, False) self.assertTrue(UUT.srv_registered)
def test_heartbeat_service_reregisters_on_success_when_told_to(self): """Is this correct? This is current behaviour, but doesn't match the comments""" address = "ipc:///tmp/nmos-nodefacade.dummy.for.test" srv_type = "dummy_type" UUT = Facade(srv_type, address=address) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_heartbeat.return_value = FAC_SUCCESS with mock.patch.object(UUT, 'reregister_all') as reregister_all: UUT.reregister = True UUT.heartbeat_service() self.assertTrue(UUT.srv_registered) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_heartbeat.assert_called_once_with( srv_type, mock.ANY) reregister_all.assert_called_once_with()
def test_register_service_bails_when_register_raises(self): address = "ipc:///tmp/nmos-nodefacade.dummy.for.test" srv_type = "dummy_type" UUT = Facade(srv_type, address=address) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_register.side_effect = Exception href = "http://dummy.example.com" proxy_path = "http://dummyproxy.example.com" UUT.register_service(href, proxy_path) self.assertIsNone(UUT.ipc) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_register.assert_called_once_with( srv_type, "urn:x-ipstudio:service:" + srv_type, mock.ANY, href, proxy_path, False) self.assertFalse(UUT.srv_registered)
def test_reregister_all_bails_if_failed_to_register(self): address = "ipc:///tmp/nmos-nodefacade.dummy.for.test" srv_type = "dummy_type" UUT = Facade(srv_type, address=address) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_register.return_value = FAC_SUCCESS href = "http://dummy.example.com" proxy_path = "http://dummyproxy.example.com" UUT.register_service(href, proxy_path) UUT.ipc.res_register.reset_mock() UUT.ipc.control_register.reset_mock() self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_register.side_effect = Exception UUT.reregister_all() self.mocks[ 'nmosnode.facade.Proxy'].return_value.res_register.assert_not_called( ) self.mocks[ 'nmosnode.facade.Proxy'].return_value.control_register.assert_not_called( )
class ConnectionManagementService: def __init__(self, logger=None): self.running = False from nmoscommon.logger import Logger from nmosnode.facade import Facade self.logger = Logger("conmanage") self.logger.writeWarning("Could not find ipppython facade") self.facade = Facade("{}/{}".format(CONN_APINAME, CONN_APIVERSIONS[-1]), address="ipc:///tmp/ips-nodefacade", logger=self.logger) self.logger.writeDebug("Running Connection Management Service") self.httpServer = HttpServer(ConnectionManagementAPI, WS_PORT, '0.0.0.0', api_args=[self.logger]) def start(self): '''Call this to run the API without blocking''' if self.running: gevent.signal_handler(signal.SIGINT, self.sig_handler) gevent.signal_handler(signal.SIGTERM, self.sig_handler) self.running = True self.httpServer.start() while not self.httpServer.started.is_set(): self.logger.writeDebug('Waiting for httpserver to start...') self.httpServer.started.wait() if self.httpServer.failed is not None: raise self.httpServer.failed self.logger.writeDebug("Running on port: {}" .format(self.httpServer.port)) self.facade.register_service("http://127.0.0.1:{}".format(self.httpServer.port), "{}{}/".format(CONN_ROOT[1:], CONN_APIVERSIONS[-1])) try: from nmosconnectiondriver.httpIpstudioDriver import httpIpstudioDriver self.logger.writeInfo("Using ipstudio driver") # Start the IPStudio driver self.driver = httpIpstudioDriver( self.httpServer.api, self.logger, self.facade ) except ImportError: # Start the mock driver self.driver = NmosDriver( self.httpServer.api, self.logger, self.facade ) def run(self): '''Call this to run the API in keep-alive (blocking) mode''' self.running = True self.start() itercount = 0 while self.running: gevent.sleep(1) itercount += 1 if itercount == 5: self.facade.heartbeat_service() itercount = 0 self._cleanup() def _cleanup(self): self.httpServer.stop() self.facade.unregister_service() def sig_handler(self): self.stop() def stop(self): '''Gracefully shut down the API''' self._cleanup() self.running = False
def test_reregister_all_bails_when_control_register_raises(self): address = "ipc:///tmp/nmos-nodefacade.dummy.for.test" srv_type = "dummy_type" UUT = Facade(srv_type, address=address) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_register.return_value = FAC_SUCCESS href = "http://dummy.example.com" proxy_path = "http://dummyproxy.example.com" UUT.register_service(href, proxy_path) resources = [("type0", "key0", "val0"), ("type0", "key1", "val1"), ("type2", "key2", "val2")] controls = [("id0", { "href": "href0" }), ("id1", { "href": "href1" }), ("id2", { "href": "href2" })] for res in resources: UUT.addResource(*res) UUT.addResource("receiver", "rkey", { "pipel_id": "DUMMY0", "pipeline_id": "DUMMY1", "dummy": "DUMMY2" }) expected_res_register_calls = [ mock.call(srv_type, mock.ANY, *res) for res in resources ] + [ mock.call(srv_type, mock.ANY, "receiver", "rkey", {"dummy": "DUMMY2"}) ] for con in controls: UUT.addControl(*con) UUT.ipc.res_register.reset_mock() UUT.ipc.control_register.reset_mock() UUT.ipc.control_register.side_effect = Exception with mock.patch('gevent.sleep'): UUT.reregister_all() self.assertIsNone(UUT.ipc) self.assertCountEqual( self.mocks['nmosnode.facade.Proxy'].return_value.res_register. mock_calls, expected_res_register_calls) self.assertEqual( len(self.mocks['nmosnode.facade.Proxy'].return_value. control_register.mock_calls), 1)
def test_reregister_all(self): address = "ipc:///tmp/nmos-nodefacade.dummy.for.test" srv_type = "dummy_type" UUT = Facade(srv_type, address=address) self.mocks[ 'nmosnode.facade.Proxy'].return_value.srv_register.return_value = FAC_SUCCESS href = "http://dummy.example.com" proxy_path = "http://dummyproxy.example.com" UUT.register_service(href, proxy_path) resources = [("type0", "key0", "val0"), ("type0", "key1", "val1"), ("type2", "key2", "val2")] controls = [("id0", { "href": "href0" }), ("id1", { "href": "href1" }), ("id2", { "href": "href2" })] for res in resources: UUT.addResource(*res) UUT.addResource("receiver", "rkey", { "pipel_id": "DUMMY0", "pipeline_id": "DUMMY1", "dummy": "DUMMY2" }) for con in controls: UUT.addControl(*con) expected_res_register_calls = [ mock.call(srv_type, mock.ANY, *res) for res in resources ] + [ mock.call(srv_type, mock.ANY, "receiver", "rkey", {"dummy": "DUMMY2"}) ] expected_control_register_calls = [ mock.call(srv_type, mock.ANY, *con) for con in controls ] UUT.ipc.res_register.reset_mock() UUT.ipc.control_register.reset_mock() UUT.reregister_all() self.assertFalse(UUT.reregister) self.assertTrue(UUT.srv_registered) self.assertCountEqual(UUT.ipc.res_register.mock_calls, expected_res_register_calls) self.assertCountEqual(UUT.ipc.control_register.mock_calls, expected_control_register_calls)