18
|
1 #!/usr/bin/env python
|
|
2
|
|
3 """
|
|
4 unit-type tests for logging
|
|
5 """
|
|
6
|
|
7 import json
|
|
8 import os
|
|
9 import tempfile
|
|
10 import unittest
|
|
11 from lemuriformes.log import ElasticLogger, read_logfile
|
|
12
|
|
13
|
|
14 class TestLogging(unittest.TestCase):
|
|
15 """test various types of logging supported"""
|
|
16
|
|
17 def test_log_line(self):
|
|
18 """test logging a single JSON line"""
|
|
19
|
|
20 message = "I was here"
|
|
21 logfile = tempfile.mktemp()
|
|
22
|
|
23 assert not os.path.exists(logfile)
|
|
24 logger = ElasticLogger(logfile)
|
|
25 logger(message)
|
|
26
|
|
27 assert os.path.exists(logfile)
|
|
28 with open(logfile) as f:
|
|
29 data = f.read()
|
|
30 assert json.loads(data)['message'] == message
|
|
31 os.remove(logfile)
|
|
32
|
|
33 def test_metadata(self):
|
|
34 """test setting global + local data on a logger"""
|
|
35
|
|
36 message = "I was here"
|
|
37 logfile = tempfile.mktemp()
|
|
38
|
|
39 assert not os.path.exists(logfile)
|
|
40 logger = ElasticLogger(logfile, source='truth')
|
|
41 logger(message, me="k0s")
|
|
42 logger("Hi")
|
|
43 logger(message, me="SomeoneElse")
|
|
44
|
|
45 assert os.path.exists(logfile)
|
|
46 log_data = read_logfile(logfile)
|
|
47 os.remove(logfile)
|
|
48 assert len(log_data) == 3
|
|
49 assert all([line.get('source') == 'truth'
|
|
50 for line in log_data])
|
|
51 assert log_data[0]['message'] == message
|
|
52 assert log_data[-1]['message'] == message
|
|
53 assert log_data[0]['me'] == 'k0s'
|
|
54 assert log_data[-1]['me'] == 'SomeoneElse'
|
|
55
|
|
56
|
|
57
|
|
58 if __name__ == '__main__':
|
|
59 unittest.main()
|