Python DSC Adapter and Test Resource implementation#1520
Open
shammu1 wants to merge 1 commit into
Open
Conversation
4c04457 to
6ccefc6
Compare
Collaborator
Author
|
@microsoft-github-policy-service agree company="Microsoft" |
3282631 to
e3b036f
Compare
e3b036f to
bb93ae9
Compare
Collaborator
Author
|
@copilot Review this |
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new Python DSC adapter implementation with test fixtures and Pester/Python tests, intended to let DSC invoke Python resource classes through adapted resource manifests.
Changes:
- Adds Python adapter manifest and
pyDscAdapterCLI/runtime modules. - Adds Python test resources, manifests, pyproject mapping, and unit/component/integration tests.
- Updates ignore rules and one PowerShell adapter test stderr redirection.
Reviewed changes
Copilot reviewed 21 out of 27 changed files in this pull request and generated 46 comments.
Show a summary per file
| File | Description |
|---|---|
.gitignore |
Adds Visual Studio and Python cache ignores. |
adapters/__init__.py |
Adds package marker. |
adapters/powershell/Tests/powershellgroup.resource.tests.ps1 |
Redirects stderr for one adapted-resource test. |
adapters/python/.project.data.json |
Defines Python adapter build project metadata. |
adapters/python/__init__.py |
Adds package marker. |
adapters/python/pythonadapter.dsc.resource.json |
Adds DSC adapter manifest for Python operations. |
adapters/python/pyDscAdapter/__init__.py |
Adds adapter package marker. |
adapters/python/pyDscAdapter/__main__.py |
Adds Python adapter entry point. |
adapters/python/pyDscAdapter/adapter.py |
Implements operation routing, class loading, logging, and profiling. |
adapters/python/pyDscAdapter/cli.py |
Adds command-line parser and stdin/input handling. |
adapters/python/pyDscAdapter/discovery.py |
Adds pyproject class-map parsing and dynamic file import. |
adapters/python/pyDscAdapter/dsc_logging.py |
Adds structured DSC-style logging. |
adapters/python/pyDscAdapter/utils.py |
Adds JSON parsing helper. |
adapters/python/tests/.project.data.json |
Defines test project copy metadata. |
adapters/python/tests/TEST_LAYERS.md |
Documents unit/component/integration test layers. |
adapters/python/tests/__init__.py |
Adds test package marker. |
adapters/python/tests/pythoncomponent.tests.ps1 |
Adds adapter subprocess component tests. |
adapters/python/tests/pythonintegration.tests.ps1 |
Adds DSC CLI integration tests. |
adapters/python/tests/pythonunit.tests.py |
Adds Python unittest coverage for adapter internals. |
adapters/python/tests/pyproject.toml |
Maps PythonTest resource types to fixture classes. |
adapters/python/tests/pythontest.dsc.manifests.json |
Adds adapted resource manifests for Python test resources. |
adapters/python/tests/src/.project.data.json |
Defines fixture resource copy metadata. |
adapters/python/tests/src/__init__.py |
Adds fixture package marker. |
adapters/python/tests/src/export.py |
Adds export-only fixture resource. |
adapters/python/tests/src/get.py |
Adds get-only fixture resource. |
adapters/python/tests/src/set.py |
Adds set fixture resource. |
adapters/python/tests/src/test.py |
Adds test-only fixture resource. |
Comment on lines
+2
to
+7
| "Name": "python-adapter", | ||
| "Kind": "Adapter", | ||
| "CopyFiles": { | ||
| "All": [ | ||
| "pyDscAdapter", | ||
| "pythonadapter.dsc.resource.json" |
| "Kind": "Adapter", | ||
| "CopyFiles": { | ||
| "All": [ | ||
| "pyDscAdapter", |
| "type": "Microsoft.DSC.Adapters/Python", | ||
| "version": "0.1.0", | ||
| "kind": "adapter", | ||
| "description": "Manages APT packages on Linux", |
| "mandatory": true | ||
| } | ||
| ], | ||
| "implementsPretest": false, |
Comment on lines
+187
to
+188
| self.logger.debug("List operation: returning empty resource list") | ||
| return 0, {"resources": []} |
Comment on lines
+14
to
+16
| from dsc_logging import setup_dsc_logging, operation_context | ||
| from utils import parse_json | ||
| from discovery import get_class_map_from_pyproject, import_class_from_file |
| from typing import Dict, Any, Optional | ||
|
|
||
| class ExportOnlyResource: | ||
| """A resource class that only implements the export operation for testing purposes.""" |
Comment on lines
+264
to
+269
| # Determine if filters are provided; otherwise export all (None) | ||
| as_obj = parse_json(json_input) | ||
| has_filters = any(k in as_obj for k in ("name", "version", "source", "dependencies")) | ||
| self.logger.debug(f"Export has_filters={has_filters}") | ||
| instance = self._instantiate_resource(cls, json_input, operation="export") if has_filters else None | ||
| data = cls.export(instance) |
| payload["resourceType"] = record.resourceType | ||
| # if hasattr(record, "method") and record.method: | ||
| # payload["method"] = record.method | ||
|
|
Comment on lines
+9
to
+10
| "pythonintegration.tests.ps1", | ||
| "pythonunit.tests.py" |
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.
PR Summary
The Python adapter (Microsoft.DSC.Adapters/Python) enables DSC v3 to dynamically execute Python resource classes. It acts as a bridge between the DSC framework and Python-based resource implementations, handling all DSC operations (get, set, test, export, list, validate).
Key Features
Dynamic Resource Discovery: Reads resource mappings from pyproject.toml under [tool.dsc.resources]
Operation Routing: Routes DSC operations to appropriate Python resource class methods
Structured Logging: Full trace/debug/info/error logging with optional profiling capabilities
State Management: Handles JSON serialization and properly emits state + diffs per DSC specification
Error Handling: Graceful error handling with proper exit codes and stderr logging
Core Components
adapter.py: Main ResourceAdapter class managing operation routing, class loading, and resource discovery
discovery.py: Parses pyproject.toml and dynamically imports resource classes
cli.py: Command-line interface handling arguments and stdin input
dsc_logging.py: Environment variable-based logging configuration
Test Coverage
Three comprehensive test layers:
Unit Tests (Python/unittest) — Tests adapter internals: initialization, manifest discovery, class resolution, operation routing
Component Tests (Pester) — Validates adapter subprocess behavior directly: output shapes, exit codes, error handling
Integration Tests (Pester) — End-to-end DSC CLI integration testing through the full DSC framework
PR Context
DSC already supports adapters for non-native resource ecosystems, and this PR adds a Python adapter so Python-based resources can participate in the same DSC execution model as built-in and PowerShell resources.
This adapter is needed to: