Tuesday, June 6, 2017

[389-commits] [lib389] 02/03: Ticket 63 - part 2, agreement test

This is an automated email from the git hooks/post-receive script.

spichugi pushed a commit to branch master
in repository lib389.

commit 5335246a2a17287c77fa2502683e01df7e6912f7
Author: William Brown <firstyear@redhat.com>
Date: Tue Jun 6 10:42:02 2017 +1000

Ticket 63 - part 2, agreement test

Bug Description: This resolves a number of issues in the python
3 replication agreement test.

Fix Description: Fix bytes vs str when needed, and replace some
raw ldap calls with object types.

https://pagure.io/lib389/issue/63

Author: wibrown

Review by: ???
---
lib389/agreement.py | 22 +++++++-------
lib389/idm/user.py | 9 ++++++
lib389/replica.py | 8 +++---
lib389/sasl.py | 4 ++-
lib389/tests/agreement_test.py | 51 ++++++++++++++++-----------------
lib389/tests/idm/user_and_group_test.py | 12 ++------
lib389/utils.py | 14 +++++++--
7 files changed, 65 insertions(+), 55 deletions(-)

diff --git a/lib389/agreement.py b/lib389/agreement.py
index fabc3a4..666f5b2 100644
--- a/lib389/agreement.py
+++ b/lib389/agreement.py
@@ -14,7 +14,7 @@ import six
from lib389._constants import *
from lib389.properties import *
from lib389._entry import FormatDict
-from lib389.utils import normalizeDN
+from lib389.utils import normalizeDN, ensure_bytes, ensure_str, ensure_dict_str
from lib389 import Entry, DirSrv, NoSuchEntryError, InvalidArgumentError


@@ -97,9 +97,10 @@ class Agreement(object):
"Reap Active: %(nsds5ReplicaReapActive)s" "\n"
)
# FormatDict manages missing fields in string formatting
- result = retstr % FormatDict(ent.data)
+ entry_data = ensure_dict_str(ent.data)
+ result = retstr % FormatDict(entry_data)
result += "Replication Status: %s\n" % status
- return retstr % FormatDict(ent.data)
+ return result

