test_augeas_cfg.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 Testing Libs
  8. from tests.support.unit import TestCase, skipIf
  9. from tests.support.mock import (
  10. MagicMock,
  11. patch,
  12. )
  13. # Import Salt Libs
  14. import salt.modules.augeas_cfg as augeas_cfg
  15. from salt.exceptions import SaltInvocationError
  16. from salt.ext import six
  17. # Make sure augeas python interface is installed
  18. if augeas_cfg.HAS_AUGEAS:
  19. from augeas import Augeas as _Augeas
  20. @skipIf(augeas_cfg.HAS_AUGEAS is False, "python-augeas is required for this test case")
  21. class AugeasCfgTestCase(TestCase):
  22. '''
  23. Test cases for salt.modules.augeas_cfg
  24. '''
  25. # 'execute' function tests: 3
  26. @skipIf(six.PY3, 'Disabled pending https://github.com/hercules-team/python-augeas/issues/30')
  27. def test_execute(self):
  28. '''
  29. Test if it execute Augeas commands
  30. '''
  31. self.assertEqual(augeas_cfg.execute(), {'retval': True})
  32. def test_execute_io_error(self):
  33. '''
  34. Test if it execute Augeas commands
  35. '''
  36. ret = {'error': 'Command is not supported (yet)', 'retval': False}
  37. self.assertEqual(augeas_cfg.execute(None, None, [" "]), ret)
  38. def test_execute_value_error(self):
  39. '''
  40. Test if it execute Augeas commands
  41. '''
  42. ret = {'retval': False,
  43. 'error':
  44. 'Invalid formatted command, see debug log for details: '}
  45. self.assertEqual(augeas_cfg.execute(None, None, ["set "]), ret)
  46. # 'get' function tests: 1
  47. def test_get(self):
  48. '''
  49. Test if it get a value for a specific augeas path
  50. '''
  51. mock = MagicMock(side_effect=RuntimeError('error'))
  52. with patch.object(_Augeas, 'match', mock):
  53. self.assertEqual(augeas_cfg.get('/etc/hosts'),
  54. {'error': 'error'})
  55. mock = MagicMock(return_value=True)
  56. with patch.object(_Augeas, 'match', mock):
  57. self.assertEqual(augeas_cfg.get('/etc/hosts'),
  58. {'/etc/hosts': None})
  59. # 'setvalue' function tests: 4
  60. def test_setvalue(self):
  61. '''
  62. Test if it set a value for a specific augeas path
  63. '''
  64. self.assertEqual(augeas_cfg.setvalue('prefix=/etc/hosts'),
  65. {'retval': True})
  66. def test_setvalue_io_error(self):
  67. '''
  68. Test if it set a value for a specific augeas path
  69. '''
  70. mock = MagicMock(side_effect=IOError(''))
  71. with patch.object(_Augeas, 'save', mock):
  72. self.assertEqual(augeas_cfg.setvalue('prefix=/files/etc/'),
  73. {'retval': False, 'error': ''})
  74. def test_setvalue_uneven_path(self):
  75. '''
  76. Test if it set a value for a specific augeas path
  77. '''
  78. mock = MagicMock(side_effect=RuntimeError('error'))
  79. with patch.object(_Augeas, 'match', mock):
  80. self.assertRaises(SaltInvocationError, augeas_cfg.setvalue,
  81. ['/files/etc/hosts/1/canonical', 'localhost'])
  82. def test_setvalue_one_prefix(self):
  83. '''
  84. Test if it set a value for a specific augeas path
  85. '''
  86. self.assertRaises(SaltInvocationError, augeas_cfg.setvalue,
  87. 'prefix=/files', '10.18.1.1', 'prefix=/etc', 'test')
  88. # 'match' function tests: 2
  89. def test_match(self):
  90. '''
  91. Test if it matches for path expression
  92. '''
  93. self.assertEqual(augeas_cfg.match('/etc/service', 'ssh'), {})
  94. def test_match_runtime_error(self):
  95. '''
  96. Test if it matches for path expression
  97. '''
  98. mock = MagicMock(side_effect=RuntimeError('error'))
  99. with patch.object(_Augeas, 'match', mock):
  100. self.assertEqual(augeas_cfg.match('/etc/service-name', 'ssh'), {})
  101. # 'remove' function tests: 2
  102. def test_remove(self):
  103. '''
  104. Test if it removes for path expression
  105. '''
  106. self.assertEqual(augeas_cfg.remove('/etc/service'),
  107. {'count': 0, 'retval': True})
  108. def test_remove_io_runtime_error(self):
  109. '''
  110. Test if it removes for path expression
  111. '''
  112. mock = MagicMock(side_effect=RuntimeError('error'))
  113. with patch.object(_Augeas, 'save', mock):
  114. self.assertEqual(augeas_cfg.remove('/etc/service-name'),
  115. {'count': 0, 'error': 'error', 'retval': False})
  116. # 'ls' function tests: 1
  117. def test_ls(self):
  118. '''
  119. Test if it list the direct children of a node
  120. '''
  121. self.assertEqual(augeas_cfg.ls('/etc/passwd'), {})
  122. # 'tree' function tests: 1
  123. def test_tree(self):
  124. '''
  125. Test if it returns recursively the complete tree of a node
  126. '''
  127. self.assertEqual(augeas_cfg.tree('/etc/'), {'/etc': None})