summaryrefslogtreecommitdiffstats
path: root/tests/test_parser.py
blob: bc041fc53d2c2e1b9c1989d041db5fc4477cb707 (plain)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import textwrap

import pytest

from debputy import DEBPUTY_DOC_ROOT_DIR
from debputy.exceptions import DebputySubstitutionError
from debputy.highlevel_manifest_parser import YAMLManifestParser
from debputy.manifest_parser.exceptions import ManifestParseException
from debputy.plugin.api.test_api import build_virtual_file_system


def normalize_doc_link(message) -> str:
    return message.replace(DEBPUTY_DOC_ROOT_DIR, "{{DEBPUTY_DOC_ROOT_DIR}}")


@pytest.fixture()
def manifest_parser_pkg_foo(
    amd64_dpkg_architecture_variables,
    dpkg_arch_query,
    source_package,
    package_single_foo_arch_all_cxt_amd64,
    amd64_substitution,
    no_profiles_or_build_options,
    debputy_plugin_feature_set,
) -> YAMLManifestParser:
    # We need an empty directory to avoid triggering packager provided files.
    debian_dir = build_virtual_file_system([])
    return YAMLManifestParser(
        "debian/test-debputy.manifest",
        source_package,
        package_single_foo_arch_all_cxt_amd64,
        amd64_substitution,
        amd64_dpkg_architecture_variables,
        dpkg_arch_query,
        no_profiles_or_build_options,
        debputy_plugin_feature_set,
        debian_dir=debian_dir,
    )


def test_parsing_no_manifest(manifest_parser_pkg_foo):
    manifest = manifest_parser_pkg_foo.build_manifest()

    assert [p.name for p in manifest.all_packages] == ["foo"]
    assert [p.name for p in manifest.active_packages] == ["foo"]


def test_parsing_version_only(manifest_parser_pkg_foo):
    content = textwrap.dedent(
        """\
    manifest-version: '0.1'
    """
    )

    manifest = manifest_parser_pkg_foo.parse_manifest(fd=content)

    assert [p.name for p in manifest.all_packages] == ["foo"]
    assert [p.name for p in manifest.active_packages] == ["foo"]


def test_parsing_variables(manifest_parser_pkg_foo):
    # https://salsa.debian.org/debian/debputy/-/issues/58
    content = textwrap.dedent(
        """\
    manifest-version: '0.1'
    definitions:
      variables:
        LIBPATH: "/usr/lib/{{DEB_HOST_MULTIARCH}}"
        SONAME: "1"
        LETTER_O: "o"
    installations:
      - install:
           source: build/libfoo.so.{{SONAME}}
           dest-dir: "{{LIBPATH}}"
           into: f{{LETTER_O}}{{LETTER_O}}
    packages:
      f{{LETTER_O}}{{LETTER_O}}:
        transformations:
          - create-symlink:
              path: "{{LIBPATH}}/libfoo.so.{{SONAME}}.0.0"
              target: "{{LIBPATH}}/libfoo.so.{{SONAME}}"
    """
    )
    manifest_parser_pkg_foo.parse_manifest(fd=content)
    # TODO: Verify that the substitution is applied correctly throughout
    # (currently, the test just verifies that we do not reject the manifest)


@pytest.mark.parametrize(
    "varname",
    [
        "PACKAGE",
        "DEB_HOST_ARCH",
        "DEB_BLAH_ARCH",
        "env:UNEXISTING",
        "token:TAB",
    ],
)
def test_parsing_variables_reserved(manifest_parser_pkg_foo, varname):
    content = textwrap.dedent(
        f"""\
    manifest-version: '0.1'
    definitions:
      variables:
        '{varname}': "test"
    """
    )

    with pytest.raises(ManifestParseException) as e_info:
        manifest_parser_pkg_foo.parse_manifest(fd=content)

    msg = f'The variable "{varname}" is already reserved/defined. Error triggered by definitions.variables.{varname}.'
    assert normalize_doc_link(e_info.value.args[0]) == msg


def test_parsing_variables_interdependent_ok(manifest_parser_pkg_foo):
    content = textwrap.dedent(
        """\
    manifest-version: '0.1'
    definitions:
      variables:
        DOC_PATH: "/usr/share/doc/foo"
        EXAMPLE_PATH: "{{DOC_PATH}}/examples"
    installations:
    - install:
        source: foo.example
        dest-dir: "{{EXAMPLE_PATH}}"
    """
    )

    manifest = manifest_parser_pkg_foo.parse_manifest(fd=content)
    resolved = manifest.substitution.substitute("{{EXAMPLE_PATH}}", "test")
    assert resolved == "/usr/share/doc/foo/examples"


