Change assert statement to raise in common/utils 53/60353/1
authorMiikka Koistinen <miikka.koistinen@nokia.com>
Mon, 30 Jul 2018 13:01:14 +0000 (16:01 +0300)
committerMiikka Koistinen <miikka.koistinen@nokia.com>
Mon, 30 Jul 2018 13:01:14 +0000 (16:01 +0300)
This modification utilises a custom exception to be raised if an invalid
mac address is passed to the function "mac_address_to_hex_list".

JIRA: YARDSTICK-966

Change-Id: Ic2940276f500e9710c54963a445a137e73bd6b25
Signed-off-by: Miikka Koistinen <miikka.koistinen@nokia.com>
yardstick/common/exceptions.py
yardstick/common/utils.py
yardstick/tests/unit/common/test_utils.py

index a559ab4..48f15c0 100644 (file)
@@ -404,3 +404,7 @@ class AclMissingActionArguments(YardstickException):
 
 class AclUnknownActionTemplate(YardstickException):
     message = 'No ACL CLI template found for "%(action_name)s" action'
+
+
+class InvalidMacAddress(YardstickException):
+    message = 'Mac address "%(mac_address)s" is invalid'
index c74dd67..6c5389c 100644 (file)
@@ -282,8 +282,12 @@ def get_free_port(ip):
 
 
 def mac_address_to_hex_list(mac):
-    octets = ["0x{:02x}".format(int(elem, 16)) for elem in mac.split(':')]
-    assert len(octets) == 6 and all(len(octet) == 4 for octet in octets)
+    try:
+        octets = ["0x{:02x}".format(int(elem, 16)) for elem in mac.split(':')]
+    except ValueError:
+        raise exceptions.InvalidMacAddress(mac_address=mac)
+    if len(octets) != 6 or all(len(octet) != 4 for octet in octets):
+        raise exceptions.InvalidMacAddress(mac_address=mac)
     return octets
 
 
index 7c58a82..b634ff4 100644 (file)
@@ -196,6 +196,14 @@ class TestMacAddressToHex(ut_base.BaseUnitTestCase):
         self.assertEqual(utils.mac_address_to_hex_list("ea:3e:e1:9a:99:e8"),
                          ['0xea', '0x3e', '0xe1', '0x9a', '0x99', '0xe8'])
 
+    def test_mac_address_to_hex_list_too_short_mac(self):
+        with self.assertRaises(exceptions.InvalidMacAddress):
+            utils.mac_address_to_hex_list("ea:3e:e1:9a")
+
+    def test_mac_address_to_hex_list_no_int_mac(self):
+        with self.assertRaises(exceptions.InvalidMacAddress):
+            utils.mac_address_to_hex_list("invalid_mac")
+
 
 class TranslateToStrTestCase(ut_base.BaseUnitTestCase):