Beispiel #1
0
    def test__publisher_user_resource(self, mock__get_cached_client):
        mock_mgr = self._manager(
            [Exception, Exception, [1, 2, 3], Exception, Exception, [4, 5]],
            _perform_for_admin_only=False,
            _tenant_resource=True)

        admin = mock.MagicMock()
        users = [{"tenant_id": 1, "id": 1}, {"tenant_id": 2, "id": 2}]
        publish = manager.SeekAndDestroy(mock_mgr, admin, users)._publisher

        queue = []
        publish(queue)

        mock_client = mock__get_cached_client.return_value
        mock_mgr.assert_has_calls([
            mock.call(admin=mock_client,
                      user=mock_client,
                      tenant_uuid=users[0]["tenant_id"]),
            mock.call().list(),
            mock.call().list(),
            mock.call().list(),
            mock.call(admin=mock_client,
                      user=mock_client,
                      tenant_uuid=users[1]["tenant_id"]),
            mock.call().list(),
            mock.call().list()
        ])
        mock__get_cached_client.assert_has_calls(
            [mock.call(admin),
             mock.call(users[0]),
             mock.call(users[1])])
        expected_queue = [(admin, users[0], x) for x in range(1, 4)]
        expected_queue += [(admin, users[1], x) for x in range(4, 6)]
        self.assertEqual(expected_queue, queue)
Beispiel #2
0
    def test__get_cached_client(self):
        destroyer = manager.SeekAndDestroy(None, None, None)
        cred = mock.Mock()
        user = {"credential": cred}

        clients = destroyer._get_cached_client(user)
        self.assertIs(cred.clients.return_value, clients)
        cred.clients.assert_called_once_with()

        self.assertIsNone(destroyer._get_cached_client(None))
Beispiel #3
0
    def test_exterminate(self, mock_broker_run):
        manager_cls = mock.MagicMock(_threads=5)
        cleaner = manager.SeekAndDestroy(manager_cls, None, None)
        cleaner._publisher = mock.Mock()
        cleaner._consumer = mock.Mock()
        cleaner.exterminate()

        mock_broker_run.assert_called_once_with(cleaner._publisher,
                                                cleaner._consumer,
                                                consumers_count=5)
Beispiel #4
0
    def test__publisher_admin(self, mock__get_cached_client):
        mock_mgr = self._manager([Exception, Exception, [1, 2, 3]],
                                 _perform_for_admin_only=False)
        admin = mock.MagicMock()
        publish = manager.SeekAndDestroy(mock_mgr, admin, None)._publisher

        queue = []
        publish(queue)
        mock__get_cached_client.assert_called_once_with(admin)
        mock_mgr.assert_called_once_with(
            admin=mock__get_cached_client.return_value)
        self.assertEqual(queue, [(admin, None, x) for x in range(1, 4)])
Beispiel #5
0
    def test__delete_single_resource_exception_in_is_deleted(self, mock_log):
        mock_resource = mock.MagicMock(_max_attempts=3,
                                       _timeout=10,
                                       _interval=0)
        mock_resource.delete.return_value = True
        mock_resource.is_deleted.side_effect = [Exception] * 4
        manager.SeekAndDestroy(None, None,
                               None)._delete_single_resource(mock_resource)

        mock_resource.delete.assert_called_once_with()
        self.assertEqual(4, mock_resource.is_deleted.call_count)

        self.assertEqual(1, mock_log.warning.call_count)
        self.assertEqual(4, mock_log.exception.call_count)
Beispiel #6
0
    def test__delete_single_resource_timeout(self, mock_log):

        mock_resource = mock.MagicMock(_max_attempts=1,
                                       _timeout=0.02,
                                       _interval=0.025)

        mock_resource.delete.return_value = True
        mock_resource.is_deleted.side_effect = [False, False, True]

        manager.SeekAndDestroy(None, None,
                               None)._delete_single_resource(mock_resource)

        mock_resource.delete.assert_called_once_with()
        mock_resource.is_deleted.assert_called_once_with()

        self.assertEqual(1, mock_log.warning.call_count)
