示例#1
0
    def test_put_raises_if_missing_fields(self):
        path = self.url_prefix + '/flavors/' + str(uuid.uuid1())
        self.simulate_put(path, body=jsonutils.dumps({}))
        self.assertEqual(self.srmock.status, falcon.HTTP_400)

        self.simulate_put(path, body=jsonutils.dumps({'capabilities': {}}))
        self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#2
0
    def test_put_raises_if_missing_fields(self):
        path = self.url_prefix + '/pools/' + str(uuid.uuid1())
        self.simulate_put(path, body=jsonutils.dumps({'weight': 100}))
        self.assertEqual(self.srmock.status, falcon.HTTP_400)

        self.simulate_put(path,
                          body=jsonutils.dumps({'uri': 'sqlite://:memory:'}))
        self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#3
0
    def test_put_raises_if_missing_fields(self):
        path = self.url_prefix + '/flavors/' + str(uuid.uuid1())
        self.simulate_put(path, body=jsonutils.dumps({}))
        self.assertEqual(self.srmock.status, falcon.HTTP_400)

        self.simulate_put(path,
                          body=jsonutils.dumps({'capabilities': {}}))
        self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#4
0
文件: test_pools.py 项目: rose/zaqar
    def test_put_raises_if_missing_fields(self):
        path = self.url_prefix + '/pools/' + str(uuid.uuid1())
        self.simulate_put(path, body=jsonutils.dumps({'weight': 100}))
        self.assertEqual(self.srmock.status, falcon.HTTP_400)

        self.simulate_put(path,
                          body=jsonutils.dumps(
                              {'uri': 'sqlite://:memory:'}))
        self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#5
0
    def setUp(self):
        super(FlavorsBaseTest, self).setUp()
        self.queue = 'test-queue'
        self.queue_path = self.url_prefix + '/queues/' + self.queue

        self.pool = 'mypool'
        self.pool_path = self.url_prefix + '/pools/' + self.pool
        self.pool_doc = {'weight': 100, 'uri': 'sqlite://:memory:'}
        self.simulate_put(self.pool_path, body=jsonutils.dumps(self.pool_doc))

        self.flavor = 'test-flavor'
        self.doc = {'capabilities': {}, 'pool': 'mypool'}
        self.flavor_path = self.url_prefix + '/flavors/' + self.flavor
        self.simulate_put(self.flavor_path, body=jsonutils.dumps(self.doc))
        self.assertEqual(self.srmock.status, falcon.HTTP_201)
示例#6
0
def flavors(test, count, pool):
    """A context manager for constructing flavors for use in testing.

    Deletes the flavors after exiting the context.

    :param test: Must expose simulate_* methods
    :param count: Number of pools to create
    :type count: int
    :returns: (paths, pool, capabilities)
    :rtype: ([six.text_type], [six.text_type], [dict])

    """

    base = test.url_prefix + '/flavors/'
    args = sorted([(base + str(i), {
        str(i): i
    }, str(i)) for i in range(count)],
                  key=lambda tup: tup[2])
    for path, capabilities, _ in args:
        doc = {'pool': pool, 'capabilities': capabilities}
        test.simulate_put(path, body=jsonutils.dumps(doc))

    try:
        yield args
    finally:
        for path, _, _ in args:
            test.simulate_delete(path)
示例#7
0
def flavor(test, name, pool, capabilities={}):
    """A context manager for constructing a flavor for use in testing.

    Deletes the flavor after exiting the context.

    :param test: Must expose simulate_* methods
    :param name: Name for this flavor
    :type name: six.text_type
    :type pool: six.text_type
    :type capabilities: dict
    :returns: (name, uri, capabilities)
    :rtype: see above

    """

    doc = {'pool': pool, 'capabilities': capabilities}
    path = test.url_prefix + '/flavors/' + name

    test.simulate_put(path, body=jsonutils.dumps(doc))

    try:
        yield name, pool, capabilities

    finally:
        test.simulate_delete(path)
示例#8
0
    def _prepare_messages(self, count):
        doc = jsonutils.dumps([{'body': 239, 'ttl': 300}] * count)
        self.simulate_post(self.messages_path,
                           body=doc,
                           headers={'Client-ID': str(uuid.uuid4())})

        self.assertEqual(self.srmock.status, falcon.HTTP_201)
