~salgado/offspring/project-ssh-key-ui

« back to all changes in this revision

Viewing changes to lib/offspring/tests/test_config.py

  • Committer: Kevin McDermott
  • Date: 2011-12-02 08:48:05 UTC
  • mfrom: (85.2.5 add-path-independence)
  • Revision ID: kevin@canonical.com-20111202084805-b1qp7vkb0g09lfvc
Merge add-path-independence [r=salgado] [f=897203]

This changes the default file used in Offspring, to use a file sourced from the
root of the offspring Python source files.

The path to the configuration file can still be explicitly set either through
the environment or explicitly on the command-line.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
2
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
3
import os
 
4
import logging
4
5
from cStringIO import StringIO
5
6
 
6
7
import mocker
7
8
 
8
9
from offspring.config import get_configuration
9
10
 
10
 
 
11
 
class TestOffspringConfiguration(mocker.MockerTestCase):
 
11
class CaptureLoggingTestCase(mocker.MockerTestCase):
 
12
 
 
13
    def setUp(self):
 
14
        super(CaptureLoggingTestCase, self).setUp()
 
15
        self.loggers = []
 
16
 
 
17
    def tearDown(self):
 
18
        for logger, handler, old_level in reversed(self.loggers):
 
19
            logger.removeHandler(handler)
 
20
            logger.setLevel(old_level)
 
21
        super(CaptureLoggingTestCase, self).tearDown()
 
22
 
 
23
    def capture_logging(self, logger_name="", log_level=logging.INFO,
 
24
                        format=""):
 
25
        """
 
26
        Call this with an optional logger_name and this will log the named
 
27
        logger into a log_file (StringIO) that is returned.
 
28
        """
 
29
        log_file = StringIO()
 
30
        log_handler = logging.StreamHandler(log_file)
 
31
        logger = logging.getLogger(logger_name)
 
32
        logger.addHandler(log_handler)
 
33
 
 
34
        if format:
 
35
            log_handler.setFormatter(logging.Formatter(format))
 
36
 
 
37
        old_logger_level = logger.level
 
38
        if log_level:
 
39
            logger.setLevel(log_level)
 
40
        self.loggers.append((logger, log_handler, old_logger_level))
 
41
        return log_file
 
42
 
 
43
 
 
44
class TestOffspringConfiguration(CaptureLoggingTestCase):
12
45
 
13
46
    def setUp(self):
14
47
        super(TestOffspringConfiguration, self).setUp()
17
50
        os.getenv = self.fake_get_environment
18
51
        self.addCleanup(setattr, os, "environ", os.environ)
19
52
        os.environ = self.fake_environment
 
53
        self.expected_base_dir = os.path.abspath(
 
54
            os.path.join(os.path.dirname(__file__), '..', '..', '..'))
20
55
 
21
56
    def fake_get_environment(self, key):
22
57
        return self.fake_environment.get(key)
26
61
        If we pass an explicit path to get_configuration, it should be used to
27
62
        read the file and fail if it doesn't exist.
28
63
        """
29
 
        filename = self.makeFile("[test]\nvalue=1\n")
30
 
        configuration = get_configuration(filename)
 
64
        log_file = self.capture_logging(log_level=logging.DEBUG)
 
65
        config_filename = self.makeFile("[test]\nvalue=1\n")
 
66
 
 
67
        configuration = get_configuration(config_filename)
 
68
 
31
69
        self.assertEqual(1, configuration.getint("test", "value"))
 
70
        self.assertEqual("Parsing configuration file %s\n" % config_filename,
 
71
                         log_file.getvalue())
32
72
 
33
73
    def test_supplement_config_with_hostname_and_username(self):
34
74
        """
53
93
        self.assertEqual("aurore", configuration.get("DEFAULT", "hostname"))
54
94
        self.assertEqual("testing", configuration.get("DEFAULT", "username"))
55
95
 
 
96
    def test_default_filename(self):
 
97
        """
 
98
        By default, get_configuration should read
 
99
        <base_dir>config/offspring.cfg if no other configuration
 
100
        file is specified.
 
101
        """
 
102
        log_file = self.capture_logging(log_level=logging.DEBUG)
 
103
        config_filename = os.path.join(self.expected_base_dir, "config",
 
104
                                       "offspring.cfg")
 
105
        path_exists_mock = self.mocker.replace("os.path.exists")
 
106
        path_exists_mock(config_filename)
 
107
        self.mocker.result(True)
 
108
        open_mock = self.mocker.replace("__builtin__.open")
 
109
        open_mock(config_filename, "r")
 
110
        self.mocker.result(StringIO("[test]\nvalue=3\n"))
 
111
        self.mocker.replay()
 
112
 
 
113
        configuration = get_configuration()
 
114
 
 
115
        self.assertEqual(3, configuration.getint("test", "value"))
 
116
        self.assertEqual("Parsing configuration file %s\n" % config_filename,
 
117
                         log_file.getvalue())
 
118
 
 
119
    def test_supplement_config_with_basedir(self):
 
120
        """
 
121
        The Configuration parsed from the file should be supplemented with
 
122
        additional variables.
 
123
 
 
124
        The base_dir variable should reflect the path to the base 'offspring'
 
125
        directory, which is above here.
 
126
        """
 
127
        config_filename = os.path.join(self.expected_base_dir, "config",
 
128
                                       "offspring.cfg")
 
129
        path_exists_mock = self.mocker.replace("os.path.exists")
 
130
        path_exists_mock(config_filename)
 
131
        self.mocker.result(True)
 
132
        open_mock = self.mocker.replace("__builtin__.open")
 
133
        open_mock(config_filename, "r")
 
134
        self.mocker.result(StringIO("[test]\nvalue=3\n"))
 
135
        self.mocker.replay()
 
136
 
 
137
        configuration = get_configuration()
 
138
 
 
139
        self.assertEqual(self.expected_base_dir,
 
140
                         configuration.get("DEFAULT", "base_dir"))
 
141
 
56
142
    def test_set_environment(self):
57
143
        """
58
144
        If no file is passed to get_configuration, then we should attempt to
59
145
        find a path in the environment.
60
146
        """
61
 
        filename = self.makeFile("[test]\nvalue=2\n")
62
 
        os.environ["OFFSPRING_CONFIGFILE"] = filename
 
147
        log_file = self.capture_logging(log_level=logging.DEBUG)
 
148
        config_filename = self.makeFile("[test]\nvalue=2\n")
 
149
        os.environ["OFFSPRING_CONFIGFILE"] = config_filename
 
150
 
63
151
        configuration = get_configuration()
 
152
 
64
153
        self.assertEqual(2, configuration.getint("test", "value"))
65
 
 
66
 
    def test_default_filename(self):
67
 
        """
68
 
        By default, get_configuration should read /srv/config/offspring.cfg if
69
 
        no other configuration file is specified.
70
 
        """
71
 
        path_exists_mock = self.mocker.replace("os.path.exists")
72
 
        path_exists_mock("/srv/config/offspring.cfg")
73
 
        self.mocker.result(True)
74
 
        open_mock = self.mocker.replace("__builtin__.open")
75
 
        open_mock("/srv/config/offspring.cfg", "r")
76
 
        self.mocker.result(StringIO("[test]\nvalue=3\n"))
77
 
        self.mocker.replay()
78
 
        configuration = get_configuration()
79
 
        self.assertEqual(3, configuration.getint("test", "value"))
 
154
        self.assertEqual("Parsing configuration file %s\n" % config_filename,
 
155
                         log_file.getvalue())