test_augeas_cfg.py 4.9 KB

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