File: test_package_squash.py

package info (click to toggle)
python-diskimage-builder 3.37.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,572 kB
  • sloc: sh: 7,380; python: 6,444; makefile: 37
file content (297 lines) | stat: -rw-r--r-- 9,471 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# Copyright 2018 Red Hat, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.
import collections
import functools
import importlib
import os
import sys
from unittest import mock

from oslotest import base
from testtools.matchers import Mismatch

importlib.machinery.SOURCE_SUFFIXES.append('')
file_path = (os.path.dirname(os.path.realpath(__file__)) +
             '/../bin/package-installs-squash')
spec = importlib.util.spec_from_file_location('installs_squash', file_path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
sys.modules['installs_squash'] = module
installs_squash = module
importlib.machinery.SOURCE_SUFFIXES.pop()


class IsMatchingInstallList(object):

    def __init__(self, expected):
        self.expected = expected

    def match(self, actual):
        for phase, ops in self.expected.items():
            if phase not in actual:
                # missing the phase
                return Mismatch(
                    "Phase %d does not exist in %s" % (phase, actual))
            for op, pkgs in ops.items():
                if op not in actual[phase]:
                    # missing op (install/uninstall)
                    return Mismatch(
                        "Operation %s does not exist in %s" % (op, ops))
                # on py2 these can be out of order, we just want a match
                expected_phase_ops = sorted(self.expected[phase][op])
                actual_phase_ops = sorted(actual[phase][op])
                if expected_phase_ops != actual_phase_ops:
                    return Mismatch(
                        "Operation list %s does not match expected  %s" %
                        (actual[phase][op], self.expected[phase][op]))


class TestPackageInstall(base.BaseTestCase):
    def setUp(self):
        super(TestPackageInstall, self).setUp()
        self.final_dict = collections.defaultdict(
            functools.partial(collections.defaultdict, list))

    def test_simple(self):
        '''Test a basic package install'''
        objs = {
            'test_package': ''
        }

        result = installs_squash.collect_data(
            self.final_dict, objs, 'test_element')

        expected = {
            'install.d': {
                'install': [('test_package', 'test_element')]
            }
        }

        self.assertThat(result, IsMatchingInstallList(expected))

    @mock.patch.object(os, 'environ', dict(ARCH='arm64'))
    def test_arch(self):
        '''Exercise the arch and not-arch flags'''
        objs = {
            'test_package': '',
            'test_arm64_package': {
                'arch': 'arm64'
            },
            'do_not_install': {
                'not-arch': 'arm64'
            }
        }

        result = installs_squash.collect_data(
            self.final_dict, objs, 'test_element')

        expected = {
            'install.d': {
                'install': [('test_package', 'test_element'),
                            ('test_arm64_package', 'test_element')]
            }
        }

        self.assertThat(result, IsMatchingInstallList(expected))

    kernel_objs = {
        'linux-image-generic': [
            {
                'not-arch': 'arm64',
                'when': 'DIB_UBUNTU_KERNEL = linux-image-generic',
            },
            {
                'arch': 'arm64',
                'when': (
                    'DIB_RELEASE != xenial',
                    'DIB_UBUNTU_KERNEL = linux-image-generic',
                )
            },
        ],
        'linux-generic-hwe-16.04': {
            'arch': 'arm64',
            'when': (
                'DIB_RELEASE = xenial',
                'DIB_UBUNTU_KERNEL = linux-image-generic',
            )
        },
    }

    def _test_kernel_objs_match(self, arch, release, expected):
        with mock.patch.object(os, 'environ',
                               dict(ARCH=arch,
                                    DIB_UBUNTU_KERNEL='linux-image-generic',
                                    DIB_RELEASE=release)):
            result = installs_squash.collect_data(
                self.final_dict, self.kernel_objs, 'test_element')

        expected = {
            'install.d': {
                'install': [(expected, 'test_element')]
            }
        }
        self.assertThat(result, IsMatchingInstallList(expected))

    def test_param_list_x86(self):
        self._test_kernel_objs_match('x86_64', 'focal', 'linux-image-generic')

    def test_param_list_arm64_xenial(self):
        self._test_kernel_objs_match('arm64', 'xenial',
                                     'linux-generic-hwe-16.04')

    def test_param_list_arm64_focal(self):
        self._test_kernel_objs_match('arm64', 'focal', 'linux-image-generic')

    @mock.patch.object(os, 'environ', dict(DIB_FEATURE='1', **os.environ))
    def test_skip_when(self):
        '''Exercise the when flag'''
        objs = {
            'skipped_package': {
                'when': 'DIB_FEATURE=0'
            },
            'not_skipped_package': {
                'when': 'DIB_FEATURE=1'
            },
            'not_equal_package': {
                'when': 'DIB_FEATURE!=0'
            },
            'not_equal_skipped_package': {
                'when': 'DIB_FEATURE!=1'
            },
        }

        result = installs_squash.collect_data(
            self.final_dict, objs, 'test_element')

        expected = {
            'install.d': {
                'install': [('not_skipped_package', 'test_element'),
                            ('not_equal_package', 'test_element')]
            }
        }

        self.assertThat(result, IsMatchingInstallList(expected))

    def test_skip_no_var(self):
        '''Exercise the skip_when missing variable failure case'''
        objs = {
            'package': {
                'when': 'MISSING_VAR=1'
            },
        }

        self.assertRaises(RuntimeError, installs_squash.collect_data,
                          self.final_dict, objs, 'test_element')

    @mock.patch.object(os, 'environ',
                       dict(
                           DIB_A_FEATURE='1',
                           DIB_B_FEATURE='1',
                           DIB_C_FEATURE='1'))
    def test_skip_when_list(self):
        '''Exercise the when flag with lists'''
        objs = {
            'not_skipped_package': {
                'when': [
                    'DIB_A_FEATURE=1',
                    'DIB_B_FEATURE=1',
                    'DIB_C_FEATURE=1'
                ]
            },
            'skipped_package': {
                'when': [
                    'DIB_A_FEATURE=1',
                    'DIB_B_FEATURE=0',
                    'DIB_C_FEATURE=1',
                ]
            },
        }

        result = installs_squash.collect_data(
            self.final_dict, objs, 'test_element')

        expected = {
            'install.d': {
                'install': [('not_skipped_package', 'test_element')]
            }
        }

        self.assertThat(result, IsMatchingInstallList(expected))

    def test_install_overrides_uninstall_install_first(self):
        '''Test an install overrides uninstall'''
        objs = {
            'test_package': ''
        }

        result = installs_squash.collect_data(
            self.final_dict, objs, 'test_element1')

        expected = {
            'install.d': {
                'install': [('test_package', 'test_element1')]
            }
        }

        self.assertThat(result, IsMatchingInstallList(expected))

        objs = {
            'test_package': {'build-only': 'true'}
        }

        result = installs_squash.collect_data(
            self.final_dict, objs, 'test_element2')

        expected = {
            'install.d': {
                'install': [('test_package', 'test_element1'),
                            ('test_package', 'test_element2')]
            }
        }

        self.assertThat(result, IsMatchingInstallList(expected))

    def test_install_overrides_uninstall_uninstall_first(self):
        '''Test an install overrides uninstall'''
        objs = {
            'test_package': {'build-only': 'true'}
        }

        result = installs_squash.collect_data(
            self.final_dict, objs, 'test_element1')

        expected = {
            'install.d': {
                'install': [('test_package', 'test_element1')],
                'uninstall': [('test_package', 'test_element1')]
            }
        }

        self.assertThat(result, IsMatchingInstallList(expected))

        objs = {
            'test_package': ''
        }

        result = installs_squash.collect_data(
            self.final_dict, objs, 'test_element2')

        expected = {
            'install.d': {
                'install': [('test_package', 'test_element1'),
                            ('test_package', 'test_element2')]
            }
        }

        self.assertThat(result, IsMatchingInstallList(expected))