diff --git a/test/PSDesiredStateConfiguration.Tests.ps1 b/test/PSDesiredStateConfiguration.Tests.ps1 index 34a8ae5..75a1cce 100644 --- a/test/PSDesiredStateConfiguration.Tests.ps1 +++ b/test/PSDesiredStateConfiguration.Tests.ps1 @@ -21,11 +21,7 @@ Function Install-ModuleIfMissing { } } -Function Test-IsInvokeDscResourceEnable { - return [ExperimentalFeature]::IsEnabled("PSDesiredStateConfiguration.InvokeDscResource") -} - -Describe "Test PSDesiredStateConfiguration" -tags CI { +Describe "Test PSDesiredStateConfiguration" { Context "Module loading" { BeforeAll { Function BeCommand { @@ -66,7 +62,7 @@ Describe "Test PSDesiredStateConfiguration" -tags CI { $commands | Should -HaveCommand -CommandName 'Configuration' -ModuleName PSDesiredStateConfiguration } - It "The module should have the Configuration Command" { + It "The module should have the New-DscChecksum Command" { $commands | Should -HaveCommand -CommandName 'New-DscChecksum' -ModuleName PSDesiredStateConfiguration } @@ -74,195 +70,15 @@ Describe "Test PSDesiredStateConfiguration" -tags CI { $commands | Should -HaveCommand -CommandName 'Get-DscResource' -ModuleName PSDesiredStateConfiguration } - It "The module should have the Invoke-DscResource Command" -Skip:(!(Test-IsInvokeDscResourceEnable)) { + It "The module should have the Invoke-DscResource Command" { $commands | Should -HaveCommand -CommandName 'Invoke-DscResource' -ModuleName PSDesiredStateConfiguration } - } - Context "Get-DscResource - Composite Resources" { - BeforeAll { - $origProgress = $global:ProgressPreference - $global:ProgressPreference = 'SilentlyContinue' - Install-ModuleIfMissing -Name PSDscResources - $testCases = @( - @{ - TestCaseName = 'case mismatch in resource name' - Name = 'groupset' - ModuleName = 'PSDscResources' - } - @{ - TestCaseName = 'Both names have matching case' - Name = 'GroupSet' - ModuleName = 'PSDscResources' - } - @{ - TestCaseName = 'case mismatch in module name' - Name = 'GroupSet' - ModuleName = 'psdscResources' - } - ) - } - AfterAll { - $Global:ProgressPreference = $origProgress - } - - it "should be able to get - " -TestCases $testCases { - param($Name) - - if ($IsWindows) { - Set-ItResult -Pending -Because "Will only find script from PSDesiredStateConfiguration without modulename" - } - - if ($IsLinux) { - Set-ItResult -Pending -Because "https://github.com/PowerShell/PSDesiredStateConfiguration/issues/26" - } - - $resource = Get-DscResource -Name $name - $resource | Should -Not -BeNullOrEmpty - $resource.Name | Should -Be $Name - if (Test-IsInvokeDscResourceEnable) { - $resource.ImplementationDetail | Should -BeNullOrEmpty - } - else { - $resource.ImplementationDetail | Should -BeNullOrEmpty - } - - } - - it "should be able to get from - " -TestCases $testCases { - param($Name, $ModuleName, $PendingBecause) - - if ($IsLinux) { - Set-ItResult -Pending -Because "https://github.com/PowerShell/PSDesiredStateConfiguration/issues/26" - } - - if ($PendingBecause) { - Set-ItResult -Pending -Because $PendingBecause - } - - $resource = Get-DscResource -Name $Name -Module $ModuleName - $resource | Should -Not -BeNullOrEmpty - $resource.Name | Should -Be $Name - if (Test-IsInvokeDscResourceEnable) { - $resource.ImplementationDetail | Should -BeNullOrEmpty - } - else { - $resource.ImplementationDetail | Should -BeNullOrEmpty - } + It "The module should have the ConvertTo-DscJsonSchema Command" { + $commands | Should -HaveCommand -CommandName 'ConvertTo-DscJsonSchema' -ModuleName PSDesiredStateConfiguration } } - Context "Get-DscResource - ScriptResources" { - BeforeAll { - $origProgress = $global:ProgressPreference - $global:ProgressPreference = 'SilentlyContinue' - - Install-ModuleIfMissing -Name PSDscResources -Force - - # Install PowerShellGet only if PowerShellGet 2.2.1 or newer does not exist - Install-ModuleIfMissing -Name PowerShellGet -MinimumVersion '2.2.1' - $module = Get-Module PowerShellGet -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1 - - $psGetModuleSpecification = @{ModuleName = $module.Name; ModuleVersion = $module.Version.ToString() } - $psGetModuleCount = @(Get-Module PowerShellGet -ListAvailable).Count - $testCases = @( - @{ - TestCaseName = 'case mismatch in resource name' - Name = 'script' - ModuleName = 'PSDscResources' - } - @{ - TestCaseName = 'Both names have matching case' - Name = 'Script' - ModuleName = 'PSDscResources' - } - @{ - TestCaseName = 'case mismatch in module name' - Name = 'Script' - ModuleName = 'psdscResources' - } - <# - Add these back when PowerShellGet is fixed https://github.com/PowerShell/PowerShellGet/pull/529 - @{ - TestCaseName = 'case mismatch in resource name' - Name = 'PsModule' - ModuleName = 'PowerShellGet' - } - @{ - TestCaseName = 'Both names have matching case' - Name = 'PSModule' - ModuleName = 'PowerShellGet' - } - @{ - TestCaseName = 'case mismatch in module name' - Name = 'PSModule' - ModuleName = 'powershellget' - } - #> - ) - } - - AfterAll { - $Global:ProgressPreference = $origProgress - } - - it "should be able to get - " -TestCases $testCases { - param($Name) - - if ($IsWindows) { - Set-ItResult -Pending -Because "Will only find script from PSDesiredStateConfiguration without modulename" - } - - if ($PendingBecause) { - Set-ItResult -Pending -Because $PendingBecause - } - - $resources = @(Get-DscResource -Name $name) - $resources | Should -Not -BeNullOrEmpty - foreach ($resource in $resource) { - $resource.Name | Should -Be $Name - if (Test-IsInvokeDscResourceEnable) { - $resource.ImplementationDetail | Should -Be 'ScriptBased' - } - else { - $resource.ImplementationDetail | Should -BeNullOrEmpty - } - - } - } - - it "should be able to get from - " -TestCases $testCases { - param($Name, $ModuleName, $PendingBecause) - - if ($IsLinux) { - Set-ItResult -Pending -Because "https://github.com/PowerShell/PSDesiredStateConfiguration/issues/12 and https://github.com/PowerShell/PowerShellGet/pull/529" - } - - if ($PendingBecause) { - Set-ItResult -Pending -Because $PendingBecause - } - - $resources = @(Get-DscResource -Name $name -Module $ModuleName) - $resources | Should -Not -BeNullOrEmpty - foreach ($resource in $resource) { - $resource.Name | Should -Be $Name - if (Test-IsInvokeDscResourceEnable) { - $resource.ImplementationDetail | Should -Be 'ScriptBased' - } - else { - $resource.ImplementationDetail | Should -BeNullOrEmpty - } - } - } - - it "should throw when resource is not found" { - Set-ItResult -Pending -Because "https://github.com/PowerShell/PSDesiredStateConfiguration/issues/17" - { - Get-DscResource -Name antoehusatnoheusntahoesnuthao -Module tanshoeusnthaosnetuhasntoheusnathoseun - } | - Should -Throw -ErrorId 'Microsoft.PowerShell.Commands.WriteErrorException,CheckResourceFound' - } - } Context "Get-DscResource - Class base Resources" { BeforeAll { @@ -302,19 +118,11 @@ Describe "Test PSDesiredStateConfiguration" -tags CI { $resource = Get-DscResource -Name $Name -Module $ModuleName $resource | Should -Not -BeNullOrEmpty $resource.Name | Should -Be $Name - if (Test-IsInvokeDscResourceEnable) { - $resource.ImplementationDetail | Should -Be 'ClassBased' - } - else { - $resource.ImplementationDetail | Should -BeNullOrEmpty - } + $resource.ImplementationDetail | Should -Be 'ClassBased' } it "should be able to get class resource - - " -TestCases $classTestCases { param($Name, $ModuleName, $PendingBecause) - if ($IsWindows) { - Set-ItResult -Pending -Because "https://github.com/PowerShell/PSDesiredStateConfiguration/issues/19" - } if ($PendingBecause) { Set-ItResult -Pending -Because $PendingBecause @@ -323,14 +131,10 @@ Describe "Test PSDesiredStateConfiguration" -tags CI { $resource = Get-DscResource -Name $Name $resource | Should -Not -BeNullOrEmpty $resource.Name | Should -Be $Name - if (Test-IsInvokeDscResourceEnable) { - $resource.ImplementationDetail | Should -Be 'ClassBased' - } - else { - $resource.ImplementationDetail | Should -BeNullOrEmpty - } + $resource.ImplementationDetail | Should -Be 'ClassBased' } } + Context "Invoke-DscResource" { BeforeAll { $origProgress = $global:ProgressPreference @@ -346,169 +150,6 @@ Describe "Test PSDesiredStateConfiguration" -tags CI { $Global:ProgressPreference = $origProgress } - Context "mof resources" { - BeforeAll { - $dscMachineStatusCases = @( - @{ - value = '1' - expectedResult = $true - } - @{ - value = '$true' - expectedResult = $true - } - @{ - value = '0' - expectedResult = $false - } - @{ - value = '$false' - expectedResult = $false - } - ) - - Install-ModuleIfMissing -Name PowerShellGet -Force -SkipPublisherCheck -MinimumVersion '2.2.1' - Install-ModuleIfMissing -Name xWebAdministration - $module = Get-Module PowerShellGet -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1 - - $psGetModuleSpecification = @{ModuleName = $module.Name; ModuleVersion = $module.Version.ToString() } - } - it "Set method should work" -Skip:(!(Test-IsInvokeDscResourceEnable)) { - if (!$IsLinux) { - $result = Invoke-DscResource -Name PSModule -ModuleName $psGetModuleSpecification -Method set -Property @{ - Name = 'PsDscResources' - InstallationPolicy = 'Trusted' - } - } - else { - # workraound because of https://github.com/PowerShell/PowerShellGet/pull/529 - Install-ModuleIfMissing -Name PsDscResources -Force - } - - $result.RebootRequired | Should -BeFalse - $module = Get-module PsDscResources -ListAvailable - $module | Should -Not -BeNullOrEmpty -Because "Resource should have installed module" - } - it 'Set method should return RebootRequired= when $global:DSCMachineStatus = ' -Skip:(!(Test-IsInvokeDscResourceEnable)) -TestCases $dscMachineStatusCases { - param( - $value, - $ExpectedResult - ) - - # using create scriptBlock because $using: doesn't work with existing Invoke-DscResource - # Verified in Windows PowerShell on 20190814 - $result = Invoke-DscResource -Name Script -ModuleName PSDscResources -Method Set -Property @{TestScript = { Write-Output 'test'; return $false }; GetScript = { return @{ } }; SetScript = [scriptblock]::Create("`$global:DSCMachineStatus = $value;return") } - $result | Should -Not -BeNullOrEmpty - $result.RebootRequired | Should -BeExactly $expectedResult - } - - it "Test method should return false" -Skip:(!(Test-IsInvokeDscResourceEnable)) { - $result = Invoke-DscResource -Name Script -ModuleName PSDscResources -Method Test -Property @{TestScript = { Write-Output 'test'; return $false }; GetScript = { return @{ } }; SetScript = { return } } - $result | Should -Not -BeNullOrEmpty - $result.InDesiredState | Should -BeFalse -Because "Test method return false" - } - - it "Test method should return true" -Skip:(!(Test-IsInvokeDscResourceEnable)) { - $result = Invoke-DscResource -Name Script -ModuleName PSDscResources -Method Test -Property @{TestScript = { Write-Verbose 'test'; return $true }; GetScript = { return @{ } }; SetScript = { return } } - $result | Should -BeTrue -Because "Test method return true" - } - - it "Test method should return true with moduleSpecification" -Skip:(!(Test-IsInvokeDscResourceEnable)) { - $module = get-module PsDscResources -ListAvailable - $moduleSpecification = @{ModuleName = $module.Name; ModuleVersion = $module.Version.ToString() } - $result = Invoke-DscResource -Name Script -ModuleName $moduleSpecification -Method Test -Property @{TestScript = { Write-Verbose 'test'; return $true }; GetScript = { return @{ } }; SetScript = { return } } - $result | Should -BeTrue -Because "Test method return true" - } - - it "Invalid moduleSpecification" -Skip:(!(Test-IsInvokeDscResourceEnable)) { - Set-ItResult -Pending -Because "https://github.com/PowerShell/PSDesiredStateConfiguration/issues/17" - $moduleSpecification = @{ModuleName = 'PsDscResources'; ModuleVersion = '99.99.99.993' } - { - Invoke-DscResource -Name Script -ModuleName $moduleSpecification -Method Test -Property @{TestScript = { Write-Host 'test'; return $true }; GetScript = { return @{ } }; SetScript = { return } } -ErrorAction Stop - } | - Should -Throw -ErrorId 'InvalidResourceSpecification,Invoke-DscResource' -ExpectedMessage 'Invalid Resource Name ''Script'' or module specification.' - } - - it "Test an embedded DSC resource" { - if (!(Test-IsInvokeDscResourceEnable)) { - Set-ItResult -Skipped -Because "Feature not enabled" - } - - $resourceName="TestRes" - $moduleName="TestEmbeddedDSCResource" - $embObj = @(New-Object -TypeName psobject -Property @{embclassprop="property1"}) - - Install-ModuleIfMissing -Name $moduleName -Force - - $resource = Get-DscResource -Name $resourceName -Module $moduleName -ErrorAction Stop - $resource | Should -Not -BeNullOrEmpty - $resource.Name | Should -Be $resourceName - - $methodName="Test" - $result = Invoke-DscResource -Name $resourceName -ModuleName $moduleName -Method $methodName -Property @{embclassobj=$embObj;propName="property1"} - $result.InDesiredState | Should -BeTrue - $result = Invoke-DscResource -Name $resourceName -ModuleName $moduleName -Method $methodName -Property @{embclassobj=$embObj;propName="property2"} - $result.InDesiredState | Should -BeFalse - - $methodName="Get" - $result = Invoke-DscResource -Name $resourceName -ModuleName $moduleName -Method $methodName -Property @{embclassobj=$embObj;propName="property1"} - $result.propName | Should -Be "property1" - $result = Invoke-DscResource -Name $resourceName -ModuleName $moduleName -Method $methodName -Property @{embclassobj=$embObj;propName="property2"} - $result.propName | Should -Not -Be "property1" - - $methodName="Set" - $result = Invoke-DscResource -Name $resourceName -ModuleName $moduleName -Method $methodName -Property @{embclassobj=$embObj;propName="property1"} - $result | Should -Not -BeNullOrEmpty - $result.RebootRequired | Should -BeFalse - } - - it "Using PsDscRunAsCredential should say not supported" -Skip:(!(Test-IsInvokeDscResourceEnable)) { - { - Invoke-DscResource -Name Script -ModuleName PSDscResources -Method Set -Property @{TestScript = { Write-Output 'test'; return $false }; GetScript = { return @{ } }; SetScript = {return}; PsDscRunAsCredential='natoheu'} -ErrorAction Stop - } | - Should -Throw -ErrorId 'PsDscRunAsCredentialNotSupport,Invoke-DscResource' - } - - # waiting on Get-DscResource to be fixed - it "Invalid module name" -Skip:(!(Test-IsInvokeDscResourceEnable)) { - Set-ItResult -Pending -Because "https://github.com/PowerShell/PSDesiredStateConfiguration/issues/17" - { - Invoke-DscResource -Name Script -ModuleName santoheusnaasonteuhsantoheu -Method Test -Property @{TestScript = { Write-Host 'test'; return $true }; GetScript = { return @{ } }; SetScript = { return } } -ErrorAction Stop - } | - Should -Throw -ErrorId 'Microsoft.PowerShell.Commands.WriteErrorException,CheckResourceFound' - } - - it "Invalid resource name" -Skip:(!(Test-IsInvokeDscResourceEnable)) { - if ($IsWindows) { - Set-ItResult -Pending -Because "https://github.com/PowerShell/PSDesiredStateConfiguration/issues/17" - } - - { - Invoke-DscResource -Name santoheusnaasonteuhsantoheu -Method Test -Property @{TestScript = { Write-Host 'test'; return $true }; GetScript = { return @{ } }; SetScript = { return } } -ErrorAction Stop - } | - Should -Throw -ErrorId 'Microsoft.PowerShell.Commands.WriteErrorException,CheckResourceFound' - } - - it "Get method should work" -Skip:(!(Test-IsInvokeDscResourceEnable)) { - if ($IsLinux) { - Set-ItResult -Pending -Because "https://github.com/PowerShell/PSDesiredStateConfiguration/issues/12 and https://github.com/PowerShell/PowerShellGet/pull/529" - } - - $result = Invoke-DscResource -Name PSModule -ModuleName $psGetModuleSpecification -Method Get -Property @{ Name = 'PsDscResources' } - $result | Should -Not -BeNullOrEmpty - $result.Author | Should -BeLike 'Microsoft*' - $result.InstallationPolicy | Should -BeOfType [string] - $result.Guid | Should -BeOfType [Guid] - $result.Ensure | Should -Be 'Present' - $result.Name | Should -be 'PsDscResources' - $result.Description | Should -BeLike 'This*DSC*' - $result.InstalledVersion | should -BeOfType [Version] - $result.ModuleBase | Should -BeLike '*PSDscResources*' - $result.Repository | should -BeOfType [string] - $result.ModuleType | Should -Be 'Manifest' - } - } - Context "Class Based Resources" { BeforeAll { Install-ModuleIfMissing -Name XmlContentDsc -Force @@ -530,7 +171,7 @@ Describe "Test PSDesiredStateConfiguration" -tags CI { $resolvedXmlPath = (Resolve-Path -Path $testXmlPath).ProviderPath } - it 'Set method should work' -Skip:(!(Test-IsInvokeDscResourceEnable)) { + it 'Set method should work' { param( $value, $ExpectedResult @@ -539,9 +180,234 @@ Describe "Test PSDesiredStateConfiguration" -tags CI { $testString = '890574209347509120348' $result = Invoke-DscResource -Name XmlFileContentResource -ModuleName XmlContentDsc -Property @{Path = $resolvedXmlPath; XPath = '/configuration/appSetting/Test1'; Ensure = 'Present'; Attributes = @{ TestValue2 = $testString; Name = $testString } } -Method Set $result | Should -Not -BeNullOrEmpty + $result.GetType() | Should -Be 'InvokeDscResourceSetResult' $result.RebootRequired | Should -BeFalse $testXmlPath | Should -FileContentMatch $testString } + + it 'Get method should work' { + param( + $value, + $ExpectedResult + ) + + $result = Invoke-DscResource -Name XmlFileContentResource -ModuleName XmlContentDsc -Property @{Path = $resolvedXmlPath; XPath = '/configuration/appSetting/Test1'} -Method Get + $result.GetType() | Should -Be 'XmlFileContentResource' + } + + it 'Test method should work' { + param( + $value, + $ExpectedResult + ) + + $result = Invoke-DscResource -Name XmlFileContentResource -ModuleName XmlContentDsc -Property @{Path = $resolvedXmlPath; XPath = '/configuration/appSetting/Test1'} -Method Test + $result | Should -Not -BeNullOrEmpty + $result.GetType() | Should -Be 'InvokeDscResourceTestResult' + $result.InDesiredState | Should -Not -BeNullOrEmpty + } } } } + +Describe "DSC MOF Compilation" { + BeforeAll { + # ensure that module is imported + Import-Module -Name PSDesiredStateConfiguration -MinimumVersion 3.0.0 + Install-ModuleIfMissing -Name XmlContentDsc -Force + } + + It "Should be able to compile a MOF using configuration keyword" { + + Write-Verbose "DSC_HOME: ${env:DSC_HOME}" -Verbose + [Scriptblock]::Create(@" +configuration DSCTestConfig +{ + Import-DscResource -ModuleName XmlContentDsc + Node "localhost" { + XmlFileContentResource f1 + { + Path = 'testpath' + XPath = '/configuration/appSetting/Test1' + Ensure = 'Absent' + } + } +} + +DSCTestConfig -OutputPath TestDrive:\DscTestConfig2 +"@) | Should -Not -Throw + + "TestDrive:\DscTestConfig2\localhost.mof" | Should -Exist + } +} + +Describe "All types DSC resource tests" { + BeforeAll { + + Import-Module -Name PSDesiredStateConfiguration -MinimumVersion 3.0.0 + + $SavedPSModulePath = $env:PSModulePath + + $testModulesPath = Join-Path $PSScriptRoot "TestModules" + "TestModulesPath is " + $testModulesPath | Write-Verbose -Verbose + $env:PSModulePath = $testModulesPath + [System.IO.Path]::PathSeparator + $env:PSModulePath + "PSModulePath is " + $env:PSModulePath | Write-Verbose -Verbose + } + + AfterAll { + $env:PSModulePath = $SavedPSModulePath + } + + It "Check all property types in Get-DscResource" { + + $resource = Get-DscResource | ? {$_.Name -eq "xTestClassResource"} + $resource | Should -Not -BeNullOrEmpty + $resource.Properties.Count | Should -Be 32 + + foreach($dscResourcePropertyInfo in $resource.Properties) + { + switch ($dscResourcePropertyInfo.Name) + { + "Name" {$dscResourcePropertyInfo.PropertyType | Should -Be '[string]'} + "Value" {$dscResourcePropertyInfo.PropertyType | Should -Be '[string]'} + "bValue" {$dscResourcePropertyInfo.PropertyType | Should -Be '[bool]'} + "sArray" {$dscResourcePropertyInfo.PropertyType | Should -Be '[string[]]'} + "bValueArray" {$dscResourcePropertyInfo.PropertyType | Should -Be '[bool[]]'} + "char16Value" {$dscResourcePropertyInfo.PropertyType | Should -Be '[char]'} + "char16ValueArray" {$dscResourcePropertyInfo.PropertyType | Should -Be '[char[]]'} + "dateTimeVal" {$dscResourcePropertyInfo.PropertyType | Should -Be '[DateTime]'} + "dateTimeArrayVal" {$dscResourcePropertyInfo.PropertyType | Should -Be '[DateTime[]]'} + "EmbClassObj" {$dscResourcePropertyInfo.PropertyType | Should -Be '[EmbClass]'} + "EmbClassObjArray" {$dscResourcePropertyInfo.PropertyType | Should -Be '[EmbClass[]]'} + "Ensure" {$dscResourcePropertyInfo.PropertyType | Should -Be '[string]'} + "Real32Value" {$dscResourcePropertyInfo.PropertyType | Should -Be '[Single]'} + "Real32ValueArray" {$dscResourcePropertyInfo.PropertyType | Should -Be '[Single[]]'} + "Real64Value" {$dscResourcePropertyInfo.PropertyType | Should -Be '[double]'} + "Real64ValueArray" {$dscResourcePropertyInfo.PropertyType | Should -Be '[double[]]'} + + "sInt8Value" {$dscResourcePropertyInfo.PropertyType | Should -Be '[SByte]'} + "sInt8ValueArray" {$dscResourcePropertyInfo.PropertyType | Should -Be '[SByte[]]'} + "sInt16Value" {$dscResourcePropertyInfo.PropertyType | Should -Be '[Int16]'} + "sInt16ValueArray" {$dscResourcePropertyInfo.PropertyType | Should -Be '[Int16[]]'} + "sInt32Value" {$dscResourcePropertyInfo.PropertyType | Should -Be '[Int32]'} + "sInt32ValueArray" {$dscResourcePropertyInfo.PropertyType | Should -Be '[Int32[]]'} + "sInt64Value" {$dscResourcePropertyInfo.PropertyType | Should -Be '[Int64]'} + "sInt64ValueArray" {$dscResourcePropertyInfo.PropertyType | Should -Be '[Int64[]]'} + + "uInt8Value" {$dscResourcePropertyInfo.PropertyType | Should -Be '[Byte]'} + "uInt8ValueArray" {$dscResourcePropertyInfo.PropertyType | Should -Be '[Byte[]]'} + "uInt16Value" {$dscResourcePropertyInfo.PropertyType | Should -Be '[UInt16]'} + "uInt16ValueArray" {$dscResourcePropertyInfo.PropertyType | Should -Be '[UInt16[]]'} + "uInt32Value" {$dscResourcePropertyInfo.PropertyType | Should -Be '[UInt32]'} + "uInt32ValueArray" {$dscResourcePropertyInfo.PropertyType | Should -Be '[UInt32[]]'} + "uInt64Value" {$dscResourcePropertyInfo.PropertyType | Should -Be '[UInt64]'} + "uInt64ValueArray" {$dscResourcePropertyInfo.PropertyType | Should -Be '[UInt64[]]'} + } + } + } + + It "Check all property types in Invoke-DscResource" { + + $resource = Invoke-DscResource -Name xTestClassResource -ModuleName xTestClassResource -Method Get -Property @{Name="Test"} + $resource | Should -Not -BeNullOrEmpty + $resource.GetType().Name | Should -Be "xTestClassResource" + $resource.Name | Should -Be "Test" + $resource.Value | Should -Be "Inside if" + + $resource.Name.GetType().Name | Should -Be "String" + $resource.Value.GetType().Name | Should -Be "String" + $resource.sArray.GetType().Name | Should -Be "String[]" + + $resource.bValue.GetType().Name | Should -Be "Boolean" + $resource.bValueArray.GetType().Name | Should -Be "Boolean[]" + $resource.char16Value.GetType().Name | Should -Be "Char" + $resource.char16ValueArray.GetType().Name | Should -Be "Char[]" + $resource.dateTimeVal.GetType().Name | Should -Be "DateTime" + $resource.dateTimeArrayVal.GetType().Name | Should -Be "DateTime[]" + $resource.EmbClassObj.GetType().Name | Should -Be "EmbClass" + $resource.EmbClassObjArray.GetType().Name | Should -Be "EmbClass[]" + $resource.Ensure.GetType().Name | Should -Be "Ensure" + $resource.Real32Value.GetType().Name | Should -Be "Single" + $resource.Real32ValueArray.GetType().Name | Should -Be "Single[]" + $resource.Real64Value.GetType().Name | Should -Be "Double" + $resource.Real64ValueArray.GetType().Name | Should -Be "Double[]" + + $resource.sInt8Value.GetType().Name | Should -Be "SByte" + $resource.sInt8ValueArray.GetType().Name | Should -Be "SByte[]" + $resource.sInt16Value.GetType().Name | Should -Be "Int16" + $resource.sInt16ValueArray.GetType().Name | Should -Be "Int16[]" + $resource.sInt32Value.GetType().Name | Should -Be "Int32" + $resource.sInt32ValueArray.GetType().Name | Should -Be "Int32[]" + $resource.sInt64Value.GetType().Name | Should -Be "Int64" + $resource.sInt64ValueArray.GetType().Name | Should -Be "Int64[]" + + $resource.uInt8Value.GetType().Name | Should -Be "Byte" + $resource.uInt8ValueArray.GetType().Name | Should -Be "Byte[]" + $resource.uInt16Value.GetType().Name | Should -Be "UInt16" + $resource.uInt16ValueArray.GetType().Name | Should -Be "UInt16[]" + $resource.uInt32Value.GetType().Name | Should -Be "UInt32" + $resource.uInt32ValueArray.GetType().Name | Should -Be "UInt32[]" + $resource.uInt64Value.GetType().Name | Should -Be "UInt64" + $resource.uInt64ValueArray.GetType().Name | Should -Be "UInt64[]" + + # extra check for embedded objects + $resource.EmbClassObj.EmbClassStr1 | Should -Be "TestEmbObjValue" + $resource.EmbClassObjArray[0].EmbClassStr1 | Should -Be "TestEmbClassStr1Value" + } + + It "Check all property types in configuration compilation" { + + [Scriptblock]::Create(@" +configuration DSCAllTypesConfig +{ + Import-DscResource -ModuleName xTestClassResource + Node "localhost" { + xTestClassResource f1 + { + Name = 'TestName' + Value = 'TestValue' + + char16Value = 'A' + char16ValueArray = @('A','B') + + sArray = @('Test1','Test2') + + bValue = `$true + bValueArray = @(`$true,`$false) + + dateTimeVal = Get-Date + dateTimeArrayVal = @(`$(Get-Date), `$(Get-Date)) + + Ensure = 'Present' + + uInt8Value = 255 + sInt8Value = -128 + uInt16Value = 65535 + sInt16Value = -32768 + uInt32Value = 4294967295 + sInt32Value = -2147483648 + uInt64Value = 18446744073709551615 + sInt64Value = -9223372036854775808 + + Real32Value = [Single]-1.234 + Real64Value = [Double]-1.234 + + uInt8ValueArray = @(255) + sInt8ValueArray = @(-128) + uInt16ValueArray = @(65535) + sInt16ValueArray = @(-32768) + uInt32ValueArray = @(4294967295) + sInt32ValueArray = @(-2147483648) + uInt64ValueArray = @(18446744073709551615) + sInt64ValueArray = @(-9223372036854775808) + } + } +} + +DSCAllTypesConfig -OutputPath TestDrive:\DSCAllTypesConfig +"@) | Should -Not -Throw + + "TestDrive:\DSCAllTypesConfig\localhost.mof" | Should -Exist + Get-Content -Raw -Path "TestDrive:\DSCAllTypesConfig\localhost.mof" | Write-Verbose -Verbose + } +} diff --git a/test/TestModules/xTestClassResource/xTestClassResource.psd1 b/test/TestModules/xTestClassResource/xTestClassResource.psd1 new file mode 100644 index 0000000..ea45f7e Binary files /dev/null and b/test/TestModules/xTestClassResource/xTestClassResource.psd1 differ diff --git a/test/TestModules/xTestClassResource/xTestClassResource.psm1 b/test/TestModules/xTestClassResource/xTestClassResource.psm1 new file mode 100644 index 0000000..016e34f --- /dev/null +++ b/test/TestModules/xTestClassResource/xTestClassResource.psm1 @@ -0,0 +1,263 @@ +enum Ensure +{ + Absent + Present +} + +class EmbClass +{ + [DscProperty()] + [string] $EmbClassStr1 +} + +[DscResource()] +class xTestClassResource +{ + [DscProperty(Key)] + [string]$Name + + [DscProperty(Mandatory)] + [string] $Value + + [DscProperty()] + [Ensure] $Ensure + + [DscProperty()] + [string[]] $sArray + + [DscProperty()] + [EmbClass] $EmbClassObj + + [DscProperty()] + [EmbClass[]] $EmbClassObjArray + + [DscProperty()] + [DateTime] $dateTimeVal; + + [DscProperty()] + [DateTime[]] $dateTimeArrayVal; + + [DscProperty()] + [Boolean] $bValue; + + [DscProperty()] + [Byte] $uInt8Value; + + [DscProperty()] + [SByte] $sInt8Value; + + [DscProperty()] + [UInt16] $uInt16Value; + + [DscProperty()] + [Int16] $sInt16Value; + + [DscProperty()] + [UInt32] $uInt32Value; + + [DscProperty()] + [Int32] $sInt32Value; + + [DscProperty()] + [UInt64] $uInt64Value; + + [DscProperty()] + [Int64] $sInt64Value; + + [DscProperty()] + [Single] $Real32Value; + + [DscProperty()] + [Double] $Real64Value; + + [DscProperty()] + [Char] $char16Value; + + [DscProperty()] + [Boolean[]] $bValueArray; + + [DscProperty()] + [Byte[]] $uInt8ValueArray; + + [DscProperty()] + [SByte[]] $sInt8ValueArray; + + [DscProperty()] + [UInt16[]] $uInt16ValueArray; + + [DscProperty()] + [Int16[]] $sInt16ValueArray; + + [DscProperty()] + [UInt32[]] $uInt32ValueArray; + + [DscProperty()] + [Int32[]] $sInt32ValueArray; + + [DscProperty()] + [UInt64[]] $uInt64ValueArray; + + [DscProperty()] + [Int64[]] $sInt64ValueArray; + + [DscProperty()] + [single[]] $Real32ValueArray; + + [DscProperty()] + [double[]] $Real64ValueArray; + + [DscProperty()] + [Char[]] $char16ValueArray; + + [void] Set() + { + + Set-StrictMode -Version Latest + + if ($this.Ensure -eq [Ensure]::Present) + { + if ($this.Value -eq "fail") + { + Write-Error "Ensure=Present failed for $($this.Name) due to value $($this.Value)" + } + } + elseif ($this.Ensure -eq [Ensure]::Absent) + { + if ($this.Value -eq "fail") + { + Write-Error "Ensure=Absent failed for $($this.Name) due to value $($this.Value)" + } + } + } + + [bool] Test() + { + Write-Debug "Inside Test()" + Set-StrictMode -Version Latest + + [bool] $result = $false + + if ($this.value -eq "fail") + { + Write-Error "Failing Test-TargetResource because Value is set to 'fail'" + } + else + { + Write-Verbose "Start of EmbClassObjArray" -Verbose + foreach ($classObj in $this.EmbClassObjArray) + { + $classObj.EmbClassStr1 | write-Verbose -verbose + } + Write-Verbose "End of EmbClassObjArray" -Verbose + + Write-Verbose "Ensure: $($this.Ensure)" -verbose + Write-Verbose "sArray: $($this.sArray)" -verbose + Write-Verbose "dateTimeVal: $($this.dateTimeVal)" -verbose + Write-Verbose "dateTimeArrayVal: $($this.dateTimeArrayVal)" -verbose + Write-Verbose "bValue: $($this.bValue)" -verbose + Write-Verbose "uInt8Value: $($this.uInt8Value)" -verbose + Write-Verbose "sInt8Value: $($this.sInt8Value)" -verbose + Write-Verbose "uInt16Value: $($this.uInt16Value)" -verbose + Write-Verbose "sInt16Value: $($this.sInt16Value)" -verbose + Write-Verbose "uInt32Value: $($this.uInt32Value)" -verbose + Write-Verbose "sInt32Value: $($this.sInt32Value)" -verbose + Write-Verbose "uInt64Value: $($this.uInt64Value)" -verbose + Write-Verbose "sInt64Value: $($this.sInt64Value)" -verbose + Write-Verbose "Real32Value: $($this.Real32Value)" -verbose + Write-Verbose "Real64Value: $($this.Real64Value)" -verbose + Write-Verbose "bValueArray: $($this.bValueArray)" -verbose + Write-Verbose "char16Value: $($this.char16Value)" -verbose + Write-Verbose "uInt8ValueArray: $($this.uInt8ValueArray)" -verbose + Write-Verbose "sInt8ValueArray: $($this.sInt8ValueArray)" -verbose + Write-Verbose "uInt16ValueArray: $($this.uInt16ValueArray)" -verbose + Write-Verbose "sInt16ValueArray: $($this.sInt16ValueArray)" -verbose + Write-Verbose "uInt32ValueArray: $($this.uInt32ValueArray)" -verbose + Write-Verbose "sInt32ValueArray: $($this.sInt32ValueArray)" -verbose + Write-Verbose "uInt64ValueArray: $($this.uInt64ValueArray)" -verbose + Write-Verbose "sInt64ValueArray: $($this.sInt64ValueArray)" -verbose + Write-Verbose "Real32ValueArray: $($this.Real32ValueArray)" -verbose + Write-Verbose "Real64ValueArray: $($this.Real64ValueArray)" -verbose + Write-Verbose "char16ValueArray: $($this.char16ValueArray)" -verbose + + [Single]$f = -1.000003 + [double]$d = -1.234 + + $result = ($this.EmbClassObjArray[1].EmbClassStr1 -eq $this.Ensure) -and ` + ($this.sArray[1] -eq "s2") -and ` + ($this.dateTimeVal -lt (get-date 2020-12-12)) -and ` + ($this.dateTimeArrayVal[1] -gt (get-date 2020-08-20)) -and ` + ($this.bValue -eq $true) -and ` + ($this.uInt8Value -eq 255) -and ` + ($this.sInt8Value -eq -128) -and ` + ($this.uInt16Value -eq 65535) -and ` + ($this.sInt16Value -eq -32768) -and ` + ($this.uInt32Value -eq 4294967295) -and ` + ($this.sInt32Value -eq -2147483648) -and ` + ($this.uInt64Value -eq 18446744073709551615) -and ` + ($this.sInt64Value -eq -9223372036854775808) -and ` + ($this.Real32Value -eq $f) -and ` + ($this.Real64Value -eq $d) -and ` + ($this.bValueArray[1] -eq $true) -and ` + ($this.char16Value -eq 'c') -and ` + ($this.uInt8ValueArray[1] -eq 254) -and ` + ($this.sInt8ValueArray[1] -eq -127) -and ` + ($this.uInt16ValueArray[1] -eq 65534) -and ` + ($this.sInt16ValueArray[1] -eq -32767) -and ` + ($this.uInt32ValueArray[1] -eq 4294967294) -and ` + ($this.sInt32ValueArray[1] -eq -2147483647) -and ` + ($this.uInt64ValueArray[1] -eq 18446744073709551614) -and ` + ($this.sInt64ValueArray[1] -eq -9223372036854775807) -and ` + ($this.Real32ValueArray[1] -eq $f) -and ` + ($this.Real64ValueArray[1] -eq $d) -and ` + ($this.char16ValueArray[1] -eq 'd') + } + + return $result + } + + [xTestClassResource] Get() + { + Write-Debug "Inside Get()" + + if ($this.Value -ne "fail") + { + $this.Value = "Inside if" + } + else + { + $this.Value = "Inside else" + } + + # initialize properties so that they are not null + $this.char16Value = "A" + $this.sArray = [String[]]::new(0) + + $this.EmbClassObj = [EmbClass]::new() + $this.EmbClassObj.EmbClassStr1 = "TestEmbObjValue" + + $EmbObj = [EmbClass]::new() + $EmbObj.EmbClassStr1 = "TestEmbClassStr1Value" + $this.EmbClassObjArray = [EmbClass[]]::new(1) + $this.EmbClassObjArray[0] = $EmbObj + + $this.dateTimeVal = [DateTime]::Now + $this.dateTimeArrayVal = [DateTime[]]::new(0) + $this.bValueArray = [Boolean[]]::new(0) + $this.char16ValueArray = [Char[]]::new(0) + + $this.uInt8ValueArray = [Byte[]]::new(0) + $this.sInt8ValueArray = [SByte[]]::new(0) + $this.uInt16ValueArray = [UInt16[]]::new(0) + $this.sInt16ValueArray = [Int16[]]::new(0) + $this.uInt32ValueArray = [UInt32[]]::new(0) + $this.sInt32ValueArray = [Int32[]]::new(0) + $this.uInt64ValueArray = [UInt64[]]::new(0) + $this.sInt64ValueArray = [Int64[]]::new(0) + + $this.Real32ValueArray = [single[]]::new(0) + $this.Real64ValueArray = [double[]]::new(0) + + return $this + } +} + diff --git a/test/configuration.Tests.ps1 b/test/configuration.Tests.ps1 deleted file mode 100644 index c423e8d..0000000 --- a/test/configuration.Tests.ps1 +++ /dev/null @@ -1,38 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -Describe "DSC MOF Compilation" -tags "CI" { - BeforeAll { - $module = Get-Module PowerShellGet -ListAvailable | Sort-Object -Property Version -Descending | Select-Object -First 1 - - $psGetModuleVersion = $module.Version.ToString() - if (!$env:DSC_HOME) - { - Import-Module PSDesiredStateConfiguration - } - } - - It "Should be able to compile a MOF using PSModule resource" { - if ($IsLinux) { - Set-ItResult -Pending -Because "https://github.com/PowerShell/PowerShellGet/pull/529" - } - - Write-Verbose "DSC_HOME: ${env:DSC_HOME}" -verbose - [Scriptblock]::Create(@" - configuration DSCTestConfig - { - Import-DscResource -ModuleName PowerShellGet -ModuleVersion $psGetModuleVersion - Node "localhost" { - PSModule f1 - { - Name = 'PsDscResources' - InstallationPolicy = 'Trusted' - } - } - } - - DSCTestConfig -OutputPath TestDrive:\DscTestConfig2 -"@) | Should -Not -Throw - - "TestDrive:\DscTestConfig2\localhost.mof" | Should -Exist - } -}