diff options
author | Petr Viktorin <pviktori@redhat.com> | 2013-12-11 19:36:46 +0100 |
---|---|---|
committer | Petr Viktorin <pviktori@redhat.com> | 2014-03-05 10:00:58 +0100 |
commit | ef0264f75fb55e08ef4d54c7fe5936123e35ef27 (patch) | |
tree | 80c114c11b9e1be60fe0813e7852732479390d71 /ipatests | |
parent | 310d8254ed7659e39f61a7ff9f7eedc8a001be45 (diff) | |
download | freeipa-ef0264f75fb55e08ef4d54c7fe5936123e35ef27.tar.gz freeipa-ef0264f75fb55e08ef4d54c7fe5936123e35ef27.tar.xz freeipa-ef0264f75fb55e08ef4d54c7fe5936123e35ef27.zip |
test_integration.config: Load/store from/to dicts
Part of the work for: https://fedorahosted.org/freeipa/ticket/3938
Reviewed-By: Tomas Babej <tbabej@redhat.com>
Diffstat (limited to 'ipatests')
-rw-r--r-- | ipatests/test_integration/config.py | 51 | ||||
-rw-r--r-- | ipatests/test_integration/host.py | 37 | ||||
-rw-r--r-- | ipatests/test_integration/util.py | 10 |
3 files changed, 93 insertions, 5 deletions
diff --git a/ipatests/test_integration/config.py b/ipatests/test_integration/config.py index aa3fd5e3b..080eed284 100644 --- a/ipatests/test_integration/config.py +++ b/ipatests/test_integration/config.py @@ -27,9 +27,8 @@ import random from ipapython import ipautil from ipapython.dn import DN from ipapython.ipa_log_manager import log_mgr -from ipatests.test_integration.host import BaseHost, Host - -TESTHOST_PREFIX = 'TESTHOST_' +from ipatests.test_integration.util import check_config_dict_empty +from ipatests.test_integration.util import TESTHOST_PREFIX _SettingInfo = collections.namedtuple('Setting', 'name var_name default') @@ -94,6 +93,27 @@ class Config(object): return filter(lambda d: d.type == 'AD', self.domains) @classmethod + def from_dict(cls, dct): + kwargs = {s.name: dct.pop(s.name, s.default) for s in _setting_infos} + self = cls(**kwargs) + + for domain_dict in dct.pop('domains'): + self.domains.append(Domain.from_dict(domain_dict, self)) + + check_config_dict_empty(dct, 'config') + + return self + + def to_dict(self): + dct = {'domains': [d.to_dict() for d in self.domains]} + for setting in _setting_infos: + value = getattr(self, setting.name) + if isinstance(value, DN): + value = str(value) + dct[setting.name] = value + return dct + + @classmethod def from_env(cls, env): """Create a test config from environment variables @@ -316,7 +336,32 @@ class Domain(object): yield role @classmethod + def from_dict(cls, dct, config): + from ipatests.test_integration.host import BaseHost + + domain_type = dct.pop('type') + assert domain_type in ('IPA', 'AD') + domain_name = dct.pop('name') + self = cls(config, domain_name, domain_type) + + for host_dict in dct.pop('hosts'): + host = BaseHost.from_dict(host_dict, self) + self.hosts.append(host) + + check_config_dict_empty(dct, 'domain %s' % domain_name) + + return self + + def to_dict(self): + return { + 'type': self.type, + 'name': self.name, + 'hosts': [h.to_dict() for h in self.hosts], + } + + @classmethod def from_env(cls, env, config, index, domain_type): + from ipatests.test_integration.host import BaseHost # Roles available in the domain depend on the type of the domain # Unix machines are added only to the IPA domains, Windows machines diff --git a/ipatests/test_integration/host.py b/ipatests/test_integration/host.py index 58f1fe4d9..f78933c0c 100644 --- a/ipatests/test_integration/host.py +++ b/ipatests/test_integration/host.py @@ -26,6 +26,8 @@ from ipapython.ipaldap import IPAdmin from ipapython import ipautil from ipapython.ipa_log_manager import log_mgr from ipatests.test_integration import transport +from ipatests.test_integration.util import check_config_dict_empty +from ipatests.test_integration.util import TESTHOST_PREFIX class BaseHost(object): @@ -99,6 +101,10 @@ class BaseHost(object): external_hostname = env.get( 'BEAKER%s%s_env%s' % (role.upper(), index, domain_index), None) + return cls._make_host(domain, hostname, role, ip, external_hostname) + + @staticmethod + def _make_host(domain, hostname, role, ip, external_hostname): # We need to determine the type of the host, this depends on the domain # type, as we assume all Unix machines are in the Unix domain and # all Windows machine in a AD domain @@ -108,8 +114,35 @@ class BaseHost(object): else: cls = Host - self = cls(domain, hostname, role, ip, external_hostname) - return self + return cls(domain, hostname, role, ip, external_hostname) + + @classmethod + def from_dict(cls, dct, domain): + if isinstance(dct, basestring): + dct = {'name': dct} + try: + role = dct.pop('role').lower() + except KeyError: + role = domain.static_roles[0] + + hostname = dct.pop('name') + if '.' not in hostname: + hostname = '.'.join((hostname, domain.name)) + + ip = dct.pop('ip', None) + external_hostname = dct.pop('external_hostname', None) + + check_config_dict_empty(dct, 'host %s' % hostname) + + return cls._make_host(domain, hostname, role, ip, external_hostname) + + def to_dict(self): + return { + 'name': self.hostname, + 'ip': self.ip, + 'role': self.role, + 'external_hostname': self.external_hostname, + } @property def config(self): diff --git a/ipatests/test_integration/util.py b/ipatests/test_integration/util.py index 1a1bb3fcc..b2b433519 100644 --- a/ipatests/test_integration/util.py +++ b/ipatests/test_integration/util.py @@ -20,6 +20,16 @@ import time +TESTHOST_PREFIX = 'TESTHOST_' + + +def check_config_dict_empty(dct, name): + """Ensure that no keys are left in a configuration dict""" + if dct: + raise ValueError('Extra keys in confuguration for %s: %s' % + (name, ', '.join(dct))) + + def run_repeatedly(host, command, assert_zero_rc=True, test=None, timeout=30, **kwargs): """ |