~ubuntu-branches/ubuntu/precise/maas/precise-updates

« back to all changes in this revision

Viewing changes to src/maasserver/tests/test_start_up.py

Tags: 1.2+bzr1373+dfsg-0ubuntu1~12.04.4
* SECURITY UPDATE: failure to authenticate downloaded content (LP: #1039513)
  - debian/patches/CVE-2013-1058.patch: Authenticate downloaded files with
    GnuPG and MD5SUM files. Thanks to Julian Edwards.
  - CVE-2013-1058
* SECURITY UPDATE: configuration options may be loaded from current working
  directory (LP: #1158425)
  - debian/patches/CVE-2013-1057-1-2.patch: Do not load configuration
    options from the current working directory. Thanks to Julian Edwards.
  - CVE-2013-1057

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Test the start up utility."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
__metaclass__ = type
 
13
__all__ = []
 
14
 
 
15
from multiprocessing import (
 
16
    Process,
 
17
    Value,
 
18
    )
 
19
 
 
20
from lockfile import (
 
21
    FileLock,
 
22
    LockTimeout,
 
23
    )
 
24
from maasserver import start_up
 
25
from maasserver.components import (
 
26
    discard_persistent_error,
 
27
    register_persistent_error,
 
28
    )
 
29
from maasserver.enum import COMPONENT
 
30
from maasserver.models import (
 
31
    BootImage,
 
32
    NodeGroup,
 
33
    )
 
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
 
38
from mock import Mock
 
39
from provisioningserver import tasks
 
40
from testresources import FixtureResource
 
41
 
 
42
 
 
43
class TestStartUp(TestCase):
 
44
    """Testing for the method `start_up`."""
 
45
 
 
46
    resources = (
 
47
        ('celery', FixtureResource(CeleryFixture())),
 
48
        )
 
49
 
 
50
    def setUp(self):
 
51
        super(TestStartUp, self).setUp()
 
52
        self.patch(start_up, 'LOCK_FILE_NAME', self.make_file())
 
53
 
 
54
    def test_start_up_calls_setup_maas_avahi_service(self):
 
55
        recorder = FakeMethod()
 
56
        self.patch(start_up, 'setup_maas_avahi_service', recorder)
 
57
        start_up.start_up()
 
58
        self.assertEqual(
 
59
            (1, [()]),
 
60
            (recorder.call_count, recorder.extract_args()))
 
61
 
 
62
    def test_start_up_calls_write_full_dns_config(self):
 
63
        recorder = FakeMethod()
 
64
        self.patch(start_up, 'write_full_dns_config', recorder)
 
65
        start_up.start_up()
 
66
        self.assertEqual(
 
67
            (1, [()]),
 
68
            (recorder.call_count, recorder.extract_args()))
 
69
 
 
70
    def test_start_up_creates_master_nodegroup(self):
 
71
        start_up.start_up()
 
72
        self.assertEqual(1, NodeGroup.objects.all().count())
 
73
 
 
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)
 
78
        start_up.start_up()
 
79
        patched_handlers['nodegroup_uuid'].assert_called_once_with(
 
80
            NodeGroup.objects.ensure_master().uuid)
 
81
 
 
82
    def test_start_up_runs_in_exclusion(self):
 
83
        called = Value('b', False)
 
84
 
 
85
        def check_lock():
 
86
            called.value = True
 
87
            lock = FileLock(start_up.LOCK_FILE_NAME)
 
88
            self.assertRaises(LockTimeout, lock.acquire, timeout=0.1)
 
89
 
 
90
        def check_lock_in_subprocess():
 
91
            proc = Process(target=check_lock)
 
92
            proc.start()
 
93
            proc.join()
 
94
 
 
95
        self.patch(start_up, 'inner_start_up', check_lock_in_subprocess)
 
96
        start_up.start_up()
 
97
        self.assertTrue(called.value)
 
98
 
 
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)
 
106
 
 
107
        self.assertRaises(LockTimeout, start_up.start_up)
 
108
        self.assertEqual(0, recorder.call_count)
 
109
 
 
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')
 
118
 
 
119
        start_up.start_up()
 
120
 
 
121
        self.assertIn(
 
122
            COMPONENT.IMPORT_PXE_FILES,
 
123
            [args[0][0] for args in recorder.call_args_list])
 
124
 
 
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
 
127
        # script.
 
128
        factory.make_boot_image()
 
129
        recorder = self.patch(start_up, 'register_persistent_error')
 
130
 
 
131
        start_up.start_up()
 
132
 
 
133
        self.assertNotIn(
 
134
            COMPONENT.IMPORT_PXE_FILES,
 
135
            [args[0][0] for args in recorder.call_args_list])
 
136
 
 
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')
 
146
 
 
147
        start_up.start_up()
 
148
 
 
149
        self.assertNotIn(
 
150
            COMPONENT.IMPORT_PXE_FILES,
 
151
            [args[0][0] for args in recorder.call_args_list])