diff options
author | kali <kali@leap.se> | 2012-08-28 06:08:58 +0900 |
---|---|---|
committer | kali <kali@leap.se> | 2012-08-28 06:08:58 +0900 |
commit | c469e396bde67db15e486a320b254de0fa6f69df (patch) | |
tree | df725247cc48e40a109022fad32b1fc8797d576a /src | |
parent | 568d52ccf33e6d7683f36f5fe2e3c32b47892216 (diff) |
checki complete eip_config tests.
completed first version of EIPChecks
Diffstat (limited to 'src')
-rw-r--r-- | src/leap/eip/checks.py | 59 | ||||
-rw-r--r-- | src/leap/eip/constants.py | 28 | ||||
-rw-r--r-- | src/leap/eip/exceptions.py | 4 | ||||
-rw-r--r-- | src/leap/eip/tests/test_checks.py | 41 |
4 files changed, 121 insertions, 11 deletions
diff --git a/src/leap/eip/checks.py b/src/leap/eip/checks.py index dbb7d524..84a2ba6b 100644 --- a/src/leap/eip/checks.py +++ b/src/leap/eip/checks.py @@ -53,7 +53,7 @@ class EIPChecker(object): checker.fetch_definition() checker.fetch_eip_config() checker.check_complete_eip_config() - checker.ping_gateway() + #checker.ping_gateway() # public checks @@ -97,6 +97,13 @@ class EIPChecker(object): def fetch_definition(self, skip_download=False, config=None, uri=None): # check_and_get_definition_file + """ + fetches a definition file from server + """ + # TODO: + # - Implement diff + # - overwrite if different. + if skip_download: return True if config is None: @@ -123,11 +130,47 @@ class EIPChecker(object): with open(definition_file, 'wb') as f: f.write(json.dumps(request.json, indent=4)) - def fetch_eip_config(self): - raise NotImplementedError + def fetch_eip_config(self, skip_download=False, + config=None, uri=None): + if skip_download: + return True + if config is None: + config = self.config + if uri is None: + if config: + domain = config.get('provider', None) + else: + domain = None + uri = self._get_eip_service_uri( + domain=domain) - def check_complete_eip_config(self): - raise NotImplementedError + # XXX move to JSONConfig Fetcher + request = self.fetcher.get(uri) + request.raise_for_status() + + definition_file = os.path.join( + baseconfig.get_default_provider_path(), + eipconstants.EIP_SERVICE_EXPECTED_PATH) + + folder, filename = os.path.split(definition_file) + if not os.path.isdir(folder): + mkdir_p(folder) + with open(definition_file, 'wb') as f: + f.write(json.dumps(request.json, indent=4)) + + def check_complete_eip_config(self, config=None): + if config is None: + config = self.config + try: + 'trying assertions' + assert 'provider' in config + assert config['provider'] is not None + except AssertionError: + raise eipexceptions.EIPConfigurationError + + # XXX TODO: + # We should WRITE eip config if missing or + # incomplete at this point def ping_gateway(self): raise NotImplementedError @@ -154,3 +197,9 @@ class EIPChecker(object): path = baseconstants.DEFINITION_EXPECTED_PATH return "https://%s/%s" % (domain, path) + def _get_eip_service_uri(self, domain=None, path=None): + if domain is None: + domain = baseconstants.DEFAULT_TEST_PROVIDER + if path is None: + path = eipconstants.EIP_SERVICE_EXPECTED_PATH + return "https://%s/%s" % (domain, path) diff --git a/src/leap/eip/constants.py b/src/leap/eip/constants.py index 7124ca57..6161d744 100644 --- a/src/leap/eip/constants.py +++ b/src/leap/eip/constants.py @@ -18,3 +18,31 @@ EIP_SAMPLE_JSON = { "secondary_gateway": "france", "management_password": "oph7Que1othahwiech6J" } + +EIP_SERVICE_EXPECTED_PATH = "eip-service.json" + +EIP_SAMPLE_SERVICE = { + "serial": 1, + "version": "0.1.0", + "capabilities": { + "transport": ["openvpn"], + "ports": ["80", "53"], + "protocols": ["udp", "tcp"], + "static_ips": True, + "adblock": True + }, + "gateways": [ + {"country_code": "us", + "label": {"en":"west"}, + "capabilities": {}, + "hosts": ["1.2.3.4", "1.2.3.5"]}, + {"country_code": "us", + "label": {"en":"east"}, + "capabilities": {}, + "hosts": ["1.2.3.4", "1.2.3.5"]}, + {"country_code": "fr", + "label": {}, + "capabilities": {}, + "hosts": ["1.2.3.4", "1.2.3.5"]} + ] +} diff --git a/src/leap/eip/exceptions.py b/src/leap/eip/exceptions.py index 800c7f0e..19a0e707 100644 --- a/src/leap/eip/exceptions.py +++ b/src/leap/eip/exceptions.py @@ -65,3 +65,7 @@ class EIPInitBadKeyFilePermError(Exception): class EIPMissingDefaultProvider(Exception): pass + + +class EIPConfigurationError(Exception): + pass diff --git a/src/leap/eip/tests/test_checks.py b/src/leap/eip/tests/test_checks.py index 8c022907..83561833 100644 --- a/src/leap/eip/tests/test_checks.py +++ b/src/leap/eip/tests/test_checks.py @@ -11,7 +11,8 @@ from mock import patch, Mock import requests from leap.base import config as baseconfig -from leap.base.constants import DEFAULT_PROVIDER_DEFINITION +from leap.base.constants import (DEFAULT_PROVIDER_DEFINITION, + DEFINITION_EXPECTED_PATH) from leap.eip import checks as eipchecks from leap.eip import constants as eipconstants from leap.eip import exceptions as eipexceptions @@ -57,8 +58,8 @@ class EIPCheckTest(BaseLeapTest): "not called") self.assertTrue(mc.check_complete_eip_config.called, "not called") - self.assertTrue(mc.ping_gateway.called, - "not called") + #self.assertTrue(mc.ping_gateway.called, + #"not called") # test individual check methods @@ -107,10 +108,38 @@ class EIPCheckTest(BaseLeapTest): sampleconfig = eipconstants.EIP_SAMPLE_JSON checker.fetch_definition(config=sampleconfig) - # XXX TODO check for ConnectionError, HTTPError, InvalidUrl - # (and proper EIPExceptions are raised). + fn = os.path.join(baseconfig.get_default_provider_path(), + DEFINITION_EXPECTED_PATH) + with open(fn, 'r') as fp: + deserialized = json.load(fp) + self.assertEqual(DEFAULT_PROVIDER_DEFINITION, deserialized) + + # XXX TODO check for ConnectionError, HTTPError, InvalidUrl + # (and proper EIPExceptions are raised). + # Look at base.test_config. - # Look at base.test_config. + def test_fetch_eip_config(self): + with patch.object(requests, "get") as mocked_get: + mocked_get.return_value.status_code = 200 + mocked_get.return_value.json = eipconstants.EIP_SAMPLE_SERVICE + checker = eipchecks.EIPChecker(fetcher=requests) + sampleconfig = eipconstants.EIP_SAMPLE_JSON + checker.fetch_definition(config=sampleconfig) + + def test_check_complete_eip_config(self): + checker = eipchecks.EIPChecker() + with self.assertRaises(eipexceptions.EIPConfigurationError): + sampleconfig = copy.copy(eipconstants.EIP_SAMPLE_JSON) + sampleconfig['provider'] = None + checker.check_complete_eip_config(config=sampleconfig) + with self.assertRaises(eipexceptions.EIPConfigurationError): + sampleconfig = copy.copy(eipconstants.EIP_SAMPLE_JSON) + del sampleconfig['provider'] + checker.check_complete_eip_config(config=sampleconfig) + + # normal case + sampleconfig = copy.copy(eipconstants.EIP_SAMPLE_JSON) + checker.check_complete_eip_config(config=sampleconfig) if __name__ == "__main__": unittest.main() |