test_boto_elasticache.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_elasticache as boto_elasticache
  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 BotoElasticacheTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.boto_elasticache
  16. """
  17. def setup_loader_modules(self):
  18. return {boto_elasticache: {}}
  19. # 'present' function tests: 1
  20. def test_present(self):
  21. """
  22. Test to ensure the cache cluster exists.
  23. """
  24. name = "myelasticache"
  25. engine = "redis"
  26. cache_node_type = "cache.t1.micro"
  27. ret = {"name": name, "result": None, "changes": {}, "comment": ""}
  28. mock = MagicMock(side_effect=[None, False, False, True])
  29. mock_bool = MagicMock(return_value=False)
  30. with patch.dict(
  31. boto_elasticache.__salt__,
  32. {"boto_elasticache.get_config": mock, "boto_elasticache.create": mock_bool},
  33. ):
  34. comt = "Failed to retrieve cache cluster info from AWS."
  35. ret.update({"comment": comt})
  36. self.assertDictEqual(
  37. boto_elasticache.present(name, engine, cache_node_type), ret
  38. )
  39. with patch.dict(boto_elasticache.__opts__, {"test": True}):
  40. comt = "Cache cluster {0} is set to be created.".format(name)
  41. ret.update({"comment": comt})
  42. self.assertDictEqual(
  43. boto_elasticache.present(name, engine, cache_node_type), ret
  44. )
  45. with patch.dict(boto_elasticache.__opts__, {"test": False}):
  46. comt = "Failed to create {0} cache cluster.".format(name)
  47. ret.update({"comment": comt, "result": False})
  48. self.assertDictEqual(
  49. boto_elasticache.present(name, engine, cache_node_type), ret
  50. )
  51. comt = "Cache cluster {0} is present.".format(name)
  52. ret.update({"comment": comt, "result": True})
  53. self.assertDictEqual(
  54. boto_elasticache.present(name, engine, cache_node_type), ret
  55. )
  56. # 'absent' function tests: 1
  57. def test_absent(self):
  58. """
  59. Test to ensure the named elasticache cluster is deleted.
  60. """
  61. name = "new_table"
  62. ret = {"name": name, "result": True, "changes": {}, "comment": ""}
  63. mock = MagicMock(side_effect=[False, True])
  64. with patch.dict(boto_elasticache.__salt__, {"boto_elasticache.exists": mock}):
  65. comt = "{0} does not exist in None.".format(name)
  66. ret.update({"comment": comt})
  67. self.assertDictEqual(boto_elasticache.absent(name), ret)
  68. with patch.dict(boto_elasticache.__opts__, {"test": True}):
  69. comt = "Cache cluster {0} is set to be removed.".format(name)
  70. ret.update({"comment": comt, "result": None})
  71. self.assertDictEqual(boto_elasticache.absent(name), ret)
  72. # 'creategroup' function tests: 1
  73. def test_creategroup(self):
  74. """
  75. Test to ensure the a replication group is create.
  76. """
  77. name = "new_table"
  78. primary_cluster_id = "A"
  79. replication_group_description = "my description"
  80. ret = {"name": name, "result": True, "changes": {}, "comment": ""}
  81. mock = MagicMock(return_value=True)
  82. with patch.dict(
  83. boto_elasticache.__salt__, {"boto_elasticache.group_exists": mock}
  84. ):
  85. comt = "{0} replication group exists .".format(name)
  86. ret.update({"comment": comt})
  87. self.assertDictEqual(
  88. boto_elasticache.creategroup(
  89. name, primary_cluster_id, replication_group_description
  90. ),
  91. ret,
  92. )