test_vbox_guest.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Rahul Handay <rahulha@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch,
  13. )
  14. # Import Salt Libs
  15. import salt.states.vbox_guest as vbox_guest
  16. class VboxGuestTestCase(TestCase, LoaderModuleMockMixin):
  17. '''
  18. Validate the vbox_guest state
  19. '''
  20. def setup_loader_modules(self):
  21. return {vbox_guest: {}}
  22. def test_additions_installed(self):
  23. '''
  24. Test to ensure that the VirtualBox Guest Additions are installed
  25. '''
  26. ret = {'name': 'salt',
  27. 'changes': {},
  28. 'result': True,
  29. 'comment': ''}
  30. mock = MagicMock(side_effect=[True, False, False, False])
  31. with patch.dict(vbox_guest.__salt__,
  32. {"vbox_guest.additions_version": mock,
  33. "vbox_guest.additions_install": mock}):
  34. ret.update({'comment': 'System already in the correct state'})
  35. self.assertDictEqual(vbox_guest.additions_installed('salt'), ret)
  36. with patch.dict(vbox_guest.__opts__, {"test": True}):
  37. ret.update({'changes': {'new': True, 'old': False},
  38. 'comment': 'The state of VirtualBox Guest'
  39. ' Additions will be changed.', 'result': None})
  40. self.assertDictEqual(vbox_guest.additions_installed('salt'),
  41. ret)
  42. with patch.dict(vbox_guest.__opts__, {"test": False}):
  43. ret.update({'changes': {'new': False, 'old': False},
  44. 'comment': 'The state of VirtualBox Guest'
  45. ' Additions was changed!', 'result': False})
  46. self.assertDictEqual(vbox_guest.additions_installed('salt'),
  47. ret)
  48. def test_additions_removed(self):
  49. '''
  50. Test to ensure that the VirtualBox Guest Additions are removed.
  51. '''
  52. ret = {'name': 'salt',
  53. 'changes': {},
  54. 'result': True,
  55. 'comment': ''}
  56. mock = MagicMock(side_effect=[False, True, True, True])
  57. with patch.dict(vbox_guest.__salt__,
  58. {"vbox_guest.additions_version": mock,
  59. "vbox_guest.additions_remove": mock}):
  60. ret.update({'comment': 'System already in the correct state'})
  61. self.assertDictEqual(vbox_guest.additions_removed('salt'), ret)
  62. with patch.dict(vbox_guest.__opts__, {"test": True}):
  63. ret.update({'changes': {'new': True, 'old': True},
  64. 'comment': 'The state of VirtualBox Guest'
  65. ' Additions will be changed.', 'result': None})
  66. self.assertDictEqual(vbox_guest.additions_removed('salt'),
  67. ret)
  68. with patch.dict(vbox_guest.__opts__, {"test": False}):
  69. ret.update({'comment': 'The state of VirtualBox Guest'
  70. ' Additions was changed!', 'result': True})
  71. self.assertDictEqual(vbox_guest.additions_removed('salt'),
  72. ret)
  73. def test_grantaccess_to_sharedfolders(self):
  74. '''
  75. Test to grant access to auto-mounted shared folders to the users.
  76. '''
  77. ret = {'name': 'AB',
  78. 'changes': {},
  79. 'result': True,
  80. 'comment': ''}
  81. mock = MagicMock(side_effect=[['AB'], 'salt', 'salt', 'salt'])
  82. with patch.dict(vbox_guest.__salt__,
  83. {"vbox_guest.list_shared_folders_users": mock,
  84. "vbox_guest.grant_access_to_shared_folders_to": mock}
  85. ):
  86. ret.update({'comment': 'System already in the correct state'})
  87. self.assert_method(ret)
  88. with patch.dict(vbox_guest.__opts__, {"test": True}):
  89. ret.update({'changes': {'new': ['AB'], 'old': 'salt'},
  90. 'comment': 'List of users who have access to'
  91. ' auto-mounted shared folders will be changed',
  92. 'result': None})
  93. self.assert_method(ret)
  94. with patch.dict(vbox_guest.__opts__, {"test": False}):
  95. ret.update({'changes': {'new': 'salt', 'old': 'salt'},
  96. 'comment': 'List of users who have access to'
  97. ' auto-mounted shared folders was changed',
  98. 'result': True})
  99. self.assert_method(ret)
  100. def assert_method(self, ret):
  101. '''
  102. Method call for assert statements
  103. '''
  104. self.assertDictEqual(vbox_guest.grant_access_to_shared_folders_to('AB'),
  105. ret)