示例#9
0
文件: log.py 项目: AsherBond/marconi
    def format(self, record):
        message = {'message': record.getMessage(),
                   'asctime': self.formatTime(record, self.datefmt),
                   'name': record.name,
                   'msg': record.msg,
                   'args': record.args,
                   'levelname': record.levelname,
                   'levelno': record.levelno,
                   'pathname': record.pathname,
                   'filename': record.filename,
                   'module': record.module,
                   'lineno': record.lineno,
                   'funcname': record.funcName,
                   'created': record.created,
                   'msecs': record.msecs,
                   'relative_created': record.relativeCreated,
                   'thread': record.thread,
                   'thread_name': record.threadName,
                   'process_name': record.processName,
                   'process': record.process,
                   'traceback': None}

        if hasattr(record, 'extra'):
            message['extra'] = record.extra

        if record.exc_info:
            message['traceback'] = self.formatException(record.exc_info)

        return jsonutils.dumps(message)
示例#10
0
    def setUp(self):
        super(MessagesBaseTest, self).setUp()

        self.default_message_ttl = self.boot.transport._defaults.message_ttl

        if self.conf.pooling:
            for i in range(4):
                uri = self.conf['drivers:storage:mongodb'].uri
                doc = {'weight': 100, 'uri': uri}
                self.simulate_put(self.url_prefix + '/pools/' + str(i),
                                  body=jsonutils.dumps(doc))
                self.assertEqual(self.srmock.status, falcon.HTTP_201)

        self.project_id = '7e55e1a7e'
        self.headers = {
            'Client-ID': str(uuid.uuid4()),
            'X-Project-ID': self.project_id
        }

        # TODO(kgriffs): Add support in self.simulate_* for a "base path"
        # so that we don't have to concatenate against self.url_prefix
        # all over the place.
        self.queue_path = self.url_prefix + '/queues/fizbit'
        self.messages_path = self.queue_path + '/messages'

        doc = '{"_ttl": 60}'
        self.simulate_put(self.queue_path, body=doc, headers=self.headers)
示例#11
0
    def test_unacceptable_new_ttl(self, ttl):
        href = self._get_a_claim()

        self.simulate_patch(href, self.project_id,
                            body=jsonutils.dumps({'ttl': ttl}))

        self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#12
0
    def setUp(self):
        super(MessagesBaseTest, self).setUp()

        self.default_message_ttl = self.boot.transport._defaults.message_ttl

        if self.conf.pooling:
            for i in range(4):
                uri = self.conf['drivers:management_store:mongodb'].uri
                doc = {'weight': 100, 'uri': uri}
                self.simulate_put(self.url_prefix + '/pools/' + str(i),
                                  body=jsonutils.dumps(doc))
                self.assertEqual(self.srmock.status, falcon.HTTP_201)

        self.project_id = '7e55e1a7e'
        self.headers = {
            'Client-ID': str(uuid.uuid4()),
            'X-Project-ID': self.project_id
        }

        # TODO(kgriffs): Add support in self.simulate_* for a "base path"
        # so that we don't have to concatenate against self.url_prefix
        # all over the place.
        self.queue_path = self.url_prefix + '/queues/fizbit'
        self.messages_path = self.queue_path + '/messages'

        doc = '{"_ttl": 60}'
        self.simulate_put(self.queue_path, body=doc, headers=self.headers)
示例#13
0
    def test_unacceptable_new_ttl(self, ttl):
        href = self._get_a_claim()

        self.simulate_patch(href, self.project_id,
                            body=jsonutils.dumps({'ttl': ttl}))

        self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#14
0
def flavor(test, name, pool, capabilities={}):
    """A context manager for constructing a flavor for use in testing.

    Deletes the flavor after exiting the context.

    :param test: Must expose simulate_* methods
    :param name: Name for this flavor
    :type name: six.text_type
    :type pool: six.text_type
    :type capabilities: dict
    :returns: (name, uri, capabilities)
    :rtype: see above

    """

    doc = {'pool': pool, 'capabilities': capabilities}
    path = test.url_prefix + '/flavors/' + name

    test.simulate_put(path, body=jsonutils.dumps(doc))

    try:
        yield name, pool, capabilities

    finally:
        test.simulate_delete(path)
示例#15
0
def flavors(test, count, pool):
    """A context manager for constructing flavors for use in testing.

    Deletes the flavors after exiting the context.

    :param test: Must expose simulate_* methods
    :param count: Number of pools to create
    :type count: int
    :returns: (paths, pool, capabilities)
    :rtype: ([six.text_type], [six.text_type], [dict])

    """

    base = test.url_prefix + '/flavors/'
    args = sorted([(base + str(i), {str(i): i}, str(i)) for i in range(count)],
                  key=lambda tup: tup[2])
    for path, capabilities, _ in args:
        doc = {'pool': pool, 'capabilities': capabilities}
        test.simulate_put(path, body=jsonutils.dumps(doc))

    try:
        yield args
    finally:
        for path, _, _ in args:
            test.simulate_delete(path)
