Пример #1
0
    def __init__(self, exchange_name, connection_url, config_file=None):
        """
        Creates a new WatcherService.  If config_file is omitted,
        it will try the default location (/etc/commissaire/watcher.conf).

        :param exchange_name: Name of the topic exchange
        :type exchange_name: str
        :param connection_url: Kombu connection URL
        :type connection_url: str
        :param config_file: Optional configuration file path
        :type config_file: str or None
        """
        queue_kwargs = [{
            'name': 'watcher',
            'exclusive': False,
            'routing_key': 'jobs.watcher',
        }]
        # Store the last address seen for backoff
        self.last_address = None

        super().__init__(exchange_name,
                         connection_url,
                         queue_kwargs,
                         config_file=config_file)

        self.storage = StorageClient(self)
Пример #2
0
    def test_get_many(self):
        """
        Verify StorageClient.get_many works as expected
        """
        storage = StorageClient(mock.MagicMock())
        storage.bus_mixin.logger = mock.MagicMock()

        self.assertEqual(storage.get_many([]), [])
        storage.bus_mixin.request.assert_not_called()

        self.assertRaises(TypeError, storage.get_many,
                          [MINI_HOST, MINI_CLUSTER])
        storage.bus_mixin.request.assert_not_called()

        storage.bus_mixin.request.return_value = {
            'jsonrpc': '2.0',
            'id': ID,
            'result': [FULL_HOST_DICT, FULL_HOST_DICT]
        }
        input_list = [MINI_HOST, MINI_HOST]
        output_list = storage.get_many(input_list)
        storage.bus_mixin.request.assert_called_once_with(
            'storage.get',
            params={
                'model_type_name': MINI_HOST.__class__.__name__,
                'model_json_data': [x.to_dict() for x in input_list]
            })
        self.assertEqual([x.to_dict_safe() for x in output_list],
                         [FULL_HOST_DICT, FULL_HOST_DICT])
Пример #3
0
 def test_save_many_invalid(self):
     """
     Verify StorageClient.save_many rejects an invalid model
     """
     storage = StorageClient(mock.MagicMock())
     storage.bus_mixin.logger = mock.MagicMock()
     storage.bus_mixin.request.side_effect = ValidationError('test')
     bad_host = Host.new(**FULL_HOST_DICT)
     bad_host.address = None
     self.assertRaises(ValidationError, storage.save_many, [bad_host])
     storage.bus_mixin.request.assert_not_called()
Пример #4
0
 def test_list_rpc_error(self):
     """
     Verify StorageClient.list re-raises RemoteProcedureCallError.
     """
     storage = StorageClient(mock.MagicMock())
     storage.bus_mixin.logger = mock.MagicMock()
     storage.bus_mixin.request.side_effect = RemoteProcedureCallError(
         'test')
     self.assertRaises(RemoteProcedureCallError, storage.list, Hosts)
     storage.bus_mixin.request.assert_called_once_with(
         'storage.list', params={'model_type_name': 'Hosts'})
Пример #5
0
 def test_delete(self):
     """
     Verify StorageClient.delete with a valid model.
     """
     storage = StorageClient(mock.MagicMock())
     storage.bus_mixin.logger = mock.MagicMock()
     storage.delete(MINI_HOST)
     storage.bus_mixin.request.assert_called_once_with(
         'storage.delete',
         params={
             'model_type_name': MINI_HOST.__class__.__name__,
             'model_json_data': MINI_HOST.to_dict()
         })
Пример #6
0
 def test_get_rpc_error(self):
     """
     Verify StorageClient.get re-raises RemoteProcedureCallError.
     """
     storage = StorageClient(mock.MagicMock())
     storage.bus_mixin.logger = mock.MagicMock()
     storage.bus_mixin.request.side_effect = RemoteProcedureCallError(
         'test')
     self.assertRaises(RemoteProcedureCallError, storage.get, MINI_HOST)
     storage.bus_mixin.request.assert_called_once_with(
         'storage.get',
         params={
             'model_type_name': MINI_HOST.__class__.__name__,
             'model_json_data': MINI_HOST.to_dict()
         })
