From d34153a61e3c276c3b1bf94cbc2cf535d79434ee Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Thu, 2 Mar 2017 22:09:06 -0600 Subject: [PATCH] Re-enable ssh retry tests with isolation fixes (#22221) * Re-enable ssh retry tests with isolation fixes * Don't use mock, use monkeypatch instead --- test/units/plugins/connection/test_ssh.py | 50 ++++++++++++++--------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/test/units/plugins/connection/test_ssh.py b/test/units/plugins/connection/test_ssh.py index 92dbf86d6a7e2c..2b0703fa7b8ca4 100644 --- a/test/units/plugins/connection/test_ssh.py +++ b/test/units/plugins/connection/test_ssh.py @@ -516,8 +516,12 @@ def test_pasword_without_data(self): @pytest.mark.usefixtures('mock_run_env') class TestSSHConnectionRetries(object): - @pytest.mark.skip('test does not pass with pytest --boxed') - def test_retry_then_success(self): + def test_retry_then_success(self, monkeypatch): + monkeypatch.setattr(C, 'HOST_KEY_CHECKING', False) + monkeypatch.setattr(C, 'ANSIBLE_SSH_RETRIES', 3) + + monkeypatch.setattr('time.sleep', lambda x: None) + self.mock_popen_res.stdout.read.side_effect = [b"", b"my_stdout\n", b"second_line"] self.mock_popen_res.stderr.read.side_effect = [b"", b"my_stderr"] type(self.mock_popen_res).returncode = PropertyMock(side_effect=[255] * 3 + [0] * 4) @@ -541,9 +545,11 @@ def test_retry_then_success(self): assert b_stdout == b'my_stdout\nsecond_line' assert b_stderr == b'my_stderr' - @patch('time.sleep') - def test_multiple_failures(self, mock_sleep): - C.ANSIBLE_SSH_RETRIES = 9 + def test_multiple_failures(self, monkeypatch): + monkeypatch.setattr(C, 'HOST_KEY_CHECKING', False) + monkeypatch.setattr(C, 'ANSIBLE_SSH_RETRIES', 9) + + monkeypatch.setattr('time.sleep', lambda x: None) self.mock_popen_res.stdout.read.side_effect = [b""] * 11 self.mock_popen_res.stderr.read.side_effect = [b""] * 11 @@ -562,9 +568,11 @@ def test_multiple_failures(self, mock_sleep): pytest.raises(AnsibleConnectionFailure, self.conn.exec_command, 'ssh', 'some data') assert self.mock_popen.call_count == 10 - @patch('time.sleep') - def test_abitrary_exceptions(self, mock_sleep): - C.ANSIBLE_SSH_RETRIES = 9 + def test_abitrary_exceptions(self, monkeypatch): + monkeypatch.setattr(C, 'HOST_KEY_CHECKING', False) + monkeypatch.setattr(C, 'ANSIBLE_SSH_RETRIES', 9) + + monkeypatch.setattr('time.sleep', lambda x: None) self.conn._build_command = MagicMock() self.conn._build_command.return_value = 'ssh' @@ -573,11 +581,12 @@ def test_abitrary_exceptions(self, mock_sleep): pytest.raises(Exception, self.conn.exec_command, 'ssh', 'some data') assert self.mock_popen.call_count == 10 - @patch('time.sleep') - @patch('ansible.plugins.connection.ssh.os') - @pytest.mark.skip('test does not pass with pytest --boxed') - def test_put_file_retries(self, os_mock, time_mock): - os_mock.path.exists.return_value = True + def test_put_file_retries(self, monkeypatch): + monkeypatch.setattr(C, 'HOST_KEY_CHECKING', False) + monkeypatch.setattr(C, 'ANSIBLE_SSH_RETRIES', 3) + + monkeypatch.setattr('time.sleep', lambda x: None) + monkeypatch.setattr('ansible.plugins.connection.ssh.os.path.exists', lambda x: True) self.mock_popen_res.stdout.read.side_effect = [b"", b"my_stdout\n", b"second_line"] self.mock_popen_res.stderr.read.side_effect = [b"", b"my_stderr"] @@ -595,7 +604,7 @@ def test_put_file_retries(self, os_mock, time_mock): self.mock_selector.get_map.side_effect = lambda: True self.conn._build_command = MagicMock() - self.conn._build_command.return_value = 'ssh' + self.conn._build_command.return_value = 'sftp' return_code, b_stdout, b_stderr = self.conn.put_file('/path/to/in/file', '/path/to/dest/file') assert return_code == 0 @@ -603,11 +612,12 @@ def test_put_file_retries(self, os_mock, time_mock): assert b_stderr == b"my_stderr" assert self.mock_popen.call_count == 2 - @patch('time.sleep') - @patch('ansible.plugins.connection.ssh.os') - @pytest.mark.skip('test does not pass with pytest --boxed') - def test_fetch_file_retries(self, os_mock, time_mock): - os_mock.path.exists.return_value = True + def test_fetch_file_retries(self, monkeypatch): + monkeypatch.setattr(C, 'HOST_KEY_CHECKING', False) + monkeypatch.setattr(C, 'ANSIBLE_SSH_RETRIES', 3) + + monkeypatch.setattr('time.sleep', lambda x: None) + monkeypatch.setattr('ansible.plugins.connection.ssh.os.path.exists', lambda x: True) self.mock_popen_res.stdout.read.side_effect = [b"", b"my_stdout\n", b"second_line"] self.mock_popen_res.stderr.read.side_effect = [b"", b"my_stderr"] @@ -625,7 +635,7 @@ def test_fetch_file_retries(self, os_mock, time_mock): self.mock_selector.get_map.side_effect = lambda: True self.conn._build_command = MagicMock() - self.conn._build_command.return_value = 'ssh' + self.conn._build_command.return_value = 'sftp' return_code, b_stdout, b_stderr = self.conn.fetch_file('/path/to/in/file', '/path/to/dest/file') assert return_code == 0