示例#16
0
    def _prepare_messages(self, count):
        doc = {'messages': [{'body': 239, 'ttl': 300}] * count}
        body = jsonutils.dumps(doc)
        self.simulate_post(self.messages_path, body=body,
                           headers=self.headers)

        self.assertEqual(self.srmock.status, falcon.HTTP_201)
示例#17
0
    def _prepare_messages(self, count):
        doc = {'messages': [{'body': 239, 'ttl': 300}] * count}
        body = jsonutils.dumps(doc)
        self.simulate_post(self.messages_path, body=body,
                           headers=self.headers)

        self.assertEqual(self.srmock.status, falcon.HTTP_201)
示例#18
0
    def setUp(self):
        super(MessagesBaseTest, self).setUp()

        if self.conf.pooling:
            for i in range(4):
                uri = self.conf['drivers:management_store:mongodb'].uri
                doc = {'weight': 100, 'uri': uri}
                self.simulate_put(self.url_prefix + '/pools/' + str(i),
                                  body=jsonutils.dumps(doc))
                self.assertEqual(self.srmock.status, falcon.HTTP_201)

        self.project_id = '7e55e1a7e'

        # TODO(kgriffs): Add support in self.simulate_* for a "base path"
        # so that we don't have to concatenate against self.url_prefix
        # all over the place.
        self.queue_path = self.url_prefix + '/queues/fizbit'
        self.messages_path = self.queue_path + '/messages'

        doc = '{"_ttl": 60}'
        self.simulate_put(self.queue_path, self.project_id, body=doc)

        # NOTE(kgriffs): Also register without a project for tests
        # that do not specify a project.
        #
        # TODO(kgriffs): Should a project id always be required or
        # automatically supplied in the simulate_* methods?
        self.simulate_put(self.queue_path, body=doc)

        self.headers = {
            'Client-ID': str(uuid.uuid4()),
        }
示例#19
0
文件: test_pools.py 项目: rose/zaqar
def pools(test, count, uri, group):
    """A context manager for constructing pools for use in testing.

    Deletes the pools after exiting the context.

    :param test: Must expose simulate_* methods
    :param count: Number of pools to create
    :type count: int
    :returns: (paths, weights, uris, options)
    :rtype: ([six.text_type], [int], [six.text_type], [dict])
    """
    base = test.url_prefix + '/pools/'
    args = [(base + str(i), i,
             {str(i): i})
            for i in range(count)]
    for path, weight, option in args:
        doc = {'weight': weight, 'uri': uri,
               'group': group, 'options': option}
        test.simulate_put(path, body=jsonutils.dumps(doc))

    try:
        yield args
    finally:
        for path, _, _ in args:
            test.simulate_delete(path)
示例#20
0
文件: test_pools.py 项目: rose/zaqar
def pool(test, name, weight, uri, group=None, options={}):
    """A context manager for constructing a pool for use in testing.

    Deletes the pool after exiting the context.

    :param test: Must expose simulate_* methods
    :param name: Name for this pool
    :type name: six.text_type
    :type weight: int
    :type uri: six.text_type
    :type options: dict
    :returns: (name, weight, uri, options)
    :rtype: see above
    """
    doc = {'weight': weight, 'uri': uri,
           'group': group, 'options': options}
    path = test.url_prefix + '/pools/' + name

    test.simulate_put(path, body=jsonutils.dumps(doc))

    try:
        yield name, weight, uri, group, options

    finally:
        test.simulate_delete(path)
示例#21
0
    def test_post_optional_ttl(self):
        sample_messages = {
            'messages': [
                {
                    'body': 239
                },
                {
                    'body': {
                        'key': 'value'
                    },
                    'ttl': 200
                },
            ],
        }

        # Manually check default TTL is max from config

        sample_doc = jsonutils.dumps(sample_messages)
        result = self.simulate_post(self.messages_path,
                                    body=sample_doc,
                                    headers=self.headers)

        self.assertEqual(self.srmock.status, falcon.HTTP_201)
        result_doc = jsonutils.loads(result[0])

        href = result_doc['resources'][0]
        result = self.simulate_get(href, headers=self.headers)
        message = jsonutils.loads(result[0])

        self.assertEqual(self.default_message_ttl, message['ttl'])
