test_host_plugin: Convert tests to imperative style
This has several advantages: - Tests other than run-command/check-response can be added easily - Tracebacks are meaningful (which means we'll be able to remove a lot of test name/description/location tracking code) - Individual tests can be selected/deselected using normal pytest mechanisms (but for isolated tests, more changes will be needed) Reviewed-By: Tomas Babej <tbabej@redhat.com>
This commit is contained in:
1 parent
61c4ecccc1
commit
4d9112b5c6
3 files changed
+1171
-1496
No files matched your search
@@ -0,0 +1,40 @@
|
||||
# Authors:
|
||||
# Petr Viktorin <pviktori@redhat.com>
|
||||
#
|
||||
# Copyright (C) 2008 Red Hat
|
||||
# see file 'COPYING' for use and warranty information
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
"""
|
||||
Per-directory test configuration
|
||||
"""
|
||||
|
||||
import pytest
|
||||
|
||||
from ipapython.version import API_VERSION
|
||||
from ipalib import api
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def command(request):
|
||||
try:
|
||||
default_version = request.cls.default_version
|
||||
except AttributeError:
|
||||
default_version = API_VERSION
|
||||
|
||||
def cmd(cmd, *args, **options):
|
||||
options.setdefault('version', default_version)
|
||||
return api.Command[cmd](*args, **options)
|
||||
return cmd
|
||||
+1076
-1496
File diff suppressed because it is too large.
Load diff
@@ -22,7 +22,10 @@ Base class for all XML-RPC tests
|
||||
"""
|
||||
|
||||
import datetime
|
||||
|
||||
import nose
|
||||
import contextlib
|
||||
|
||||
from ipatests.util import assert_deepequal, Fuzzy
|
||||
from ipalib import api, request, errors
|
||||
from ipalib.x509 import valid_issuer
|
||||
@@ -225,6 +228,9 @@ KWARGS = """Command %r raised %s with wrong kwargs.
|
||||
class Declarative(XMLRPC_test):
|
||||
"""A declarative-style test suite
|
||||
|
||||
This class is DEPRECATED. Use RPCTest instead.
|
||||
See host plugin tests for an example.
|
||||
|
||||
A Declarative test suite is controlled by the ``tests`` and
|
||||
``cleanup_commands`` class variables.
|
||||
|
||||
@@ -341,3 +347,52 @@ class Declarative(XMLRPC_test):
|
||||
assert_deepequal(expected, got, nice)
|
||||
if extra_check and not extra_check(got):
|
||||
raise AssertionError('Extra check %s failed' % extra_check)
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def raises_exact(expected_exception):
|
||||
"""Check that a specific PublicError is raised
|
||||
|
||||
Both type and message of the error are checked.
|
||||
|
||||
>>> with raises_exact(errors.ValidationError(name='x', error='y')):
|
||||
... raise errors.ValidationError(name='x', error='y')
|
||||
"""
|
||||
try:
|
||||
yield
|
||||
except errors.PublicError as got_exception:
|
||||
assert type(expected_exception) is type(got_exception)
|
||||
# FIXME: We should return error information in a structured way.
|
||||
# For now just compare the strings
|
||||
assert expected_exception.strerror == got_exception.strerror
|
||||
else:
|
||||
raise AssertionError('did not raise!')
|
||||
|
||||
|
||||
class RPCTest(XMLRPC_test):
|
||||
"""Base class for RPC tests"""
|
||||
@classmethod
|
||||
def setup_class(cls):
|
||||
super(RPCTest, cls).setup_class()
|
||||
cls.clean_up()
|
||||
|
||||
@classmethod
|
||||
def teardown_class(cls):
|
||||
cls.clean_up()
|
||||
super(RPCTest, cls).teardown_class()
|
||||
|
||||
@classmethod
|
||||
def clean_up(self):
|
||||
"""Cleanup run on both setup and teardown
|
||||
|
||||
To be overridden in subclasses.
|
||||
Usually calls the clean() method.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
def clean(cls, command, *args, **options):
|
||||
"""Run a command, ignoring NotFound/EmptyModlist errors"""
|
||||
try:
|
||||
api.Command[command](*args, **options)
|
||||
except (errors.NotFound, errors.EmptyModlist):
|
||||
pass
|
||||
Reference in new issue
Block a user