Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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 == {}
Original file line number Diff line number Diff line change
Expand Up @@ -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 == []
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand All @@ -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):
Expand Down