示例#22
0
    def setUp(self):
        super(MessagesBaseTest, self).setUp()

        if self.conf.pooling:
            for i in range(4):
                uri = self.conf['drivers:storage:mongodb'].uri
                doc = {'weight': 100, 'uri': uri}
                self.simulate_put(self.url_prefix + '/pools/' + str(i),
                                  body=jsonutils.dumps(doc))
                self.assertEqual(self.srmock.status, falcon.HTTP_201)

        self.project_id = '7e55e1a7e'

        # TODO(kgriffs): Add support in self.simulate_* for a "base path"
        # so that we don't have to concatenate against self.url_prefix
        # all over the place.
        self.queue_path = self.url_prefix + '/queues/fizbit'
        self.messages_path = self.queue_path + '/messages'

        doc = '{"_ttl": 60}'
        self.simulate_put(self.queue_path, self.project_id, body=doc)

        # NOTE(kgriffs): Also register without a project for tests
        # that do not specify a project.
        #
        # TODO(kgriffs): Should a project id always be required or
        # automatically supplied in the simulate_* methods?
        self.simulate_put(self.queue_path, body=doc)

        self.headers = {
            'Client-ID': str(uuid.uuid4()),
        }
示例#23
0
    def test_unacceptable_ttl(self, ttl):
        doc = {'messages': [{'ttl': ttl, 'body': None}]}

        self.simulate_post(self.queue_path + '/messages',
                           body=jsonutils.dumps(doc),
                           headers=self.headers)

        self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#24
0
 def test_put_raises_if_invalid_uri(self, uri):
     path = self.url_prefix + '/pools/' + str(uuid.uuid1())
     self.simulate_put(path,
                       body=jsonutils.dumps({
                           'weight': 1,
                           'uri': uri
                       }))
     self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#25
0
    def test_unacceptable_ttl(self, ttl):
        doc = {'messages': [{'ttl': ttl, 'body': None}]}

        self.simulate_post(self.queue_path + '/messages',
                           body=jsonutils.dumps(doc),
                           headers=self.headers)

        self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#26
0
文件: test_pools.py 项目: rose/zaqar
 def setUp(self):
     super(PoolsBaseTest, self).setUp()
     self.doc = {'weight': 100,
                 'group': 'mygroup',
                 'uri': 'sqlite://:memory:'}
     self.pool = self.url_prefix + '/pools/' + str(uuid.uuid1())
     self.simulate_put(self.pool, body=jsonutils.dumps(self.doc))
     self.assertEqual(self.srmock.status, falcon.HTTP_201)
示例#27
0
    def _post_messages(self, target, repeat=1):
        doc = {'messages': [{'body': 239, 'ttl': 300}] * repeat}
        body = jsonutils.dumps(doc)

        return self.simulate_post(target,
                                  self.project_id,
                                  body=body,
                                  headers=self.headers)
示例#28
0
    def test_queue_create_no_flavor(self):
        metadata = {'_flavor': self.flavor}

        self.simulate_delete(self.flavor_path)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        self.simulate_put(self.queue_path, body=jsonutils.dumps(metadata))
        self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#29
0
    def test_queue_create_no_flavor(self):
        metadata = {'_flavor': self.flavor}

        self.simulate_delete(self.flavor_path)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        self.simulate_put(self.queue_path, body=jsonutils.dumps(metadata))
        self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#30
0
    def test_create_flavor_no_pool(self):
        self.simulate_delete(self.flavor_path)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        self.simulate_delete(self.pool_path)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        self.simulate_put(self.flavor_path, body=jsonutils.dumps(self.doc))
        self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#31
0
    def _patch_test(self, doc):
        self.simulate_patch(self.pool, body=jsonutils.dumps(doc))
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        result = self.simulate_get(self.pool, query_string='?detailed=True')
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        pool = jsonutils.loads(result[0])
        self._pool_expect(pool, self.pool, doc['weight'], doc['uri'])
        self.assertEqual(pool['options'], doc['options'])
示例#32
0
    def test_create_flavor_no_pool(self):
        self.simulate_delete(self.flavor_path)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        self.simulate_delete(self.pool_path)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        self.simulate_put(self.flavor_path, body=jsonutils.dumps(self.doc))
        self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#33
0
    def _patch_test(self, doc):
        self.simulate_patch(self.flavor_path, body=jsonutils.dumps(doc))
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        result = self.simulate_get(self.flavor_path,
                                   query_string='?detailed=True')
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        pool = jsonutils.loads(result[0])
        self._flavor_expect(pool, self.flavor_path, doc['pool'])
        self.assertEqual(pool['capabilities'], doc['capabilities'])
