Skip to content

Commit

Permalink
Add option to only check the image schema and exit
Browse files Browse the repository at this point in the history
Signed-off-by: Gondermann <gondermann@b1-systems.de>
  • Loading branch information
gndrmnn authored and berendt committed Aug 28, 2024
1 parent abb571f commit 0944a3a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/validate-configuration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ jobs:
with:
python-version: '3.x'
- run: pip3 install tox
- run: tox -- --check
- run: tox -- --check-only
env:
OS_CLIENT_CONFIG_FILE: .github/clouds.yml
12 changes: 10 additions & 2 deletions openstack_image_manager/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,12 @@ def create_cli_args(
check: bool = typer.Option(
True,
"--check/--no-check",
help="Check the local image definitions against the SCS Image Metadata Standard",
help="Check the local image definitions against the SCS Image Metadata Standard (and process the images)",
),
check_only: bool = typer.Option(
False,
"--check-only",
help="Quit after checking the image definitions against the SCS Image Metadata Standard",
),
):
self.CONF = Munch.fromDict(locals())
Expand Down Expand Up @@ -209,9 +214,12 @@ def main(self) -> None:
)

# check local image definitions with yamale
if self.CONF.check:
if self.CONF.check or self.CONF.check_only:
self.validate_yaml_schema()

if self.CONF.check_only:
return

# share image (previously share.py)
if self.CONF.share_image:
self.create_connection()
Expand Down
61 changes: 51 additions & 10 deletions test/unit/test_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ def setUp(self):
share_type="project",
filter="",
check=False,
check_only=False,
hypervisor=None,
)

Expand Down Expand Up @@ -663,7 +664,6 @@ def test_main(

# test with check = True
self.sot.CONF.check = True

self.sot.CONF.dry_run = False

self.sot.main()
Expand All @@ -675,16 +675,57 @@ def test_main(
mock_share_image.assert_not_called()
mock_unshare_image.assert_not_called()

@mock.patch("openstack_image_manager.main.ImageManager.read_image_files")
@mock.patch("openstack_image_manager.main.openstack.connect")
def test_validate_images(
self,
mock_connect,
mock_read_image_files,
):
"""Validate the image definitions in this repo against the schema"""
# reset
mock_connect.reset_mock()
mock_read_image_files.reset_mock()
mock_get_images.reset_mock()
mock_process_images.reset_mock()
mock_manage_outdated.reset_mock()
mock_validate_yaml.reset_mock()
mock_share_image.reset_mock()
mock_unshare_image.reset_mock()

# test with check_only = True
self.sot.CONF.check = False
self.sot.CONF.check_only = True
self.sot.CONF.dry_run = False

self.sot.main()
mock_connect.assert_not_called()
mock_read_image_files.assert_not_called()
mock_process_images.assert_not_called()
mock_manage_outdated.assert_not_called()
mock_validate_yaml.assert_called_once()
mock_share_image.assert_not_called()
mock_unshare_image.assert_not_called()

# reset
mock_connect.reset_mock()
mock_read_image_files.reset_mock()
mock_get_images.reset_mock()
mock_process_images.reset_mock()
mock_manage_outdated.reset_mock()
mock_validate_yaml.reset_mock()
mock_share_image.reset_mock()
mock_unshare_image.reset_mock()

# test with check_only = True and check = True
self.sot.CONF.check = True
self.sot.CONF.dry_run = True
self.sot.CONF.check_only = True
self.sot.CONF.dry_run = False

self.sot.main()
mock_connect.assert_not_called()
mock_read_image_files.assert_not_called()
mock_process_images.assert_not_called()
mock_manage_outdated.assert_not_called()
mock_validate_yaml.assert_called_once()
mock_share_image.assert_not_called()
mock_unshare_image.assert_not_called()

def test_validate_images(self):
"""Validate the image definitions in this repo against the schema"""
self.sot.CONF.check_only = True

# When image validation fails, we sys.exit and fail the test
self.sot.main()
Expand Down

0 comments on commit 0944a3a

Please sign in to comment.