1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
import logging
import mock
import os
from openstackclient.common import context
from openstackclient.tests import utils
class TestContext(utils.TestCase):
TEST_LOG_FILE = "/tmp/test_log_file"
def setUp(self):
super(TestContext, self).setUp()
def tearDown(self):
super(TestContext, self).tearDown()
if os.path.exists(self.TEST_LOG_FILE):
os.remove(self.TEST_LOG_FILE)
def setup_handler_logging_level(self):
handler_type = logging.FileHandler
handler = logging.FileHandler(filename=self.TEST_LOG_FILE)
handler.setLevel(logging.ERROR)
logging.getLogger('').addHandler(handler)
context.setup_handler_logging_level(handler_type, logging.INFO)
self.log.info("test log")
ld = open(self.TEST_LOG_FILE)
line = ld.readlines()
ld.close()
if os.path.exists(self.TEST_LOG_FILE):
os.remove(self.TEST_LOG_FILE)
self.assertGreaterEqual(line.find("test log"), 0)
@mock.patch("openstackclient.common.context._setup_handler_for_logging")
def test_setup_logging(self, setuph):
setuph.return_value = mock.MagicMock()
shell = mock.MagicMock()
cloud_config = mock.MagicMock()
cloud_config.auth = {
'project_name': 'heart-o-gold',
'username': 'zaphod'
}
cloud_config.config = {
'log_level': 'debug',
'log_file': self.TEST_LOG_FILE,
'cloud': 'megadodo'
}
context.setup_logging(shell, cloud_config)
self.assertEqual(True, shell.enable_operation_logging)
def test_log_level_from_options(self):
opts = mock.Mock()
opts.verbose_level = 0
self.assertEqual(logging.ERROR, context.log_level_from_options(opts))
opts.verbose_level = 1
self.assertEqual(logging.WARNING, context.log_level_from_options(opts))
opts.verbose_level = 2
self.assertEqual(logging.INFO, context.log_level_from_options(opts))
opts.verbose_level = 3
self.assertEqual(logging.DEBUG, context.log_level_from_options(opts))
def test_log_level_from_config(self):
cfg = {'verbose_level': 0}
self.assertEqual(logging.ERROR, context.log_level_from_config(cfg))
cfg = {'verbose_level': 1}
self.assertEqual(logging.WARNING, context.log_level_from_config(cfg))
cfg = {'verbose_level': 2}
self.assertEqual(logging.INFO, context.log_level_from_config(cfg))
cfg = {'verbose_level': 3}
self.assertEqual(logging.DEBUG, context.log_level_from_config(cfg))
cfg = {'verbose_level': 1, 'log_level': 'critical'}
self.assertEqual(logging.CRITICAL, context.log_level_from_config(cfg))
cfg = {'verbose_level': 1, 'log_level': 'error'}
self.assertEqual(logging.ERROR, context.log_level_from_config(cfg))
cfg = {'verbose_level': 1, 'log_level': 'warning'}
self.assertEqual(logging.WARNING, context.log_level_from_config(cfg))
cfg = {'verbose_level': 1, 'log_level': 'info'}
self.assertEqual(logging.INFO, context.log_level_from_config(cfg))
cfg = {'verbose_level': 1, 'log_level': 'debug'}
self.assertEqual(logging.DEBUG, context.log_level_from_config(cfg))
cfg = {'verbose_level': 1, 'log_level': 'bogus'}
self.assertEqual(logging.WARNING, context.log_level_from_config(cfg))
cfg = {'verbose_level': 1, 'log_level': 'info', 'debug': True}
self.assertEqual(logging.DEBUG, context.log_level_from_config(cfg))
@mock.patch('warnings.simplefilter')
def test_set_warning_filter(self, simplefilter):
context.set_warning_filter(logging.ERROR)
simplefilter.assert_called_with("ignore")
context.set_warning_filter(logging.WARNING)
simplefilter.assert_called_with("ignore")
context.set_warning_filter(logging.INFO)
simplefilter.assert_called_with("once")
class Test_LogContext(utils.TestCase):
def setUp(self):
super(Test_LogContext, self).setUp()
def test_context(self):
ctx = context._LogContext()
self.assertTrue(ctx)
def test_context_to_dict(self):
ctx = context._LogContext('cloudsName', 'projectName', 'userNmae')
ctx_dict = ctx.to_dict()
self.assertEqual('cloudsName', ctx_dict['clouds_name'])
self.assertEqual('projectName', ctx_dict['project_name'])
self.assertEqual('userNmae', ctx_dict['username'])
class Test_LogContextFormatter(utils.TestCase):
def setUp(self):
super(Test_LogContextFormatter, self).setUp()
self.ctx = context._LogContext('cloudsName', 'projectName', 'userNmae')
self.addfmt = "%(clouds_name)s %(project_name)s %(username)s"
def test_contextrrormatter(self):
ctxfmt = context._LogContextFormatter()
self.assertTrue(ctxfmt)
def test_context_format(self):
record = mock.MagicMock()
logging.Formatter.format = mock.MagicMock()
logging.Formatter.format.return_value = record
ctxfmt = context._LogContextFormatter(context=self.ctx,
fmt=self.addfmt)
addctx = ctxfmt.format(record)
self.assertEqual('cloudsName', addctx.clouds_name)
self.assertEqual('projectName', addctx.project_name)
self.assertEqual('userNmae', addctx.username)
|