test_ports.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. '''
  5. # Import Python libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. import os
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase
  11. from tests.support.mock import (
  12. MagicMock,
  13. patch)
  14. from salt.exceptions import SaltInvocationError
  15. # Import Salt Libs
  16. import salt.states.ports as ports
  17. class MockModule(object):
  18. """
  19. Mock of __module__
  20. """
  21. __module__ = 'A'
  22. class MockContext(object):
  23. """
  24. Mock of __context__
  25. """
  26. __context__ = {'ports.install_error': 'salt'}
  27. class MockSys(object):
  28. """
  29. Mock of sys
  30. """
  31. def __init__(self):
  32. self.modules = {'A': MockContext()}
  33. class PortsTestCase(TestCase, LoaderModuleMockMixin):
  34. '''
  35. Test cases for salt.states.ports
  36. '''
  37. def setup_loader_modules(self):
  38. return {ports: {'sys': MockSys()}}
  39. # 'installed' function tests: 1
  40. def test_installed(self):
  41. '''
  42. Test to verify that the desired port is installed,
  43. and that it was compiled with the desired options.
  44. '''
  45. name = 'security/nmap'
  46. options = [{'IPV6': 'on'}]
  47. ret = {'name': name,
  48. 'result': None,
  49. 'comment': '',
  50. 'changes': {}}
  51. mock = MagicMock(side_effect=SaltInvocationError)
  52. with patch.dict(ports.__salt__, {'ports.showconfig': mock}):
  53. comt = ('Unable to get configuration for {0}. Port name may '
  54. 'be invalid, or ports tree may need to be updated. '
  55. 'Error message: '.format(name))
  56. ret.update({'comment': comt, 'result': False})
  57. self.assertDictEqual(ports.installed(name), ret)
  58. mock = MagicMock(return_value={})
  59. mock_lst = MagicMock(return_value={'origin': {'origin': name}})
  60. with patch.dict(ports.__salt__, {'ports.showconfig': mock,
  61. 'pkg.list_pkgs': mock_lst}):
  62. comt = ('security/nmap is already installed')
  63. ret.update({'comment': comt, 'result': True})
  64. self.assertDictEqual(ports.installed(name), ret)
  65. comt = ('security/nmap does not have any build options,'
  66. ' yet options were specified')
  67. ret.update({'comment': comt, 'result': False})
  68. self.assertDictEqual(ports.installed(name, options), ret)
  69. mock_dict = MagicMock(return_value={'origin': {'origin': 'salt'}})
  70. with patch.dict(ports.__salt__, {'pkg.list_pkgs': mock_dict}):
  71. with patch.dict(ports.__opts__, {'test': True}):
  72. comt = ('{0} will be installed'.format(name))
  73. ret.update({'comment': comt, 'result': None})
  74. self.assertDictEqual(ports.installed(name), ret)
  75. mock = MagicMock(return_value={'salt': {'salt': 'salt'}})
  76. mock_dict = MagicMock(return_value={'origin': {'origin': 'salt'}})
  77. mock_f = MagicMock(return_value=False)
  78. mock_t = MagicMock(return_value=True)
  79. with patch.dict(ports.__salt__, {'ports.showconfig': mock,
  80. 'pkg.list_pkgs': mock_dict,
  81. 'ports.config': mock_f,
  82. 'ports.rmconfig': mock_t}):
  83. with patch.dict(ports.__opts__, {'test': True}):
  84. comt = ('The following options are not available'
  85. ' for security/nmap: IPV6')
  86. ret.update({'comment': comt, 'result': False})
  87. self.assertDictEqual(ports.installed(name, options), ret)
  88. comt = ('security/nmap will be installed with the '
  89. 'default build options')
  90. ret.update({'comment': comt, 'result': None})
  91. self.assertDictEqual(ports.installed(name), ret)
  92. with patch.dict(ports.__opts__, {'test': False}):
  93. comt = ('Unable to set options for security/nmap')
  94. ret.update({'comment': comt, 'result': False})
  95. self.assertDictEqual(ports.installed(name, [{'salt': 'salt'}]),
  96. ret)
  97. with patch.object(os.path, 'isfile', mock_t):
  98. with patch.object(os.path, 'isdir', mock_t):
  99. comt = ('Unable to clear options for security/nmap')
  100. ret.update({'comment': comt, 'result': False})
  101. self.assertDictEqual(ports.installed(name), ret)
  102. with patch.dict(ports.__salt__, {'ports.config': mock_t,
  103. 'ports.install': mock_t,
  104. 'test.ping': MockModule()}):
  105. comt = ('Failed to install security/nmap.'
  106. ' Error message:\nsalt')
  107. ret.update({'comment': comt, 'result': False,
  108. 'changes': True})
  109. self.assertDictEqual(ports.installed(name,
  110. [{'salt': 'salt'}]),
  111. ret)