~ubuntu-branches/ubuntu/utopic/maas/utopic-security

« back to all changes in this revision

Viewing changes to src/provisioningserver/import_images/tests/test_download_descriptions.py

  • Committer: Package Import Robot
  • Author(s): Julian Edwards, Julian Edwards, Andres Rodriguez
  • Date: 2014-08-21 18:38:27 UTC
  • mfrom: (1.2.34)
  • Revision ID: package-import@ubuntu.com-20140821183827-9xyb5u2o4l8g3zxj
Tags: 1.6.1+bzr2550-0ubuntu1
* New upstream bugfix release:
  - Auto-link node MACs to Networks (LP: #1341619)

[ Julian Edwards ]
* debian/maas-region-controller.postinst: Don't restart RabbitMQ on
  upgrades, just ensure it's running.  Should prevent a race with the
  cluster celery restarting.
* debian/rules: Pull upstream branch from the right place.

[ Andres Rodriguez ]
* debian/maas-region-controller.postinst: Ensure cluster celery is
  started if it also runs on the region.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2014 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Tests for the `download_descriptions` module."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
str = None
 
13
 
 
14
__metaclass__ = type
 
15
__all__ = []
 
16
 
 
17
from maastesting.factory import factory
 
18
from maastesting.matchers import MockAnyCall
 
19
from maastesting.testcase import MAASTestCase
 
20
from provisioningserver.import_images import download_descriptions
 
21
from provisioningserver.import_images.boot_image_mapping import (
 
22
    BootImageMapping,
 
23
    )
 
24
from provisioningserver.import_images.testing.factory import (
 
25
    make_image_spec,
 
26
    set_resource,
 
27
    )
 
28
 
 
29
 
 
30
class TestValuePassesFilterList(MAASTestCase):
 
31
    """Tests for `value_passes_filter_list`."""
 
32
 
 
33
    def test_nothing_passes_empty_list(self):
 
34
        self.assertFalse(
 
35
            download_descriptions.value_passes_filter_list(
 
36
                [], factory.make_name('value')))
 
37
 
 
38
    def test_unmatched_value_does_not_pass(self):
 
39
        self.assertFalse(
 
40
            download_descriptions.value_passes_filter_list(
 
41
                [factory.make_name('filter')], factory.make_name('value')))
 
42
 
 
43
    def test_matched_value_passes(self):
 
44
        value = factory.make_name('value')
 
45
        self.assertTrue(
 
46
            download_descriptions.value_passes_filter_list([value], value))
 
47
 
 
48
    def test_value_passes_if_matched_anywhere_in_filter(self):
 
49
        value = factory.make_name('value')
 
50
        self.assertTrue(
 
51
            download_descriptions.value_passes_filter_list(
 
52
                [
 
53
                    factory.make_name('filter'),
 
54
                    value,
 
55
                    factory.make_name('filter'),
 
56
                ],
 
57
                value))
 
58
 
 
59
    def test_any_value_passes_asterisk(self):
 
60
        self.assertTrue(
 
61
            download_descriptions.value_passes_filter_list(
 
62
                ['*'], factory.make_name('value')))
 
63
 
 
64
 
 
65
class TestValuePassesFilter(MAASTestCase):
 
66
    """Tests for `value_passes_filter`."""
 
67
 
 
68
    def test_unmatched_value_does_not_pass(self):
 
69
        self.assertFalse(
 
70
            download_descriptions.value_passes_filter(
 
71
                factory.make_name('filter'), factory.make_name('value')))
 
72
 
 
73
    def test_matching_value_passes(self):
 
74
        value = factory.make_name('value')
 
75
        self.assertTrue(
 
76
            download_descriptions.value_passes_filter(value, value))
 
77
 
 
78
    def test_any_value_matches_asterisk(self):
 
79
        self.assertTrue(
 
80
            download_descriptions.value_passes_filter(
 
81
                '*', factory.make_name('value')))
 
82
 
 
83
 
 
84
class TestImagePassesFilter(MAASTestCase):
 
85
    """Tests for `image_passes_filter`."""
 
86
 
 
87
    def make_filter_from_image(self, image_spec=None):
 
88
        """Create a filter dict that matches the given `ImageSpec`.
 
89
 
 
90
        If `image_spec` is not given, creates a random value.
 
91
        """
 
92
        if image_spec is None:
 
93
            image_spec = make_image_spec()
 
94
        return {
 
95
            'arches': [image_spec.arch],
 
96
            'subarches': [image_spec.subarch],
 
97
            'release': image_spec.release,
 
98
            'labels': [image_spec.label],
 
99
            }
 
100
 
 
101
    def test_any_image_passes_none_filter(self):
 
102
        arch, subarch, release, label = make_image_spec()
 
103
        self.assertTrue(
 
104
            download_descriptions.image_passes_filter(
 
105
                None, arch, subarch, release, label))
 
106
 
 
107
    def test_any_image_passes_empty_filter(self):
 
108
        arch, subarch, release, label = make_image_spec()
 
109
        self.assertTrue(
 
110
            download_descriptions.image_passes_filter(
 
111
                [], arch, subarch, release, label))
 
112
 
 
113
    def test_image_passes_matching_filter(self):
 
114
        image = make_image_spec()
 
115
        self.assertTrue(
 
116
            download_descriptions.image_passes_filter(
 
117
                [self.make_filter_from_image(image)],
 
118
                image.arch, image.subarch, image.release, image.label))
 
119
 
 
120
    def test_image_does_not_pass_nonmatching_filter(self):
 
121
        image = make_image_spec()
 
122
        self.assertFalse(
 
123
            download_descriptions.image_passes_filter(
 
124
                [self.make_filter_from_image()],
 
125
                image.arch, image.subarch, image.release, image.label))
 
126
 
 
127
    def test_image_passes_if_one_filter_matches(self):
 
128
        image = make_image_spec()
 
129
        self.assertTrue(
 
130
            download_descriptions.image_passes_filter(
 
131
                [
 
132
                    self.make_filter_from_image(),
 
133
                    self.make_filter_from_image(image),
 
134
                    self.make_filter_from_image(),
 
135
                ], image.arch, image.subarch, image.release, image.label))
 
136
 
 
137
    def test_filter_checks_release(self):
 
138
        image = make_image_spec()
 
139
        self.assertFalse(
 
140
            download_descriptions.image_passes_filter(
 
141
                [
 
142
                    self.make_filter_from_image(image._replace(
 
143
                        release=factory.make_name('other-release')))
 
144
                ], image.arch, image.subarch, image.release, image.label))
 
145
 
 
146
    def test_filter_checks_arches(self):
 
147
        image = make_image_spec()
 
148
        self.assertFalse(
 
149
            download_descriptions.image_passes_filter(
 
150
                [
 
151
                    self.make_filter_from_image(image._replace(
 
152
                        arch=factory.make_name('other-arch')))
 
153
                ], image.arch, image.subarch, image.release, image.label))
 
154
 
 
155
    def test_filter_checks_subarches(self):
 
156
        image = make_image_spec()
 
157
        self.assertFalse(
 
158
            download_descriptions.image_passes_filter(
 
159
                [
 
160
                    self.make_filter_from_image(image._replace(
 
161
                        subarch=factory.make_name('other-subarch')))
 
162
                ], image.arch, image.subarch, image.release, image.label))
 
163
 
 
164
    def test_filter_checks_labels(self):
 
165
        image = make_image_spec()
 
166
        self.assertFalse(
 
167
            download_descriptions.image_passes_filter(
 
168
                [
 
169
                    self.make_filter_from_image(image._replace(
 
170
                        label=factory.make_name('other-label')))
 
171
                ], image.arch, image.subarch, image.release, image.label))
 
172
 
 
173
 
 
174
class TestBootMerge(MAASTestCase):
 
175
    """Tests for `boot_merge`."""
 
176
 
 
177
    def test_integrates(self):
 
178
        # End-to-end scenario for boot_merge: start with an empty boot
 
179
        # resources dict, and receive one resource from Simplestreams.
 
180
        total_resources = BootImageMapping()
 
181
        resources_from_repo = set_resource()
 
182
        download_descriptions.boot_merge(total_resources, resources_from_repo)
 
183
        # Since we started with an empty dict, the result contains the same
 
184
        # item that we got from Simplestreams, and nothing else.
 
185
        self.assertEqual(resources_from_repo.mapping, total_resources.mapping)
 
186
 
 
187
    def test_obeys_filters(self):
 
188
        filters = [
 
189
            {
 
190
                'arches': [factory.make_name('other-arch')],
 
191
                'subarches': [factory.make_name('other-subarch')],
 
192
                'release': factory.make_name('other-release'),
 
193
                'label': [factory.make_name('other-label')],
 
194
            },
 
195
            ]
 
196
        total_resources = BootImageMapping()
 
197
        resources_from_repo = set_resource()
 
198
        download_descriptions.boot_merge(
 
199
            total_resources, resources_from_repo, filters=filters)
 
200
        self.assertEqual({}, total_resources.mapping)
 
201
 
 
202
    def test_does_not_overwrite_existing_entry(self):
 
203
        image = make_image_spec()
 
204
        total_resources = set_resource(
 
205
            resource="Original resource", image_spec=image)
 
206
        original_resources = total_resources.mapping.copy()
 
207
        resources_from_repo = set_resource(
 
208
            resource="New resource", image_spec=image)
 
209
        download_descriptions.boot_merge(total_resources, resources_from_repo)
 
210
        self.assertEqual(original_resources, total_resources.mapping)
 
211
 
 
212
 
 
213
class TestDownloadImageDescriptions(MAASTestCase):
 
214
 
 
215
    def test_warns_if_no_resources_found(self):
 
216
        self.patch(download_descriptions, 'logger')
 
217
        # Stop the RepoDumper from finding any boot resources at all.
 
218
        self.patch(download_descriptions, 'RepoDumper')
 
219
        path = factory.make_name('path')
 
220
        download_descriptions.download_image_descriptions(path)
 
221
        self.assertThat(
 
222
            download_descriptions.logger.warn,
 
223
            MockAnyCall(
 
224
                "No resources found in Simplestreams repository %r.  "
 
225
                "Is it correctly configured?",
 
226
                path))