"Fossies" - the Fresh Open Source Software Archive

Member "zaqar-7.0.0/zaqar/tests/unit/transport/websocket/v2/test_claims.py" (30 Aug 2018, 16459 Bytes) of package /linux/misc/openstack/zaqar-7.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. See also the latest Fossies "Diffs" side-by-side code changes report for "test_claims.py": 5.0.0_vs_7.0.0.

    1 # Copyright (c) 2015 Red Hat, Inc.
    2 #
    3 # Licensed under the Apache License, Version 2.0 (the "License"); you may not
    4 # use this file except in compliance with the License.  You may obtain a copy
    5 # 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, WITHOUT
   11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   12 # License for the specific language governing permissions and limitations under
   13 # the License.
   14 
   15 import json
   16 
   17 import ddt
   18 import mock
   19 from oslo_utils import timeutils
   20 from oslo_utils import uuidutils
   21 
   22 from zaqar.common import consts
   23 from zaqar.tests.unit.transport.websocket import base
   24 from zaqar.tests.unit.transport.websocket import utils as test_utils
   25 
   26 
   27 @ddt.ddt
   28 class ClaimsBaseTest(base.V1_1Base):
   29 
   30     config_file = "websocket_mongodb.conf"
   31 
   32     def setUp(self):
   33         super(ClaimsBaseTest, self).setUp()
   34         self.protocol = self.transport.factory()
   35         self.defaults = self.api.get_defaults()
   36 
   37         self.project_id = '7e55e1a7e'
   38         self.headers = {
   39             'Client-ID': uuidutils.generate_uuid(),
   40             'X-Project-ID': self.project_id
   41         }
   42 
   43         action = consts.QUEUE_CREATE
   44         body = {"queue_name": "skittle"}
   45         req = test_utils.create_request(action, body, self.headers)
   46 
   47         with mock.patch.object(self.protocol, 'sendMessage') as msg_mock:
   48             self.protocol.onMessage(req, False)
   49             resp = json.loads(msg_mock.call_args[0][0].decode())
   50             self.assertIn(resp['headers']['status'], [201, 204])
   51 
   52         action = consts.MESSAGE_POST
   53         body = {"queue_name": "skittle",
   54                 "messages": [
   55                     {'body': 239, 'ttl': 300},
   56                     {'body': {'key_1': 'value_1'}, 'ttl': 300},
   57                     {'body': [1, 3], 'ttl': 300},
   58                     {'body': 439, 'ttl': 300},
   59                     {'body': {'key_2': 'value_2'}, 'ttl': 300},
   60                     {'body': ['a', 'b'], 'ttl': 300},
   61                     {'body': 639, 'ttl': 300},
   62                     {'body': {'key_3': 'value_3'}, 'ttl': 300},
   63                     {'body': ["aa", "bb"], 'ttl': 300}]
   64                 }
   65 
   66         send_mock = mock.Mock()
   67         self.protocol.sendMessage = send_mock
   68 
   69         req = test_utils.create_request(action, body, self.headers)
   70 
   71         self.protocol.onMessage(req, False)
   72 
   73         resp = json.loads(send_mock.call_args[0][0].decode())
   74         self.assertEqual(201, resp['headers']['status'])
   75 
   76     def tearDown(self):
   77         super(ClaimsBaseTest, self).tearDown()
   78         action = consts.QUEUE_DELETE
   79         body = {'queue_name': 'skittle'}
   80 
   81         send_mock = mock.Mock()
   82         self.protocol.sendMessage = send_mock
   83 
   84         req = test_utils.create_request(action, body, self.headers)
   85         self.protocol.onMessage(req, False)
   86 
   87         resp = json.loads(send_mock.call_args[0][0].decode())
   88         self.assertEqual(204, resp['headers']['status'])
   89 
   90     @ddt.data('[', '[]', '.', '"fail"')
   91     def test_bad_claim(self, doc):
   92         action = consts.CLAIM_CREATE
   93         body = doc
   94 
   95         send_mock = mock.Mock()
   96         self.protocol.sendMessage = send_mock
   97 
   98         req = test_utils.create_request(action, body, self.headers)
   99         self.protocol.onMessage(req, False)
  100         resp = json.loads(send_mock.call_args[0][0].decode())
  101         self.assertEqual(400, resp['headers']['status'])
  102 
  103         action = consts.CLAIM_UPDATE
  104         body = doc
  105 
  106         req = test_utils.create_request(action, body, self.headers)
  107         self.protocol.onMessage(req, False)
  108         resp = json.loads(send_mock.call_args[0][0].decode())
  109         self.assertEqual(400, resp['headers']['status'])
  110 
  111     def test_exceeded_claim(self):
  112         action = consts.CLAIM_CREATE
  113         body = {"queue_name": "skittle",
  114                 "ttl": 100,
  115                 "grace": 60,
  116                 "limit": 21}
  117 
  118         send_mock = mock.Mock()
  119         self.protocol.sendMessage = send_mock
  120 
  121         req = test_utils.create_request(action, body, self.headers)
  122         self.protocol.onMessage(req, False)
  123         resp = json.loads(send_mock.call_args[0][0].decode())
  124         self.assertEqual(400, resp['headers']['status'])
  125 
  126     @ddt.data((-1, -1), (59, 60), (60, 59), (60, 43201), (43201, 60))
  127     def test_unacceptable_ttl_or_grace(self, ttl_grace):
  128         ttl, grace = ttl_grace
  129         action = consts.CLAIM_CREATE
  130         body = {"queue_name": "skittle",
  131                 "ttl": ttl,
  132                 "grace": grace}
  133 
  134         send_mock = mock.Mock()
  135         self.protocol.sendMessage = send_mock
  136 
  137         req = test_utils.create_request(action, body, self.headers)
  138         self.protocol.onMessage(req, False)
  139         resp = json.loads(send_mock.call_args[0][0].decode())
  140         self.assertEqual(400, resp['headers']['status'])
  141 
  142     @ddt.data(-1, 59, 43201)
  143     def test_unacceptable_new_ttl(self, ttl):
  144         claim = self._get_a_claim()
  145 
  146         action = consts.CLAIM_UPDATE
  147         body = {"queue_name": "skittle",
  148                 "claim_id": claim['body']['claim_id'],
  149                 "ttl": ttl}
  150 
  151         send_mock = mock.Mock()
  152         self.protocol.sendMessage = send_mock
  153 
  154         req = test_utils.create_request(action, body, self.headers)
  155         self.protocol.onMessage(req, False)
  156         resp = json.loads(send_mock.call_args[0][0].decode())
  157         self.assertEqual(400, resp['headers']['status'])
  158 
  159     def test_default_ttl_and_grace(self):
  160         action = consts.CLAIM_CREATE
  161         body = {"queue_name": "skittle"}
  162 
  163         send_mock = mock.Mock()
  164         self.protocol.sendMessage = send_mock
  165 
  166         req = test_utils.create_request(action, body, self.headers)
  167         self.protocol.onMessage(req, False)
  168         resp = json.loads(send_mock.call_args[0][0].decode())
  169         self.assertEqual(201, resp['headers']['status'])
  170 
  171         action = consts.CLAIM_GET
  172         body = {"queue_name": "skittle",
  173                 "claim_id": resp['body']['claim_id']}
  174 
  175         req = test_utils.create_request(action, body, self.headers)
  176         self.protocol.onMessage(req, False)
  177         resp = json.loads(send_mock.call_args[0][0].decode())
  178 
  179         self.assertEqual(200, resp['headers']['status'])
  180         self.assertEqual(self.defaults.claim_ttl, resp['body']['ttl'])
  181 
  182     def test_lifecycle(self):
  183         # First, claim some messages
  184         action = consts.CLAIM_CREATE
  185         body = {"queue_name": "skittle",
  186                 "ttl": 100,
  187                 "grace": 60}
  188 
  189         send_mock = mock.Mock()
  190         self.protocol.sendMessage = send_mock
  191 
  192         req = test_utils.create_request(action, body, self.headers)
  193         self.protocol.onMessage(req, False)
  194         resp = json.loads(send_mock.call_args[0][0].decode())
  195         self.assertEqual(201, resp['headers']['status'])
  196         claimed_messages = resp['body']['messages']
  197         claim_id = resp['body']['claim_id']
  198 
  199         # No more messages to claim
  200         body = {"queue_name": "skittle",
  201                 "ttl": 100,
  202                 "grace": 60}
  203 
  204         req = test_utils.create_request(action, body, self.headers)
  205         self.protocol.onMessage(req, False)
  206         resp = json.loads(send_mock.call_args[0][0].decode())
  207         self.assertEqual(204, resp['headers']['status'])
  208 
  209         # Listing messages, by default, won't include claimed, will echo
  210         action = consts.MESSAGE_LIST
  211         body = {"queue_name": "skittle",
  212                 "echo": True}
  213 
  214         req = test_utils.create_request(action, body, self.headers)
  215         self.protocol.onMessage(req, False)
  216         resp = json.loads(send_mock.call_args[0][0].decode())
  217         self.assertEqual(200, resp['headers']['status'])
  218         self.assertEqual([], resp['body']['messages'])
  219 
  220         # Listing messages, by default, won't include claimed, won't echo
  221 
  222         body = {"queue_name": "skittle",
  223                 "echo": False}
  224 
  225         req = test_utils.create_request(action, body, self.headers)
  226         self.protocol.onMessage(req, False)
  227         resp = json.loads(send_mock.call_args[0][0].decode())
  228         self.assertEqual(200, resp['headers']['status'])
  229         self.assertEqual([], resp['body']['messages'])
  230 
  231         # List messages, include_claimed, but don't echo
  232 
  233         body = {"queue_name": "skittle",
  234                 "include_claimed": True,
  235                 "echo": False}
  236 
  237         req = test_utils.create_request(action, body, self.headers)
  238         self.protocol.onMessage(req, False)
  239         resp = json.loads(send_mock.call_args[0][0].decode())
  240         self.assertEqual(200, resp['headers']['status'])
  241         self.assertEqual(resp['body']['messages'], [])
  242 
  243         # List messages with a different client-id and echo=false.
  244         # Should return some messages
  245 
  246         body = {"queue_name": "skittle",
  247                 "echo": False}
  248 
  249         headers = {
  250             'Client-ID': uuidutils.generate_uuid(),
  251             'X-Project-ID': self.project_id
  252         }
  253 
  254         req = test_utils.create_request(action, body, headers)
  255         self.protocol.onMessage(req, False)
  256         resp = json.loads(send_mock.call_args[0][0].decode())
  257         self.assertEqual(200, resp['headers']['status'])
  258 
  259         # Include claimed messages this time, and echo
  260 
  261         body = {"queue_name": "skittle",
  262                 "include_claimed": True,
  263                 "echo": True}
  264 
  265         req = test_utils.create_request(action, body, self.headers)
  266         self.protocol.onMessage(req, False)
  267         resp = json.loads(send_mock.call_args[0][0].decode())
  268         self.assertEqual(200, resp['headers']['status'])
  269         self.assertEqual(len(claimed_messages), len(resp['body']['messages']))
  270 
  271         message_id_1 = resp['body']['messages'][0]['id']
  272         message_id_2 = resp['body']['messages'][1]['id']
  273 
  274         # Try to delete the message without submitting a claim_id
  275         action = consts.MESSAGE_DELETE
  276         body = {"queue_name": "skittle",
  277                 "message_id": message_id_1}
  278 
  279         req = test_utils.create_request(action, body, self.headers)
  280         self.protocol.onMessage(req, False)
  281         resp = json.loads(send_mock.call_args[0][0].decode())
  282         self.assertEqual(403,  resp['headers']['status'])
  283 
  284         # Delete the message and its associated claim
  285         body = {"queue_name": "skittle",
  286                 "message_id": message_id_1,
  287                 "claim_id": claim_id}
  288 
  289         req = test_utils.create_request(action, body, self.headers)
  290         self.protocol.onMessage(req, False)
  291         resp = json.loads(send_mock.call_args[0][0].decode())
  292         self.assertEqual(204, resp['headers']['status'])
  293 
  294         # Try to get it from the wrong project
  295         headers = {
  296             'Client-ID': uuidutils.generate_uuid(),
  297             'X-Project-ID': 'someproject'
  298         }
  299 
  300         action = consts.MESSAGE_GET
  301         body = {"queue_name": "skittle",
  302                 "message_id": message_id_2}
  303         req = test_utils.create_request(action, body, headers)
  304         self.protocol.onMessage(req, False)
  305         resp = json.loads(send_mock.call_args[0][0].decode())
  306         self.assertEqual(404,  resp['headers']['status'])
  307 
  308         # Get the message
  309         action = consts.MESSAGE_GET
  310         body = {"queue_name": "skittle",
  311                 "message_id": message_id_2}
  312         req = test_utils.create_request(action, body, self.headers)
  313         self.protocol.onMessage(req, False)
  314         resp = json.loads(send_mock.call_args[0][0].decode())
  315         self.assertEqual(200, resp['headers']['status'])
  316 
  317         # Update the claim
  318         creation = timeutils.utcnow()
  319         action = consts.CLAIM_UPDATE
  320         body = {"queue_name": "skittle",
  321                 "ttl": 60,
  322                 "grace": 60,
  323                 "claim_id": claim_id}
  324         req = test_utils.create_request(action, body, self.headers)
  325         self.protocol.onMessage(req, False)
  326         resp = json.loads(send_mock.call_args[0][0].decode())
  327         self.assertEqual(204, resp['headers']['status'])
  328 
  329         # Get the claimed messages (again)
  330         action = consts.CLAIM_GET
  331         body = {"queue_name": "skittle",
  332                 "claim_id": claim_id}
  333         req = test_utils.create_request(action, body, self.headers)
  334         self.protocol.onMessage(req, False)
  335         query = timeutils.utcnow()
  336         resp = json.loads(send_mock.call_args[0][0].decode())
  337         self.assertEqual(200, resp['headers']['status'])
  338         self.assertEqual(60, resp['body']['ttl'])
  339 
  340         message_id_3 = resp['body']['messages'][0]['id']
  341 
  342         estimated_age = timeutils.delta_seconds(creation, query)
  343         # The claim's age should be 0 at this moment. But in some unexpected
  344         # case, such as slow test, the age maybe larger than 0. Just skip
  345         # asserting if so.
  346         if resp['body']['age'] == 0:
  347             self.assertGreater(estimated_age, resp['body']['age'])
  348 
  349         # Delete the claim
  350         action = consts.CLAIM_DELETE
  351         body = {"queue_name": "skittle",
  352                 "claim_id": claim_id}
  353         req = test_utils.create_request(action, body, self.headers)
  354         self.protocol.onMessage(req, False)
  355         resp = json.loads(send_mock.call_args[0][0].decode())
  356         self.assertEqual(204, resp['headers']['status'])
  357 
  358         # Try to delete a message with an invalid claim ID
  359         action = consts.MESSAGE_DELETE
  360         body = {"queue_name": "skittle",
  361                 "message_id": message_id_3,
  362                 "claim_id": claim_id}
  363 
  364         req = test_utils.create_request(action, body, self.headers)
  365         self.protocol.onMessage(req, False)
  366         resp = json.loads(send_mock.call_args[0][0].decode())
  367         self.assertEqual(400, resp['headers']['status'])
  368 
  369         # Make sure it wasn't deleted!
  370         action = consts.MESSAGE_GET
  371         body = {"queue_name": "skittle",
  372                 "message_id": message_id_2}
  373         req = test_utils.create_request(action, body, self.headers)
  374         self.protocol.onMessage(req, False)
  375         resp = json.loads(send_mock.call_args[0][0].decode())
  376         self.assertEqual(200, resp['headers']['status'])
  377 
  378         # Try to get a claim that doesn't exist
  379         action = consts.CLAIM_GET
  380         body = {"queue_name": "skittle",
  381                 "claim_id": claim_id}
  382         req = test_utils.create_request(action, body, self.headers)
  383         self.protocol.onMessage(req, False)
  384         resp = json.loads(send_mock.call_args[0][0].decode())
  385         self.assertEqual(404,  resp['headers']['status'])
  386 
  387         # Try to update a claim that doesn't exist
  388         action = consts.CLAIM_UPDATE
  389         body = {"queue_name": "skittle",
  390                 "ttl": 60,
  391                 "grace": 60,
  392                 "claim_id": claim_id}
  393         req = test_utils.create_request(action, body, self.headers)
  394         self.protocol.onMessage(req, False)
  395         resp = json.loads(send_mock.call_args[0][0].decode())
  396         self.assertEqual(404,  resp['headers']['status'])
  397 
  398     def test_post_claim_nonexistent_queue(self):
  399         action = consts.CLAIM_CREATE
  400         body = {"queue_name": "nonexistent",
  401                 "ttl": 100,
  402                 "grace": 60}
  403 
  404         send_mock = mock.Mock()
  405         self.protocol.sendMessage = send_mock
  406 
  407         req = test_utils.create_request(action, body, self.headers)
  408         self.protocol.onMessage(req, False)
  409         resp = json.loads(send_mock.call_args[0][0].decode())
  410         self.assertEqual(204, resp['headers']['status'])
  411 
  412     def test_get_claim_nonexistent_queue(self):
  413         action = consts.CLAIM_GET
  414         body = {"queue_name": "nonexistent",
  415                 "claim_id": "aaabbbba"}
  416 
  417         send_mock = mock.Mock()
  418         self.protocol.sendMessage = send_mock
  419 
  420         req = test_utils.create_request(action, body, self.headers)
  421         self.protocol.onMessage(req, False)
  422         resp = json.loads(send_mock.call_args[0][0].decode())
  423         self.assertEqual(404,  resp['headers']['status'])
  424 
  425     def _get_a_claim(self):
  426         action = consts.CLAIM_CREATE
  427         body = {"queue_name": "skittle",
  428                 "ttl": 100,
  429                 "grace": 60}
  430 
  431         send_mock = mock.Mock()
  432         self.protocol.sendMessage = send_mock
  433 
  434         req = test_utils.create_request(action, body, self.headers)
  435         self.protocol.onMessage(req, False)
  436         resp = json.loads(send_mock.call_args[0][0].decode())
  437         self.assertEqual(201, resp['headers']['status'])
  438 
  439         return resp