Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for image building #1094

Merged
merged 2 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -2429,18 +2429,7 @@ async def compose_push(compose, args):
await compose.podman.run([], "push", [cnt["image"]])


async def build_one(compose, args, cnt):
if "build" not in cnt:
return None
if getattr(args, "if_not_exists", None):
try:
img_id = await compose.podman.output(
[], "inspect", ["-t", "image", "-f", "{{.Id}}", cnt["image"]]
)
except subprocess.CalledProcessError:
img_id = None
if img_id:
return None
def container_to_build_args(compose, cnt, args, path_exists):
build_desc = cnt["build"]
if not hasattr(build_desc, "items"):
build_desc = {"context": build_desc}
Expand All @@ -2459,9 +2448,9 @@ async def build_one(compose, args, cnt):
]
for dockerfile in dockerfile_alts:
dockerfile = os.path.join(ctx, dockerfile)
if os.path.exists(dockerfile):
if path_exists(dockerfile):
break
if not os.path.exists(dockerfile):
if not path_exists(dockerfile):
raise OSError("Dockerfile not found in " + ctx)
build_args = ["-f", dockerfile, "-t", cnt["image"]]
if "platform" in cnt:
Expand Down Expand Up @@ -2495,6 +2484,23 @@ async def build_one(compose, args, cnt):
build_arg,
))
build_args.append(ctx)
return build_args


async def build_one(compose, args, cnt):
if "build" not in cnt:
return None
if getattr(args, "if_not_exists", None):
try:
img_id = await compose.podman.output(
[], "inspect", ["-t", "image", "-f", "{{.Id}}", cnt["image"]]
)
except subprocess.CalledProcessError:
img_id = None
if img_id:
return None

build_args = container_to_build_args(compose, cnt, args, os.path.exists)
status = await compose.podman.run([], "build", build_args)
return status

Expand Down
128 changes: 128 additions & 0 deletions tests/unit/test_container_to_build_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# SPDX-License-Identifier: GPL-2.0

import unittest
from unittest import mock

from podman_compose import container_to_build_args


def create_compose_mock(project_name='test_project_name'):
compose = mock.Mock()
compose.project_name = project_name
compose.dirname = 'test_dirname'
compose.container_names_by_service.get = mock.Mock(return_value=None)
compose.prefer_volume_over_mount = False
compose.default_net = None
compose.networks = {}
compose.x_podman = {}
return compose


def get_minimal_container():
return {
'name': 'project_name_service_name1',
'service_name': 'service_name',
'image': 'new-image',
'build': {},
}


def get_minimal_args():
args = mock.Mock()
args.build_arg = []
return args


class TestContainerToBuildArgs(unittest.TestCase):
def test_minimal(self):
c = create_compose_mock()

cnt = get_minimal_container()
args = get_minimal_args()

args = container_to_build_args(c, cnt, args, lambda path: True)
self.assertEqual(
args,
[
'-f',
'./Containerfile',
'-t',
'new-image',
'--no-cache',
'--pull-always',
'.',
],
)

def test_platform(self):
c = create_compose_mock()

cnt = get_minimal_container()
cnt['platform'] = 'linux/amd64'
args = get_minimal_args()

args = container_to_build_args(c, cnt, args, lambda path: True)
self.assertEqual(
args,
[
'-f',
'./Containerfile',
'-t',
'new-image',
'--platform',
'linux/amd64',
'--no-cache',
'--pull-always',
'.',
],
)

def test_tags(self):
c = create_compose_mock()

cnt = get_minimal_container()
cnt['build']['tags'] = ['some-tag1', 'some-tag2:2']
args = get_minimal_args()

args = container_to_build_args(c, cnt, args, lambda path: True)
self.assertEqual(
args,
[
'-f',
'./Containerfile',
'-t',
'new-image',
'-t',
'some-tag1',
'-t',
'some-tag2:2',
'--no-cache',
'--pull-always',
'.',
],
)

def test_labels(self):
c = create_compose_mock()

cnt = get_minimal_container()
cnt['build']['labels'] = ['some-label1', 'some-label2.2']
args = get_minimal_args()

args = container_to_build_args(c, cnt, args, lambda path: True)
self.assertEqual(
args,
[
'-f',
'./Containerfile',
'-t',
'new-image',
'--label',
'some-label1',
'--label',
'some-label2.2',
'--no-cache',
'--pull-always',
'.',
],
)
Loading