aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMiikka Koistinen <miikka.koistinen@nokia.com>2018-07-31 17:03:10 +0300
committerMiikka Koistinen <miikka.koistinen@nokia.com>2018-07-31 17:11:41 +0300
commit6e0e8fde5efc85e57cf4021991ec2d17298eaf3d (patch)
tree2bd094057277ac2a83c62c0a03f105a2724f18e8
parent8a52d114404a03b4adbb49ff7f221819502bf672 (diff)
Replace assert statements in PipelineRules
The class PipelineRules uses assert statements to check the correctness of an input parameter IP address. This commit will modify these lines to use a dedicated function "common.utils.make_ipv4_address". JIRA: YARDSTICK-966 Change-Id: I1c4cc439a7716d36548125c87dfb0bc200714a56 Signed-off-by: Miikka Koistinen <miikka.koistinen@nokia.com>
-rw-r--r--yardstick/common/utils.py4
-rw-r--r--yardstick/network_services/pipeline.py11
-rw-r--r--yardstick/tests/unit/common/test_utils.py13
3 files changed, 22 insertions, 6 deletions
diff --git a/yardstick/common/utils.py b/yardstick/common/utils.py
index 6c5389cd0..f1822a03d 100644
--- a/yardstick/common/utils.py
+++ b/yardstick/common/utils.py
@@ -291,6 +291,10 @@ def mac_address_to_hex_list(mac):
return octets
+def make_ipv4_address(ip_addr):
+ return ipaddress.IPv4Address(six.text_type(ip_addr))
+
+
def safe_ip_address(ip_addr):
""" get ip address version v6 or v4 """
try:
diff --git a/yardstick/network_services/pipeline.py b/yardstick/network_services/pipeline.py
index d781ba0cd..7155480d4 100644
--- a/yardstick/network_services/pipeline.py
+++ b/yardstick/network_services/pipeline.py
@@ -18,6 +18,8 @@ import itertools
from six.moves import zip
+from yardstick.common import utils
+
FIREWALL_ADD_DEFAULT = "p {0} firewall add default 1"
FIREWALL_ADD_PRIO = """\
p {0} firewall add priority 1 ipv4 {1} 24 0.0.0.0 0 0 65535 0 65535 6 0xFF port 0"""
@@ -59,8 +61,7 @@ class PipelineRules(object):
self.add_rule(FIREWALL_ADD_PRIO, ip)
def add_firewall_script(self, ip):
- ip_addr = ip.split('.')
- assert len(ip_addr) == 4
+ ip_addr = str(utils.make_ipv4_address(ip)).split('.')
ip_addr[-1] = '0'
for i in range(256):
ip_addr[-2] = str(i)
@@ -87,8 +88,7 @@ class PipelineRules(object):
self.add_rule(ROUTE_ADD_ETHER_MPLS, ip, mac_addr, index)
def add_route_script(self, ip, mac_addr):
- ip_addr = ip.split('.')
- assert len(ip_addr) == 4
+ ip_addr = str(utils.make_ipv4_address(ip)).split('.')
ip_addr[-1] = '0'
for index in range(0, 256, 8):
ip_addr[-2] = str(index)
@@ -101,8 +101,7 @@ class PipelineRules(object):
self.add_rule(ROUTE_ADD_ETHER_QINQ, ip, mask, mac_addr, index)
def add_route_script2(self, ip, mac_addr):
- ip_addr = ip.split('.')
- assert len(ip_addr) == 4
+ ip_addr = str(utils.make_ipv4_address(ip)).split('.')
ip_addr[-1] = '0'
mask = 24
for i in range(0, 256):
diff --git a/yardstick/tests/unit/common/test_utils.py b/yardstick/tests/unit/common/test_utils.py
index b634ff4b4..b0304284b 100644
--- a/yardstick/tests/unit/common/test_utils.py
+++ b/yardstick/tests/unit/common/test_utils.py
@@ -1123,6 +1123,19 @@ class TestUtilsIpAddrMethods(ut_base.BaseUnitTestCase):
u'123:4567:89ab:cdef:123:4567:89ab:cdef/129',
]
+ def test_make_ipv4_address(self):
+ for addr in self.GOOD_IP_V4_ADDRESS_STR_LIST:
+ # test with no mask
+ expected = ipaddress.IPv4Address(addr)
+ self.assertEqual(utils.make_ipv4_address(addr), expected, addr)
+
+ def test_make_ipv4_address_error(self):
+ addr_list = self.INVALID_IP_ADDRESS_STR_LIST +\
+ self.GOOD_IP_V6_ADDRESS_STR_LIST
+ for addr in addr_list:
+ self.assertRaises(Exception, utils.make_ipv4_address, addr)
+
+
def test_safe_ip_address(self):
addr_list = self.GOOD_IP_V4_ADDRESS_STR_LIST
for addr in addr_list: