Пример #1
0
class TestYelpSoaEventHandler(unittest.TestCase):
    def setUp(self):
        self.handler = YelpSoaEventHandler()
        self.mock_filewatcher = mock.Mock()
        with 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='marathon-cluster.yaml')
        type(mock_event).name = name
        assert mock_event == self.handler.filter_event(mock_event)
        name = mock.PropertyMock(return_value='deployments.json')
        type(mock_event).name = name
        assert mock_event == self.handler.filter_event(mock_event)
        name = mock.PropertyMock(return_value='another.file')
        type(mock_event).name = name
        assert self.handler.filter_event(mock_event) is None

    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):
        mock_event = mock.Mock(path='/folder/universe')
        with mock.patch(
            'paasta_tools.deployd.watchers.list_all_instances_for_service', autospec=True
        ) as mock_list_instances, mock.patch(
            'paasta_tools.deployd.watchers.get_service_instances_with_changed_id', autospec=True
        ) as mock_get_service_instances_with_changed_id, mock.patch(
            'time.time', autospec=True, return_value=1
        ):
            mock_list_instances.return_value = ['c137', 'c138']
            mock_get_service_instances_with_changed_id.return_value = [('universe', 'c137')]
            self.handler.process_default(mock_event)
            mock_list_instances.assert_called_with(service='universe',
                                                   clusters=[self.handler.filewatcher.cluster],
                                                   instance_type='marathon')
            mock_get_service_instances_with_changed_id.assert_called_with(self.handler.marathon_client,
                                                                          [('universe', 'c137'),
                                                                           ('universe', 'c138')],
                                                                          self.handler.filewatcher.cluster)
            expected_si = ServiceInstance(service='universe',
                                          instance='c137',
                                          bounce_by=1,
                                          watcher='YelpSoaEventHandler',
                                          bounce_timers=None)
            self.mock_filewatcher.inbox_q.put.assert_called_with(expected_si)
            assert self.mock_filewatcher.inbox_q.put.call_count == 1
