Skip to content

Commit 68f6c83

Browse files
committed
Add tests
1 parent d032063 commit 68f6c83

File tree

5 files changed

+77
-6
lines changed

5 files changed

+77
-6
lines changed

dsc/examples/variable.dsc.ps1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
configuration VariableConfiguration {
2+
Import-DscResource -ModuleName PSDesiredStateConfiguration
3+
Node localhost
4+
{
5+
Environment PathEnvironmentVariable {
6+
Name = 'TestPathEnvironmentVariable'
7+
Value = 'TestValue'
8+
Ensure = 'Present'
9+
Path = $true
10+
Target = @('Process')
11+
}
12+
}
13+
}

extensions/powershell/convert-resource.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ process {
1919
end {
2020
if ($lines.Count -ne 0) {
2121
$result = $scriptModule.invoke( { param($lines) Build-DscConfigDocument -Content $lines }, ($lines | Out-String) )
22-
22+
2323
return ($result | ConvertTo-Json -Depth 10 -Compress)
2424
}
2525
}

extensions/powershell/convertDscResource.psm1

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
$Script:IsPowerShellCore = $PSVersionTable.PSEdition -eq 'Core'
2+
3+
if ($Script:IsPowerShellCore)
4+
{
5+
if ($IsWindows)
6+
{
7+
Import-Module -Name 'PSDesiredStateConfiguration' -RequiredVersion 1.1 -UseWindowsPowerShell -WarningAction SilentlyContinue
8+
}
9+
Import-Module -Name 'PSDesiredStateConfiguration' -MinimumVersion 2.0.7 -Prefix 'Pwsh'
10+
}
11+
112
function Write-DscTrace {
213
param(
314
[Parameter(Mandatory = $false)]
@@ -178,11 +189,11 @@ function ConvertTo-DscObject
178189

179190
if ($null -eq $loadedModuleTest -and -not [System.String]::IsNullOrEmpty($moduleToLoad.ModuleVersion))
180191
{
181-
throw "Module {$($moduleToLoad.ModuleName)} version {$($moduleToLoad.ModuleVersion)} specified in the configuration isn't installed on the machine/agent. Install it by running: Install-Module -Name '$($moduleToLoad.ModuleName)' -RequiredVersion '$($moduleToLoad.ModuleVersion)'"
192+
"Module {$($moduleToLoad.ModuleName)} version {$($moduleToLoad.ModuleVersion)} specified in the configuration isn't installed on the machine/agent. Install it by running: Install-Module -Name '$($moduleToLoad.ModuleName)' -RequiredVersion '$($moduleToLoad.ModuleVersion)'" | Write-DscTrace -Operation Error
193+
exit 1
182194
}
183195
else
184196
{
185-
"Retrieving module: '$($moduleToLoad.ModuleName)'" | Write-DscTrace -Operation Debug
186197
if ($Script:IsPowerShellCore)
187198
{
188199
$currentResources = Get-PwshDscResource -Module $moduleToLoad.ModuleName
@@ -200,6 +211,12 @@ function ConvertTo-DscObject
200211
}
201212
}
202213

214+
if ($DSCResources.Count -eq 0)
215+
{
216+
"No DSC resources found in the imported modules." | Write-DscTrace -Operation Error
217+
exit 1
218+
}
219+
203220
# Drill down
204221
# Body.ScriptBlock is the part after "Configuration <InstanceName> {"
205222
# EndBlock is the actual code within that Configuration block
@@ -310,7 +327,6 @@ function ConvertTo-DscObject
310327
if ($null -eq $valueType)
311328
{
312329
$propertyFound = $false
313-
"Defined property {$key} was not found in resource {$resourceType}" | Write-DscTrace -Operation Warn
314330
}
315331

316332
if ($propertyFound)

extensions/powershell/powershell.dsc.extension.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
"-ExecutionPolicy",
1414
"Bypass",
1515
"-Command",
16+
"Get-Content",
1617
{
17-
"fileArg": "Get-Content"
18+
"fileArg": ""
1819
},
19-
"$args | ./convert-resource.ps1 $args"
20+
"| ./convert-resource.ps1"
2021
]
2122
}
2223
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
BeforeDiscovery {
5+
if ($IsWindows) {
6+
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
7+
$principal = [System.Security.Principal.WindowsPrincipal]::new($identity)
8+
$isElevated = $principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
9+
}
10+
}
11+
12+
Describe 'PowerShell extension tests' {
13+
It 'Example PowerShell file should work' -Skip:(!$isElevated) {
14+
$psFile = Resolve-Path -Path "$PSScriptRoot\..\..\dsc\examples\variable.dsc.ps1"
15+
$out = dsc -l trace config get -f $psFile 2>$TestDrive/error.log | ConvertFrom-Json
16+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Path $TestDrive/error.log -Raw | Out-String)
17+
$out.results[0].result.actualState.Ensure | Should -Be 'Absent'
18+
(Get-Content -Path $TestDrive/error.log -Raw) | Should -Match "Importing file '$psFile' with extension 'Microsoft.DSC.Extension/PowerShell'"
19+
}
20+
21+
It 'Invalid PowerShell configuration document file returns error' {
22+
$psFile = "$TestDrive/invalid.ps1"
23+
Set-Content -Path $psFile -Value @"
24+
configuration InvalidConfiguration {
25+
Import-DscResource -ModuleName InvalidModule
26+
Node localhost
27+
{
28+
Test Invalid {
29+
Name = 'InvalidTest'
30+
Ensure = 'Present'
31+
}
32+
}
33+
}
34+
"@
35+
$out = dsc -l trace config get -f $psFile 2>$TestDrive/error.log | ConvertFrom-Json
36+
$LASTEXITCODE | Should -Be 2 -Because (Get-Content -Path $TestDrive/error.log -Raw | Out-String)
37+
$content = (Get-Content -Path $TestDrive/error.log -Raw)
38+
$content | Should -BeLike "*Importing file '$psFile' with extension 'Microsoft.DSC.Extension/PowerShell'*"
39+
$content | Should -Match "No DSC resources found in the imported modules."
40+
}
41+
}

0 commit comments

Comments
 (0)