diff --git a/dsc/src/util.rs b/dsc/src/util.rs index 2689afb2..e9b9d002 100644 --- a/dsc/src/util.rs +++ b/dsc/src/util.rs @@ -118,7 +118,7 @@ pub fn add_fields_to_json(json: &str, fields_to_add: &HashMap) - pub fn add_type_name_to_json(json: String, type_name: String) -> String { let mut map:HashMap = HashMap::new(); - map.insert(String::from("type"), type_name); + map.insert(String::from("adapted_dsc_type"), type_name); let mut j = json; if j.is_empty() diff --git a/powershell-adapter/Tests/TestAdapter/testadapter.dsc.resource.json b/powershell-adapter/Tests/TestAdapter/testadapter.dsc.resource.json new file mode 100644 index 00000000..407dbd01 --- /dev/null +++ b/powershell-adapter/Tests/TestAdapter/testadapter.dsc.resource.json @@ -0,0 +1,86 @@ +{ + "$schema": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/bundled/resource/manifest.json", + "type": "Test/TestAdapter", + "version": "0.1.0", + "kind": "Adapter", + "description": "Resource adapter for testing.", + "tags": [ + "PowerShell" + ], + "adapter": { + "list": { + "executable": "pwsh", + "args": [ + "-NoLogo", + "-NonInteractive", + "-NoProfile", + "-Command", + "./testadapter.resource.ps1 List" + ] + }, + "config": "full" + }, + "get": { + "executable": "pwsh", + "args": [ + "-NoLogo", + "-NonInteractive", + "-NoProfile", + "-Command", + "$Input | ./testadapter.resource.ps1 Get" + ], + "input": "stdin" + }, + "set": { + "executable": "pwsh", + "args": [ + "-NoLogo", + "-NonInteractive", + "-NoProfile", + "-Command", + "$Input | ./testadapter.resource.ps1 Set" + ], + "input": "stdin", + "implementsPretest": true, + "return": "state" + }, + "test": { + "executable": "pwsh", + "args": [ + "-NoLogo", + "-NonInteractive", + "-NoProfile", + "-Command", + "$Input | ./testadapter.resource.ps1 Test" + ], + "input": "stdin", + "return": "state" + }, + "export": { + "executable": "pwsh", + "args": [ + "-NoLogo", + "-NonInteractive", + "-NoProfile", + "-Command", + "$Input | ./testadapter.resource.ps1 Export" + ], + "input": "stdin", + "return": "state" + }, + "validate": { + "executable": "pwsh", + "args": [ + "-NoLogo", + "-NonInteractive", + "-NoProfile", + "-Command", + "$Input | ./testadapter.resource.ps1 Validate" + ], + "input": "stdin" + }, + "exitCodes": { + "0": "Success", + "1": "Error" + } + } diff --git a/powershell-adapter/Tests/TestAdapter/testadapter.resource.ps1 b/powershell-adapter/Tests/TestAdapter/testadapter.resource.ps1 new file mode 100644 index 00000000..6db84584 --- /dev/null +++ b/powershell-adapter/Tests/TestAdapter/testadapter.resource.ps1 @@ -0,0 +1,64 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +[CmdletBinding()] +param( + [Parameter(Mandatory = $true, Position = 0, HelpMessage = 'Operation to perform. Choose from List, Get, Set, Test, Export, Validate.')] + [ValidateSet('List', 'Get', 'Set', 'Test', 'Export', 'Validate')] + [string]$Operation, + [Parameter(Mandatory = $false, Position = 1, ValueFromPipeline = $true, HelpMessage = 'Configuration or resource input in JSON format.')] + [string]$jsonInput = '@{}' +) + +function Write-DscTrace { + param( + [Parameter(Mandatory = $false)] + [ValidateSet('Error', 'Warn', 'Info', 'Debug', 'Trace')] + [string]$Operation = 'Debug', + [Parameter(Mandatory = $true, ValueFromPipeline = $true)] + [string]$Message + ) + + $trace = @{$Operation = $Message } | ConvertTo-Json -Compress + $host.ui.WriteErrorLine($trace) +} + +'Hello from TestAdapter.' | Write-DscTrace +'PSPath=' + $PSHome | Write-DscTrace +'PSModulePath=' + $env:PSModulePath | Write-DscTrace + +if ($jsonInput -ne '@{}') { + $inputobj = $jsonInput | ConvertFrom-Json +} + +$jsonInput | Write-DscTrace + +switch ($Operation) { + 'List' { + @{ + type = "Test/TestCase" + kind = 'Resource' + version = '1' + capabilities = @('Get', 'Set', 'Test', 'Export') + path = $PSScriptRoot + directory = Split-Path $PSScriptRoot + implementedAs = 'Adapter' + author = 'Test' + properties = @('TestCaseId', 'Input', 'Result') + requireAdapter = 'Test/TestAdapter' + description = 'TestCase resource' + } | ConvertTo-Json -Compress + } + { @('Get','Set','Test','Export') -contains $_ } { + + # TestCase 1 = 'Verify adapted_dsc_type field' + if (($inputobj.TestCaseId -eq 1 ) -or ($_ -eq 'Export')){ + $result = $inputobj.adapted_dsc_type -eq 'Test/TestCase' + $result = @{'TestCaseId'=1; 'Input'=''; result = $result } | ConvertTo-Json -Depth 10 -Compress + return $result + } + + } + 'Validate' { + @{ valid = $true } | ConvertTo-Json + } +} diff --git a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 index 74856cfc..5946e7aa 100644 --- a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 @@ -128,4 +128,69 @@ Describe 'PowerShell adapter resource tests' { $t = $resources | ? {$_.Type -eq 'TestClassResource/TestClassResource'} $t.properties | Should -Contain "BaseProperty" } + + It 'Verify adapted_dsc_type field in Get' { + $oldPath = $env:PATH + try { + $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' + $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath + + $r = '{TestCaseId: 1}'| dsc resource get -r 'Test/TestCase' + $LASTEXITCODE | Should -Be 0 + $resources = $r | ConvertFrom-Json + $resources.actualState.result | Should -Be $True + } + finally { + $env:PATH = $oldPath + } + } + + It 'Verify adapted_dsc_type field in Set' { + $oldPath = $env:PATH + try { + $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' + $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath + + $r = '{TestCaseId: 1}'| dsc resource set -r 'Test/TestCase' + $LASTEXITCODE | Should -Be 0 + $resources = $r | ConvertFrom-Json + $resources.beforeState.result | Should -Be $True + $resources.afterState.result | Should -Be $True + } + finally { + $env:PATH = $oldPath + } + } + + It 'Verify adapted_dsc_type field in Test' { + $oldPath = $env:PATH + try { + $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' + $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath + + $r = '{TestCaseId: 1}'| dsc resource test -r 'Test/TestCase' + $LASTEXITCODE | Should -Be 0 + $resources = $r | ConvertFrom-Json + $resources.actualState.result | Should -Be $True + } + finally { + $env:PATH = $oldPath + } + } + + It 'Verify adapted_dsc_type field in Export' { + $oldPath = $env:PATH + try { + $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' + $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath + + $r = dsc resource export -r 'Test/TestCase' + $LASTEXITCODE | Should -Be 0 + $resources = $r | ConvertFrom-Json + $resources.resources[0].properties.result | Should -Be $True + } + finally { + $env:PATH = $oldPath + } + } } diff --git a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 index 53d7dda4..4b30971e 100644 --- a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 @@ -379,8 +379,8 @@ function Get-DscResourceObject { } else { # mimic a config object with a single resource - $type = $inputObj.type - $inputObj.psobject.properties.Remove('type') + $type = $inputObj.adapted_dsc_type + $inputObj.psobject.properties.Remove('adapted_dsc_type') $desiredState += [dscResourceObject]@{ name = $adapterName type = $type @@ -408,9 +408,6 @@ function Invoke-DscOperation { $psVersion = $PSVersionTable.PSVersion.ToString() 'PowerShell version: ' + $psVersion | Write-DscTrace - $moduleVersion = Get-Module PSDesiredStateConfiguration | ForEach-Object Version - 'PSDesiredStateConfiguration module version: ' + $moduleVersion | Write-DscTrace - # get details from cache about the DSC resource, if it exists $cachedDscResourceInfo = $dscResourceCache | Where-Object Type -EQ $DesiredState.type | ForEach-Object DscResourceInfo diff --git a/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 index ee1e2b00..928c48a5 100644 --- a/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/win_psDscAdapter.psm1 @@ -276,8 +276,8 @@ function Get-DscResourceObject { } else { # mimic a config object with a single resource - $type = $inputObj.type - $inputObj.psobject.properties.Remove('type') + $type = $inputObj.adapted_dsc_type + $inputObj.psobject.properties.Remove('adapted_dsc_type') $desiredState += [dscResourceObject]@{ name = $adapterName type = $type diff --git a/wmi-adapter/wmi.resource.ps1 b/wmi-adapter/wmi.resource.ps1 index f51d628f..6eb2f88d 100644 --- a/wmi-adapter/wmi.resource.ps1 +++ b/wmi-adapter/wmi.resource.ps1 @@ -105,7 +105,7 @@ elseif ($Operation -eq 'Get') } else # we are processing an individual resource call { - $type_fields = $inputobj_pscustomobj.type -split "/" + $type_fields = $inputobj_pscustomobj.adapted_dsc_type -split "/" $wmi_namespace = $type_fields[0].Replace('.','\') $wmi_classname = $type_fields[1]