test_system.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.modules.system as system
  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 SystemTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Test cases for salt.modules.system
  16. """
  17. def setup_loader_modules(self):
  18. return {system: {}}
  19. def test_halt(self):
  20. """
  21. Test to halt a running system
  22. """
  23. with patch.dict(system.__salt__, {"cmd.run": MagicMock(return_value="A")}):
  24. self.assertEqual(system.halt(), "A")
  25. def test_init(self):
  26. """
  27. Test to change the system runlevel on sysV compatible systems
  28. """
  29. with patch.dict(system.__salt__, {"cmd.run": MagicMock(return_value="A")}):
  30. self.assertEqual(system.init("r"), "A")
  31. def test_poweroff(self):
  32. """
  33. Test to poweroff a running system
  34. """
  35. with patch.dict(system.__salt__, {"cmd.run": MagicMock(return_value="A")}):
  36. self.assertEqual(system.poweroff(), "A")
  37. def test_reboot(self):
  38. """
  39. Test to reboot the system with shutdown -r
  40. """
  41. cmd_mock = MagicMock(return_value="A")
  42. with patch.dict(system.__salt__, {"cmd.run": cmd_mock}):
  43. self.assertEqual(system.reboot(), "A")
  44. cmd_mock.assert_called_with(["shutdown", "-r", "now"], python_shell=False)
  45. def test_reboot_with_delay(self):
  46. """
  47. Test to reboot the system using shutdown -r with a delay
  48. """
  49. cmd_mock = MagicMock(return_value="A")
  50. with patch.dict(system.__salt__, {"cmd.run": cmd_mock}):
  51. self.assertEqual(system.reboot(at_time=5), "A")
  52. cmd_mock.assert_called_with(["shutdown", "-r", "5"], python_shell=False)
  53. def test_shutdown(self):
  54. """
  55. Test to shutdown a running system
  56. """
  57. cmd_mock = MagicMock(return_value="A")
  58. with patch.dict(system.__salt__, {"cmd.run": cmd_mock}), patch(
  59. "salt.utils.platform.is_freebsd", MagicMock(return_value=False)
  60. ), patch("salt.utils.platform.is_netbsd", MagicMock(return_value=False)), patch(
  61. "salt.utils.platform.is_openbsd", MagicMock(return_value=False)
  62. ):
  63. self.assertEqual(system.shutdown(), "A")
  64. cmd_mock.assert_called_with(["shutdown", "-h", "now"], python_shell=False)
  65. def test_shutdown_freebsd(self):
  66. """
  67. Test to shutdown a running FreeBSD system
  68. """
  69. cmd_mock = MagicMock(return_value="A")
  70. with patch.dict(system.__salt__, {"cmd.run": cmd_mock}), patch(
  71. "salt.utils.platform.is_freebsd", MagicMock(return_value=True)
  72. ), patch("salt.utils.platform.is_netbsd", MagicMock(return_value=False)), patch(
  73. "salt.utils.platform.is_openbsd", MagicMock(return_value=False)
  74. ):
  75. self.assertEqual(system.shutdown(), "A")
  76. cmd_mock.assert_called_with(["shutdown", "-p", "now"], python_shell=False)
  77. def test_shutdown_netbsd(self):
  78. """
  79. Test to shutdown a running NetBSD system
  80. """
  81. cmd_mock = MagicMock(return_value="A")
  82. with patch.dict(system.__salt__, {"cmd.run": cmd_mock}), patch(
  83. "salt.utils.platform.is_freebsd", MagicMock(return_value=False)
  84. ), patch("salt.utils.platform.is_netbsd", MagicMock(return_value=True)), patch(
  85. "salt.utils.platform.is_openbsd", MagicMock(return_value=False)
  86. ):
  87. self.assertEqual(system.shutdown(), "A")
  88. cmd_mock.assert_called_with(["shutdown", "-p", "now"], python_shell=False)
  89. def test_shutdown_openbsd(self):
  90. """
  91. Test to shutdown a running OpenBSD system
  92. """
  93. cmd_mock = MagicMock(return_value="A")
  94. with patch.dict(system.__salt__, {"cmd.run": cmd_mock}), patch(
  95. "salt.utils.platform.is_freebsd", MagicMock(return_value=False)
  96. ), patch("salt.utils.platform.is_netbsd", MagicMock(return_value=False)), patch(
  97. "salt.utils.platform.is_openbsd", MagicMock(return_value=True)
  98. ):
  99. self.assertEqual(system.shutdown(), "A")
  100. cmd_mock.assert_called_with(["shutdown", "-p", "now"], python_shell=False)