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).
4
5
from cStringIO import StringIO
8
9
from offspring.config import get_configuration
11
class TestOffspringConfiguration(mocker.MockerTestCase):
11
class CaptureLoggingTestCase(mocker.MockerTestCase):
14
super(CaptureLoggingTestCase, self).setUp()
18
for logger, handler, old_level in reversed(self.loggers):
19
logger.removeHandler(handler)
20
logger.setLevel(old_level)
21
super(CaptureLoggingTestCase, self).tearDown()
23
def capture_logging(self, logger_name="", log_level=logging.INFO,
26
Call this with an optional logger_name and this will log the named
27
logger into a log_file (StringIO) that is returned.
30
log_handler = logging.StreamHandler(log_file)
31
logger = logging.getLogger(logger_name)
32
logger.addHandler(log_handler)
35
log_handler.setFormatter(logging.Formatter(format))
37
old_logger_level = logger.level
39
logger.setLevel(log_level)
40
self.loggers.append((logger, log_handler, old_logger_level))
44
class TestOffspringConfiguration(CaptureLoggingTestCase):
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__), '..', '..', '..'))
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.
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")
67
configuration = get_configuration(config_filename)
31
69
self.assertEqual(1, configuration.getint("test", "value"))
70
self.assertEqual("Parsing configuration file %s\n" % config_filename,
33
73
def test_supplement_config_with_hostname_and_username(self):
53
93
self.assertEqual("aurore", configuration.get("DEFAULT", "hostname"))
54
94
self.assertEqual("testing", configuration.get("DEFAULT", "username"))
96
def test_default_filename(self):
98
By default, get_configuration should read
99
<base_dir>config/offspring.cfg if no other configuration
102
log_file = self.capture_logging(log_level=logging.DEBUG)
103
config_filename = os.path.join(self.expected_base_dir, "config",
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"))
113
configuration = get_configuration()
115
self.assertEqual(3, configuration.getint("test", "value"))
116
self.assertEqual("Parsing configuration file %s\n" % config_filename,
119
def test_supplement_config_with_basedir(self):
121
The Configuration parsed from the file should be supplemented with
122
additional variables.
124
The base_dir variable should reflect the path to the base 'offspring'
125
directory, which is above here.
127
config_filename = os.path.join(self.expected_base_dir, "config",
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"))
137
configuration = get_configuration()
139
self.assertEqual(self.expected_base_dir,
140
configuration.get("DEFAULT", "base_dir"))
56
142
def test_set_environment(self):
58
144
If no file is passed to get_configuration, then we should attempt to
59
145
find a path in the environment.
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
63
151
configuration = get_configuration()
64
153
self.assertEqual(2, configuration.getint("test", "value"))
66
def test_default_filename(self):
68
By default, get_configuration should read /srv/config/offspring.cfg if
69
no other configuration file is specified.
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"))
78
configuration = get_configuration()
79
self.assertEqual(3, configuration.getint("test", "value"))
154
self.assertEqual("Parsing configuration file %s\n" % config_filename,