diff --git a/usr/lib/python3/dist-packages/linuxmusterTools/lmnconfig/pytests/test_samba.py b/usr/lib/python3/dist-packages/linuxmusterTools/lmnconfig/pytests/test_samba.py index 4de8f77..c848867 100644 --- a/usr/lib/python3/dist-packages/linuxmusterTools/lmnconfig/pytests/test_samba.py +++ b/usr/lib/python3/dist-packages/linuxmusterTools/lmnconfig/pytests/test_samba.py @@ -12,6 +12,10 @@ independent of any of that, so it is fully covered below. """ +import importlib +from subprocess import CalledProcessError + +import linuxmusterTools.lmnconfig.samba as samba_module from linuxmusterTools.lmnconfig.samba import parse_log_level @@ -46,3 +50,35 @@ def test_last_bare_int_wins_for_general(): def test_extra_whitespace_is_ignored(): result = parse_log_level(' 1 auth_audit:2 ') assert result == {'general': 1, 'auth_audit': 2} + + +# --------------------------------------------------------------------------- +# Import without a Samba installation +# --------------------------------------------------------------------------- + +def _reload_samba_with_check_output(monkeypatch, raising): + """Re-execute samba.py with a check_output that fails the given way.""" + import subprocess + + monkeypatch.setattr(subprocess, 'check_output', raising) + return importlib.reload(samba_module) + + +def test_import_survives_missing_net_binary(monkeypatch): + def _no_binary(*args, **kwargs): + raise FileNotFoundError(2, 'No such file or directory', '/usr/bin/net') + + reloaded = _reload_samba_with_check_output(monkeypatch, _no_binary) + + assert reloaded.SHARES_LIST == [] + assert reloaded.DFS == {} + + +def test_import_survives_failing_net_call(monkeypatch): + def _fails(*args, **kwargs): + raise CalledProcessError(1, ['/usr/bin/net', 'conf', 'list']) + + reloaded = _reload_samba_with_check_output(monkeypatch, _fails) + + assert reloaded.SHARES_LIST == [] + assert reloaded.DFS == {} diff --git a/usr/lib/python3/dist-packages/linuxmusterTools/lmnconfig/pytests/test_sophomorix.py b/usr/lib/python3/dist-packages/linuxmusterTools/lmnconfig/pytests/test_sophomorix.py index 141eb8a..2ef1b8c 100644 --- a/usr/lib/python3/dist-packages/linuxmusterTools/lmnconfig/pytests/test_sophomorix.py +++ b/usr/lib/python3/dist-packages/linuxmusterTools/lmnconfig/pytests/test_sophomorix.py @@ -234,3 +234,22 @@ def test_sophomorix_conf_file_found_reads_data_attribute(monkeypatch, fake_lmnfi # Note: SophomorixConf reads config.data (not config.read()), unlike the # other classes in this module. assert config.data == canned + + +# --------------------------------------------------------------------------- +# SophomorixIni without a sophomorix installation +# --------------------------------------------------------------------------- + +def test_sophomorix_ini_without_role_user_section_gives_empty_roles(monkeypatch): + # ConfigParser.read() silently ignores a missing file, so an unconfigured + # machine leaves the parser with nothing but the DEFAULT section. Reading + # ROLE_USER unguarded raised KeyError and made the whole package + # unimportable off-server. + monkeypatch.setattr( + sophomorix_module.ConfigParser, 'read', lambda self, *args, **kwargs: [] + ) + + ini = SophomorixIni() + + assert ini.userrole == [] + assert ini.computerrole == [] diff --git a/usr/lib/python3/dist-packages/linuxmusterTools/lmnconfig/samba.py b/usr/lib/python3/dist-packages/linuxmusterTools/lmnconfig/samba.py index b1d907a..ec7eabb 100644 --- a/usr/lib/python3/dist-packages/linuxmusterTools/lmnconfig/samba.py +++ b/usr/lib/python3/dist-packages/linuxmusterTools/lmnconfig/samba.py @@ -2,7 +2,7 @@ import os from configparser import ConfigParser from configobj import ConfigObj -from subprocess import check_output +from subprocess import CalledProcessError, check_output from io import StringIO @@ -57,7 +57,11 @@ def parse_log_level(level): DFS = {} -config = ConfigObj(StringIO(check_output(["/usr/bin/net", "conf", "list"], shell=False).decode())) +try: + config = ConfigObj(StringIO(check_output(["/usr/bin/net", "conf", "list"], shell=False).decode())) +except (OSError, CalledProcessError) as e: + logger.error(f"Can not read the samba share configuration: {str(e)}. Is linuxmuster.net installed and configured ?") + config = ConfigObj() SHARES_LIST = list(config.keys()) diff --git a/usr/lib/python3/dist-packages/linuxmusterTools/lmnconfig/sophomorix.py b/usr/lib/python3/dist-packages/linuxmusterTools/lmnconfig/sophomorix.py index a25bce9..2993f8d 100644 --- a/usr/lib/python3/dist-packages/linuxmusterTools/lmnconfig/sophomorix.py +++ b/usr/lib/python3/dist-packages/linuxmusterTools/lmnconfig/sophomorix.py @@ -36,6 +36,8 @@ class SophomorixIni: def __init__(self): self.path = "/usr/share/sophomorix/devel/sophomorix.ini" self.data = ConfigParser(delimiters=("=",), dict_type=MultiOrderedDict, strict=False) + if not os.path.isfile(self.path): + logger.warning(f"No sophomorix ini found at {self.path}. Is sophomorix installed and configured ?") self.data.read(self.path) self.sections = list(self.data.keys()) @@ -56,7 +58,7 @@ def __init__(self): 'thinclient', 'iponly', ] - self.userrole = list(self.dict['ROLE_USER'].keys()) + self.userrole = list(self.dict.get('ROLE_USER', {}).keys()) @staticmethod def sanitize(value):