diff options
-rw-r--r-- | src/leap/base/config.py | 3 | ||||
-rw-r--r-- | src/leap/base/tests/test_config.py | 25 | ||||
-rw-r--r-- | src/leap/eip/checks.py | 49 | ||||
-rw-r--r-- | src/leap/eip/config.py | 37 | ||||
-rw-r--r-- | src/leap/eip/constants.py | 20 | ||||
-rw-r--r-- | src/leap/eip/tests/test_checks.py | 35 | ||||
-rw-r--r-- | src/leap/eip/tests/test_config.py | 4 |
7 files changed, 123 insertions, 50 deletions
diff --git a/src/leap/base/config.py b/src/leap/base/config.py index 4d7db018..93a89638 100644 --- a/src/leap/base/config.py +++ b/src/leap/base/config.py @@ -162,7 +162,7 @@ def get_groupname(): # json stuff -# XXX merge with JSONConfig +# XXX merge with JSONConfig / EIPChecks as appropiate. def get_config_json(config_file=None): """ will replace get_config function be developing them @@ -236,6 +236,7 @@ class Configuration(object): except requests.ConnectionError as e: if e.message == "[Errno 113] No route to host": if not is_internet_up: + # this was meant to be a function invocation I guess... self.error = "No valid internet connection found" else: self.error = "Provider server appears currently down." diff --git a/src/leap/base/tests/test_config.py b/src/leap/base/tests/test_config.py index 67be0e54..93de0782 100644 --- a/src/leap/base/tests/test_config.py +++ b/src/leap/base/tests/test_config.py @@ -10,6 +10,7 @@ import requests from leap.base import config from leap.base import constants from leap.base import exceptions +from leap.eip import constants as eipconstants from leap.util.fileutil import mkdir_p from leap.testing.basetest import BaseLeapTest @@ -50,26 +51,13 @@ class ProviderDefinitionTestCase(ProviderTest): def setUp(self): # dump a sample eip file # XXX Move to Use EIP Spec Instead!!! - EIP_JSON = { - "provider": "testprovider.org", - "transport": "openvpn", - "openvpn_protocol": "tcp", - "openvpn_port": "80", - "openvpn_ca_certificate": "~/.config/leap/testprovider.org/" - "keys/ca/testprovider-ca-cert-" - "2013-01-01.pem", - "openvpn_client_certificate": "~/.config/leap/testprovider.org/" - "keys/client/openvpn-2012-09-31.pem", - "connect_on_login": True, - "block_cleartext_traffic": True, - "primary_gateway": "usa_west", - "secondary_gateway": "france", - "management_password": "oph7Que1othahwiech6J" - } + # XXX tests to be moved to eip.checks and eip.providers + # XXX can use eipconfig.dump_default_eipconfig + path = os.path.join(self.home, '.config', 'leap') mkdir_p(path) with open(os.path.join(path, 'eip.json'), 'w') as fp: - json.dump(EIP_JSON, fp) + json.dump(eipconstants.EIP_SAMPLE_JSON, fp) def test_complete_file(self): with mock.patch.object(requests, "get") as mock_method: @@ -88,6 +76,7 @@ class ProviderDefinitionTestCase(ProviderTest): u'serial': 1, u'services': [u'eip'], u'version': u'0.1.0'} + # XXX why init to localhost? cf = config.Configuration("http://localhost/") self.assertIn('default', cf.providers) @@ -238,7 +227,7 @@ class ConfigHelperFunctions(BaseLeapTest): """ code to be written yet """ - pass + raise NotImplementedError if __name__ == "__main__": diff --git a/src/leap/eip/checks.py b/src/leap/eip/checks.py index bb588cf7..1726e73a 100644 --- a/src/leap/eip/checks.py +++ b/src/leap/eip/checks.py @@ -1,20 +1,26 @@ import logging logger = logging.getLogger(name=__name__) +import os + +from leap.base import config as baseconfig +from leap.eip import config as eipconfig +from leap.eip import constants as eipconstants class EIPChecker(object): """ - Executes all tests needed + Several tests needed to ensure a EIPConnection can be sucessful """ - def __init__(self): - pass + #def __init__(self): + ## no init needed atm.. + #pass - def do_all_checks(self, checker=None): + def run_all(self, checker=None): """ - just runs all tests in a row. - will raise if some error encounter. + runs all checks in a row. + will raise if some error encountered. catching those exceptions is not our responsibility at this moment """ @@ -24,20 +30,32 @@ class EIPChecker(object): # let's call all tests # needed for a sane eip session. - checker.dump_default_eipconfig() + checker.check_default_eipconfig() checker.check_is_there_default_provider() checker.fetch_definition() checker.fetch_eip_config() checker.check_complete_eip_config() checker.ping_gateway() - def dump_default_eipconfig(self): - raise NotImplementedError + # public checks + + def check_default_eipconfig(self): + """ + checks if default eipconfig exists, + and dumps a default file if not + """ + # it *really* does not make sense to + # dump it right now, we can get an in-memory + # config object and dump it to disk in a + # later moment + if not self._is_there_default_eipconfig(): + self._dump_default_eipconfig() def check_is_there_default_provider(self): raise NotImplementedError def fetch_definition(self): + # check_and_get_definition_file raise NotImplementedError def fetch_eip_config(self): @@ -48,3 +66,16 @@ class EIPChecker(object): def ping_gateway(self): raise NotImplementedError + + # private helpers + + def _get_default_eipconfig_path(self): + return baseconfig.get_config_file(eipconstants.EIP_CONFIG) + + def _is_there_default_eipconfig(self): + return os.path.isfile( + self._get_default_eipconfig_path()) + + def _dump_default_eipconfig(self): + eipconfig.dump_default_eipconfig( + self._get_default_eipconfig_path()) diff --git a/src/leap/eip/config.py b/src/leap/eip/config.py index 8d5c19da..2694ca61 100644 --- a/src/leap/eip/config.py +++ b/src/leap/eip/config.py @@ -1,4 +1,5 @@ -import ConfigParser +import ConfigParser # to be deprecated +import json import logging import os import platform @@ -6,6 +7,8 @@ import socket from leap.util.fileutil import (which, mkdir_p, check_and_fix_urw_only) + +# from leap.base import config as baseconfig from leap.base.config import (get_default_provider_path, get_config_file, get_username, @@ -14,6 +17,7 @@ from leap.base.config import (get_default_provider_path, from leap.baseapp.permcheck import (is_pkexec_in_system, is_auth_agent_running) from leap.eip import exceptions as eip_exceptions +from leap.eip import constants as eipconstants logger = logging.getLogger(name=__name__) logger.setLevel('DEBUG') @@ -276,6 +280,8 @@ def get_sensible_defaults(): return defaults +# XXX to be deprecated. see dump_default_eipconfig +# and the new JSONConfig classes. def get_config(config_file=None): """ temporary method for getting configs, @@ -286,10 +292,6 @@ def get_config(config_file=None): @rtype: ConfigParser instance @rparam: a config object """ - # TODO - # - refactor out common things and get - # them to util/ or baseapp/ - defaults = get_sensible_defaults() config = ConfigParser.ConfigParser(defaults) @@ -302,21 +304,24 @@ def get_config(config_file=None): with open(fpath, 'wb') as configfile: config.write(configfile) config_file = open(fpath) - - #TODO - # - convert config_file to list; - # look in places like /etc/leap/eip.cfg - # for global settings. - # - raise warnings/error if bad options. - - # at this point, the file should exist. - # errors would have been raised above. - config.readfp(config_file) - return config +def dump_default_eipconfig(filepath): + """ + writes a sample eip config + in the given location + """ + # XXX TODO: + # use EIPConfigSpec istead + folder, filename = os.path.split(filepath) + if not os.path.isdir(folder): + mkdir_p(folder) + with open(filepath, 'w') as fp: + json.dump(eipconstants.EIP_SAMPLE_JSON, fp) + + def check_vpn_keys(config): """ performs an existance and permission check diff --git a/src/leap/eip/constants.py b/src/leap/eip/constants.py new file mode 100644 index 00000000..7124ca57 --- /dev/null +++ b/src/leap/eip/constants.py @@ -0,0 +1,20 @@ +EIP_CONFIG = "eip.json" + +EIP_SAMPLE_JSON = { + "provider": "testprovider.example.org", + "transport": "openvpn", + "openvpn_protocol": "tcp", + "openvpn_port": "80", + "openvpn_ca_certificate": "~/.config/leap/providers/" + "testprovider.example.org/" + "keys/ca/testprovider-ca-cert-" + "2013-01-01.pem", + "openvpn_client_certificate": "~/.config/leap/providers/" + "testprovider.example.org/" + "keys/client/openvpn-2012-09-31.pem", + "connect_on_login": True, + "block_cleartext_traffic": True, + "primary_gateway": "usa_west", + "secondary_gateway": "france", + "management_password": "oph7Que1othahwiech6J" +} diff --git a/src/leap/eip/tests/test_checks.py b/src/leap/eip/tests/test_checks.py index 53f8dc6c..ea2b3d15 100644 --- a/src/leap/eip/tests/test_checks.py +++ b/src/leap/eip/tests/test_checks.py @@ -1,11 +1,15 @@ +import json try: import unittest2 as unittest except ImportError: import unittest +import os from mock import Mock -from leap.eip import checks as eip_checks +from leap.base import config as baseconfig +from leap.eip import checks as eipchecks +from leap.eip import constants as eipconstants from leap.testing.basetest import BaseLeapTest @@ -19,10 +23,12 @@ class EIPCheckTest(BaseLeapTest): def tearDown(self): pass + # test methods are there, and can be called from run_all + def test_checker_should_implement_check_methods(self): - checker = eip_checks.EIPChecker() + checker = eipchecks.EIPChecker() - self.assertTrue(hasattr(checker, "dump_default_eipconfig"), + self.assertTrue(hasattr(checker, "check_default_eipconfig"), "missing meth") self.assertTrue(hasattr(checker, "check_is_there_default_provider"), "missing meth") @@ -33,11 +39,11 @@ class EIPCheckTest(BaseLeapTest): self.assertTrue(hasattr(checker, "ping_gateway"), "missing meth") def test_checker_should_actually_call_all_tests(self): - checker = eip_checks.EIPChecker() + checker = eipchecks.EIPChecker() mc = Mock() - checker.do_all_checks(checker=mc) - self.assertTrue(mc.dump_default_eipconfig.called, "not called") + checker.run_all(checker=mc) + self.assertTrue(mc.check_default_eipconfig.called, "not called") self.assertTrue(mc.check_is_there_default_provider.called, "not called") self.assertTrue(mc.fetch_definition.called, @@ -49,6 +55,23 @@ class EIPCheckTest(BaseLeapTest): self.assertTrue(mc.ping_gateway.called, "not called") + # test individual check methods + + def test_dump_default_eipconfig(self): + checker = eipchecks.EIPChecker() + # no eip config (empty home) + eipconfig = baseconfig.get_config_file(eipconstants.EIP_CONFIG) + self.assertFalse(os.path.isfile(eipconfig)) + checker.check_default_eipconfig() + # we've written one, so it should be there. + self.assertTrue(os.path.isfile(eipconfig)) + with open(eipconfig, 'rb') as fp: + deserialized = json.load(fp) + self.assertEqual(deserialized, + eipconstants.EIP_SAMPLE_JSON) + # TODO: when new JSONConfig class is in place, we shold + # run validation methods. + if __name__ == "__main__": unittest.main() diff --git a/src/leap/eip/tests/test_config.py b/src/leap/eip/tests/test_config.py index ed9fe270..fac4729d 100644 --- a/src/leap/eip/tests/test_config.py +++ b/src/leap/eip/tests/test_config.py @@ -81,6 +81,10 @@ class EIPConfigTest(BaseLeapTest): self.assertEqual(command, 'openvpn') self.assertEqual(args, self.get_expected_openvpn_args()) + # XXX TODO: + # - should use touch_exec to plant an "executabe" in the path + # - should check that "which" for openvpn returns what's expected. + if __name__ == "__main__": unittest.main() |