ticket47910_test.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # Copyright (C) 2016 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 logging
  9. import subprocess
  10. from datetime import datetime, timedelta
  11. import pytest
  12. from lib389.tasks import *
  13. from lib389.topologies import topology_st
  14. logging.getLogger(__name__).setLevel(logging.DEBUG)
  15. log = logging.getLogger(__name__)
  16. @pytest.fixture(scope="module")
  17. def log_dir(topology_st):
  18. '''
  19. Do a search operation
  20. and disable access log buffering
  21. to generate the access log
  22. '''
  23. log.info("Diable access log buffering")
  24. topology_st.standalone.setAccessLogBuffering(False)
  25. log.info("Do a ldapsearch operation")
  26. topology_st.standalone.search_s(SUFFIX, ldap.SCOPE_SUBTREE, "(objectclass=*)")
  27. log.info("sleep for sometime so that access log file get generated")
  28. time.sleep(1)
  29. return topology_st.standalone.accesslog
  30. def format_time(local_datetime):
  31. formatted_time = (local_datetime.strftime("[%d/%b/%Y:%H:%M:%S]"))
  32. return formatted_time
  33. def execute_logconv(inst, start_time_stamp, end_time_stamp, access_log):
  34. '''
  35. This function will take start time and end time
  36. as input parameter and
  37. assign these values to -S and -E options of logconv
  38. and, it will execute logconv and return result value
  39. '''
  40. log.info("Executing logconv.pl with -S current time and -E end time")
  41. cmd = [os.path.join(inst.get_bin_dir(), 'logconv.pl'), '-S', start_time_stamp, '-E', end_time_stamp, access_log]
  42. log.info(" ".join(cmd))
  43. proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  44. stdout, stderr = proc.communicate()
  45. log.info("standard output" + stdout)
  46. log.info("standard errors" + stderr)
  47. return proc.returncode
  48. def test_ticket47910_logconv_start_end_positive(topology_st, log_dir):
  49. '''
  50. Execute logconv.pl with -S and -E(endtime) with random time stamp
  51. This is execute successfully
  52. '''
  53. #
  54. # Execute logconv.pl -S -E with random timestamp
  55. #
  56. log.info('Running test_ticket47910 - Execute logconv.pl -S -E with random values')
  57. log.info("taking current time with offset of 2 mins and formatting it to feed -S")
  58. start_time_stamp = (datetime.now() - timedelta(minutes=2))
  59. formatted_start_time_stamp = format_time(start_time_stamp)
  60. log.info("taking current time with offset of 2 mins and formatting it to feed -E")
  61. end_time_stamp = (datetime.now() + timedelta(minutes=2))
  62. formatted_end_time_stamp = format_time(end_time_stamp)
  63. log.info("Executing logconv.pl with -S and -E")
  64. result = execute_logconv(topology_st.standalone, formatted_start_time_stamp, formatted_end_time_stamp, log_dir)
  65. assert result == 0
  66. def test_ticket47910_logconv_start_end_negative(topology_st, log_dir):
  67. '''
  68. Execute logconv.pl with -S and -E(endtime) with random time stamp
  69. This is a negative test case, where endtime will be lesser than the
  70. starttime
  71. This should give error message
  72. '''
  73. #
  74. # Execute logconv.pl -S and -E with random timestamp
  75. #
  76. log.info('Running test_ticket47910 - Execute logconv.pl -S -E with starttime>endtime')
  77. log.info("taking current time with offset of 2 mins and formatting it to feed -S")
  78. start_time_stamp = (datetime.now() + timedelta(minutes=2))
  79. formatted_start_time_stamp = format_time(start_time_stamp)
  80. log.info("taking current time with offset of 2 mins and formatting it to feed -E")
  81. end_time_stamp = (datetime.now() - timedelta(minutes=2))
  82. formatted_end_time_stamp = format_time(end_time_stamp)
  83. log.info("Executing logconv.pl with -S and -E")
  84. result = execute_logconv(topology_st.standalone, formatted_start_time_stamp, formatted_end_time_stamp, log_dir)
  85. assert result == 1
  86. def test_ticket47910_logconv_start_end_invalid(topology_st, log_dir):
  87. '''
  88. Execute logconv.pl with -S and -E(endtime) with invalid time stamp
  89. This is a negative test case, where it should give error message
  90. '''
  91. #
  92. # Execute logconv.pl -S and -E with invalid timestamp
  93. #
  94. log.info('Running test_ticket47910 - Execute logconv.pl -S -E with invalid timestamp')
  95. log.info("Set start time and end time to invalid values")
  96. start_time_stamp = "invalid"
  97. end_time_stamp = "invalid"
  98. log.info("Executing logconv.pl with -S and -E")
  99. result = execute_logconv(topology_st.standalone, start_time_stamp, end_time_stamp, log_dir)
  100. assert result == 1
  101. def test_ticket47910_logconv_noaccesslogs(topology_st, log_dir):
  102. '''
  103. Execute logconv.pl -S(starttime) without specify
  104. access logs location
  105. '''
  106. #
  107. # Execute logconv.pl -S with random timestamp and no access log location
  108. #
  109. log.info('Running test_ticket47910 - Execute logconv.pl without access logs')
  110. log.info("taking current time with offset of 2 mins and formatting it to feed -S")
  111. time_stamp = (datetime.now() - timedelta(minutes=2))
  112. formatted_time_stamp = format_time(time_stamp)
  113. log.info("Executing logconv.pl with -S current time")
  114. cmd = [os.path.join(topology_st.standalone.get_bin_dir(), 'logconv.pl'), '-S', formatted_time_stamp]
  115. log.info(" ".join(cmd))
  116. proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  117. stdout, stderr = proc.communicate()
  118. log.info("standard output" + stdout)
  119. log.info("standard errors" + stderr)
  120. assert proc.returncode == 1
  121. if __name__ == '__main__':
  122. # Run isolated
  123. # -s for DEBUG mode
  124. pytest.main("-s ticket47910_test.py")