summaryrefslogtreecommitdiff
path: root/openstackclient/tests/common/test_context.py
blob: 145546a3c332346b49ff82deab0280d4857cc7c8 (plain)
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
#   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)


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)