From b6ed4534959f309203126758fbffbd0f9b16db65 Mon Sep 17 00:00:00 2001
From: Mark Reynolds <mreynolds@redhat.com>
Date: Jan 17 2019 13:33:00 +0000
Subject: Ticket 50164 - Add test for dscreate


Description:  Add a simple test in basic suite to make sure dscreate works,
              also moved setup/remove tests from lib389 and moved them inside
              dirsrvtests directory

https://pagure.io/389-ds-base/issue/50164

Reviewed by: ?

---

diff --git a/dirsrvtests/tests/suites/basic/basic_test.py b/dirsrvtests/tests/suites/basic/basic_test.py
index 50db65d..50b5dc0 100644
--- a/dirsrvtests/tests/suites/basic/basic_test.py
+++ b/dirsrvtests/tests/suites/basic/basic_test.py
@@ -870,7 +870,7 @@ def test_mod_def_rootdse_attr(topology_st, import_example_ldif, rootdse_attr):
 
    :id: c7831e04-f458-4e23-83c7-b6f66109f639
 
-   :setup: Standalone instance and we are using rootdse_attr fixture which 
+   :setup: Standalone instance and we are using rootdse_attr fixture which
 adds nsslapd-return-default-opattr attr with value of one operation attribute.
 
    :steps:
@@ -1143,6 +1143,56 @@ def test_ticketldbm_audit(topology_st):
         assert audit_pattern_found(inst, regex)
 
 
+def test_dscreate(request):
+    """Test that dscreate works, we need this for now until setup-ds.pl is
+    fully discontinued.
+
+    :id: 5bf75c47-a283-430e-a65c-3c5fd8dbadb9
+    :setup: None
+    :steps:
+        1. Create template file for dscreate
+        2. Create instance using template file
+    :expectedresults:
+        1. Should succeeds
+        2. Should succeeds
+    """
+
+    template_file = "dssetup.inf"
+    template_text = """[general]
+config_version = 2
+full_machine_name = localhost.localdomain
+
+
+[slapd]
+instance_name = test_dscreate
+root_dn = cn=directory manager
+root_password = someLongPassword_123
+
+[backend-userroot]
+suffix = dc=example,dc=com
+sample_entries = yes
+"""
+
+    with open(template_file, "w") as template_fd:
+        template_fd.write(template_text)
+    cmd = 'dscreate from-file ' + template_file
+
+    try:
+        subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
+    except subprocess.CalledProcessError as e:
+        log.fatal("dscreate failed!  Error ({}) {}".format(e.returncode, e.output))
+        assert False
+
+    def fin():
+        os.remove(template_file)
+        try:
+            subprocess.check_output('dsctl test_dscreate remove --do-it', shell=True)
+        except subprocess.CalledProcessError as e:
+            log.fatal("Failed to remove test instance  Error ({}) {}".format(e.returncode, e.output))
+
+    request.addfinalizer(fin)
+
+
 if __name__ == '__main__':
     # Run isolated
     # -s for DEBUG mode
diff --git a/dirsrvtests/tests/suites/setup_ds/__init__.py b/dirsrvtests/tests/suites/setup_ds/__init__.py
index 8371b76..80ce751 100644
--- a/dirsrvtests/tests/suites/setup_ds/__init__.py
+++ b/dirsrvtests/tests/suites/setup_ds/__init__.py
@@ -1,3 +1,12 @@
+# --- BEGIN COPYRIGHT BLOCK ---
+# Copyright (C) 2019 Red Hat, Inc.
+# All rights reserved.
+#
+# License: GPL (version 3 or any later version).
+# See LICENSE for details.
+# --- END COPYRIGHT BLOCK ---
+
 """
-   :Requirement: 389-ds-base: Basic Directory Server Operations
+    :Requirement: 389-ds-base: Basic Directory Server Operations
 """
