From 26277489735998c257463ce771c8b37b683b8b59 Mon Sep 17 00:00:00 2001
From: Amita Sharma <amsharma@redhat.com>
Date: Fri, 17 Nov 2017 13:23:29 +0530
Subject: [PATCH] Ticket 49455- Add tests to monitor test suit.

Description: Add test cases for monitor test suit
Add methods in lib389 monitor.py to support test cases

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

Reviewed by: William Brown
---
 dirsrvtests/tests/suites/monitor/monitor_test.py | 69 ++++++++++++++++++
 src/lib389/lib389/monitor.py                     | 91 +++++++++++++++++-------
 2 files changed, 136 insertions(+), 24 deletions(-)
 create mode 100644 dirsrvtests/tests/suites/monitor/monitor_test.py

diff --git a/dirsrvtests/tests/suites/monitor/monitor_test.py b/dirsrvtests/tests/suites/monitor/monitor_test.py
new file mode 100644
index 0000000..5c53021
--- /dev/null
+++ b/dirsrvtests/tests/suites/monitor/monitor_test.py
@@ -0,0 +1,69 @@
+import logging
+import pytest
+import os
+from lib389.monitor import *
+from lib389._constants import *
+from lib389.topologies import topology_st as topo
+
+DEBUGGING = os.getenv("DEBUGGING", default=False)
+if DEBUGGING:
+    logging.getLogger(__name__).setLevel(logging.DEBUG)
+else:
+    logging.getLogger(__name__).setLevel(logging.INFO)
+log = logging.getLogger(__name__)
+
+def test_monitor(topo):
+    """This test is to display monitor attributes to check the performace
+
+    :id: f7c8a815-07cf-4e67-9574-d26a0937d3db
+    :setup: Single instance
+    :steps:
+        1. Get the cn=monitor connections attributes
+        2. Print connections attributes
+        3. Get the cn=monitor version
+        4. Print cn=monitor version
+        5. Get the cn=monitor threads attributes
+        6. Print cn=monitor threads attributes
+        7. Get cn=monitor backends attributes
+        8. Print cn=monitor backends attributes
+        9. Get cn=monitor operations attributes
+        10. Print cn=monitor operations attributes
+        11. Get cn=monitor statistics attributes
+        12. Print cn=monitor statistics attributes
+    :expectedresults:
+        1. cn=monitor attributes should be fetched and printed successfully.
+    """
+
+    #define the monitor object from Monitor class in lib389
+    monitor = Monitor(topo.standalone)
+
+    #get monitor connections
+    connections = monitor.get_connections()
+    log.info('connection: {0[0]}, currentconnections: {0[1]}, totalconnections: {0[2]}'.format(connections))
+
+    #get monitor version
+    version = monitor.get_version()
+    log.info('version :: %s' %version)
+
+    #get monitor threads
+    threads = monitor.get_threads()
+    log.info('threads: {0[0]},currentconnectionsatmaxthreads: {0[1]},maxthreadsperconnhits: {0[2]}'.format(threads))
+
+    #get monitor backends
+    backend = monitor.get_backends()
+    log.info('nbackends: {0[0]}, backendmonitordn: {0[1]}'.format(backend))
+
+    #get monitor operations
+    operations = monitor.get_operations()
+    log.info('opsinitiated: {0[0]}, opscompleted: {0[1]}'.format(operations))
+
+    #get monitor stats
+    stats = monitor.get_statistics()
+    log.info('dtablesize: {0[0]},readwaiters: {0[1]},entriessent: {0[2]},bytessent: {0[3]},currenttime: {0[4]},starttime: {0[5]}'.format(stats))
+
+
+if __name__ == '__main__':
+    # Run isolated
+    # -s for DEBUG mode
+    CURRENT_FILE = os.path.realpath(__file__)
+    pytest.main("-s %s" % CURRENT_FILE)
diff --git a/src/lib389/lib389/monitor.py b/src/lib389/lib389/monitor.py
index 5226a67..557e04b 100644
--- a/src/lib389/lib389/monitor.py
+++ b/src/lib389/lib389/monitor.py
@@ -6,39 +6,83 @@
 # See LICENSE for details.
 # --- END COPYRIGHT BLOCK ---
 
