From 2705b461399e30e7cae9bed1022e94cb2fea3631 Mon Sep 17 00:00:00 2001
From: William Brown <firstyear@redhat.com>
Date: Tue, 6 Dec 2016 11:30:29 +1000
Subject: [PATCH] Ticket 48835 - Tests with setup.py.in

Bug Description:  We should properly handle our tests as python libraries
rather than putting them into /etc

Fix Description:  Add setup.py.in, and integrate with our rpm for python2 and
python3 versions. Fix some tests that didn't bytecompile with python 3

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

Author: wibrown

Review by: ???
---
 Makefile.am                                        | 10 ++++
 dirsrvtests/cmd/dsadm/dsadm.py                     | 40 ++++++++--------
 dirsrvtests/tests/__init__.py                      |  0
 .../replication/wait_for_async_feature_test.py     |  2 +-
 dirsrvtests/tests/tickets/ticket48906_test.py      | 12 ++---
 dirsrvtests/tests/tmp/__init__.py                  |  0
 rpm/389-ds-base.spec.in                            | 50 ++++++++++++++-----
 setup.py.in                                        | 56 ++++++++++++++++++++++
 8 files changed, 130 insertions(+), 40 deletions(-)
 create mode 100644 dirsrvtests/tests/__init__.py
 create mode 100644 dirsrvtests/tests/tmp/__init__.py
 create mode 100644 setup.py.in

diff --git a/Makefile.am b/Makefile.am
index b425354..2e8873b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1839,6 +1839,7 @@ fixupcmd = sed \
 	-e 's,@vendor\@,$(vendor),g' \
 	-e 's,@PACKAGE_NAME\@,$(PACKAGE_NAME),g' \
 	-e 's,@PACKAGE_VERSION\@,$(PACKAGE_VERSION),g' \
+	-e 's,@RPM_VERSION\@,$(RPM_VERSION),g' \
 	-e 's,@PACKAGE_BASE_VERSION\@,$(PACKAGE_BASE_VERSION),g' \
 	-e 's,@CONSOLE_VERSION\@,$(CONSOLE_VERSION),g' \
 	-e 's,@BUILDNUM\@,$(BUILDNUM),g' \
@@ -1910,6 +1911,7 @@ fixupcmd = sed \
 	-e 's,@vendor\@,$(vendor),g' \
 	-e 's,@PACKAGE_NAME\@,$(PACKAGE_NAME),g' \
 	-e 's,@PACKAGE_VERSION\@,$(PACKAGE_VERSION),g' \
+	-e 's,@RPM_VERSION\@,$(RPM_VERSION),g' \
 	-e 's,@PACKAGE_BASE_VERSION\@,$(PACKAGE_BASE_VERSION),g' \
 	-e 's,@CONSOLE_VERSION\@,$(CONSOLE_VERSION),g' \
 	-e 's,@BUILDNUM\@,$(BUILDNUM),g' \
@@ -2008,6 +2010,14 @@ git-archive:
 	fi ; \
 	git archive --prefix=$(distdir)/ $$gittag | bzip2 > $$srcdistdir/$(distdir).tar.bz2
 
+# Python test tests
+# How will we update this to python 3?
+
+tests: setup.py.in
+	python setup.py build
+
+tests-install: tests
+	python setup.py install
 
 # RPM-related tasks
 
diff --git a/dirsrvtests/cmd/dsadm/dsadm.py b/dirsrvtests/cmd/dsadm/dsadm.py
index ece5f23..247a295 100755
--- a/dirsrvtests/cmd/dsadm/dsadm.py
+++ b/dirsrvtests/cmd/dsadm/dsadm.py
@@ -109,50 +109,50 @@ def validate_group(group):
 def test_get_group():
     try:
         grpname = get_default_group()
-        print 'get_group: %s' % grpname
+        print('get_group: %s' % grpname)
     except:
         raise
-        print "Can not find user group"
+        print("Can not find user group")
         pass
     try:
         grpname = get_default_group(group='tbordaz')
-        print 'get_group: %s' % grpname
+        print('get_group: %s' % grpname)
     except:
         raise
-        print "Can not find user group"
+        print("Can not find user group")
         pass
     try:
         grpname = get_default_group(group='coucou')
-        print 'get_group: %s' % grpname
+        print('get_group: %s' % grpname)
     except:
-        print "Can not find user group coucou"
+        print("Can not find user group coucou")
         pass
     try:
         grpname = get_default_group('thierry')
-        print 'get_group: %s' % grpname
+        print('get_group: %s' % grpname)
     except:
         raise
-        print "Can not find user group thierry"
+        print("Can not find user group thierry")
         pass
     try:
         grpname = get_default_group(1000)
-        print 'get_group: %s' % grpname
+        print('get_group: %s' % grpname)
     except:
         raise
-        print "Can not find user group 1000"
+        print("Can not find user group 1000")
         pass
     try:
         grpname = get_default_group(20532)