Beispiel #7
0
    def test__delete_single_resource(self, mock_log):
        mock_resource = mock.MagicMock(_max_attempts=3,
                                       _timeout=10,
                                       _interval=0.01)
        mock_resource.delete.side_effect = [Exception, Exception, True]
        mock_resource.is_deleted.side_effect = [False, False, True]

        manager.SeekAndDestroy(None, None,
                               None)._delete_single_resource(mock_resource)

        mock_resource.delete.assert_has_calls([mock.call()] * 3)
        self.assertEqual(3, mock_resource.delete.call_count)
        mock_resource.is_deleted.assert_has_calls([mock.call()] * 3)
        self.assertEqual(3, mock_resource.is_deleted.call_count)

        # NOTE(boris-42): No logs and no exceptions means no bugs!
        self.assertEqual(0, mock_log.call_count)
Beispiel #8
0
    def test__gen_publisher_tenant_resource(self, mock__get_cached_client,
                                            mock_log):
        mock_mgr = self._manager([
            Exception, [1, 2, 3], Exception, Exception, Exception,
            ["this shouldn't be in results"]
        ],
                                 _perform_for_admin_only=False,
                                 _tenant_resource=True)
        users = [{
            "tenant_id": 1,
            "id": 1
        }, {
            "tenant_id": 1,
            "id": 2
        }, {
            "tenant_id": 2,
            "id": 3
        }]

        publish = manager.SeekAndDestroy(mock_mgr, None, users)._publisher

        queue = []
        publish(queue)

        mock_client = mock__get_cached_client.return_value
        mock_mgr.assert_has_calls([
            mock.call(admin=mock_client,
                      user=mock_client,
                      tenant_uuid=users[0]["tenant_id"]),
            mock.call().list(),
            mock.call().list(),
            mock.call(admin=mock_client,
                      user=mock_client,
                      tenant_uuid=users[2]["tenant_id"]),
            mock.call().list(),
            mock.call().list(),
            mock.call().list()
        ])
        mock__get_cached_client.assert_has_calls(
            [mock.call(None),
             mock.call(users[0]),
             mock.call(users[2])])
        self.assertEqual(queue, [(None, users[0], x) for x in range(1, 4)])
        self.assertTrue(mock_log.warning.mock_called)
        self.assertTrue(mock_log.exception.mock_called)
Beispiel #9
0
    def test__consumer(self, mock__delete_single_resource,
                       mock__get_cached_client, mock_name_matches_object):
        mock_mgr = mock.MagicMock(__name__="Test")
        resource_classes = [mock.Mock()]
        task_id = "task_id"
        mock_name_matches_object.return_value = True

        consumer = manager.SeekAndDestroy(mock_mgr,
                                          None,
                                          None,
                                          resource_classes=resource_classes,
                                          task_id=task_id)._consumer

        admin = mock.MagicMock()
        user1 = {"id": "a", "tenant_id": "uuid1"}
        cache = {}

        consumer(cache, (admin, user1, "res"))
        mock_mgr.assert_called_once_with(
            resource="res",
            admin=mock__get_cached_client.return_value,
            user=mock__get_cached_client.return_value,
            tenant_uuid=user1["tenant_id"])
        mock__get_cached_client.assert_has_calls(
            [mock.call(admin), mock.call(user1)])
        mock__delete_single_resource.assert_called_once_with(
            mock_mgr.return_value)

        mock_mgr.reset_mock()
        mock__get_cached_client.reset_mock()
        mock__delete_single_resource.reset_mock()
        mock_name_matches_object.reset_mock()

        consumer(cache, (admin, None, "res2"))
        mock_mgr.assert_called_once_with(
            resource="res2",
            admin=mock__get_cached_client.return_value,
            user=mock__get_cached_client.return_value,
            tenant_uuid=None)

        mock__get_cached_client.assert_has_calls(
            [mock.call(admin), mock.call(None)])
        mock__delete_single_resource.assert_called_once_with(
            mock_mgr.return_value)
Beispiel #10
0
    def test__consumer_with_noname_resource(self, mock__delete_single_resource,
                                            mock__get_cached_client,
                                            mock_name_matches_object):
        mock_mgr = mock.MagicMock(__name__="Test")
        mock_mgr.return_value.name.return_value = True
        task_id = "task_id"
        mock_name_matches_object.return_value = False

        consumer = manager.SeekAndDestroy(mock_mgr,
                                          None,
                                          None,
                                          task_id=task_id)._consumer

        consumer(None, (None, None, "res"))
        self.assertFalse(mock__delete_single_resource.called)

        mock_mgr.return_value.name.return_value = base.NoName("foo")
        consumer(None, (None, None, "res"))
        mock__delete_single_resource.assert_called_once_with(
            mock_mgr.return_value)