diff options
author | kali <kali@leap.se> | 2012-08-27 23:21:43 +0900 |
---|---|---|
committer | kali <kali@leap.se> | 2012-08-27 23:31:39 +0900 |
commit | 4a46723219e5284bec21b9dccd6589a670babc63 (patch) | |
tree | b2d435b0c7e616c91cbf92b185f097985aec041e /src/leap/eip | |
parent | e896190d159342d9819f0ad6f11fe01deb8eb9e5 (diff) |
add test_dump_default_eipconfig to eip.test_checks
plus a little bit of cleaning around (created constants file).
added some notes about inminent deprecation *work in progress*
Diffstat (limited to 'src/leap/eip')
-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 |
5 files changed, 114 insertions, 31 deletions
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() |