+
diff --git a/dirsrvtests/tests/suites/setup_ds/dscreate_test.py b/dirsrvtests/tests/suites/setup_ds/dscreate_test.py
new file mode 100644
index 0000000..aa2d13b
--- /dev/null
+++ b/dirsrvtests/tests/suites/setup_ds/dscreate_test.py
@@ -0,0 +1,121 @@
+# --- BEGIN COPYRIGHT BLOCK ---
+# Copyright (C) 2016 Red Hat, Inc.
+# All rights reserved.
+#
+# License: GPL (version 3 or any later version).
+# See LICENSE for details.
+# --- END COPYRIGHT BLOCK ---
+
+
+import sys
+import pytest
+from lib389 import DirSrv
+from lib389.cli_base import LogCapture
+from lib389.instance.setup import SetupDs
+from lib389.instance.remove import remove_ds_instance
+from lib389.instance.options import General2Base, Slapd2Base
+from lib389._constants import *
+
+import tempfile
+
+INSTANCE_PORT = 54321
+INSTANCE_SERVERID = 'standalone'
+
+DEBUGGING = True
+
+MAJOR, MINOR, _, _, _ = sys.version_info
+
+class TopologyInstance(object):
+    def __init__(self, standalone):
+        # For these tests, we don't want to open the instance.
+        # instance.open()
+        self.standalone = standalone
+
+# Need a teardown to destroy the instance.
+@pytest.fixture
+def topology(request):
+    instance = DirSrv(verbose=DEBUGGING)
+    instance.log.debug("Instance allocated")
+    args = {SER_PORT: INSTANCE_PORT,
+            SER_SERVERID_PROP: INSTANCE_SERVERID}
+    instance.allocate(args)
+    if instance.exists():
+        instance.delete()
+
+    def fin():
+        if instance.exists() and not DEBUGGING:
+            instance.delete()
+    request.addfinalizer(fin)
+
+    return TopologyInstance(instance)
+
+def test_setup_ds_minimal_dry(topology):
+    # Create the setupDs
+    lc = LogCapture()
+    # Give it the right types.
+    sds = SetupDs(verbose=DEBUGGING, dryrun=True, log=lc.log)
+
+    # Get the dicts from Type2Base, as though they were from _validate_ds_2_config
+    # IE get the defaults back just from Slapd2Base.collect
+    # Override instance name, root password, port and secure port.
+
+    general_options = General2Base(lc.log)
+    general_options.verify()
+    general = general_options.collect()
+
+    slapd_options = Slapd2Base(lc.log)
+    slapd_options.set('instance_name', INSTANCE_SERVERID)
+    slapd_options.set('port', INSTANCE_PORT)
+    slapd_options.set('root_password', PW_DM)
+    slapd_options.verify()
+    slapd = slapd_options.collect()
+
+    sds.create_from_args(general, slapd, {}, None)
+
+    insts = topology.standalone.list(serverid=INSTANCE_SERVERID)
+    # Assert we did not change the system.
+    assert(len(insts) == 0)
+
+def test_setup_ds_minimal(topology):
+    # Create the setupDs
+    lc = LogCapture()
+    # Give it the right types.
+    sds = SetupDs(verbose=DEBUGGING, dryrun=False, log=lc.log)
+
+    # Get the dicts from Type2Base, as though they were from _validate_ds_2_config
+    # IE get the defaults back just from Slapd2Base.collect
+    # Override instance name, root password, port and secure port.
+
+    general_options = General2Base(lc.log)
+    general_options.verify()
+    general = general_options.collect()
+
+    slapd_options = Slapd2Base(lc.log)
+    slapd_options.set('instance_name', INSTANCE_SERVERID)
+    slapd_options.set('port', INSTANCE_PORT)
+    slapd_options.set('root_password', PW_DM)
+    slapd_options.verify()
+    slapd = slapd_options.collect()
+
+    sds.create_from_args(general, slapd, {}, None)
+    insts = topology.standalone.list(serverid=INSTANCE_SERVERID)
+    # Assert we did change the system.
+    assert(len(insts) == 1)
+    # Make sure we can connect
+    topology.standalone.open()
+    # Make sure we can start stop.
+    topology.standalone.stop()
+    topology.standalone.start()
+    # Okay, actually remove the instance
+    remove_ds_instance(topology.standalone)
+
+
+def test_setup_ds_inf_minimal(topology):
+    if MAJOR < 3:
+        return
+    # Write a template inf
+    # Check it?
+    # Setup the server
+
+    pass
+
diff --git a/dirsrvtests/tests/suites/setup_ds/remove_test.py b/dirsrvtests/tests/suites/setup_ds/remove_test.py
new file mode 100644
index 0000000..0be7c8b
--- /dev/null
+++ b/dirsrvtests/tests/suites/setup_ds/remove_test.py
@@ -0,0 +1,63 @@
+# --- BEGIN COPYRIGHT BLOCK ---
+# Copyright (C) 2018 Red Hat, Inc.
+# All rights reserved.
+#
+# License: GPL (version 3 or any later version).
+# See LICENSE for details.
+# --- END COPYRIGHT BLOCK ---
+
+import os
+import subprocess
+import pytest
+import logging
+from lib389 import DirSrv
+from lib389.instance.remove import remove_ds_instance
+from lib389._constants import ReplicaRole
+from lib389.topologies import create_topology
+
+
+@pytest.fixture(scope="function")
+def topology_st(request):
+    """Create DS standalone instance"""
+
+    topology = create_topology({ReplicaRole.STANDALONE: 1})
+
+    def fin():
+        if topology.standalone.exists():
+            topology.standalone.delete()
+    request.addfinalizer(fin)
+
+    return topology
+
+
+@pytest.mark.parametrize("simple_allocate", (True, False))
+def test_basic(topology_st, simple_allocate):
+    """Check that all DS directories and systemd items were removed"""
+
+    inst = topology_st.standalone
+
+    # FreeIPA uses local_simple_allocate for the removal process
+    if simple_allocate:
+        inst = DirSrv(verbose=inst.verbose)
+        inst.local_simple_allocate(topology_st.standalone.serverid)
+
+    remove_ds_instance(inst)
+
+    paths = [inst.ds_paths.backup_dir,
+             inst.ds_paths.cert_dir,
+             inst.ds_paths.config_dir,
+             inst.ds_paths.db_dir,
+             inst.get_changelog_dir(),
+             inst.ds_paths.ldif_dir,
+             inst.ds_paths.lock_dir,
+             inst.ds_paths.log_dir,
+             "{}/sysconfig/dirsrv-{}".format(inst.ds_paths.sysconf_dir, inst.serverid)]
+    for path in paths:
+        assert not os.path.exists(path)
+
+    try:
+        subprocess.check_output(['systemctl', 'is-enabled', 'dirsrv@{}'.format(inst.serverid)], encoding='utf-8')
+    except subprocess.CalledProcessError as ex:
+        assert "disabled" in ex.output
+
+
diff --git a/src/lib389/lib389/tests/instance/__init__.py b/src/lib389/lib389/tests/instance/__init__.py
deleted file mode 100644
index d57ac33..0000000
--- a/src/lib389/lib389/tests/instance/__init__.py
+++ /dev/null
@@ -1,8 +0,0 @@
-# --- BEGIN COPYRIGHT BLOCK ---
-# Copyright (C) 2016 Red Hat, Inc.
-# All rights reserved.
-#
-# License: GPL (version 3 or any later version).
-# See LICENSE for details.
-# --- END COPYRIGHT BLOCK ---
-
diff --git a/src/lib389/lib389/tests/instance/remove_test.py b/src/lib389/lib389/tests/instance/remove_test.py
deleted file mode 100644
index 0be7c8b..0000000
--- a/src/lib389/lib389/tests/instance/remove_test.py
+++ /dev/null
@@ -1,63 +0,0 @@
-# --- BEGIN COPYRIGHT BLOCK ---
-# Copyright (C) 2018 Red Hat, Inc.
-# All rights reserved.
-#
-# License: GPL (version 3 or any later version).
-# See LICENSE for details.
-# --- END COPYRIGHT BLOCK ---
-
-import os
-import subprocess
-import pytest
-import logging
-from lib389 import DirSrv
-from lib389.instance.remove import remove_ds_instance
-from lib389._constants import ReplicaRole
-from lib389.topologies import create_topology
-
-
-@pytest.fixture(scope="function")
-def topology_st(request):
-    """Create DS standalone instance"""
-
-    topology = create_topology({ReplicaRole.STANDALONE: 1})
-
-    def fin():
-        if topology.standalone.exists():
-            topology.standalone.delete()
-    request.addfinalizer(fin)
-
-    return topology
-
-
-@pytest.mark.parametrize("simple_allocate", (True, False))
-def test_basic(topology_st, simple_allocate):
-    """Check that all DS directories and systemd items were removed"""
-
-    inst = topology_st.standalone
-
-    # FreeIPA uses local_simple_allocate for the removal process
-    if simple_allocate:
-        inst = DirSrv(verbose=inst.verbose)
-        inst.local_simple_allocate(topology_st.standalone.serverid)
-
-    remove_ds_instance(inst)
-
-    paths = [inst.ds_paths.backup_dir,
-             inst.ds_paths.cert_dir,
-             inst.ds_paths.config_dir,
-             inst.ds_paths.db_dir,
-             inst.get_changelog_dir(),
-             inst.ds_paths.ldif_dir,
-             inst.ds_paths.lock_dir,
-             inst.ds_paths.log_dir,
-             "{}/sysconfig/dirsrv-{}".format(inst.ds_paths.sysconf_dir, inst.serverid)]
-    for path in paths:
-        assert not os.path.exists(path)
-
-    try:
-        subprocess.check_output(['systemctl', 'is-enabled', 'dirsrv@{}'.format(inst.serverid)], encoding='utf-8')
-    except subprocess.CalledProcessError as ex:
-        assert "disabled" in ex.output
-
-
diff --git a/src/lib389/lib389/tests/instance/setup_test.py b/src/lib389/lib389/tests/instance/setup_test.py
deleted file mode 100644
index aa2d13b..0000000
--- a/src/lib389/lib389/tests/instance/setup_test.py
+++ /dev/null
@@ -1,121 +0,0 @@
-# --- BEGIN COPYRIGHT BLOCK ---
-# Copyright (C) 2016 Red Hat, Inc.
-# All rights reserved.
-#
-# License: GPL (version 3 or any later version).
-# See LICENSE for details.
-# --- END COPYRIGHT BLOCK ---
-
-
-import sys
-import pytest
-from lib389 import DirSrv
-from lib389.cli_base import LogCapture
-from lib389.instance.setup import SetupDs
-from lib389.instance.remove import remove_ds_instance
-from lib389.instance.options import General2Base, Slapd2Base
-from lib389._constants import *
-
-import tempfile
-
-INSTANCE_PORT = 54321
-INSTANCE_SERVERID = 'standalone'
-
-DEBUGGING = True
-
-MAJOR, MINOR, _, _, _ = sys.version_info
-
-class TopologyInstance(object):
-    def __init__(self, standalone):
-        # For these tests, we don't want to open the instance.
-        # instance.open()
-        self.standalone = standalone
-
-# Need a teardown to destroy the instance.
-@pytest.fixture
-def topology(request):
-    instance = DirSrv(verbose=DEBUGGING)
-    instance.log.debug("Instance allocated")
-    args = {SER_PORT: INSTANCE_PORT,
-            SER_SERVERID_PROP: INSTANCE_SERVERID}
-    instance.allocate(args)
-    if instance.exists():
-        instance.delete()
-
-    def fin():
-        if instance.exists() and not DEBUGGING:
-            instance.delete()
-    request.addfinalizer(fin)
-
-    return TopologyInstance(instance)
-
-def test_setup_ds_minimal_dry(topology):
-    # Create the setupDs
-    lc = LogCapture()
-    # Give it the right types.
-    sds = SetupDs(verbose=DEBUGGING, dryrun=True, log=lc.log)
-
-    # Get the dicts from Type2Base, as though they were from _validate_ds_2_config
-    # IE get the defaults back just from Slapd2Base.collect
-    # Override instance name, root password, port and secure port.
-
-    general_options = General2Base(lc.log)
-    general_options.verify()
-    general = general_options.collect()
-
-    slapd_options = Slapd2Base(lc.log)
-    slapd_options.set('instance_name', INSTANCE_SERVERID)
-    slapd_options.set('port', INSTANCE_PORT)
-    slapd_options.set('root_password', PW_DM)
-    slapd_options.verify()
-    slapd = slapd_options.collect()
-
-    sds.create_from_args(general, slapd, {}, None)
-
-    insts = topology.standalone.list(serverid=INSTANCE_SERVERID)
-    # Assert we did not change the system.
-    assert(len(insts) == 0)
-
-def test_setup_ds_minimal(topology):
-    # Create the setupDs
-    lc = LogCapture()
-    # Give it the right types.
-    sds = SetupDs(verbose=DEBUGGING, dryrun=False, log=lc.log)
-
-    # Get the dicts from Type2Base, as though they were from _validate_ds_2_config
-    # IE get the defaults back just from Slapd2Base.collect
-    # Override instance name, root password, port and secure port.
-
-    general_options = General2Base(lc.log)
-    general_options.verify()
-    general = general_options.collect()
-
-    slapd_options = Slapd2Base(lc.log)
-    slapd_options.set('instance_name', INSTANCE_SERVERID)
-    slapd_options.set('port', INSTANCE_PORT)
-    slapd_options.set('root_password', PW_DM)
-    slapd_options.verify()
-    slapd = slapd_options.collect()
-
-    sds.create_from_args(general, slapd, {}, None)
-    insts = topology.standalone.list(serverid=INSTANCE_SERVERID)
-    # Assert we did change the system.
-    assert(len(insts) == 1)
-    # Make sure we can connect
-    topology.standalone.open()
-    # Make sure we can start stop.
-    topology.standalone.stop()
-    topology.standalone.start()
-    # Okay, actually remove the instance
-    remove_ds_instance(topology.standalone)
-
-
-def test_setup_ds_inf_minimal(topology):
-    if MAJOR < 3:
-        return
-    # Write a template inf
-    # Check it?
-    # Setup the server
-
-    pass
-

