From 5e77b77765154850fb708e6ea188fcf7ba99fdce Mon Sep 17 00:00:00 2001 From: kali Date: Thu, 30 Aug 2012 04:37:30 +0900 Subject: add test for JSONLeapConfig metaclass --- src/leap/base/config.py | 29 +++++++++++++++++++++++----- src/leap/base/connection.py | 15 +++++++++++---- src/leap/base/exceptions.py | 4 ++++ src/leap/base/tests/test_config.py | 39 +++++++++++++++++++++++++++++++++----- src/leap/eip/checks.py | 8 ++++++++ 5 files changed, 81 insertions(+), 14 deletions(-) diff --git a/src/leap/base/config.py b/src/leap/base/config.py index 7a65474a..5a52637c 100644 --- a/src/leap/base/config.py +++ b/src/leap/base/config.py @@ -4,7 +4,6 @@ Configuration Base Class import grp import json import logging -import requests import socket import tempfile import os @@ -64,7 +63,15 @@ class MetaConfigWithSpec(type): def __new__(meta, classname, bases, classDict): spec_options = classDict.get('spec', None) - # XXX if not spec_options, raise BadConfiguration or something + # not quite happy with this workaround. + # I want to raise if missing spec dict, but only + # for grand-children of this metaclass. + # maybe should use abc module for this. + abcderived = ("JSONLeapConfig",) + if spec_options is None and classname not in abcderived: + raise exceptions.ImproperlyConfigured( + "missing spec dict on your derived class") + # we create a configuration spec attribute from the spec dict config_class = type( classname + "Spec", @@ -103,8 +110,18 @@ class JSONLeapConfig(BaseLeapConfig): def __init__(self, *args, **kwargs): # sanity check - assert self.slug is not None - assert self.spec is not None + try: + assert self.slug is not None + except AssertionError: + raise exceptions.ImproperlyConfigured( + "missing slug on JSONLeapConfig" + " derived class") + try: + assert self.spec is not None + except AssertionError: + raise exceptions.ImproperlyConfigured( + "missing spec on JSONLeapConfig" + " derived class") assert issubclass(self.spec, configuration.Configuration) self._config = self.spec() @@ -298,9 +315,11 @@ def is_internet_up(): pass return False +# XXX DEPRECATE. +# move to eip.checks # # XXX merge conflict -# tests are still using this deprecated Configuration object. +# some tests are still using this deprecated Configuration object. # moving it here transiently until I clean merge commit. # -- kali 2012-08-24 00:32 # diff --git a/src/leap/base/connection.py b/src/leap/base/connection.py index 9cdc33fa..f594d21c 100644 --- a/src/leap/base/connection.py +++ b/src/leap/base/connection.py @@ -5,18 +5,25 @@ from __future__ import (division, unicode_literals, print_function) import logging -from leap.base.config import JSONLeapConfig +#from leap.base.config import JSONLeapConfig from leap.base.authentication import Authentication logger = logging.getLogger(name=__name__) -class Connection(JSONLeapConfig, Authentication): +class Connection(Authentication): + # JSONLeapConfig + #spec = {} + def __init__(self, *args, **kwargs): self.connection_state = None self.desired_connection_state = None - #XXX FIXME this is only initializing one - #of the bases.. + #XXX FIXME diamond inheritance gotcha.. + #If you inherit from >1 class, + #super is only initializing one + #of the bases..!! + # I think we better pass config as a constructor + # parameter -- kali 2012-08-30 04:33 super(Connection, self).__init__(*args, **kwargs) def connect(self): diff --git a/src/leap/base/exceptions.py b/src/leap/base/exceptions.py index 93dde385..9c4aa77b 100644 --- a/src/leap/base/exceptions.py +++ b/src/leap/base/exceptions.py @@ -1,2 +1,6 @@ class MissingConfigFileError(Exception): pass + + +class ImproperlyConfigured(Exception): + pass diff --git a/src/leap/base/tests/test_config.py b/src/leap/base/tests/test_config.py index ef897a23..40461b99 100644 --- a/src/leap/base/tests/test_config.py +++ b/src/leap/base/tests/test_config.py @@ -23,6 +23,38 @@ except ImportError: _system = platform.system() +class JSONLeapConfigTest(BaseLeapTest): + def setUp(self): + pass + + def tearDown(self): + pass + + def test_metaclass(self): + with self.assertRaises(exceptions.ImproperlyConfigured) as exc: + class DummyTestConfig(config.JSONLeapConfig): + __metaclass__ = config.MetaConfigWithSpec + exc.startswith("missing spec dict") + + class DummyTestConfig(config.JSONLeapConfig): + __metaclass__ = config.MetaConfigWithSpec + spec = {} + with self.assertRaises(exceptions.ImproperlyConfigured) as exc: + DummyTestConfig() + exc.startswith("missing slug") + + class DummyTestConfig(config.JSONLeapConfig): + __metaclass__ = config.MetaConfigWithSpec + spec = {} + slug = "foo" + DummyTestConfig() + +######################################3 +# +# provider fetch tests block +# + + class ProviderTest(BaseLeapTest): # override per test fixtures @@ -45,7 +77,7 @@ class BareHomeTestCase(ProviderTest): class ProviderDefinitionTestCase(ProviderTest): - # XXX See how to merge with test_providers + # XXX MOVE TO eip.test_checks # -- kali 2012-08-24 00:38 __name__ = "provider_config_tests" @@ -62,10 +94,6 @@ class ProviderDefinitionTestCase(ProviderTest): json.dump(eipconstants.EIP_SAMPLE_JSON, fp) -# -# provider fetch tests block -# - # these tests below should move to wherever # we put the fetcher for provider files and related stuff. # TODO: @@ -107,6 +135,7 @@ class ProviderFetchInvalidUrl(ProviderTest): # end provider fetch tests +########################################### class ConfigHelperFunctions(BaseLeapTest): diff --git a/src/leap/eip/checks.py b/src/leap/eip/checks.py index b57977f0..1db7158f 100644 --- a/src/leap/eip/checks.py +++ b/src/leap/eip/checks.py @@ -35,6 +35,14 @@ Other related checkers - not implemented yet -: """ +class LeapNetworkChecker(object): + pass + + +class ProviderCertChecker(object): + pass + + class EIPConfigChecker(object): """ Several tests needed -- cgit v1.2.3