def _check_interval(self, interval):
'''
@@ -173,8 +174,7 @@ class Agreement(object):

# update it
self.log.info("Schedule replication agreement %s" % agmtdn)
- mod = [(
- ldap.MOD_REPLACE, 'nsds5replicaupdateschedule', [interval])]
+ mod = [(ldap.MOD_REPLACE, 'nsds5replicaupdateschedule', [ensure_bytes(interval)])]
self.conn.modify_s(agmtdn, mod)

def getProperties(self, agmnt_dn=None, properties=None):
@@ -348,7 +348,7 @@ class Agreement(object):
if prop_name == RA_SCHEDULE:
self._check_interval(properties[prop])

- mod.append((mod_type, attr, properties[prop]))
+ mod.append((mod_type, attr, ensure_bytes(properties[prop])))

# Now time to run the effective modify
self.conn.modify_s(agmnt_dn, mod)
@@ -645,7 +645,7 @@ class Agreement(object):
# trigger the total init
#
self.log.info("Starting total init %s" % entry.dn)
- mod = [(ldap.MOD_ADD, 'nsds5BeginReplicaRefresh', 'start')]
+ mod = [(ldap.MOD_ADD, 'nsds5BeginReplicaRefresh', ensure_bytes('start'))]
self.conn.modify_s(entry.dn, mod)

def pause(self, agmtdn, interval=NEVER):
@@ -705,7 +705,7 @@ class Agreement(object):
"""Return a list of changes sent by this agreement."""
retval = 0
try:
- ent = self.conn.getEntry(agmnt_dn, ldap.SCOPE_BASE,
+ ent = self.conn.getEntry(ensure_str(agmnt_dn), ldap.SCOPE_BASE,
"(objectclass=*)",
[RA_PROPNAME_TO_ATTRNAME[RA_CHANGES]])
except:
@@ -714,12 +714,12 @@ class Agreement(object):

if ent.nsds5replicaChangesSentSinceStartup:
val = ent.nsds5replicaChangesSentSinceStartup
- items = val.split(' ')
+ items = val.split(ensure_bytes(' '))
if len(items) == 1:
retval = int(items[0])
else:
for item in items:
- ary = item.split(":")
+ ary = item.split(ensure_bytes(":"))
if ary and len(ary) > 1:
- retval = retval + int(ary[1].split("/")[0])
+ retval = retval + int(ary[1].split(ensure_bytes("/"))[0])
return retval
diff --git a/lib389/idm/user.py b/lib389/idm/user.py
index 965d0be..ee4e08d 100644
--- a/lib389/idm/user.py
+++ b/lib389/idm/user.py
@@ -21,6 +21,15 @@ MUST_ATTRIBUTES = [
]
RDN = 'uid'

+TEST_USER_PROPERTIES = {
+ 'uid': 'testuser',
+ 'cn' : 'testuser',
+ 'sn' : 'user',
+ 'uidNumber' : '1000',
+ 'gidNumber' : '2000',
+ 'homeDirectory' : '/home/testuser'
+}
+

class UserAccount(Account):
def __init__(self, instance, dn=None, batch=False):
diff --git a/lib389/replica.py b/lib389/replica.py
index 8ba527c..4de88be 100644
--- a/lib389/replica.py
+++ b/lib389/replica.py
@@ -12,7 +12,7 @@ import decimal
import time
from lib389._constants import *
from lib389.properties import *
-from lib389.utils import normalizeDN, escapeDNValue
+from lib389.utils import normalizeDN, escapeDNValue, ensure_bytes
from lib389._replication import RUV
from lib389.repltools import ReplTools
from lib389 import DirSrv, Entry, NoSuchEntryError, InvalidArgumentError
@@ -557,14 +557,14 @@ class ReplicaLegacy(object):
if not refresh: # done - check status
if not status:
print("No status yet")
- elif status.find("replica busy") > -1:
+ elif status.find(ensure_bytes("replica busy")) > -1:
print("Update failed - replica busy - status", status)
done = True
hasError = 2
- elif status.find("Total update succeeded") > -1:
+ elif status.find(ensure_bytes("Total update succeeded")) > -1:
print("Update succeeded: status ", status)
done = True
- elif inprogress.lower() == 'true':
+ elif inprogress.lower() == ensure_bytes('true'):
print("Update in progress yet not in progress: status ",
status)
else:
diff --git a/lib389/sasl.py b/lib389/sasl.py
index e64e3c3..0015f60 100644
--- a/lib389/sasl.py
+++ b/lib389/sasl.py
@@ -12,7 +12,9 @@ Lib389 python ldap sasl operations.
These should be upstreamed if possible.
"""

-from ldap.sasl import sasl, CB_AUTHNAME, CB_PASS
+from ldap.sasl import sasl, CB_AUTHNAME, CB_PASS, CB_USER
+
+from lib389.utils import ensure_bytes

class LdapSSOTokenSASL(sasl):
"""
diff --git a/lib389/tests/agreement_test.py b/lib389/tests/agreement_test.py
index 4be609d..456cbfa 100644
--- a/lib389/tests/agreement_test.py
+++ b/lib389/tests/agreement_test.py
@@ -16,6 +16,9 @@ from lib389.agreement import Agreement
from lib389._constants import *
from lib389.properties import *
from lib389 import DirSrv, Entry
+from lib389.utils import ensure_bytes, ensure_str
+from lib389.idm.user import UserAccounts, TEST_USER_PROPERTIES
+from lib389.idm.domain import Domain

# Used for One master / One consumer topology
HOST_MASTER = LOCALHOST
@@ -107,18 +110,18 @@ def test_create(topology):
topology.master.agreement.init(SUFFIX, HOST_CONSUMER, PORT_CONSUMER)
topology.master.waitForReplInit(repl_agreement)

- # Add a test entry
- topology.master.add_s(Entry((ENTRY_DN, {'objectclass':
- "top person".split(),
- 'sn': 'test_entry',
- 'cn': 'test_entry'})))
+ master_users = UserAccounts(topology.master, SUFFIX)
+ consumer_users = UserAccounts(topology.consumer, SUFFIX)
+
+ testuser = master_users.create(properties=TEST_USER_PROPERTIES)
+ testuser_dn = testuser.dn

+ # Add a test entry
# Check replication is working
loop = 0
while loop <= 10:
try:
- ent = topology.consumer.getEntry(ENTRY_DN, ldap.SCOPE_BASE,
- "(objectclass=*)")
+ consumer_users.get(dn=testuser_dn)
break
except ldap.NO_SUCH_OBJECT:
time.sleep(1)
@@ -148,9 +151,9 @@ def test_list(topology):
ents = topology.master.agreement.list(suffix=SUFFIX)
assert len(ents) == 1
assert ents[0].getValue(RA_PROPNAME_TO_ATTRNAME[RA_CONSUMER_HOST]) == \
- topology.consumer.host
+ ensure_bytes(topology.consumer.host)
assert ents[0].getValue(RA_PROPNAME_TO_ATTRNAME[RA_CONSUMER_PORT]) == \
- str(topology.consumer.port)
+ ensure_bytes(str(topology.consumer.port))

# Create a second RA to check .list returns 2 RA
properties = {RA_NAME: r'meTo_%s:%d' % (topology.consumer.host,
@@ -172,9 +175,9 @@ def test_list(topology):
consumer_port=topology.consumer.port)
assert len(ents) == 1
assert ents[0].getValue(RA_PROPNAME_TO_ATTRNAME[RA_CONSUMER_HOST]) == \
- topology.consumer.host
+ ensure_bytes(topology.consumer.host)
assert ents[0].getValue(RA_PROPNAME_TO_ATTRNAME[RA_CONSUMER_PORT]) == \
- str(topology.consumer.port)
+ ensure_bytes(str(topology.consumer.port))


def test_delete(topology):
@@ -216,7 +219,7 @@ def test_status(topology):
topology.master.log.info("\n\n###########\n## STATUS\n##########")
ents = topology.master.agreement.list(suffix=SUFFIX)
for ent in ents:
- ra_status = topology.master.agreement.status(ent.dn)
+ ra_status = topology.master.agreement.status(ensure_str(ent.dn))
assert ra_status
topology.master.log.info("Status of %s: %s" % (ent.dn, ra_status))

@@ -236,7 +239,7 @@ def test_schedule(topology):
consumer_port=topology.consumer.port)
assert len(ents) == 1
assert ents[0].getValue(RA_PROPNAME_TO_ATTRNAME[RA_SCHEDULE]) == \
- Agreement.ALWAYS
+ ensure_bytes(Agreement.ALWAYS)

topology.master.agreement.schedule(ents[0].dn, Agreement.NEVER)
ents = topology.master.agreement.list(suffix=SUFFIX,
@@ -244,7 +247,7 @@ def test_schedule(topology):
consumer_port=topology.consumer.port)
assert len(ents) == 1
assert ents[0].getValue(RA_PROPNAME_TO_ATTRNAME[RA_SCHEDULE]) == \
- Agreement.NEVER
+ ensure_bytes(Agreement.NEVER)

CUSTOM_SCHEDULE = "0000-1234 6420"
topology.master.agreement.schedule(ents[0].dn, CUSTOM_SCHEDULE)
@@ -253,7 +256,7 @@ def test_schedule(topology):
consumer_port=topology.consumer.port)
assert len(ents) == 1
assert ents[0].getValue(RA_PROPNAME_TO_ATTRNAME[RA_SCHEDULE]) == \
- CUSTOM_SCHEDULE
+ ensure_bytes(CUSTOM_SCHEDULE)

CUSTOM_SCHEDULES = ("2500-1234 6420", # Invalid HOUR schedule
"0000-2534 6420", # ^^
@@ -308,8 +311,8 @@ def test_setProperties(topology):
properties = topology.master.agreement.getProperties(
agmnt_dn=ents[0].dn, properties=[RA_SCHEDULE, RA_DESCRIPTION])
assert len(properties) == 2
- assert properties[RA_SCHEDULE][0] == test_schedule
- assert properties[RA_DESCRIPTION][0] == test_desc
+ assert properties[RA_SCHEDULE][0] == ensure_bytes(test_schedule)
+ assert properties[RA_DESCRIPTION][0] == ensure_bytes(test_desc)

# Set RA Schedule back to "always"
topology.master.agreement.schedule(ents[0].dn, Agreement.ALWAYS)
@@ -331,20 +334,16 @@ def test_changes(topology):

# Do an update
TEST_STRING = 'test_string'
- mod = [(ldap.MOD_REPLACE, 'description', [TEST_STRING])]
- topology.master.modify_s(ENTRY_DN, mod)
+ master_domain = Domain(topology.master, SUFFIX)
+ consumer_domain = Domain(topology.consumer, SUFFIX)

- ent = topology.consumer.getEntry(ENTRY_DN, ldap.SCOPE_BASE,
- "(objectclass=*)")
+ master_domain.set('description', TEST_STRING)

# The update has been replicated
loop = 0
while loop <= 10:
- ent = topology.consumer.getEntry(ENTRY_DN, ldap.SCOPE_BASE,
- "(objectclass=*)")
- if ent and ent.hasValue('description'):
- if ent.getValue('description') == TEST_STRING:
- break
+ if consumer_domain.present('description', TEST_STRING):
+ break
time.sleep(1)
loop += 1
assert loop <= 10
diff --git a/lib389/tests/idm/user_and_group_test.py b/lib389/tests/idm/user_and_group_test.py
index f072dfd..09ff74e 100644
--- a/lib389/tests/idm/user_and_group_test.py
+++ b/lib389/tests/idm/user_and_group_test.py
@@ -11,7 +11,7 @@ from lib389.properties import *
from lib389.tasks import *
from lib389.utils import *

-from lib389.idm.user import UserAccounts
+from lib389.idm.user import UserAccounts, TEST_USER_PROPERTIES
from lib389.idm.group import Groups

from lib389.topologies import topology_st as topology
@@ -46,15 +46,7 @@ def test_users_and_groups(topology):

# Create a user

- user_properties = {
- 'uid': 'testuser',
- 'cn' : 'testuser',
- 'sn' : 'user',
- 'uidNumber' : '1000',
- 'gidNumber' : '2000',
- 'homeDirectory' : '/home/testuser'
- }
- users.create(properties=user_properties)
+ users.create(properties=TEST_USER_PROPERTIES)

assert(len(users.list()) == 1)

diff --git a/lib389/utils.py b/lib389/utils.py
index a42cdaa..2108475 100644
--- a/lib389/utils.py
+++ b/lib389/utils.py
@@ -782,16 +782,24 @@ def ensure_bytes(val):
return val.encode()
return val

-
def ensure_str(val):
if val != None and type(val) != str:
return val.decode('utf-8')
return val

-
def ensure_list_bytes(val):
- # if MAJOR >= 3:
return [ensure_bytes(v) for v in val]

def ensure_list_str(val):
return [ensure_str(v) for v in val]
+
+def ensure_dict_str(val):
+ if MAJOR <= 2:
+ return val
+ retdict = {}
+ for k in val:
+ if isinstance(val[k], list):
+ retdict[k] = ensure_list_str(val[k])
+ else:
+ retdict[k] = ensure_str(val[k])
+ return retdict

--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
_______________________________________________
389-commits mailing list -- 389-commits@lists.fedoraproject.org
To unsubscribe send an email to 389-commits-leave@lists.fedoraproject.org

No comments:

Post a Comment