1
0

test_boto_sns.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # -*- coding: utf-8 -*-
  2. """
  3. Validate the boto_sns module
  4. """
  5. # Import Python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import re
  8. # Import Salt Testing libs
  9. from tests.support.case import ModuleCase
  10. from tests.support.unit import skipIf
  11. # Import 3rd-party libs
  12. try:
  13. import boto
  14. NO_BOTO_MODULE = False
  15. except ImportError:
  16. NO_BOTO_MODULE = True
  17. @skipIf(
  18. NO_BOTO_MODULE,
  19. "Please install the boto library before running boto integration tests.",
  20. )
  21. class BotoSNSTest(ModuleCase):
  22. def setUp(self):
  23. try:
  24. boto.connect_iam()
  25. except boto.exception.NoAuthHandlerFound:
  26. self.skipTest(
  27. "Please setup boto AWS credentials before running boto integration tests."
  28. )
  29. # The name of the topic you want to create.
  30. # Constraints: Topic names must be made up of only uppercase and
  31. # lowercase ASCII letters, numbers, underscores, and hyphens,
  32. # and must be between 1 and 256 characters long.
  33. # http://docs.aws.amazon.com/sns/latest/api/API_CreateTopic.html
  34. self.topic_name = re.sub(r"[^a-zA-Z_-]", "_", self.id())[0:256]
  35. self.topic_names = [self.topic_name]
  36. self.run_function("boto_sns.delete", name=self.topic_name)
  37. def tearDown(self):
  38. for topic in self.topic_names:
  39. self.run_function("boto_sns.delete", name=topic)
  40. def test_exists_non_existing(self):
  41. ret = self.run_function("boto_sns.exists", ["nonexistent"])
  42. self.assertSaltModuleFalseReturn(ret)
  43. def test_exists_existing(self):
  44. self.run_function("boto_sns.create", [self.topic_name])
  45. ret = self.run_function("boto_sns.exists", [self.topic_name])
  46. self.assertSaltModuleTrueReturn(ret)
  47. def test_create(self):
  48. ret = self.run_function("boto_sns.create", [self.topic_name])
  49. self.assertSaltModuleTrueReturn(ret)
  50. ret = self.run_function("boto_sns.get_all_topics")
  51. self.assertIn(self.topic_name, list(ret.keys()))
  52. self.assertIn(self._get_arn(self.topic_name), list(ret.values()))
  53. def test_delete_non_existing(self):
  54. ret = self.run_function("boto_sns.delete", [self.topic_name])
  55. self.assertSaltModuleTrueReturn(ret)
  56. def test_delete_existing(self):
  57. self.run_function("boto_sns.create", [self.topic_name])
  58. ret = self.run_function("boto_sns.delete", [self.topic_name])
  59. self.assertSaltModuleTrueReturn(ret)
  60. ret = self.run_function("boto_sns.get_all_topics")
  61. self.assertNotIn(self.topic_name, list(ret.keys()))
  62. self.assertNotIn(self._get_arn(self.topic_name), list(ret.values()))
  63. def test_get_all_topics(self):
  64. self.topic_names.append(self.topic_name + "-2")
  65. for topic in self.topic_names:
  66. self.run_function("boto_sns.create", [topic])
  67. ret = self.run_function("boto_sns.get_all_topics")
  68. for topic in self.topic_names:
  69. self.assertIn(topic, list(ret.keys()))
  70. self.assertIn(self._get_arn(topic), list(ret.values()))
  71. def test_subscribe_and_get_all_subscriptions_by_topic(self):
  72. topic_name = self.topic_name
  73. ret = self.run_function("boto_sns.create", [topic_name])
  74. ret = self.run_function(
  75. "boto_sns.subscribe",
  76. [topic_name, "https", "https://www.example.com/sns/endpoint"],
  77. )
  78. self.assertSaltModuleTrueReturn(ret)
  79. ret = self.run_function("boto_sns.get_all_subscriptions_by_topic", [topic_name])
  80. self.assertDictContainsSubset(
  81. {"Protocol": "https", "Endpoint": "https://www.example.com/sns/endpoint"},
  82. ret[0],
  83. )
  84. def _get_arn(self, name):
  85. return "arn:aws:sns:us-east-1:{0}:{1}".format(self.account_id, name)
  86. @property
  87. def account_id(self):
  88. if not hasattr(self, "_account_id"):
  89. account_id = self.run_function("boto_iam.get_account_id")
  90. setattr(self, "_account_id", account_id)
  91. return self._account_id
  92. def assertSaltModuleTrueReturn(self, ret):
  93. self.assertIsInstance(ret, bool)
  94. self.assertTrue(ret)
  95. def assertSaltModuleFalseReturn(self, ret):
  96. self.assertIsInstance(ret, bool)
  97. self.assertFalse(ret)