annotate tests/test_logging.py @ 18:56596902e9ae default tip

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