-        print 'get_group: %s' % grpname
+        print('get_group: %s' % grpname)
     except:
         raise
-        print "Can not find user group 20532"
+        print("Can not find user group 20532")
         pass
     try:
         grpname = get_default_group(123)
-        print 'get_group: %s' % grpname
+        print('get_group: %s' % grpname)
     except:
-        print "Can not find user group 123"
+        print("Can not find user group 123")
         pass
     
 def get_default_port():
@@ -332,9 +332,9 @@ class DSadmCmd(object):
         
         rc = pipe.wait()
         if rc == 0:
-            print "Directory %s %s" % (serverid, action_str)
+            print("Directory %s %s" % (serverid, action_str))
         else:
-            print "Failure: directory %s not %s (%s)" % (serverid, action_str, rc)
+            print("Failure: directory %s not %s (%s)" % (serverid, action_str, rc))
         return
     
     def start_action(self, args):
@@ -383,9 +383,9 @@ class DSadmCmd(object):
         
         rc = pipe.wait()
         if rc == 0:
-            print "Directory server \'%s\' successfully deleted" % serverid
+            print("Directory server \'%s\' successfully deleted" % serverid)
         else:
-            print "Fail to delete directory \'%s\': %d" % (serverid, rc)
+            print("Fail to delete directory \'%s\': %d" % (serverid, rc))
         return
 
     #
@@ -498,9 +498,9 @@ class DSadmCmd(object):
         os.unlink(tempf.name)
         rc = pipe.wait()
         if rc == 0:
-            print "Directory server \'%s\' successfully created" % serverid
+            print("Directory server \'%s\' successfully created" % serverid)
         else:
-            print "Fail to create directory \'%s\': %d" % (serverid, rc)
+            print("Fail to create directory \'%s\': %d" % (serverid, rc))
         return
 
     #
diff --git a/dirsrvtests/tests/__init__.py b/dirsrvtests/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/dirsrvtests/tests/suites/replication/wait_for_async_feature_test.py b/dirsrvtests/tests/suites/replication/wait_for_async_feature_test.py
index 54b47e6..b79784e 100644
--- a/dirsrvtests/tests/suites/replication/wait_for_async_feature_test.py
+++ b/dirsrvtests/tests/suites/replication/wait_for_async_feature_test.py
@@ -88,7 +88,7 @@ def entries(topology_m2, request):
     for test_dn in test_list:
         try:
             master1.delete_s(test_dn)
-        except ldap.LDAPError, e:
+        except ldap.LDAPError as e:
             log.error('Failed to delete entry (%s): error (%s)' % (test_dn,
                                                                    e.message['desc']))
             assert False
