示例#1
0
    def test__gen_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)._gen_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(queue, expected_queue)
示例#2
0
    def test_exterminate(self, mock_broker_run, mock__gen_publisher,
                         mock__gen_consumer):

        manager_cls = mock.MagicMock(_threads=5)
        manager.SeekAndDestroy(manager_cls, None, None).exterminate()

        mock__gen_publisher.assert_called_once_with()
        mock__gen_consumer.assert_called_once_with()
        mock_broker_run.assert_called_once_with(
            mock__gen_publisher.return_value,
            mock__gen_consumer.return_value,
            consumers_count=5)
示例#3
0
    def test__gen_publisher_admin_only(self, mock__get_cached_client):
        mock_mgr = self._manager([Exception, Exception, [1, 2, 3]],
                                 _perform_for_admin_only=True)
        admin = mock.MagicMock()
        publish = manager.SeekAndDestroy(mock_mgr, admin,
                                         ["u1", "u2"])._gen_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)])
示例#4
0
    def test__delete_single_resource_excpetion_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(5, mock_log.warning.call_count)
        self.assertEqual(4, mock_log.exception.call_count)
示例#5
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)
示例#6
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(mock_resource.delete.call_count, 3)
        mock_resource.is_deleted.assert_has_calls([mock.call()] * 3)
        self.assertEqual(mock_resource.is_deleted.call_count, 3)

        # NOTE(boris-42): No logs and no exceptions means no bugs!
        self.assertEqual(0, mock_log.call_count)
示例#7
0
    def test__gen_publisher_tenant_resource(self, mock__get_cached_client):
        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)._gen_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)])
示例#8
0
    def test__gen_consumer(self, mock__delete_single_resource,
                           mock__get_cached_client):
        mock_mgr = mock.MagicMock(__name__="Test")

        consumer = manager.SeekAndDestroy(mock_mgr, None, None)._gen_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, cache=cache),
             mock.call(user1, cache=cache)])
        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()

        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, cache=cache),
             mock.call(None, cache=cache)])
        mock__delete_single_resource.assert_called_once_with(
            mock_mgr.return_value)