Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save the original subprocess.Popen before mocking it #2340

Merged
merged 1 commit into from
Aug 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions tests/ga/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from datetime import datetime, timedelta
from threading import currentThread

_ORIGINAL_POPEN = subprocess.Popen

from mock import PropertyMock

from azurelinuxagent.common import conf
Expand Down Expand Up @@ -1105,7 +1107,7 @@ def _test_run_latest(self, mock_child=None, mock_time=None, child_args=None):
if mock_time is None:
mock_time = TimeMock()

with patch('subprocess.Popen', return_value=mock_child) as mock_popen:
with patch('azurelinuxagent.ga.update.subprocess.Popen', return_value=mock_child) as mock_popen:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't change the behavior of the tests, but is more explicit as to what we intent to mock

with patch('time.time', side_effect=mock_time.time):
with patch('time.sleep', side_effect=mock_time.sleep):
self.update_handler.run_latest(child_args=child_args)
Expand Down Expand Up @@ -1596,14 +1598,13 @@ def test_it_should_recreate_handler_env_on_service_startup(self):
"{0} not found in HandlerEnv file".format(HandlerEnvironment.eventsFolder))

def test_it_should_not_setup_persistent_firewall_rules_if_EnableFirewall_is_disabled(self):
original_popen = subprocess.Popen
executed_firewall_commands = []

def _mock_popen(cmd, *args, **kwargs):
if 'firewall-cmd' in cmd:
executed_firewall_commands.append(cmd)
cmd = ["echo", "running"]
return original_popen(cmd, *args, **kwargs)
return _ORIGINAL_POPEN(cmd, *args, **kwargs)

with patch("azurelinuxagent.common.logger.info") as patch_info:
with self._get_update_handler(iterations=1) as (update_handler, _):
Expand All @@ -1618,14 +1619,13 @@ def _mock_popen(cmd, *args, **kwargs):

def test_it_should_setup_persistent_firewall_rules_on_startup(self):
iterations = 1
original_popen = subprocess.Popen
executed_commands = []

def _mock_popen(cmd, *args, **kwargs):
if 'firewall-cmd' in cmd:
executed_commands.append(cmd)
cmd = ["echo", "running"]
return original_popen(cmd, *args, **kwargs)
return _ORIGINAL_POPEN(cmd, *args, **kwargs)

with self._get_update_handler(iterations) as (update_handler, _):
with patch("azurelinuxagent.common.utils.shellutil.subprocess.Popen", side_effect=_mock_popen):
Expand Down