"Fossies" - the Fresh Open Source Software Archive

Member "zaqar-9.0.0/zaqar/tests/unit/transport/wsgi/v1_1/test_claims.py" (16 Oct 2019, 12566 Bytes) of package /linux/misc/openstack/zaqar-9.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # Copyright (c) 2013 Rackspace, Inc.
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License");
    4 # you may not use this file except in compliance with the License.
    5 # You may obtain a copy of the License at
    6 #
    7 #    http://www.apache.org/licenses/LICENSE-2.0
    8 #
    9 # Unless required by applicable law or agreed to in writing, software
   10 # distributed under the License is distributed on an "AS IS" BASIS,
   11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
   12 # implied.
   13 # See the License for the specific language governing permissions and
   14 # limitations under the License.
   15 
   16 import datetime
   17 import json
   18 
   19 import ddt
   20 import falcon
   21 import mock
   22 from oslo_serialization import jsonutils
   23 from oslo_utils import timeutils
   24 from oslo_utils import uuidutils
   25 from testtools import matchers
   26 
   27 from zaqar import tests as testing
   28 from zaqar.tests.unit.transport.wsgi import base
   29 
   30 
   31 @ddt.ddt
   32 class TestClaimsMongoDB(base.V1_1Base):
   33 
   34     config_file = 'wsgi_mongodb.conf'
   35 
   36     @testing.requires_mongodb
   37     def setUp(self):
   38         super(TestClaimsMongoDB, self).setUp()
   39 
   40         self.default_claim_ttl = self.boot.transport._defaults.claim_ttl
   41         self.project_id = '737_abc8332832'
   42         self.headers = {
   43             'Client-ID': uuidutils.generate_uuid(),
   44             'X-Project-ID': self.project_id
   45         }
   46         self.queue_path = self.url_prefix + '/queues/fizbit'
   47         self.claims_path = self.queue_path + '/claims'
   48         self.messages_path = self.queue_path + '/messages'
   49 
   50         doc = json.dumps({"_ttl": 60})
   51 
   52         self.simulate_put(self.queue_path, body=doc, headers=self.headers)
   53         self.assertEqual(falcon.HTTP_201, self.srmock.status)
   54 
   55         doc = json.dumps({'messages': [{'body': 239, 'ttl': 300}] * 10})
   56         self.simulate_post(self.queue_path + '/messages',
   57                            body=doc, headers=self.headers)
   58         self.assertEqual(falcon.HTTP_201, self.srmock.status)
   59 
   60     def tearDown(self):
   61         storage = self.boot.storage._storage
   62         control = self.boot.control
   63         connection = storage.connection
   64 
   65         connection.drop_database(control.queues_database)
   66 
   67         for db in storage.message_databases:
   68             connection.drop_database(db)
   69         self.simulate_delete(self.queue_path, headers=self.headers)
   70 
   71         super(TestClaimsMongoDB, self).tearDown()
   72 
   73     @ddt.data('[', '[]', '.', '"fail"')
   74     def test_bad_claim(self, doc):
   75         self.simulate_post(self.claims_path, body=doc, headers=self.headers)
   76         self.assertEqual(falcon.HTTP_400, self.srmock.status)
   77 
   78         href = self._get_a_claim()
   79 
   80         self.simulate_patch(href, body=doc, headers=self.headers)
   81         self.assertEqual(falcon.HTTP_400, self.srmock.status)
   82 
   83     def test_exceeded_claim(self):
   84         self.simulate_post(self.claims_path,
   85                            body='{"ttl": 100, "grace": 60}',
   86                            query_string='limit=21', headers=self.headers)
   87 
   88         self.assertEqual(falcon.HTTP_400, self.srmock.status)
   89 
   90     @ddt.data((-1, -1), (59, 60), (60, 59), (60, 43201), (43201, 60))
   91     def test_unacceptable_ttl_or_grace(self, ttl_grace):
   92         ttl, grace = ttl_grace
   93         self.simulate_post(self.claims_path,
   94                            body=json.dumps({'ttl': ttl, 'grace': grace}),
   95                            headers=self.headers)
   96 
   97         self.assertEqual(falcon.HTTP_400, self.srmock.status)
   98 
   99     @ddt.data(-1, 59, 43201)
  100     def test_unacceptable_new_ttl(self, ttl):
  101         href = self._get_a_claim()
  102 
  103         self.simulate_patch(href,
  104                             body=json.dumps({'ttl': ttl}),
  105                             headers=self.headers)
  106 
  107         self.assertEqual(falcon.HTTP_400, self.srmock.status)
  108 
  109     def test_default_ttl_and_grace(self):
  110         self.simulate_post(self.claims_path,
  111                            body='{}', headers=self.headers)
  112 
  113         self.assertEqual(falcon.HTTP_201, self.srmock.status)
  114 
  115         body = self.simulate_get(self.srmock.headers_dict['location'],
  116                                  headers=self.headers)
  117 
  118         claim = jsonutils.loads(body[0])
  119         self.assertEqual(self.default_claim_ttl, claim['ttl'])
  120 
  121     def _get_a_claim(self):
  122         doc = '{"ttl": 100, "grace": 60}'
  123         self.simulate_post(self.claims_path, body=doc, headers=self.headers)
  124         return self.srmock.headers_dict['Location']
  125 
  126     def test_lifecycle(self):
  127         doc = '{"ttl": 100, "grace": 60}'
  128 
  129         # First, claim some messages
  130         body = self.simulate_post(self.claims_path, body=doc,
  131                                   headers=self.headers)
  132         self.assertEqual(falcon.HTTP_201, self.srmock.status)
  133 
  134         claimed = jsonutils.loads(body[0])['messages']
  135         claim_href = self.srmock.headers_dict['Location']
  136         message_href, params = claimed[0]['href'].split('?')
  137 
  138         # No more messages to claim
  139         self.simulate_post(self.claims_path, body=doc,
  140                            query_string='limit=3', headers=self.headers)
  141         self.assertEqual(falcon.HTTP_204, self.srmock.status)
  142 
  143         # Listing messages, by default, won't include claimed, will echo
  144         body = self.simulate_get(self.messages_path,
  145                                  headers=self.headers,
  146                                  query_string="echo=true")
  147         self.assertEqual(falcon.HTTP_200, self.srmock.status)
  148         self._empty_message_list(body)
  149 
  150         # Listing messages, by default, won't include claimed, won't echo
  151         body = self.simulate_get(self.messages_path,
  152                                  headers=self.headers,
  153                                  query_string="echo=false")
  154         self.assertEqual(falcon.HTTP_200, self.srmock.status)
  155         self._empty_message_list(body)
  156 
  157         # List messages, include_claimed, but don't echo
  158         body = self.simulate_get(self.messages_path,
  159                                  query_string='include_claimed=true'
  160                                               '&echo=false',
  161                                  headers=self.headers)
  162 
  163         self.assertEqual(falcon.HTTP_200, self.srmock.status)
  164         self._empty_message_list(body)
  165 
  166         # List messages with a different client-id and echo=false.
  167         # Should return some messages
  168         headers = self.headers.copy()
  169         headers["Client-ID"] = uuidutils.generate_uuid()
  170         body = self.simulate_get(self.messages_path,
  171                                  query_string='include_claimed=true'
  172                                               '&echo=false',
  173                                  headers=headers)
  174 
  175         self.assertEqual(falcon.HTTP_200, self.srmock.status)
  176 
  177         # Include claimed messages this time, and echo
  178         body = self.simulate_get(self.messages_path,
  179                                  query_string='include_claimed=true'
  180                                               '&echo=true',
  181                                  headers=self.headers)
  182         listed = jsonutils.loads(body[0])
  183         self.assertEqual(falcon.HTTP_200, self.srmock.status)
  184         self.assertEqual(len(claimed), len(listed['messages']))
  185 
  186         now = timeutils.utcnow() + datetime.timedelta(seconds=10)
  187         timeutils_utcnow = 'oslo_utils.timeutils.utcnow'
  188         with mock.patch(timeutils_utcnow) as mock_utcnow:
  189             mock_utcnow.return_value = now
  190             body = self.simulate_get(claim_href, headers=self.headers)
  191 
  192         claim = jsonutils.loads(body[0])
  193 
  194         self.assertEqual(falcon.HTTP_200, self.srmock.status)
  195         self.assertEqual(100, claim['ttl'])
  196         # NOTE(cpp-cabrera): verify that claim age is non-negative
  197         self.assertThat(claim['age'], matchers.GreaterThan(-1))
  198 
  199         # Try to delete the message without submitting a claim_id
  200         self.simulate_delete(message_href, headers=self.headers)
  201         self.assertEqual(falcon.HTTP_403, self.srmock.status)
  202 
  203         # Delete the message and its associated claim
  204         self.simulate_delete(message_href,
  205                              query_string=params, headers=self.headers)
  206         self.assertEqual(falcon.HTTP_204, self.srmock.status)
  207 
  208         # Try to get it from the wrong project
  209         headers = {
  210             'Client-ID': uuidutils.generate_uuid(),
  211             'X-Project-ID': 'bogusproject'
  212         }
  213         self.simulate_get(message_href, query_string=params, headers=headers)
  214         self.assertEqual(falcon.HTTP_404, self.srmock.status)
  215 
  216         # Get the message
  217         self.simulate_get(message_href, query_string=params,
  218                           headers=self.headers)
  219         self.assertEqual(falcon.HTTP_404, self.srmock.status)
  220 
  221         # Update the claim
  222         new_claim_ttl = '{"ttl": 60, "grace": 60}'
  223         creation = timeutils.utcnow()
  224         self.simulate_patch(claim_href, body=new_claim_ttl,
  225                             headers=self.headers)
  226         self.assertEqual(falcon.HTTP_204, self.srmock.status)
  227 
  228         # Get the claimed messages (again)
  229         body = self.simulate_get(claim_href, headers=self.headers)
  230         query = timeutils.utcnow()
  231         claim = jsonutils.loads(body[0])
  232         message_href, params = claim['messages'][0]['href'].split('?')
  233 
  234         self.assertEqual(60, claim['ttl'])
  235         estimated_age = timeutils.delta_seconds(creation, query)
  236         self.assertGreater(estimated_age, claim['age'])
  237 
  238         # Delete the claim
  239         self.simulate_delete(claim['href'], headers=self.headers)
  240         self.assertEqual(falcon.HTTP_204, self.srmock.status)
  241 
  242         # Try to delete a message with an invalid claim ID
  243         self.simulate_delete(message_href,
  244                              query_string=params, headers=self.headers)
  245         self.assertEqual(falcon.HTTP_400, self.srmock.status)
  246 
  247         # Make sure it wasn't deleted!
  248         self.simulate_get(message_href, query_string=params,
  249                           headers=self.headers)
  250         self.assertEqual(falcon.HTTP_200, self.srmock.status)
  251 
  252         # Try to get a claim that doesn't exist
  253         self.simulate_get(claim['href'], headers=self.headers)
  254         self.assertEqual(falcon.HTTP_404, self.srmock.status)
  255 
  256         # Try to update a claim that doesn't exist
  257         self.simulate_patch(claim['href'], body=doc,
  258                             headers=self.headers)
  259         self.assertEqual(falcon.HTTP_404, self.srmock.status)
  260 
  261     def test_post_claim_nonexistent_queue(self):
  262         path = self.url_prefix + '/queues/nonexistent/claims'
  263         self.simulate_post(path,
  264                            body='{"ttl": 100, "grace": 60}',
  265                            headers=self.headers)
  266         self.assertEqual(falcon.HTTP_204, self.srmock.status)
  267 
  268     def test_get_claim_nonexistent_queue(self):
  269         path = self.url_prefix + '/queues/nonexistent/claims/aaabbbba'
  270         self.simulate_get(path, headers=self.headers)
  271         self.assertEqual(falcon.HTTP_404, self.srmock.status)
  272 
  273     # NOTE(cpp-cabrera): regression test against bug #1203842
  274     def test_get_nonexistent_claim_404s(self):
  275         self.simulate_get(self.claims_path + '/a', headers=self.headers)
  276         self.assertEqual(falcon.HTTP_404, self.srmock.status)
  277 
  278     def test_delete_nonexistent_claim_204s(self):
  279         self.simulate_delete(self.claims_path + '/a',
  280                              headers=self.headers)
  281         self.assertEqual(falcon.HTTP_204, self.srmock.status)
  282 
  283     def test_patch_nonexistent_claim_404s(self):
  284         patch_data = json.dumps({'ttl': 100})
  285         self.simulate_patch(self.claims_path + '/a', body=patch_data,
  286                             headers=self.headers)
  287         self.assertEqual(falcon.HTTP_404, self.srmock.status)
  288 
  289 
  290 class TestClaimsFaultyDriver(base.V1_1BaseFaulty):
  291 
  292     config_file = 'wsgi_faulty.conf'
  293 
  294     def test_simple(self):
  295         self.project_id = '480924abc_'
  296         self.headers = {
  297             'Client-ID': uuidutils.generate_uuid(),
  298             'X-Project-ID': self.project_id
  299         }
  300 
  301         claims_path = self.url_prefix + '/queues/fizbit/claims'
  302         doc = '{"ttl": 100, "grace": 60}'
  303 
  304         self.simulate_post(claims_path, body=doc, headers=self.headers)
  305         self.assertEqual(falcon.HTTP_503, self.srmock.status)
  306 
  307         self.simulate_get(claims_path + '/nichts', headers=self.headers)
  308         self.assertEqual(falcon.HTTP_503, self.srmock.status)
  309 
  310         self.simulate_patch(claims_path + '/nichts', body=doc,
  311                             headers=self.headers)
  312         self.assertEqual(falcon.HTTP_503, self.srmock.status)
  313 
  314         self.simulate_delete(claims_path + '/foo', headers=self.headers)
  315         self.assertEqual(falcon.HTTP_503, self.srmock.status)