-
Notifications
You must be signed in to change notification settings - Fork 489
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1094 from p12tic/build-args-test
Add unit tests for image building
- Loading branch information
Showing
2 changed files
with
148 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
'.', | ||
], | ||
) |