diff --git a/CHANGELOG.md b/CHANGELOG.md index 35f3ca9..bd45734 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - Moved change log to CHANGELOG.md file - Fixed Markdown validation warnings in README.md - Added .MetaTestOptIn.json file to root of module +- Add Integration Tests for module resources ## 1.2.0.0 diff --git a/Misc/xBitlockerCommon.psm1 b/Misc/xBitlockerCommon.psm1 index 53d7843..aa20481 100644 --- a/Misc/xBitlockerCommon.psm1 +++ b/Misc/xBitlockerCommon.psm1 @@ -192,7 +192,7 @@ function EnableBitlocker throw "A TpmProtector must be used if Pin is used." } - Add-MissingBitLockerKeyProtector @PSBoundParameters -Verbose:$VerbosePreference + Add-MissingBitLockerKeyProtector @PSBoundParameters #Now enable Bitlocker with the primary key protector if ($blv.VolumeStatus -eq "FullyDecrypted") diff --git a/Test/Test-xBitlocker.ps1 b/Test/Test-xBitlocker.ps1 deleted file mode 100644 index b5c8f52..0000000 --- a/Test/Test-xBitlocker.ps1 +++ /dev/null @@ -1,165 +0,0 @@ -[CmdletBinding()] -param() - -$showVerbose = $true -$showValidSettings -eq $true - -if ($null -eq $pin) -{ - $pin = Get-Credential -Message "Enter the Bitlocker Pin in the password field" -} - -#Define the parameters that can be passed into individual tests -$blParams1 = @{ - MountPoint = "C:" - PrimaryProtector = "RecoveryPasswordProtector" - StartupKeyProtector = $true - StartupKeyPath = "A:" - RecoveryPasswordProtector = $true - UsedSpaceOnly = $true -} - -$blParams2 = @{ - MountPoint = "C:" - PrimaryProtector = "StartupKeyProtector" - StartupKeyProtector = $true - StartupKeyPath = "A:" - RecoveryPasswordProtector = $true - UsedSpaceOnly = $true -} - -$blParams7 = @{ - MountPoint = 'C:' - PrimaryProtector = 'StartupKeyProtector' - AdAccountOrGroupProtector = $true - AdAccountOrGroup = "mikelab.local\ucctest" - StartupKeyProtector = $true - StartupKeyPath = 'A:' - RecoveryPasswordProtector = $true - UsedSpaceOnly = $true -} - -$blParams8 = @{ - MountPoint = 'C:' - PrimaryProtector = 'RecoveryPasswordProtector' - AdAccountOrGroupProtector = $true - AdAccountOrGroup = "mikelab.local\ucctest" - StartupKeyProtector = $true - StartupKeyPath = 'A:' - RecoveryPasswordProtector = $true - UsedSpaceOnly = $true -} - -$blParams9 = @{ - MountPoint = 'C:' - PrimaryProtector = 'PasswordProtector' - StartupKeyProtector = $true - StartupKeyPath = 'A:' - PasswordProtector = $true - Password = $pin - RecoveryPasswordProtector = $true - UsedSpaceOnly = $true -} - -$autoBlParams1 = @{ - DriveType = "Fixed" - MinDiskCapacityGB = 20 - PrimaryProtector = "RecoveryPasswordProtector" - RecoveryPasswordProtector = $true - UsedSpaceOnly = $true -} - -function DisableBitlocker -{ - $blv = Get-BitLockerVolume - - foreach ($v in $blv) - { - if ($null -ne $v.KeyProtector -and $v.KeyProtector.Count -gt 0) - { - $v | Disable-BitLocker | Out-Null - } - } -} - -#Compares two values and reports whether they are the same or not -function CheckSetting($testName, $expectedValue, $actualValue) -{ - if ($expectedValue -ne $actualValue) - { - Write-Error -Message "Test: '$($testName)'. Result: Fail. Expected value: '$($expectedValue)'. Actual value: '$($actualValue)'." - } - else - { - if ($showValidSettings -eq $true) - { - Write-Verbose -Message "Test: '$($testName)'. Result: Pass. Value: '$($expectedValue)'." - } - } -} - -#Actually runs the specified test -function RunTest -{ - param([string]$TestName, [string[]]$ModulesToImport, [Hashtable]$Parameters) - - #Load Required Modules - foreach ($module in $ModulesToImport) - { - $modulePath = "..\DSCResources\$($module)\$($module).psm1" - Import-Module $modulePath - } - - DisableBitlocker - - Write-Verbose "Beginning test '$($TestName)'" - - if ($showVerbose -eq $true) - { - Set-TargetResource @Parameters -Verbose - - $getResult = Get-TargetResource @Parameters -Verbose - checkSetting -testName "$($TestName): Get" -expectedValue $true -actualValue ($null -ne $getResult) - - $testResult = Test-TargetResource @Parameters -Verbose - checkSetting -testName "$($TestName): Test" -expectedValue $true -actualValue $testResult - } - else - { - Set-TargetResource @Parameters - - $getResult = Get-TargetResource @Parameters - checkSetting -testName "$($TestName): Get" -expectedValue $true -actualValue ($null -ne $getResult) - - $testResult = Test-TargetResource @Parameters - checkSetting -testName "$($TestName): Test" -expectedValue $true -actualValue $testResult - } - - #Unload Required Modules - foreach ($module in $ModulesToImport) - { - Remove-Module $module - } -} - -#Runs any tests that match the filter -function RunTests -{ - param([string]$Filter) - - if ("TestBitlocker" -like $Filter) - { - RunTest -TestName "TestBitlocker1" -ModulesToImport "MSFT_xBLBitlocker" -Parameters $blParams1 - RunTest -TestName "TestBitlocker2" -ModulesToImport "MSFT_xBLBitlocker" -Parameters $blParams2 - RunTest -TestName "TestBitlocker7" -ModulesToImport "MSFT_xBLBitlocker" -Parameters $blParams7 - RunTest -TestName "TestBitlocker8" -ModulesToImport "MSFT_xBLBitlocker" -Parameters $blParams8 - RunTest -TestName "TestBitlocker9" -ModulesToImport "MSFT_xBLBitlocker" -Parameters $blParams9 - } - - if ("TestAutoBitlocker" -like $Filter) - { - RunTest -TestName "TestAutoBitlocker1" -ModulesToImport "MSFT_xBLAutoBitlocker" -Parameters $autoBlParams1 - } -} - -RunTests -Filter "TestBitlocker*" diff --git a/Tests/Integration/MSFT_xBLAutoBitlocker.Integration.tests.ps1 b/Tests/Integration/MSFT_xBLAutoBitlocker.Integration.tests.ps1 new file mode 100644 index 0000000..c978bff --- /dev/null +++ b/Tests/Integration/MSFT_xBLAutoBitlocker.Integration.tests.ps1 @@ -0,0 +1,114 @@ +$script:dscModuleName = 'xBitlocker' +$script:dscResourceFriendlyName = 'xBLAutoBitlocker' +$script:dcsResourceName = "MSFT_$($script:dscResourceFriendlyName)" + +#region HEADER +# Integration Test Template Version: 1.3.1 +[String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) +if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` + (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) +{ + & git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $script:moduleRoot -ChildPath 'DscResource.Tests')) +} + +Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force +$TestEnvironment = Initialize-TestEnvironment ` + -DSCModuleName $script:dscModuleName ` + -DSCResourceName $script:dcsResourceName ` + -TestType Integration + +# Import xBitlocker Test Helper Module +Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'Tests' -ChildPath (Join-Path -Path 'TestHelpers' -ChildPath 'xBitlockerTestHelper.psm1'))) -Force +#endregion + +# Make sure required features are installed before running tests +if (!(Test-RequiredFeaturesInstalled)) +{ + return +} + +# Make sure there are available Data disks to test AutoBitlocker on +$fixedDriveBlvs = Get-BitLockerVolume | Where-Object -FilterScript {$_.VolumeType -eq 'Data'} + +if ($null -eq $fixedDriveBlvs) +{ + Write-Warning -Message 'One or more Bitlocker volumes of type Data must be available. Skipping Integration tests.' + return +} + +# Disable Bitlocker on the Fixed drives before performing any tests +foreach ($fixedDriveBlv in $fixedDriveBlvs) +{ + if ($fixedDriveBlv.KeyProtector.Count -gt 0 -or $fixedDriveBlv.ProtectionStatus -ne 'Off') + { + Disable-BitLocker -MountPoint $fixedDriveBlv.MountPoint + } +} + +# Using try/finally to always cleanup. +try +{ + #region Integration Tests + $configurationFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:dcsResourceName).config.ps1" + . $configurationFile + + Describe "$($script:dcsResourceName)_Integration" { + $configurationName = "$($script:dcsResourceName)_EnablePasswordProtectorOnDataDrives_Config" + + Context ('When using configuration {0}' -f $configurationName) { + It 'Should compile and apply the MOF without throwing' { + { + $configurationParameters = @{ + OutputPath = $TestDrive + ConfigurationData = $ConfigurationData + } + + & $configurationName @configurationParameters + + $startDscConfigurationParameters = @{ + Path = $TestDrive + ComputerName = 'localhost' + Wait = $true + Verbose = $true + Force = $true + ErrorAction = 'Stop' + } + + Start-DscConfiguration @startDscConfigurationParameters + } | Should -Not -Throw + } + + It 'Should be able to call Get-DscConfiguration without throwing' { + { + $script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop + } | Should -Not -Throw + } + + It 'Should have set the resource and all the parameters should match' { + $resourceCurrentState = $script:currentConfiguration | Where-Object -FilterScript { + $_.ConfigurationName -eq $configurationName ` + -and $_.ResourceId -eq "[$($script:dscResourceFriendlyName)]Integration_Test" + } + + $fixedDriveBlvs = Get-BitLockerVolume | Where-Object -FilterScript {$_.VolumeType -eq 'Data'} + + foreach ($fixedDriveBlv in $fixedDriveBlvs) + { + $fixedDriveBlv.KeyProtector.Count | Should -BeGreaterThan 0 + } + } + + It 'Should return $true when Test-DscConfiguration is run' { + Test-DscConfiguration -Verbose | Should -Be $true + } + } + } + #endregion + +} +finally +{ + #region FOOTER + Restore-TestEnvironment -TestEnvironment $TestEnvironment + #endregion +} diff --git a/Tests/Integration/MSFT_xBLAutoBitlocker.config.ps1 b/Tests/Integration/MSFT_xBLAutoBitlocker.config.ps1 new file mode 100644 index 0000000..e930f73 --- /dev/null +++ b/Tests/Integration/MSFT_xBLAutoBitlocker.config.ps1 @@ -0,0 +1,43 @@ +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')] +param() + +#region HEADER +# Integration Test Config Template Version: 1.2.0 +#endregion + +$configFile = [System.IO.Path]::ChangeExtension($MyInvocation.MyCommand.Path, 'json') +if (Test-Path -Path $configFile) +{ + $ConfigurationData = Get-Content -Path $configFile | ConvertFrom-Json +} +else +{ + $ConfigurationData = @{ + AllNodes = @( + @{ + NodeName = 'localhost' + PsDscAllowPlainTextPassword = $true + } + ) + } +} + +<# + .SYNOPSIS + Enables Bitlocker on Fixed drives using a PasswordProtector +#> +Configuration MSFT_xBLAutoBitlocker_EnablePasswordProtectorOnDataDrives_Config +{ + Import-DscResource -ModuleName 'xBitlocker' + + Node $AllNodes.NodeName + { + xBLAutoBitlocker Integration_Test + { + DriveType = 'Fixed' + PrimaryProtector = 'PasswordProtector' + Password = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList 'AutoBitlocker', (ConvertTo-SecureString 'Password1' -AsPlainText -Force) + UsedSpaceOnly = $true + } + } +} diff --git a/Tests/Integration/MSFT_xBLBitlocker.Integration.tests.ps1 b/Tests/Integration/MSFT_xBLBitlocker.Integration.tests.ps1 new file mode 100644 index 0000000..b6ce04e --- /dev/null +++ b/Tests/Integration/MSFT_xBLBitlocker.Integration.tests.ps1 @@ -0,0 +1,105 @@ +$script:dscModuleName = 'xBitlocker' +$script:dscResourceFriendlyName = 'xBLBitlocker' +$script:dcsResourceName = "MSFT_$($script:dscResourceFriendlyName)" + +#region HEADER +# Integration Test Template Version: 1.3.1 +[String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) +if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` + (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) +{ + & git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $script:moduleRoot -ChildPath 'DscResource.Tests')) +} + +Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force +$TestEnvironment = Initialize-TestEnvironment ` + -DSCModuleName $script:dscModuleName ` + -DSCResourceName $script:dcsResourceName ` + -TestType Integration + +# Import xBitlocker Test Helper Module +Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'Tests' -ChildPath (Join-Path -Path 'TestHelpers' -ChildPath 'xBitlockerTestHelper.psm1'))) -Force +#endregion + +# Make sure the TPM is present before running tests +if (!(Test-HasPresentTpm)) +{ + return +} + +# Make sure required features are installed before running tests +if (!(Test-RequiredFeaturesInstalled)) +{ + return +} + +# Disable Bitlocker on the OS drive before performing any tests +$sysDriveBlv = Get-BitLockerVolume -MountPoint $env:SystemDrive + +if ($sysDriveBlv.KeyProtector.Count -gt 0 -or $sysDriveBlv.ProtectionStatus -ne 'Off') +{ + Disable-BitLocker -MountPoint $env:SystemDrive +} + +# Using try/finally to always cleanup. +try +{ + #region Integration Tests + $configurationFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:dcsResourceName).config.ps1" + . $configurationFile + + Describe "$($script:dcsResourceName)_Integration" { + $configurationName = "$($script:dcsResourceName)_BasicTPMEncryptionOnSysDrive_Config" + + Context ('When using configuration {0}' -f $configurationName) { + It 'Should compile and apply the MOF without throwing' { + { + $configurationParameters = @{ + OutputPath = $TestDrive + ConfigurationData = $ConfigurationData + } + + & $configurationName @configurationParameters + + $startDscConfigurationParameters = @{ + Path = $TestDrive + ComputerName = 'localhost' + Wait = $true + Verbose = $true + Force = $true + ErrorAction = 'Stop' + } + + Start-DscConfiguration @startDscConfigurationParameters + } | Should -Not -Throw + } + + It 'Should be able to call Get-DscConfiguration without throwing' { + { + $script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop + } | Should -Not -Throw + } + + It 'Should have set the resource and all the parameters should match' { + $resourceCurrentState = $script:currentConfiguration | Where-Object -FilterScript { + $_.ConfigurationName -eq $configurationName ` + -and $_.ResourceId -eq "[$($script:dscResourceFriendlyName)]Integration_Test" + } + + (Get-BitlockerVolume -MountPoint $env:SystemDrive).KeyProtector[0].KeyProtectorType | Should -Be 'Tpm' + } + + It 'Should return $true when Test-DscConfiguration is run' { + Test-DscConfiguration -Verbose | Should -Be $true + } + } + } + #endregion + +} +finally +{ + #region FOOTER + Restore-TestEnvironment -TestEnvironment $TestEnvironment + #endregion +} diff --git a/Tests/Integration/MSFT_xBLBitlocker.config.ps1 b/Tests/Integration/MSFT_xBLBitlocker.config.ps1 new file mode 100644 index 0000000..e14e19c --- /dev/null +++ b/Tests/Integration/MSFT_xBLBitlocker.config.ps1 @@ -0,0 +1,37 @@ +#region HEADER +# Integration Test Config Template Version: 1.2.0 +#endregion + +$configFile = [System.IO.Path]::ChangeExtension($MyInvocation.MyCommand.Path, 'json') +if (Test-Path -Path $configFile) +{ + $ConfigurationData = Get-Content -Path $configFile | ConvertFrom-Json +} +else +{ + $ConfigurationData = @{ + AllNodes = @( + @{ + NodeName = 'localhost' + } + ) + } +} + +<# + .SYNOPSIS + Enables Bitlocker on the Operating System drive using a TpmProtector +#> +Configuration MSFT_xBLBitlocker_BasicTPMEncryptionOnSysDrive_Config +{ + Import-DscResource -ModuleName 'xBitlocker' + + Node $AllNodes.NodeName + { + xBLBitlocker Integration_Test + { + MountPoint = $env:SystemDrive + PrimaryProtector = 'TpmProtector' + } + } +} diff --git a/Tests/Integration/MSFT_xBLTpm.Integration.tests.ps1 b/Tests/Integration/MSFT_xBLTpm.Integration.tests.ps1 new file mode 100644 index 0000000..c2c96f5 --- /dev/null +++ b/Tests/Integration/MSFT_xBLTpm.Integration.tests.ps1 @@ -0,0 +1,97 @@ +$script:dscModuleName = 'xBitlocker' +$script:dscResourceFriendlyName = 'xBLTpm' +$script:dcsResourceName = "MSFT_$($script:dscResourceFriendlyName)" + +#region HEADER +# Integration Test Template Version: 1.3.1 +[String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) +if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` + (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) +{ + & git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $script:moduleRoot -ChildPath 'DscResource.Tests')) +} + +Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force +$TestEnvironment = Initialize-TestEnvironment ` + -DSCModuleName $script:dscModuleName ` + -DSCResourceName $script:dcsResourceName ` + -TestType Integration + +# Import xBitlocker Test Helper Module +Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'Tests' -ChildPath (Join-Path -Path 'TestHelpers' -ChildPath 'xBitlockerTestHelper.psm1'))) -Force +#endregion + +# Make sure the TPM is present before running tests +if (!(Test-HasPresentTpm)) +{ + return +} + +# Make sure required features are installed before running tests +if (!(Test-RequiredFeaturesInstalled)) +{ + return +} + +# Using try/finally to always cleanup. +try +{ + #region Integration Tests + $configurationFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:dcsResourceName).config.ps1" + . $configurationFile + + Describe "$($script:dcsResourceName)_Integration" { + $configurationName = "$($script:dcsResourceName)_BasicTPMInitialization_Config" + + Context ('When using configuration {0}' -f $configurationName) { + It 'Should compile and apply the MOF without throwing' { + { + $configurationParameters = @{ + OutputPath = $TestDrive + ConfigurationData = $ConfigurationData + } + + & $configurationName @configurationParameters + + $startDscConfigurationParameters = @{ + Path = $TestDrive + ComputerName = 'localhost' + Wait = $true + Verbose = $true + Force = $true + ErrorAction = 'Stop' + } + + Start-DscConfiguration @startDscConfigurationParameters + } | Should -Not -Throw + } + + It 'Should be able to call Get-DscConfiguration without throwing' { + { + $script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop + } | Should -Not -Throw + } + + It 'Should have set the resource and all the parameters should match' { + $resourceCurrentState = $script:currentConfiguration | Where-Object -FilterScript { + $_.ConfigurationName -eq $configurationName ` + -and $_.ResourceId -eq "[$($script:dscResourceFriendlyName)]Integration_Test" + } + + (Get-Tpm).TpmReady | Should -Be $true + } + + It 'Should return $true when Test-DscConfiguration is run' { + Test-DscConfiguration -Verbose | Should -Be $true + } + } + } + #endregion + +} +finally +{ + #region FOOTER + Restore-TestEnvironment -TestEnvironment $TestEnvironment + #endregion +} diff --git a/Tests/Integration/MSFT_xBLTpm.config.ps1 b/Tests/Integration/MSFT_xBLTpm.config.ps1 new file mode 100644 index 0000000..3d737fe --- /dev/null +++ b/Tests/Integration/MSFT_xBLTpm.config.ps1 @@ -0,0 +1,36 @@ +#region HEADER +# Integration Test Config Template Version: 1.2.0 +#endregion + +$configFile = [System.IO.Path]::ChangeExtension($MyInvocation.MyCommand.Path, 'json') +if (Test-Path -Path $configFile) +{ + $ConfigurationData = Get-Content -Path $configFile | ConvertFrom-Json +} +else +{ + $ConfigurationData = @{ + AllNodes = @( + @{ + NodeName = 'localhost' + } + ) + } +} + +<# + .SYNOPSIS + Initializes a TPM chip on the test machine +#> +Configuration MSFT_xBLTpm_BasicTPMInitialization_Config +{ + Import-DscResource -ModuleName 'xBitlocker' + + Node $AllNodes.NodeName + { + xBLTpm Integration_Test + { + Identity = 'TPMTest' + } + } +} diff --git a/Tests/Integration/integration_template.config.ps1 b/Tests/Integration/integration_template.config.ps1 deleted file mode 100644 index bb36208..0000000 --- a/Tests/Integration/integration_template.config.ps1 +++ /dev/null @@ -1,26 +0,0 @@ -<# -.Synopsis - DSC Configuration Template for DSC Resource Integration tests. -.DESCRIPTION - To Use: - 1. Copy to \Tests\Integration\ folder and rename .config.ps1 (e.g. MSFT_Firewall.config.ps1) - 2. Customize TODO sections. - -.NOTES -#> - -# Integration Test Config Template Version: 1.0.1 - -# TODO: Modify ResourceName (e.g. MSFT_Firewall_config) -configuration _config { - # TODO: Modify ModuleName (e.g. NetworkingDsc) - Import-DscResource -ModuleName '' - node localhost { - # TODO: Modify ResourceFriendlyName (e.g. Firewall_Integration_Test) - Integration_Test { - # TODO: Fill Configuration Code Here - } - } -} - -# TODO: (Optional): Add More Configuration Templates diff --git a/Tests/Integration/integration_template.ps1 b/Tests/Integration/integration_template.ps1 deleted file mode 100644 index 346818a..0000000 --- a/Tests/Integration/integration_template.ps1 +++ /dev/null @@ -1,87 +0,0 @@ -<# -.Synopsis - Template for creating DSC Resource Integration Tests -.DESCRIPTION - To Use: - 1. Copy to \Tests\Integration\ folder and rename .Integration.tests.ps1 (e.g. MSFT_Firewall.Integration.tests.ps1) - 2. Customize TODO sections. - 3. Create test DSC Configuration file .config.ps1 (e.g. MSFT_Firewall.config.ps1) from integration_config_template.ps1 file. - -.NOTES - Code in HEADER, FOOTER and DEFAULT TEST regions are standard and may be moved into - DSCResource.Tools in Future and therefore should not be altered if possible. -#> - -# TODO: Customize these parameters... -$script:DSCModuleName = '' # Example NetworkingDsc -$script:DSCResourceName = '' # Example MSFT_Firewall - -#region HEADER -# Integration Test Template Version: 1.2.1 -[String] $script:moduleRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot) -if ( (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests'))) -or ` - (-not (Test-Path -Path (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests\TestHelper.psm1'))) ) -{ - & git @('clone', 'https://github.com/PowerShell/DscResource.Tests.git', (Join-Path -Path $script:moduleRoot -ChildPath 'DSCResource.Tests')) -} - -Import-Module -Name (Join-Path -Path $script:moduleRoot -ChildPath (Join-Path -Path 'DSCResource.Tests' -ChildPath 'TestHelper.psm1')) -Force -$TestEnvironment = Initialize-TestEnvironment ` - -DSCModuleName $script:DSCModuleName ` - -DSCResourceName $script:DSCResourceName ` - -TestType Integration - -#endregion - -# TODO: Other Init Code Goes Here... - -# Using try/finally to always cleanup. -try -{ - #region Integration Tests - $configFile = Join-Path -Path $PSScriptRoot -ChildPath "$($script:DSCResourceName).config.ps1" - . $configFile - - Describe "$($script:DSCResourceName)_Integration" { - #region DEFAULT TESTS - It 'Should compile and apply the MOF without throwing' { - { - & "$($script:DSCResourceName)_Config" -OutputPath $TestDrive - - $startDscConfigurationParameters = @{ - Path = $TestDrive - ComputerName = 'localhost' - Wait = $true - Verbose = $true - Force = $true - ErrorAction = 'Stop' - } - - Start-DscConfiguration @startDscConfigurationParameters - } | Should -Not -Throw - } - - It 'Should be able to call Get-DscConfiguration without throwing' { - { - Get-DscConfiguration -Verbose -ErrorAction Stop - } | Should -Not -Throw - } - #endregion - - It 'Should have set the resource and all the parameters should match' { - # TODO: Validate the Config was Set Correctly Here... - } - } - #endregion - -} -finally -{ - #region FOOTER - - Restore-TestEnvironment -TestEnvironment $TestEnvironment - - #endregion - - # TODO: Other Optional Cleanup Code Goes Here... -} diff --git a/Tests/TestHelpers/xBitlockerTestHelper.psm1 b/Tests/TestHelpers/xBitlockerTestHelper.psm1 new file mode 100644 index 0000000..43a48d0 --- /dev/null +++ b/Tests/TestHelpers/xBitlockerTestHelper.psm1 @@ -0,0 +1,44 @@ +<# + .SYNOPSIS + Checks whether the appropriate features are installed to be able to + test Bitlocker. +#> +function Test-RequiredFeaturesInstalled +{ + [CmdletBinding()] + [OutputType([System.Boolean])] + param() + + $featuresInstalled = $true + + if ((Get-WindowsFeature -Name 'BitLocker').InstallState -ne 'Installed' -or + (Get-WindowsFeature -Name 'RSAT-Feature-Tools-BitLocker').InstallState -ne 'Installed' -or + (Get-WindowsFeature -Name 'RSAT-Feature-Tools-BitLocker-RemoteAdminTool').InstallState -ne 'Installed') + { + Write-Warning -Message 'One or more of the following Windows Features are not installed: BitLocker, RSAT-Feature-Tools-BitLocker, RSAT-Feature-Tools-BitLocker-RemoteAdminTool. Skipping Integration tests.' + $featuresInstalled = $false + } + + return $featuresInstalled +} + +<# + .SYNOPSIS + Checks whether the system has a TPM chip. +#> +function Test-HasPresentTpm +{ + [CmdletBinding()] + [OutputType([System.Boolean])] + param() + + $hasReadyTpm = $true + + if ($null -eq (Get-Command -Name Get-Tpm -ErrorAction SilentlyContinue) -or !((Get-Tpm).TpmPresent)) + { + Write-Warning -Message 'No TPM is present on test machine. Skipping Integration tests.' + $hasReadyTpm = $false + } + + return $hasReadyTpm +}