diff --git a/changelog/61966.fixed b/changelog/61966.fixed new file mode 100644 index 00000000000..e772fcf33af --- /dev/null +++ b/changelog/61966.fixed @@ -0,0 +1 @@ +Fixed listing minions on OpenBSD diff --git a/salt/utils/network.py b/salt/utils/network.py index 6cc73bad44f..55b360411ab 100644 --- a/salt/utils/network.py +++ b/salt/utils/network.py @@ -1928,11 +1928,11 @@ def _openbsd_remotes_on(port, which_end): data = subprocess.check_output( ["netstat", "-nf", "inet"] ) # pylint: disable=minimum-python-version - except subprocess.CalledProcessError: - log.error("Failed netstat") + except subprocess.CalledProcessError as exc: + log.error('Failed "netstat" with returncode = %s', exc.returncode) raise - lines = data.split("\n") + lines = salt.utils.stringutils.to_str(data).split("\n") for line in lines: if "ESTABLISHED" not in line: continue diff --git a/tests/unit/utils/test_network.py b/tests/unit/utils/test_network.py index 3270fcbc2ab..f7d39729300 100644 --- a/tests/unit/utils/test_network.py +++ b/tests/unit/utils/test_network.py @@ -111,6 +111,12 @@ USER COMMAND PID FD PROTO LOCAL ADDRESS FOREIGN ADDRESS salt-master python2.781106 35 tcp4 127.0.0.1:61115 127.0.0.1:4506 """ +OPENBSD_NETSTAT = """\ +Active Internet connections +Proto Recv-Q Send-Q Local Address Foreign Address (state) +tcp 0 0 127.0.0.1.61115 127.0.0.1.4506 ESTABLISHED +""" + LINUX_NETLINK_SS_OUTPUT = """\ State Recv-Q Send-Q Local Address:Port Peer Address:Port TIME-WAIT 0 0 [::1]:8009 [::1]:40368 @@ -647,6 +653,20 @@ class NetworkTestCase(TestCase): remotes = network._netlink_tool_remote_on("4505", "remote_port") self.assertEqual(remotes, {"127.0.0.1", "::ffff:1.2.3.4"}) + def test_openbsd_remotes_on(self): + with patch("subprocess.check_output", return_value=OPENBSD_NETSTAT): + remotes = network._openbsd_remotes_on("4506", "remote") + self.assertEqual(remotes, {"127.0.0.1"}) + + def test_openbsd_remotes_on_issue_61966(self): + """ + Test that the command output is correctly converted to string before + treating it as such + """ + with patch("subprocess.check_output", return_value=OPENBSD_NETSTAT.encode()): + remotes = network._openbsd_remotes_on("4506", "remote") + self.assertEqual(remotes, {"127.0.0.1"}) + def test_generate_minion_id_distinct(self): """ Test if minion IDs are distinct in the pool.