| 1 |
from kiwi.builder.template.container_import import BuilderTemplateSystemdUnit |
| 2 |
|
| 3 |
|
| 4 |
class TestBuilderTemplateSystemdUnit: |
| 5 |
def setup_method(self): |
| 6 |
self.template = BuilderTemplateSystemdUnit() |
| 7 |
|
| 8 |
def test_get_container_import_template_with_container_snap_service(self): |
| 9 |
container_files = ['/var/tmp/kiwi_containers/test-app_v1.0'] |
| 10 |
load_commands = [['/usr/bin/container-snap', 'load', '-i', '/var/tmp/kiwi_containers/test-app_v1.0']] |
| 11 |
after_services = ['container-snap.service'] |
| 12 |
|
| 13 |
template = self.template.get_container_import_template( |
| 14 |
container_files, load_commands, after_services |
| 15 |
) |
| 16 |
|
| 17 |
unit_content = template.substitute() |
| 18 |
|
| 19 |
expected_unit = """# kiwi generated unit file |
| 20 |
[Unit] |
| 21 |
Description=Import Local Container(s) |
| 22 |
ConditionPathExists=/var/tmp/kiwi_containers/test-app_v1.0 |
| 23 |
After=container-snap.service |
| 24 |
[Service] |
| 25 |
Type=oneshot |
| 26 |
ExecStart=/usr/bin/container-snap load -i /var/tmp/kiwi_containers/test-app_v1.0 |
| 27 |
ExecStartPost=/bin/rm -f /var/tmp/kiwi_containers/test-app_v1.0 |
| 28 |
[Install] |
| 29 |
WantedBy=multi-user.target |
| 30 |
""" |
| 31 |
|
| 32 |
assert unit_content == expected_unit |
| 33 |
|
| 34 |
def test_get_container_import_template_with_multiple_services(self): |
| 35 |
container_files = [ |
| 36 |
'/var/tmp/kiwi_containers/app1_latest', |
| 37 |
'/var/tmp/kiwi_containers/app2_v1.0' |
| 38 |
] |
| 39 |
load_commands = [ |
| 40 |
['/usr/bin/docker', 'load', '-i', '/var/tmp/kiwi_containers/app1_latest'], |
| 41 |
['/usr/bin/container-snap', 'load', '-i', '/var/tmp/kiwi_containers/app2_v1.0'] |
| 42 |
] |
| 43 |
after_services = ['docker.service', 'container-snap.service'] |
| 44 |
|
| 45 |
template = self.template.get_container_import_template( |
| 46 |
container_files, load_commands, after_services |
| 47 |
) |
| 48 |
|
| 49 |
unit_content = template.substitute() |
| 50 |
|
| 51 |
expected_unit = """# kiwi generated unit file |
| 52 |
[Unit] |
| 53 |
Description=Import Local Container(s) |
| 54 |
ConditionPathExists=/var/tmp/kiwi_containers/app1_latest |
| 55 |
ConditionPathExists=/var/tmp/kiwi_containers/app2_v1.0 |
| 56 |
After=docker.service container-snap.service |
| 57 |
[Service] |
| 58 |
Type=oneshot |
| 59 |
ExecStart=/usr/bin/docker load -i /var/tmp/kiwi_containers/app1_latest |
| 60 |
ExecStart=/usr/bin/container-snap load -i /var/tmp/kiwi_containers/app2_v1.0 |
| 61 |
ExecStartPost=/bin/rm -f /var/tmp/kiwi_containers/app1_latest |
| 62 |
ExecStartPost=/bin/rm -f /var/tmp/kiwi_containers/app2_v1.0 |
| 63 |
[Install] |
| 64 |
WantedBy=multi-user.target |
| 65 |
""" |
| 66 |
|
| 67 |
assert unit_content == expected_unit |
| 68 |
|
| 69 |
def test_get_container_import_template_no_after_services(self): |
| 70 |
container_files = ['/var/tmp/kiwi_containers/test-app_latest'] |
| 71 |
load_commands = [['/usr/bin/podman', 'load', '-i', '/var/tmp/kiwi_containers/test-app_latest']] |
| 72 |
after_services = [] |
| 73 |
|
| 74 |
template = self.template.get_container_import_template( |
| 75 |
container_files, load_commands, after_services |
| 76 |
) |
| 77 |
|
| 78 |
unit_content = template.substitute() |
| 79 |
|
| 80 |
expected_unit = """# kiwi generated unit file |
| 81 |
[Unit] |
| 82 |
Description=Import Local Container(s) |
| 83 |
ConditionPathExists=/var/tmp/kiwi_containers/test-app_latest |
| 84 |
[Service] |
| 85 |
Type=oneshot |
| 86 |
ExecStart=/usr/bin/podman load -i /var/tmp/kiwi_containers/test-app_latest |
| 87 |
ExecStartPost=/bin/rm -f /var/tmp/kiwi_containers/test-app_latest |
| 88 |
[Install] |
| 89 |
WantedBy=multi-user.target |
| 90 |
""" |
| 91 |
|
| 92 |
assert unit_content == expected_unit |
| 93 |
|
| 94 |
def test_get_container_import_template_empty_inputs(self): |
| 95 |
template = self.template.get_container_import_template([], [], []) |
| 96 |
|
| 97 |
unit_content = template.substitute() |
| 98 |
|
| 99 |
expected_unit = """# kiwi generated unit file |
| 100 |
[Unit] |
| 101 |
Description=Import Local Container(s) |
| 102 |
[Service] |
| 103 |
Type=oneshot |
| 104 |
[Install] |
| 105 |
WantedBy=multi-user.target |
| 106 |
""" |
| 107 |
|
| 108 |
assert unit_content == expected_unit |