From 8d9e8bff52971b506ef71f5b39bf8d1b6c425066 Mon Sep 17 00:00:00 2001 From: Simon Pichugin Date: Fri, 9 Oct 2015 18:12:09 +0200 Subject: [PATCH] Ticket 48303 - Fix lib389 broken tests - backend_test Description: Fix the imports to the correct ones. Add Red Hat copyright block. Remove "Created on" block, because git contains this information. Add a logging. Add docstrings to the all tests. Divide the delete test case into two: for valid and invalid cases. Fix expected exception assertions within the create and the delete_invalid test cases. Add assert statement to the toSuffix test case. Refactore code to the pytest compatibility. https://fedorahosted.org/389/ticket/48303 Review by: ? --- tests/backend_test.py | 440 ++++++++++++++++++++++++++------------------------ 1 file changed, 233 insertions(+), 207 deletions(-) diff --git a/tests/backend_test.py b/tests/backend_test.py index b4345c0..7924cf7 100644 --- a/tests/backend_test.py +++ b/tests/backend_test.py @@ -1,22 +1,25 @@ -''' -Created on Dec 17, 2013 - -@author: tbordaz -''' +# --- BEGIN COPYRIGHT BLOCK --- +# Copyright (C) 2015 Red Hat, Inc. +# All rights reserved. +# +# License: GPL (version 3 or any later version). +# See LICENSE for details. +# --- END COPYRIGHT BLOCK --- +# import os -import pwd -import ldap -from random import randint -from lib389.tools import DirSrvTools +import pytest +import logging + from lib389._constants import * from lib389.properties import * -from lib389 import DirSrv,Entry, InvalidArgumentError +from lib389 import DirSrv, InvalidArgumentError, UnwillingToPerformError +logging.getLogger(__name__).setLevel(logging.DEBUG) +log = logging.getLogger(__name__) TEST_REPL_DN = "uid=test,%s" % DEFAULT_SUFFIX INSTANCE_PORT = 54321 INSTANCE_SERVERID = 'dirsrv' -#INSTANCE_PREFIX = os.environ.get('PREFIX', None) INSTANCE_PREFIX = None INSTANCE_BACKUP = os.environ.get('BACKUPDIR', DEFAULT_BACKUPDIR) NEW_SUFFIX_1 = 'o=test_create' @@ -28,202 +31,225 @@ NEW_BACKEND_2 = 'test_bis_createdb' NEW_CHILDSUFFIX_2 = 'o=child2,o=test_bis_create' NEW_CHILDBACKEND_2 = 'test_bis_createchilddb' -class Test_Backend(object): - - - def setUp(self): - instance = DirSrv(verbose=False) - instance.log.debug("Instance allocated") - args = {SER_HOST: LOCALHOST, - SER_PORT: INSTANCE_PORT, - SER_DEPLOYED_DIR: INSTANCE_PREFIX, - SER_SERVERID_PROP: INSTANCE_SERVERID - } - instance.allocate(args) - if instance.exists(): - instance.delete() - instance.create() - instance.open() - self.instance = instance - - - def tearDown(self): - if self.instance.exists(): - self.instance.delete() - - - def test_list(self): - ents = self.instance.backend.list() - nb_backend = len(ents) - for ent in ents: - self.instance.log.info("List(%d): backend %s" % (nb_backend, ent.dn)) - - # Create a first backend and check list all backends - backendEntry = self.instance.backend.create(suffix=NEW_SUFFIX_1, properties={BACKEND_NAME: NEW_BACKEND_1}) - ents = self.instance.backend.list() - for ent in ents: - self.instance.log.info("List(%d): backend %s" % (nb_backend + 1, ent.dn)) - assert len(ents) == (nb_backend + 1) - - # Create a second backend and check list all backends - backendEntry = self.instance.backend.create(suffix=NEW_SUFFIX_2, properties={BACKEND_NAME: NEW_BACKEND_2}) - ents = self.instance.backend.list() - for ent in ents: - self.instance.log.info("List(%d): backend %s" % (nb_backend + 2, ent.dn)) - assert len(ents) == (nb_backend + 2) - - # Check list a backend per suffix - ents = self.instance.backend.list(suffix=NEW_SUFFIX_1) - for ent in ents: - self.instance.log.info("List suffix (%d): backend %s" % (1, ent.dn)) - assert len(ents) == 1 - - # Check list a backend by its name - ents = self.instance.backend.list(bename=NEW_BACKEND_2) - for ent in ents: - self.instance.log.info("List name (%d): backend %s" % (1, ent.dn)) + +class TopologyStandalone(object): + def __init__(self, standalone): + standalone.open() + self.standalone = standalone + + +@pytest.fixture(scope="module") +def topology(request): + standalone = DirSrv(verbose=False) + standalone.log.debug("Instance allocated") + args = {SER_HOST: LOCALHOST, + SER_PORT: INSTANCE_PORT, + SER_DEPLOYED_DIR: INSTANCE_PREFIX, + SER_SERVERID_PROP: INSTANCE_SERVERID} + standalone.allocate(args) + if standalone.exists(): + standalone.delete() + standalone.create() + standalone.open() + + def fin(): + standalone.delete() + request.addfinalizer(fin) + + return TopologyStandalone(standalone) + + +def test_list(topology): + """Test backend.list() function behaviour after: + - creating new suffixes + - filter with suffix + - filter with backend name (bename) + - filter with backend dn + - filter with invalid suffix/bename + """ + + ents = topology.standalone.backend.list() + nb_backend = len(ents) + for ent in ents: + topology.standalone.log.info("List(%d): backend %s" % (nb_backend, ent.dn)) + + log.info("Create a first backend and check list all backends") + topology.standalone.backend.create(suffix=NEW_SUFFIX_1, + properties={BACKEND_NAME: NEW_BACKEND_1}) + ents = topology.standalone.backend.list() + for ent in ents: + topology.standalone.log.info("List(%d): backend %s" % (nb_backend + 1, ent.dn)) + assert len(ents) == (nb_backend + 1) + + log.info("Create a second backend and check list all backends") + topology.standalone.backend.create(suffix=NEW_SUFFIX_2, + properties={BACKEND_NAME: NEW_BACKEND_2}) + ents = topology.standalone.backend.list() + for ent in ents: + topology.standalone.log.info("List(%d): backend %s" % (nb_backend + 2, ent.dn)) + assert len(ents) == (nb_backend + 2) + + log.info("Check list a backend per suffix") + ents = topology.standalone.backend.list(suffix=NEW_SUFFIX_1) + for ent in ents: + topology.standalone.log.info("List suffix (%d): backend %s" % (1, ent.dn)) + assert len(ents) == 1 + + log.info("Check list a backend by its name") + ents = topology.standalone.backend.list(bename=NEW_BACKEND_2) + for ent in ents: + topology.standalone.log.info("List name (%d): backend %s" % (1, ent.dn)) + assert len(ents) == 1 + + log.info("Check list backends by their DN") + all = topology.standalone.backend.list() + for ent in all: + ents = topology.standalone.backend.list(backend_dn=ent.dn) + for bck in ents: + topology.standalone.log.info("List DN (%d): backend %s" % (1, bck.dn)) assert len(ents) == 1 - - # Check list backends by their DN - all = self.instance.backend.list() - for ent in all: - ents = self.instance.backend.list(backend_dn=ent.dn) - for bck in ents: - self.instance.log.info("List DN (%d): backend %s" % (1, bck.dn)) - assert len(ents) == 1 - - # Check list with valid backend DN but invalid suffix/bename - all = self.instance.backend.list() - for ent in all: - ents = self.instance.backend.list(suffix="o=dummy", backend_dn=ent.dn, bename="dummydb") - for bck in ents: - self.instance.log.info("List invalid suffix+bename (%d): backend %s" % (1, bck.dn)) - assert len(ents) == 1 - - def test_create(self): - # just to make it clean before starting - self.instance.backend.delete(suffix=NEW_SUFFIX_1) - self.instance.backend.delete(suffix=NEW_SUFFIX_2) - - # create a backend - backendEntry = self.instance.backend.create(suffix=NEW_SUFFIX_1, properties={BACKEND_NAME: NEW_BACKEND_1}) - - # check missing suffix - try: - backendEntry = self.instance.backend.create() - assert backendEntry == None - except Exception as e: - self.instance.log.info('Fail to create (expected)%s: %s' % (type(e).__name__,e.args)) - pass - - # check already existing backend for that suffix - try: - backendEntry = self.instance.backend.create(suffix=NEW_SUFFIX_1) - assert backendEntry == None - except Exception as e: - self.instance.log.info('Fail to create (expected)%s: %s' % (type(e).__name__, e.args)) - pass - - # check already existing backend DN, create for a different suffix but same backend name - try: - backendEntry = self.instance.backend.create(suffix=NEW_SUFFIX_2, properties={BACKEND_NAME: NEW_BACKEND_1}) - assert backendEntry == None - except Exception as e: - self.instance.log.info('Fail to create (expected)%s: %s' % (type(e).__name__, e.args)) - pass - - # create a backend without properties - backendEntry = self.instance.backend.create(suffix=NEW_SUFFIX_2) - ents = self.instance.backend.list() - for ent in ents: - self.instance.log.info("List: backend %s" % (ent.dn)) - - - def test_delete(self): - # just to make it clean before starting - self.instance.backend.delete(suffix=NEW_SUFFIX_1) - self.instance.backend.delete(suffix=NEW_SUFFIX_2) - ents = self.instance.backend.list() - nb_backend = len(ents) - - # Check the various possibility to delete a backend - # First with suffix - backendEntry = self.instance.backend.create(suffix=NEW_SUFFIX_1, properties={BACKEND_NAME: NEW_BACKEND_1}) - self.instance.backend.delete(suffix=NEW_SUFFIX_1) - ents = self.instance.backend.list() - assert len(ents) == nb_backend - - # Second with backend name - backendEntry = self.instance.backend.create(suffix=NEW_SUFFIX_1, properties={BACKEND_NAME: NEW_BACKEND_1}) - self.instance.backend.delete(bename=NEW_BACKEND_1) - ents = self.instance.backend.list() - assert len(ents) == nb_backend - - # Third with backend DN - backendEntry = self.instance.backend.create(suffix=NEW_SUFFIX_1, properties={BACKEND_NAME: NEW_BACKEND_1}) - ents = self.instance.backend.list(suffix=NEW_SUFFIX_1) + + log.info("Check list with valid backend DN but invalid suffix/bename") + all = topology.standalone.backend.list() + for ent in all: + ents = topology.standalone.backend.list(suffix="o=dummy", backend_dn=ent.dn, bename="dummydb") + for bck in ents: + topology.standalone.log.info("List invalid suffix+bename (%d): backend %s" % (1, bck.dn)) assert len(ents) == 1 - self.instance.backend.delete(backend_dn=ents[0].dn) - ents = self.instance.backend.list() - assert len(ents) == nb_backend - - # Now check the failure cases - # First no argument -> InvalidArgumentError - try: - backendEntry = self.instance.backend.create(suffix=NEW_SUFFIX_1, properties={BACKEND_NAME: NEW_BACKEND_1}) - self.instance.backend.delete() - except Exception as e: - self.instance.log.info('Fail to delete (expected)%s: %s' % (type(e).__name__,e.args)) - self.instance.backend.delete(suffix=NEW_SUFFIX_1) - pass - - # second invalid suffix -> InvalidArgumentError - try: - self.instance.backend.delete(suffix=NEW_SUFFIX_2) - except Exception as e: - self.instance.log.info('Fail to delete (expected)%s: %s' % (type(e).__name__,e.args)) - pass - - # existing a mapping tree -> UnwillingToPerformError - backendEntry = self.instance.backend.create(suffix=NEW_SUFFIX_1, properties={BACKEND_NAME: NEW_BACKEND_1}) - self.instance.mappingtree.create(NEW_SUFFIX_1, bename=NEW_BACKEND_1) - try: - self.instance.backend.delete(suffix=NEW_SUFFIX_1) - except Exception as e: - self.instance.log.info('Fail to delete (expected)%s: %s' % (type(e).__name__,e.args)) - self.instance.mappingtree.delete(suffix=NEW_SUFFIX_1,bename=NEW_BACKEND_1) - self.instance.backend.delete(suffix=NEW_SUFFIX_1) - - # backend name differs -> UnwillingToPerformError - backendEntry = self.instance.backend.create(suffix=NEW_SUFFIX_1, properties={BACKEND_NAME: NEW_BACKEND_1}) - try: - self.instance.backend.delete(suffix=NEW_SUFFIX_1, bename='dummydb') - except Exception as e: - self.instance.log.info('Fail to delete (expected)%s: %s' % (type(e).__name__,e.args)) - self.instance.backend.delete(suffix=NEW_SUFFIX_1) - - def test_toSuffix(self): - backendEntry = self.instance.backend.create(suffix=NEW_SUFFIX_1, properties={BACKEND_NAME: NEW_BACKEND_1}) - self.instance.mappingtree.create(NEW_SUFFIX_1, bename=NEW_BACKEND_1) - - ents = self.instance.backend.list() - for ent in ents: - suffix = ent.getValues(BACKEND_PROPNAME_TO_ATTRNAME[BACKEND_SUFFIX]) - values = self.instance.backend.toSuffix(name=ent.dn) - if not suffix[0] in values: - self.instance.log.info("Fail we do not retrieve the suffix %s in %s" % (suffix[0], values)) - else: - self.instance.log.info("%s retrieved" % suffix) - - + + log.info("Just to make it clean in the end") + topology.standalone.backend.delete(suffix=NEW_SUFFIX_1) + topology.standalone.backend.delete(suffix=NEW_SUFFIX_2) + + +def test_create(topology): + """Test backend.create() function with: + - specifying no suffix + - specifying already existing backend suffix + - specifying already existing backend name, but new suffix + - specifying no properties + """ + + log.info("Create a backend") + topology.standalone.backend.create(suffix=NEW_SUFFIX_1, + properties={BACKEND_NAME: NEW_BACKEND_1}) + + log.info("Check behaviour with missing suffix") + with pytest.raises(ValueError) as excinfo: + topology.standalone.backend.create() + assert 'suffix is mandatory' in str(excinfo.value) + + log.info("Check behaviour with already existing backend for that suffix") + with pytest.raises(InvalidArgumentError) as excinfo: + topology.standalone.backend.create(suffix=NEW_SUFFIX_1) + assert 'It already exists backend(s)' in str(excinfo.value) + + log.info("Check behaviour with already existing backend DN, but new suffix") + with pytest.raises(InvalidArgumentError) as excinfo: + topology.standalone.backend.create(suffix=NEW_SUFFIX_2, + properties={BACKEND_NAME: NEW_BACKEND_1}) + assert 'It already exists a backend with that DN' in str(excinfo.value) + + log.info("Create a backend without properties") + topology.standalone.backend.create(suffix=NEW_SUFFIX_2) + ents = topology.standalone.backend.list(suffix=NEW_SUFFIX_2) + assert len(ents) == 1 + + log.info("Just to make it clean in the end") + topology.standalone.backend.delete(suffix=NEW_SUFFIX_1) + topology.standalone.backend.delete(suffix=NEW_SUFFIX_2) + + +def test_delete_valid(topology): + """Test the various possibilities to delete a backend: + - with suffix + - with backend name + - with backend DN + """ + + ents = topology.standalone.backend.list() + nb_backend = len(ents) + + log.info("Try to delete a backend with suffix") + topology.standalone.backend.create(suffix=NEW_SUFFIX_1, + properties={BACKEND_NAME: NEW_BACKEND_1}) + topology.standalone.backend.delete(suffix=NEW_SUFFIX_1) + ents = topology.standalone.backend.list() + assert len(ents) == nb_backend + + log.info("Try to delete a backend with backend name") + topology.standalone.backend.create(suffix=NEW_SUFFIX_1, + properties={BACKEND_NAME: NEW_BACKEND_1}) + topology.standalone.backend.delete(bename=NEW_BACKEND_1) + ents = topology.standalone.backend.list() + assert len(ents) == nb_backend + + log.info("Try to delete a backend with backend DN") + topology.standalone.backend.create(suffix=NEW_SUFFIX_1, + properties={BACKEND_NAME: NEW_BACKEND_1}) + ents = topology.standalone.backend.list(suffix=NEW_SUFFIX_1) + assert len(ents) == 1 + topology.standalone.backend.delete(backend_dn=ents[0].dn) + ents = topology.standalone.backend.list() + assert len(ents) == nb_backend + + +def test_delete_invalid(topology): + """Test the invalid situations with the backend removal: + - no argument + - invalid suffix + - existing a mapping tree + - backend name differs + """ + + topology.standalone.backend.create(suffix=NEW_SUFFIX_1, + properties={BACKEND_NAME: NEW_BACKEND_1}) + topology.standalone.mappingtree.create(NEW_SUFFIX_1, bename=NEW_BACKEND_1) + + log.info("First no argument -> InvalidArgumentError") + with pytest.raises(InvalidArgumentError) as excinfo: + topology.standalone.backend.delete() + assert 'suffix and backend DN and backend name are missing' in str(excinfo.value) + + log.info("Second invalid suffix -> InvalidArgumentError") + with pytest.raises(InvalidArgumentError) as excinfo: + topology.standalone.backend.delete(suffix=NEW_SUFFIX_2) + assert 'Unable to retrieve the backend' in str(excinfo.value) + + log.info("Existing a mapping tree -> UnwillingToPerformError") + with pytest.raises(UnwillingToPerformError) as excinfo: + topology.standalone.backend.delete(suffix=NEW_SUFFIX_1) + assert 'It still exists a mapping tree' in str(excinfo.value) + topology.standalone.mappingtree.delete(suffix=NEW_SUFFIX_1, bename=NEW_BACKEND_1) + + log.info("Backend name differs -> UnwillingToPerformError") + with pytest.raises(UnwillingToPerformError) as excinfo: + topology.standalone.backend.delete(suffix=NEW_SUFFIX_1, bename='dummydb') + assert 'Backend name specified (dummydb) differs from' in str(excinfo.value) + topology.standalone.backend.delete(suffix=NEW_SUFFIX_1) + + +def test_toSuffix(topology): + """Test backend.toSuffix() function + by comparing its result to the true value + """ + + log.info("Create one backend") + topology.standalone.backend.create(suffix=NEW_SUFFIX_1, + properties={BACKEND_NAME: NEW_BACKEND_1}) + + log.info("Run through all backends and compare backend.toSuffix() "\ + "function results with true values taken from attributes") + ents = topology.standalone.backend.list() + for ent in ents: + suffix = ent.getValues(BACKEND_PROPNAME_TO_ATTRNAME[BACKEND_SUFFIX]) + values = topology.standalone.backend.toSuffix(name=ent.dn) + assert suffix[0] in values + + log.info("Clean up after test") + topology.standalone.backend.delete(suffix=NEW_SUFFIX_1) + + if __name__ == "__main__": - test = Test_Backend() - test.setUp() - - test.test_list() - test.test_create() - test.test_delete() - test.test_toSuffix() - - test.tearDown() - + CURRENT_FILE = os.path.realpath(__file__) + pytest.main("-s -v %s" % CURRENT_FILE) -- 2.4.3