test_boto_iam_role.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch)
  13. # Import Salt Libs
  14. import salt.states.boto_iam_role as boto_iam_role
  15. class BotoIAMRoleTestCase(TestCase, LoaderModuleMockMixin):
  16. '''
  17. Test cases for salt.states.boto_iam_role
  18. '''
  19. def setup_loader_modules(self):
  20. return {boto_iam_role: {}}
  21. # 'present' function tests: 1
  22. def test_present(self):
  23. '''
  24. Test to ensure the IAM role exists.
  25. '''
  26. name = 'myrole'
  27. ret = {'name': name,
  28. 'result': False,
  29. 'changes': {},
  30. 'comment': ''}
  31. _desc_role = {
  32. 'create_date': '2015-02-11T19:47:14Z',
  33. 'role_id': 'HIUHBIUBIBNKJNBKJ',
  34. 'assume_role_policy_document': {
  35. 'Version': '2008-10-17',
  36. 'Statement': [{
  37. 'Action': 'sts:AssumeRole',
  38. 'Principal': {'Service': 'ec2.amazonaws.com'},
  39. 'Effect': 'Allow'
  40. }]},
  41. 'role_name': 'myfakerole',
  42. 'path': '/',
  43. 'arn': 'arn:aws:iam::12345:role/myfakerole'
  44. }
  45. _desc_role2 = {
  46. 'create_date': '2015-02-11T19:47:14Z',
  47. 'role_id': 'HIUHBIUBIBNKJNBKJ',
  48. 'assume_role_policy_document': {
  49. 'Version': '2008-10-17',
  50. 'Statement': [{
  51. 'Action': 'sts:AssumeRole',
  52. 'Principal': {
  53. 'Service': [
  54. 'ec2.amazonaws.com',
  55. 'datapipeline.amazonaws.com'
  56. ]
  57. },
  58. 'Effect': 'Allow'
  59. }]},
  60. 'role_name': 'myfakerole',
  61. 'path': '/',
  62. 'arn': 'arn:aws:iam::12345:role/myfakerole'
  63. }
  64. mock_desc = MagicMock(side_effect=[
  65. False, _desc_role, _desc_role, _desc_role2, _desc_role
  66. ])
  67. _build_policy = {
  68. 'Version': '2008-10-17',
  69. 'Statement': [{
  70. 'Action': 'sts:AssumeRole',
  71. 'Effect': 'Allow',
  72. 'Principal': {'Service': 'ec2.amazonaws.com'}
  73. }]
  74. }
  75. mock_policy = MagicMock(return_value=_build_policy)
  76. mock_ipe = MagicMock(side_effect=[False, True, True, True])
  77. mock_pa = MagicMock(side_effect=[False, True, True, True])
  78. mock_bool = MagicMock(return_value=False)
  79. mock_lst = MagicMock(return_value=[])
  80. with patch.dict(boto_iam_role.__salt__,
  81. {'boto_iam.describe_role': mock_desc,
  82. 'boto_iam.create_role': mock_bool,
  83. 'boto_iam.build_policy': mock_policy,
  84. 'boto_iam.update_assume_role_policy': mock_bool,
  85. 'boto_iam.instance_profile_exists': mock_ipe,
  86. 'boto_iam.list_attached_role_policies': mock_lst,
  87. 'boto_iam.create_instance_profile': mock_bool,
  88. 'boto_iam.profile_associated': mock_pa,
  89. 'boto_iam.associate_profile_to_role': mock_bool,
  90. 'boto_iam.list_role_policies': mock_lst}):
  91. with patch.dict(boto_iam_role.__opts__, {'test': False}):
  92. comt = (' Failed to create {0} IAM role.'.format(name))
  93. ret.update({'comment': comt})
  94. self.assertDictEqual(boto_iam_role.present(name), ret)
  95. comt = (' myrole role present. '
  96. 'Failed to create myrole instance profile.')
  97. ret.update({'comment': comt})
  98. self.assertDictEqual(boto_iam_role.present(name), ret)
  99. comt = (' myrole role present. Failed to associate myrole'
  100. ' instance profile with myrole role.')
  101. ret.update({'comment': comt})
  102. self.assertDictEqual(boto_iam_role.present(name), ret)
  103. comt = (' myrole role present. Failed to update assume role'
  104. ' policy.')
  105. ret.update({'comment': comt})
  106. self.assertDictEqual(boto_iam_role.present(name), ret)
  107. comt = (' myrole role present. ')
  108. ret.update({'comment': comt, 'result': True})
  109. self.assertDictEqual(boto_iam_role.present(name), ret)
  110. # 'absent' function tests: 1
  111. def test_absent(self):
  112. '''
  113. Test to ensure the IAM role is deleted.
  114. '''
  115. name = 'myrole'
  116. ret = {'name': name,
  117. 'result': False,
  118. 'changes': {},
  119. 'comment': ''}
  120. mock = MagicMock(side_effect=[['mypolicy'], ['mypolicy'], False, True,
  121. False, False, True, False, False, False,
  122. True])
  123. mock_bool = MagicMock(return_value=False)
  124. mock_lst = MagicMock(return_value=[])
  125. with patch.dict(boto_iam_role.__salt__,
  126. {'boto_iam.list_role_policies': mock,
  127. 'boto_iam.delete_role_policy': mock_bool,
  128. 'boto_iam.profile_associated': mock,
  129. 'boto_iam.disassociate_profile_from_role': mock_bool,
  130. 'boto_iam.instance_profile_exists': mock,
  131. 'boto_iam.list_attached_role_policies': mock_lst,
  132. 'boto_iam.delete_instance_profile': mock_bool,
  133. 'boto_iam.role_exists': mock,
  134. 'boto_iam.delete_role': mock_bool}):
  135. with patch.dict(boto_iam_role.__opts__, {'test': False}):
  136. comt = (' Failed to add policy mypolicy to role myrole')
  137. ret.update({'comment': comt,
  138. 'changes': {'new': {'policies': ['mypolicy']},
  139. 'old': {'policies': ['mypolicy']}}})
  140. self.assertDictEqual(boto_iam_role.absent(name), ret)
  141. comt = (' No policies in role myrole.'
  142. ' No attached policies in role myrole. Failed to disassociate '
  143. 'myrole instance profile from myrole role.')
  144. ret.update({'comment': comt, 'changes': {}})
  145. self.assertDictEqual(boto_iam_role.absent(name), ret)
  146. comt = (' No policies in role myrole.'
  147. ' No attached policies in role myrole. '
  148. ' Failed to delete myrole instance profile.')
  149. ret.update({'comment': comt, 'changes': {}})
  150. self.assertDictEqual(boto_iam_role.absent(name), ret)
  151. comt = (' No policies in role myrole.'
  152. ' No attached policies in role myrole. myrole instance profile '
  153. 'does not exist. Failed to delete myrole iam role.')
  154. ret.update({'comment': comt, 'changes': {}})
  155. self.assertDictEqual(boto_iam_role.absent(name), ret)