From 68c60fcdfb0c5045cbc60ac65a8f395ca28c37ff Mon Sep 17 00:00:00 2001 From: Simon Pichugin Date: Wed, 14 Oct 2015 10:48:47 +0200 Subject: [PATCH] Ticket 48303 - Fix lib389 broken tests - dereference_test Description: Add Red Hat copyright block. Remove "Created on" block, because git contains this information. Add a logging. Add docstrings to the all tests and functions. Fix expected exception assertions within the dereference test case. Refactore code to the pytest compatibility. https://fedorahosted.org/389/ticket/48303 Review by: ? --- tests/dereference_test.py | 149 +++++++++++++++++++++++++++------------------- 1 file changed, 87 insertions(+), 62 deletions(-) diff --git a/tests/dereference_test.py b/tests/dereference_test.py index f45a41b..218c94b 100644 --- a/tests/dereference_test.py +++ b/tests/dereference_test.py @@ -1,70 +1,95 @@ -''' -Created on Aug 1, 2015 - -@author: William Brown -''' +# --- 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 --- +# from lib389._constants import * from lib389 import DirSrv,Entry import ldap +import pytest +import logging + +logging.getLogger(__name__).setLevel(logging.DEBUG) +log = logging.getLogger(__name__) INSTANCE_PORT = 54321 INSTANCE_SERVERID = 'dereferenceds' -class Test_dereference(): - def setUp(self): - instance = DirSrv(verbose=False) - instance.log.debug("Instance allocated") - args = {SER_HOST: LOCALHOST, - SER_PORT: INSTANCE_PORT, - 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.db2ldif(bename='userRoot', suffixes=[DEFAULT_SUFFIX], excludeSuffixes=[], encrypt=False, \ - #repl_data=False, outputfile='%s/ldif/%s.ldif' % (self.instance.dbdir,INSTANCE_SERVERID )) - #self.instance.clearBackupFS() - #self.instance.backupFS() - self.instance.delete() - - def add_user(self): - # Create a user entry - for i in range(0,2): - uentry = Entry('uid=test%s,%s' % (i, DEFAULT_SUFFIX)) - uentry.setValues('objectclass', 'top', 'extensibleobject') - uentry.setValues('uid', 'test') - self.instance.add_s(uentry) - - def add_group(self): - gentry = Entry('cn=testgroup,%s' % DEFAULT_SUFFIX) - gentry.setValues('objectclass', 'top', 'extensibleobject') - gentry.setValues('cn', 'testgroup') - for i in range(0,2): - gentry.setValues('uniqueMember', 'uid=test%s,%s' % (i,DEFAULT_SUFFIX)) - self.instance.add_s(gentry) - - def test_dereference(self): - try: - result, control_response = self.instance.dereference('uniqueMember:dn,uid;uniqueMember:dn,uid', filterstr='(cn=testgroup)') - assert False - except ldap.UNAVAILABLE_CRITICAL_EXTENSION: - # This is a good thing! It means our deref Control Value is correctly formatted. - pass - result, control_response = self.instance.dereference('uniqueMember:cn,uid,objectclass', filterstr='(cn=testgroup)') - - assert result[0][2][0].entry == [{'derefVal': 'uid=test1,dc=example,dc=com', 'derefAttr': 'uniqueMember', 'attrVals': [{'vals': ['top', 'extensibleobject'], 'type': 'objectclass'}, {'vals': ['test', 'test1'], 'type': 'uid'}]}] - -if __name__ == "__main__": - test = Test_dereference() - test.setUp() - test.add_user() - test.add_group() - test.test_dereference() - test.tearDown() +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_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) + + +@pytest.fixture(scope='module') +def user(topology): + """Create user entries""" + + for i in range(0,2): + uentry = Entry('uid=test%s,%s' % (i, DEFAULT_SUFFIX)) + uentry.setValues('objectclass', 'top', 'extensibleobject') + uentry.setValues('uid', 'test') + topology.standalone.add_s(uentry) + + +@pytest.fixture(scope='module') +def group(topology): + """Create a group entry""" + + gentry = Entry('cn=testgroup,%s' % DEFAULT_SUFFIX) + gentry.setValues('objectclass', 'top', 'extensibleobject') + gentry.setValues('cn', 'testgroup') + for i in range(0,2): + gentry.setValues('uniqueMember', 'uid=test%s,%s' % (i,DEFAULT_SUFFIX)) + topology.standalone.add_s(gentry) + + +def test_dereference(topology, user, group): + """Test dereference search argument formating + Check, that result is correct + """ + + log.info('Check, that our dereference Control Value is correctly formatted') + with pytest.raises(ldap.UNAVAILABLE_CRITICAL_EXTENSION): + result, control_response = topology.standalone.dereference('uniqueMember:dn,uid;uniqueMember:dn,uid', + filterstr='(cn=testgroup)') + + result, control_response = topology.standalone.dereference('uniqueMember:cn,uid,objectclass', + filterstr='(cn=testgroup)') + + log.info('Check, that the dereference search result is correct') + assert result[0][2][0].entry == [{'derefVal': 'uid=test1,dc=example,dc=com', + 'derefAttr': 'uniqueMember', + 'attrVals': [{'vals': ['top', 'extensibleobject'], + 'type': 'objectclass'}, + {'vals': ['test', 'test1'], + 'type': 'uid'}]}] + + +if __name__ == '__main__': + CURRENT_FILE = os.path.realpath(__file__) + pytest.main('-s -v %s' % CURRENT_FILE) -- 2.4.3