class TestPublicConfigEventHandler(unittest.TestCase): def setUp(self): self.handler = PublicConfigEventHandler() self.mock_filewatcher = mock.Mock() self.mock_config = mock.Mock(get_cluster=mock.Mock()) with mock.patch( "paasta_tools.deployd.watchers.load_system_paasta_config", autospec=True, return_value=self.mock_config, ), mock.patch( "paasta_tools.deployd.watchers.get_marathon_clients_from_config", autospec=True, ): self.handler.my_init(self.mock_filewatcher) def test_log(self): self.handler.log.info("WHAAAAAT") def test_filter_event(self): mock_event = mock.Mock() name = mock.PropertyMock(return_value="deployd.json") type(mock_event).name = name assert mock_event == self.handler.filter_event(mock_event) mock_event = mock.Mock(maskname="MAJORAS") name = mock.PropertyMock(return_value="another.file") type(mock_event).name = name assert self.handler.filter_event(mock_event) is None mock_event = mock.Mock(maskname="IN_CREATE|IN_ISDIR", pathname="/foo/bar") name = mock.PropertyMock(return_value="another.file") type(mock_event).name = name assert mock_event == self.handler.filter_event(mock_event) def test_watch_new_folder(self): mock_event = mock.Mock(maskname="MAJORAS") self.handler.watch_new_folder(mock_event) assert not self.mock_filewatcher.wm.add_watch.called mock_event = mock.Mock(maskname="IN_CREATE|IN_ISDIR", pathname="/foo/") self.handler.watch_new_folder(mock_event) assert self.mock_filewatcher.wm.add_watch.called def test_process_default(self): with mock.patch( "paasta_tools.deployd.watchers.PublicConfigEventHandler.filter_event", autospec=True, ) as mock_filter_event, mock.patch( "paasta_tools.deployd.watchers.PublicConfigEventHandler.watch_new_folder", autospec=True, ), mock.patch( "paasta_tools.deployd.watchers.get_services_for_cluster", autospec=True ) as mock_get_services_for_cluster, mock.patch( "paasta_tools.deployd.watchers.load_system_paasta_config", autospec=True ) as mock_load_system_config, mock.patch( "paasta_tools.deployd.watchers.get_service_instances_needing_update", autospec=True, ) as mock_get_service_instances_needing_update, mock.patch( "time.time", return_value=1.0, autospec=True): mock_event = mock.Mock() mock_filter_event.return_value = mock_event mock_load_system_config.return_value = self.mock_config self.handler.process_default(mock_event) assert mock_load_system_config.called assert not mock_get_services_for_cluster.called assert not mock_get_service_instances_needing_update.called assert not self.mock_filewatcher.instances_to_bounce.put.called mock_load_system_config.return_value = mock.Mock( get_cluster=mock.Mock()) mock_get_service_instances_needing_update.return_value = [] self.handler.process_default(mock_event) assert mock_load_system_config.called assert mock_get_services_for_cluster.called assert mock_get_service_instances_needing_update.called assert not self.mock_filewatcher.instances_to_bounce.put.called mock_load_system_config.return_value = mock.Mock( get_deployd_big_bounce_deadline=mock.Mock(return_value=100.0)) fake_si = ( "someservice", "someinstance", mock.Mock(), "someservice.someinstance.stuff.otherstuff", ) mock_get_service_instances_needing_update.return_value = [fake_si] self.handler.process_default(mock_event) assert mock_load_system_config.called assert mock_get_services_for_cluster.called assert mock_get_service_instances_needing_update.called # call objects are (name, posargs, kwargs), so this grabs the first posarg of the most recent call. assert self.mock_filewatcher.instances_to_bounce.put.mock_calls[ -1][1][0] == ServiceInstance( service="someservice", instance="someinstance", watcher="PublicConfigEventHandler", bounce_by=101.0, wait_until=1.0, enqueue_time=1.0, bounce_start_time=1.0, )
class TestPublicConfigEventHandler(unittest.TestCase): def setUp(self): self.handler = PublicConfigEventHandler() self.mock_filewatcher = mock.Mock() self.mock_config = mock.Mock(get_cluster=mock.Mock()) with mock.patch( 'paasta_tools.deployd.watchers.load_system_paasta_config', autospec=True, return_value=self.mock_config ), mock.patch( 'paasta_tools.deployd.watchers.get_marathon_client_from_config', autospec=True): self.handler.my_init(self.mock_filewatcher) def test_log(self): self.handler.log.info('WHAAAAAT') def test_filter_event(self): mock_event = mock.Mock() name = mock.PropertyMock(return_value='deployd.json') type(mock_event).name = name assert mock_event == self.handler.filter_event(mock_event) mock_event = mock.Mock(maskname='MAJORAS') name = mock.PropertyMock(return_value='another.file') type(mock_event).name = name assert self.handler.filter_event(mock_event) is None mock_event = mock.Mock(maskname='IN_CREATE|IN_ISDIR') name = mock.PropertyMock(return_value='another.file') type(mock_event).name = name assert mock_event == self.handler.filter_event(mock_event) def test_watch_new_folder(self): mock_event = mock.Mock(maskname='MAJORAS') self.handler.watch_new_folder(mock_event) assert not self.mock_filewatcher.wm.add_watch.called mock_event = mock.Mock(maskname='IN_CREATE|IN_ISDIR') self.handler.watch_new_folder(mock_event) assert self.mock_filewatcher.wm.add_watch.called def test_process_default(self): with mock.patch( 'paasta_tools.deployd.watchers.PublicConfigEventHandler.filter_event', autospec=True ) as mock_filter_event, mock.patch( 'paasta_tools.deployd.watchers.PublicConfigEventHandler.watch_new_folder', autospec=True ), mock.patch( 'paasta_tools.deployd.watchers.get_services_for_cluster', autospec=True ) as mock_get_services_for_cluster, mock.patch( 'paasta_tools.deployd.watchers.load_system_paasta_config', autospec=True, ) as mock_load_system_config, mock.patch( 'paasta_tools.deployd.watchers.get_service_instances_with_changed_id', autospec=True ) as mock_get_service_instances_with_changed_id, mock.patch( 'paasta_tools.deployd.watchers.rate_limit_instances', autospec=True) as mock_rate_limit_instances: mock_event = mock.Mock() mock_filter_event.return_value = mock_event mock_load_system_config.return_value = self.mock_config self.handler.process_default(mock_event) assert mock_load_system_config.called assert not mock_get_services_for_cluster.called assert not mock_get_service_instances_with_changed_id.called assert not mock_rate_limit_instances.called assert not self.mock_filewatcher.inbox_q.put.called mock_load_system_config.return_value = mock.Mock( get_cluster=mock.Mock()) mock_get_service_instances_with_changed_id.return_value = [] self.handler.process_default(mock_event) assert mock_load_system_config.called assert mock_get_services_for_cluster.called assert mock_get_service_instances_with_changed_id.called assert not mock_rate_limit_instances.called assert not self.mock_filewatcher.inbox_q.put.called mock_load_system_config.return_value = mock.Mock( get_deployd_big_bounce_rate=mock.Mock()) mock_si = mock.Mock() mock_get_service_instances_with_changed_id.return_value = [mock_si] mock_rate_limit_instances.return_value = [mock_si] self.handler.process_default(mock_event) assert mock_load_system_config.called assert mock_get_services_for_cluster.called assert mock_get_service_instances_with_changed_id.called assert mock_rate_limit_instances.called self.mock_filewatcher.inbox_q.put.assert_called_with(mock_si)