File: modulepackager.py

package info (click to toggle)
libmodulemd 2.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 5,152 kB
  • sloc: ansic: 37,845; python: 3,236; xml: 1,739; sh: 377; makefile: 42
file content (199 lines) | stat: -rw-r--r-- 6,684 bytes parent folder | download | duplicates (3)
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
import json
import logging
import os
import sys

try:
    import unittest
    import gi

    gi.require_version("Modulemd", "2.0")
    from gi.repository import GLib
    from gi.repository import Modulemd
except ImportError:
    # Return error 77 to skip this test on platforms without the necessary
    # python modules
    sys.exit(77)

from copy import deepcopy
from base import TestBase

logging.basicConfig(level=logging.DEBUG)


class TestModulemdPackager(TestBase):
    def test_sanity(self):
        # Validate that the specification parses correctly
        stream = Modulemd.ModuleStream.read_file(
            os.path.join(
                self.source_root, "yaml_specs/modulemd_packager_v2.yaml"
            ),
            True,
        )
        self.assertIsNotNone(stream)

        # Once read in, a modulemd-packager document is a ModuleStream of the
        # same version.
        self.assertIs(type(stream), Modulemd.ModuleStreamV2)

    def test_read_minimal(self):
        minimal_valid = """---
document: modulemd-packager
version: 2
data:
  summary: A minimal valid module
  description: A minimalistic module description
  license:
    module:
      - DUMMY
...
"""
        stream = Modulemd.ModuleStream.read_string(minimal_valid, True)
        self.assertIsNotNone(stream)

        # Once read in, a modulemd-packager document is a ModuleStream of the
        # same version.
        self.assertIs(type(stream), Modulemd.ModuleStreamV2)

    def test_read_extra_values(self):
        minimal_valid = {
            "document": "modulemd-packager",
            "version": 2,
            "data": {
                "summary": "A minimal valid module",
                "description": "A minimalistic module description",
                "license": {"module": ["DUMMY1", "DUMMY2"]},
            },
        }

        extra_attrs = [
            "name",
            "stream",
            "context",
            "arch",
            "servicelevels",
            "xmd",
            "buildopts",
            "artifacts",
        ]

        for attr in extra_attrs:
            minimal_plus_extra = deepcopy(minimal_valid)

            # We can just pretend they're all scalars, because the parser will
            # halt before it tries to validate the value.
            minimal_plus_extra["data"][attr] = "invalid key"

            logging.debug(
                "YAML: {}".format(json.dumps(minimal_plus_extra, indent=2))
            )

            # This code takes advantage of the fact that JSON is a proper
            # subset of YAML, so we can avoid requiring pyYAML for the
            # tests.
            with self.assertRaises(gi.repository.GLib.GError) as cm:
                stream = Modulemd.ModuleStream.read_string(
                    json.dumps(minimal_plus_extra), True
                )

            gerror = cm.exception
            self.assertTrue(
                gerror.matches(
                    domain=Modulemd.yaml_error_quark(),
                    code=Modulemd.YamlError.UNKNOWN_ATTR,
                )
            )

        # Handle license.content as a special case since it's not directly
        # under `data`.
        minimal_plus_extra = deepcopy(minimal_valid)
        minimal_plus_extra["data"]["license"]["content"] = "invalid key"

        logging.debug(
            "YAML: {}".format(json.dumps(minimal_plus_extra, indent=2))
        )

        # This code takes advantage of the fact that JSON is a proper
        # subset of YAML, so we can avoid requiring pyYAML for the tests.
        with self.assertRaises(gi.repository.GLib.GError) as cm:
            stream = Modulemd.ModuleStream.read_string(
                json.dumps(minimal_plus_extra), True
            )

        gerror = cm.exception
        self.assertTrue(
            gerror.matches(
                domain=Modulemd.yaml_error_quark(),
                code=Modulemd.YamlError.UNKNOWN_ATTR,
            )
        )

    def test_fail_v1(self):
        minimal_v1 = {
            "document": "modulemd-packager",
            "version": 1,
            "data": {
                "summary": "A minimal valid module",
                "description": "A minimalistic module description",
                "license": {"module": ["DUMMY1", "DUMMY2"]},
            },
        }

        with self.assertRaises(gi.repository.GLib.Error) as cm:
            stream = Modulemd.ModuleStream.read_string(
                json.dumps(minimal_v1), True
            )
        gerror = cm.exception
        self.assertTrue(
            gerror.matches(
                domain=Modulemd.yaml_error_quark(),
                code=Modulemd.YamlError.INCONSISTENT,
            )
        )

    def test_v3_xmd(self):

        # We have a chicken-egg problem with overrides, since they can only
        # be tested if they are already installed. This means they need to
        # be run in the CI. In order to avoid changes to these tests or the
        # overrides breaking things, we'll skip them if the appropriate
        # override is not installed.
        if "_overrides_module" in dir(Modulemd) and hasattr(
            gi.overrides.Modulemd, "PackagerV3"
        ):

            # The XMD python tests can only be run against the installed lib
            # because the overrides that translate between python and GVariant
            # must be installed in /usr/lib/python*/site-packages/gi/overrides
            # or they are not included when importing Modulemd
            packager = Modulemd.PackagerV3.new()
            # An empty dictionary should be returned if no xmd value is set
            self.assertEqual(packager.get_xmd(), dict())

            xmd = {"outer_key": {"inner_key": ["scalar", "another_scalar"]}}

            packager.set_xmd(xmd)

            xmd_copy = packager.get_xmd()
            assert xmd_copy
            assert "outer_key" in xmd_copy
            assert "inner_key" in xmd_copy["outer_key"]
            assert "scalar" in xmd_copy["outer_key"]["inner_key"]
            assert "another_scalar" in xmd_copy["outer_key"]["inner_key"]

            # Verify that we can add content and save it back
            xmd["something"] = ["foo", "bar"]
            packager.set_xmd(xmd)

            xmd_copy = packager.get_xmd()
            assert xmd_copy
            assert "outer_key" in xmd_copy
            assert "inner_key" in xmd_copy["outer_key"]
            assert "scalar" in xmd_copy["outer_key"]["inner_key"]
            assert "another_scalar" in xmd_copy["outer_key"]["inner_key"]
            assert "something" in xmd_copy
            assert xmd_copy["something"] == ["foo", "bar"]


if __name__ == "__main__":
    unittest.main()