File: test_isoinfo.py

package info (click to toggle)
osinfo-db 0.20221130-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 22,780 kB
  • sloc: python: 1,672; sh: 344; makefile: 89
file content (85 lines) | stat: -rw-r--r-- 3,001 bytes parent folder | download
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
# This work is licensed under the GNU GPLv2 or later.
# See the COPYING file in the top-level directory.

import logging
from pathlib import Path
import pytest

from . import util
from . import isodata


def _get_isodatapaths():
    """
    Collect iso media data and return a list of tuples:
        (osname, isodatapaths)
    """
    this = Path(__file__).resolve()
    isodata_path = Path(this.parent, "isodata")

    ret = []
    osdirs = [d for d in isodata_path.glob(str(Path("*", "*"))) if d.is_dir()]

    for osdir in sorted(osdirs, key=util.path_sort):
        isodatapaths = list(osdir.glob("*.txt"))
        if len(isodatapaths):
            ret.append((osdir.name, isodatapaths))

    return ret


@pytest.mark.parametrize("testdata", _get_isodatapaths(), ids=lambda d: d[0])
def test_iso_detection(testdata):
    osname, isodatapaths = testdata
    for isodatapath in isodatapaths:
        # A symlink indicates an ISO from another version
        # that is ambiguous and so accidentally matches
        # this distro too. We ignore them here, because
        # we'll validate against the primary OS version
        if isodatapath.is_symlink():
            continue

        detected = []
        isodatamedia = isodata.get_isodatamedia(isodatapath)
        for osxml2 in util.DataFiles.oses():
            for media in osxml2.medias:
                if isodatamedia.match(media.iso):
                    if osname != osxml2.shortid:
                        logging.warning(
                            "ISO '%s' was matched by OS '%s' while it "
                            "should only be matched by OS '%s'",
                            isodatamedia.path,
                            osxml2.shortid,
                            osname,
                        )
                    else:
                        logging.info(
                            "ISO '%s' matched by OS '%s'",
                            isodatamedia.path,
                            osxml2.shortid,
                        )
                    # For several distros we do not have the volume-size
                    # set as part of our DB, thus multiple detections may
                    # occur. Although this case is not the optimal, as long
                    # as we detect the very same distro it's okay-ish.
                    if osxml2.shortid not in detected:
                        detected.append(osxml2.shortid)

        ok = True
        for osid in detected:
            # Primary (correct) match
            if osid == osname:
                continue

            # Ambiguous (incorrect) matches, where we can't
            # distinguish from a different distro version
            link = Path(isodatapath.parent.parent, osid, isodatapath.name)
            if not link.exists():
                ok = False
        if ok:
            continue

        raise AssertionError(
            "isodata: %s\nMatched=%s but expected=%s"
            % (isodatapath, detected, [osname])
        )