Skip to content

Commit b1e7710

Browse files
authored
Merge pull request #5812 from xenserver-next/extauth-ad-fix-byte-logging-found-by-mypy
2 parents b658808 + 3317717 commit b1e7710

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

python3/plugins/extauth-hook-AD.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,14 @@ def setup_logger():
6161
logger = logging.getLogger(__name__)
6262

6363

64-
def run_cmd(cmd, log_cmd=True):
65-
"""Helper function to run command"""
64+
def run_cmd(command: "list[str]"):
65+
"""Helper function to run a command and log the output"""
6666
try:
67-
result = subprocess.check_output(cmd)
68-
if log_cmd:
69-
msg = "{} -> {}".format(cmd, result)
70-
logger.debug(msg)
71-
return result.strip()
72-
except Exception: # pylint: disable=broad-except
73-
logger.exception("Failed to run command %s", cmd)
74-
return None
67+
output = subprocess.check_output(command, universal_newlines=True)
68+
logger.debug("%s -> %s", command, output.strip())
69+
70+
except OSError:
71+
logger.exception("Failed to run command %s", command)
7572

7673

7774
class ADBackend(Enum):

python3/plugins/test_extauth_hook_AD.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
11
"""
22
Test module for extauth_hook_ad
33
"""
4-
#pylint: disable=invalid-name
5-
import sys
4+
5+
import logging
66
import os
7+
import sys
78
from unittest import TestCase
8-
from mock import MagicMock, patch
9-
10-
import pytest
9+
from unittest.mock import MagicMock, patch
1110

1211
# mock modules to avoid dependencies
1312
sys.modules["XenAPIPlugin"] = MagicMock()
1413
sys.modules["XenAPI"] = MagicMock()
1514
# pylint: disable=wrong-import-position
1615
# Import must after mock modules
1716
from extauth_hook_ad import StaticSSHPam, NssConfig, SshdConfig, UsersList, GroupsList
17+
from extauth_hook_ad import run_cmd
18+
19+
def test_run_cmd(caplog):
20+
"""Assert the current buggy behavior of the run_cmd function after py3 migration"""
21+
cmd = ["echo", " Hello World! "]
1822

23+
# Call the function under test, check the return value and capture the log message
24+
with caplog.at_level(logging.DEBUG):
25+
assert run_cmd(cmd) is None # The return value is None (not used in the code)
1926

20-
if sys.version_info < (3, ): # pragma: no cover
21-
pytest.skip(allow_module_level=True)
27+
# Assert the log message
28+
assert caplog.records[0].message == "%s -> Hello World!" % (cmd)
2229

30+
# Test the case where the command fails:
31+
assert run_cmd(["bad command"]) is None
32+
assert caplog.records[1].message == "Failed to run command ['bad command']"
2333

2434
def line_exists_in_config(lines, line):
2535
"""

0 commit comments

Comments
 (0)