aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCédric Ollivier <cedric.ollivier@orange.com>2016-11-30 01:25:24 +0100
committerCédric Ollivier <cedric.ollivier@orange.com>2016-11-30 01:40:42 +0100
commit81189460862be9d03c951c318df6ada2ffefb02f (patch)
treee7ef47fea367a96d2a080bbdf640bd057acf9547
parent46bdeab0b547045a83d60854a95f83349f17fc96 (diff)
Allow unit testing w/o internet connectivity
urllib2.urlopen() is now patched when testing the internet connectivity helper. Change-Id: I4ddf1dd8f494fe282735e1051986a0b5dd1a040f Signed-off-by: Cédric Ollivier <cedric.ollivier@orange.com>
-rw-r--r--functest/tests/unit/utils/test_utils.py27
1 files changed, 21 insertions, 6 deletions
diff --git a/functest/tests/unit/utils/test_utils.py b/functest/tests/unit/utils/test_utils.py
index 835797f7..8b6c5e1b 100644
--- a/functest/tests/unit/utils/test_utils.py
+++ b/functest/tests/unit/utils/test_utils.py
@@ -8,7 +8,9 @@
# http://www.apache.org/licenses/LICENSE-2.0
import logging
+import mock
import unittest
+import urllib2
from functest.utils import functest_utils
@@ -18,12 +20,25 @@ class FunctestUtilsTesting(unittest.TestCase):
logging.disable(logging.CRITICAL)
def setUp(self):
- self.test = functest_utils
-
- def test_check_internet_connectivity(self):
- self.assertTrue(self.test.check_internet_connectivity())
-# TODO
-# ...
+ self.url = 'http://www.opnfv.org/'
+ self.timeout = 5
+
+ @mock.patch('urllib2.urlopen',
+ side_effect=urllib2.URLError('no host given'))
+ def test_check_internet_connectivity_failed(self, mock_method):
+ self.assertFalse(functest_utils.check_internet_connectivity())
+ mock_method.assert_called_once_with(self.url, timeout=self.timeout)
+
+ @mock.patch('urllib2.urlopen')
+ def test_check_internet_connectivity_default(self, mock_method):
+ self.assertTrue(functest_utils.check_internet_connectivity())
+ mock_method.assert_called_once_with(self.url, timeout=self.timeout)
+
+ @mock.patch('urllib2.urlopen')
+ def test_check_internet_connectivity_debian(self, mock_method):
+ self.url = "https://www.debian.org/"
+ self.assertTrue(functest_utils.check_internet_connectivity(self.url))
+ mock_method.assert_called_once_with(self.url, timeout=self.timeout)
if __name__ == "__main__":