PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/boto-2.5.2/tests/integration/dynamodb/test_layer1.py

#
Python | 222 lines | 195 code | 3 blank | 24 comment | 0 complexity | 563d6881cc666ced6a2223888857a63a MD5 | raw file
  1. # Copyright (c) 2012 Mitch Garnaat http://garnaat.org/
  2. # All rights reserved.
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a
  5. # copy of this software and associated documentation files (the
  6. # "Software"), to deal in the Software without restriction, including
  7. # without limitation the rights to use, copy, modify, merge, publish, dis-
  8. # tribute, sublicense, and/or sell copies of the Software, and to permit
  9. # persons to whom the Software is furnished to do so, subject to the fol-
  10. # lowing conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included
  13. # in all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
  17. # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  18. # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. # IN THE SOFTWARE.
  22. """
  23. Tests for Layer1 of DynamoDB
  24. """
  25. import unittest
  26. import time
  27. from boto.dynamodb.exceptions import DynamoDBKeyNotFoundError
  28. from boto.dynamodb.exceptions import DynamoDBConditionalCheckFailedError
  29. from boto.dynamodb.exceptions import DynamoDBValidationError
  30. from boto.dynamodb.layer1 import Layer1
  31. from boto.sts.credentials import Credentials
  32. json_doc = """{"access_key": "ASIAIV7R2NUUJ6SB7GKQ", "secret_key": "eIfijGxJlejHDSQiaGr6b7U805U0GKWmllCTt2ZM", "request_id": "28c17897-4555-11e1-8bb1-2529f165f2f0", "expiration": "2012-01-23T00:59:45.617Z", "session_token": "AQoDYXdzEPn//////////wEasAGDXeGY8bx36NLRSA1v3dy2x00k3FNA2KVsMEXkQuKY08gPTtYs2tefZTBsTjgjC+O6j8ieoB1on2bPyCq872+Yq3cipls8jna+PNSEcsXtC8CJBKai/FfYNg1XUHam6EUCtRiUHvqztOVgaGqUUS1UbrBKB7kKSXzgKrJ9AT0bvqi4hZS0ayaU8969f2HIbN9psXhRBKpJyB9FUPuVYpYYZsz9NY3y2kGtK+dgfrKvxyDxxfL4BA=="}"""
  33. class DynamoDBLayer1Test (unittest.TestCase):
  34. dynamodb = True
  35. def test_layer1_basic(self):
  36. print '--- running DynamoDB Layer1 tests ---'
  37. # Create a Layer1 connection with an expired set of
  38. # credentials to test the automatic renewal of tokens
  39. bad_creds = Credentials.from_json(json_doc)
  40. c = Layer1(session_token=bad_creds)
  41. # First create a table
  42. table_name = 'test-%d' % int(time.time())
  43. hash_key_name = 'forum_name'
  44. hash_key_type = 'S'
  45. range_key_name = 'subject'
  46. range_key_type = 'S'
  47. read_units = 5
  48. write_units = 5
  49. schema = {'HashKeyElement': {'AttributeName': hash_key_name,
  50. 'AttributeType': hash_key_type},
  51. 'RangeKeyElement': {'AttributeName': range_key_name,
  52. 'AttributeType': range_key_type}}
  53. provisioned_throughput = {'ReadCapacityUnits': read_units,
  54. 'WriteCapacityUnits': write_units}
  55. result = c.create_table(table_name, schema, provisioned_throughput)
  56. assert result['TableDescription']['TableName'] == table_name
  57. result_schema = result['TableDescription']['KeySchema']
  58. assert result_schema['HashKeyElement']['AttributeName'] == hash_key_name
  59. assert result_schema['HashKeyElement']['AttributeType'] == hash_key_type
  60. assert result_schema['RangeKeyElement']['AttributeName'] == range_key_name
  61. assert result_schema['RangeKeyElement']['AttributeType'] == range_key_type
  62. result_thruput = result['TableDescription']['ProvisionedThroughput']
  63. assert result_thruput['ReadCapacityUnits'] == read_units
  64. assert result_thruput['WriteCapacityUnits'] == write_units
  65. # Wait for table to become active
  66. result = c.describe_table(table_name)
  67. while result['Table']['TableStatus'] != 'ACTIVE':
  68. time.sleep(5)
  69. result = c.describe_table(table_name)
  70. # List tables and make sure new one is there
  71. result = c.list_tables()
  72. assert table_name in result['TableNames']
  73. # Update the tables ProvisionedThroughput
  74. new_read_units = 10
  75. new_write_units = 5
  76. new_provisioned_throughput = {'ReadCapacityUnits': new_read_units,
  77. 'WriteCapacityUnits': new_write_units}
  78. result = c.update_table(table_name, new_provisioned_throughput)
  79. # Wait for table to be updated
  80. result = c.describe_table(table_name)
  81. while result['Table']['TableStatus'] == 'UPDATING':
  82. time.sleep(5)
  83. result = c.describe_table(table_name)
  84. result_thruput = result['Table']['ProvisionedThroughput']
  85. assert result_thruput['ReadCapacityUnits'] == new_read_units
  86. assert result_thruput['WriteCapacityUnits'] == new_write_units
  87. # Put an item
  88. item1_key = 'Amazon DynamoDB'
  89. item1_range = 'DynamoDB Thread 1'
  90. item1_data = {
  91. hash_key_name: {hash_key_type: item1_key},
  92. range_key_name: {range_key_type: item1_range},
  93. 'Message': {'S': 'DynamoDB thread 1 message text'},
  94. 'LastPostedBy': {'S': 'User A'},
  95. 'Views': {'N': '0'},
  96. 'Replies': {'N': '0'},
  97. 'Answered': {'N': '0'},
  98. 'Tags': {'SS': ["index", "primarykey", "table"]},
  99. 'LastPostDateTime': {'S': '12/9/2011 11:36:03 PM'}
  100. }
  101. result = c.put_item(table_name, item1_data)
  102. # Now do a consistent read and check results
  103. key1 = {'HashKeyElement': {hash_key_type: item1_key},
  104. 'RangeKeyElement': {range_key_type: item1_range}}
  105. result = c.get_item(table_name, key=key1, consistent_read=True)
  106. for name in item1_data:
  107. assert name in result['Item']
  108. # Try to get an item that does not exist.
  109. invalid_key = {'HashKeyElement': {hash_key_type: 'bogus_key'},
  110. 'RangeKeyElement': {range_key_type: item1_range}}
  111. self.assertRaises(DynamoDBKeyNotFoundError,
  112. c.get_item, table_name, key=invalid_key)
  113. # Try retrieving only select attributes
  114. attributes = ['Message', 'Views']
  115. result = c.get_item(table_name, key=key1, consistent_read=True,
  116. attributes_to_get=attributes)
  117. for name in result['Item']:
  118. assert name in attributes
  119. # Try to delete the item with the wrong Expected value
  120. expected = {'Views': {'Value': {'N': '1'}}}
  121. self.assertRaises(DynamoDBConditionalCheckFailedError,
  122. c.delete_item, table_name, key=key1,
  123. expected=expected)
  124. # Now update the existing object
  125. attribute_updates = {'Views': {'Value': {'N': '5'},
  126. 'Action': 'PUT'},
  127. 'Tags': {'Value': {'SS': ['foobar']},
  128. 'Action': 'ADD'}}
  129. result = c.update_item(table_name, key=key1,
  130. attribute_updates=attribute_updates)
  131. # Try and update an item, in a fashion which makes it too large.
  132. # The new message text is the item size limit minus 32 bytes and
  133. # the current object is larger than 32 bytes.
  134. item_size_overflow_text = 'Text to be padded'.zfill(64*1024-32)
  135. attribute_updates = {'Message': {'Value': {'S': item_size_overflow_text},
  136. 'Action': 'PUT'}}
  137. self.assertRaises(DynamoDBValidationError,
  138. c.update_item, table_name, key=key1,
  139. attribute_updates=attribute_updates)
  140. # Put a few more items into the table
  141. item2_key = 'Amazon DynamoDB'
  142. item2_range = 'DynamoDB Thread 2'
  143. item2_data = {
  144. hash_key_name: {hash_key_type: item2_key},
  145. range_key_name: {range_key_type: item2_range},
  146. 'Message': {'S': 'DynamoDB thread 2 message text'},
  147. 'LastPostedBy': {'S': 'User A'},
  148. 'Views': {'N': '0'},
  149. 'Replies': {'N': '0'},
  150. 'Answered': {'N': '0'},
  151. 'Tags': {'SS': ["index", "primarykey", "table"]},
  152. 'LastPostDateTime': {'S': '12/9/2011 11:36:03 PM'}
  153. }
  154. result = c.put_item(table_name, item2_data)
  155. key2 = {'HashKeyElement': {hash_key_type: item2_key},
  156. 'RangeKeyElement': {range_key_type: item2_range}}
  157. item3_key = 'Amazon S3'
  158. item3_range = 'S3 Thread 1'
  159. item3_data = {
  160. hash_key_name: {hash_key_type: item3_key},
  161. range_key_name: {range_key_type: item3_range},
  162. 'Message': {'S': 'S3 Thread 1 message text'},
  163. 'LastPostedBy': {'S': 'User A'},
  164. 'Views': {'N': '0'},
  165. 'Replies': {'N': '0'},
  166. 'Answered': {'N': '0'},
  167. 'Tags': {'SS': ['largeobject', 'multipart upload']},
  168. 'LastPostDateTime': {'S': '12/9/2011 11:36:03 PM'}
  169. }
  170. result = c.put_item(table_name, item3_data)
  171. key3 = {'HashKeyElement': {hash_key_type: item3_key},
  172. 'RangeKeyElement': {range_key_type: item3_range}}
  173. # Try a few queries
  174. result = c.query(table_name, {'S': 'Amazon DynamoDB'},
  175. {'AttributeValueList': [{'S': 'DynamoDB'}],
  176. 'ComparisonOperator': 'BEGINS_WITH'})
  177. assert 'Count' in result
  178. assert result['Count'] == 2
  179. # Try a few scans
  180. result = c.scan(table_name,
  181. {'Tags': {'AttributeValueList':[{'S': 'table'}],
  182. 'ComparisonOperator': 'CONTAINS'}})
  183. assert 'Count' in result
  184. assert result['Count'] == 2
  185. # Now delete the items
  186. result = c.delete_item(table_name, key=key1)
  187. result = c.delete_item(table_name, key=key2)
  188. result = c.delete_item(table_name, key=key3)
  189. # Now delete the table
  190. result = c.delete_table(table_name)
  191. assert result['TableDescription']['TableStatus'] == 'DELETING'
  192. print '--- tests completed ---'