From ffb015f0f6b7c927dad5ad7bfeed601839e3295b Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Fri, 28 Apr 2023 06:32:20 +0100 Subject: [PATCH 1/2] Add missing changelog entries for the work done in #64113 Signed-off-by: Pedro Algarvio --- changelog/64111.fixed.md | 1 + changelog/64113.fixed.md | 2 ++ 2 files changed, 3 insertions(+) create mode 100644 changelog/64111.fixed.md create mode 100644 changelog/64113.fixed.md diff --git a/changelog/64111.fixed.md b/changelog/64111.fixed.md new file mode 100644 index 000000000000..a6c00a1b9994 --- /dev/null +++ b/changelog/64111.fixed.md @@ -0,0 +1 @@ +Disable class level caching of the file client on `SaltCacheLoader` and properly use context managers to take care of initialization and termination of the file client. diff --git a/changelog/64113.fixed.md b/changelog/64113.fixed.md new file mode 100644 index 000000000000..b2a530eeb3d1 --- /dev/null +++ b/changelog/64113.fixed.md @@ -0,0 +1,2 @@ +Fixed several file client uses which were not properly terminating it by switching to using it as a context manager +whenever possible or making sure `.destroy()` was called when using a context manager was not possible. From eef70b2d590e5c6f65d99e6fca17e0779eea62f2 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Fri, 28 Apr 2023 07:24:58 +0100 Subject: [PATCH 2/2] Add test case to assert that `salt.client.ssh.SSH.fsclient.destroy()` is called. Signed-off-by: Pedro Algarvio --- changelog/64184.fixed.md | 1 + salt/cli/ssh.py | 5 ++++- tests/pytests/unit/cli/test_ssh.py | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 changelog/64184.fixed.md create mode 100644 tests/pytests/unit/cli/test_ssh.py diff --git a/changelog/64184.fixed.md b/changelog/64184.fixed.md new file mode 100644 index 000000000000..c63583324e36 --- /dev/null +++ b/changelog/64184.fixed.md @@ -0,0 +1 @@ + Make sure the `salt-ssh` CLI calls it's `fsclient.destroy()` method when done. diff --git a/salt/cli/ssh.py b/salt/cli/ssh.py index 6048cb5f58fb..78522a044a9b 100644 --- a/salt/cli/ssh.py +++ b/salt/cli/ssh.py @@ -16,4 +16,7 @@ def run(self): self.parse_args() ssh = salt.client.ssh.SSH(self.config) - ssh.run() + try: + ssh.run() + finally: + ssh.fsclient.destroy() diff --git a/tests/pytests/unit/cli/test_ssh.py b/tests/pytests/unit/cli/test_ssh.py new file mode 100644 index 000000000000..3cc4a5c04192 --- /dev/null +++ b/tests/pytests/unit/cli/test_ssh.py @@ -0,0 +1,16 @@ +from salt.cli.ssh import SaltSSH +from tests.support.mock import MagicMock, call, patch + + +def test_fsclient_destroy_called(minion_opts): + """ + Test that `salt.client.ssh.SSH.fsclient.destroy()` is called. + """ + ssh_mock = MagicMock() + with patch( + "salt.utils.parsers.SaltSSHOptionParser.parse_args", return_value=MagicMock() + ), patch("salt.client.ssh.SSH", return_value=ssh_mock): + parser = SaltSSH() + parser.config = minion_opts + parser.run() + assert ssh_mock.fsclient.mock_calls == [call.destroy()]