def test_parsing_variables_unused(manifest_parser_pkg_foo):
    content = textwrap.dedent(
        f"""\
    manifest-version: '0.1'
    definitions:
      variables:
        UNUSED: "test"
    """
    )

    with pytest.raises(ManifestParseException) as e_info:
        manifest_parser_pkg_foo.parse_manifest(fd=content)

    msg = (
        'The variable "UNUSED" is unused. Either use it or remove it.'
        " The variable was declared at definitions.variables.UNUSED."
    )
    assert normalize_doc_link(e_info.value.args[0]) == msg


def test_parsing_package_foo_empty(manifest_parser_pkg_foo):
    content = textwrap.dedent(
        """\
    manifest-version: '0.1'
    packages:
        foo:
    """
    )

    with pytest.raises(ManifestParseException) as e_info:
        manifest_parser_pkg_foo.parse_manifest(fd=content)

    msg = (
        "The attribute packages.foo must be a non-empty mapping. Please see"
        " {{DEBPUTY_DOC_ROOT_DIR}}/MANIFEST-FORMAT.md#binary-package-rules for the documentation."
    )
    assert normalize_doc_link(e_info.value.args[0]) == msg


def test_parsing_package_bar_empty(manifest_parser_pkg_foo):
    content = textwrap.dedent(
        """\
    manifest-version: '0.1'
    packages:
        bar:
    """
    )

    with pytest.raises(ManifestParseException) as e_info:
        manifest_parser_pkg_foo.parse_manifest(fd=content)

    assert 'package "bar" is not present' in e_info.value.args[0]


def test_transformations_no_list(manifest_parser_pkg_foo):
    content = textwrap.dedent(
        """\
    manifest-version: '0.1'
    packages:
        foo:
            transformations:
              create-symlinks:
                 path: a
                 target: b
    """
    )

    with pytest.raises(ManifestParseException) as e_info:
        manifest_parser_pkg_foo.parse_manifest(fd=content)

    assert "packages.foo.transformations" in e_info.value.args[0]
    assert "must be a list" in e_info.value.args[0]


def test_create_symlinks_missing_path(manifest_parser_pkg_foo):
    content = textwrap.dedent(
        """\
    manifest-version: '0.1'
    packages:
        foo:
            transformations:
              - create-symlink:
                  target: b
    """
    )

    with pytest.raises(ManifestParseException) as e_info:
        manifest_parser_pkg_foo.parse_manifest(fd=content)

    msg = (
        "The following keys were required but not present at packages.foo.transformations[0].create-symlink: 'path'"
        " (Documentation: "
        "{{DEBPUTY_DOC_ROOT_DIR}}/MANIFEST-FORMAT.md#create-symlinks-transformation-rule-create-symlink)"
    )
    assert normalize_doc_link(e_info.value.args[0]) == msg


def test_create_symlinks_unknown_replacement_rule(manifest_parser_pkg_foo):
    content = textwrap.dedent(
        """\
    manifest-version: '0.1'
    packages:
        foo:
            transformations:
              - create-symlink:
                  path: usr/share/foo
                  target: /usr/share/bar
                  replacement-rule: golf
    """
    )

    with pytest.raises(ManifestParseException) as e_info:
        manifest_parser_pkg_foo.parse_manifest(fd=content)

    msg = (
        'The attribute "packages.foo.transformations[0].create-symlink.replacement-rule <Search for: usr/share/foo>"'
        " did not have a valid structure/type: Value (golf) must be one of the following literal values:"
        ' "error-if-exists", "error-if-directory", "abort-on-non-empty-directory", "discard-existing"'
    )
    assert normalize_doc_link(e_info.value.args[0]) == msg


def test_create_symlinks_missing_target(manifest_parser_pkg_foo):
    content = textwrap.dedent(
        """\
    manifest-version: '0.1'
    packages:
        foo:
            transformations:
              - create-symlink:
                  path: a
    """
    )

    with pytest.raises(ManifestParseException) as e_info:
        manifest_parser_pkg_foo.parse_manifest(fd=content)

    msg = (
        "The following keys were required but not present at packages.foo.transformations[0].create-symlink: 'target'"
        " (Documentation: "
        "{{DEBPUTY_DOC_ROOT_DIR}}/MANIFEST-FORMAT.md#create-symlinks-transformation-rule-create-symlink)"
    )
    assert normalize_doc_link(e_info.value.args[0]) == msg


def test_create_symlinks_not_normalized_path(manifest_parser_pkg_foo):
    content = textwrap.dedent(
        """\
    manifest-version: '0.1'
    packages:
        foo:
            transformations:
              - create-symlink:
                  path: ../bar
                  target: b
    """
    )

    with pytest.raises(ManifestParseException) as e_info:
        manifest_parser_pkg_foo.parse_manifest(fd=content)

    expected = (
        'The path "../bar" provided in packages.foo.transformations[0].create-symlink.path <Search for: ../bar>'
        ' should be relative to the root of the package and not use any ".." or "." segments.'
    )
    assert e_info.value.args[0] == expected


