Skip to content

Commit 4379369

Browse files
committed
Add enum tests and resolve remarks
2 parents 683556d + 04dc007 commit 4379369

File tree

3 files changed

+170
-75
lines changed

3 files changed

+170
-75
lines changed

powershell-adapter/Tests/win_powershellgroup.tests.ps1

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ resources:
292292
Tags = @(
293293
'PSDscResource_PSClassResource'
294294
)
295+
DscCapabilities = @(
296+
'get'
297+
'test'
298+
'set'
299+
'export'
300+
)
295301
}
296302
}
297303
}
@@ -303,6 +309,11 @@ resources:
303309

304310

305311
$module = @'
312+
enum Ensure {
313+
Present
314+
Absent
315+
}
316+
306317
[DSCResource()]
307318
class PSClassResource {
308319
[DscProperty(Key)]
@@ -313,6 +324,9 @@ class PSClassResource {
313324
hidden
314325
[string] $HiddenNonDscProperty
315326
327+
[DscProperty()]
328+
[Ensure] $Ensure = [Ensure]::Present
329+
316330
PSClassResource() {
317331
}
318332
@@ -327,6 +341,23 @@ class PSClassResource {
327341
[void] Set() {
328342
329343
}
344+
345+
static [PSClassResource[]] Export()
346+
{
347+
$resultList = [System.Collections.Generic.List[PSClassResource]]::new()
348+
$resultCount = 5
349+
if ($env:PSClassResourceResultCount) {
350+
$resultCount = $env:PSClassResourceResultCount
351+
}
352+
1..$resultCount | %{
353+
$obj = New-Object PSClassResource
354+
$obj.Name = "Object$_"
355+
$obj.Ensure = [Ensure]::Present
356+
$resultList.Add($obj)
357+
}
358+
359+
return $resultList.ToArray()
360+
}
330361
}
331362
'@
332363

@@ -357,4 +388,15 @@ class PSClassResource {
357388
$LASTEXITCODE | Should -Be 0
358389
$out.afterstate.InDesiredState | Should -Be $true
359390
}
391+
392+
It 'Export works with class-based PS DSC resources' -Skip:(!$IsWindows) {
393+
394+
$out = dsc resource export -r PSClassResource/PSClassResource | ConvertFrom-Json
395+
$LASTEXITCODE | Should -Be 0
396+
$out | Should -Not -BeNullOrEmpty
397+
$out.resources.count | Should -Be 5
398+
$out.resources[0].properties.Ensure | Should -Be 'Present' # Check for enum property
399+
}
360400
}
401+
402+

powershell-adapter/psDscAdapter/win_psDscAdapter.psm1

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,21 @@ function Invoke-DscOperation {
452452
$addToActualState.properties = [psobject]@{'InDesiredState' = $Result }
453453
}
454454
'Export' {
455-
$t = $dscResourceInstance.GetType()
456-
$method = $t.GetMethod('Export')
457-
$resultArray = $method.Invoke($null, $null)
455+
$method = ValidateMethod -operation $Operation -class $dscResourceInstance
456+
$resultArray = @()
457+
$raw_obj_array = $method.Invoke($null, $null)
458+
foreach ($raw_obj in $raw_obj_array) {
459+
$Result_obj = @{}
460+
$ValidProperties | ForEach-Object {
461+
if ($raw_obj.$_ -is [System.Enum]) {
462+
$Result_obj[$_] = $raw_obj.$_.ToString()
463+
}
464+
else {
465+
$Result_obj[$_] = $raw_obj.$_
466+
}
467+
}
468+
$resultArray += $Result_obj
469+
}
458470
$addToActualState = $resultArray
459471
}
460472
}
@@ -546,6 +558,33 @@ function GetTypeInstanceFromModule {
546558
return $instance
547559
}
548560