-"""
-Monitor class to display current server performance details
-"""
-
 import ldap
 from ldap import filter as ldap_filter
 from lib389._constants import *
-from lib389._mapped_object import DSLdapObject
-
+from lib389._mapped_object import DSLdapObjects, DSLdapObject
 
 class Monitor(DSLdapObject):
+    """An object that helps reading of cn=monitor for server statistics.
+        :param instance: An instance
+        :type instance: lib389.DirSrv
+        :param dn: not used
+        :param batch: not used
     """
-        Allows reading of cn=monitor for server statistics.
-    """
-
     def __init__(self, instance, dn=None, batch=False):
         super(Monitor, self).__init__(instance=instance, batch=batch)
         self._dn = DN_MONITOR
-        self._monitor_keys = [
-            'instanceection',
-            'currentinstanceections',
-            'currentconnections',
-            'opscompleted',
-            'opsinitiated',
-            'threads',
-            'totalinstanceections',
-            'version',
-            'currenttime',
-            'connection',
-        ]
 
-    def status(self):
-        return self.get_attrs_vals(self._monitor_keys)
+    def get_connections(self):
+        """Get connection related attribute values for cn=monitor
+
+        :returns: Values of connection, currentconnections,
+                  totalconnections attributes of cn=monitor
+        """
+        connection = self.get_attr_vals_utf8('connection')
+        currentconnections = self.get_attr_vals_utf8('currentconnections')
+        totalconnections = self.get_attr_vals_utf8('totalconnections')
+        return (connection, currentconnections, totalconnections)
+
+    def get_version(self):
+        """Get version attribute value for cn=monitor
+
+        :returns: Value of version attribute of cn=monitor
+        """
+        version = self.get_attr_vals_utf8('connection')
+        return version
+
+    def get_threads(self):
+        """Get thread related attributes value for cn=monitor
+
+        :returns: Values of threads, currentconnectionsatmaxthreads, and
+                  maxthreadsperconnhits attributes of cn=monitor
+        """
+        threads = self.get_attr_vals_utf8('threads')
+        currentconnectionsatmaxthreads = self.get_attr_vals_utf8('currentconnectionsatmaxthreads')
+        maxthreadsperconnhits = self.get_attr_vals_utf8('maxthreadsperconnhits')
+        return (threads, currentconnectionsatmaxthreads, maxthreadsperconnhits)
+
+    def get_backends(self):
+        """Get backends related attributes value for cn=monitor
+
+        :returns: Values of nbackends and backendmonitordn attributes of cn=monitor
+        """
+        nbackends = self.get_attr_vals_utf8('nbackends')
+        backendmonitordn = self.get_attr_vals_utf8('backendmonitordn')
+        return (nbackends, backendmonitordn)
+
+    def get_operations(self):
+        """Get operations related attributes value for cn=monitor
+
+        :returns: Values of opsinitiated and opscompleted attributes of cn=monitor
+        """
+        opsinitiated = self.get_attr_vals_utf8('opsinitiated')
+        opscompleted = self.get_attr_vals_utf8('opsinitiated')
+        return (opsinitiated, opscompleted)
+
+    def get_statistics(self):
+        """Get statistics attributes value for cn=monitor
+
+        :returns: Values of dtablesize, readwaiters, entriessent,
+                  bytessent, currenttime, starttime attributes of cn=monitor
+        """
+        dtablesize = self.get_attr_vals_utf8('dtablesize')
+        readwaiters = self.get_attr_vals_utf8('readwaiters')
+        entriessent = self.get_attr_vals_utf8('entriessent')
+        bytessent = self.get_attr_vals_utf8('bytessent')
+        currenttime = self.get_attr_vals_utf8('currenttime')
+        starttime = self.get_attr_vals_utf8('starttime')
+        return (dtablesize, readwaiters, entriessent, bytessent, currenttime, starttime)
 
 class MonitorLDBM(DSLdapObject):
     def __init__(self, instance, dn=None, batch=False):
@@ -53,7 +97,6 @@ class MonitorLDBM(DSLdapObject):
             'dbcacheroevict',
             'dbcacherwevict',
         ]
-
     def status(self):
         return self.get_attrs_vals(self._backend_keys)
 
-- 
2.9.5

