diff --git a/src/ansible_builder/ee_schema.py b/src/ansible_builder/ee_schema.py index 61db11c6..23f3e152 100644 --- a/src/ansible_builder/ee_schema.py +++ b/src/ansible_builder/ee_schema.py @@ -20,14 +20,24 @@ TYPE_DictOrStringOrListOfStrings = { "anyOf": [ - {"type": "object"}, - {"type": "string"}, { - "type": "array", - "items": { - "type": "string" - } - } + "type": "object", + "additionalProperties": False, + "properties": { + "roles": { + "type": "array", + "items": {"type": "string"}, + }, + "collections": { + "type": "array", + "items": {"type": "string"}, + }, + }, + }, + { + "type": "string", + "additionalProperties": False, + }, ] } diff --git a/test/unit/test_user_definition.py b/test/unit/test_user_definition.py index 8323b679..cda4d0a3 100644 --- a/test/unit/test_user_definition.py +++ b/test/unit/test_user_definition.py @@ -1,6 +1,10 @@ import os import pytest +from jsonschema import validate +from jsonschema.exceptions import ValidationError + +from ansible_builder.ee_schema import TYPE_DictOrStringOrListOfStrings from ansible_builder.exceptions import DefinitionError from ansible_builder.main import AnsibleBuilder from ansible_builder.user_definition import UserDefinition, ImageDescription @@ -299,3 +303,41 @@ def test_missing_orig_name_tag(self, key, image): with pytest.raises(DefinitionError) as error: ImageDescription(ee_section, key) assert f"Container image requires a tag: {image}" in str(error.value.args[0]) + + +class TestGalaxyDefinition: + + instances = [ + ("", True), + ("requirements.yml", True), + ("| requirements.yml", True), + ([], False), + ({"invalid_key": []}, False), + ({"roles": [], "collections2": []}, False), + ({"roles2": [], "collections": []}, False), + ({"roles": "", "collections": []}, False), + ({"roles": [], "collections": ""}, False), + ({"roles": [1], "collections": []}, False), + ({"roles": [], "collections": [1]}, False), + ({}, True), + ({"roles": []}, True), + ({"collections": []}, True), + ({"roles": [], "collections": []}, True), + ({"roles": ["openshift"], "collections": []}, True), + ({"roles": ["openshift"], "collections": ["community.okd"]}, True), + ({"roles": [], "collections": ["community.okd"]}, True), + ({"collections": ["community.okd"]}, True), + ({"roles": ["openshift"]}, True), + ] + + @pytest.mark.parametrize("test_input,expected", instances) + def test_galaxy_requirements(self, test_input, expected): + """ + Test Galaxy definition + """ + try: + validate(instance=test_input, schema=TYPE_DictOrStringOrListOfStrings) + test_passed = True + except ValidationError: + test_passed = False + assert test_passed == expected