From ad3735581379e5d4bbc7baef3eaa4a1b8387ccbf Mon Sep 17 00:00:00 2001 From: Victor Zhestkov Date: Tue, 12 Apr 2022 10:06:43 +0300 Subject: [PATCH] Prevent affection of SSH.opts with LazyLoader (bsc#1197637) * Prevent affection SSH.opts with LazyLoader * Restore parsed targets * Fix test_ssh unit tests --- salt/client/ssh/__init__.py | 19 +++++++++++-------- tests/unit/client/test_ssh.py | 18 +++++++++--------- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/salt/client/ssh/__init__.py b/salt/client/ssh/__init__.py index 8ae417f575..fe1213b723 100644 --- a/salt/client/ssh/__init__.py +++ b/salt/client/ssh/__init__.py @@ -224,15 +224,16 @@ class SSH(MultiprocessingStateMixin): ROSTER_UPDATE_FLAG = "#__needs_update" def __init__(self, opts, context=None): + self.opts = copy.deepcopy(opts) + self.sopts = copy.deepcopy(self.opts) self.__parsed_rosters = {SSH.ROSTER_UPDATE_FLAG: True} - pull_sock = os.path.join(opts["sock_dir"], "master_event_pull.ipc") + pull_sock = os.path.join(self.opts["sock_dir"], "master_event_pull.ipc") if os.path.exists(pull_sock) and zmq: self.event = salt.utils.event.get_event( - "master", opts["sock_dir"], opts=opts, listen=False + "master", self.opts["sock_dir"], opts=self.opts, listen=False ) else: self.event = None - self.opts = opts if self.opts["regen_thin"]: self.opts["ssh_wipe"] = True if not salt.utils.path.which("ssh"): @@ -243,7 +244,7 @@ class SSH(MultiprocessingStateMixin): " to run. Exiting." ), ) - self.opts["_ssh_version"] = ssh_version() + self.sopts["_ssh_version"] = ssh_version() self.tgt_type = ( self.opts["selected_target_option"] if self.opts["selected_target_option"] @@ -339,6 +340,9 @@ class SSH(MultiprocessingStateMixin): self.opts["cachedir"], "salt-ssh.session.lock" ) self.ssh_session_grace_time = int(self.opts.get("ssh_session_grace_time", 1)) + self.sopts["tgt"] = copy.deepcopy(self.opts["tgt"]) + self.sopts["ssh_cli_tgt"] = copy.deepcopy(self.opts["ssh_cli_tgt"]) + self.opts = self.sopts # __setstate__ and __getstate__ are only used on spawning platforms. def __setstate__(self, state): @@ -607,7 +611,6 @@ class SSH(MultiprocessingStateMixin): Spin up the needed threads or processes and execute the subsequent routines """ - opts = copy.deepcopy(self.opts) que = multiprocessing.Queue() running = {} targets_queue = deque(self.targets.keys()) @@ -618,7 +621,7 @@ class SSH(MultiprocessingStateMixin): if not self.targets: log.error("No matching targets found in roster.") break - if len(running) < opts.get("ssh_max_procs", 25) and not init: + if len(running) < self.opts.get("ssh_max_procs", 25) and not init: if targets_queue: host = targets_queue.popleft() else: @@ -682,7 +685,7 @@ class SSH(MultiprocessingStateMixin): continue args = ( que, - opts, + self.opts, host, self.targets[host], mine, @@ -776,7 +779,7 @@ class SSH(MultiprocessingStateMixin): if len(rets) >= len(self.targets): break # Sleep when limit or all threads started - if len(running) >= opts.get("ssh_max_procs", 25) or len( + if len(running) >= self.opts.get("ssh_max_procs", 25) or len( self.targets ) >= len(running): time.sleep(0.1) diff --git a/tests/unit/client/test_ssh.py b/tests/unit/client/test_ssh.py index 00313ed55f..92a9314149 100644 --- a/tests/unit/client/test_ssh.py +++ b/tests/unit/client/test_ssh.py @@ -95,7 +95,7 @@ class SSHReturnEventTests(ShellCase): assert "localhost" in ret assert "fun" in ret["localhost"] client.run() - display_output.assert_called_once_with(expected, "nested", opts) + display_output.assert_called_once_with(expected, "nested", client.opts) self.assertIs(ret, handle_ssh_ret[0]) assert len(client.event.fire_event.call_args_list) == 2 assert "fun" in client.event.fire_event.call_args_list[0][0][0] @@ -539,7 +539,7 @@ class SSHTests(ShellCase): MagicMock(return_value=salt.utils.yaml.safe_load(self.roster)), ): client._expand_target() - assert opts["tgt"] == host + assert client.opts["tgt"] == host def test_expand_target_no_host(self): """ @@ -564,7 +564,7 @@ class SSHTests(ShellCase): assert opts["tgt"] == user + host with patch("salt.roster.get_roster_file", MagicMock(return_value=roster_file)): client._expand_target() - assert opts["tgt"] == host + assert client.opts["tgt"] == host def test_expand_target_dns(self): """ @@ -587,7 +587,7 @@ class SSHTests(ShellCase): MagicMock(return_value=salt.utils.yaml.safe_load(self.roster)), ): client._expand_target() - assert opts["tgt"] == host + assert client.opts["tgt"] == host def test_expand_target_no_user(self): """ @@ -627,7 +627,7 @@ class SSHTests(ShellCase): client = ssh.SSH(opts) assert opts["tgt"] == user + host client._update_targets() - assert opts["tgt"] == host + assert client.opts["tgt"] == host assert client.targets[host]["user"] == user.split("@")[0] def test_update_targets_dns(self): @@ -645,7 +645,7 @@ class SSHTests(ShellCase): client = ssh.SSH(opts) assert opts["tgt"] == user + host client._update_targets() - assert opts["tgt"] == host + assert client.opts["tgt"] == host assert client.targets[host]["user"] == user.split("@")[0] def test_update_targets_no_user(self): @@ -686,7 +686,7 @@ class SSHTests(ShellCase): ): client._expand_target() client._update_targets() - assert opts["tgt"] == host + assert client.opts["tgt"] == host assert client.targets[host]["user"] == user.split("@")[0] def test_parse_tgt(self): @@ -706,7 +706,7 @@ class SSHTests(ShellCase): client = ssh.SSH(opts) assert client.parse_tgt["hostname"] == host assert client.parse_tgt["user"] == user.split("@")[0] - assert self.opts.get("ssh_cli_tgt") == user + host + assert client.opts.get("ssh_cli_tgt") == user + host def test_parse_tgt_no_user(self): """ @@ -725,7 +725,7 @@ class SSHTests(ShellCase): client = ssh.SSH(opts) assert client.parse_tgt["hostname"] == host assert client.parse_tgt["user"] == opts["ssh_user"] - assert self.opts.get("ssh_cli_tgt") == host + assert client.opts.get("ssh_cli_tgt") == host def test_extra_filerefs(self): """ -- 2.37.3