From 132d90b53ff03f2b87e9ff38235b8ce775df5dc4 Mon Sep 17 00:00:00 2001
From: Simon Pichugin <spichugi@redhat.com>
Date: Fri, 29 Jul 2016 13:40:41 +0200
Subject: [PATCH] Ticket 48832 - Fix timing and localhost issues

Description: Add smart time.sleep() before a checking of access log.
Replace 'localhost' with DirSrvTools.getLocalhost() function for
rootdn-allow-host and rootdn-deny-host attrs.

https://fedorahosted.org/389/ticket/48832

Reviewed by: ?
---
 .../suites/rootdn_plugin/rootdn_plugin_test.py     |  6 ++-
 dirsrvtests/tests/tickets/ticket1347760_test.py    | 44 +++++++++++-----------
 dirsrvtests/tests/tickets/ticket47838_test.py      | 25 ++++++------
 3 files changed, 38 insertions(+), 37 deletions(-)

diff --git a/dirsrvtests/tests/suites/rootdn_plugin/rootdn_plugin_test.py b/dirsrvtests/tests/suites/rootdn_plugin/rootdn_plugin_test.py
index 7b2c01a..a904644 100644
--- a/dirsrvtests/tests/suites/rootdn_plugin/rootdn_plugin_test.py
+++ b/dirsrvtests/tests/suites/rootdn_plugin/rootdn_plugin_test.py
@@ -377,13 +377,14 @@ def test_rootdn_access_denied_host(topology):
 
     log.info('Running test_rootdn_access_denied_host...')
     hostname = socket.gethostname()
+    localhost = DirSrvTools.getLocalhost()
     try:
         topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_ADD,
                                                   'rootdn-deny-host',
                                                   hostname)])
         topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_ADD,
                                                   'rootdn-deny-host',
-                                                  'localhost')])
+                                                  localhost)])
     except ldap.LDAPError as e:
         log.fatal('test_rootdn_access_denied_host: Failed to set deny host: error ' +
                   e.message['desc'])
@@ -559,10 +560,11 @@ def test_rootdn_access_allowed_host(topology):
         assert False
 
     hostname = socket.gethostname()
+    localhost = DirSrvTools.getLocalhost()
     try:
         topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_ADD,
                                                   'rootdn-allow-host',
-                                                  'localhost')])
+                                                  localhost)])
         topology.standalone.modify_s(PLUGIN_DN, [(ldap.MOD_ADD,
                                                   'rootdn-allow-host',
                                                   hostname)])
diff --git a/dirsrvtests/tests/tickets/ticket1347760_test.py b/dirsrvtests/tests/tickets/ticket1347760_test.py
index a54aea1..6b0be59 100644
--- a/dirsrvtests/tests/tickets/ticket1347760_test.py
+++ b/dirsrvtests/tests/tickets/ticket1347760_test.py
@@ -85,27 +85,29 @@ def topology(request):
 
 
 def pattern_accesslog(file, log_pattern):
-    try:
-        pattern_accesslog.last_pos += 1
-    except AttributeError:
-        pattern_accesslog.last_pos = 0
-
-    found = None
-    file.seek(pattern_accesslog.last_pos)
-
-    # Use a while true iteration because 'for line in file: hit a
-    # python bug that break file.tell()
-    while True:
-        line = file.readline()
-        found = log_pattern.search(line)
-        if ((line == '') or (found)):
-            break
-
-    pattern_accesslog.last_pos = file.tell()
-    if found:
-        return line
-    else:
-        return None
+    for i in range(5):
+        try:
+            pattern_accesslog.last_pos += 1
+        except AttributeError:
+            pattern_accesslog.last_pos = 0
+
+        found = None
+        file.seek(pattern_accesslog.last_pos)
+
+        # Use a while true iteration because 'for line in file: hit a
+        # python bug that break file.tell()
+        while True:
+            line = file.readline()
+            found = log_pattern.search(line)
+            if ((line == '') or (found)):
+                break
+
+        pattern_accesslog.last_pos = file.tell()
+        if found:
+            return line
+        else:
+            time.sleep(1)
+    return None
 
 
 def check_op_result(server, op, dn, superior, exists, rc):
diff --git a/dirsrvtests/tests/tickets/ticket47838_test.py b/dirsrvtests/tests/tickets/ticket47838_test.py
index 85e5166..d2b07cf 100644
--- a/dirsrvtests/tests/tickets/ticket47838_test.py
+++ b/dirsrvtests/tests/tickets/ticket47838_test.py
@@ -373,7 +373,7 @@ def test_47838_run_4(topology):
     global plus_all_ecount
     global plus_all_dcount
     if nss_version >= NSS323:
-        assert ecount == 23
+        assert ecount == 29
     else:
         assert ecount == 20
     assert dcount == (plus_all_ecount + plus_all_dcount - ecount)
@@ -411,10 +411,10 @@ def test_47838_run_5(topology):
     log.info("Disabled ciphers: %d" % dcount)
     global plus_all_ecount
     global plus_all_dcount
-    if nss_version >= NSS320:
-        assert ecount == 23
+    if nss_version >= NSS323:
+        assert ecount == 29
     else:
-        assert ecount == 12
+        assert ecount == 23
     assert dcount == (plus_all_ecount + plus_all_dcount - ecount)
     weak = os.popen('egrep "SSL alert:" %s | egrep \": enabled\" | egrep "WEAK CIPHER" | wc -l' % topology.standalone.errlog)
     wcount = int(weak.readline().rstrip())
@@ -516,10 +516,10 @@ def test_47838_run_8(topology):
     log.info("Disabled ciphers: %d" % dcount)
     global plus_all_ecount
     global plus_all_dcount
-    if nss_version >= NSS320:
-       assert ecount == 23
+    if nss_version >= NSS323:
+       assert ecount == 29
     else:
-       assert ecount == 12
+       assert ecount == 23
     assert dcount == (plus_all_ecount + plus_all_dcount - ecount)
     weak = os.popen('egrep "SSL alert:" %s | egrep \": enabled\" | egrep "WEAK CIPHER" | wc -l' % topology.standalone.errlog)
     wcount = int(weak.readline().rstrip())
@@ -556,10 +556,10 @@ def test_47838_run_9(topology):
 
     log.info("Enabled ciphers: %d" % ecount)
     log.info("Disabled ciphers: %d" % dcount)
-    if nss_version >= NSS320:
-        assert ecount == 30
+    if nss_version >= NSS323:
+        assert ecount == 36
     else:
-        assert ecount == 23
+        assert ecount == 30
     assert dcount == 0
     weak = os.popen('egrep "SSL alert:" %s | egrep \": enabled\" | egrep "WEAK CIPHER" | wc -l' % topology.standalone.errlog)
     wcount = int(weak.readline().rstrip())
@@ -606,10 +606,7 @@ def test_47838_run_10(topology):
     log.info("Disabled ciphers: %d" % dcount)
     global plus_all_ecount
     global plus_all_dcount
-    if nss_version >= NSS320:
-        assert ecount == 5
-    else:
-        assert ecount == 9
+    assert ecount == 9
     assert dcount == 0
     weak = os.popen('egrep "SSL alert:" %s | egrep \": enabled\" | egrep "WEAK CIPHER" | wc -l' % topology.standalone.errlog)
     wcount = int(weak.readline().rstrip())
-- 
2.7.4

