Skip to content

Commit 5297b5e

Browse files
author
Andrew
committed
Added tests
1 parent 8ce3724 commit 5297b5e

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
{
2+
"$schema": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/bundled/resource/manifest.json",
3+
"type": "Test/TestAdapter",
4+
"version": "0.1.0",
5+
"kind": "Adapter",
6+
"description": "Resource adapter for testing.",
7+
"tags": [
8+
"PowerShell"
9+
],
10+
"adapter": {
11+
"list": {
12+
"executable": "pwsh",
13+
"args": [
14+
"-NoLogo",
15+
"-NonInteractive",
16+
"-NoProfile",
17+
"-Command",
18+
"./testadapter.resource.ps1 List"
19+
]
20+
},
21+
"config": "full"
22+
},
23+
"get": {
24+
"executable": "pwsh",
25+
"args": [
26+
"-NoLogo",
27+
"-NonInteractive",
28+
"-NoProfile",
29+
"-Command",
30+
"$Input | ./testadapter.resource.ps1 Get"
31+
],
32+
"input": "stdin"
33+
},
34+
"set": {
35+
"executable": "pwsh",
36+
"args": [
37+
"-NoLogo",
38+
"-NonInteractive",
39+
"-NoProfile",
40+
"-Command",
41+
"$Input | ./testadapter.resource.ps1 Set"
42+
],
43+
"input": "stdin",
44+
"implementsPretest": true,
45+
"return": "state"
46+
},
47+
"test": {
48+
"executable": "pwsh",
49+
"args": [
50+
"-NoLogo",
51+
"-NonInteractive",
52+
"-NoProfile",
53+
"-Command",
54+
"$Input | ./testadapter.resource.ps1 Test"
55+
],
56+
"input": "stdin",
57+
"return": "state"
58+
},
59+
"export": {
60+
"executable": "pwsh",
61+
"args": [
62+
"-NoLogo",
63+
"-NonInteractive",
64+
"-NoProfile",
65+
"-Command",
66+
"$Input | ./testadapter.resource.ps1 Export"
67+
],
68+
"input": "stdin",
69+
"return": "state"
70+
},
71+
"validate": {
72+
"executable": "pwsh",
73+
"args": [
74+
"-NoLogo",
75+
"-NonInteractive",
76+
"-NoProfile",
77+
"-Command",
78+
"$Input | ./testadapter.resource.ps1 Validate"
79+
],
80+
"input": "stdin"
81+
},
82+
"exitCodes": {
83+
"0": "Success",
84+
"1": "Error"
85+
}
86+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
[CmdletBinding()]
4+
param(
5+
[Parameter(Mandatory = $true, Position = 0, HelpMessage = 'Operation to perform. Choose from List, Get, Set, Test, Export, Validate.')]
6+
[ValidateSet('List', 'Get', 'Set', 'Test', 'Export', 'Validate')]
7+
[string]$Operation,
8+
[Parameter(Mandatory = $false, Position = 1, ValueFromPipeline = $true, HelpMessage = 'Configuration or resource input in JSON format.')]
9+
[string]$jsonInput = '@{}'
10+
)
11+
12+
function Write-DscTrace {
13+
param(
14+
[Parameter(Mandatory = $false)]
15+
[ValidateSet('Error', 'Warn', 'Info', 'Debug', 'Trace')]
16+
[string]$Operation = 'Debug',
17+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
18+
[string]$Message
19+
)
20+
21+
$trace = @{$Operation = $Message } | ConvertTo-Json -Compress
22+
$host.ui.WriteErrorLine($trace)
23+
}
24+
25+
'Hello from TestAdapter.' | Write-DscTrace
26+
'PSPath=' + $PSHome | Write-DscTrace
27+
'PSModulePath=' + $env:PSModulePath | Write-DscTrace
28+
29+
if ($jsonInput -ne '@{}') {
30+
$inputobj = $jsonInput | ConvertFrom-Json
31+
}
32+
33+
$jsonInput | Write-DscTrace
34+
35+
switch ($Operation) {
36+
'List' {
37+
@{
38+
type = "Test/TestCase"
39+
kind = 'Resource'
40+
version = '1'
41+
capabilities = @('Get', 'Set', 'Test', 'Export')
42+
path = $PSScriptRoot
43+
directory = Split-Path $PSScriptRoot
44+
implementedAs = 'Adapter'
45+
author = 'Test'
46+
properties = @('TestCaseId', 'Input', 'Result')
47+
requireAdapter = 'Test/TestAdapter'
48+
description = 'TestCase resource'
49+
} | ConvertTo-Json -Compress
50+
}
51+
{ @('Get','Set','Test','Export') -contains $_ } {
52+
53+
# TestCase 1 = 'Verify adapted_dsc_type field'
54+
if (($inputobj.TestCaseId -eq 1 ) -or ($_ -eq 'Export')){
55+
$result = $inputobj.adapted_dsc_type -eq 'Test/TestCase'
56+
$result = @{'TestCaseId'=1; 'Input'=''; result = $result } | ConvertTo-Json -Depth 10 -Compress
57+
return $result
58+
}
59+
60+
}
61+
'Validate' {
62+
@{ valid = $true } | ConvertTo-Json
63+
}
64+
}

powershell-adapter/Tests/powershellgroup.resource.tests.ps1

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,33 @@ Describe 'PowerShell adapter resource tests' {
128128
$t = $resources | ? {$_.Type -eq 'TestClassResource/TestClassResource'}
129129
$t.properties | Should -Contain "BaseProperty"
130130
}
131+
132+
It 'Verify adapted_dsc_type field in Get' {
133+
$r = '{TestCaseId: 1}'| dsc resource get -r 'Test/TestCase'
134+
$LASTEXITCODE | Should -Be 0
135+
$resources = $r | ConvertFrom-Json
136+
$resources.actualState.result | Should -Be $True
137+
}
138+
139+
It 'Verify adapted_dsc_type field in Set' {
140+
$r = '{TestCaseId: 1}'| dsc resource set -r 'Test/TestCase'
141+
$LASTEXITCODE | Should -Be 0
142+
$resources = $r | ConvertFrom-Json
143+
$resources.beforeState.result | Should -Be $True
144+
$resources.afterState.result | Should -Be $True
145+
}
146+
147+
It 'Verify adapted_dsc_type field in Test' {
148+
$r = '{TestCaseId: 1}'| dsc resource test -r 'Test/TestCase'
149+
$LASTEXITCODE | Should -Be 0
150+
$resources = $r | ConvertFrom-Json
151+
$resources.actualState.result | Should -Be $True
152+
}
153+
154+
It 'Verify adapted_dsc_type field in Export' {
155+
$r = dsc resource export -r 'Test/TestCase'
156+
$LASTEXITCODE | Should -Be 0
157+
$resources = $r | ConvertFrom-Json
158+
$resources.resources[0].properties.result | Should -Be $True
159+
}
131160
}

0 commit comments

Comments
 (0)