3
3
from optparse import OptionParser
7
from landscape.lib.dbus_util import Object
8
from landscape.deployment import (
9
LandscapeService, Configuration, get_versioned_persist,
10
assert_unowned_bus_name, run_landscape_service)
11
from landscape.tests.helpers import (
12
LandscapeTest, LandscapeIsolatedTest, DBusHelper)
5
from landscape.deployment import Configuration, get_versioned_persist
7
from landscape.tests.helpers import LandscapeTest
13
8
from landscape.tests.mocker import ANY
16
11
class BabbleConfiguration(Configuration):
17
12
config_section = "babble"
18
13
default_config_filenames = []
19
15
def make_parser(self):
20
16
parser = super(BabbleConfiguration, self).make_parser()
21
17
parser.add_option("--whatever", metavar="STUFF")
56
54
self.assertEquals(self.config.log_level, "command line")
58
56
def test_command_line_option_without_default(self):
59
58
class MyConfiguration(Configuration):
60
60
def make_parser(self):
61
61
parser = OptionParser()
62
62
# Keep the dash in the option name to ensure it works.
63
63
parser.add_option("--foo-bar")
65
66
self.assertEquals(MyConfiguration().foo_bar, None)
67
68
def test_command_line_with_required_options(self):
68
70
class MyConfiguration(Configuration):
69
71
required_options = ("foo_bar",)
71
74
def make_parser(self):
72
75
parser = super(MyConfiguration, self).make_parser()
73
76
# Keep the dash in the option name to ensure it works.
86
89
self.assertEquals(self.config.foo_bar, "ooga")
88
91
def test_command_line_with_unsaved_options(self):
89
93
class MyConfiguration(Configuration):
90
94
unsaved_options = ("foo_bar",)
92
97
def make_parser(self):
93
98
parser = super(MyConfiguration, self).make_parser()
94
99
# Keep the dash in the option name to ensure it works.
95
100
parser.add_option("--foo-bar", metavar="NAME")
97
103
self.reset_config(configuration_class=MyConfiguration)
98
104
self.write_config_file()
225
233
data = open(filename).read()
226
234
self.assertEquals(data.strip(), "[client]\nlog_level = error")
228
def test_bus_option(self):
229
"""The bus option must be specified as 'system' or 'session'."""
230
self.assertRaises(SystemExit,
233
self.config.load(["--bus", "session"])
234
self.assertEquals(self.config.bus, "session")
235
self.config.load(["--bus", "system"])
236
self.assertEquals(self.config.bus, "system")
238
236
def test_config_option(self):
239
237
opts = self.parser.parse_args(["--config", "hello.cfg"])[0]
240
238
self.assertEquals(opts.config, "hello.cfg")
245
243
self.assertEquals(self.config.hello, "world")
247
245
def test_load_typed_option_from_file(self):
248
247
class MyConfiguration(self.config_class):
249
249
def make_parser(self):
250
250
parser = super(MyConfiguration, self).make_parser()
251
251
parser.add_option("--year", default=1, type="int")
253
254
filename = self.makeFile("[client]\nyear = 2008\n")
254
255
config = MyConfiguration()
255
256
config.load(["--config", filename])
256
257
self.assertEquals(config.year, 2008)
258
259
def test_load_typed_option_from_command_line(self):
259
261
class MyConfiguration(self.config_class):
260
263
def make_parser(self):
261
264
parser = super(MyConfiguration, self).make_parser()
262
265
parser.add_option("--year", default=1, type="int")
264
268
config = MyConfiguration()
265
269
config.load(["--year", "2008"])
266
270
self.assertEquals(config.year, 2008)
339
343
self.assertEquals(self.config.get_config_filename(),
340
344
explicit_filename)
346
def test_sockets_path(self):
348
The L{Configuration.socket_path} property returns the path to the
351
self.assertEquals(self.config.sockets_path,
352
"/var/lib/landscape/client/sockets")
344
355
class GetVersionedPersistTest(LandscapeTest):
346
357
def test_upgrade_service(self):
347
359
class FakeService(object):
348
360
persist_filename = self.makePersistFile(content="")
349
361
service_name = "monitor"
360
372
persist = get_versioned_persist(FakeService())
361
373
self.assertEquals(stash[0], persist)
364
class LandscapeServiceTest(LandscapeTest):
367
super(LandscapeServiceTest, self).setUp()
368
signal.signal(signal.SIGUSR1, signal.SIG_DFL)
371
super(LandscapeServiceTest, self).tearDown()
372
signal.signal(signal.SIGUSR1, signal.SIG_DFL)
374
def test_create_persist(self):
375
class FakeService(LandscapeService):
376
persist_filename = self.makePersistFile(content="")
377
service_name = "monitor"
378
service = FakeService(None)
379
self.assertEquals(service.persist.filename, service.persist_filename)
381
def test_no_persist_without_filename(self):
382
class FakeService(LandscapeService):
383
service_name = "monitor"
384
service = FakeService(None)
385
self.assertFalse(hasattr(service, "persist"))
387
def test_usr1_rotates_logs(self):
389
SIGUSR1 should cause logs to be reopened.
391
logging.getLogger().addHandler(logging.FileHandler(self.makeFile()))
392
# Store the initial set of handlers
393
original_streams = [handler.stream for handler in
394
logging.getLogger().handlers if
395
isinstance(handler, logging.FileHandler)]
397
# Instantiating LandscapeService should register the handler
398
LandscapeService(None)
399
# We'll call it directly
400
handler = signal.getsignal(signal.SIGUSR1)
401
self.assertTrue(handler)
403
new_streams = [handler.stream for handler in
404
logging.getLogger().handlers if
405
isinstance(handler, logging.FileHandler)]
407
for stream in new_streams:
408
self.assertTrue(stream not in original_streams)
410
def test_ignore_sigusr1(self):
412
SIGUSR1 is ignored if we so request.
415
ignore_sigusr1 = True
417
# Instantiating LandscapeService should not register the
418
# handler if we request to ignore it.
419
config = Configuration()
420
LandscapeService(config)
422
handler = signal.getsignal(signal.SIGUSR1)
423
self.assertFalse(handler)
426
class AssertUnownedBusNameTest(LandscapeIsolatedTest):
428
helpers = [DBusHelper]
430
class BoringService(Object):
431
bus_name = "com.example.BoringService"
432
object_path = "/com/example/BoringService"
434
def test_raises_sysexit_when_owned(self):
435
service = self.BoringService(self.bus)
436
self.assertRaises(SystemExit, assert_unowned_bus_name,
437
self.bus, self.BoringService.bus_name)
439
def test_do_nothing_when_unowned(self):
440
assert_unowned_bus_name(self.bus, self.BoringService.bus_name)
443
class RunLandscapeServiceTests(LandscapeTest):
444
def test_wrong_user(self):
445
getuid_mock = self.mocker.replace("os.getuid")
446
reactor_install_mock = self.mocker.replace("landscape.reactor.install")
447
reactor_install_mock()
449
self.mocker.result(1)
452
class MyService(LandscapeService):
453
service_name = "broker"
455
sys_exit = self.assertRaises(
456
SystemExit, run_landscape_service, Configuration,
457
MyService, [], "whatever")
458
self.assertIn("landscape-broker must be run as landscape",