Пример #2
0
class TestYelpSoaEventHandler(unittest.TestCase):
    def setUp(self):
        self.handler = YelpSoaEventHandler()
        self.mock_filewatcher = mock.Mock(cluster="clustername")
        with 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_get_service_name_from_event(self):
        mock_event = mock.Mock()
        name = mock.PropertyMock(return_value="marathon-cluster.yaml")
        type(mock_event).name = name
        mock_event.path = "/blah/test-service"
        assert "test-service" == self.handler.get_service_name_from_event(
            mock_event)

        mock_event = mock.Mock()
        name = mock.PropertyMock(return_value="marathon-cluster.yaml")
        type(mock_event).name = name
        mock_event.path = f"/blah/test-service/{AUTO_SOACONFIG_SUBDIR}"
        assert "test-service" == self.handler.get_service_name_from_event(
            mock_event)

        name = mock.PropertyMock(return_value="deployments.json")
        type(mock_event).name = name
        mock_event.path = "/blah/test-service"
        assert "test-service" == self.handler.get_service_name_from_event(
            mock_event)

        name = mock.PropertyMock(return_value="test-secret.json")
        type(mock_event).name = name
        mock_event.path = "/blah/test-service/secrets"
        assert "test-service" == self.handler.get_service_name_from_event(
            mock_event)

        name = mock.PropertyMock(return_value="something.json")
        type(mock_event).name = name
        mock_event.path = "/blah/test-service"
        assert self.handler.get_service_name_from_event(mock_event) is None

        name = mock.PropertyMock(return_value="another.file")
        type(mock_event).name = name
        mock_event.path = "/nail/blah/test-service"
        assert self.handler.get_service_name_from_event(mock_event) is None

    def test_watch_new_folder(self):
        with mock.patch(
                "os.listdir", autospec=True
        ) as mock_os_list, mock.patch(
                "paasta_tools.deployd.watchers.YelpSoaEventHandler.bounce_service",
                autospec=True,
        ) as mock_bounce_service:
            mock_os_list.return_value = ["some.file", "some_other.file"]
            mock_event = mock.Mock(maskname="MAJORAS", pathname="/some/path")
            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")
            name = mock.PropertyMock(return_value="universe")
            type(mock_event).name = name
            self.handler.watch_new_folder(mock_event)
            assert self.mock_filewatcher.wm.add_watch.called
            assert not mock_bounce_service.called

            mock_os_list.return_value = ["some.file", "marathon-cluster.yaml"]
            self.handler.watch_new_folder(mock_event)
            assert self.mock_filewatcher.wm.add_watch.called
            mock_bounce_service.assert_called_with(self.handler, "universe")

            mock_os_list.side_effect = OSError
            mock_bounce_service.reset_mock()
            self.handler.watch_new_folder(mock_event)
            assert self.mock_filewatcher.wm.add_watch.called
            assert not mock_bounce_service.called

    def test_process_default(self):
        mock_event = mock.Mock(path="/folder/universe")
        type(mock_event).name = "marathon-blah.yaml"
        with mock.patch(
                "paasta_tools.deployd.watchers.YelpSoaEventHandler.bounce_service",
                autospec=True,
        ) as mock_bounce_service, mock.patch(
                "paasta_tools.deployd.watchers.YelpSoaEventHandler.watch_new_folder",
                autospec=True,
        ) as mock_watch_folder, mock.patch(
                "paasta_tools.deployd.watchers.YelpSoaEventHandler.get_service_name_from_event",
                autospec=True,
        ) as mock_get_service_name_from_event:
            mock_get_service_name_from_event.return_value = None
            self.handler.process_default(mock_event)
            mock_watch_folder.assert_called_with(self.handler, mock_event)
            mock_get_service_name_from_event.assert_called_with(
                self.handler, mock_event)
            assert not mock_bounce_service.called

            mock_get_service_name_from_event.return_value = "universe"
            self.handler.process_default(mock_event)
            mock_watch_folder.assert_called_with(self.handler, mock_event)
            mock_get_service_name_from_event.assert_called_with(
                self.handler, mock_event)
            mock_bounce_service.assert_called_with(self.handler, "universe")

    def test_bounce_service(self):
        with mock.patch(
                "paasta_tools.deployd.watchers.list_all_instances_for_service",
                autospec=True,
        ) as mock_list_instances, mock.patch(
                "paasta_tools.deployd.watchers.get_service_instances_needing_update",
                autospec=True,
        ) as mock_get_service_instances_needing_update, mock.patch(
                "time.time", autospec=True, return_value=1):
            mock_list_instances.return_value = ["c137", "c138"]
            mock_get_service_instances_needing_update.return_value = [(
                "universe",
                "c137",
                mock.Mock(get_bounce_start_deadline=mock.Mock(return_value=0)),
                "/universe.c137.c1.g1",
            )]
            self.handler.bounce_service("universe")
            mock_list_instances.assert_called_with(
                service="universe",
                clusters=[self.handler.filewatcher.cluster],
                instance_type="marathon",
                cache=False,
            )
            mock_get_service_instances_needing_update.assert_called_with(
                self.handler.marathon_clients,
                [("universe", "c137"), ("universe", "c138")],
                self.handler.filewatcher.cluster,
            )
            expected_si = ServiceInstance(
                service="universe",
                instance="c137",
                bounce_by=1,
                wait_until=1,
                watcher="YelpSoaEventHandler",
                failures=0,
                processed_count=0,
                enqueue_time=1,
                bounce_start_time=1,
            )
            self.mock_filewatcher.instances_to_bounce.put.assert_called_with(
                expected_si)
            assert self.mock_filewatcher.instances_to_bounce.put.call_count == 1
