From abb571f48d0c5a39188b6b598a8339cde11fa5ff Mon Sep 17 00:00:00 2001 From: Gondermann Date: Wed, 28 Aug 2024 11:26:20 +0200 Subject: [PATCH] Enable schema checking by default Signed-off-by: Gondermann --- openstack_image_manager/main.py | 14 ++++++++++---- test/unit/test_manage.py | 23 +++++++++++++++++++---- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/openstack_image_manager/main.py b/openstack_image_manager/main.py index 2acfd011..aa2911fa 100644 --- a/openstack_image_manager/main.py +++ b/openstack_image_manager/main.py @@ -92,8 +92,8 @@ def create_cli_args( "project", "--share-type", help="Share - Type: 'project' or 'domain'" ), check: bool = typer.Option( - False, - "--check", + True, + "--check/--no-check", help="Check the local image definitions against the SCS Image Metadata Standard", ), ): @@ -213,7 +213,7 @@ def main(self) -> None: self.validate_yaml_schema() # share image (previously share.py) - elif self.CONF.share_image: + if self.CONF.share_image: self.create_connection() image = self.conn.get_image(self.CONF.share_image) @@ -1108,23 +1108,29 @@ def validate_yaml_schema(self): """Validate all image.yaml files against the SCS Metadata spec""" schema = yamale.make_schema("etc/schema.yaml") try: + validation_error_log = [] for file in os.listdir(self.CONF.images): try: data = yamale.make_data(self.CONF.images + file) yamale.validate(schema, data) except YamaleError as e: - self.exit_with_error = True for result in e.results: logger.error( f"Error validating data '{result.data}' with '{result.schema}'" ) for error in result.errors: logger.error(f"\t{error}") + validation_error_log.append((file, error)) else: logger.debug(f"Image file {file} is valid") except FileNotFoundError: logger.error(f"Invalid path '{self.CONF.images}'") + if len(validation_error_log) > 0: + sys.exit( + f"Image definition validation failed with these error(s): {validation_error_log}" + ) + def share_image_with_project(self, image, project): member = self.conn.image.find_member(project.id, image.id) diff --git a/test/unit/test_manage.py b/test/unit/test_manage.py index 73984faa..4211a9d2 100644 --- a/test/unit/test_manage.py +++ b/test/unit/test_manage.py @@ -652,6 +652,7 @@ def test_main( 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() @@ -666,14 +667,28 @@ def test_main( self.sot.CONF.dry_run = False self.sot.main() - mock_read_image_files.assert_not_called() - mock_get_images.assert_not_called() - mock_process_images.assert_not_called() - mock_manage_outdated.assert_not_called() + mock_connect.assert_called_once_with(cloud=self.sot.CONF.cloud) + mock_read_image_files.assert_called_once() + mock_process_images.assert_called_once_with([self.fake_image_dict]) + mock_manage_outdated.assert_called_once_with(set()) mock_validate_yaml.assert_called_once() 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""" + self.sot.CONF.check = True + self.sot.CONF.dry_run = True + + # When image validation fails, we sys.exit and fail the test + self.sot.main() + @mock.patch("openstack_image_manager.main.os.path.isfile") @mock.patch("openstack_image_manager.main.os.listdir") @mock.patch("builtins.open", mock.mock_open(read_data=str(FAKE_YML)))