NAS-140296 / 27.0.0-BETA.1 / Fix mock infrastructure to coerce dict results to Pydantic models for generic services#18457
Open
NAS-140296 / 27.0.0-BETA.1 / Fix mock infrastructure to coerce dict results to Pydantic models for generic services#18457
Conversation
Contributor
… generic services This commit fixes an issue where the mock infrastructure (test.set_mock) returns plain dicts for methods that internally return Pydantic models. Generic services (those with Config.generic = True) like DockerService return Pydantic model instances from their methods (e.g. docker.config returns a DockerEntry, docker.status returns a DockerStatusInfo). When internal callers use attribute access (e.g. .pool) on the result, mocks returning plain dicts would fail with "'dict' object has no attribute 'pool'". The fix detects at mock registration time whether the mocked method belongs to a generic service and returns any Pydantic model. If so, the mock's dict results are automatically wrapped via model_construct() before being returned to callers. Non-generic services and primitive-returning methods are unaffected.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This commit fixes an issue where the mock infrastructure (test.set_mock) returns plain dicts for methods that internally return Pydantic models. Generic services (those with Config.generic = True) like DockerService return Pydantic model instances from their methods (e.g. docker.config returns a DockerEntry, docker.status returns a DockerStatusInfo). When internal callers use attribute access (e.g. .pool) on the result, mocks returning plain dicts would fail with "'dict' object has no attribute 'pool'".
The fix detects at mock registration time whether the mocked method belongs to a generic service and returns any Pydantic model. If so, the mock's dict results are automatically wrapped via model_construct() before being returned to callers. Non-generic services and primitive-returning methods are unaffected.
STIG tests: http://jenkins.eng.ixsystems.net:8080/job/tests/job/stig_tests/1812/ ( failing test was http://jenkins.eng.ixsystems.net:8080/job/tests/job/stig_tests/1811/testReport/junit/stig/test_01_stig/test_docker_apps_enabled_fail/ which has been fixed)
API tests: http://jenkins.eng.ixsystems.net:8080/job/tests/job/api_tests/8072/