test_boto_dynamodb.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Libs
  8. import salt.states.boto_dynamodb as boto_dynamodb
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.mock import MagicMock, patch
  12. from tests.support.unit import TestCase
  13. class BotoDynamodbTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.boto_dynamodb
  16. """
  17. def setup_loader_modules(self):
  18. return {boto_dynamodb: {}}
  19. # 'present' function tests: 1
  20. def test_present(self):
  21. """
  22. Test to ensure the DynamoDB table exists.
  23. """
  24. name = "new_table"
  25. ret = {"name": name, "result": True, "changes": {}, "comment": ""}
  26. exists_mock = MagicMock(side_effect=[True, False, False])
  27. dict_mock = MagicMock(return_value={})
  28. mock_bool = MagicMock(return_value=True)
  29. pillar_mock = MagicMock(return_value=[])
  30. with patch.dict(
  31. boto_dynamodb.__salt__,
  32. {
  33. "boto_dynamodb.exists": exists_mock,
  34. "boto_dynamodb.describe": dict_mock,
  35. "config.option": dict_mock,
  36. "pillar.get": pillar_mock,
  37. "boto_dynamodb.create_table": mock_bool,
  38. },
  39. ):
  40. comt = (
  41. "DynamoDB table {0} exists,\n"
  42. "DynamoDB table {0} throughput matches,\n"
  43. "All global secondary indexes match,\n".format(name)
  44. )
  45. ret.update({"comment": comt})
  46. self.assertDictEqual(boto_dynamodb.present(name), ret)
  47. with patch.dict(boto_dynamodb.__opts__, {"test": True}):
  48. comt = "DynamoDB table {0} would be created.".format(name)
  49. ret.update({"comment": comt, "result": None})
  50. self.assertDictEqual(boto_dynamodb.present(name), ret)
  51. changes = {
  52. "new": {
  53. "global_indexes": None,
  54. "hash_key": None,
  55. "hash_key_data_type": None,
  56. "local_indexes": None,
  57. "range_key": None,
  58. "range_key_data_type": None,
  59. "read_capacity_units": None,
  60. "table": "new_table",
  61. "write_capacity_units": None,
  62. }
  63. }
  64. with patch.dict(boto_dynamodb.__opts__, {"test": False}):
  65. comt = (
  66. "DynamoDB table {0} was successfully created,\n"
  67. "DynamoDB table new_table throughput matches,\n".format(name)
  68. )
  69. ret.update({"comment": comt, "result": True, "changes": changes})
  70. self.assertDictEqual(ret, boto_dynamodb.present(name))
  71. # 'absent' function tests: 1
  72. def test_absent(self):
  73. """
  74. Test to ensure the DynamoDB table does not exist.
  75. """
  76. name = "new_table"
  77. ret = {"name": name, "result": True, "changes": {}, "comment": ""}
  78. mock = MagicMock(side_effect=[False, True, True])
  79. mock_bool = MagicMock(return_value=True)
  80. with patch.dict(
  81. boto_dynamodb.__salt__,
  82. {"boto_dynamodb.exists": mock, "boto_dynamodb.delete": mock_bool},
  83. ):
  84. comt = "DynamoDB table {0} does not exist".format(name)
  85. ret.update({"comment": comt})
  86. self.assertDictEqual(boto_dynamodb.absent(name), ret)
  87. with patch.dict(boto_dynamodb.__opts__, {"test": True}):
  88. comt = "DynamoDB table {0} is set to be deleted".format(name)
  89. ret.update({"comment": comt, "result": None})
  90. self.assertDictEqual(boto_dynamodb.absent(name), ret)
  91. changes = {
  92. "new": "Table new_table deleted",
  93. "old": "Table new_table exists",
  94. }
  95. with patch.dict(boto_dynamodb.__opts__, {"test": False}):
  96. comt = "Deleted DynamoDB table {0}".format(name)
  97. ret.update({"comment": comt, "result": True, "changes": changes})
  98. self.assertDictEqual(boto_dynamodb.absent(name), ret)