예제 #1
0
class test_AsyncAWSQueryConnection(AWSCase):

    def setup(self):
        session = boto3.session.Session(
            aws_access_key_id='AAA',
            aws_secret_access_key='AAAA',
            region_name='us-west-2',
        )
        sqs_client = session.client('sqs')
        self.x = AsyncAWSQueryConnection(sqs_client,
                                         http_client=Mock(name='client'))

    def test_make_request(self):
        _mexe, self.x._mexe = self.x._mexe, Mock(name='_mexe')
        Conn = self.x.get_http_connection = Mock(name='get_http_connection')
        callback = PromiseMock(name='callback')
        self.x.make_request(
            'action', {'foo': 1}, 'https://foo.com/', 'GET', callback=callback,
        )
        self.x._mexe.assert_called()
        request = self.x._mexe.call_args[0][0]
        parsed = urlparse(request.url)
        params = parse_qs(parsed.query)
        assert params['Action'][0] == 'action'

        ret = _mexe(request, callback=callback)
        assert ret is callback
        Conn.return_value.request.assert_called()
        Conn.return_value.getresponse.assert_called_with(
            callback=callback,
        )

    def test_make_request__no_action(self):
        self.x._mexe = Mock(name='_mexe')
        self.x.get_http_connection = Mock(name='get_http_connection')
        callback = PromiseMock(name='callback')
        self.x.make_request(
            None, {'foo': 1}, 'http://foo.com/', 'GET', callback=callback,
        )
        self.x._mexe.assert_called()
        request = self.x._mexe.call_args[0][0]
        parsed = urlparse(request.url)
        params = parse_qs(parsed.query)
        assert 'Action' not in params

    def Response(self, status, body):
        r = Mock(name='response')
        r.status = status
        r.read.return_value = body
        return r

    @contextmanager
    def mock_make_request(self):
        self.x.make_request = Mock(name='make_request')
        callback = PromiseMock(name='callback')
        yield callback

    def assert_make_request_called(self):
        self.x.make_request.assert_called()
        return self.x.make_request.call_args[1]['callback']
예제 #2
0
 def __init__(self, sqs_connection, debug=0, region=None, **kwargs):
     if boto3 is None:
         raise ImportError('boto3 is not installed')
     AsyncAWSQueryConnection.__init__(self,
                                      sqs_connection,
                                      region_name=region,
                                      debug=debug,
                                      **kwargs)
예제 #3
0
 def __init__(self, sqs_connection, debug=0, region=None, **kwargs):
     if boto3 is None:
         raise ImportError('boto3 is not installed')
     AsyncAWSQueryConnection.__init__(
         self,
         sqs_connection,
         region_name=region, debug=debug,
         **kwargs
     )
예제 #4
0
 def setup(self):
     session = boto3.session.Session(
         aws_access_key_id='AAA',
         aws_secret_access_key='AAAA',
         region_name='us-west-2',
     )
     sqs_client = session.client('sqs')
     self.x = AsyncAWSQueryConnection(sqs_client,
                                      http_client=Mock(name='client'))
예제 #5
0
 def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
              is_secure=True, port=None, proxy=None, proxy_port=None,
              proxy_user=None, proxy_pass=None, debug=0,
              https_connection_factory=None, region=None, *args, **kwargs):
     if boto is None:
         raise ImportError('boto is not installed')
     self.region = region or RegionInfo(
         self, self.DefaultRegionName, self.DefaultRegionEndpoint,
         connection_cls=type(self),
     )
     AsyncAWSQueryConnection.__init__(
         self,
         aws_access_key_id=aws_access_key_id,
         aws_secret_access_key=aws_secret_access_key,
         is_secure=is_secure, port=port,
         proxy=proxy, proxy_port=proxy_port,
         proxy_user=proxy_user, proxy_pass=proxy_pass,
         host=self.region.endpoint, debug=debug,
         https_connection_factory=https_connection_factory, **kwargs
     )
