tests.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Licensed to Cloudera, Inc. under one
  4. # or more contributor license agreements. See the NOTICE file
  5. # distributed with this work for additional information
  6. # regarding copyright ownership. Cloudera, Inc. licenses this file
  7. # to you under the Apache License, Version 2.0 (the
  8. # "License"); you may not use this file except in compliance
  9. # with the License. You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. import logging
  19. import tempfile
  20. from nose.tools import assert_true, assert_false, assert_equal, assert_not_equal
  21. from desktop.log import get_audit_logger, AuditHandler
  22. from desktop.conf import AUDIT_EVENT_LOG_DIR, AUDIT_LOG_MAX_FILE_SIZE
  23. def test_one_audit():
  24. with tempfile.NamedTemporaryFile("w+t") as log_tmp:
  25. # KB
  26. reset = [
  27. AUDIT_EVENT_LOG_DIR.set_for_testing(log_tmp.name),
  28. AUDIT_LOG_MAX_FILE_SIZE.set_for_testing('25KB')
  29. ]
  30. audit_logger = get_audit_logger()
  31. audit_handler = audit_logger.handlers[0]
  32. assert_equal(25 * 1024 ** 1, audit_handler.maxBytes)
  33. assert_equal(len(audit_logger.handlers), 1, audit_logger.handlers)
  34. assert_true(isinstance(audit_handler, AuditHandler), audit_logger.handlers)
  35. audit_logger = get_audit_logger()
  36. assert_equal(len(audit_logger.handlers), 1, audit_logger.handlers) # Not adding handler twice
  37. # Cleanup
  38. audit_logger.removeHandler(audit_handler)
  39. for r in reset:
  40. r()
  41. # MB
  42. reset = [
  43. AUDIT_EVENT_LOG_DIR.set_for_testing(log_tmp.name),
  44. AUDIT_LOG_MAX_FILE_SIZE.set_for_testing('25MB')
  45. ]
  46. audit_logger = get_audit_logger()
  47. audit_handler = audit_logger.handlers[0]
  48. assert_equal(25 * 1024 ** 2, audit_handler.maxBytes)
  49. assert_equal(len(audit_logger.handlers), 1, audit_logger.handlers)
  50. assert_true(isinstance(audit_handler, AuditHandler), audit_logger.handlers)
  51. audit_logger = get_audit_logger()
  52. assert_equal(len(audit_logger.handlers), 1, audit_logger.handlers) # Not adding handler twice
  53. # Cleanup
  54. audit_logger.removeHandler(audit_handler)
  55. for r in reset:
  56. r()
  57. # GB
  58. reset = [
  59. AUDIT_EVENT_LOG_DIR.set_for_testing(log_tmp.name),
  60. AUDIT_LOG_MAX_FILE_SIZE.set_for_testing('25GB')
  61. ]
  62. audit_logger = get_audit_logger()
  63. audit_handler = audit_logger.handlers[0]
  64. assert_equal(25 * 1024 ** 3, audit_handler.maxBytes)
  65. assert_equal(len(audit_logger.handlers), 1, audit_logger.handlers)
  66. assert_true(isinstance(audit_handler, AuditHandler), audit_logger.handlers)
  67. audit_logger = get_audit_logger()
  68. assert_equal(len(audit_logger.handlers), 1, audit_logger.handlers) # Not adding handler twice
  69. # Cleanup
  70. audit_logger.removeHandler(audit_handler)
  71. for r in reset:
  72. r()