From 1698dd8e89b8d11be94c547c194e7a455ffd42a0 Mon Sep 17 00:00:00 2001
From: Simon Pichugin <spichugi@redhat.com>
Date: Dec 05 2018 15:39:58 +0000
Subject: Issue 50071 - Set ports in local_simple_allocate function


Description: remove_ds_instance function require DirSrv
object having port and sslport defined for semanage remove
label operation. We should set it in local_simple_allocate too.
Fix DSEldif.get function so it returns a list instead of a view
(Python 3 change).

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

Reviewed by: mhonek, tbordaz, cheimes (Thanks!)

---

diff --git a/src/lib389/lib389/__init__.py b/src/lib389/lib389/__init__.py
index adeb512..ad09fc4 100644
--- a/src/lib389/lib389/__init__.py
+++ b/src/lib389/lib389/__init__.py
@@ -85,6 +85,7 @@ from lib389.utils import (
 from lib389.paths import Paths
 from lib389.nss_ssl import NssSsl
 from lib389.tasks import BackupTask, RestoreTask
+from lib389.dseldif import DSEldif
 
 # mixin
 # from lib389.tools import DirSrvTools
@@ -431,8 +432,13 @@ class DirSrv(SimpleLDAPObject, object):
 
         # We must also alloc host and ports for some manipulation tasks
         self.host = socket.gethostname()
-        # self.port ...
-        # self.sslport ...
+
+        dse_ldif = DSEldif(self)
+        port = dse_ldif.get(DN_CONFIG, "nsslapd-port", single=True)
+        sslport = dse_ldif.get(DN_CONFIG, "nsslapd-secureport", single=True)
+
+        self.port = int(port) if port is not None else None
+        self.sslport = int(sslport) if sslport is not None else None
 
         self.binddn = binddn
         self.bindpw = password
diff --git a/src/lib389/lib389/dseldif.py b/src/lib389/lib389/dseldif.py
index 1e0de6c..719ba6b 100644
--- a/src/lib389/lib389/dseldif.py
+++ b/src/lib389/lib389/dseldif.py
@@ -89,13 +89,10 @@ class DSEldif(object):
         except ValueError:
             return None
 
+        vals = list(attr_data.values())
         if single:
-            vals = list(attr_data.values())
-            if len(vals) > 0:
-                return vals[0]
-            else:
-                return None
-        return attr_data.values()
+            return vals[0] if len(vals) > 0 else None
+        return vals
 
     def add(self, entry_dn, attr, value):
         """Add an attribute under a given entry
diff --git a/src/lib389/lib389/instance/remove.py b/src/lib389/lib389/instance/remove.py
index 0245db7..21da083 100644
--- a/src/lib389/lib389/instance/remove.py
+++ b/src/lib389/lib389/instance/remove.py
@@ -78,7 +78,8 @@ def remove_ds_instance(dirsrv):
     subprocess.check_call(["systemctl", "disable", "dirsrv@{}".format(dirsrv.serverid)])
 
     # Remove selinux port label
-    selinux_label_port(dirsrv.port, remove_label=True)
+    if dirsrv.port is not None:
+        selinux_label_port(dirsrv.port, remove_label=True)
     if dirsrv.sslport is not None:
         selinux_label_port(dirsrv.sslport, remove_label=True)
 
diff --git a/src/lib389/lib389/tests/instance/remove_test.py b/src/lib389/lib389/tests/instance/remove_test.py
index 4442726..0be7c8b 100644
--- a/src/lib389/lib389/tests/instance/remove_test.py
+++ b/src/lib389/lib389/tests/instance/remove_test.py
@@ -9,12 +9,14 @@
 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="module")
+@pytest.fixture(scope="function")
 def topology_st(request):
     """Create DS standalone instance"""
 
@@ -28,11 +30,17 @@ def topology_st(request):
     return topology
 
 
-def test_basic(topology_st):
+@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,
@@ -51,3 +59,5 @@ def test_basic(topology_st):
         subprocess.check_output(['systemctl', 'is-enabled', 'dirsrv@{}'.format(inst.serverid)], encoding='utf-8')
     except subprocess.CalledProcessError as ex:
         assert "disabled" in ex.output
+
+