示例#34
0
 def setUp(self):
     super(PoolsBaseTest, self).setUp()
     self.doc = {
         'weight': 100,
         'group': 'mygroup',
         'uri': 'sqlite://:memory:'
     }
     self.pool = self.url_prefix + '/pools/' + str(uuid.uuid1())
     self.simulate_put(self.pool, body=jsonutils.dumps(self.doc))
     self.assertEqual(self.srmock.status, falcon.HTTP_201)
示例#35
0
    def test_put_existing_overwrites(self):
        # NOTE(cabrera): setUp creates default flavor
        expect = self.doc
        self.simulate_put(self.flavor_path, body=jsonutils.dumps(expect))
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        result = self.simulate_get(self.flavor_path)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        doc = jsonutils.loads(result[0])
        self.assertEqual(doc['pool'], expect['pool'])
示例#36
0
    def _patch_test(self, doc):
        self.simulate_patch(self.flavor_path,
                            body=jsonutils.dumps(doc))
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        result = self.simulate_get(self.flavor_path,
                                   query_string='?detailed=True')
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        pool = jsonutils.loads(result[0])
        self._flavor_expect(pool, self.flavor_path, doc['pool'])
        self.assertEqual(pool['capabilities'], doc['capabilities'])
示例#37
0
    def test_put_existing_overwrites(self):
        # NOTE(cabrera): setUp creates default flavor
        expect = self.doc
        self.simulate_put(self.flavor_path,
                          body=jsonutils.dumps(expect))
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        result = self.simulate_get(self.flavor_path)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        doc = jsonutils.loads(result[0])
        self.assertEqual(doc['pool'], expect['pool'])
示例#38
0
    def setUp(self):
        super(FlavorsBaseTest, self).setUp()
        self.queue = 'test-queue'
        self.queue_path = self.url_prefix + '/queues/' + self.queue

        self.pool = 'mypool'
        self.pool_group = 'mypool-group'
        self.pool_path = self.url_prefix + '/pools/' + self.pool
        self.pool_doc = {
            'weight': 100,
            'group': self.pool_group,
            'uri': 'sqlite://:memory:'
        }
        self.simulate_put(self.pool_path, body=jsonutils.dumps(self.pool_doc))

        self.flavor = 'test-flavor'
        self.doc = {'capabilities': {}, 'pool': self.pool_group}
        self.flavor_path = self.url_prefix + '/flavors/' + self.flavor
        self.simulate_put(self.flavor_path, body=jsonutils.dumps(self.doc))
        self.assertEqual(self.srmock.status, falcon.HTTP_201)
示例#39
0
文件: test_pools.py 项目: rose/zaqar
    def _patch_test(self, doc):
        self.simulate_patch(self.pool,
                            body=jsonutils.dumps(doc))
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        result = self.simulate_get(self.pool,
                                   query_string='?detailed=True')
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        pool = jsonutils.loads(result[0])
        self._pool_expect(pool, self.pool, doc['weight'],
                          doc['uri'])
        self.assertEqual(pool['options'], doc['options'])
示例#40
0
    def test_exceeded_message_posting(self):
        # Total (raw request) size
        doc = jsonutils.dumps([{'body': "some body", 'ttl': 100}] * 20,
                              indent=4)

        max_len = self.transport_cfg.max_messages_post_size
        long_doc = doc + (' ' * (max_len - len(doc) + 1))

        self.simulate_post(self.queue_path + '/messages',
                           body=long_doc,
                           headers=self.headers)

        self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#41
0
    def test_exceeded_message_posting(self):
        # Total (raw request) size
        doc = {'messages': [{'body': "some body", 'ttl': 100}] * 20}
        body = jsonutils.dumps(doc, indent=4)

        max_len = self.transport_cfg.max_messages_post_size
        long_body = body + (' ' * (max_len - len(body) + 1))

        self.simulate_post(self.queue_path + '/messages',
                           body=long_body,
                           headers=self.headers)

        self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#42
0
    def setUp(self):
        super(ClaimsBaseTest, self).setUp()

        self.project_id = '480924'
        self.queue_path = self.url_prefix + '/queues/fizbit'
        self.claims_path = self.queue_path + '/claims'
        self.messages_path = self.queue_path + '/messages'

        doc = '{"_ttl": 60}'

        self.simulate_put(self.queue_path, self.project_id, body=doc)
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        doc = jsonutils.dumps([{'body': 239, 'ttl': 300}] * 10)
        self.simulate_post(self.queue_path + '/messages', self.project_id,
                           body=doc, headers={'Client-ID': str(uuid.uuid4())})
        self.assertEqual(self.srmock.status, falcon.HTTP_201)
