-
Notifications
You must be signed in to change notification settings - Fork 5
Add test suite validation #11
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
Merged
t-persson
merged 2 commits into
eiffel-community:master
from
t-persson:test_suite_validation
Nov 3, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,181 @@ | ||
| # Copyright 2020 Axis Communications AB. | ||
| # | ||
| # For a full list of individual contributors, please see the commit history. | ||
| # | ||
| # 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. | ||
| """ETOS API suite validator module.""" | ||
| import logging | ||
| from uuid import UUID | ||
| from typing import Union, List | ||
| from pydantic import BaseModel, validator, ValidationError, constr, conlist | ||
| import requests | ||
|
|
||
|
|
||
| class Environment(BaseModel): | ||
| """ETOS suite definion 'ENVIRONMENT' constraint.""" | ||
|
|
||
| key: str | ||
| value: dict | ||
|
|
||
|
|
||
| class Command(BaseModel): | ||
| """ETOS suite definion 'COMMAND' constraint.""" | ||
|
|
||
| key: str | ||
| value: constr(min_length=1) | ||
|
|
||
|
|
||
| class Checkout(BaseModel): | ||
| """ETOS suite definion 'CHECKOUT' constraint.""" | ||
|
|
||
| key: str | ||
| value: conlist(str, min_items=1) | ||
|
|
||
|
|
||
| class Parameters(BaseModel): | ||
| """ETOS suite definion 'PARAMETERS' constraint.""" | ||
|
|
||
| key: str | ||
| value: dict | ||
|
|
||
|
|
||
| class Execute(BaseModel): | ||
| """ETOS suite definion 'EXECUTE' constraint.""" | ||
|
|
||
| key: str | ||
| value: List[str] | ||
|
|
||
|
|
||
| class TestRunner(BaseModel): | ||
| """ETOS suite definion 'TEST_RUNNER' constraint.""" | ||
|
|
||
| key: str | ||
| value: constr(min_length=1) | ||
|
|
||
|
|
||
| class TestCase(BaseModel): | ||
| """ETOS suite definion 'testCase' field.""" | ||
|
|
||
| id: str | ||
| tracker: str | ||
| url: str | ||
|
|
||
|
|
||
| class Constraint(BaseModel): | ||
| """ETOS suite definion 'constraints' field.""" | ||
|
|
||
| key: str | ||
| value: Union[str, list, dict] # pylint:disable=unsubscriptable-object | ||
|
|
||
|
|
||
| class Recipe(BaseModel): | ||
| """ETOS suite definion 'recipes' field.""" | ||
|
|
||
| constraints: List[Constraint] | ||
| id: UUID | ||
| testCase: TestCase | ||
|
|
||
| __constraint_models = { | ||
| "ENVIRONMENT": Environment, | ||
| "COMMAND": Command, | ||
| "CHECKOUT": Checkout, | ||
| "PARAMETERS": Parameters, | ||
| "EXECUTE": Execute, | ||
| "TEST_RUNNER": TestRunner, | ||
| } | ||
|
|
||
| @validator("constraints") | ||
| def validate_constraints( | ||
| cls, value | ||
| ): # Pydantic requires cls. pylint:disable=no-self-argument | ||
| """Validate the constraints fields for each recipe. | ||
|
|
||
| Validation is done manually because error messages from pydantic | ||
| are not clear enough when using a Union check on the models. | ||
| Pydantic does not check the number of unions either, which is something | ||
| that is required for ETOS. | ||
|
|
||
| :raises ValueError: if there are too many or too few constraints. | ||
| :raises TypeError: If an unknown constraint is detected. | ||
| :raises ValidationError: If constraint model does not validate. | ||
|
|
||
| :param value: The current constraint that is being validated. | ||
| :type value: Any | ||
| :return: Same as value, if validated. | ||
| :rtype: Any | ||
| """ | ||
| count = dict.fromkeys(cls.__constraint_models.keys(), 0) | ||
| for constraint in value: | ||
| model = cls.__constraint_models.get(constraint.key) | ||
| if model is None: | ||
| raise TypeError( | ||
| "Unknown key %r, valid keys: %r" | ||
| % (constraint.key, tuple(cls.__constraint_models.keys())) | ||
| ) | ||
| try: | ||
| model(**constraint.dict()) | ||
| except ValidationError as exception: | ||
| raise ValueError(str(exception)) from exception | ||
| count[constraint.key] += 1 | ||
| more_than_one = [key for key, number in count.items() if number > 1] | ||
| if more_than_one: | ||
| raise ValueError( | ||
| "Too many instances of keys %r. Only 1 allowed." % more_than_one | ||
| ) | ||
| missing = [key for key, number in count.items() if number == 0] | ||
| if missing: | ||
| raise ValueError( | ||
| "Too few instances of keys %r. At least 1 required." % missing | ||
| ) | ||
| return value | ||
|
|
||
|
|
||
| class Suite(BaseModel): | ||
| """ETOS base suite definition.""" | ||
|
|
||
| name: str | ||
| priority: int | ||
| recipes: List[Recipe] | ||
|
|
||
|
|
||
| class SuiteValidator: # pylint:disable=too-few-public-methods | ||
| """Validate ETOS suite definitions to make sure they are executable.""" | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| def __init__(self, params): | ||
| """Initialize validator. | ||
|
|
||
| :param params: Parameter instance. | ||
| :type params: :obj:`etos_api.lib.params.Params` | ||
| """ | ||
| self.params = params | ||
|
|
||
| def _download_suite(self): | ||
| """Attempt to download suite.""" | ||
| try: | ||
| suite = requests.get(self.params.test_suite) | ||
| suite.raise_for_status() | ||
| except Exception as exception: # pylint:disable=broad-except | ||
| raise AssertionError( | ||
| "Unable to download suite from %r" % self.params.test_suite | ||
| ) from exception | ||
| return suite.json() | ||
|
|
||
| def validate(self): | ||
| """Validate the ETOS suite definition. | ||
|
|
||
| :raises ValidationError: If the suite did not validate. | ||
| """ | ||
| downloaded_suite = self._download_suite() | ||
| assert Suite(**downloaded_suite) | ||
This file contains hidden or 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 hidden or 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,16 @@ | ||
| # Copyright 2020 Axis Communications AB. | ||
| # | ||
| # For a full list of individual contributors, please see the commit history. | ||
| # | ||
| # 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. | ||
| """Tests for the library module.""" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.