From 95500a95af72d9d80e6451172172d7de8dcefff2 Mon Sep 17 00:00:00 2001 From: Thomas Phipps Date: Wed, 26 Apr 2023 00:21:49 +0000 Subject: [PATCH] fix #64082 --- changelog/64082.fixed.md | 1 + salt/modules/cryptdev.py | 2 +- tests/pytests/unit/modules/test_cryptdev.py | 38 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 changelog/64082.fixed.md create mode 100644 tests/pytests/unit/modules/test_cryptdev.py diff --git a/changelog/64082.fixed.md b/changelog/64082.fixed.md new file mode 100644 index 00000000000..c5bbc5a0ccb --- /dev/null +++ b/changelog/64082.fixed.md @@ -0,0 +1 @@ +Fix dmsetup device names with hyphen being picked up. diff --git a/salt/modules/cryptdev.py b/salt/modules/cryptdev.py index f4d24e5227f..40c28d17f10 100644 --- a/salt/modules/cryptdev.py +++ b/salt/modules/cryptdev.py @@ -113,7 +113,7 @@ def active(): ret = {} # TODO: This command should be extended to collect more information, such as UUID. devices = __salt__["cmd.run_stdout"]("dmsetup ls --target crypt") - out_regex = re.compile(r"(?P\w+)\W+\((?P\d+), (?P\d+)\)") + out_regex = re.compile(r"(?P\S+)\s+\((?P\d+), (?P\d+)\)") log.debug(devices) for line in devices.split("\n"): diff --git a/tests/pytests/unit/modules/test_cryptdev.py b/tests/pytests/unit/modules/test_cryptdev.py new file mode 100644 index 00000000000..c5932b5f369 --- /dev/null +++ b/tests/pytests/unit/modules/test_cryptdev.py @@ -0,0 +1,38 @@ +import pytest + +import salt.modules.cryptdev as cryptdev +from tests.support.mock import MagicMock, patch + + +@pytest.fixture +def configure_loader_modules(minion_opts): + return {cryptdev: {"__opts__": minion_opts}} + + +def test_active(caplog): + with patch.dict( + cryptdev.__salt__, + {"cmd.run_stdout": MagicMock(return_value="my-device (253, 1)\n")}, + ): + assert cryptdev.active() == { + "my-device": { + "devname": "my-device", + "major": "253", + "minor": "1", + } + } + + # debien output when no devices setup. + with patch.dict(cryptdev.__salt__, {"cmd.run_stdout": MagicMock(return_value="")}): + caplog.clear() + assert cryptdev.active() == {} + assert "dmsetup output does not match expected format" in caplog.text + + # centos output of dmsetup when no devices setup. + with patch.dict( + cryptdev.__salt__, + {"cmd.run_stdout": MagicMock(return_value="No devices found")}, + ): + caplog.clear() + assert cryptdev.active() == {} + assert "dmsetup output does not match expected format" in caplog.text