Пример #7
0
 def test_list_invalid(self):
     """
     Verify StorageClient.list rejects an invalid list element.
     """
     storage = StorageClient(mock.MagicMock())
     storage.bus_mixin.logger = mock.MagicMock()
     storage.bus_mixin.request.return_value = {
         'jsonrpc': '2.0',
         'id': ID,
         'result': [{
             'address': None
         }]
     }
     self.assertRaises(ValidationError, storage.list, Hosts)
     storage.bus_mixin.request.assert_called_once_with(
         'storage.list', params={'model_type_name': 'Hosts'})
Пример #8
0
 def test_save_many_rpc_error(self):
     """
     Verify StorageClient.save_many re-raises RemoteProcedureCallError
     """
     storage = StorageClient(mock.MagicMock())
     storage.bus_mixin.logger = mock.MagicMock()
     storage.bus_mixin.request.side_effect = RemoteProcedureCallError(
         'test')
     self.assertRaises(RemoteProcedureCallError, storage.save_many,
                       [FULL_HOST])
     storage.bus_mixin.request.assert_called_once_with(
         'storage.save',
         params={
             'model_type_name': FULL_HOST.__class__.__name__,
             'model_json_data': [FULL_HOST.to_dict()]
         })
Пример #9
0
    def __init__(self, exchange_name, connection_url, config_file=None):
        """
        Creates a new ClusterExecService.  If config_file is omitted,
        it will try the default location (/etc/commissaire/clusterexec.conf).

        :param exchange_name: Name of the topic exchange
        :type exchange_name: str
        :param connection_url: Kombu connection URL
        :type connection_url: str
        :param config_file: Optional configuration file path
        :type config_file: str or None
        """
        queue_kwargs = [{'routing_key': 'jobs.clusterexec.*'}]
        super().__init__(exchange_name, connection_url, queue_kwargs)
        self.storage = StorageClient(self)

        # Apply any logging configuration for this service.
        read_config_file(config_file, '/etc/commissaire/clusterexec.conf')
Пример #10
0
 def test_list(self):
     """
     Verify StorageClient.list returns valid models.
     """
     storage = StorageClient(mock.MagicMock())
     storage.bus_mixin.logger = mock.MagicMock()
     storage.bus_mixin.request.return_value = {
         'jsonrpc': '2.0',
         'id': ID,
         'result': [FULL_HOST_DICT]
     }
     model = storage.list(Hosts)
     storage.bus_mixin.request.assert_called_once_with(
         'storage.list', params={'model_type_name': 'Hosts'})
     self.assertIsInstance(model, Hosts)
     self.assertEqual(len(model.hosts), 1)
     self.assertIsInstance(model.hosts[0], Host)
     self.assertEqual(model.hosts[0].to_dict_safe(), FULL_HOST_DICT)
Пример #11
0
    def __init__(self, exchange_name, connection_url, config_file=None):
        """
        Creates a new InvestigatorService.  If config_file is omitted,
        it will try the default location (/etc/commissaire/investigator.conf).

        :param exchange_name: Name of the topic exchange
        :type exchange_name: str
        :param connection_url: Kombu connection URL
        :type connection_url: str
        :param config_file: Optional configuration file path
        :type config_file: str or None
        """
        queue_kwargs = [{'routing_key': 'jobs.investigate'}]

        super().__init__(exchange_name,
                         connection_url,
                         queue_kwargs,
                         config_file=config_file)

        self.storage = StorageClient(self)
