test_keystone.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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.keystone as keystone
  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 KeystoneTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.keystone
  16. """
  17. def setup_loader_modules(self):
  18. return {keystone: {}}
  19. # 'user_present' function tests: 1
  20. def test_user_present(self):
  21. """
  22. Test to ensure that the keystone user is present
  23. with the specified properties.
  24. """
  25. name = "nova"
  26. password = "$up3rn0v4"
  27. email = "nova@domain.com"
  28. tenant = "demo"
  29. ret = {"name": name, "result": False, "changes": {}, "comment": ""}
  30. mock_f = MagicMock(return_value=False)
  31. mock_lst = MagicMock(return_value=["Error"])
  32. with patch.dict(keystone.__salt__, {"keystone.tenant_get": mock_lst}):
  33. comt = 'Tenant / project "{0}" does not exist'.format(tenant)
  34. ret.update({"comment": comt})
  35. self.assertDictEqual(
  36. keystone.user_present(name, password, email, tenant), ret
  37. )
  38. mock_dict = MagicMock(
  39. side_effect=[
  40. {name: {"email": "a@a.com"}},
  41. {name: {"email": email, "enabled": False}},
  42. {name: {"email": email, "enabled": True}},
  43. {name: {"email": email, "enabled": True}},
  44. {"Error": "error"},
  45. {"Error": "error"},
  46. ]
  47. )
  48. mock_l = MagicMock(return_value={tenant: {"id": "abc"}})
  49. with patch.dict(
  50. keystone.__salt__,
  51. {
  52. "keystone.user_get": mock_dict,
  53. "keystone.tenant_get": mock_l,
  54. "keystone.user_verify_password": mock_f,
  55. "keystone.user_create": mock_f,
  56. },
  57. ):
  58. with patch.dict(keystone.__opts__, {"test": True}):
  59. comt = 'User "{0}" will be updated'.format(name)
  60. ret.update(
  61. {
  62. "comment": comt,
  63. "result": None,
  64. "changes": {
  65. "Email": "Will be updated",
  66. "Enabled": "Will be True",
  67. "Password": "Will be updated",
  68. },
  69. }
  70. )
  71. self.assertDictEqual(keystone.user_present(name, password, email), ret)
  72. ret.update(
  73. {
  74. "comment": comt,
  75. "result": None,
  76. "changes": {
  77. "Enabled": "Will be True",
  78. "Password": "Will be updated",
  79. },
  80. }
  81. )
  82. self.assertDictEqual(keystone.user_present(name, password, email), ret)
  83. ret.update(
  84. {
  85. "comment": comt,
  86. "result": None,
  87. "changes": {
  88. "Tenant": 'Will be added to "demo" tenant',
  89. "Password": "Will be updated",
  90. },
  91. }
  92. )
  93. self.assertDictEqual(
  94. keystone.user_present(name, password, email, tenant), ret
  95. )
  96. ret.update(
  97. {
  98. "comment": comt,
  99. "result": None,
  100. "changes": {"Password": "Will be updated"},
  101. }
  102. )
  103. self.assertDictEqual(keystone.user_present(name, password, email), ret)
  104. comt = 'Keystone user "nova" will be added'
  105. ret.update(
  106. {
  107. "comment": comt,
  108. "result": None,
  109. "changes": {"User": "Will be created"},
  110. }
  111. )
  112. self.assertDictEqual(keystone.user_present(name, password, email), ret)
  113. with patch.dict(keystone.__opts__, {"test": False}):
  114. comt = "Keystone user {0} has been added".format(name)
  115. ret.update(
  116. {"comment": comt, "result": True, "changes": {"User": "Created"}}
  117. )
  118. self.assertDictEqual(keystone.user_present(name, password, email), ret)
  119. # 'user_absent' function tests: 1
  120. def test_user_absent(self):
  121. """
  122. Test to ensure that the keystone user is absent.
  123. """
  124. name = "nova"
  125. ret = {
  126. "name": name,
  127. "changes": {},
  128. "result": True,
  129. "comment": 'User "{0}" is already absent'.format(name),
  130. }
  131. mock_lst = MagicMock(side_effect=[["Error"], []])
  132. with patch.dict(keystone.__salt__, {"keystone.user_get": mock_lst}):
  133. self.assertDictEqual(keystone.user_absent(name), ret)
  134. with patch.dict(keystone.__opts__, {"test": True}):
  135. comt = 'User "{0}" will be deleted'.format(name)
  136. ret.update({"comment": comt, "result": None})
  137. self.assertDictEqual(keystone.user_absent(name), ret)
  138. # 'tenant_present' function tests: 1
  139. def test_tenant_present(self):
  140. """
  141. Test to ensures that the keystone tenant exists
  142. """
  143. name = "nova"
  144. description = "OpenStack Compute Service"
  145. ret = {
  146. "name": name,
  147. "changes": {},
  148. "result": True,
  149. "comment": 'Tenant / project "{0}" already exists'.format(name),
  150. }
  151. mock_dict = MagicMock(
  152. side_effect=[
  153. {name: {"description": "desc"}},
  154. {name: {"description": description, "enabled": False}},
  155. {"Error": "error"},
  156. {"Error": "error"},
  157. ]
  158. )
  159. mock_t = MagicMock(return_value=True)
  160. with patch.dict(
  161. keystone.__salt__,
  162. {"keystone.tenant_get": mock_dict, "keystone.tenant_create": mock_t},
  163. ):
  164. with patch.dict(keystone.__opts__, {"test": True}):
  165. comt = 'Tenant / project "{0}" will be updated'.format(name)
  166. ret.update(
  167. {
  168. "comment": comt,
  169. "result": None,
  170. "changes": {"Description": "Will be updated"},
  171. }
  172. )
  173. self.assertDictEqual(keystone.tenant_present(name), ret)
  174. comt = 'Tenant / project "{0}" will be updated'.format(name)
  175. ret.update(
  176. {
  177. "comment": comt,
  178. "result": None,
  179. "changes": {"Enabled": "Will be True"},
  180. }
  181. )
  182. self.assertDictEqual(keystone.tenant_present(name, description), ret)
  183. comt = 'Tenant / project "{0}" will be added'.format(name)
  184. ret.update(
  185. {
  186. "comment": comt,
  187. "result": None,
  188. "changes": {"Tenant": "Will be created"},
  189. }
  190. )
  191. self.assertDictEqual(keystone.tenant_present(name), ret)
  192. with patch.dict(keystone.__opts__, {"test": False}):
  193. comt = 'Tenant / project "{0}" has been added'.format(name)
  194. ret.update(
  195. {"comment": comt, "result": True, "changes": {"Tenant": "Created"}}
  196. )
  197. self.assertDictEqual(keystone.tenant_present(name), ret)
  198. # 'tenant_absent' function tests: 1
  199. def test_tenant_absent(self):
  200. """
  201. Test to ensure that the keystone tenant is absent.
  202. """
  203. name = "nova"
  204. ret = {
  205. "name": name,
  206. "changes": {},
  207. "result": True,
  208. "comment": 'Tenant / project "{0}" is already absent'.format(name),
  209. }
  210. mock_lst = MagicMock(side_effect=[["Error"], []])
  211. with patch.dict(keystone.__salt__, {"keystone.tenant_get": mock_lst}):
  212. self.assertDictEqual(keystone.tenant_absent(name), ret)
  213. with patch.dict(keystone.__opts__, {"test": True}):
  214. comt = 'Tenant / project "{0}" will be deleted'.format(name)
  215. ret.update({"comment": comt, "result": None})
  216. self.assertDictEqual(keystone.tenant_absent(name), ret)
  217. # 'role_present' function tests: 1
  218. def test_role_present(self):
  219. """
  220. Test to ensures that the keystone role exists
  221. """
  222. name = "nova"
  223. ret = {
  224. "name": name,
  225. "changes": {},
  226. "result": True,
  227. "comment": 'Role "{0}" already exists'.format(name),
  228. }
  229. mock_lst = MagicMock(side_effect=[[], ["Error"]])
  230. with patch.dict(keystone.__salt__, {"keystone.role_get": mock_lst}):
  231. self.assertDictEqual(keystone.role_present(name), ret)
  232. with patch.dict(keystone.__opts__, {"test": True}):
  233. comt = 'Role "{0}" will be added'.format(name)
  234. ret.update({"comment": comt, "result": None})
  235. self.assertDictEqual(keystone.role_present(name), ret)
  236. # 'role_absent' function tests: 1
  237. def test_role_absent(self):
  238. """
  239. Test to ensure that the keystone role is absent.
  240. """
  241. name = "nova"
  242. ret = {
  243. "name": name,
  244. "changes": {},
  245. "result": True,
  246. "comment": 'Role "{0}" is already absent'.format(name),
  247. }
  248. mock_lst = MagicMock(side_effect=[["Error"], []])
  249. with patch.dict(keystone.__salt__, {"keystone.role_get": mock_lst}):
  250. self.assertDictEqual(keystone.role_absent(name), ret)
  251. with patch.dict(keystone.__opts__, {"test": True}):
  252. comt = 'Role "{0}" will be deleted'.format(name)
  253. ret.update({"comment": comt, "result": None})
  254. self.assertDictEqual(keystone.role_absent(name), ret)
  255. # 'service_present' function tests: 1
  256. def test_service_present(self):
  257. """
  258. Test to ensure service present in Keystone catalog
  259. """
  260. name = "nova"
  261. service_type = "compute"
  262. ret = {
  263. "name": name,
  264. "changes": {},
  265. "result": True,
  266. "comment": 'Service "{0}" already exists'.format(name),
  267. }
  268. mock_lst = MagicMock(side_effect=[[], ["Error"]])
  269. with patch.dict(keystone.__salt__, {"keystone.service_get": mock_lst}):
  270. self.assertDictEqual(keystone.service_present(name, service_type), ret)
  271. with patch.dict(keystone.__opts__, {"test": True}):
  272. comt = 'Service "{0}" will be added'.format(name)
  273. ret.update({"comment": comt, "result": None})
  274. self.assertDictEqual(keystone.service_present(name, service_type), ret)
  275. # 'service_absent' function tests: 1
  276. def test_service_absent(self):
  277. """
  278. Test to ensure that the service doesn't exist in Keystone catalog
  279. """
  280. name = "nova"
  281. ret = {
  282. "name": name,
  283. "changes": {},
  284. "result": True,
  285. "comment": 'Service "{0}" is already absent'.format(name),
  286. }
  287. mock_lst = MagicMock(side_effect=[["Error"], []])
  288. with patch.dict(keystone.__salt__, {"keystone.service_get": mock_lst}):
  289. self.assertDictEqual(keystone.service_absent(name), ret)
  290. with patch.dict(keystone.__opts__, {"test": True}):
  291. comt = 'Service "{0}" will be deleted'.format(name)
  292. ret.update({"comment": comt, "result": None})
  293. self.assertDictEqual(keystone.service_absent(name), ret)
  294. # 'endpoint_present' function tests: 1
  295. def test_endpoint_present(self):
  296. """
  297. Test to ensure the specified endpoints exists for service
  298. """
  299. name = "nova"
  300. region = "RegionOne"
  301. ret = {"name": name, "changes": {}, "result": True, "comment": ""}
  302. endpoint = {
  303. "adminurl": None,
  304. "region": None,
  305. "internalurl": None,
  306. "publicurl": None,
  307. "id": 1,
  308. "service_id": None,
  309. }
  310. mock_lst = MagicMock(
  311. side_effect=[endpoint, ["Error"], {"id": 1, "service_id": None}, []]
  312. )
  313. mock = MagicMock(return_value=True)
  314. with patch.dict(
  315. keystone.__salt__,
  316. {"keystone.endpoint_get": mock_lst, "keystone.endpoint_create": mock},
  317. ):
  318. comt = 'Endpoint for service "{0}" already exists'.format(name)
  319. ret.update({"comment": comt, "result": True, "changes": {}})
  320. self.assertDictEqual(keystone.endpoint_present(name), ret)
  321. with patch.dict(keystone.__opts__, {"test": True}):
  322. comt = 'Endpoint for service "{0}" will be added'.format(name)
  323. ret.update(
  324. {
  325. "comment": comt,
  326. "result": None,
  327. "changes": {"Endpoint": "Will be created"},
  328. }
  329. )
  330. self.assertDictEqual(keystone.endpoint_present(name), ret)
  331. comt = 'Endpoint for service "{0}" already exists'.format(name)
  332. ret.update({"comment": comt, "result": True, "changes": {}})
  333. self.assertDictEqual(keystone.endpoint_present(name), ret)
  334. with patch.dict(keystone.__opts__, {"test": False}):
  335. comt = 'Endpoint for service "{0}" has been added'.format(name)
  336. ret.update({"comment": comt, "result": True, "changes": True})
  337. self.assertDictEqual(keystone.endpoint_present(name), ret)
  338. # 'endpoint_absent' function tests: 1
  339. def test_endpoint_absent(self):
  340. """
  341. Test to ensure that the endpoint for a service doesn't
  342. exist in Keystone catalog
  343. """
  344. name = "nova"
  345. region = "RegionOne"
  346. comment = 'Endpoint for service "{0}" is already absent'.format(name)
  347. ret = {"name": name, "changes": {}, "result": True, "comment": comment}
  348. mock_lst = MagicMock(side_effect=[[], ["Error"]])
  349. with patch.dict(keystone.__salt__, {"keystone.endpoint_get": mock_lst}):
  350. self.assertDictEqual(keystone.endpoint_absent(name, region), ret)
  351. with patch.dict(keystone.__opts__, {"test": True}):
  352. comt = 'Endpoint for service "{0}" will be deleted'.format(name)
  353. ret.update({"comment": comt, "result": None})
  354. self.assertDictEqual(keystone.endpoint_absent(name, region), ret)