Merge "Change assert statement to raise in common/utils"
[yardstick.git] / yardstick / tests / unit / common / test_utils.py
index 446afdd..b634ff4 100644 (file)
@@ -12,12 +12,14 @@ import errno
 import importlib
 import ipaddress
 from itertools import product, chain
-import mock
 import os
-import six
-from six.moves import configparser
 import socket
 import time
+import threading
+
+import mock
+import six
+from six.moves import configparser
 import unittest
 
 import yardstick
@@ -194,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):
 
@@ -1196,6 +1206,20 @@ class TestUtilsIpAddrMethods(ut_base.BaseUnitTestCase):
         for value in chain(value_iter, self.INVALID_IP_ADDRESS_STR_LIST):
             self.assertEqual(utils.ip_to_hex(value), value)
 
+    def test_get_mask_from_ip_range_ipv4(self):
+        ip_str = '1.1.1.1'
+        for mask in range(8, 30):
+            ip = ipaddress.ip_network(ip_str + '/' + str(mask), strict=False)
+            result = utils.get_mask_from_ip_range(ip[2], ip[-2])
+            self.assertEqual(mask, result)
+
+    def test_get_mask_from_ip_range_ipv6(self):
+        ip_str = '2001::1'
+        for mask in range(8, 120):
+            ip = ipaddress.ip_network(ip_str + '/' + str(mask), strict=False)
+            result = utils.get_mask_from_ip_range(ip[2], ip[-2])
+            self.assertEqual(mask, result)
+
 
 class SafeDecodeUtf8TestCase(ut_base.BaseUnitTestCase):
 
@@ -1263,6 +1287,10 @@ class TimerTestCase(ut_base.BaseUnitTestCase):
             time.sleep(1.1)
         self.assertEqual(2, len(iterations))
 
+    def test_delta_time_sec(self):
+        with utils.Timer() as timer:
+            self.assertIsInstance(timer.delta_time_sec(), float)
+
 
 class WaitUntilTrueTestCase(ut_base.BaseUnitTestCase):
 
@@ -1284,6 +1312,15 @@ class WaitUntilTrueTestCase(ut_base.BaseUnitTestCase):
                 utils.wait_until_true(lambda: False, timeout=1, sleep=1,
                                       exception=MyTimeoutException))
 
+    def _run_thread(self):
+        with self.assertRaises(exceptions.WaitTimeout):
+            utils.wait_until_true(lambda: False, timeout=1, sleep=1)
+
+    def test_timeout_no_main_thread(self):
+        new_thread = threading.Thread(target=self._run_thread)
+        new_thread.start()
+        new_thread.join(timeout=3)
+
 
 class SendSocketCommandTestCase(unittest.TestCase):