test_vbox_guest.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # -*- coding: utf-8 -*-
  2. """
  3. :codeauthor: Rahul Handay <rahulha@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.vbox_guest as vbox_guest
  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 VboxGuestTestCase(TestCase, LoaderModuleMockMixin):
  14. """
  15. Validate the vbox_guest state
  16. """
  17. def setup_loader_modules(self):
  18. return {vbox_guest: {}}
  19. def test_additions_installed(self):
  20. """
  21. Test to ensure that the VirtualBox Guest Additions are installed
  22. """
  23. ret = {"name": "salt", "changes": {}, "result": True, "comment": ""}
  24. mock = MagicMock(side_effect=[True, False, False, False])
  25. with patch.dict(
  26. vbox_guest.__salt__,
  27. {
  28. "vbox_guest.additions_version": mock,
  29. "vbox_guest.additions_install": mock,
  30. },
  31. ):
  32. ret.update({"comment": "System already in the correct state"})
  33. self.assertDictEqual(vbox_guest.additions_installed("salt"), ret)
  34. with patch.dict(vbox_guest.__opts__, {"test": True}):
  35. ret.update(
  36. {
  37. "changes": {"new": True, "old": False},
  38. "comment": "The state of VirtualBox Guest"
  39. " Additions will be changed.",
  40. "result": None,
  41. }
  42. )
  43. self.assertDictEqual(vbox_guest.additions_installed("salt"), ret)
  44. with patch.dict(vbox_guest.__opts__, {"test": False}):
  45. ret.update(
  46. {
  47. "changes": {"new": False, "old": False},
  48. "comment": "The state of VirtualBox Guest"
  49. " Additions was changed!",
  50. "result": False,
  51. }
  52. )
  53. self.assertDictEqual(vbox_guest.additions_installed("salt"), ret)
  54. def test_additions_removed(self):
  55. """
  56. Test to ensure that the VirtualBox Guest Additions are removed.
  57. """
  58. ret = {"name": "salt", "changes": {}, "result": True, "comment": ""}
  59. mock = MagicMock(side_effect=[False, True, True, True])
  60. with patch.dict(
  61. vbox_guest.__salt__,
  62. {"vbox_guest.additions_version": mock, "vbox_guest.additions_remove": mock},
  63. ):
  64. ret.update({"comment": "System already in the correct state"})
  65. self.assertDictEqual(vbox_guest.additions_removed("salt"), ret)
  66. with patch.dict(vbox_guest.__opts__, {"test": True}):
  67. ret.update(
  68. {
  69. "changes": {"new": True, "old": True},
  70. "comment": "The state of VirtualBox Guest"
  71. " Additions will be changed.",
  72. "result": None,
  73. }
  74. )
  75. self.assertDictEqual(vbox_guest.additions_removed("salt"), ret)
  76. with patch.dict(vbox_guest.__opts__, {"test": False}):
  77. ret.update(
  78. {
  79. "comment": "The state of VirtualBox Guest"
  80. " Additions was changed!",
  81. "result": True,
  82. }
  83. )
  84. self.assertDictEqual(vbox_guest.additions_removed("salt"), ret)
  85. def test_grantaccess_to_sharedfolders(self):
  86. """
  87. Test to grant access to auto-mounted shared folders to the users.
  88. """
  89. ret = {"name": "AB", "changes": {}, "result": True, "comment": ""}
  90. mock = MagicMock(side_effect=[["AB"], "salt", "salt", "salt"])
  91. with patch.dict(
  92. vbox_guest.__salt__,
  93. {
  94. "vbox_guest.list_shared_folders_users": mock,
  95. "vbox_guest.grant_access_to_shared_folders_to": mock,
  96. },
  97. ):
  98. ret.update({"comment": "System already in the correct state"})
  99. self.assert_method(ret)
  100. with patch.dict(vbox_guest.__opts__, {"test": True}):
  101. ret.update(
  102. {
  103. "changes": {"new": ["AB"], "old": "salt"},
  104. "comment": "List of users who have access to"
  105. " auto-mounted shared folders will be changed",
  106. "result": None,
  107. }
  108. )
  109. self.assert_method(ret)
  110. with patch.dict(vbox_guest.__opts__, {"test": False}):
  111. ret.update(
  112. {
  113. "changes": {"new": "salt", "old": "salt"},
  114. "comment": "List of users who have access to"
  115. " auto-mounted shared folders was changed",
  116. "result": True,
  117. }
  118. )
  119. self.assert_method(ret)
  120. def assert_method(self, ret):
  121. """
  122. Method call for assert statements
  123. """
  124. self.assertDictEqual(vbox_guest.grant_access_to_shared_folders_to("AB"), ret)