Refactor Popen usage to allow overrides

* Add subprocess.Popen wrapper in common
* Change bash.py and smb.py to use wrapper.

Because subprocess.Popen uses os.fork() to spawn child processes, it may
be desirable for a calling application to override subprocess.Popen with
a custom class that implements a different spawning method - such as
posix_spawn. With large-memory applications, posix_spawn may be more
desirable than os.fork() due to memory allocation behavior. This change
allows Satori to be compatible with such a workflow.

Change-Id: Ia60adea27d1a6cbf28341ea3aece5ee604a94583
Closes-Bug: #1412485
This commit is contained in:
Ryan Walker 2015-01-19 11:28:04 -06:00
parent dea382bae1
commit fd350f7f1c
5 changed files with 32 additions and 9 deletions

View File

@ -20,8 +20,8 @@ Execute commands over ssh or using the python subprocess module.
import logging
import shlex
import subprocess
from satori.common import popen
from satori import errors
from satori import smb
from satori import ssh
@ -147,11 +147,11 @@ class LocalShell(ShellMixin):
"""
cwd = kwargs.get('cwd')
with_exit_code = kwargs.get('with_exit_code')
spipe = subprocess.PIPE
spipe = popen.PIPE
cmd = shlex.split(command)
LOG.debug("Executing `%s` on local machine", command)
result = subprocess.Popen(
result = popen.popen(
cmd, stdout=spipe, stderr=spipe, cwd=cwd)
out, err = result.communicate()
resultdict = {

23
satori/common/popen.py Normal file
View File

@ -0,0 +1,23 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
""" Popen wrapper to allow custom patching of subprocess.Popen."""
import subprocess
PIPE = subprocess.PIPE
STDOUT = subprocess.STDOUT
def popen(*args, **kwargs):
"""Wrap Popen to allow for higher level patching if necessary."""
return subprocess.Popen(*args, **kwargs)

View File

@ -26,9 +26,9 @@ import logging
import os
import re
import shlex
import subprocess
import tempfile
from satori.common import popen
from satori import errors
from satori import ssh
from satori import tunnel
@ -199,11 +199,11 @@ class SMBClient(object): # pylint: disable=R0902
self.username,
self.password,
self.host)
self._process = subprocess.Popen(
self._process = popen.popen(
shlex.split(self._substituted_command),
stdout=self._file_write,
stderr=subprocess.STDOUT,
stdin=subprocess.PIPE,
stderr=popen.STDOUT,
stdin=popen.PIPE,
close_fds=True,
universal_newlines=True,
bufsize=-1)

View File

@ -39,7 +39,7 @@ class TestLocalShell(TestBashModule):
def setUp(self):
super(TestLocalShell, self).setUp()
popen_patcher = mock.patch.object(bash.subprocess, 'Popen')
popen_patcher = mock.patch.object(bash.popen, 'popen')
self.mock_popen = popen_patcher.start()
mock_result = mock.MagicMock()
mock_result.returncode = self.testrun.returncode

View File

@ -49,4 +49,4 @@ downloadcache = ~/cache/pip
ignore = H102
show-source = True
exclude = .venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,build,tools,*satori/contrib*,*.ropeproject,*satori/tests*,setup.py
max-complexity = 12
max-complexity = 16