1
0

test_boto_sns.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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('Please setup boto AWS credentials before running boto integration tests.')
  27. # The name of the topic you want to create.
  28. # Constraints: Topic names must be made up of only uppercase and
  29. # lowercase ASCII letters, numbers, underscores, and hyphens,
  30. # and must be between 1 and 256 characters long.
  31. # http://docs.aws.amazon.com/sns/latest/api/API_CreateTopic.html
  32. self.topic_name = re.sub(r'[^a-zA-Z_-]', '_', self.id())[0:256]
  33. self.topic_names = [self.topic_name]
  34. self.run_function('boto_sns.delete', name=self.topic_name)
  35. def tearDown(self):
  36. for topic in self.topic_names:
  37. self.run_function('boto_sns.delete', name=topic)
  38. def test_exists_non_existing(self):
  39. ret = self.run_function('boto_sns.exists', ['nonexistent'])
  40. self.assertSaltModuleFalseReturn(ret)
  41. def test_exists_existing(self):
  42. self.run_function('boto_sns.create', [self.topic_name])
  43. ret = self.run_function('boto_sns.exists', [self.topic_name])
  44. self.assertSaltModuleTrueReturn(ret)
  45. def test_create(self):
  46. ret = self.run_function('boto_sns.create', [self.topic_name])
  47. self.assertSaltModuleTrueReturn(ret)
  48. ret = self.run_function('boto_sns.get_all_topics')
  49. self.assertIn(self.topic_name, list(ret.keys()))
  50. self.assertIn(self._get_arn(self.topic_name), list(ret.values()))
  51. def test_delete_non_existing(self):
  52. ret = self.run_function('boto_sns.delete', [self.topic_name])
  53. self.assertSaltModuleTrueReturn(ret)
  54. def test_delete_existing(self):
  55. self.run_function('boto_sns.create', [self.topic_name])
  56. ret = self.run_function('boto_sns.delete', [self.topic_name])
  57. self.assertSaltModuleTrueReturn(ret)
  58. ret = self.run_function('boto_sns.get_all_topics')
  59. self.assertNotIn(self.topic_name, list(ret.keys()))
  60. self.assertNotIn(self._get_arn(self.topic_name), list(ret.values()))
  61. def test_get_all_topics(self):
  62. self.topic_names.append(self.topic_name + '-2')
  63. for topic in self.topic_names:
  64. self.run_function('boto_sns.create', [topic])
  65. ret = self.run_function('boto_sns.get_all_topics')
  66. for topic in self.topic_names:
  67. self.assertIn(topic, list(ret.keys()))
  68. self.assertIn(self._get_arn(topic), list(ret.values()))
  69. def test_subscribe_and_get_all_subscriptions_by_topic(self):
  70. topic_name = self.topic_name
  71. ret = self.run_function('boto_sns.create', [topic_name])
  72. ret = self.run_function(
  73. 'boto_sns.subscribe',
  74. [topic_name, 'https', 'https://www.example.com/sns/endpoint']
  75. )
  76. self.assertSaltModuleTrueReturn(ret)
  77. ret = self.run_function('boto_sns.get_all_subscriptions_by_topic',
  78. [topic_name])
  79. self.assertDictContainsSubset({
  80. 'Protocol': 'https',
  81. 'Endpoint': 'https://www.example.com/sns/endpoint'
  82. }, ret[0])
  83. def _get_arn(self, name):
  84. return 'arn:aws:sns:us-east-1:{0}:{1}'.format(self.account_id, name)
  85. @property
  86. def account_id(self):
  87. if not hasattr(self, '_account_id'):
  88. account_id = self.run_function('boto_iam.get_account_id')
  89. setattr(self, '_account_id', account_id)
  90. return self._account_id
  91. def assertSaltModuleTrueReturn(self, ret):
  92. self.assertIsInstance(ret, bool)
  93. self.assertTrue(ret)
  94. def assertSaltModuleFalseReturn(self, ret):
  95. self.assertIsInstance(ret, bool)
  96. self.assertFalse(ret)