diff --git a/dirsrvtests/tests/tickets/ticket48906_test.py b/dirsrvtests/tests/tickets/ticket48906_test.py
index 393743b..2c9e6ca 100644
--- a/dirsrvtests/tests/tickets/ticket48906_test.py
+++ b/dirsrvtests/tests/tickets/ticket48906_test.py
@@ -171,12 +171,12 @@ def _check_guardian_value(topology, attr=DBLOCK_ATTR_CONFIG, expected_value=None
             break
         elif attr in line.lower():
             value = line.split(':')[1].replace("\n", "")
-            print "line"
-            print line
-            print "expected_value"
-            print expected_value
-            print "value"
-            print value
+            print("line")
+            print(line)
+            print("expected_value")
+            print(expected_value)
+            print("value")
+            print(value)
             assert(str(value) == str(expected_value))
             break
     assert(value)
diff --git a/dirsrvtests/tests/tmp/__init__.py b/dirsrvtests/tests/tmp/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/rpm/389-ds-base.spec.in b/rpm/389-ds-base.spec.in
index 4ba47b3..2445767 100644
--- a/rpm/389-ds-base.spec.in
+++ b/rpm/389-ds-base.spec.in
@@ -1,5 +1,6 @@
 
 %global pkgname   dirsrv
+%global srcname   389-ds-base
 
 # This is used in certain builds to help us know if it has extra features.
 %global variant base
@@ -232,15 +233,30 @@ Obsoletes:        %{name} <= 1.3.5.4
 %description      snmp
 SNMP Agent for the 389 Directory Server base package.
 
-%package          tests
+
+
+%package -n python2-%{srcname}-tests
 Summary:          The lib389 Continuous Integration Tests
 Group:            Development/Libraries
-Requires:         python-lib389
+Requires:         python2-lib389
 BuildArch:        noarch
 
-%description      tests
+%description  -n  python2-%{srcname}-tests
 The lib389 CI tests that can be run against the Directory Server.
 
+# Can't build on EL7! Python3 tooling is too broken :(
+# We have to use >= 8, because <= 7 doesn't work ....
+%if 0%{?rhel} >= 8 || 0%{?fedora}
+%package -n python%{python3_pkgversion}-%{srcname}-tests
+Summary:          The lib389 Continuous Integration Tests
+Group:            Development/Libraries
+Requires:         python%{python3_pkgversion}-lib389
+BuildArch:        noarch
+
+%description  -n  python%{python3_pkgversion}-%{srcname}-tests
+The lib389 CI tests that can be run against the Directory Server.
+%endif
+
 %prep
 %setup -q -n %{name}-%{version}%{?prerel}
 
@@ -300,6 +316,12 @@ export USE_64=1
 
 make %{?_smp_mflags}
 
+make setup.py
+
+%py2_build
+%if 0%{?rhel} >= 8 || 0%{?fedora}
+%py3_build
+%endif
 
 %install
 rm -rf $RPM_BUILD_ROOT
@@ -313,6 +335,11 @@ popd
 
 make DESTDIR="$RPM_BUILD_ROOT" install
 
+%py2_install
+%if 0%{?rhel} >= 8 || 0%{?fedora}
+%py3_install
+%endif
+
 mkdir -p $RPM_BUILD_ROOT/var/log/%{pkgname}
 mkdir -p $RPM_BUILD_ROOT/var/lib/%{pkgname}
 mkdir -p $RPM_BUILD_ROOT/var/lock/%{pkgname}
@@ -329,14 +356,6 @@ rm -f $RPM_BUILD_ROOT%{_libdir}/%{pkgname}/plugins/*.la
 # make sure perl scripts have a proper shebang 
 sed -i -e 's|#{{PERL-EXEC}}|#!/usr/bin/perl|' $RPM_BUILD_ROOT%{_datadir}/%{pkgname}/script-templates/template-*.pl
 
-# Why are we not making this a proper python package?
-pushd ../%{name}-%{version}%{?prerel}
-cp -r dirsrvtests $RPM_BUILD_ROOT/%{_sysconfdir}/%{pkgname}
-find $RPM_BUILD_ROOT/%{_sysconfdir}/%{pkgname}/dirsrvtests -type f -name '*.pyc' -delete
-find $RPM_BUILD_ROOT/%{_sysconfdir}/%{pkgname}/dirsrvtests -type f -name '*.pyo' -delete
-find $RPM_BUILD_ROOT/%{_sysconfdir}/%{pkgname}/dirsrvtests -type d -name '__pycache__' -delete
-popd
-
 %clean
 rm -rf $RPM_BUILD_ROOT
 
@@ -511,10 +530,15 @@ fi
 %{_mandir}/man1/ldap-agent.1.gz
 %{_unitdir}/%{pkgname}-snmp.service
 
-%files tests
+%files -n python2-%{srcname}-tests
+%defattr(-,root,root,-)
+%doc LICENSE LICENSE.GPLv3+
+%{python2_sitelib}/*
+
+%files -n python%{python3_pkgversion}-%{srcname}-tests
 %defattr(-,root,root,-)
 %doc LICENSE LICENSE.GPLv3+
-%{_sysconfdir}/%{pkgname}/dirsrvtests
+%{python3_sitelib}/*
 
 %changelog
 * Mon Dec 21 2015 Mark Reynolds <mreynolds@redhat.com> - 1.3.4.1-3
diff --git a/setup.py.in b/setup.py.in
new file mode 100644
index 0000000..6954287
--- /dev/null
+++ b/setup.py.in
@@ -0,0 +1,56 @@
+#!/usr/bin/env python
+
+# --- 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 ---
+
+#
+# A setup.py file
+#
+
+from setuptools import setup, find_packages
+from os import path
+import subprocess
+
+here = path.abspath(path.dirname(__file__))
+
+version = "@RPM_VERSION@"
+
+long_description = """
+The 389 Directory Server test suites. These are used to assert the correct
+behaviour of the LDAP server and relevant standards. Many of these tests are
+related to correctness of features, isolation of bugs, stress testing, and
+other configurations.
+"""
+
+setup(
+    name='dirsrvtests',
+    license='GPLv3+',
+    # How can we handle this better?
+    version=version,
+    description='The set of test suites for the 389 Directory Server',
+    long_description=long_description,
+    url='http://port389.org/wiki/Upstream_test_framework',
+
+    author='Red Hat Inc.',
+    author_email='389-devel@lists.fedoraproject.org',
+
+    classifiers=[
+        'Development Status :: 4 - Beta',
+        'Intended Audience :: Developers',
+        'Operating System :: POSIX :: Linux',
+        'Programming Language :: Python :: 2.7',
+        'Topic :: Software Development :: Libraries',
+        'Topic :: Software Development :: Quality Assurance',
+        'Topic :: Software Development :: Testing'],
+
+    keywords='389 directory server test configure',
+    packages=find_packages(),
+
+    install_requires=['python-ldap', 'lib389'],
+)
+
-- 
1.8.3.1

