Skip to content

Commit

Permalink
Enable schema checking by default
Browse files Browse the repository at this point in the history
Signed-off-by: Gondermann <[email protected]>
  • Loading branch information
gndrmnn authored and berendt committed Aug 28, 2024
1 parent 38cb60f commit abb571f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
14 changes: 10 additions & 4 deletions openstack_image_manager/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
):
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down
23 changes: 19 additions & 4 deletions test/unit/test_manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)))
Expand Down

0 comments on commit abb571f

Please sign in to comment.