Replace subprocess with processutils

Trying to fix the dsvm-python3 job.

Depends-On: https://review.openstack.org/608620
Change-Id: Ibdfed9545a26e752ab7aeed2db122a368c3c06fb
This commit is contained in:
Kaifeng Wang 2018-09-29 17:08:44 +08:00 committed by Dmitry Tantsur
parent 4dc1400063
commit 1ef55e3603
2 changed files with 13 additions and 17 deletions

View File

@ -15,7 +15,7 @@ import contextlib
import os import os
import re import re
from eventlet.green import subprocess from oslo_concurrency import processutils
from oslo_config import cfg from oslo_config import cfg
from oslo_log import log from oslo_log import log
@ -68,10 +68,9 @@ class IptablesFilter(pxe_filter.BaseFilter):
# -w flag makes iptables wait for xtables lock, but it's not supported # -w flag makes iptables wait for xtables lock, but it's not supported
# everywhere yet # everywhere yet
try: try:
with open(os.devnull, 'wb') as null: cmd = self.base_command + ('-w', '-h')
subprocess.check_call(self.base_command + ('-w', '-h'), processutils.execute(*cmd)
stderr=null, stdout=null) except processutils.ProcessExecutionError:
except subprocess.CalledProcessError:
LOG.warning('iptables does not support -w flag, please update ' LOG.warning('iptables does not support -w flag, please update '
'it to at least version 1.4.21') 'it to at least version 1.4.21')
else: else:
@ -151,18 +150,15 @@ class IptablesFilter(pxe_filter.BaseFilter):
cmd = self.base_command + args cmd = self.base_command + args
ignore = kwargs.pop('ignore', False) ignore = kwargs.pop('ignore', False)
LOG.debug('Running iptables %s', args) LOG.debug('Running iptables %s', args)
kwargs['stderr'] = subprocess.STDOUT
try: try:
subprocess.check_output(cmd, **kwargs) processutils.execute(*cmd)
except subprocess.CalledProcessError as exc: except processutils.ProcessExecutionError as exc:
decoded_output = exc.output.decode("utf-8")
output = decoded_output.replace('\n', '. ')
if ignore: if ignore:
LOG.debug('Ignoring failed iptables %(args)s: %(output)s', LOG.debug('Ignoring failed iptables %(args)s: %(error)s',
{'args': args, 'output': output}) {'args': args, 'error': exc})
else: else:
LOG.error('iptables %(iptables)s failed: %(exc)s', LOG.error('iptables %(iptables)s failed: %(error)s',
{'iptables': args, 'exc': output}) {'iptables': args, 'error': exc})
raise raise
def _clean_up(self, chain): def _clean_up(self, chain):

View File

@ -37,7 +37,7 @@ class TestIptablesDriver(test_base.NodeTest):
self.mock_fsm = self.useFixture( self.mock_fsm = self.useFixture(
fixtures.MockPatchObject(iptables.IptablesFilter, 'fsm')).mock fixtures.MockPatchObject(iptables.IptablesFilter, 'fsm')).mock
self.mock_call = self.useFixture( self.mock_call = self.useFixture(
fixtures.MockPatchObject(iptables.subprocess, 'check_call')).mock fixtures.MockPatchObject(iptables.processutils, 'execute')).mock
self.driver = iptables.IptablesFilter() self.driver = iptables.IptablesFilter()
self.mock_iptables = self.useFixture( self.mock_iptables = self.useFixture(
fixtures.MockPatchObject(self.driver, '_iptables')).mock fixtures.MockPatchObject(self.driver, '_iptables')).mock
@ -73,8 +73,8 @@ class TestIptablesDriver(test_base.NodeTest):
self.check_fsm([pxe_filter.Events.initialize]) self.check_fsm([pxe_filter.Events.initialize])
def test_init_args_old_iptables(self): def test_init_args_old_iptables(self):
self.mock_call.side_effect = iptables.subprocess.CalledProcessError( exc = iptables.processutils.ProcessExecutionError(2, '')
2, '') self.mock_call.side_effect = exc
self.driver.init_filter() self.driver.init_filter()
init_expected_args = [ init_expected_args = [
('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport', '67', ('-D', 'INPUT', '-i', 'br-ctlplane', '-p', 'udp', '--dport', '67',