"Fossies" - the Fresh Open Source Software Archive

Member "salt-3002.2/tests/unit/states/test_sysctl.py" (18 Nov 2020, 7967 Bytes) of package /linux/misc/salt-3002.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "test_sysctl.py": 3002.1_vs_3002.2.

    1 """
    2     :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
    3 """
    4 
    5 import salt.states.sysctl as sysctl
    6 from salt.exceptions import CommandExecutionError
    7 from tests.support.mixins import LoaderModuleMockMixin
    8 from tests.support.mock import MagicMock, patch
    9 from tests.support.unit import TestCase
   10 
   11 
   12 class SysctlTestCase(TestCase):
   13     """
   14     Test cases for salt.states.sysctl
   15     """
   16 
   17 
   18 class SysctlPresentTestCase(SysctlTestCase, LoaderModuleMockMixin):
   19     """
   20     Test cases for salt.states.sysctl.present
   21     """
   22 
   23     def setup_loader_modules(self):
   24         return {sysctl: {}}
   25 
   26     def test_empty_config_file_and_value_not_found(self):
   27         """
   28         Test sysctl.present for an unknown sysctl, not present in config file
   29         """
   30         name = "some.unknown.oid"
   31         value = "1"
   32         comment = "Sysctl option {} would be changed to {}" "".format(name, value)
   33 
   34         ret = {"name": name, "result": None, "changes": {}, "comment": comment}
   35 
   36         with patch.dict(sysctl.__opts__, {"test": True}):
   37             mock = MagicMock(return_value={})
   38             with patch.dict(sysctl.__salt__, {"sysctl.show": mock}):
   39                 self.assertDictEqual(sysctl.present(name, value), ret)
   40 
   41     def test_inaccessible_config_file(self):
   42         """
   43         Test sysctl.present with a config file that cannot be opened
   44         """
   45         name = "some.unknown.oid"
   46         value = "1"
   47         config = "/etc/sysctl.conf"
   48         comment = (
   49             "Sysctl option {} might be changed, we failed to check "
   50             "config file at {}. The file is either unreadable, or "
   51             "missing.".format(name, config)
   52         )
   53         ret = {"name": name, "result": None, "changes": {}, "comment": comment}
   54 
   55         with patch.dict(sysctl.__opts__, {"test": True}):
   56             mock = MagicMock(return_value=None)
   57             with patch.dict(sysctl.__salt__, {"sysctl.show": mock}):
   58                 self.assertDictEqual(sysctl.present(name, value), ret)
   59 
   60     def test_to_be_changed_not_configured(self):
   61         """
   62         Test sysctl.present for a sysctl that isn't in the config file and must
   63         be changed.
   64         """
   65         name = "vfs.usermount"
   66         value = "1"
   67         comment = "Sysctl option {} set to be changed to {}".format(name, value)
   68         ret = {"name": name, "result": None, "changes": {}, "comment": comment}
   69 
   70         def mock_current(config_file=None):
   71             """
   72             Mock return value for __salt__.
   73             """
   74             if config_file is None:
   75                 return {name: "0"}
   76             return [""]
   77 
   78         with patch.dict(sysctl.__opts__, {"test": True}):
   79             with patch.dict(sysctl.__salt__, {"sysctl.show": mock_current}):
   80                 self.assertDictEqual(sysctl.present(name, value), ret)
   81 
   82     def test_not_to_be_changed_not_configured(self):
   83         """
   84         Test sysctl.present for a sysctl that isn't in the config file but
   85         already has the correct value
   86         """
   87         name = "some.unknown.oid"
   88         value = "1"
   89         comment = (
   90             "Sysctl value is currently set on the running system but "
   91             "not in a config file. Sysctl option {} set to be "
   92             "changed to 1 in config file.".format(name)
   93         )
   94 
   95         ret = {"name": name, "result": None, "changes": {}, "comment": comment}
   96 
   97         def mock_current(config_file=None):
   98             if config_file is None:
   99                 return {name: "1"}
  100             return [""]
  101 
  102         with patch.dict(sysctl.__opts__, {"test": True}):
  103             with patch.dict(sysctl.__salt__, {"sysctl.show": mock_current}):
  104                 self.assertDictEqual(sysctl.present(name, value), ret)
  105 
  106     def test_configured_but_unknown(self):
  107         """
  108         Test sysctl.present for a sysctl that is already configured but is
  109         not known by the system.  For example, a sysctl used by a kernel module
  110         that isn't loaded.
  111         """
  112         name = "vfs.usermount"
  113         value = "1"
  114         comment = (
  115             "Sysctl value {0} is present in configuration file but is not "
  116             "present in the running config. The value {0} is set to be "
  117             "changed to {1}".format(name, value)
  118         )
  119         ret = {"name": name, "result": None, "changes": {}, "comment": comment}
  120 
  121         def mock_config(config_file=None):
  122             if config_file is None:
  123                 return {}
  124             return {name: 1}
  125 
  126         with patch.dict(sysctl.__opts__, {"test": True}):
  127             with patch.dict(sysctl.__salt__, {"sysctl.show": mock_config}):
  128                 self.assertDictEqual(sysctl.present(name, value), ret)
  129 
  130     def test_no_change(self):
  131         """
  132         Test sysctl.present for an already-configured value
  133         """
  134         name = "vfs.usermount"
  135         value = "1"
  136         comment = "Sysctl value {} = {} is already set".format(name, value)
  137         ret = {"name": name, "result": True, "changes": {}, "comment": comment}
  138 
  139         def mock_both(config_file=None):
  140             if config_file is None:
  141                 return {name: value}
  142             return {name: value}
  143 
  144         with patch.dict(sysctl.__opts__, {"test": True}):
  145             mock = MagicMock(return_value=value)
  146             with patch.dict(
  147                 sysctl.__salt__, {"sysctl.show": mock_both, "sysctl.get": mock}
  148             ):
  149                 self.assertDictEqual(sysctl.present(name, value), ret)
  150 
  151     def test_change(self):
  152         """
  153         Test sysctl.present for a value whose configuration must change
  154         """
  155         name = "vfs.usermount"
  156         value = "1"
  157         comment = "Sysctl option {} would be changed to {}".format(name, value)
  158         ret = {"name": name, "result": None, "changes": {}, "comment": comment}
  159 
  160         def mock_both(config_file=None):
  161             if config_file is None:
  162                 return {name: "2"}
  163             return {name: "2"}
  164 
  165         with patch.dict(sysctl.__opts__, {"test": True}):
  166             mock = MagicMock(return_value="2")
  167             with patch.dict(
  168                 sysctl.__salt__, {"sysctl.show": mock_both, "sysctl.get": mock}
  169             ):
  170                 self.assertDictEqual(sysctl.present(name, value), ret)
  171 
  172     def test_failed_to_set(self):
  173         """
  174         Test sysctl.present when the sysctl command fails to change a value
  175         """
  176         name = "net.isr.maxthreads"
  177         value = "8"
  178         comment = "Failed to set {} to {}: ".format(name, value)
  179         ret = {"name": name, "result": False, "changes": {}, "comment": comment}
  180 
  181         with patch.dict(sysctl.__opts__, {"test": False}):
  182             mock = MagicMock(side_effect=CommandExecutionError)
  183             with patch.dict(sysctl.__salt__, {"sysctl.persist": mock}):
  184                 self.assertDictEqual(sysctl.present(name, value), ret)
  185 
  186     def test_already_set(self):
  187         """
  188         Test sysctl.present when the value is already set
  189         """
  190         name = "vfs.usermount"
  191         value = "1"
  192         comment = "Sysctl value {} = {} is already set".format(name, value)
  193         ret = {"name": name, "result": True, "changes": {}, "comment": comment}
  194         with patch.dict(sysctl.__opts__, {"test": False}):
  195             mock = MagicMock(return_value="Already set")
  196             with patch.dict(sysctl.__salt__, {"sysctl.persist": mock}):
  197                 self.assertDictEqual(sysctl.present(name, value), ret)
  198 
  199     def test_updated(self):
  200         """
  201         Test sysctl.present when the value is not already set
  202         """
  203         name = "vfs.usermount"
  204         value = "1"
  205         comment = "Updated sysctl value {} = {}".format(name, value)
  206         changes = {name: value}
  207         ret = {"name": name, "result": True, "changes": changes, "comment": comment}
  208         with patch.dict(sysctl.__opts__, {"test": False}):
  209             mock = MagicMock(return_value="Updated")
  210             with patch.dict(sysctl.__salt__, {"sysctl.persist": mock}):
  211                 self.assertDictEqual(sysctl.present(name, value), ret)