示例#43
0
    def setUp(self):
        super(ClaimsBaseTest, self).setUp()

        self.project_id = '480924'
        self.queue_path = self.url_prefix + '/queues/fizbit'
        self.claims_path = self.queue_path + '/claims'
        self.messages_path = self.queue_path + '/messages'

        doc = '{"_ttl": 60}'

        self.simulate_put(self.queue_path, self.project_id, body=doc)
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        doc = jsonutils.dumps([{'body': 239, 'ttl': 300}] * 10)
        self.simulate_post(self.queue_path + '/messages', self.project_id,
                           body=doc, headers={'Client-ID': str(uuid.uuid4())})
        self.assertEqual(self.srmock.status, falcon.HTTP_201)
示例#44
0
    def test_post_optional_ttl(self):
        sample_messages = {
            'messages': [
                {'body': 239},
                {'body': {'key': 'value'}, 'ttl': 200},
            ],
        }

        # Manually check default TTL is max from config

        sample_doc = jsonutils.dumps(sample_messages)
        result = self.simulate_post(self.messages_path,
                                    body=sample_doc, headers=self.headers)

        self.assertEqual(self.srmock.status, falcon.HTTP_201)
        result_doc = jsonutils.loads(result[0])

        href = result_doc['resources'][0]
        result = self.simulate_get(href, headers=self.headers)
        message = jsonutils.loads(result[0])

        self.assertEqual(self.default_message_ttl, message['ttl'])
示例#45
0
def pools(test, count, uri, group):
    """A context manager for constructing pools for use in testing.

    Deletes the pools after exiting the context.

    :param test: Must expose simulate_* methods
    :param count: Number of pools to create
    :type count: int
    :returns: (paths, weights, uris, options)
    :rtype: ([six.text_type], [int], [six.text_type], [dict])
    """
    base = test.url_prefix + '/pools/'
    args = [(base + str(i), i, {str(i): i}) for i in range(count)]
    for path, weight, option in args:
        doc = {'weight': weight, 'uri': uri, 'group': group, 'options': option}
        test.simulate_put(path, body=jsonutils.dumps(doc))

    try:
        yield args
    finally:
        for path, _, _ in args:
            test.simulate_delete(path)
示例#46
0
def pool(test, name, weight, uri, group=None, options={}):
    """A context manager for constructing a pool for use in testing.

    Deletes the pool after exiting the context.

    :param test: Must expose simulate_* methods
    :param name: Name for this pool
    :type name: six.text_type
    :type weight: int
    :type uri: six.text_type
    :type options: dict
    :returns: (name, weight, uri, options)
    :rtype: see above
    """
    doc = {'weight': weight, 'uri': uri, 'group': group, 'options': options}
    path = test.url_prefix + '/pools/' + name

    test.simulate_put(path, body=jsonutils.dumps(doc))

    try:
        yield name, weight, uri, group, options

    finally:
        test.simulate_delete(path)
示例#47
0
 def test_patch_raises_400_on_invalid_pool(self, pool):
     self.simulate_patch(self.flavor_path,
                         body=jsonutils.dumps({'pool': pool}))
     self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#48
0
文件: utils.py 项目: rose/zaqar
def json_encode(obj):
    return encodeutils.safe_encode(jsonutils.dumps(obj), 'utf-8')
示例#49
0
 def test_patch_raises_if_missing_fields(self):
     self.simulate_patch(self.flavor_path,
                         body=jsonutils.dumps({'location': 1}))
     self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#50
