test_boto_iam_role.py 7.6 KB

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