test_output.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  2. # not use this file except in compliance with the License. You may obtain
  3. # a copy of the License at
  4. #
  5. # http://www.apache.org/licenses/LICENSE-2.0
  6. #
  7. # Unless required by applicable law or agreed to in writing, software
  8. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. # License for the specific language governing permissions and limitations
  11. # under the License.
  12. import syslog
  13. import unittest
  14. from datetime import timedelta
  15. from daiquiri import output
  16. class TestOutput(unittest.TestCase):
  17. def test_find_facility(self):
  18. self.assertEqual(syslog.LOG_USER,
  19. output.Syslog._find_facility("user"))
  20. self.assertEqual(syslog.LOG_LOCAL1,
  21. output.Syslog._find_facility("log_local1"))
  22. self.assertEqual(syslog.LOG_LOCAL2,
  23. output.Syslog._find_facility("LOG_local2"))
  24. self.assertEqual(syslog.LOG_LOCAL3,
  25. output.Syslog._find_facility("LOG_LOCAL3"))
  26. self.assertEqual(syslog.LOG_LOCAL4,
  27. output.Syslog._find_facility("LOCaL4"))
  28. def test_get_log_file_path(self):
  29. self.assertEqual("foobar.log",
  30. output._get_log_file_path("foobar.log"))
  31. self.assertEqual("/var/log/foo/foobar.log",
  32. output._get_log_file_path("foobar.log",
  33. logdir="/var/log/foo"))
  34. self.assertEqual("/var/log/foobar.log",
  35. output._get_log_file_path(logdir="/var/log",
  36. program_name="foobar"))
  37. self.assertEqual("/var/log/foobar.log",
  38. output._get_log_file_path(logdir="/var/log",
  39. program_name="foobar"))
  40. self.assertEqual("/var/log/foobar.journal",
  41. output._get_log_file_path(
  42. logdir="/var/log",
  43. logfile_suffix=".journal",
  44. program_name="foobar"))
  45. def test_timedelta_seconds(self):
  46. fn = output.TimedRotatingFile._timedelta_to_seconds
  47. hour = 60 * 60 # seconds * minutes
  48. one_hour = [
  49. timedelta(hours=1),
  50. timedelta(minutes=60),
  51. timedelta(seconds=hour),
  52. hour,
  53. float(hour)
  54. ]
  55. for t in one_hour:
  56. self.assertEqual(hour, fn(t))
  57. error_cases = [
  58. 'string',
  59. ['some', 'list'],
  60. ('some', 'tuple',),
  61. ('tuple',),
  62. {'dict': 'mapping'}
  63. ]
  64. for t in error_cases:
  65. self.assertRaises(AttributeError, fn, t)