From ffe551fdbbade14e1a8de84ac48064aa7b45e2c1 Mon Sep 17 00:00:00 2001 From: antialias Date: Mon, 10 Sep 2012 19:59:30 -0400 Subject: Implemented basic networks checks: valid interface, default route, and can ping the listed gateway. --- src/leap/eip/tests/test_checks.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) (limited to 'src/leap/eip/tests/test_checks.py') diff --git a/src/leap/eip/tests/test_checks.py b/src/leap/eip/tests/test_checks.py index 0a87f573..1edcdfb2 100644 --- a/src/leap/eip/tests/test_checks.py +++ b/src/leap/eip/tests/test_checks.py @@ -8,8 +8,10 @@ except ImportError: import os import urlparse -from mock import patch, Mock +from StringIO import StringIO +from mock import patch, Mock, MagicMock +import ping import requests from leap.base import config as baseconfig @@ -23,6 +25,8 @@ from leap.testing.basetest import BaseLeapTest from leap.testing.https_server import BaseHTTPSServerTestCase from leap.testing.https_server import where as where_cert +_uid = os.getuid() + class NoLogRequestHandler: def log_message(self, *args): @@ -170,6 +174,26 @@ class EIPCheckTest(BaseLeapTest): sampleconfig = copy.copy(testdata.EIP_SAMPLE_JSON) checker.check_complete_eip_config(config=sampleconfig) + def test_get_default_interface_no_interface(self): + checker = eipchecks.EIPConfigChecker() + with patch('leap.eip.checks.open', create=True) as mock_open: + with self.assertRaises(eipexceptions.NoDefaultInterfaceFoundError): + mock_open.return_value = \ + StringIO("Iface\tDestination Gateway\tFlags\tRefCntd\tUse\tMetric\tMask\tMTU\tWindow\tIRTT") + checker.get_default_interface_gateway() + + def test_ping_gateway_fail(self): + checker = eipchecks.EIPConfigChecker() + with patch.object(ping, "quiet_ping") as mocked_ping: + with self.assertRaises(eipexceptions.NoConnectionToGateway): + mocked_ping.return_value = [11, "", ""] + checker.ping_gateway("4.2.2.2") + + @unittest.skipUnless(_uid == 0, "root only") + def test_ping_gateway(self): + checker = eipchecks.EIPConfigChecker() + checker.ping_gateway("4.2.2.2") + class ProviderCertCheckerTest(BaseLeapTest): -- cgit v1.2.3 From 4304ef6107a97d9d03cb626d4a7fcbd5afc1a2c9 Mon Sep 17 00:00:00 2001 From: kali Date: Wed, 12 Sep 2012 08:01:26 +0900 Subject: pep8 --- src/leap/eip/tests/test_checks.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/leap/eip/tests/test_checks.py') diff --git a/src/leap/eip/tests/test_checks.py b/src/leap/eip/tests/test_checks.py index 1edcdfb2..caaa371f 100644 --- a/src/leap/eip/tests/test_checks.py +++ b/src/leap/eip/tests/test_checks.py @@ -178,8 +178,10 @@ class EIPCheckTest(BaseLeapTest): checker = eipchecks.EIPConfigChecker() with patch('leap.eip.checks.open', create=True) as mock_open: with self.assertRaises(eipexceptions.NoDefaultInterfaceFoundError): - mock_open.return_value = \ - StringIO("Iface\tDestination Gateway\tFlags\tRefCntd\tUse\tMetric\tMask\tMTU\tWindow\tIRTT") + mock_open.return_value = StringIO( + "Iface\tDestination Gateway\t" + "Flags\tRefCntd\tUse\tMetric\t" + "Mask\tMTU\tWindow\tIRTT") checker.get_default_interface_gateway() def test_ping_gateway_fail(self): -- cgit v1.2.3 From f3b601cb525b2884e7a48c7bfc41b4aef915adf7 Mon Sep 17 00:00:00 2001 From: kali Date: Wed, 12 Sep 2012 08:38:36 +0900 Subject: moved network checks to its own class so it can be more easily moved to base.checks and reused when eip is a module. --- src/leap/eip/tests/test_checks.py | 79 +++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 24 deletions(-) (limited to 'src/leap/eip/tests/test_checks.py') diff --git a/src/leap/eip/tests/test_checks.py b/src/leap/eip/tests/test_checks.py index caaa371f..bc7db79c 100644 --- a/src/leap/eip/tests/test_checks.py +++ b/src/leap/eip/tests/test_checks.py @@ -9,7 +9,7 @@ import os import urlparse from StringIO import StringIO -from mock import patch, Mock, MagicMock +from mock import (patch, Mock) import ping import requests @@ -37,6 +37,60 @@ class NoLogRequestHandler: return '' +class LeapNetworkCheckTest(BaseLeapTest): + # XXX to be moved to base.checks + + __name__ = "leap_network_check_tests" + + def setUp(self): + pass + + def tearDown(self): + pass + + def test_checker_should_implement_check_methods(self): + checker = eipchecks.LeapNetworkChecker() + + self.assertTrue(hasattr(checker, "test_internet_connection"), + "missing meth") + self.assertTrue(hasattr(checker, "is_internet_up"), + "missing meth") + self.assertTrue(hasattr(checker, "ping_gateway"), + "missing meth") + + def test_checker_should_actually_call_all_tests(self): + checker = eipchecks.LeapNetworkChecker() + + mc = Mock() + checker.run_all(checker=mc) + self.assertTrue(mc.test_internet_connection.called, "not called") + self.assertTrue(mc.ping_gateway.called, "not called") + self.assertTrue(mc.is_internet_up.called, + "not called") + + def test_get_default_interface_no_interface(self): + checker = eipchecks.LeapNetworkChecker() + with patch('leap.eip.checks.open', create=True) as mock_open: + with self.assertRaises(eipexceptions.NoDefaultInterfaceFoundError): + mock_open.return_value = StringIO( + "Iface\tDestination Gateway\t" + "Flags\tRefCntd\tUse\tMetric\t" + "Mask\tMTU\tWindow\tIRTT") + checker.get_default_interface_gateway() + + def test_ping_gateway_fail(self): + checker = eipchecks.LeapNetworkChecker() + with patch.object(ping, "quiet_ping") as mocked_ping: + with self.assertRaises(eipexceptions.NoConnectionToGateway): + mocked_ping.return_value = [11, "", ""] + checker.ping_gateway("4.2.2.2") + + @unittest.skipUnless(_uid == 0, "root only") + def test_ping_gateway(self): + checker = eipchecks.LeapNetworkChecker() + checker.ping_gateway("4.2.2.2") + + class EIPCheckTest(BaseLeapTest): __name__ = "eip_check_tests" @@ -61,7 +115,6 @@ class EIPCheckTest(BaseLeapTest): "missing meth") self.assertTrue(hasattr(checker, "check_complete_eip_config"), "missing meth") - self.assertTrue(hasattr(checker, "ping_gateway"), "missing meth") def test_checker_should_actually_call_all_tests(self): checker = eipchecks.EIPConfigChecker() @@ -174,28 +227,6 @@ class EIPCheckTest(BaseLeapTest): sampleconfig = copy.copy(testdata.EIP_SAMPLE_JSON) checker.check_complete_eip_config(config=sampleconfig) - def test_get_default_interface_no_interface(self): - checker = eipchecks.EIPConfigChecker() - with patch('leap.eip.checks.open', create=True) as mock_open: - with self.assertRaises(eipexceptions.NoDefaultInterfaceFoundError): - mock_open.return_value = StringIO( - "Iface\tDestination Gateway\t" - "Flags\tRefCntd\tUse\tMetric\t" - "Mask\tMTU\tWindow\tIRTT") - checker.get_default_interface_gateway() - - def test_ping_gateway_fail(self): - checker = eipchecks.EIPConfigChecker() - with patch.object(ping, "quiet_ping") as mocked_ping: - with self.assertRaises(eipexceptions.NoConnectionToGateway): - mocked_ping.return_value = [11, "", ""] - checker.ping_gateway("4.2.2.2") - - @unittest.skipUnless(_uid == 0, "root only") - def test_ping_gateway(self): - checker = eipchecks.EIPConfigChecker() - checker.ping_gateway("4.2.2.2") - class ProviderCertCheckerTest(BaseLeapTest): -- cgit v1.2.3