Skip to content

Commit

Permalink
Don't run safety checks in null/implicit guardrail mode
Browse files Browse the repository at this point in the history
  • Loading branch information
bitprophet committed Aug 6, 2023
1 parent a418d0d commit e83d660
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
7 changes: 7 additions & 0 deletions fabric/testing/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ def __init__(
"You can't give both 'commands' and individual "
"Command parameters!"
) # noqa
# Early test for "did user actually request expectations?"
self.guard_only = not (commands or cmd or transfers)
# Fill in values
self.host = host
self.user = user
Expand Down Expand Up @@ -304,6 +306,11 @@ def sanity_check(self):
return self.safety_check()

def safety_check(self):
# Short-circuit if user didn't give any expectations; otherwise our
# assumptions below will be inaccurately violated and explode.
if self.guard_only:
return

# Per-session we expect a single transport get
transport = self.client.get_transport
transport.assert_called_once_with()
Expand Down
22 changes: 21 additions & 1 deletion tests/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,34 @@
https://en.wikipedia.org/wiki/Buffalo_buffalo_Buffalo_buffalo_buffalo_buffalo_Buffalo_buffalo)
"""

from unittest.mock import Mock
from unittest.mock import Mock, patch

from fabric import Connection
from fabric.testing.base import MockRemote
from pytest import raises


class MockRemote_:
class mocks_by_default:
@patch("paramiko.transport.socket")
def prevents_real_ssh_connectivity_via_paramiko_guts(self, socket):
with MockRemote():
cxn = Connection(host="host")
cxn.run("nope", in_stream=False)
# High level mock...
assert isinstance(cxn.client, Mock)
# ...prevented low level connectivity (would have been at least
# one socket.socket() and one .connect() on result of same)
assert not socket.mock_calls

def does_not_run_safety_checks_when_nothing_really_expected(self):
with MockRemote():
cxn = Connection(host="host")
assert isinstance(cxn.client, Mock)
# NOTE: no run() or etc!
# Would explode with old behavior due to always asserting transport
# and connect method calls.

class contextmanager_behavior:
def calls_safety_and_stop_on_exit_with_try_finally(self):
mr = MockRemote()
Expand Down

0 comments on commit e83d660

Please sign in to comment.