Пример #12
0
    def __init__(self, exchange_name, connection_url, qkwargs):
        """
        Initializes a new Bus instance.

        :param exchange_name: Name of the topic exchange.
        :type exchange_name: str
        :param connection_url: Kombu connection url.
        :type connection_url: str
        :param qkwargs: One or more dicts keyword arguments for queue creation
        :type qkwargs: list
        """
        self.logger = logging.getLogger('Bus')
        self.logger.debug('Initializing bus connection')
        self.connection = None
        self._channel = None
        self._exchange = None
        self.exchange_name = exchange_name
        self.connection_url = connection_url
        self.qkwargs = qkwargs
        self.storage = StorageClient(self)
Пример #13
0
 def test_get(self):
     """
     Verify StorageClient.get with a valid model.
     """
     storage = StorageClient(mock.MagicMock())
     storage.bus_mixin.logger = mock.MagicMock()
     storage.bus_mixin.request.return_value = {
         'jsonrpc': '2.0',
         'id': ID,
         'result': FULL_HOST_DICT
     }
     model = storage.get(MINI_HOST)
     storage.bus_mixin.request.assert_called_once_with(
         'storage.get',
         params={
             'model_type_name': MINI_HOST.__class__.__name__,
             'model_json_data': MINI_HOST.to_dict()
         })
     self.assertIsInstance(model, Host)
     self.assertEqual(model.to_dict_safe(), FULL_HOST_DICT)
Пример #14
0
    def test_delete_many(self):
        """
        Verify StorageClient.delete_many works as expected
        """
        storage = StorageClient(mock.MagicMock())
        storage.bus_mixin.logger = mock.MagicMock()

        storage.delete_many([])
        storage.bus_mixin.request.assert_not_called()

        self.assertRaises(TypeError, storage.delete_many,
                          [MINI_HOST, MINI_CLUSTER])
        storage.bus_mixin.request.assert_not_called()

        input_list = [MINI_HOST, MINI_HOST]
        storage.delete_many(input_list)
        storage.bus_mixin.request.assert_called_once_with(
            'storage.delete',
            params={
                'model_type_name': MINI_HOST.__class__.__name__,
                'model_json_data': [x.to_dict() for x in input_list]
            })
Пример #15
0
    def test_register_callback(self):
        """
        Verify StorageClient.register_callback routing keys.
        """
        storage = StorageClient(mock.MagicMock())

        # Verify entry per unique routing key.
        storage.register_callback(mock.MagicMock(), Host, NOTIFY_EVENT_CREATED)
        storage.register_callback(mock.MagicMock(), model_type=Host)
        storage.register_callback(mock.MagicMock(), event=NOTIFY_EVENT_CREATED)
        storage.register_callback(mock.MagicMock())
        self.assertIn('notify.storage.Host.created', storage.notify_callbacks)
        self.assertIn('notify.storage.Host.*', storage.notify_callbacks)
        self.assertIn('notify.storage.*.created', storage.notify_callbacks)
        self.assertIn('notify.storage.*.*', storage.notify_callbacks)
        self.assertEquals(len(storage.notify_callbacks), 4)

        # Verify callbacks with identical routing keys are queued.
        storage.register_callback(mock.MagicMock())
        self.assertEquals(len(storage.notify_callbacks), 4)
        callbacks = storage.notify_callbacks['notify.storage.*.*']
        self.assertEquals(len(callbacks), 2)
Пример #16
0
    def __init__(self, exchange_name, connection_url, config_file=None):
        """
        Creates a new ContainerManagerService.  If config_file is omitted,
        it will try the default location (/etc/commissaire/containermgr.conf).

        :param exchange_name: Name of the topic exchange
        :type exchange_name: str
        :param connection_url: Kombu connection URL
        :type connection_url: str
        :param config_file: Optional configuration file path
        :type config_file: str or None
        """
        queue_kwargs = [{
            'name': 'containermgr',
            'routing_key': 'container.*',
            'exclusive': False,
        }]
        super().__init__(exchange_name, connection_url, queue_kwargs)
        self.storage = StorageClient(self)
        self.managers = {}

        # Apply any logging configuration for this service.
        read_config_file(config_file, '/etc/commissaire/containermgr.conf')