Skip to content

Commit

Permalink
Re-enable ssh retry tests with isolation fixes (ansible#22221)
Browse files Browse the repository at this point in the history
* Re-enable ssh retry tests with isolation fixes
* Don't use mock, use monkeypatch instead
  • Loading branch information
sivel authored and mattclay committed Mar 3, 2017
1 parent 6c8025c commit d34153a
Showing 1 changed file with 30 additions and 20 deletions.
50 changes: 30 additions & 20 deletions test/units/plugins/connection/test_ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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'
Expand All @@ -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"]
Expand All @@ -595,19 +604,20 @@ 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
assert b_stdout == b"my_stdout\nsecond_line"
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"]
Expand All @@ -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
Expand Down

0 comments on commit d34153a

Please sign in to comment.