Пример #3
0
class TestYelpSoaEventHandler(unittest.TestCase):
    def setUp(self):
        self.handler = YelpSoaEventHandler()
        self.mock_filewatcher = mock.Mock()
        with 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_get_service_name_from_event(self):
        mock_event = mock.Mock()
        name = mock.PropertyMock(return_value='marathon-cluster.yaml')
        type(mock_event).name = name
        mock_event.path = '/blah/test-service'
        assert "test-service" == self.handler.get_service_name_from_event(
            mock_event)

        name = mock.PropertyMock(return_value='deployments.json')
        type(mock_event).name = name
        mock_event.path = '/blah/test-service'
        assert "test-service" == self.handler.get_service_name_from_event(
            mock_event)

        name = mock.PropertyMock(return_value='test-secret.json')
        type(mock_event).name = name
        mock_event.path = '/blah/test-service/secrets'
        assert "test-service" == self.handler.get_service_name_from_event(
            mock_event)

        name = mock.PropertyMock(return_value='something.json')
        type(mock_event).name = name
        mock_event.path = '/blah/test-service'
        assert self.handler.get_service_name_from_event(mock_event) is None

        name = mock.PropertyMock(return_value='another.file')
        type(mock_event).name = name
        mock_event.path = '/nail/blah/test-service'
        assert self.handler.get_service_name_from_event(mock_event) is None

    def test_watch_new_folder(self):
        with mock.patch(
                'os.listdir',
                autospec=True,
        ) as mock_os_list, mock.patch(
                'paasta_tools.deployd.watchers.YelpSoaEventHandler.bounce_service',
                autospec=True,
        ) as mock_bounce_service:
            mock_os_list.return_value = ["some.file", "some_other.file"]
            mock_event = mock.Mock(maskname='MAJORAS', pathname='/some/path')
            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')
            name = mock.PropertyMock(return_value='universe')
            type(mock_event).name = name
            self.handler.watch_new_folder(mock_event)
            assert self.mock_filewatcher.wm.add_watch.called
            assert not mock_bounce_service.called

            mock_os_list.return_value = ["some.file", "marathon-cluster.yaml"]
            self.handler.watch_new_folder(mock_event)
            assert self.mock_filewatcher.wm.add_watch.called
            mock_bounce_service.assert_called_with(self.handler, 'universe')

            mock_os_list.side_effect = OSError
            mock_bounce_service.reset_mock()
            self.handler.watch_new_folder(mock_event)
            assert self.mock_filewatcher.wm.add_watch.called
            assert not mock_bounce_service.called

    def test_process_default(self):
        mock_event = mock.Mock(path='/folder/universe')
        type(mock_event).name = 'marathon-blah.yaml'
        with mock.patch(
                'paasta_tools.deployd.watchers.YelpSoaEventHandler.bounce_service',
                autospec=True,
        ) as mock_bounce_service, mock.patch(
                'paasta_tools.deployd.watchers.YelpSoaEventHandler.watch_new_folder',
                autospec=True,
        ) as mock_watch_folder, mock.patch(
                'paasta_tools.deployd.watchers.YelpSoaEventHandler.get_service_name_from_event',
                autospec=True,
        ) as mock_get_service_name_from_event:
            mock_get_service_name_from_event.return_value = None
            self.handler.process_default(mock_event)
            mock_watch_folder.assert_called_with(self.handler, mock_event)
            mock_get_service_name_from_event.assert_called_with(
                self.handler, mock_event)
            assert not mock_bounce_service.called

            mock_get_service_name_from_event.return_value = 'universe'
            self.handler.process_default(mock_event)
            mock_watch_folder.assert_called_with(self.handler, mock_event)
            mock_get_service_name_from_event.assert_called_with(
                self.handler, mock_event)
            mock_bounce_service.assert_called_with(self.handler, 'universe')

    def test_bounce_service(self):
        with mock.patch(
                'paasta_tools.deployd.common.get_priority',
                autospec=True,
                return_value=0,
        ), mock.patch(
                'paasta_tools.deployd.watchers.list_all_instances_for_service',
                autospec=True,
        ) as mock_list_instances, mock.patch(
                'paasta_tools.deployd.watchers.get_service_instances_needing_update',
                autospec=True,
        ) as mock_get_service_instances_needing_update, mock.patch(
                'time.time',
                autospec=True,
                return_value=1,
        ):
            mock_list_instances.return_value = ['c137', 'c138']
            mock_get_service_instances_needing_update.return_value = [
                ('universe', 'c137')
            ]
            self.handler.bounce_service('universe')
            mock_list_instances.assert_called_with(
                service='universe',
                clusters=[self.handler.filewatcher.cluster],
                instance_type='marathon',
                cache=False,
            )
            mock_get_service_instances_needing_update.assert_called_with(
                self.handler.marathon_clients,
                [
                    ('universe', 'c137'),
                    ('universe', 'c138'),
                ],
                self.handler.filewatcher.cluster,
            )
            expected_si = BaseServiceInstance(
                service='universe',
                instance='c137',
                bounce_by=1,
                watcher='YelpSoaEventHandler',
                bounce_timers=None,
                priority=0,
                failures=0,
            )
            self.mock_filewatcher.instances_to_bounce_later.put.assert_called_with(
                expected_si)
            assert self.mock_filewatcher.instances_to_bounce_later.put.call_count == 1