Skip to content
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

[WIP] WMI adapter improvements #520

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
105 changes: 79 additions & 26 deletions wmi-adapter/Tests/wmi.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Describe 'WMI adapter resource tests' {
BeforeAll {
if ($IsWindows)
{
$OldPSModulePath = $env:PSModulePath
$OldPSModulePath = $env:PSModulePath
$env:PSModulePath += ";" + $PSScriptRoot

$configPath = Join-path $PSScriptRoot "test_wmi_config.dsc.yaml"
Expand All @@ -19,39 +19,92 @@ Describe 'WMI adapter resource tests' {
}
}

It 'List shows WMI resources' -Skip:(!$IsWindows){
Context 'List WMI resources' {
It 'List shows WMI resources' -Skip:(!$IsWindows) {

$r = dsc resource list *OperatingSystem* -a Microsoft.Windows/WMI
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.Count | Should -BeGreaterOrEqual 1
$r = dsc resource list *OperatingSystem* -a Microsoft.Windows/WMI
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.Count | Should -BeGreaterOrEqual 1
}
}

It 'Get works on an individual WMI resource' -Skip:(!$IsWindows){
Context 'Get WMI resources' {
It 'Get works on an individual WMI resource' -Skip:(!$IsWindows) {

$r = dsc resource get -r root.cimv2/Win32_OperatingSystem
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.CreationClassName | Should -Be "Win32_OperatingSystem"
$r = dsc resource get -r root.cimv2/Win32_OperatingSystem
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.actualState.result.type | Should -BeLike "*Win32_OperatingSystem"
}

It 'Get works on a config with WMI resources' -Skip:(!$IsWindows) {

$r = Get-Content -Raw $configPath | dsc config get
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.results.result.actualstate.result[0].properties.LastBootUpTime | Should -Not -BeNull
$res.results.result.actualstate.result[0].properties.Caption | Should -Not -BeNull
$res.results.result.actualstate.result[0].properties.NumberOfProcesses | Should -Not -BeNull
}

It 'Example config works' -Skip:(!$IsWindows) {
$configPath = Join-Path $PSScriptRoot '..\..\dsc\examples\wmi.dsc.yaml'
$r = dsc config get -p $configPath
$LASTEXITCODE | Should -Be 0
$r | Should -Not -BeNullOrEmpty
$res = $r | ConvertFrom-Json
$res.results.result.actualstate.result[0].properties.Model | Should -Not -BeNullOrEmpty
$res.results.result.actualstate.result[0].properties.Description | Should -Not -BeNullOrEmpty
}
}

It 'Get works on a config with WMI resources' -Skip:(!$IsWindows){
# TODO: work on set test configs
Context "Set WMI resources" {
It 'Set a resource' -Skip:(!$IsWindows) {
$inputs = @{
adapted_dsc_type = "root.cimv2/Win32_Process"
properties = @{
MethodName = 'Create'
CommandLine = 'powershell.exe'
}
}
# get the start of processes
$ref = Get-Process

# run the creation of process
$r = ($inputs | ConvertTo-Json -Compress) | dsc resource set -r root.cimv2/Win32_Process

$r = Get-Content -Raw $configPath | dsc config get
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.results[0].result.actualState[0].LastBootUpTime | Should -Not -BeNull
$res.results[0].result.actualState[1].BiosCharacteristics | Should -Not -BeNull
$res.results[0].result.actualState[2].NumberOfLogicalProcessors | Should -Not -BeNull
# handle the output as we do not have a filter yet on the get method
$diff = Get-Process

$comparison = (Compare-Object -ReferenceObject $ref -DifferenceObject $diff | Where-Object { $_.SideIndicator -eq '=>' })
$process = foreach ($c in $comparison)
{
if ($c.InputObject.Path -like "*$($inputs.properties.CommandLine)*")
{
$c.InputObject
}
}
$res = $r | ConvertFrom-Json
$res.afterState.result | Should -Not -BeNull
$LASTEXITCODE | Should -Be 0
$process | Should -Not -BeNullOrEmpty
$process.Path | Should -BeLike "*powershell.exe*"
}
AfterAll {
$process = Get-Process -Name "powershell" -ErrorAction SilentlyContinue | Sort-Object StartTime -Descending -Top 1
Stop-Process $process
}
}

It 'Example config works' -Skip:(!$IsWindows) {
$configPath = Join-Path $PSScriptRoot '..\..\dsc\examples\wmi.dsc.yaml'
$r = dsc config get -p $configPath
$LASTEXITCODE | Should -Be 0
$r | Should -Not -BeNullOrEmpty
$res = $r | ConvertFrom-Json
$res.results[0].result.actualState[0].Model | Should -Not -BeNullOrEmpty
$res.results[0].result.actualState[1].Description | Should -Not -BeNullOrEmpty
Context "Export WMI resources" {
It 'Exports all resources' -Skip:(!$IsWindows) {
$r = dsc resource export -r root.cimv2/Win32_Process
$LASTEXITCODE | Should -Be 0
$res = $r | ConvertFrom-Json
$res.resources.properties.result.properties.value.count | Should -BeGreaterThan 1
$res.resources.properties.result.properties.value[0].CreationClassName | Should -Be 'Win32_Process'
}
}
}
4 changes: 3 additions & 1 deletion wmi-adapter/copy_files.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
wmi.resource.ps1
wmi.dsc.resource.json
wmi.dsc.resource.json
wmiAdapter.psd1
wmiAdapter.psm1
98 changes: 59 additions & 39 deletions wmi-adapter/wmi.dsc.resource.json
Original file line number Diff line number Diff line change
@@ -1,48 +1,68 @@
{
"$schema": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/bundled/resource/manifest.json",
"type": "Microsoft.Windows/WMI",
"version": "0.1.0",
"kind": "Adapter",
"description": "Resource adapter to WMI resources.",
"tags": [
"PowerShell"
],
"adapter": {
"list": {
"executable": "powershell",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-Command",
"./wmi.resource.ps1 List"
]
},
"config": "full"
},
"get": {
"$schema": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/bundled/resource/manifest.json",
"type": "Microsoft.Windows/WMI",
"version": "0.1.0",
"kind": "Adapter",
"description": "Resource adapter to WMI resources.",
"tags": ["PowerShell"],
"adapter": {
"list": {
"executable": "powershell",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-Command",
"$Input | ./wmi.resource.ps1 Get"
],
"input": "stdin"
"./wmi.resource.ps1 List"
]
},
"validate": {
"executable": "powershell",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-Command",
"$Input | ./wmi.resource.ps1 Validate"
]
},
"exitCodes": {
"0": "Success",
"1": "Error"
}
"config": "full"
},
"get": {
"executable": "powershell",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-Command",
"$Input | ./wmi.resource.ps1 Get"
],
"input": "stdin"
},
"set": {
"executable": "powershell",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-Command",
"$Input | ./wmi.resource.ps1 Set"
],
"input": "stdin"
},
"export": {
"executable": "powershell",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-Command",
"$Input | ./wmi.resource.ps1 Export"
],
"input": "stdin"
},
"validate": {
"executable": "powershell",
"args": [
"-NoLogo",
"-NonInteractive",
"-NoProfile",
"-Command",
"$Input | ./wmi.resource.ps1 Validate"
]
},
"exitCodes": {
"0": "Success",
"1": "Error"
}
}
Loading