예제 #6
0
 def __init__(self, aws_access_key_id=None, aws_secret_access_key=None,
              is_secure=True, port=None, proxy=None, proxy_port=None,
              proxy_user=None, proxy_pass=None, debug=0,
              https_connection_factory=None, region=None, *args, **kwargs):
     if boto is None:
         raise ImportError('boto is not installed')
     self.region = region or RegionInfo(
         self, self.DefaultRegionName, self.DefaultRegionEndpoint,
         connection_cls=type(self),
     )
     AsyncAWSQueryConnection.__init__(
         self,
         aws_access_key_id=aws_access_key_id,
         aws_secret_access_key=aws_secret_access_key,
         is_secure=is_secure, port=port,
         proxy=proxy, proxy_port=proxy_port,
         proxy_user=proxy_user, proxy_pass=proxy_pass,
         host=self.region.endpoint, debug=debug,
         https_connection_factory=https_connection_factory, **kwargs
     )
예제 #7
0
 def setup(self):
     self.x = AsyncAWSQueryConnection('aws.vandelay.com',
                                      http_client=Mock(name='client'))
예제 #8
0
class test_AsyncAWSQueryConnection(AWSCase):
    def setup(self):
        self.x = AsyncAWSQueryConnection('aws.vandelay.com',
                                         http_client=Mock(name='client'))

    @patch('boto.log', create=True)
    def test_make_request(self, _):
        _mexe, self.x._mexe = self.x._mexe, Mock(name='_mexe')
        Conn = self.x.get_http_connection = Mock(name='get_http_connection')
        callback = PromiseMock(name='callback')
        self.x.make_request(
            'action',
            {'foo': 1},
            '/',
            'GET',
            callback=callback,
        )
        self.x._mexe.assert_called()
        request = self.x._mexe.call_args[0][0]
        self.assertEqual(request.params['Action'], 'action')
        self.assertEqual(request.params['Version'], self.x.APIVersion)

        ret = _mexe(request, callback=callback)
        self.assertIs(ret, callback)
        Conn.return_value.request.assert_called()
        Conn.return_value.getresponse.assert_called_with(callback=callback, )

    @patch('boto.log', create=True)
    def test_make_request__no_action(self, _):
        self.x._mexe = Mock(name='_mexe')
        self.x.get_http_connection = Mock(name='get_http_connection')
        callback = PromiseMock(name='callback')
        self.x.make_request(
            None,
            {'foo': 1},
            '/',
            'GET',
            callback=callback,
        )
        self.x._mexe.assert_called()
        request = self.x._mexe.call_args[0][0]
        self.assertNotIn('Action', request.params)
        self.assertEqual(request.params['Version'], self.x.APIVersion)

    @contextmanager
    def mock_sax_parse(self, parser):
        with patch('kombu.async.aws.connection.sax_parse') as sax_parse:
            with patch('kombu.async.aws.connection.XmlHandler') as xh:

                def effect(body, h):
                    return parser(xh.call_args[0][0], body, h)

                sax_parse.side_effect = effect
                yield (sax_parse, xh)
                sax_parse.assert_called()

    def Response(self, status, body):
        r = Mock(name='response')
        r.status = status
        r.read.return_value = body
        return r

    @contextmanager
    def mock_make_request(self):
        self.x.make_request = Mock(name='make_request')
        callback = PromiseMock(name='callback')
        yield callback

    def assert_make_request_called(self):
        self.x.make_request.assert_called()
        return self.x.make_request.call_args[1]['callback']

    def test_get_list(self):
        with self.mock_make_request() as callback:
            self.x.get_list('action', {'p': 3.3}, ['m'], callback=callback)
            on_ready = self.assert_make_request_called()

            def parser(dest, body, h):
                dest.append('hi')
                dest.append('there')

            with self.mock_sax_parse(parser):
                on_ready(self.Response(200, 'hello'))
            callback.assert_called_with(['hi', 'there'])

    def test_get_list_error(self):
        with self.mock_make_request() as callback:
            self.x.get_list('action', {'p': 3.3}, ['m'], callback=callback)
            on_ready = self.assert_make_request_called()

            with self.assertRaises(self.x.ResponseError):
                on_ready(self.Response(404, 'Not found'))

    def test_get_object(self):
        with self.mock_make_request() as callback:

            class Result(object):
                parent = None
                value = None

                def __init__(self, parent):
                    self.parent = parent

            self.x.get_object('action', {'p': 3.3}, Result, callback=callback)
            on_ready = self.assert_make_request_called()

            def parser(dest, body, h):
                dest.value = 42

            with self.mock_sax_parse(parser):
                on_ready(self.Response(200, 'hello'))

            callback.assert_called()
            result = callback.call_args[0][0]
            self.assertEqual(result.value, 42)
            self.assertTrue(result.parent)

    def test_get_object_error(self):
        with self.mock_make_request() as callback:
            self.x.get_object('action', {'p': 3.3}, object, callback=callback)
            on_ready = self.assert_make_request_called()

            with self.assertRaises(self.x.ResponseError):
                on_ready(self.Response(404, 'Not found'))

    def test_get_status(self):
        with self.mock_make_request() as callback:
            self.x.get_status('action', {'p': 3.3}, callback=callback)
            on_ready = self.assert_make_request_called()
            set_status_to = [True]

            def parser(dest, body, b):
                dest.status = set_status_to[0]

            with self.mock_sax_parse(parser):
                on_ready(self.Response(200, 'hello'))
            callback.assert_called_with(True)

            set_status_to[0] = False
            with self.mock_sax_parse(parser):
                on_ready(self.Response(200, 'hello'))
            callback.assert_called_with(False)

    def test_get_status_error(self):
        with self.mock_make_request() as callback:
            self.x.get_status('action', {'p': 3.3}, callback=callback)
            on_ready = self.assert_make_request_called()

            with self.assertRaises(self.x.ResponseError):
                on_ready(self.Response(404, 'Not found'))

    def test_get_status_error_empty_body(self):
        with self.mock_make_request() as callback:
            self.x.get_status('action', {'p': 3.3}, callback=callback)
            on_ready = self.assert_make_request_called()

            with self.assertRaises(self.x.ResponseError):
                on_ready(self.Response(200, ''))
