def test_update_metadata(self):
        xyz_queue_path = self.url_prefix + '/queues/xyz'
        xyz_queue_path_metadata = xyz_queue_path + '/metadata'

        # Create
        project_id = '480924'
        self.simulate_put(xyz_queue_path, project_id)
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        # Set meta
        doc1 = '{"messages": {"ttl": 600}}'
        self.simulate_put(xyz_queue_path_metadata, project_id, body=doc1)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Update
        doc2 = '{"messages": {"ttl": 100}}'
        self.simulate_put(xyz_queue_path_metadata, project_id, body=doc2)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Get
        result = self.simulate_get(xyz_queue_path_metadata, project_id)
        result_doc = jsonutils.loads(result[0])

        self.assertEqual(result_doc, jsonutils.loads(doc2))
        self.assertEqual(self.srmock.headers_dict['Content-Location'],
                         xyz_queue_path_metadata)
    def test_basics_thoroughly(self, project_id):
        headers = {
            'Client-ID': str(uuid.uuid4()),
            'X-Project-ID': project_id
        }
        gumshoe_queue_path_metadata = self.gumshoe_queue_path + '/metadata'
        gumshoe_queue_path_stats = self.gumshoe_queue_path + '/stats'

        # Stats are empty - queue not created yet
        self.simulate_get(gumshoe_queue_path_stats, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        # Metadata not found - queue not created yet
        self.simulate_get(gumshoe_queue_path_metadata, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_404)

        # Create
        self.simulate_put(self.gumshoe_queue_path, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        location = self.srmock.headers_dict['Location']
        self.assertEqual(location, self.gumshoe_queue_path)

        # Ensure queue existence
        self.simulate_head(self.gumshoe_queue_path, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Add metadata

        doc = '{"messages": {"ttl": 600}}'
        self.simulate_put(gumshoe_queue_path_metadata,
                          headers=headers, body=doc)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Fetch metadata
        result = self.simulate_get(gumshoe_queue_path_metadata,
                                   headers=headers)
        result_doc = jsonutils.loads(result[0])
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        self.assertEqual(result_doc, jsonutils.loads(doc))

        # Stats empty queue
        self.simulate_get(gumshoe_queue_path_stats, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        # Delete
        self.simulate_delete(self.gumshoe_queue_path, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Get non-existent queue
        self.simulate_get(self.gumshoe_queue_path, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_404)

        # Get non-existent stats
        self.simulate_get(gumshoe_queue_path_stats, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        # Get non-existent metadata
        self.simulate_get(gumshoe_queue_path_metadata, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_404)
    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'])
        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 = 'marconi.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)
    def test_simple(self):
        self.headers = {
            'Client-ID': str(uuid.uuid4()),
            'X-Project-ID': '338730984abc_1'
        }

        gumshoe_queue_path = self.url_prefix + '/queues/gumshoe'
        doc = '{"messages": {"ttl": 600}}'
        self.simulate_put(gumshoe_queue_path,
                          headers=self.headers,
                          body=doc)
        self.assertEqual(self.srmock.status, falcon.HTTP_503)

        location = ('Location', gumshoe_queue_path)
        self.assertNotIn(location, self.srmock.headers)

        result = self.simulate_get(gumshoe_queue_path + '/metadata',
                                   headers=self.headers)
        result_doc = jsonutils.loads(result[0])
        self.assertEqual(self.srmock.status, falcon.HTTP_503)
        self.assertNotEqual(result_doc, jsonutils.loads(doc))

        self.simulate_get(gumshoe_queue_path + '/stats',
                          headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_503)

        self.simulate_get(self.url_prefix + '/queues',
                          headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_503)

        self.simulate_delete(gumshoe_queue_path, headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_503)
    def test_update_metadata(self):
        xyz_queue_path = self.url_prefix + '/queues/xyz'
        xyz_queue_path_metadata = xyz_queue_path + '/metadata'

        # Create
        self.simulate_put(xyz_queue_path, headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        # Set meta
        doc1 = '{"messages": {"ttl": 600}}'
        self.simulate_put(xyz_queue_path_metadata,
                          headers=self.headers,
                          body=doc1)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Update
        doc2 = '{"messages": {"ttl": 100}}'
        self.simulate_put(xyz_queue_path_metadata,
                          headers=self.headers,
                          body=doc2)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Get
        result = self.simulate_get(xyz_queue_path_metadata,
                                   headers=self.headers)
        result_doc = jsonutils.loads(result[0])

        self.assertEqual(result_doc, jsonutils.loads(doc2))
        self.assertEqual(self.srmock.headers_dict['Content-Location'],
                         xyz_queue_path_metadata)
    def test_basics_thoroughly(self, project_id):
        headers = {"Client-ID": str(uuid.uuid4()), "X-Project-ID": project_id}
        gumshoe_queue_path_metadata = self.gumshoe_queue_path + "/metadata"
        gumshoe_queue_path_stats = self.gumshoe_queue_path + "/stats"

        # Stats are empty - queue not created yet
        self.simulate_get(gumshoe_queue_path_stats, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        # Metadata not found - queue not created yet
        self.simulate_get(gumshoe_queue_path_metadata, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_404)

        # Create
        self.simulate_put(self.gumshoe_queue_path, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        location = self.srmock.headers_dict["Location"]
        self.assertEqual(location, self.gumshoe_queue_path)

        # Ensure queue existence
        self.simulate_head(self.gumshoe_queue_path, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Add metadata

        doc = '{"messages": {"ttl": 600}}'
        self.simulate_put(gumshoe_queue_path_metadata, headers=headers, body=doc)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Fetch metadata
        result = self.simulate_get(gumshoe_queue_path_metadata, headers=headers)
        result_doc = jsonutils.loads(result[0])
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        self.assertEqual(result_doc, jsonutils.loads(doc))

        # Stats empty queue
        self.simulate_get(gumshoe_queue_path_stats, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        # Delete
        self.simulate_delete(self.gumshoe_queue_path, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Get non-existent queue
        self.simulate_get(self.gumshoe_queue_path, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_404)

        # Get non-existent stats
        self.simulate_get(gumshoe_queue_path_stats, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        # Get non-existent metadata
        self.simulate_get(gumshoe_queue_path_metadata, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_404)
    def test_basics_thoroughly(self, project_id):
        gumshoe_queue_path_metadata = self.gumshoe_queue_path + '/metadata'
        gumshoe_queue_path_stats = self.gumshoe_queue_path + '/stats'

        # Stats not found - queue not created yet
        self.simulate_get(gumshoe_queue_path_stats, project_id)
        self.assertEqual(self.srmock.status, falcon.HTTP_404)

        # Metadata not found - queue not created yet
        self.simulate_get(gumshoe_queue_path_metadata, project_id)
        self.assertEqual(self.srmock.status, falcon.HTTP_404)

        # Create
        self.simulate_put(self.gumshoe_queue_path, project_id)
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        location = self.srmock.headers_dict['Location']
        self.assertEqual(location, self.gumshoe_queue_path)

        # Ensure queue existence
        self.simulate_head(self.gumshoe_queue_path, project_id)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Add metadata
        doc = '{"messages": {"ttl": 600}}'
        self.simulate_put(gumshoe_queue_path_metadata,
                          project_id, body=doc)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Fetch metadata
        result = self.simulate_get(gumshoe_queue_path_metadata,
                                   project_id)
        result_doc = jsonutils.loads(result[0])
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        self.assertEqual(result_doc, jsonutils.loads(doc))

        # Stats empty queue
        self.simulate_get(gumshoe_queue_path_stats, project_id)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        # Delete
        self.simulate_delete(self.gumshoe_queue_path, project_id)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Get non-existent queue
        self.simulate_get(self.gumshoe_queue_path, project_id)
        self.assertEqual(self.srmock.status, falcon.HTTP_404)

        # Get non-existent stats
        self.simulate_get(gumshoe_queue_path_stats, project_id)
        self.assertEqual(self.srmock.status, falcon.HTTP_404)

        # Get non-existent metadata
        self.simulate_get(gumshoe_queue_path_metadata, project_id)
        self.assertEqual(self.srmock.status, falcon.HTTP_404)
    def test_json_response(self):
        body = self.simulate_get(self.url_prefix)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        content_type = self.srmock.headers_dict['Content-Type']
        self.assertEqual(content_type, 'application/json-home')

        try:
            jsonutils.loads(body[0])
        except ValueError:
            self.fail('Home document is not valid JSON')
    def test_json_response(self):
        body = self.simulate_get(self.url_prefix)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        content_type = self.srmock.headers_dict['Content-Type']
        self.assertEqual(content_type, 'application/json-home')

        try:
            jsonutils.loads(body[0])
        except ValueError:
            self.fail('Home document is not valid JSON')
    def test_list(self):
        path = self.queue_path + '/messages'
        self._post_messages(path, repeat=10)

        query_string = 'limit=3&echo=true'
        body = self.simulate_get(path,
                                 query_string=query_string,
                                 headers=self.headers)

        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        self.assertEqual(self.srmock.headers_dict['Content-Location'],
                         path + '?' + query_string)

        cnt = 0
        while jsonutils.loads(body[0])['messages'] != []:
            contents = jsonutils.loads(body[0])
            [target, params] = contents['links'][0]['href'].split('?')

            for msg in contents['messages']:
                self.simulate_get(msg['href'], headers=self.headers)
                self.assertEqual(self.srmock.status, falcon.HTTP_200)

            body = self.simulate_get(target,
                                     query_string=params,
                                     headers=self.headers)
            cnt += 1

        self.assertEqual(cnt, 4)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        self._empty_message_list(body)

        # Stats
        body = self.simulate_get(self.queue_path + '/stats',
                                 headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        message_stats = jsonutils.loads(body[0])['messages']
        self.assertEqual(self.srmock.headers_dict['Content-Location'],
                         self.queue_path + '/stats')

        # NOTE(kgriffs): The other parts of the stats are tested
        # in tests.storage.base and so are not repeated here.
        expected_pattern = self.queue_path + '/messages/[^/]+$'
        for message_stat_name in ('oldest', 'newest'):
            self.assertThat(message_stats[message_stat_name]['href'],
                            matchers.MatchesRegex(expected_pattern))

        # NOTE(kgriffs): Try to get messages for a missing queue
        body = self.simulate_get(self.url_prefix +
                                 '/queues/nonexistent/messages',
                                 headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        self._empty_message_list(body)
    def test_list(self):
        path = self.queue_path + '/messages'
        self._post_messages(path, repeat=10)

        query_string = 'limit=3&echo=true'
        body = self.simulate_get(path, self.project_id,
                                 query_string=query_string,
                                 headers=self.headers)

        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        self.assertEqual(self.srmock.headers_dict['Content-Location'],
                         path + '?' + query_string)

        cnt = 0
        while self.srmock.status == falcon.HTTP_200:
            contents = jsonutils.loads(body[0])
            [target, params] = contents['links'][0]['href'].split('?')

            for msg in contents['messages']:
                self.simulate_get(msg['href'], self.project_id)
                self.assertEqual(self.srmock.status, falcon.HTTP_200)

            body = self.simulate_get(target, self.project_id,
                                     query_string=params,
                                     headers=self.headers)
            cnt += 1

        self.assertEqual(cnt, 4)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Stats
        body = self.simulate_get(self.queue_path + '/stats', self.project_id)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        message_stats = jsonutils.loads(body[0])['messages']
        self.assertEqual(self.srmock.headers_dict['Content-Location'],
                         self.queue_path + '/stats')

        # NOTE(kgriffs): The other parts of the stats are tested
        # in tests.storage.base and so are not repeated here.
        expected_pattern = self.queue_path + '/messages/[^/]+$'
        for message_stat_name in ('oldest', 'newest'):
            self.assertThat(message_stats[message_stat_name]['href'],
                            matchers.MatchesRegex(expected_pattern))

        # NOTE(kgriffs): Try to get messages for a missing queue
        self.simulate_get(self.url_prefix + '/queues/nonexistent/messages',
                          self.project_id,
                          headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)
Example #12
0
    def test_json_response(self):
        self.headers = {
            'Client-ID': str(uuid.uuid4()),
            'X-Project-ID': '8383830383abc_'
        }
        body = self.simulate_get(self.url_prefix, headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        content_type = self.srmock.headers_dict['Content-Type']
        self.assertEqual(content_type, 'application/json-home')

        try:
            jsonutils.loads(body[0])
        except ValueError:
            self.fail('Home document is not valid JSON')
Example #13
0
    def test_href_template(self):
        self.headers = {
            'Client-ID': str(uuid.uuid4()),
            'X-Project-ID': '8383830383'
        }
        body = self.simulate_get(self.url_prefix, headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        resp = jsonutils.loads(body[0])
        queue_href_template = resp['resources']['rel/queue']['href-template']
        path_1 = 'https://marconi.example.com' + self.url_prefix
        path_2 = 'https://marconi.example.com' + self.url_prefix + '/'

        # Verify all the href template start with the correct version prefix
        for resource in list(resp['resources']):
            self.assertTrue(
                resp['resources'][resource]['href-template'].startswith(
                    self.url_prefix))

        url = urlparse.urljoin(path_1, queue_href_template)
        expected = ('https://marconi.example.com' + self.url_prefix +
                    '/queues/foo')
        self.assertEqual(url.format(queue_name='foo'), expected)

        url = urlparse.urljoin(path_2, queue_href_template)
        self.assertEqual(url.format(queue_name='foo'), expected)
Example #14
0
    def _listing_test(self, count=10, limit=10,
                      marker=None, detailed=False):
        # NOTE(cpp-cabrera): delete initial pool - it will interfere
        # with listing tests
        self.simulate_delete(self.pool)
        query = '?limit={0}&detailed={1}'.format(limit, detailed)
        if marker:
            query += '&marker={2}'.format(marker)

        with pools(self, count, self.doc['uri']) as expected:
            result = self.simulate_get(self.url_prefix + '/pools',
                                       query_string=query)
            self.assertEqual(self.srmock.status, falcon.HTTP_200)
            results = jsonutils.loads(result[0])
            self.assertIsInstance(results, dict)
            self.assertIn('pools', results)
            pool_list = results['pools']
            self.assertEqual(len(pool_list), min(limit, count))
            for s in pool_list:
                # NOTE(flwang): It can't assumed that both sqlalchemy and
                # mongodb can return query result with the same order. Just
                # like the order they're inserted. Actually, sqlalchemy can't
                # guarantee that. So we're leveraging the relationship between
                # pool weight and the index of pools fixture to get the
                # right pool to verify.
                expect = expected[s['weight']]
                path, weight = expect[:2]
                self._pool_expect(s, path, weight, self.doc['uri'])
                if detailed:
                    self.assertIn('options', s)
                    self.assertEqual(s['options'], expect[-1])
                else:
                    self.assertNotIn('options', s)
Example #15
0
    def test_pop_empty_queue(self):

        query_string = 'pop=1'
        result = self.simulate_delete(self.messages_path, self.project_id,
                                      query_string=query_string)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        result_doc = jsonutils.loads(result[0])
        self.assertEqual(result_doc['messages'], [])
Example #16
0
 def test_detailed_get_works(self):
     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, self.doc['weight'],
                       self.doc['uri'])
     self.assertIn('options', pool)
     self.assertEqual(pool['options'], {})
    def test_delete_message_with_invalid_claim_doesnt_delete_message(self):
        path = self.queue_path
        resp = self._post_messages(path + '/messages', 1)
        location = jsonutils.loads(resp[0])['resources'][0]

        self.simulate_delete(location, query_string='claim_id=invalid')
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        self.simulate_get(location, self.project_id)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
Example #18
0
    def test_custom_metadata(self):
        self.simulate_put(self.fizbat_queue_path, '480924')
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        # Set
        doc = '{{"messages": {{"ttl": 600}}, "padding": "{pad}"}}'

        max_size = self.transport_cfg.max_queue_metadata
        padding_len = max_size - (len(doc) - 2)

        doc = doc.format(pad='x' * padding_len)
        self.simulate_put(self.fizbat_queue_path_metadata, '480924', body=doc)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Get
        result = self.simulate_get(self.fizbat_queue_path_metadata, '480924')
        result_doc = jsonutils.loads(result[0])
        self.assertEqual(result_doc, jsonutils.loads(doc))
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
Example #19
0
    def test_claim_creation(self):
        self._prepare_messages(storage.DEFAULT_MESSAGES_PER_CLAIM + 1)

        result = self.simulate_post(self.claims_path,
                                    body='{"ttl": 60, "grace": 60}')

        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        messages = jsonutils.loads(result[0])
        self.assertEqual(len(messages), storage.DEFAULT_MESSAGES_PER_CLAIM)
    def test_custom_metadata(self):
        self.simulate_put(self.fizbat_queue_path, headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        # Set
        doc = '{{"messages": {{"ttl": 600}}, "padding": "{pad}"}}'

        max_size = self.transport_cfg.max_queue_metadata
        padding_len = max_size - (len(doc) - 2)

        doc = doc.format(pad="x" * padding_len)
        self.simulate_put(self.fizbat_queue_path_metadata, headers=self.headers, body=doc)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Get
        result = self.simulate_get(self.fizbat_queue_path_metadata, headers=self.headers)
        result_doc = jsonutils.loads(result[0])
        self.assertEqual(result_doc, jsonutils.loads(doc))
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
Example #21
0
    def test_message_listing(self):
        self._prepare_messages(storage.DEFAULT_MESSAGES_PER_PAGE + 1)

        result = self.simulate_get(self.messages_path,
                                   headers={'Client-ID': str(uuid.uuid4())})

        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        messages = jsonutils.loads(result[0])['messages']
        self.assertEqual(len(messages), storage.DEFAULT_MESSAGES_PER_PAGE)
    def test_claim_creation(self):
        self._prepare_messages(storage.DEFAULT_MESSAGES_PER_CLAIM + 1)

        result = self.simulate_post(self.claims_path,
                                    body='{"ttl": 60, "grace": 60}')

        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        messages = jsonutils.loads(result[0])
        self.assertEqual(len(messages), storage.DEFAULT_MESSAGES_PER_CLAIM)
    def test_message_listing(self):
        self._prepare_messages(storage.DEFAULT_MESSAGES_PER_PAGE + 1)

        result = self.simulate_get(self.messages_path,
                                   headers={'Client-ID': str(uuid.uuid4())})

        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        messages = jsonutils.loads(result[0])['messages']
        self.assertEqual(len(messages), storage.DEFAULT_MESSAGES_PER_PAGE)
Example #24
0
    def test_queue_listing(self):
        # 2 queues to list
        self.simulate_put(self.queue_path + '/q2')
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        with self._prepare_queues(storage.DEFAULT_QUEUES_PER_PAGE + 1):
            result = self.simulate_get(self.queue_path)
            self.assertEqual(self.srmock.status, falcon.HTTP_200)

            queues = jsonutils.loads(result[0])['queues']
            self.assertEqual(len(queues), storage.DEFAULT_QUEUES_PER_PAGE)
Example #25
0
    def test_listing_marker_is_respected(self):
        self.simulate_delete(self.pool)

        with pools(self, 10, self.doc['uri']) as expected:
            result = self.simulate_get(self.url_prefix + '/pools',
                                       query_string='?marker=3')
            self.assertEqual(self.srmock.status, falcon.HTTP_200)
            pool_list = jsonutils.loads(result[0])['pools']
            self.assertEqual(len(pool_list), 6)
            path, weight = expected[4][:2]
            self._pool_expect(pool_list[0], path, weight, self.doc['uri'])
    def test_queue_listing(self):
        # 2 queues to list
        self.simulate_put(self.queue_path + '/q2')
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        with self._prepare_queues(storage.DEFAULT_QUEUES_PER_PAGE + 1):
            result = self.simulate_get(self.queue_path)
            self.assertEqual(self.srmock.status, falcon.HTTP_200)

            queues = jsonutils.loads(result[0])['queues']
            self.assertEqual(len(queues), storage.DEFAULT_QUEUES_PER_PAGE)
Example #27
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'])
Example #28
0
    def test_put_existing_overwrites(self):
        # NOTE(cabrera): setUp creates default pool
        expect = self.doc
        self.simulate_put(self.pool,
                          body=jsonutils.dumps(expect))
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        result = self.simulate_get(self.pool)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        doc = jsonutils.loads(result[0])
        self.assertEqual(doc['weight'], expect['weight'])
        self.assertEqual(doc['uri'], expect['uri'])
Example #29
0
    def test_simple(self):
        gumshoe_queue_path = self.url_prefix + '/queues/gumshoe'
        doc = '{"messages": {"ttl": 600}}'
        self.simulate_put(gumshoe_queue_path, '480924', body=doc)
        self.assertEqual(self.srmock.status, falcon.HTTP_503)

        location = ('Location', gumshoe_queue_path)
        self.assertNotIn(location, self.srmock.headers)

        result = self.simulate_get(gumshoe_queue_path + '/metadata', '480924')
        result_doc = jsonutils.loads(result[0])
        self.assertEqual(self.srmock.status, falcon.HTTP_503)
        self.assertNotEqual(result_doc, jsonutils.loads(doc))

        self.simulate_get(gumshoe_queue_path + '/stats', '480924')
        self.assertEqual(self.srmock.status, falcon.HTTP_503)

        self.simulate_get(self.url_prefix + '/queues', '480924')
        self.assertEqual(self.srmock.status, falcon.HTTP_503)

        self.simulate_delete(gumshoe_queue_path, '480924')
        self.assertEqual(self.srmock.status, falcon.HTTP_503)
    def test_simple(self):
        self.headers = {"Client-ID": str(uuid.uuid4()), "X-Project-ID": "338730984abc_1"}

        gumshoe_queue_path = self.url_prefix + "/queues/gumshoe"
        doc = '{"messages": {"ttl": 600}}'
        self.simulate_put(gumshoe_queue_path, headers=self.headers, body=doc)
        self.assertEqual(self.srmock.status, falcon.HTTP_503)

        location = ("Location", gumshoe_queue_path)
        self.assertNotIn(location, self.srmock.headers)

        result = self.simulate_get(gumshoe_queue_path + "/metadata", headers=self.headers)
        result_doc = jsonutils.loads(result[0])
        self.assertEqual(self.srmock.status, falcon.HTTP_503)
        self.assertNotEqual(result_doc, jsonutils.loads(doc))

        self.simulate_get(gumshoe_queue_path + "/stats", headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_503)

        self.simulate_get(self.url_prefix + "/queues", headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_503)

        self.simulate_delete(gumshoe_queue_path, headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_503)
    def test_update_metadata(self):
        xyz_queue_path = self.url_prefix + "/queues/xyz"
        xyz_queue_path_metadata = xyz_queue_path + "/metadata"

        # Create
        self.simulate_put(xyz_queue_path, headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        # Set meta
        doc1 = '{"messages": {"ttl": 600}}'
        self.simulate_put(xyz_queue_path_metadata, headers=self.headers, body=doc1)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Update
        doc2 = '{"messages": {"ttl": 100}}'
        self.simulate_put(xyz_queue_path_metadata, headers=self.headers, body=doc2)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Get
        result = self.simulate_get(xyz_queue_path_metadata, headers=self.headers)
        result_doc = jsonutils.loads(result[0])

        self.assertEqual(result_doc, jsonutils.loads(doc2))
        self.assertEqual(self.srmock.headers_dict["Content-Location"], xyz_queue_path_metadata)
    def test_no_duplicated_messages_path_in_href(self):
        """Test for bug 1240897."""

        path = self.queue_path + '/messages'
        self._post_messages(path, repeat=1)

        msg_id = self._get_msg_id(self.srmock.headers_dict)

        query_string = 'ids=%s' % msg_id
        body = self.simulate_get(path, self.project_id,
                                 query_string=query_string,
                                 headers=self.headers)
        messages = jsonutils.loads(body[0])

        self.assertNotIn(self.queue_path + '/messages/messages',
                         messages[0]['href'])
Example #33
0
    def test_pop(self, message_count):

        self._post_messages(self.messages_path, repeat=message_count)
        msg_id = self._get_msg_id(self.srmock.headers_dict)
        target = self.messages_path + '/' + msg_id

        self.simulate_get(target, self.project_id)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        query_string = 'pop=' + str(message_count)
        result = self.simulate_delete(self.messages_path, self.project_id,
                                      query_string=query_string)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        result_doc = jsonutils.loads(result[0])

        self.assertEqual(len(result_doc['messages']), message_count)

        self.simulate_get(target, self.project_id)
        self.assertEqual(self.srmock.status, falcon.HTTP_404)
Example #34
0
    def test_href_template(self):
        body = self.simulate_get(self.url_prefix)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        resp = jsonutils.loads(body[0])
        queue_href_template = resp['resources']['rel/queue']['href-template']
        path_1 = 'https://marconi.example.com' + self.url_prefix
        path_2 = 'https://marconi.example.com' + self.url_prefix + '/'

        # Verify all the href template start with the correct version prefix
        for resource in list(resp['resources']):
            self.assertTrue(resp['resources'][resource]['href-template'].
                            startswith(self.url_prefix))

        url = urlparse.urljoin(path_1, queue_href_template)
        expected = ('https://marconi.example.com' + self.url_prefix +
                    '/queues/foo')
        self.assertEqual(url.format(queue_name='foo'), expected)

        url = urlparse.urljoin(path_2, queue_href_template)
        self.assertEqual(url.format(queue_name='foo'), expected)
Example #35
0
    def test_pop_single_message(self):

        self._post_messages(self.messages_path, repeat=5)
        msg_id = self._get_msg_id(self.srmock.headers_dict)
        target = self.messages_path + '/' + msg_id

        self.simulate_get(target, self.project_id)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        # Pop Single message from the queue
        query_string = 'pop=1'
        result = self.simulate_delete(self.messages_path, self.project_id,
                                      query_string=query_string)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        # Get messages from the queue & verify message count
        query_string = 'echo=True'
        result = self.simulate_get(self.messages_path, self.project_id,
                                   query_string=query_string,
                                   headers=self.headers)
        result_doc = jsonutils.loads(result[0])
        actual_msg_count = len(result_doc['messages'])
        expected_msg_count = 4
        self.assertEqual(actual_msg_count, expected_msg_count)
Example #36
0
 def json(self):
     return jsonutils.loads(self._body)
Example #37
0
 def _empty_message_list(self, body):
     self.assertEqual(jsonutils.loads(body[0])['messages'], [])
Example #38
0
 def _deserialize(self, message):
     return jsonutils.loads(message)
Example #39
0
 def json(self):
     return jsonutils.loads(self._body)
Example #40
0
 def _empty_message_list(self, body):
     self.assertEqual(jsonutils.loads(body[0])['messages'], [])
Example #41
0
def json_decode(binary):
    return jsonutils.loads(binary, 'utf-8')
    def test_list(self):
        arbitrary_number = 644079696574693
        project_id = str(arbitrary_number)
        header = {
            'X-Project-ID': project_id
        }

        # NOTE(kgriffs): It's important that this one sort after the one
        # above. This is in order to prove that bug/1236605 is fixed, and
        # stays fixed!
        alt_project_id = str(arbitrary_number + 1)

        # List empty
        self.simulate_get(self.queue_path, headers=header)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Payload exceeded
        self.simulate_get(self.queue_path, headers=header,
                          query_string='limit=21')
        self.assertEqual(self.srmock.status, falcon.HTTP_400)

        # Create some
        def create_queue(name, project_id, body):
            altheader = {}
            if project_id is not None:
                altheader['X-Project-ID'] = project_id
            uri = self.queue_path + '/' + name
            self.simulate_put(uri, headers=altheader)
            self.simulate_put(uri + '/metadata', headers=altheader, body=body)

        create_queue('g1', None, '{"answer": 42}')
        create_queue('g2', None, '{"answer": 42}')

        create_queue('q1', project_id, '{"node": 31}')
        create_queue('q2', project_id, '{"node": 32}')
        create_queue('q3', project_id, '{"node": 33}')

        create_queue('q3', alt_project_id, '{"alt": 1}')

        # List (global queues)
        result = self.simulate_get(self.queue_path,
                                   query_string='limit=2&detailed=true')

        result_doc = jsonutils.loads(result[0])
        queues = result_doc['queues']
        self.assertEqual(len(queues), 2)

        for queue in queues:
            self.assertEqual(queue['metadata'], {'answer': 42})

        # List (limit)
        result = self.simulate_get(self.queue_path, headers=header,
                                   query_string='limit=2')

        result_doc = jsonutils.loads(result[0])
        self.assertEqual(len(result_doc['queues']), 2)

        # List (no metadata, get all)
        result = self.simulate_get(self.queue_path,
                                   headers=header, query_string='limit=5')

        result_doc = jsonutils.loads(result[0])
        [target, params] = result_doc['links'][0]['href'].split('?')

        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        self.assertEqual(self.srmock.headers_dict['Content-Location'],
                         self.queue_path + '?limit=5')

        # Ensure we didn't pick up the queue from the alt project.
        queues = result_doc['queues']
        self.assertEqual(len(queues), 3)

        for queue in queues:
            self.simulate_get(queue['href'] + '/metadata', headers=header)
            self.assertEqual(self.srmock.status, falcon.HTTP_200)

            altheader = header.copy()
            altheader['X-Project-ID'] = 'imnothere'
            self.simulate_get(queue['href'] + '/metadata', headers=altheader)
            self.assertEqual(self.srmock.status, falcon.HTTP_404)

            self.assertNotIn('metadata', queue)

        # List with metadata
        result = self.simulate_get(self.queue_path, headers=header,
                                   query_string='detailed=true')

        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        result_doc = jsonutils.loads(result[0])
        [target, params] = result_doc['links'][0]['href'].split('?')

        queue = result_doc['queues'][0]
        result = self.simulate_get(queue['href'] + '/metadata', headers=header)
        result_doc = jsonutils.loads(result[0])
        self.assertEqual(result_doc, queue['metadata'])
        self.assertEqual(result_doc, {'node': 31})

        # List tail
        self.simulate_get(target, headers=header, query_string=params)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # List manually-constructed tail
        self.simulate_get(target, headers=header, query_string='marker=zzz')
        self.assertEqual(self.srmock.status, falcon.HTTP_204)
    def test_list(self):
        arbitrary_number = 644079696574693
        project_id = str(arbitrary_number)
        header = {"X-Project-ID": project_id}

        # NOTE(kgriffs): It's important that this one sort after the one
        # above. This is in order to prove that bug/1236605 is fixed, and
        # stays fixed!
        alt_project_id = str(arbitrary_number + 1)

        # List empty
        self.simulate_get(self.queue_path, headers=header)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Payload exceeded
        self.simulate_get(self.queue_path, headers=header, query_string="limit=21")
        self.assertEqual(self.srmock.status, falcon.HTTP_400)

        # Create some
        def create_queue(name, project_id, body):
            altheader = {}
            if project_id is not None:
                altheader["X-Project-ID"] = project_id
            uri = self.queue_path + "/" + name
            self.simulate_put(uri, headers=altheader)
            self.simulate_put(uri + "/metadata", headers=altheader, body=body)

        create_queue("g1", None, '{"answer": 42}')
        create_queue("g2", None, '{"answer": 42}')

        create_queue("q1", project_id, '{"node": 31}')
        create_queue("q2", project_id, '{"node": 32}')
        create_queue("q3", project_id, '{"node": 33}')

        create_queue("q3", alt_project_id, '{"alt": 1}')

        # List (global queues)
        result = self.simulate_get(self.queue_path, query_string="limit=2&detailed=true")

        result_doc = jsonutils.loads(result[0])
        queues = result_doc["queues"]
        self.assertEqual(len(queues), 2)

        for queue in queues:
            self.assertEqual(queue["metadata"], {"answer": 42})

        # List (limit)
        result = self.simulate_get(self.queue_path, headers=header, query_string="limit=2")

        result_doc = jsonutils.loads(result[0])
        self.assertEqual(len(result_doc["queues"]), 2)

        # List (no metadata, get all)
        result = self.simulate_get(self.queue_path, headers=header, query_string="limit=5")

        result_doc = jsonutils.loads(result[0])
        [target, params] = result_doc["links"][0]["href"].split("?")

        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        self.assertEqual(self.srmock.headers_dict["Content-Location"], self.queue_path + "?limit=5")

        # Ensure we didn't pick up the queue from the alt project.
        queues = result_doc["queues"]
        self.assertEqual(len(queues), 3)

        for queue in queues:
            self.simulate_get(queue["href"] + "/metadata", headers=header)
            self.assertEqual(self.srmock.status, falcon.HTTP_200)

            altheader = header.copy()
            altheader["X-Project-ID"] = "imnothere"
            self.simulate_get(queue["href"] + "/metadata", headers=altheader)
            self.assertEqual(self.srmock.status, falcon.HTTP_404)

            self.assertNotIn("metadata", queue)

        # List with metadata
        result = self.simulate_get(self.queue_path, headers=header, query_string="detailed=true")

        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        result_doc = jsonutils.loads(result[0])
        [target, params] = result_doc["links"][0]["href"].split("?")

        queue = result_doc["queues"][0]
        result = self.simulate_get(queue["href"] + "/metadata", headers=header)
        result_doc = jsonutils.loads(result[0])
        self.assertEqual(result_doc, queue["metadata"])
        self.assertEqual(result_doc, {"node": 31})

        # List tail
        self.simulate_get(target, headers=header, query_string=params)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # List manually-constructed tail
        self.simulate_get(target, headers=header, query_string="marker=zzz")
        self.assertEqual(self.srmock.status, falcon.HTTP_204)
Example #44
0
    def test_lifecycle(self):
        doc = '{"ttl": 100, "grace": 60}'

        # First, claim some messages
        body = self.simulate_post(self.claims_path, body=doc,
                                  headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_201)

        claimed = jsonutils.loads(body[0])
        claim_href = self.srmock.headers_dict['Location']
        message_href, params = claimed[0]['href'].split('?')

        # No more messages to claim
        self.simulate_post(self.claims_path, body=doc,
                           query_string='limit=3', headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Listing messages, by default, won't include claimed, will echo
        body = self.simulate_get(self.messages_path,
                                 headers=self.headers,
                                 query_string="echo=true")
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        self._empty_message_list(body)

        # Listing messages, by default, won't include claimed, won't echo
        body = self.simulate_get(self.messages_path,
                                 headers=self.headers,
                                 query_string="echo=false")
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        self._empty_message_list(body)

        # List messages, include_claimed, but don't echo
        body = self.simulate_get(self.messages_path,
                                 query_string='include_claimed=true'
                                              '&echo=false',
                                 headers=self.headers)

        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        self._empty_message_list(body)

        # List messages with a different client-id and echo=false.
        # Should return some messages
        headers = self.headers.copy()
        headers["Client-ID"] = str(uuid.uuid4())
        body = self.simulate_get(self.messages_path,
                                 query_string='include_claimed=true'
                                              '&echo=false',
                                 headers=headers)

        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        # Include claimed messages this time, and echo
        body = self.simulate_get(self.messages_path,
                                 query_string='include_claimed=true'
                                              '&echo=true',
                                 headers=self.headers)
        listed = jsonutils.loads(body[0])
        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        self.assertEqual(len(listed['messages']), len(claimed))

        now = timeutils.utcnow() + datetime.timedelta(seconds=10)
        timeutils_utcnow = 'marconi.openstack.common.timeutils.utcnow'
        with mock.patch(timeutils_utcnow) as mock_utcnow:
            mock_utcnow.return_value = now
            body = self.simulate_get(claim_href, headers=self.headers)

        claim = jsonutils.loads(body[0])

        self.assertEqual(self.srmock.status, falcon.HTTP_200)
        self.assertEqual(self.srmock.headers_dict['Content-Location'],
                         claim_href)
        self.assertEqual(claim['ttl'], 100)
        # NOTE(cpp-cabrera): verify that claim age is non-negative
        self.assertThat(claim['age'], matchers.GreaterThan(-1))

        # Try to delete the message without submitting a claim_id
        self.simulate_delete(message_href, headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_403)

        # Delete the message and its associated claim
        self.simulate_delete(message_href,
                             query_string=params, headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Try to get it from the wrong project
        headers = {
            'Client-ID': str(uuid.uuid4()),
            'X-Project-ID': 'bogusproject'
        }
        self.simulate_get(message_href, query_string=params, headers=headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_404)

        # Get the message
        self.simulate_get(message_href, query_string=params,
                          headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_404)

        # Update the claim
        new_claim_ttl = '{"ttl": 60}'
        creation = timeutils.utcnow()
        self.simulate_patch(claim_href, body=new_claim_ttl,
                            headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Get the claimed messages (again)
        body = self.simulate_get(claim_href, headers=self.headers)
        query = timeutils.utcnow()
        claim = jsonutils.loads(body[0])
        message_href, params = claim['messages'][0]['href'].split('?')

        self.assertEqual(claim['ttl'], 60)
        estimated_age = timeutils.delta_seconds(creation, query)
        self.assertTrue(estimated_age > claim['age'])

        # Delete the claim
        self.simulate_delete(claim['href'], headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_204)

        # Try to delete a message with an invalid claim ID
        self.simulate_delete(message_href,
                             query_string=params, headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_403)

        # Make sure it wasn't deleted!
        self.simulate_get(message_href, query_string=params,
                          headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_200)

        # Try to get a claim that doesn't exist
        self.simulate_get(claim['href'], headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_404)

        # Try to update a claim that doesn't exist
        self.simulate_patch(claim['href'], body=doc,
                            headers=self.headers)
        self.assertEqual(self.srmock.status, falcon.HTTP_404)
Example #45
0
 def test_get_works(self):
     result = self.simulate_get(self.pool)
     self.assertEqual(self.srmock.status, falcon.HTTP_200)
     pool = jsonutils.loads(result[0])
     self._pool_expect(pool, self.pool, self.doc['weight'],
                       self.doc['uri'])