From 0937dca14b1f60bdf3d4377ee305d41a1721f153 Mon Sep 17 00:00:00 2001 From: Cedric Brandily Date: Fri, 19 Dec 2014 15:05:53 +0100 Subject: [PATCH] get_binary_name should returns strings without spaces Iptables does not support chain names with spaces. It implies get_binary_name should return strings without spaces (they are used as chain name prefix). But currently 'python -m unittest $module' implies spaces in get_binary_name() result, it disallows to use it when $module is a functional test module. This change replaces spaces with underscores in get_binary_name results. Change-Id: Ie7c8518b92be46a4eea4e9345713fdeba844126d Closes-Bug: #1404250 --- neutron/agent/linux/iptables_manager.py | 2 +- neutron/tests/unit/test_iptables_manager.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/neutron/agent/linux/iptables_manager.py b/neutron/agent/linux/iptables_manager.py index 11b275d3d..4b2988cee 100644 --- a/neutron/agent/linux/iptables_manager.py +++ b/neutron/agent/linux/iptables_manager.py @@ -42,7 +42,7 @@ LOG = logging.getLogger(__name__) # (max_chain_name_length - len('-POSTROUTING') == 16) def get_binary_name(): """Grab the name of the binary we're running in.""" - return os.path.basename(sys.argv[0])[:16] + return os.path.basename(sys.argv[0])[:16].replace(' ', '_') binary_name = get_binary_name() diff --git a/neutron/tests/unit/test_iptables_manager.py b/neutron/tests/unit/test_iptables_manager.py index e96309fec..2991fbf08 100644 --- a/neutron/tests/unit/test_iptables_manager.py +++ b/neutron/tests/unit/test_iptables_manager.py @@ -84,6 +84,15 @@ COMMENTED_NAT_DUMP = ( '# Completed by iptables_manager\n' % IPTABLES_ARG) +class IptablesTestCase(base.BaseTestCase): + + def test_get_binary_name_in_unittest(self): + # Corresponds to sys.argv content when running python -m unittest class + with mock.patch('sys.argv', ['python -m unittest', 'class']): + binary_name = iptables_manager.get_binary_name() + self.assertEqual('python_-m_unitte', binary_name) + + class IptablesCommentsTestCase(base.BaseTestCase): def setUp(self): -- 2.45.2