Skip to content

Commit

Permalink
Enable Python bindings for enhanced Modulemd.read_packager_file/string()
Browse files Browse the repository at this point in the history
Signed-off-by: Merlin Mathesius <mmathesi@redhat.com>
  • Loading branch information
mmathesius authored and sgallagher committed Jan 5, 2021
1 parent 4939077 commit 169136d
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 4 deletions.
8 changes: 4 additions & 4 deletions modulemd/include/modulemd-2.0/modulemd.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ ModulemdModuleIndex *
modulemd_load_string (const gchar *yaml_string, GError **error);

/**
* modulemd_read_packager_file:
* modulemd_read_packager_file: (skip)
* @yaml_path: (in): A path to a YAML file containing a packager document.
* @object: (out): (transfer full): A newly allocated #ModulemdModuleStreamV2 or
* #ModulemdPackagerV3 object initialized with the content from @yaml_path.
Expand All @@ -362,7 +362,7 @@ modulemd_read_packager_file (const gchar *yaml_path,
GError **error);

/**
* modulemd_read_packager_file_ext:
* modulemd_read_packager_file_ext: (rename-to modulemd_read_packager_file)
* @yaml_path: (in): A path to a YAML file containing a packager document.
* @object: (out): (transfer full): A newly allocated #ModulemdModuleStreamV2 or
* #ModulemdPackagerV3 object initialized with the content from @yaml_path.
Expand All @@ -389,7 +389,7 @@ modulemd_read_packager_file_ext (const gchar *yaml_path,
GError **error);

/**
* modulemd_read_packager_string:
* modulemd_read_packager_string: (skip)
* @yaml_string: (in): A YAML string containing a packager document.
* @object: (out): (transfer full): A newly allocated #ModulemdModuleStreamV2 or
* #ModulemdPackagerV3 object initialized with the content from @yaml_string.
Expand All @@ -408,7 +408,7 @@ modulemd_read_packager_string (const gchar *yaml_string,
GError **error);

/**
* modulemd_read_packager_string_ext:
* modulemd_read_packager_string_ext: (rename-to modulemd_read_packager_string)
* @yaml_string: (in): A YAML string containing a packager document.
* @object: (out): (transfer full): A newly allocated #ModulemdModuleStreamV2 or
* #ModulemdPackagerV3 object initialized with the content from @yaml_string.
Expand Down
1 change: 1 addition & 0 deletions modulemd/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ endforeach

python_tests = {
'buildopts' : 'tests/ModulemdTests/buildopts.py',
'common' : 'tests/ModulemdTests/common.py',
'componentrpm' : 'tests/ModulemdTests/componentrpm.py',
'defaults' : 'tests/ModulemdTests/defaults.py',
'defaultsv1' : 'tests/ModulemdTests/defaultsv1.py',
Expand Down
221 changes: 221 additions & 0 deletions modulemd/tests/ModulemdTests/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
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 base import TestBase

logging.basicConfig(level=logging.DEBUG)


class TestCommon(TestBase):
def test_packager_read_file(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 not (
"_overrides_module" in dir(Modulemd)
and hasattr(gi.overrides.Modulemd, "read_packager_file")
):
logging.debug(
"read_packager_file() override not installed, skipping tests"
)
return

# Validate that the PackagerV2 specification parses correctly
doc = Modulemd.read_packager_file(
os.path.join(
self.source_root, "yaml_specs/modulemd_packager_v2.yaml"
),
)
self.assertIsNotNone(doc)
# Once read in, a modulemd-packager v2 document is a ModuleStream of the
# same version.
self.assertIs(type(doc), Modulemd.ModuleStreamV2)
# Confirm module and stream names are undefined
self.assertIsNone(doc.get_module_name())
self.assertIsNone(doc.get_stream_name())

# Read the PackagerV2 specification with module/stream name overrides
doc = Modulemd.read_packager_file(
os.path.join(
self.source_root, "yaml_specs/modulemd_packager_v2.yaml"
),
"modulename-override",
"streamname-override",
)
self.assertIsNotNone(doc)
self.assertIs(type(doc), Modulemd.ModuleStreamV2)
# Confirm module and stream names were set
self.assertEqual(doc.get_module_name(), "modulename-override")
self.assertEqual(doc.get_stream_name(), "streamname-override")

# Valid PackagerV3 file
doc = Modulemd.read_packager_file(
os.path.join(
self.source_root, "yaml_specs/modulemd_packager_v3.yaml"
),
)
self.assertIsNotNone(doc)
self.assertIs(type(doc), Modulemd.PackagerV3)

# Read PackagerV3 with module/stream name overrides
doc = Modulemd.read_packager_file(
os.path.join(
self.source_root, "yaml_specs/modulemd_packager_v3.yaml"
),
"modulename-override",
"streamname-override",
)
self.assertIsNotNone(doc)
self.assertIs(type(doc), Modulemd.PackagerV3)
# Confirm module and stream names were set
self.assertEqual(doc.get_module_name(), "modulename-override")
self.assertEqual(doc.get_stream_name(), "streamname-override")

# Validate that the ModuleStreamV2 specification parses correctly
doc = Modulemd.read_packager_file(
os.path.join(
self.source_root, "yaml_specs/modulemd_stream_v2.yaml"
),
)
self.assertIsNotNone(doc)
self.assertIs(type(doc), Modulemd.ModuleStreamV2)
# Confirm module and stream names are correct
self.assertEqual(doc.get_module_name(), "foo")
self.assertEqual(doc.get_stream_name(), "latest")

# Read the ModuleStreamV2 specification with module/stream name overrides
doc = Modulemd.read_packager_file(
os.path.join(
self.source_root, "yaml_specs/modulemd_stream_v2.yaml"
),
"modulename-override",
"streamname-override",
)
self.assertIsNotNone(doc)
self.assertIs(type(doc), Modulemd.ModuleStreamV2)
# Confirm module and stream names were set
self.assertEqual(doc.get_module_name(), "modulename-override")
self.assertEqual(doc.get_stream_name(), "streamname-override")

def test_packager_read_string(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 not (
"_overrides_module" in dir(Modulemd)
and hasattr(gi.overrides.Modulemd, "read_packager_string")
):
logging.debug(
"read_packager_string() override not installed, skipping tests"
)
return

# PackagerV2
minimal_valid = """---
document: modulemd-packager
version: 2
data:
summary: A minimal valid module
description: A minimalistic module description
license:
module:
- MIT
...
"""
doc = Modulemd.read_packager_string(minimal_valid)
self.assertIsNotNone(doc)
# Once read in, a modulemd-packager document is a ModuleStream of the
# same version.
self.assertIs(type(doc), Modulemd.ModuleStreamV2)
# Confirm module and stream names are undefined
self.assertIsNone(doc.get_module_name())
self.assertIsNone(doc.get_stream_name())

# Read again with module/stream name overrides
doc = Modulemd.read_packager_string(
minimal_valid, "modulename-override", "streamname-override",
)
self.assertIsNotNone(doc)
self.assertIs(type(doc), Modulemd.ModuleStreamV2)
# Confirm module and stream names were set
self.assertEqual(doc.get_module_name(), "modulename-override")
self.assertEqual(doc.get_stream_name(), "streamname-override")

# PackagerV3
minimal_valid = """---
document: modulemd-packager
version: 3
data:
summary: A minimal valid module
description: A minimalistic module description
license:
- MIT
...
"""
doc = Modulemd.read_packager_string(minimal_valid)
self.assertIsNotNone(doc)
self.assertIs(type(doc), Modulemd.PackagerV3)
# Confirm module and stream names are undefined
self.assertIsNone(doc.get_module_name())
self.assertIsNone(doc.get_stream_name())

# Read again with module/stream name overrides
doc = Modulemd.read_packager_string(
minimal_valid, "modulename-override", "streamname-override",
)
self.assertIsNotNone(doc)
self.assertIs(type(doc), Modulemd.PackagerV3)
# Confirm module and stream names were set
self.assertEqual(doc.get_module_name(), "modulename-override")
self.assertEqual(doc.get_stream_name(), "streamname-override")

# ModuleStreamV2
minimal_valid = """---
document: modulemd-stream
version: 2
data:
summary: A minimal valid module
description: A minimalistic module description
license:
module:
- MIT
...
"""
doc = Modulemd.read_packager_string(minimal_valid)
self.assertIsNotNone(doc)
self.assertIs(type(doc), Modulemd.ModuleStreamV2)
# Confirm module and stream names are undefined
self.assertIsNone(doc.get_module_name())
self.assertIsNone(doc.get_stream_name())

# Read again with module/stream name overrides
doc = Modulemd.read_packager_string(
minimal_valid, "modulename-override", "streamname-override",
)
self.assertIsNotNone(doc)
self.assertIs(type(doc), Modulemd.ModuleStreamV2)
# Confirm module and stream names were set
self.assertEqual(doc.get_module_name(), "modulename-override")
self.assertEqual(doc.get_stream_name(), "streamname-override")


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

0 comments on commit 169136d

Please sign in to comment.