-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1199 from shreyabiradar07/test_list_metadata
Add testcases for GET /dsmetadata API
- Loading branch information
Showing
12 changed files
with
893 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
tests/scripts/helpers/list_metadata_json_cluster_name_without_verbose_schema.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
list_metadata_json_cluster_name_without_verbose_schema = { | ||
"type": "object", | ||
"properties": { | ||
"datasources": { | ||
"type": "object", | ||
"patternProperties": { | ||
"^[a-zA-Z0-9_-]+$": { | ||
"type": "object", | ||
"properties": { | ||
"datasource_name": { | ||
"type": "string", | ||
"pattern": "^[a-zA-Z0-9_-]+$" | ||
}, | ||
"clusters": { | ||
"type": "object", | ||
"patternProperties": { | ||
"^[a-zA-Z0-9_-]+$": { | ||
"type": "object", | ||
"properties": { | ||
"cluster_name": { | ||
"type": "string", | ||
"pattern": "^[a-zA-Z0-9_-]+$" | ||
}, | ||
"namespaces": { | ||
"type": "object", | ||
"patternProperties": { | ||
"^[a-zA-Z0-9_-]+$": { | ||
"type": "object", | ||
"properties": { | ||
"namespace": { | ||
"type": "string", | ||
"pattern": "^[a-zA-Z0-9_-]+$" | ||
} | ||
}, | ||
"required": ["namespace"] | ||
} | ||
} | ||
} | ||
}, | ||
"required": ["cluster_name", "namespaces"] | ||
} | ||
} | ||
} | ||
}, | ||
"required": ["datasource_name", "clusters"] | ||
} | ||
} | ||
} | ||
}, | ||
"required": ["datasources"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
list_metadata_json_schema = { | ||
"type": "object", | ||
"properties": { | ||
"datasources": { | ||
"type": "object", | ||
"patternProperties": { | ||
"^[a-zA-Z0-9_-]+$": { | ||
"type": "object", | ||
"properties": { | ||
"datasource_name": { | ||
"type": "string", | ||
"pattern": "^[a-zA-Z0-9_-]+$" | ||
}, | ||
"clusters": { | ||
"type": "object", | ||
"patternProperties": { | ||
"^[a-zA-Z0-9_-]+$": { | ||
"type": "object", | ||
"properties": { | ||
"cluster_name": { | ||
"type": "string", | ||
"pattern": "^[a-zA-Z0-9_-]+$" | ||
} | ||
}, | ||
"required": ["cluster_name"] | ||
} | ||
} | ||
} | ||
}, | ||
"required": ["datasource_name", "clusters"] | ||
} | ||
} | ||
} | ||
}, | ||
"required": ["datasources"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
Copyright (c) 2024, 2024 Red Hat, IBM Corporation and others. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
""" | ||
import json | ||
import jsonschema | ||
from jsonschema import FormatChecker | ||
from jsonschema.exceptions import ValidationError | ||
from helpers.import_metadata_json_schema import import_metadata_json_schema | ||
|
||
JSON_NULL_VALUES = ("is not of type 'string'", "is not of type 'integer'", "is not of type 'number'") | ||
VALUE_MISSING = " cannot be empty or null!" | ||
|
||
def validate_list_metadata_json(list_metadata_json, json_schema): | ||
errorMsg = "" | ||
try: | ||
# create a validator with the format checker | ||
print("Validating json against the json schema...") | ||
validator = jsonschema.Draft7Validator(json_schema, format_checker=FormatChecker()) | ||
|
||
# validate the JSON data against the schema | ||
errors = "" | ||
errors = list(validator.iter_errors(list_metadata_json)) | ||
print("Validating json against the json schema...done") | ||
errorMsg = validate_list_metadata_json_values(list_metadata_json) | ||
|
||
if errors: | ||
custom_err = ValidationError(errorMsg) | ||
errors.append(custom_err) | ||
return errors | ||
else: | ||
return errorMsg | ||
except ValidationError as err: | ||
print("Received a VaidationError") | ||
|
||
# Check if the exception is due to empty or null required parameters and prepare the response accordingly | ||
if any(word in err.message for word in JSON_NULL_VALUES): | ||
errorMsg = "Parameters" + VALUE_MISSING | ||
return errorMsg | ||
# Modify the error response in case of additional properties error | ||
elif str(err.message).__contains__('('): | ||
errorMsg = str(err.message).split('(') | ||
return errorMsg[0] | ||
else: | ||
return err.message | ||
|
||
def validate_list_metadata_json_values(metadata): | ||
validationErrorMsg = "" | ||
|
||
for key in metadata.keys(): | ||
|
||
# Check if any of the key is empty or null | ||
if not (str(metadata[key]) and str(metadata[key]).strip()): | ||
validationErrorMsg = ",".join([validationErrorMsg, "Parameters" + VALUE_MISSING]) | ||
|
||
return validationErrorMsg.lstrip(',') | ||
|
88 changes: 88 additions & 0 deletions
88
tests/scripts/helpers/list_metadata_json_verbose_true_schema.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
list_metadata_json_verbose_true_schema = { | ||
"type": "object", | ||
"properties": { | ||
"datasources": { | ||
"type": "object", | ||
"patternProperties": { | ||
"^[a-zA-Z0-9_-]+$": { | ||
"type": "object", | ||
"properties": { | ||
"datasource_name": { | ||
"type": "string", | ||
"pattern": "^[a-zA-Z0-9_-]+$" | ||
}, | ||
"clusters": { | ||
"type": "object", | ||
"patternProperties": { | ||
"^[a-zA-Z0-9_-]+$": { | ||
"type": "object", | ||
"properties": { | ||
"cluster_name": { | ||
"type": "string", | ||
"pattern": "^[a-zA-Z0-9_-]+$" | ||
}, | ||
"namespaces": { | ||
"type": "object", | ||
"patternProperties": { | ||
"^[a-zA-Z0-9_-]+$": { | ||
"type": "object", | ||
"properties": { | ||
"namespace": { | ||
"type": "string", | ||
"pattern": "^[a-zA-Z0-9_-]+$" | ||
}, | ||
"workloads": { | ||
"type": "object", | ||
"patternProperties": { | ||
"^[a-zA-Z0-9_-]+$": { | ||
"type": "object", | ||
"properties": { | ||
"workload_name": { | ||
"type": "string", | ||
"pattern": "^[a-zA-Z0-9_-]+$" | ||
}, | ||
"workload_type": { | ||
"type": "string" | ||
}, | ||
"containers": { | ||
"type": "object", | ||
"patternProperties": { | ||
"^[a-zA-Z0-9_-]+$": { | ||
"type": "object", | ||
"properties": { | ||
"container_name": { | ||
"type": "string", | ||
"pattern": "^[a-zA-Z0-9_-]+$" | ||
}, | ||
"container_image_name": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": ["container_name", | ||
"container_image_name"] | ||
} | ||
} | ||
} | ||
}, | ||
"required": ["workload_name", "workload_type", "containers"] | ||
} | ||
} | ||
} | ||
}, | ||
"required": ["namespace"] | ||
} | ||
} | ||
} | ||
}, | ||
"required": ["cluster_name", "namespaces"] | ||
} | ||
} | ||
} | ||
}, | ||
"required": ["datasource_name", "clusters"] | ||
} | ||
} | ||
} | ||
}, | ||
"required": ["datasources"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.