The test cases for `os_data` do not cover the case that `lsb_release` is
imported successfully. So add a test case for that.
Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
In `os_data()` only the kernel branch for `SunOS` might not set the `os`
grain. To make the code easier to read, ensure that `os` is set in each
branch in `os_data()`.
Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
To make the code easier to read, set `os_family` in each branch in
`os_data()`. Most branches have an hard-coded OS family and can be
dropped from `_OS_FAMILY_MAP`. The dictionary `_OS_FAMILY_MAP` is now
only used for deriving the OS family of Linux.
Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
Move fetching `lsb_distrib_*` grains into separate function
`_linux_lsb_distrib_data` to make `_linux_distribution_data` shorter.
Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
`os_data` has a case-like statement to differentiate the different
operating systems, but this is split into two parts.
To make the code more readable, combine all case-like statement in
`os_data` to use one long if-elif chain.
Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
The `os_data` function is huge (531 lines for one function is too long)
and also deeply nested (8 levels deep). That make reading, modifying,
and testing this function harder.
Split `os_data` into smaller functions to a readable 167 lines with only
a nesting level of three.
Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
* test_linux_sysctl: Reduce indentation level
Reduce indentation level to improve readability.
Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
* test_linux_sysctl: Fix test_persist_no_conf_failure
`test_persist_no_conf_failure` has different behaviour depending on the
operating system it is executed on. Mock all system calls and let it
fail in the first try to write the sysctl configuration file.
Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
* linux_sysctl: Pass parameters as list to cmd.run
Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
* linux_sysctl.show: Fix parsing of sysctl.conf files
The lines in sysctl.conf files can contain leading and trailing spaces.
The equal sign (that separates the key and value) might have zero to
multiple white spaces. Valid example:
```
kernel.core_pattern = |/usr/share/kdump-tools/dump-core %p %s %t %e
# Stop low-level messages on console = less logging
kernel.printk = 3 4 1 3
net.ipv4.ip_forward=1
```
`sysctl.show` currently has two issues:
* all values contain a trailing newline
* Comments with leading spaces and equal sign in them are not skipped
Address both issues and simplify the code and cover them with unit tests.
Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
* linux_sysctl.present: Fix replacing spaces by tabs
`salt/modules/linux_sysctl.py` tries to be smart in `persist()` and
replaces spaces by tabs, which will be correct for keys like
`net.ipv4.tcp_rmem` but not for `kernel.core_pattern`.
So only sanitize the values for comparison, but not for persisting them
in the configuration file.
Bug: https://github.com/saltstack/salt/issues/40054
Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
* linux_sysctl: Drop unreachable code
The check `if "=" not in line` already ensures that the split by `=`
will return two elements.
Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
* fixing failing tests
* need to mock which in test_assign_success
Signed-off-by: Benjamin Drung <benjamin.drung@ionos.com>
Co-authored-by: Benjamin Drung <benjamin.drung@ionos.com>
Co-authored-by: Gareth J. Greenaway <gareth@saltstack.com>
Since the check option in iptables was introduced (https://git.netfilter.org/iptables/commit/?id=d59b9db031abee37a9aa9776662dd15370faabf4) it's safe to use the return code to check if a rule exists or not.
Before this commit salt would check if the return from `iptables --check` was empty, if it was it assumed the rule was there. Since Debian Bullseye `iptables --check` return the active rule breaking `iptables.checks` module.
This commit changes this to return True or False based on the `cmd.run` returncode.
Signed-off-by: Daan van Gorkum <djvg@djvg.net>
Fixes: 60467