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 8, 2024
1 parent 17d7a29 commit b0c62b8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 83 deletions.
27 changes: 0 additions & 27 deletions src/bci_build/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,30 +188,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>
"""
)
100 changes: 44 additions & 56 deletions tests/test_service.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from bci_build.containercrate import ContainerCrate
import pytest

from bci_build.os_version import OsVersion
from bci_build.package import BuildType
from bci_build.package import DevelopmentContainer
from bci_build.package import ParseVersion
from bci_build.package import Replacement
from bci_build.templates import SERVICE_TEMPLATE
from bci_build.package.obs_package import ObsPackage
from bci_build.service import Service


Expand Down Expand Up @@ -75,37 +75,33 @@ 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 @@ -122,31 +118,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 @@ -180,15 +172,11 @@ def test_service_with_multi_flavor_docker():
)
for flavor in ("flavor1", "flavor2")
]
ContainerCrate(containers)

assert (
SERVICE_TEMPLATE.render(
image=containers[0],
)
ObsPackage.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 b0c62b8

Please sign in to comment.