ticket47910_test.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Copyright (C) 2015 Red Hat, Inc.
  2. # All rights reserved.
  3. #
  4. # License: GPL (version 3 or any later version).
  5. # See LICENSE for details.
  6. # --- END COPYRIGHT BLOCK ---
  7. #
  8. import os
  9. import sys
  10. import time
  11. import ldap
  12. import logging
  13. import pytest
  14. import re
  15. import subprocess
  16. from lib389 import DirSrv, Entry, tools, tasks
  17. from lib389.tools import DirSrvTools
  18. from lib389._constants import *
  19. from lib389.properties import *
  20. from lib389.tasks import *
  21. from datetime import datetime, timedelta
  22. logging.getLogger(__name__).setLevel(logging.DEBUG)
  23. log = logging.getLogger(__name__)
  24. installation1_prefix = None
  25. class TopologyStandalone(object):
  26. def __init__(self, standalone):
  27. standalone.open()
  28. self.standalone = standalone
  29. @pytest.fixture(scope="module")
  30. def topology(request):
  31. global installation1_prefix
  32. if installation1_prefix:
  33. args_instance[SER_DEPLOYED_DIR] = installation1_prefix
  34. # Creating standalone instance ...
  35. standalone = DirSrv(verbose=False)
  36. args_instance[SER_HOST] = HOST_STANDALONE
  37. args_instance[SER_PORT] = PORT_STANDALONE
  38. args_instance[SER_SERVERID_PROP] = SERVERID_STANDALONE
  39. args_instance[SER_CREATION_SUFFIX] = DEFAULT_SUFFIX
  40. args_standalone = args_instance.copy()
  41. standalone.allocate(args_standalone)
  42. instance_standalone = standalone.exists()
  43. if instance_standalone:
  44. standalone.delete()
  45. standalone.create()
  46. standalone.open()
  47. # Clear out the tmp dir
  48. standalone.clearTmpDir(__file__)
  49. def fin():
  50. standalone.delete()
  51. request.addfinalizer(fin)
  52. return TopologyStandalone(standalone)
  53. @pytest.fixture(scope="module")
  54. def log_dir(topology):
  55. '''
  56. Do a search operation
  57. and disable access log buffering
  58. to generate the access log
  59. '''
  60. log.info("Diable access log buffering")
  61. topology.standalone.setAccessLogBuffering(False)
  62. log.info("Do a ldapsearch operation")
  63. topology.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, "(objectclass=*)")
  64. log.info("sleep for sometime so that access log file get generated")
  65. time.sleep( 1 )
  66. return topology.standalone.accesslog
  67. def format_time(local_datetime):
  68. formatted_time = (local_datetime.strftime("[%d/%b/%Y:%H:%M:%S]"))
  69. return formatted_time
  70. def execute_logconv(start_time_stamp, end_time_stamp, access_log):
  71. '''
  72. This function will take start time and end time
  73. as input parameter and
  74. assign these values to -S and -E options of logconv
  75. and, it will execute logconv and return result value
  76. '''
  77. log.info("Executing logconv.pl with -S current time and -E end time")
  78. cmd = ['logconv.pl', '-S', start_time_stamp, '-E', end_time_stamp, access_log]
  79. log.info(" ".join(cmd))
  80. proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  81. stdout, stderr = proc.communicate()
  82. log.info("standard output" + stdout)
  83. log.info("standard errors" + stderr)
  84. return proc.returncode
  85. def test_ticket47910_logconv_start_end_positive(topology, log_dir):
  86. '''
  87. Execute logconv.pl with -S and -E(endtime) with random time stamp
  88. This is execute successfully
  89. '''
  90. #
  91. # Execute logconv.pl -S -E with random timestamp
  92. #
  93. log.info('Running test_ticket47910 - Execute logconv.pl -S -E with random values')
  94. log.info("taking current time with offset of 2 mins and formatting it to feed -S")
  95. start_time_stamp = (datetime.now() - timedelta(minutes=2))
  96. formatted_start_time_stamp = format_time(start_time_stamp)
  97. log.info("taking current time with offset of 2 mins and formatting it to feed -E")
  98. end_time_stamp = (datetime.now() + timedelta(minutes=2))
  99. formatted_end_time_stamp = format_time(end_time_stamp)
  100. log.info("Executing logconv.pl with -S and -E")
  101. result = execute_logconv(formatted_start_time_stamp, formatted_end_time_stamp, log_dir)
  102. assert result == 0
  103. def test_ticket47910_logconv_start_end_negative(topology, log_dir):
  104. '''
  105. Execute logconv.pl with -S and -E(endtime) with random time stamp
  106. This is a negative test case, where endtime will be lesser than the
  107. starttime
  108. This should give error message
  109. '''
  110. #
  111. # Execute logconv.pl -S and -E with random timestamp
  112. #
  113. log.info('Running test_ticket47910 - Execute logconv.pl -S -E with starttime>endtime')
  114. log.info("taking current time with offset of 2 mins and formatting it to feed -S")
  115. start_time_stamp = (datetime.now() + timedelta(minutes=2))
  116. formatted_start_time_stamp = format_time(start_time_stamp)
  117. log.info("taking current time with offset of 2 mins and formatting it to feed -E")
  118. end_time_stamp = (datetime.now() - timedelta(minutes=2))
  119. formatted_end_time_stamp = format_time(end_time_stamp)
  120. log.info("Executing logconv.pl with -S and -E")
  121. result = execute_logconv(formatted_start_time_stamp, formatted_end_time_stamp, log_dir)
  122. assert result == 1
  123. def test_ticket47910_logconv_start_end_invalid(topology, log_dir):
  124. '''
  125. Execute logconv.pl with -S and -E(endtime) with invalid time stamp
  126. This is a negative test case, where it should give error message
  127. '''
  128. #
  129. # Execute logconv.pl -S and -E with invalid timestamp
  130. #
  131. log.info('Running test_ticket47910 - Execute logconv.pl -S -E with invalid timestamp')
  132. log.info("Set start time and end time to invalid values")
  133. start_time_stamp = "invalid"
  134. end_time_stamp = "invalid"
  135. log.info("Executing logconv.pl with -S and -E")
  136. result = execute_logconv(start_time_stamp, end_time_stamp, log_dir)
  137. assert result == 1
  138. def test_ticket47910_logconv_noaccesslogs(topology, log_dir):
  139. '''
  140. Execute logconv.pl -S(starttime) without specify
  141. access logs location
  142. '''
  143. #
  144. # Execute logconv.pl -S with random timestamp and no access log location
  145. #
  146. log.info('Running test_ticket47910 - Execute logconv.pl without access logs')
  147. log.info("taking current time with offset of 2 mins and formatting it to feed -S")
  148. time_stamp = (datetime.now() - timedelta(minutes=2))
  149. formatted_time_stamp = format_time(time_stamp)
  150. log.info("Executing logconv.pl with -S current time")
  151. cmd = ['logconv.pl', '-S', formatted_time_stamp]
  152. log.info(" ".join(cmd))
  153. proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  154. stdout, stderr = proc.communicate()
  155. log.info("standard output" + stdout)
  156. log.info("standard errors" + stderr)
  157. assert proc.returncode == 1
  158. if __name__ == '__main__':
  159. # Run isolated
  160. # -s for DEBUG mode
  161. pytest.main("-s ticket47910_test.py")