def test_unresolvable_subst_in_source_context(manifest_parser_pkg_foo):
    content = textwrap.dedent(
        """\
    manifest-version: '0.1'
    installations:
    - install:
       source: "foo.sh"
       as: "usr/bin/{{PACKAGE}}"
    """
    )

    with pytest.raises(DebputySubstitutionError) as e_info:
        manifest_parser_pkg_foo.parse_manifest(fd=content)

    expected = (
        "The variable {{PACKAGE}} is not available while processing installations[0].install.as"
        " <Search for: foo.sh>."
    )

    assert e_info.value.args[0] == expected


def test_yaml_error_duplicate_key(manifest_parser_pkg_foo):
    content = textwrap.dedent(
        """\
    manifest-version: '0.1'
    packages:
        foo:
            transformations:
              - create-symlink:
                  path: ../bar
                  target: b
                  # Duplicate key error
                  path: ../foo
    """
    )

    with pytest.raises(ManifestParseException) as e_info:
        manifest_parser_pkg_foo.parse_manifest(fd=content)

    assert "duplicate key" in e_info.value.args[0]


def test_yaml_error_tab_start(manifest_parser_pkg_foo):
    content = textwrap.dedent(
        """\
    manifest-version: '0.1'
    packages:
        foo:
            transformations:
              - create-symlink:
                  path: ../bar
                  target: b
    # Tab is not allowed here in this case.
    \ta
    """
    )

    with pytest.raises(ManifestParseException) as e_info:
        manifest_parser_pkg_foo.parse_manifest(fd=content)

    assert "'\\t' that cannot start any token" in e_info.value.args[0]


def test_yaml_octal_mode_int(manifest_parser_pkg_foo):
    content = textwrap.dedent(
        """\
    manifest-version: '0.1'
    packages:
        foo:
            transformations:
              - path-metadata:
                  path: usr/share/bar
                  mode: 0755
    """
    )

    with pytest.raises(ManifestParseException) as e_info:
        manifest_parser_pkg_foo.parse_manifest(fd=content)

    msg = (
        'The attribute "packages.foo.transformations[0].path-metadata.mode <Search for: usr/share/bar>" did not'
        " have a valid structure/type: The attribute must be a FileSystemMode (string)"
    )

    assert e_info.value.args[0] == msg


def test_yaml_clean_after_removal(manifest_parser_pkg_foo):
    content = textwrap.dedent(
        """\
    manifest-version: '0.1'
    packages:
        foo:
            clean-after-removal:
            - /var/log/foo/*.log
            - /var/log/foo/*.log.gz
            - path: /var/log/foo/
              ignore-non-empty-dir: true
            - /etc/non-conffile-configuration.conf
            - path: /var/cache/foo
              recursive: true

    """
    )

    manifest_parser_pkg_foo.parse_manifest(fd=content)


def test_binary_version(manifest_parser_pkg_foo):
    content = textwrap.dedent(
        """\
    manifest-version: '0.1'
    packages:
        foo:
            binary-version: 1:2.3

    """
    )

    manifest = manifest_parser_pkg_foo.parse_manifest(fd=content)
    assert manifest.package_state_for("foo").binary_version == "1:2.3"


@pytest.mark.parametrize(
    "path,is_accepted",
    [
        ("usr/bin/foo", False),
        ("var/cache/foo*", False),
        ("var/cache/foo", True),
        ("var/cache/foo/", True),
        ("var/cache/foo/*", True),
        ("var/cache/foo/*.*", True),
        ("var/cache/foo/*.txt", True),
        ("var/cache/foo/cache.*", True),
        ("etc/foo*", False),
        ("etc/foo/*", True),
        ("etc/foo/", True),
        # /var/log is special-cased
        ("/var/log/foo*", True),
        ("/var/log/foo/*.*", True),
        ("/var/log/foo/", True),
        # Unsupported pattern at the time of writing
        ("/var/log/foo/*.*.*", False),
        # Questionable rules
        ("*", False),
        ("*.la", False),
        ("*/foo/*", False),
    ],
)
def test_yaml_clean_after_removal_unsafe_path(
    manifest_parser_pkg_foo,
    path: str,
    is_accepted: bool,
) -> None:
    content = textwrap.dedent(
        f"""\
    manifest-version: '0.1'
    packages:
        foo:
            clean-after-removal:
            - {path}
    """
    )

    if is_accepted:
        manifest_parser_pkg_foo.parse_manifest(fd=content)
    else:
        with pytest.raises(ManifestParseException) as e_info:
            manifest_parser_pkg_foo.parse_manifest(fd=content)