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 separator to schema.yaml #827

Merged
merged 3 commits into from
Aug 28, 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
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
1 change: 1 addition & 0 deletions etc/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ image:
password: str(required=False)
shortname: str(required=False)
status: enum('active', 'deactivated')
separator: str(required=False)
tags: list(str())
versions: list(include('versions'))
visibility: enum('public', 'private', 'community', 'shared')
Expand Down
24 changes: 19 additions & 5 deletions openstack_image_manager/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,14 @@ def create_cli_args(
"project", "--share-type", help="Share - Type: 'project' or 'domain'"
),
check: bool = typer.Option(
True,
"--check/--no-check",
help="Check the local image definitions against the SCS Image Metadata Standard (and process the images)",
),
check_only: bool = typer.Option(
False,
"--check",
help="Check the local image definitions against the SCS Image Metadata Standard",
"--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,11 +214,14 @@ 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)
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 +1116,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
58 changes: 57 additions & 1 deletion 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 @@ -652,6 +653,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 @@ -662,18 +664,72 @@ def test_main(

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

self.sot.main()
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()

# 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.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_get_images.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()

@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