From ddf5e546916ad94c62b1e42b6f03831f906b2f29 Mon Sep 17 00:00:00 2001 From: antialias Date: Mon, 24 Sep 2012 17:34:25 -0400 Subject: improved network checks on the way to a network checker. --- src/leap/eip/checks.py | 15 +++++++-------- src/leap/eip/exceptions.py | 2 ++ src/leap/eip/tests/test_checks.py | 22 ++++++++++++++++++++-- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/src/leap/eip/checks.py b/src/leap/eip/checks.py index 9b7b1cee..82940fd3 100644 --- a/src/leap/eip/checks.py +++ b/src/leap/eip/checks.py @@ -70,21 +70,20 @@ class LeapNetworkChecker(object): checker.is_internet_up() checker.ping_gateway() - def test_internet_connection(self): - # XXX we're not passing the error anywhere. - # XXX we probably should raise an exception here? - # unless we use this as smoke test + def check_internet_connection(self): try: # XXX remove this hardcoded random ip requests.get('http://216.172.161.165') except (requests.HTTPError, requests.RequestException) as e: - self.error = e.message - except requests.ConenctionError as e: + raise eipexceptions.NoInternetConnection(e.message) + except requests.ConnectionError as e: + error = "Unidentified Connection Error" if e.message == "[Errno 113] No route to host": if not self.is_internet_up(): - self.error = "No valid internet connection found." + error = "No valid internet connection found." else: - self.error = "Provider server appears to be down." + error = "Provider server appears to be down." + raise eipexceptions.NoInternetConnection(error) def is_internet_up(self): iface, gateway = self.get_default_interface_gateway() diff --git a/src/leap/eip/exceptions.py b/src/leap/eip/exceptions.py index f048621f..f883a173 100644 --- a/src/leap/eip/exceptions.py +++ b/src/leap/eip/exceptions.py @@ -136,6 +136,8 @@ class NoConnectionToGateway(EIPClientError): message = "no connection to gateway" usermessage = "Looks like there are problems with your internet connection" +class NoInternetConnection(EIPClientError): + message = "No Internet connection found" # # Errors that probably we don't need anymore diff --git a/src/leap/eip/tests/test_checks.py b/src/leap/eip/tests/test_checks.py index 19b54c04..f412dbec 100644 --- a/src/leap/eip/tests/test_checks.py +++ b/src/leap/eip/tests/test_checks.py @@ -52,7 +52,7 @@ class LeapNetworkCheckTest(BaseLeapTest): def test_checker_should_implement_check_methods(self): checker = eipchecks.LeapNetworkChecker() - self.assertTrue(hasattr(checker, "test_internet_connection"), + self.assertTrue(hasattr(checker, "check_internet_connection"), "missing meth") self.assertTrue(hasattr(checker, "is_internet_up"), "missing meth") @@ -64,7 +64,7 @@ class LeapNetworkCheckTest(BaseLeapTest): mc = Mock() checker.run_all(checker=mc) - self.assertTrue(mc.test_internet_connection.called, "not called") + self.assertTrue(mc.check_internet_connection.called, "not called") self.assertTrue(mc.ping_gateway.called, "not called") self.assertTrue(mc.is_internet_up.called, "not called") @@ -86,6 +86,24 @@ class LeapNetworkCheckTest(BaseLeapTest): mocked_ping.return_value = [11, "", ""] checker.ping_gateway("4.2.2.2") + def test_check_internet_connection_failures(self): + checker = eipchecks.LeapNetworkChecker() + with patch.object(requests, "get") as mocked_get: + mocked_get.side_effect = requests.HTTPError + with self.assertRaises(eipexceptions.NoInternetConnection): + checker.check_internet_connection() + + with patch.object(requests, "get") as mocked_get: + mocked_get.side_effect = requests.RequestException + with self.assertRaises(eipexceptions.NoInternetConnection): + checker.check_internet_connection() + + #TODO: Mock possible errors that can be raised by is_internet_up + with patch.object(requests, "get") as mocked_get: + mocked_get.side_effect = requests.ConnectionError + with self.assertRaises(eipexceptions.NoInternetConnection): + checker.check_internet_connection() + @unittest.skipUnless(_uid == 0, "root only") def test_ping_gateway(self): checker = eipchecks.LeapNetworkChecker() -- cgit v1.2.3