mirror of
https://salsa.debian.org/freeipa-team/freeipa.git
synced 2025-02-25 18:55:28 -06:00
tests: tracker: Split Tracker into one-purpose Trackers
There are multiple types of entries and objects accessible in API and not all of them have the same set methods. Spliting Tracker into multiple trackers should reflect this better. https://pagure.io/freeipa/issue/7105 Reviewed-By: Florence Blanc-Renaud <frenaud@redhat.com>
This commit is contained in:
parent
9c1ab3ca50
commit
ac7712c93e
@ -15,61 +15,7 @@ from ipapython.version import API_VERSION
|
||||
from ipatests.util import Fuzzy
|
||||
|
||||
|
||||
class Tracker(object):
|
||||
"""Wraps and tracks modifications to a plugin LDAP entry object
|
||||
|
||||
Stores a copy of state of a plugin entry object and allows checking that
|
||||
the state in the database is the same as expected.
|
||||
This allows creating independent tests: the individual tests check
|
||||
that the relevant changes have been made. At the same time
|
||||
the entry doesn't need to be recreated and cleaned up for each test.
|
||||
|
||||
Two attributes are used for tracking: ``exists`` (true if the entry is
|
||||
supposed to exist) and ``attrs`` (a dict of LDAP attributes that are
|
||||
expected to be returned from IPA commands).
|
||||
|
||||
For commonly used operations, there is a helper method, e.g.
|
||||
``create``, ``update``, or ``find``, that does these steps:
|
||||
|
||||
* ensure the entry exists (or does not exist, for "create")
|
||||
* store the expected modifications
|
||||
* get the IPA command to run, and run it
|
||||
* check that the result matches the expected state
|
||||
|
||||
Tests that require customization of these steps are expected to do them
|
||||
manually, using lower-level methods.
|
||||
Especially the first step (ensure the entry exists) is important for
|
||||
achieving independent tests.
|
||||
|
||||
The Tracker object also stores information about the entry, e.g.
|
||||
``dn``, ``rdn`` and ``name`` which is derived from DN property.
|
||||
|
||||
To use this class, the programer must subclass it and provide the
|
||||
implementation of following methods:
|
||||
|
||||
* make_*_command -- implementing the API call for particular plugin
|
||||
and operation (add, delete, ...)
|
||||
These methods should use the make_command method
|
||||
* check_* commands -- an assertion for a plugin command (CRUD)
|
||||
* track_create -- to make an internal representation of the
|
||||
entry
|
||||
|
||||
Apart from overriding these methods, the subclass must provide the
|
||||
distinguished name of the entry in `self.dn` property.
|
||||
|
||||
It is also required to override the class variables defining the sets
|
||||
of ldap attributes/keys for these operations specific to the plugin
|
||||
being implemented. Take the host plugin test for an example.
|
||||
|
||||
The implementation of these methods is not strictly enforced.
|
||||
A missing method will cause a NotImplementedError during runtime
|
||||
as a result.
|
||||
"""
|
||||
retrieve_keys = None
|
||||
retrieve_all_keys = None
|
||||
create_keys = None
|
||||
update_keys = None
|
||||
|
||||
class BaseTracker(object):
|
||||
_override_me_msg = "This method needs to be overridden in a subclass"
|
||||
|
||||
def __init__(self, default_version=None):
|
||||
@ -78,8 +24,6 @@ class Tracker(object):
|
||||
self._dn = None
|
||||
self.attrs = {}
|
||||
|
||||
self.exists = False
|
||||
|
||||
@property
|
||||
def dn(self):
|
||||
"""A property containing the distinguished name of the entry."""
|
||||
@ -137,6 +81,159 @@ class Tracker(object):
|
||||
"""Make a functools.partial function to run the given command"""
|
||||
return functools.partial(self.run_command, name, *args, **options)
|
||||
|
||||
def make_fixture(self, request):
|
||||
"""Make fixture for the tracker
|
||||
|
||||
Don't do anything here.
|
||||
"""
|
||||
return self
|
||||
|
||||
|
||||
class RetrievalTracker(BaseTracker):
|
||||
retrieve_keys = None
|
||||
retrieve_all_keys = None
|
||||
|
||||
def make_retrieve_command(self, all=False, raw=False):
|
||||
"""Make function that retrieves the entry using ${CMD}_show"""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def check_retrieve(self, result, all=False, raw=False):
|
||||
"""Check the plugin's `show` command result"""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def retrieve(self, all=False, raw=False):
|
||||
"""Helper function to retrieve an entry and check the result"""
|
||||
command = self.make_retrieve_command(all=all, raw=raw)
|
||||
result = command()
|
||||
self.check_retrieve(result, all=all, raw=raw)
|
||||
|
||||
|
||||
class SearchTracker(BaseTracker):
|
||||
|
||||
def make_find_command(self, *args, **kwargs):
|
||||
"""Make function that finds the entry using ${CMD}_find
|
||||
|
||||
Note that the name (or other search terms) needs to be specified
|
||||
in arguments.
|
||||
"""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def check_find(self, result, all=False, raw=False):
|
||||
"""Check the plugin's `find` command result"""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def find(self, all=False, raw=False):
|
||||
"""Helper function to search for this hosts and check the result"""
|
||||
command = self.make_find_command(self.name, all=all, raw=raw)
|
||||
result = command()
|
||||
self.check_find(result, all=all, raw=raw)
|
||||
|
||||
|
||||
class ModificationTracker(BaseTracker):
|
||||
update_keys = None
|
||||
|
||||
def make_update_command(self, updates):
|
||||
"""Make function that modifies the entry using ${CMD}_mod"""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def check_update(self, result, extra_keys=()):
|
||||
"""Check the plugin's `mod` command result"""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def update(self, updates, expected_updates=None):
|
||||
"""Helper function to update this hosts and check the result
|
||||
|
||||
The ``updates`` are used as options to the *_mod command,
|
||||
and the self.attrs is updated with this dict.
|
||||
Additionally, self.attrs is updated with ``expected_updates``.
|
||||
"""
|
||||
if expected_updates is None:
|
||||
expected_updates = {}
|
||||
|
||||
command = self.make_update_command(updates)
|
||||
result = command()
|
||||
self.attrs.update(updates)
|
||||
self.attrs.update(expected_updates)
|
||||
for key, value in self.attrs.items():
|
||||
if value is None:
|
||||
del self.attrs[key]
|
||||
|
||||
self.check_update(
|
||||
result,
|
||||
extra_keys=set(updates.keys()) | set(expected_updates.keys())
|
||||
)
|
||||
|
||||
|
||||
class CreationTracker(BaseTracker):
|
||||
create_keys = None
|
||||
|
||||
def __init__(self, default_version=None):
|
||||
super(CreationTracker, self).__init__(default_version=default_version)
|
||||
self.exists = False
|
||||
|
||||
def make_create_command(self):
|
||||
"""Make function that creates the plugin entry object."""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def track_create(self):
|
||||
"""Update expected state for host creation
|
||||
|
||||
The method should look similar to the following
|
||||
example of host plugin.
|
||||
|
||||
self.attrs = dict(
|
||||
dn=self.dn,
|
||||
fqdn=[self.fqdn],
|
||||
description=[self.description],
|
||||
... # all required attributes
|
||||
)
|
||||
self.exists = True
|
||||
"""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def check_create(self, result):
|
||||
"""Check plugin's add command result"""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def create(self):
|
||||
"""Helper function to create an entry and check the result"""
|
||||
self.track_create()
|
||||
command = self.make_create_command()
|
||||
result = command()
|
||||
self.check_create(result)
|
||||
|
||||
def ensure_exists(self):
|
||||
"""If the entry does not exist (according to tracker state), create it
|
||||
"""
|
||||
if not self.exists:
|
||||
self.create()
|
||||
|
||||
def make_delete_command(self):
|
||||
"""Make function that deletes the plugin entry object."""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def track_delete(self):
|
||||
"""Update expected state for host deletion"""
|
||||
self.exists = False
|
||||
self.attrs = {}
|
||||
|
||||
def check_delete(self, result):
|
||||
"""Check plugin's `del` command result"""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def delete(self):
|
||||
"""Helper function to delete a host and check the result"""
|
||||
self.track_delete()
|
||||
command = self.make_delete_command()
|
||||
result = command()
|
||||
self.check_delete(result)
|
||||
|
||||
def ensure_missing(self):
|
||||
"""If the entry exists (according to tracker state), delete it
|
||||
"""
|
||||
if self.exists:
|
||||
self.delete()
|
||||
|
||||
def make_fixture(self, request):
|
||||
"""Make a pytest fixture for this tracker
|
||||
|
||||
@ -160,96 +257,7 @@ class Tracker(object):
|
||||
|
||||
request.addfinalizer(cleanup)
|
||||
|
||||
return self
|
||||
|
||||
def ensure_exists(self):
|
||||
"""If the entry does not exist (according to tracker state), create it
|
||||
"""
|
||||
if not self.exists:
|
||||
self.create()
|
||||
|
||||
def ensure_missing(self):
|
||||
"""If the entry exists (according to tracker state), delete it
|
||||
"""
|
||||
if self.exists:
|
||||
self.delete()
|
||||
|
||||
def make_create_command(self):
|
||||
"""Make function that creates the plugin entry object."""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def make_delete_command(self):
|
||||
"""Make function that deletes the plugin entry object."""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def make_retrieve_command(self, all=False, raw=False):
|
||||
"""Make function that retrieves the entry using ${CMD}_show"""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def make_find_command(self, *args, **kwargs):
|
||||
"""Make function that finds the entry using ${CMD}_find
|
||||
|
||||
Note that the name (or other search terms) needs to be specified
|
||||
in arguments.
|
||||
"""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def make_update_command(self, updates):
|
||||
"""Make function that modifies the entry using ${CMD}_mod"""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def create(self):
|
||||
"""Helper function to create an entry and check the result"""
|
||||
self.track_create()
|
||||
command = self.make_create_command()
|
||||
result = command()
|
||||
self.check_create(result)
|
||||
|
||||
def track_create(self):
|
||||
"""Update expected state for host creation
|
||||
|
||||
The method should look similar to the following
|
||||
example of host plugin.
|
||||
|
||||
self.attrs = dict(
|
||||
dn=self.dn,
|
||||
fqdn=[self.fqdn],
|
||||
description=[self.description],
|
||||
... # all required attributes
|
||||
)
|
||||
self.exists = True
|
||||
"""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def check_create(self, result):
|
||||
"""Check plugin's add command result"""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def delete(self):
|
||||
"""Helper function to delete a host and check the result"""
|
||||
self.track_delete()
|
||||
command = self.make_delete_command()
|
||||
result = command()
|
||||
self.check_delete(result)
|
||||
|
||||
def track_delete(self):
|
||||
"""Update expected state for host deletion"""
|
||||
self.exists = False
|
||||
self.attrs = {}
|
||||
|
||||
def check_delete(self, result):
|
||||
"""Check plugin's `del` command result"""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
def retrieve(self, all=False, raw=False):
|
||||
"""Helper function to retrieve an entry and check the result"""
|
||||
command = self.make_retrieve_command(all=all, raw=raw)
|
||||
result = command()
|
||||
self.check_retrieve(result, all=all, raw=raw)
|
||||
|
||||
def check_retrieve(self, result, all=False, raw=False):
|
||||
"""Check the plugin's `show` command result"""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
return super(CreationTracker, self).make_fixture(request)
|
||||
|
||||
def find(self, all=False, raw=False):
|
||||
"""Helper function to search for this hosts and check the result"""
|
||||
@ -282,6 +290,56 @@ class Tracker(object):
|
||||
self.check_update(result, extra_keys=set(updates.keys()) |
|
||||
set(expected_updates.keys()))
|
||||
|
||||
def check_update(self, result, extra_keys=()):
|
||||
"""Check the plugin's `mod` command result"""
|
||||
raise NotImplementedError(self._override_me_msg)
|
||||
|
||||
class Tracker(RetrievalTracker, SearchTracker, ModificationTracker,
|
||||
CreationTracker):
|
||||
"""Wraps and tracks modifications to a plugin LDAP entry object
|
||||
|
||||
Stores a copy of state of a plugin entry object and allows checking that
|
||||
the state in the database is the same as expected.
|
||||
This allows creating independent tests: the individual tests check
|
||||
that the relevant changes have been made. At the same time
|
||||
the entry doesn't need to be recreated and cleaned up for each test.
|
||||
|
||||
Two attributes are used for tracking: ``exists`` (true if the entry is
|
||||
supposed to exist) and ``attrs`` (a dict of LDAP attributes that are
|
||||
expected to be returned from IPA commands).
|
||||
|
||||
For commonly used operations, there is a helper method, e.g.
|
||||
``create``, ``update``, or ``find``, that does these steps:
|
||||
|
||||
* ensure the entry exists (or does not exist, for "create")
|
||||
* store the expected modifications
|
||||
* get the IPA command to run, and run it
|
||||
* check that the result matches the expected state
|
||||
|
||||
Tests that require customization of these steps are expected to do them
|
||||
manually, using lower-level methods.
|
||||
Especially the first step (ensure the entry exists) is important for
|
||||
achieving independent tests.
|
||||
|
||||
The Tracker object also stores information about the entry, e.g.
|
||||
``dn``, ``rdn`` and ``name`` which is derived from DN property.
|
||||
|
||||
To use this class, the programer must subclass it and provide the
|
||||
implementation of following methods:
|
||||
|
||||
* make_*_command -- implementing the API call for particular plugin
|
||||
and operation (add, delete, ...)
|
||||
These methods should use the make_command method
|
||||
* check_* commands -- an assertion for a plugin command (CRUD)
|
||||
* track_create -- to make an internal representation of the
|
||||
entry
|
||||
|
||||
Apart from overriding these methods, the subclass must provide the
|
||||
distinguished name of the entry in `self.dn` property.
|
||||
|
||||
It is also required to override the class variables defining the sets
|
||||
of ldap attributes/keys for these operations specific to the plugin
|
||||
being implemented. Take the host plugin test for an example.
|
||||
|
||||
The implementation of these methods is not strictly enforced.
|
||||
A missing method will cause a NotImplementedError during runtime
|
||||
as a result.
|
||||
"""
|
||||
pass
|
||||
|
Loading…
Reference in New Issue
Block a user