test_serverdensity_device.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.modules.serverdensity_device as serverdensity_device
  8. # Import Salt Libs
  9. import salt.utils.json
  10. from salt.exceptions import CommandExecutionError
  11. # Import Salt Testing Libs
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.mock import MagicMock, patch
  14. from tests.support.unit import TestCase
  15. class MockRequests(object):
  16. """
  17. Mock smtplib class
  18. """
  19. flag = None
  20. content = """{"message": "Invalid token", "errors": [{"type": "invalid_token", "subject": "token"}]}"""
  21. status_code = None
  22. def __init__(self):
  23. self.url = None
  24. self.data = None
  25. self.kwargs = None
  26. def return_request(self, url, data=None, **kwargs):
  27. """
  28. Mock request method.
  29. """
  30. self.url = url
  31. self.data = data
  32. self.kwargs = kwargs
  33. requests = MockRequests()
  34. if self.flag == 1:
  35. requests.status_code = 401
  36. else:
  37. requests.status_code = 200
  38. return requests
  39. def post(self, url, data=None, **kwargs):
  40. """
  41. Mock post method.
  42. """
  43. return self.return_request(url, data, **kwargs)
  44. def delete(self, url, **kwargs):
  45. """
  46. Mock delete method.
  47. """
  48. return self.return_request(url, **kwargs)
  49. def get(self, url, **kwargs):
  50. """
  51. Mock get method.
  52. """
  53. return self.return_request(url, **kwargs)
  54. def put(self, url, data=None, **kwargs):
  55. """
  56. Mock put method.
  57. """
  58. return self.return_request(url, data, **kwargs)
  59. class ServerdensityDeviceTestCase(TestCase, LoaderModuleMockMixin):
  60. """
  61. TestCase for salt.modules.serverdensity_device
  62. """
  63. def setup_loader_modules(self):
  64. return {serverdensity_device: {"requests": MockRequests()}}
  65. def setUp(self):
  66. self.mock_json_loads = MagicMock(side_effect=ValueError())
  67. # 'get_sd_auth' function tests: 1
  68. def test_get_sd_auth(self):
  69. """
  70. Tests if it returns requested Server Density
  71. authentication value from pillar.
  72. """
  73. with patch.dict(serverdensity_device.__pillar__, {"serverdensity": False}):
  74. self.assertRaises(
  75. CommandExecutionError, serverdensity_device.get_sd_auth, "1"
  76. )
  77. with patch.dict(
  78. serverdensity_device.__pillar__, {"serverdensity": {"1": "salt"}}
  79. ):
  80. self.assertEqual(serverdensity_device.get_sd_auth("1"), "salt")
  81. self.assertRaises(
  82. CommandExecutionError, serverdensity_device.get_sd_auth, "2"
  83. )
  84. # 'create' function tests: 1
  85. def test_create(self):
  86. """
  87. Tests if it create device in Server Density.
  88. """
  89. with patch.dict(
  90. serverdensity_device.__pillar__, {"serverdensity": {"api_token": "salt"}}
  91. ):
  92. self.assertTrue(serverdensity_device.create("rich_lama", group="lama_band"))
  93. with patch.object(salt.utils.json, "loads", self.mock_json_loads):
  94. self.assertRaises(
  95. CommandExecutionError,
  96. serverdensity_device.create,
  97. "rich_lama",
  98. group="lama_band",
  99. )
  100. MockRequests.flag = 1
  101. self.assertIsNone(
  102. serverdensity_device.create("rich_lama", group="lama_band")
  103. )
  104. # 'delete' function tests: 1
  105. def test_delete(self):
  106. """
  107. Tests if it delete a device from Server Density.
  108. """
  109. with patch.dict(
  110. serverdensity_device.__pillar__, {"serverdensity": {"api_token": "salt"}}
  111. ):
  112. MockRequests.flag = 0
  113. self.assertTrue(serverdensity_device.delete("51f7eaf"))
  114. with patch.object(salt.utils.json, "loads", self.mock_json_loads):
  115. self.assertRaises(
  116. CommandExecutionError, serverdensity_device.delete, "51f7eaf"
  117. )
  118. MockRequests.flag = 1
  119. self.assertIsNone(serverdensity_device.delete("51f7eaf"))
  120. # 'ls' function tests: 1
  121. def test_ls(self):
  122. """
  123. Tests if it list devices in Server Density.
  124. """
  125. with patch.dict(
  126. serverdensity_device.__pillar__, {"serverdensity": {"api_token": "salt"}}
  127. ):
  128. MockRequests.flag = 0
  129. self.assertTrue(serverdensity_device.ls(name="lama"))
  130. with patch.object(salt.utils.json, "loads", self.mock_json_loads):
  131. self.assertRaises(
  132. CommandExecutionError, serverdensity_device.ls, name="lama"
  133. )
  134. MockRequests.flag = 1
  135. self.assertIsNone(serverdensity_device.ls(name="lama"))
  136. # 'update' function tests: 1
  137. def test_update(self):
  138. """
  139. Tests if it updates device information in Server Density.
  140. """
  141. with patch.dict(
  142. serverdensity_device.__pillar__, {"serverdensity": {"api_token": "salt"}}
  143. ):
  144. MockRequests.flag = 0
  145. self.assertTrue(serverdensity_device.update("51f7eaf", name="lama"))
  146. with patch.object(salt.utils.json, "loads", self.mock_json_loads):
  147. self.assertRaises(
  148. CommandExecutionError,
  149. serverdensity_device.update,
  150. "51f7eaf",
  151. name="lama",
  152. )
  153. MockRequests.flag = 1
  154. self.assertIsNone(serverdensity_device.update("51f7eaf", name="lama"))
  155. # 'install_agent' function tests: 1
  156. def test_install_agent(self):
  157. """
  158. Tests if it downloads Server Density installation agent,
  159. and installs sd-agent with agent_key.
  160. """
  161. mock = MagicMock(return_value=True)
  162. with patch.dict(
  163. serverdensity_device.__pillar__, {"serverdensity": {"account_url": "salt"}}
  164. ):
  165. with patch.dict(serverdensity_device.__salt__, {"cmd.run": mock}):
  166. with patch.dict(serverdensity_device.__opts__, {"cachedir": "/"}):
  167. self.assertTrue(serverdensity_device.install_agent("51f7e"))
  168. # 'install_agent_v2' function tests: 1
  169. def test_install_agent_v2(self):
  170. """
  171. Tests if it downloads Server Density installation agent,
  172. and installs sd-agent with agent_key.
  173. """
  174. mock = MagicMock(return_value=True)
  175. with patch.dict(
  176. serverdensity_device.__pillar__, {"serverdensity": {"account_name": "salt"}}
  177. ):
  178. with patch.dict(serverdensity_device.__salt__, {"cmd.run": mock}):
  179. with patch.dict(serverdensity_device.__opts__, {"cachedir": "/"}):
  180. self.assertTrue(
  181. serverdensity_device.install_agent("51f7e", agent_version=2)
  182. )