Update project maturity status
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / tests / test_logger.py
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements.  See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License.  You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import logging
17
18 from aria.logger import (create_logger,
19                          create_console_log_handler,
20                          create_file_log_handler,
21                          _default_file_formatter,
22                          LoggerMixin,
23                          _DefaultConsoleFormat)
24
25
26 def test_create_logger():
27
28     logger = create_logger()
29     assert logger.name == 'aria'
30     assert len(logger.handlers) == 0
31     assert logger.level == logging.DEBUG
32
33     custom_logger = logging.getLogger('custom_logger')
34     handlers = [logging.FileHandler, logging.StreamHandler]
35     logger = create_logger(logger=custom_logger, handlers=handlers, level=logging.INFO)
36     assert custom_logger.name == 'custom_logger'
37     assert logger.handlers == handlers
38     assert logger.level == logging.INFO
39
40
41 def test_create_console_log_handler(capsys):
42
43     debug_test_string = 'debug_create_console_test_string'
44     info_test_string = 'info_create_console_test_string'
45
46     # Default handler
47     handler = create_console_log_handler()
48     assert isinstance(handler, logging.StreamHandler)
49     assert isinstance(handler.formatter, _DefaultConsoleFormat)
50     assert handler.level == logging.DEBUG
51
52     logger = create_logger(handlers=[handler])
53
54     logger.info(info_test_string)
55     logger.debug(debug_test_string)
56     _, err = capsys.readouterr()
57
58     assert '[DEBUG]> {test_string}'.format(test_string=debug_test_string) in err
59     assert err.count(info_test_string) == 1
60
61     # Custom handler
62     custom_handler = create_console_log_handler(level=logging.INFO, formatter=logging.Formatter())
63     assert isinstance(custom_handler.formatter, logging.Formatter)
64     assert custom_handler.level == logging.INFO
65
66     logger = create_logger(handlers=[custom_handler])
67
68     logger.info(info_test_string)
69     _, err = capsys.readouterr()
70
71     assert err.count(info_test_string) == 1
72
73
74 def test_create_file_log_handler(tmpdir):
75
76     test_string = 'create_file_log_test_string'
77
78     debug_log = tmpdir.join('debug.log')
79     handler = create_file_log_handler(file_path=str(debug_log))
80     assert handler.baseFilename == str(debug_log)
81     assert handler.maxBytes == 5 * 1000 * 1024
82     assert handler.backupCount == 10
83     assert handler.stream is None
84     assert handler.level == logging.DEBUG
85     assert handler.formatter == _default_file_formatter
86
87     logger = create_logger(handlers=[handler])
88     logger.debug(test_string)
89     assert test_string in debug_log.read()
90
91     info_log = tmpdir.join('info.log')
92     handler = create_file_log_handler(
93         file_path=str(info_log),
94         level=logging.INFO,
95         max_bytes=1000,
96         backup_count=2,
97         formatter=logging.Formatter()
98     )
99     assert handler.baseFilename == str(info_log)
100     assert handler.level == logging.INFO
101     assert handler.maxBytes == 1000
102     assert handler.backupCount == 2
103     assert isinstance(handler.formatter, logging.Formatter)
104
105     logger = create_logger(handlers=[handler])
106     logger.info(test_string)
107     assert test_string in info_log.read()
108
109
110 def test_loggermixin(capsys):
111
112     test_string = 'loggermixing_test_string'
113
114     logger = create_logger(handlers=[create_console_log_handler()])
115
116     custom_class = type('CustomClass', (LoggerMixin,), {}).with_logger()
117     custom_class.logger.debug(test_string)
118
119     _, err = capsys.readouterr()
120     assert test_string in err
121
122     for handler in logger.handlers:
123         logger.removeHandler(handler)
124
125     # TODO: figure out what up with pickle
126     # class_pickled = pickle.dumps(custom_class)
127     # class_unpickled = pickle.loads(class_pickled)
128     #
129     # assert vars(class_unpickled) == vars(custom_class)