561+
# ValidateMethod checks if the specified method exists in the class
562+
function ValidateMethod {
563+
param (
564+
[Parameter(Mandatory = $true)]
565+
[ValidateSet('Export', 'WhatIf')]
566+
[string] $operation,
567+
[Parameter(Mandatory = $true)]
568+
[object] $class
569+
)
570+
571+
$t = $class.GetType()
572+
$methods = $t.GetMethods() | Where-Object -Property Name -EQ $operation
573+
$method = foreach ($mt in $methods) {
574+
if ($mt.GetParameters().Count -eq 0) {
575+
$mt
576+
break
577+
}
578+
}
579+
580+
if ($null -eq $method) {
581+
"Method '$operation' not implemented by resource '$($t.Name)'" | Write-DscTrace -Operation Error
582+
exit 1
583+
}
584+
585+
return $method
586+
}
587+
549588
# cached resource
550589
class dscResourceCacheEntry {
551590
[string] $Type
Lines changed: 86 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,14 @@
11
{
2-
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
3-
"type": "Microsoft.Windows/WindowsPowerShell",
4-
"version": "0.1.0",
5-
"kind": "adapter",
6-
"description": "Resource adapter to classic DSC Powershell resources in Windows PowerShell.",
7-
"tags": [
8-
"PowerShell"
9-
],
10-
"adapter": {
11-
"list": {
12-
"executable": "powershell",
13-
"args": [
14-
"-NoLogo",
15-
"-NonInteractive",
16-
"-NoProfile",
17-
"-ExecutionPolicy",
18-
"Bypass",
19-
"-Command",
20-
"./psDscAdapter/powershell.resource.ps1 List"
21-
]
22-
},
23-
"config": "full"
24-
},
25-
"get": {
26-
"executable": "powershell",
27-
"args": [
28-
"-NoLogo",
29-
"-NonInteractive",
30-
"-NoProfile",
31-
"-ExecutionPolicy",
32-
"Bypass",
33-
"-Command",
34-
"$Input | ./psDscAdapter/powershell.resource.ps1 Get"
35-
],
36-
"input": "stdin"
37-
},
38-
"set": {
2+
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
3+
"type": "Microsoft.Windows/WindowsPowerShell",
4+
"version": "0.1.0",
5+
"kind": "adapter",
6+
"description": "Resource adapter to classic DSC Powershell resources in Windows PowerShell.",
7+
"tags": [
8+
"PowerShell"
9+
],
10+
"adapter": {
11+
"list": {
3912
"executable": "powershell",
4013
"args": [
4114
"-NoLogo",
@@ -44,39 +17,80 @@
4417
"-ExecutionPolicy",
4518
"Bypass",
4619
"-Command",
47-
"$Input | ./psDscAdapter/powershell.resource.ps1 Set"
48-
],
49-
"input": "stdin",
50-
"preTest": true
51-
},
52-
"test": {
53-
"executable": "powershell",
54-
"args": [
55-
"-NoLogo",
56-
"-NonInteractive",
57-
"-NoProfile",
58-
"-ExecutionPolicy",
59-
"Bypass",
60-
"-Command",
61-
"$Input | ./psDscAdapter/powershell.resource.ps1 Test"
62-
],
63-
"input": "stdin",
64-
"return": "state"
65-
},
66-
"validate": {
67-
"executable": "powershell",
68-
"args": [
69-
"-NoLogo",
70-
"-NonInteractive",
71-
"-NoProfile",
72-
"-ExecutionPolicy",
73-
"Bypass",
74-
"-Command",
75-
"$Input | ./psDscAdapter/powershell.resource.ps1 Validate"
76-
]
77-
},
78-
"exitCodes": {
79-
"0": "Success",
80-
"1": "Error"
81-
}
20+
"./psDscAdapter/powershell.resource.ps1 List"
21+
]
22+
},
23+
"config": "full"
24+
},
25+
"get": {
26+
"executable": "powershell",
27+
"args": [
28+
"-NoLogo",
29+
"-NonInteractive",
30+
"-NoProfile",
31+
"-ExecutionPolicy",
32+
"Bypass",
33+
"-Command",
34+
"$Input | ./psDscAdapter/powershell.resource.ps1 Get"
35+
],
36+
"input": "stdin"
37+
},
38+
"set": {
39+
"executable": "powershell",
40+
"args": [
41+
"-NoLogo",
42+
"-NonInteractive",
43+
"-NoProfile",
44+
"-ExecutionPolicy",
45+
"Bypass",
46+
"-Command",
47+
"$Input | ./psDscAdapter/powershell.resource.ps1 Set"
48+
],
49+
"input": "stdin",
50+
"preTest": true
51+
},
52+
"test": {
53+
"executable": "powershell",
54+
"args": [
55+
"-NoLogo",
56+
"-NonInteractive",
57+
"-NoProfile",
58+
"-ExecutionPolicy",
59+
"Bypass",
60+
"-Command",
61+
"$Input | ./psDscAdapter/powershell.resource.ps1 Test"
62+
],
63+
"input": "stdin",
64+
"return": "state"
65+
},
66+
"export": {
67+
"executable": "powershell",
68+
"args": [
69+
"-NoLogo",
70+
"-NonInteractive",
71+
"-NoProfile",
72+
"-ExecutionPolicy",
73+
"Bypass",
74+
"-Command",
75+
"$Input | ./psDscAdapter/powershell.resource.ps1 Export"
76+
],
77+
"input": "stdin",
78+
"return": "state"
79+
},
80+
"validate": {
81+
"executable": "powershell",
82+
"args": [
83+
"-NoLogo",
84+
"-NonInteractive",
85+
"-NoProfile",
86+
"-ExecutionPolicy",
87+
"Bypass",
88+
"-Command",
89+
"$Input | ./psDscAdapter/powershell.resource.ps1 Validate"
90+
]
91+
},
92+
"exitCodes": {
93+
"0": "Success",
94+
"1": "Error"
8295
}
96+
}

0 commit comments

Comments
 (0)