test_kmod.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.kmod as kmod
  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 KmodTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.states.kmod
  16. """
  17. def setup_loader_modules(self):
  18. return {kmod: {}}
  19. # 'present' function tests: 2
  20. def test_present(self):
  21. """
  22. Test to ensure that the specified kernel module is loaded.
  23. """
  24. name = "cheese"
  25. ret = {"name": name, "result": True, "comment": "", "changes": {}}
  26. mock_mod_list = MagicMock(return_value=[name])
  27. with patch.dict(kmod.__salt__, {"kmod.mod_list": mock_mod_list}):
  28. comment = "Kernel module {0} is already present".format(name)
  29. ret.update({"comment": comment})
  30. self.assertDictEqual(kmod.present(name), ret)
  31. mock_mod_list = MagicMock(return_value=[])
  32. with patch.dict(kmod.__salt__, {"kmod.mod_list": mock_mod_list}):
  33. with patch.dict(kmod.__opts__, {"test": True}):
  34. comment = "Kernel module {0} is set to be loaded".format(name)
  35. ret.update({"comment": comment, "result": None})
  36. self.assertDictEqual(kmod.present(name), ret)
  37. mock_mod_list = MagicMock(return_value=[])
  38. mock_available = MagicMock(return_value=[name])
  39. mock_load = MagicMock(return_value=[name])
  40. with patch.dict(
  41. kmod.__salt__,
  42. {
  43. "kmod.mod_list": mock_mod_list,
  44. "kmod.available": mock_available,
  45. "kmod.load": mock_load,
  46. },
  47. ):
  48. with patch.dict(kmod.__opts__, {"test": False}):
  49. comment = "Loaded kernel module {0}".format(name)
  50. ret.update(
  51. {"comment": comment, "result": True, "changes": {name: "loaded"}}
  52. )
  53. self.assertDictEqual(kmod.present(name), ret)
  54. def test_present_multi(self):
  55. """
  56. Test to ensure that multiple kernel modules are loaded.
  57. """
  58. name = "salted kernel"
  59. mods = ["cheese", "crackers"]
  60. ret = {"name": name, "result": True, "changes": {}}
  61. mock_mod_list = MagicMock(return_value=mods)
  62. with patch.dict(kmod.__salt__, {"kmod.mod_list": mock_mod_list}):
  63. call_ret = kmod.present(name, mods=mods)
  64. # Check comment independently: makes test more stable on PY3
  65. comment = call_ret.pop("comment")
  66. self.assertIn("cheese", comment)
  67. self.assertIn("crackers", comment)
  68. self.assertIn("are already present", comment)
  69. # Assert against all other dictionary key/values
  70. self.assertDictEqual(ret, call_ret)
  71. mock_mod_list = MagicMock(return_value=[])
  72. with patch.dict(kmod.__salt__, {"kmod.mod_list": mock_mod_list}):
  73. with patch.dict(kmod.__opts__, {"test": True}):
  74. call_ret = kmod.present(name, mods=mods)
  75. ret.update({"result": None})
  76. # Check comment independently: makes test more stable on PY3
  77. comment = call_ret.pop("comment")
  78. self.assertIn("cheese", comment)
  79. self.assertIn("crackers", comment)
  80. self.assertIn("are set to be loaded", comment)
  81. # Assert against all other dictionary key/values
  82. self.assertDictEqual(ret, call_ret)
  83. mock_mod_list = MagicMock(return_value=[])
  84. mock_available = MagicMock(return_value=mods)
  85. mock_load = MagicMock(return_value=mods)
  86. with patch.dict(
  87. kmod.__salt__,
  88. {
  89. "kmod.mod_list": mock_mod_list,
  90. "kmod.available": mock_available,
  91. "kmod.load": mock_load,
  92. },
  93. ):
  94. with patch.dict(kmod.__opts__, {"test": False}):
  95. call_ret = kmod.present(name, mods=mods)
  96. ret.update(
  97. {"result": True, "changes": {mods[0]: "loaded", mods[1]: "loaded"}}
  98. )
  99. # Check comment independently: makes test more stable on PY3
  100. comment = call_ret.pop("comment")
  101. self.assertIn("cheese", comment)
  102. self.assertIn("crackers", comment)
  103. self.assertIn("Loaded kernel modules", comment)
  104. # Assert against all other dictionary key/values
  105. self.assertDictEqual(ret, call_ret)
  106. # 'absent' function tests: 2
  107. def test_absent(self):
  108. """
  109. Test to verify that the named kernel module is not loaded.
  110. """
  111. name = "cheese"
  112. ret = {"name": name, "result": True, "comment": "", "changes": {}}
  113. mock_mod_list = MagicMock(return_value=[name])
  114. with patch.dict(kmod.__salt__, {"kmod.mod_list": mock_mod_list}):
  115. with patch.dict(kmod.__opts__, {"test": True}):
  116. comment = "Kernel module {0} is set to be removed".format(name)
  117. ret.update({"comment": comment, "result": None})
  118. self.assertDictEqual(kmod.absent(name), ret)
  119. mock_mod_list = MagicMock(return_value=[name])
  120. mock_remove = MagicMock(return_value=[name])
  121. with patch.dict(
  122. kmod.__salt__, {"kmod.mod_list": mock_mod_list, "kmod.remove": mock_remove}
  123. ):
  124. with patch.dict(kmod.__opts__, {"test": False}):
  125. comment = "Removed kernel module {0}".format(name)
  126. ret.update(
  127. {"comment": comment, "result": True, "changes": {name: "removed"}}
  128. )
  129. self.assertDictEqual(kmod.absent(name), ret)
  130. mock_mod_list = MagicMock(return_value=[])
  131. with patch.dict(kmod.__salt__, {"kmod.mod_list": mock_mod_list}):
  132. with patch.dict(kmod.__opts__, {"test": True}):
  133. comment = "Kernel module {0} is already removed".format(name)
  134. ret.update({"comment": comment, "result": True, "changes": {}})
  135. self.assertDictEqual(kmod.absent(name), ret)
  136. def test_absent_multi(self):
  137. """
  138. Test to verify that multiple kernel modules are not loaded.
  139. """
  140. name = "salted kernel"
  141. mods = ["cheese", "crackers"]
  142. ret = {"name": name, "result": True, "changes": {}}
  143. mock_mod_list = MagicMock(return_value=mods)
  144. with patch.dict(kmod.__salt__, {"kmod.mod_list": mock_mod_list}):
  145. with patch.dict(kmod.__opts__, {"test": True}):
  146. ret.update({"result": None})
  147. call_ret = kmod.absent(name, mods=mods)
  148. # Check comment independently: makes test more stable on PY3
  149. comment = call_ret.pop("comment")
  150. self.assertIn("cheese", comment)
  151. self.assertIn("crackers", comment)
  152. self.assertIn("are set to be removed", comment)
  153. # Assert against all other dictionary key/values
  154. self.assertDictEqual(ret, call_ret)
  155. mock_mod_list = MagicMock(return_value=mods)
  156. mock_remove = MagicMock(return_value=mods)
  157. with patch.dict(
  158. kmod.__salt__, {"kmod.mod_list": mock_mod_list, "kmod.remove": mock_remove}
  159. ):
  160. with patch.dict(kmod.__opts__, {"test": False}):
  161. call_ret = kmod.absent(name, mods=mods)
  162. ret.update(
  163. {
  164. "result": True,
  165. "changes": {mods[0]: "removed", mods[1]: "removed"},
  166. }
  167. )
  168. # Check comment independently: makes test more stable on PY3
  169. comment = call_ret.pop("comment")
  170. self.assertIn("cheese", comment)
  171. self.assertIn("crackers", comment)
  172. self.assertIn("Removed kernel modules", comment)
  173. # Assert against all other dictionary key/values
  174. self.assertDictEqual(ret, call_ret)
  175. mock_mod_list = MagicMock(return_value=[])
  176. with patch.dict(kmod.__salt__, {"kmod.mod_list": mock_mod_list}):
  177. with patch.dict(kmod.__opts__, {"test": True}):
  178. comment = "Kernel modules {0} are already removed".format(
  179. ", ".join(mods)
  180. )
  181. ret.update({"comment": comment, "result": True, "changes": {}})
  182. self.assertDictEqual(kmod.absent(name, mods=mods), ret)