0
    def _test_post(self, sample_messages):
        sample_doc = jsonutils.dumps({'messages': sample_messages})

        result = self.simulate_post(self.messages_path,
                                    body=sample_doc,
                                    headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        result_doc = jsonutils.loads(result[0])

        msg_ids = self._get_msg_ids(self.srmock.headers_dict)
        self.assertEqual(len(msg_ids), len(sample_messages))

        expected_resources = [
            six.text_type(self.messages_path + '/' + id) for id in msg_ids
        ]
        self.assertEqual(expected_resources, result_doc['resources'])

        # NOTE(kgriffs): As of v1.1, "partial" is no longer given
        # in the response document.
        self.assertNotIn('partial', result_doc)

        self.assertEqual(len(msg_ids), len(sample_messages))

        lookup = dict([(m['ttl'], m['body']) for m in sample_messages])

        # Test GET on the message resource directly
        # NOTE(cpp-cabrera): force the passing of time to age a message
        timeutils_utcnow = 'zaqar.openstack.common.timeutils.utcnow'
        now = timeutils.utcnow() + datetime.timedelta(seconds=10)
        with mock.patch(timeutils_utcnow) as mock_utcnow:
            mock_utcnow.return_value = now
            for msg_id in msg_ids:
                message_uri = self.messages_path + '/' + msg_id

                headers = self.headers.copy()
                headers['X-Project-ID'] = '777777'
                # Wrong project ID
                self.simulate_get(message_uri, headers=headers)
                self.assertEqual(self.srmock.status, falcon.HTTP_404)

                # Correct project ID
                result = self.simulate_get(message_uri, headers=self.headers)
                self.assertEqual(self.srmock.status, falcon.HTTP_200)

                # Check message properties
                message = jsonutils.loads(result[0])
                self.assertEqual(message['href'], message_uri)
                self.assertEqual(message['body'], lookup[message['ttl']])
                self.assertEqual(msg_id, message['id'])

                # no negative age
                # NOTE(cpp-cabrera): testtools lacks GreaterThanEqual on py26
                self.assertThat(message['age'], matchers.GreaterThan(-1))

        # Test bulk GET
        query_string = 'ids=' + ','.join(msg_ids)
        result = self.simulate_get(self.messages_path,
                                   query_string=query_string,
                                   headers=self.headers)

        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        result_doc = jsonutils.loads(result[0])
        expected_ttls = set(m['ttl'] for m in sample_messages)
        actual_ttls = set(m['ttl'] for m in result_doc['messages'])
        self.assertFalse(expected_ttls - actual_ttls)
        actual_ids = set(m['id'] for m in result_doc['messages'])
        self.assertFalse(set(msg_ids) - actual_ids)
示例#51
0
 def test_patch_raises_400_on_invalid_options(self, options):
     self.simulate_patch(self.pool,
                         body=jsonutils.dumps({'options': options}))
     self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#52
0
 def test_patch_raises_400_on_invalid_capabilities(self, capabilities):
     doc = {'capabilities': capabilities}
     self.simulate_patch(self.flavor_path, body=jsonutils.dumps(doc))
     self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#53
0
    def _test_post(self, sample_messages):
        sample_doc = jsonutils.dumps(sample_messages)

        result = self.simulate_post(self.messages_path,
                                    self.project_id,
                                    body=sample_doc,
                                    headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        result_doc = jsonutils.loads(result[0])

        msg_ids = self._get_msg_ids(self.srmock.headers_dict)
        self.assertEqual(len(msg_ids), len(sample_messages))

        expected_resources = [
            six.text_type(self.messages_path + '/' + id) for id in msg_ids
        ]
        self.assertEqual(expected_resources, result_doc['resources'])

        # NOTE(kgriffs): As of the Icehouse release, drivers are
        # required to either completely succeed, or completely fail
        # to enqueue the entire batch of messages.
        self.assertFalse(result_doc['partial'])

        self.assertEqual(len(msg_ids), len(sample_messages))

        lookup = dict([(m['ttl'], m['body']) for m in sample_messages])

        # Test GET on the message resource directly
        # NOTE(cpp-cabrera): force the passing of time to age a message
        timeutils_utcnow = 'zaqar.openstack.common.timeutils.utcnow'
        now = timeutils.utcnow() + datetime.timedelta(seconds=10)
        with mock.patch(timeutils_utcnow) as mock_utcnow:
            mock_utcnow.return_value = now
            for msg_id in msg_ids:
                message_uri = self.messages_path + '/' + msg_id

                # Wrong project ID
                self.simulate_get(message_uri, '777777')
                self.assertEqual(self.srmock.status, falcon.HTTP_404)

                # Correct project ID
                result = self.simulate_get(message_uri, self.project_id)
                self.assertEqual(self.srmock.status, falcon.HTTP_200)
                self.assertEqual(self.srmock.headers_dict['Content-Location'],
                                 message_uri)

                # Check message properties
                message = jsonutils.loads(result[0])
                self.assertEqual(message['href'], message_uri)
                self.assertEqual(message['body'], lookup[message['ttl']])

                # no negative age
                # NOTE(cpp-cabrera): testtools lacks GreaterThanEqual on py26
                self.assertThat(message['age'], matchers.GreaterThan(-1))

        # Test bulk GET
        query_string = 'ids=' + ','.join(msg_ids)
        result = self.simulate_get(self.messages_path,
                                   self.project_id,
                                   query_string=query_string)

        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        result_doc = jsonutils.loads(result[0])
        expected_ttls = set(m['ttl'] for m in sample_messages)
        actual_ttls = set(m['ttl'] for m in result_doc)
        self.assertFalse(expected_ttls - actual_ttls)
示例#54
0
 def _post_messages(self, target, repeat=1):
     doc = jsonutils.dumps([{'body': 239, 'ttl': 300}] * repeat)
     return self.simulate_post(target, self.project_id, body=doc,
                               headers=self.headers)
示例#55
0
    def test_unacceptable_ttl_or_grace(self, ttl_grace):
        ttl, grace = ttl_grace
        self.simulate_post(self.claims_path, self.project_id,
                           body=jsonutils.dumps({'ttl': ttl, 'grace': grace}))

        self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#56
0
 def test_patch_nonexistent_claim_404s(self):
     patch_data = jsonutils.dumps({'ttl': 100})
     self.simulate_patch(self.claims_path + '/a', body=patch_data)
     self.assertEqual(self.srmock.status, falcon.HTTP_404)
示例#57
0
    def _test_post(self, sample_messages):
        sample_doc = jsonutils.dumps(sample_messages)

        result = self.simulate_post(self.messages_path, self.project_id,
                                    body=sample_doc, headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        result_doc = jsonutils.loads(result[0])

        msg_ids = self._get_msg_ids(self.srmock.headers_dict)
        self.assertEqual(len(msg_ids), len(sample_messages))

        expected_resources = [six.text_type(self.messages_path + '/' + id)
                              for id in msg_ids]
        self.assertEqual(expected_resources, result_doc['resources'])

        # NOTE(kgriffs): As of the Icehouse release, drivers are
        # required to either completely succeed, or completely fail
        # to enqueue the entire batch of messages.
        self.assertFalse(result_doc['partial'])

        self.assertEqual(len(msg_ids), len(sample_messages))

        lookup = dict([(m['ttl'], m['body']) for m in sample_messages])

        # Test GET on the message resource directly
        # NOTE(cpp-cabrera): force the passing of time to age a message
        timeutils_utcnow = 'zaqar.openstack.common.timeutils.utcnow'
        now = timeutils.utcnow() + datetime.timedelta(seconds=10)
        with mock.patch(timeutils_utcnow) as mock_utcnow:
            mock_utcnow.return_value = now
            for msg_id in msg_ids:
                message_uri = self.messages_path + '/' + msg_id

                # Wrong project ID
                self.simulate_get(message_uri, '777777')
                self.assertEqual(self.srmock.status, falcon.HTTP_404)

                # Correct project ID
                result = self.simulate_get(message_uri, self.project_id)
                self.assertEqual(self.srmock.status, falcon.HTTP_200)
                self.assertEqual(self.srmock.headers_dict['Content-Location'],
                                 message_uri)

                # Check message properties
                message = jsonutils.loads(result[0])
                self.assertEqual(message['href'], message_uri)
                self.assertEqual(message['body'], lookup[message['ttl']])

                # no negative age
                # NOTE(cpp-cabrera): testtools lacks GreaterThanEqual on py26
                self.assertThat(message['age'],
                                matchers.GreaterThan(-1))

        # Test bulk GET
        query_string = 'ids=' + ','.join(msg_ids)
        result = self.simulate_get(self.messages_path, self.project_id,
                                   query_string=query_string)

        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        result_doc = jsonutils.loads(result[0])
        expected_ttls = set(m['ttl'] for m in sample_messages)
        actual_ttls = set(m['ttl'] for m in result_doc)
        self.assertFalse(expected_ttls - actual_ttls)
示例#58
0
 def test_patch_raises_404_if_pool_not_found(self):
     self.simulate_patch(self.url_prefix + '/pools/notexists',
                         body=jsonutils.dumps({'weight': 1}))
     self.assertEqual(self.srmock.status, falcon.HTTP_404)
示例#59
0
 def test_put_raises_if_invalid_capabilities(self, capabilities):
     path = self.url_prefix + '/flavors/' + str(uuid.uuid1())
     doc = {'pool': 'a', 'capabilities': capabilities}
     self.simulate_put(path, body=jsonutils.dumps(doc))
     self.assertEqual(self.srmock.status, falcon.HTTP_400)
示例#60
0
 def test_patch_raises_404_if_flavor_not_found(self):
     self.simulate_patch(self.url_prefix + '/flavors/notexists',
                         body=jsonutils.dumps({'pool': 'test'}))
     self.assertEqual(self.srmock.status, falcon.HTTP_404)