Skip to content

Commit

Permalink
Stop using SERVICE_TEMPLATE and use ObsPackage._service_file_contents…
Browse files Browse the repository at this point in the history
… instead
  • Loading branch information
dcermak committed Oct 14, 2024
1 parent dd16f6b commit 8216c97
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 84 deletions.
27 changes: 0 additions & 27 deletions src/bci_build/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,30 +190,3 @@
</image>
"""
)

#: Jinja2 template used to generate :file:`_service`.
SERVICE_TEMPLATE = jinja2.Template(
"""<services>
<service mode="buildtime" name="{{ image.build_recipe_type }}_label_helper"/>
<service mode="buildtime" name="kiwi_metainfo_helper"/>
{%- set all_build_flavors = [""] %}
{%- if image.crate and image.build_flavor %}
{%- set all_build_flavors = image.crate.all_build_flavors(image) %}
{%- endif %}
{%- for flavor in all_build_flavors %}
{%- for replacement in image.replacements_via_service %}
<service name="replace_using_package_version" mode="buildtime">
<param name="file">
{%- if replacement.file_name != None %}{{replacement.file_name}}
{%- elif (image.build_recipe_type|string) == "docker" %}{% if flavor %}Dockerfile.{{ flavor }}{% else %}Dockerfile{% endif %}
{%- else %}{{ image.package_name }}.kiwi
{%- endif %}</param>
<param name="regex">{{ replacement.regex_in_build_description }}</param>
<param name="package">{{ replacement.package_name }}</param>{% if replacement.parse_version %}
<param name="parse-version">{{ replacement.parse_version }}</param>{% endif %}
</service>
{%- endfor -%}
{% endfor %}
</services>
"""
)
103 changes: 46 additions & 57 deletions tests/test_service.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import pytest

from bci_build.container_attributes import BuildType
from bci_build.containercrate import ContainerCrate
from bci_build.os_version import OsVersion
from bci_build.package import DevelopmentContainer
from bci_build.package import ParseVersion
from bci_build.package import Replacement
from bci_build.package.obs_package import MultiBuildObsPackage
from bci_build.service import Service
from bci_build.templates import SERVICE_TEMPLATE


def test_service_without_params_as_xml():
Expand All @@ -26,7 +25,7 @@ def test_service_with_params_as_xml():
[
# bare bone example
(
Replacement(regex := "%ver%", pkg := "pkgFoo"),
Replacement(regex := "%%ver%%", pkg := "pkgFoo"),
"Dockerfile",
Service(
name=(name := "replace_using_package_version"),
Expand Down Expand Up @@ -76,37 +75,35 @@ def test_replacement_to_service(

def test_service_without_replacement_kiwi():
assert (
SERVICE_TEMPLATE.render(
image=DevelopmentContainer(**_BASE_KWARGS, build_recipe_type=BuildType.KIWI)
)
DevelopmentContainer(
**_BASE_KWARGS, build_recipe_type=BuildType.KIWI
)._service_file_contents
== """<services>
<service mode="buildtime" name="kiwi_label_helper"/>
<service mode="buildtime" name="kiwi_metainfo_helper"/>
<service name="kiwi_label_helper" mode="buildtime" />
<service name="kiwi_metainfo_helper" mode="buildtime" />
</services>"""
)


def test_service_with_replacement_kiwi():
assert (
SERVICE_TEMPLATE.render(
image=DevelopmentContainer(
**_BASE_KWARGS,
build_recipe_type=BuildType.KIWI,
replacements_via_service=[
Replacement(
regex_in_build_description="%%re%%", package_name="coreutils"
),
Replacement(
regex_in_build_description="%%re%%",
package_name="coreutils",
file_name="replacementfile",
),
],
)
)
DevelopmentContainer(
**_BASE_KWARGS,
build_recipe_type=BuildType.KIWI,
replacements_via_service=[
Replacement(
regex_in_build_description="%%re%%", package_name="coreutils"
),
Replacement(
regex_in_build_description="%%re%%",
package_name="coreutils",
file_name="replacementfile",
),
],
)._service_file_contents
== """<services>
<service mode="buildtime" name="kiwi_label_helper"/>
<service mode="buildtime" name="kiwi_metainfo_helper"/>
<service name="kiwi_label_helper" mode="buildtime" />
<service name="kiwi_metainfo_helper" mode="buildtime" />
<service name="replace_using_package_version" mode="buildtime">
<param name="file">test-image.kiwi</param>
<param name="regex">%%re%%</param>
Expand All @@ -123,31 +120,27 @@ def test_service_with_replacement_kiwi():

def test_service_with_replacement_docker():
assert (
SERVICE_TEMPLATE.render(
image=DevelopmentContainer(
**_BASE_KWARGS,
build_recipe_type=BuildType.DOCKER,
replacements_via_service=[
Replacement(
regex_in_build_description="%%my_ver%%", package_name="sh"
),
Replacement(
regex_in_build_description="%%minor_ver%%",
package_name="filesystem",
parse_version=ParseVersion.MINOR,
),
Replacement(
regex_in_build_description="%%minor_ver%%",
file_name="replacementfile",
package_name="filesystem",
parse_version=ParseVersion.MINOR,
),
],
)
)
DevelopmentContainer(
**_BASE_KWARGS,
build_recipe_type=BuildType.DOCKER,
replacements_via_service=[
Replacement(regex_in_build_description="%%my_ver%%", package_name="sh"),
Replacement(
regex_in_build_description="%%minor_ver%%",
package_name="filesystem",
parse_version=ParseVersion.MINOR,
),
Replacement(
regex_in_build_description="%%minor_ver%%",
file_name="replacementfile",
package_name="filesystem",
parse_version=ParseVersion.MINOR,
),
],
)._service_file_contents
== """<services>
<service mode="buildtime" name="docker_label_helper"/>
<service mode="buildtime" name="kiwi_metainfo_helper"/>
<service name="docker_label_helper" mode="buildtime" />
<service name="kiwi_metainfo_helper" mode="buildtime" />
<service name="replace_using_package_version" mode="buildtime">
<param name="file">Dockerfile</param>
<param name="regex">%%my_ver%%</param>
Expand Down Expand Up @@ -181,15 +174,11 @@ def test_service_with_multi_flavor_docker():
)
for flavor in ("flavor1", "flavor2")
]
ContainerCrate(containers)

assert (
SERVICE_TEMPLATE.render(
image=containers[0],
)
MultiBuildObsPackage.from_bcis(containers)._service_file_contents
== """<services>
<service mode="buildtime" name="docker_label_helper"/>
<service mode="buildtime" name="kiwi_metainfo_helper"/>
<service name="docker_label_helper" mode="buildtime" />
<service name="kiwi_metainfo_helper" mode="buildtime" />
<service name="replace_using_package_version" mode="buildtime">
<param name="file">Dockerfile.flavor1</param>
<param name="regex">%%my_ver%%</param>
Expand Down

0 comments on commit 8216c97

Please sign in to comment.