예제 #9
0
 def setup(self):
     self.x = AsyncAWSQueryConnection('aws.vandelay.com',
                                      http_client=Mock(name='client'))
예제 #10
0
class test_AsyncAWSQueryConnection(AWSCase):

    def setup(self):
        self.x = AsyncAWSQueryConnection('aws.vandelay.com',
                                         http_client=Mock(name='client'))

    @patch('boto.log', create=True)
    def test_make_request(self, _):
        _mexe, self.x._mexe = self.x._mexe, Mock(name='_mexe')
        Conn = self.x.get_http_connection = Mock(name='get_http_connection')
        callback = PromiseMock(name='callback')
        self.x.make_request(
            'action', {'foo': 1}, '/', 'GET', callback=callback,
        )
        self.assertTrue(self.x._mexe.called)
        request = self.x._mexe.call_args[0][0]
        self.assertEqual(request.params['Action'], 'action')
        self.assertEqual(request.params['Version'], self.x.APIVersion)

        ret = _mexe(request, callback=callback)
        self.assertIs(ret, callback)
        self.assertTrue(Conn.return_value.request.called)
        Conn.return_value.getresponse.assert_called_with(
            callback=callback,
        )

    @patch('boto.log', create=True)
    def test_make_request__no_action(self, _):
        self.x._mexe = Mock(name='_mexe')
        self.x.get_http_connection = Mock(name='get_http_connection')
        callback = PromiseMock(name='callback')
        self.x.make_request(
            None, {'foo': 1}, '/', 'GET', callback=callback,
        )
        self.assertTrue(self.x._mexe.called)
        request = self.x._mexe.call_args[0][0]
        self.assertNotIn('Action', request.params)
        self.assertEqual(request.params['Version'], self.x.APIVersion)

    @contextmanager
    def mock_sax_parse(self, parser):
        with patch('kombu.async.aws.connection.sax_parse') as sax_parse:
            with patch('kombu.async.aws.connection.XmlHandler') as xh:

                def effect(body, h):
                    return parser(xh.call_args[0][0], body, h)
                sax_parse.side_effect = effect
                yield (sax_parse, xh)
                self.assertTrue(sax_parse.called)

    def Response(self, status, body):
        r = Mock(name='response')
        r.status = status
        r.read.return_value = body
        return r

    @contextmanager
    def mock_make_request(self):
        self.x.make_request = Mock(name='make_request')
        callback = PromiseMock(name='callback')
        yield callback

    def assert_make_request_called(self):
        self.assertTrue(self.x.make_request.called)
        return self.x.make_request.call_args[1]['callback']

    def test_get_list(self):
        with self.mock_make_request() as callback:
            self.x.get_list('action', {'p': 3.3}, ['m'], callback=callback)
            on_ready = self.assert_make_request_called()

            def parser(dest, body, h):
                dest.append('hi')
                dest.append('there')

            with self.mock_sax_parse(parser):
                on_ready(self.Response(200, 'hello'))
            self.assertTrue(callback.called_with(['hi', 'there']))

    def test_get_list_error(self):
        with self.mock_make_request() as callback:
            self.x.get_list('action', {'p': 3.3}, ['m'], callback=callback)
            on_ready = self.assert_make_request_called()

            with self.assertRaises(self.x.ResponseError):
                on_ready(self.Response(404, 'Not found'))

    def test_get_object(self):
        with self.mock_make_request() as callback:

            class Result(object):
                parent = None
                value = None

                def __init__(self, parent):
                    self.parent = parent

            self.x.get_object('action', {'p': 3.3}, Result, callback=callback)
            on_ready = self.assert_make_request_called()

            def parser(dest, body, h):
                dest.value = 42

            with self.mock_sax_parse(parser):
                on_ready(self.Response(200, 'hello'))

            self.assertTrue(callback.called)
            result = callback.call_args[0][0]
            self.assertEqual(result.value, 42)
            self.assertTrue(result.parent)

    def test_get_object_error(self):
        with self.mock_make_request() as callback:
            self.x.get_object('action', {'p': 3.3}, object, callback=callback)
            on_ready = self.assert_make_request_called()

            with self.assertRaises(self.x.ResponseError):
                on_ready(self.Response(404, 'Not found'))

    def test_get_status(self):
        with self.mock_make_request() as callback:
            self.x.get_status('action', {'p': 3.3}, callback=callback)
            on_ready = self.assert_make_request_called()
            set_status_to = [True]

            def parser(dest, body, b):
                dest.status = set_status_to[0]

            with self.mock_sax_parse(parser):
                on_ready(self.Response(200, 'hello'))
            callback.assert_called_with(True)

            set_status_to[0] = False
            with self.mock_sax_parse(parser):
                on_ready(self.Response(200, 'hello'))
            callback.assert_called_with(False)

    def test_get_status_error(self):
        with self.mock_make_request() as callback:
            self.x.get_status('action', {'p': 3.3}, callback=callback)
            on_ready = self.assert_make_request_called()

            with self.assertRaises(self.x.ResponseError):
                on_ready(self.Response(404, 'Not found'))

    def test_get_status_error_empty_body(self):
        with self.mock_make_request() as callback:
            self.x.get_status('action', {'p': 3.3}, callback=callback)
            on_ready = self.assert_make_request_called()

            with self.assertRaises(self.x.ResponseError):
                on_ready(self.Response(200, ''))