1
# Copyright 2012 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Test the start up utility."""
6
from __future__ import (
15
from multiprocessing import (
20
from lockfile import (
24
from maasserver import start_up
25
from maasserver.components import (
26
discard_persistent_error,
27
register_persistent_error,
29
from maasserver.enum import COMPONENT
30
from maasserver.models import (
34
from maasserver.testing.factory import factory
35
from maasserver.testing.testcase import TestCase
36
from maastesting.celery import CeleryFixture
37
from maastesting.fakemethod import FakeMethod
39
from provisioningserver import tasks
40
from testresources import FixtureResource
43
class TestStartUp(TestCase):
44
"""Testing for the method `start_up`."""
47
('celery', FixtureResource(CeleryFixture())),
51
super(TestStartUp, self).setUp()
52
self.patch(start_up, 'LOCK_FILE_NAME', self.make_file())
54
def test_start_up_calls_setup_maas_avahi_service(self):
55
recorder = FakeMethod()
56
self.patch(start_up, 'setup_maas_avahi_service', recorder)
60
(recorder.call_count, recorder.extract_args()))
62
def test_start_up_calls_write_full_dns_config(self):
63
recorder = FakeMethod()
64
self.patch(start_up, 'write_full_dns_config', recorder)
68
(recorder.call_count, recorder.extract_args()))
70
def test_start_up_creates_master_nodegroup(self):
72
self.assertEqual(1, NodeGroup.objects.all().count())
74
def test_start_up_refreshes_workers(self):
75
patched_handlers = tasks.refresh_functions.copy()
76
patched_handlers['nodegroup_uuid'] = Mock()
77
self.patch(tasks, 'refresh_functions', patched_handlers)
79
patched_handlers['nodegroup_uuid'].assert_called_once_with(
80
NodeGroup.objects.ensure_master().uuid)
82
def test_start_up_runs_in_exclusion(self):
83
called = Value('b', False)
87
lock = FileLock(start_up.LOCK_FILE_NAME)
88
self.assertRaises(LockTimeout, lock.acquire, timeout=0.1)
90
def check_lock_in_subprocess():
91
proc = Process(target=check_lock)
95
self.patch(start_up, 'inner_start_up', check_lock_in_subprocess)
97
self.assertTrue(called.value)
99
def test_start_up_respects_timeout_to_acquire_lock(self):
100
recorder = FakeMethod()
101
self.patch(start_up, 'inner_start_up', recorder)
102
# Use a timeout more suitable for automated testing.
103
self.patch(start_up, 'LOCK_TIMEOUT', 0.1)
104
# Manually create a lock.
105
self.make_file(FileLock(start_up.LOCK_FILE_NAME).lock_file)
107
self.assertRaises(LockTimeout, start_up.start_up)
108
self.assertEqual(0, recorder.call_count)
110
def test_start_up_warns_about_missing_boot_images(self):
111
# If no boot images have been registered yet, that may mean that
112
# the import script has not been successfully run yet, or that
113
# the master worker is having trouble reporting its images. And
114
# so start_up registers a persistent warning about this.
115
BootImage.objects.all().delete()
116
discard_persistent_error(COMPONENT.IMPORT_PXE_FILES)
117
recorder = self.patch(start_up, 'register_persistent_error')
122
COMPONENT.IMPORT_PXE_FILES,
123
[args[0][0] for args in recorder.call_args_list])
125
def test_start_up_does_not_warn_if_boot_images_are_known(self):
126
# If boot images are known, there is no warning about the import
128
factory.make_boot_image()
129
recorder = self.patch(start_up, 'register_persistent_error')
134
COMPONENT.IMPORT_PXE_FILES,
135
[args[0][0] for args in recorder.call_args_list])
137
def test_start_up_does_not_warn_if_already_warning(self):
138
# If there already is a warning about missing boot images, it is
139
# based on more precise knowledge of whether we ever heard from
140
# the region worker at all. It will not be replaced by a less
141
# knowledgeable warning.
142
BootImage.objects.all().delete()
143
register_persistent_error(
144
COMPONENT.IMPORT_PXE_FILES, factory.getRandomString())
145
recorder = self.patch(start_up, 'register_persistent_error')
150
COMPONENT.IMPORT_PXE_FILES,
151
[args[0][0] for args in recorder.call_args_list])