
import logging
import unittest

from functest.utils import functest_utils


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
# ...


if __name__ == "__main__":
    unittest.main(verbosity=2)


import logging
import mock
import unittest
import urllib2

from functest.utils import functest_utils


class FunctestUtilsTesting(unittest.TestCase):

    logging.disable(logging.CRITICAL)

    def setUp(self):
        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__":
    unittest.main(verbosity=2)

Allow unit testing w/o internet connectivity
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 <d48310251a4a484d041bc5d09a9ac4d86d20f793@orange.com>

  
  import logging
+ import mock
  import unittest
+ import urllib2
  
  from functest.utils import functest_utils
  
  
  class FunctestUtilsTesting(unittest.TestCase):
  
      logging.disable(logging.CRITICAL)
  
      def setUp(self):
-         self.test = functest_utils
+         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(self):
+     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(self.test.check_internet_connectivity())
+         self.assertTrue(functest_utils.check_internet_connectivity())
- # TODO
- # ...
+         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__":
      unittest.main(verbosity=2)
  
Allow unit testing w/o internet connectivity
## Code Before:

import logging
import unittest

from functest.utils import functest_utils


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
# ...


if __name__ == "__main__":
    unittest.main(verbosity=2)

## Instruction:
Allow unit testing w/o internet connectivity
## Code After:

import logging
import mock
import unittest
import urllib2

from functest.utils import functest_utils


class FunctestUtilsTesting(unittest.TestCase):

    logging.disable(logging.CRITICAL)

    def setUp(self):
        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__":
    unittest.main(verbosity=2)
