diff --git a/README.md b/README.md index f5b5cd2..cef4a54 100644 --- a/README.md +++ b/README.md @@ -128,6 +128,7 @@ Defaults to false. * Add remaining Unit Tests for xBitlockerCommon. * Add Unit tests for MSFT_xBLTpm * Add remaining Unit Tests for xBLAutoBitlocker +* Add Unit tests for MSFT_xBLBitlocker ### 1.2.0.0 diff --git a/Tests/Unit/MSFT_xBLBitlocker.tests.ps1 b/Tests/Unit/MSFT_xBLBitlocker.tests.ps1 new file mode 100644 index 0000000..a6fbe08 --- /dev/null +++ b/Tests/Unit/MSFT_xBLBitlocker.tests.ps1 @@ -0,0 +1,104 @@ +#region HEADER +$script:DSCModuleName = 'xBitlocker' +$script:DSCResourceName = 'MSFT_xBLBitlocker' + +# Unit Test Template Version: 1.2.4 +$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 ` + -ResourceType 'Mof' ` + -TestType Unit + +#endregion HEADER + +function Invoke-TestSetup +{ + +} + +function Invoke-TestCleanup +{ + Restore-TestEnvironment -TestEnvironment $TestEnvironment +} + +# Begin Testing +try +{ + Invoke-TestSetup + + InModuleScope $script:DSCResourceName { + # Override helper functions + function CheckForPreReqs {} + + # Setup common test variables + $testMountPoint = 'C:' + $testPrimaryProtector = 'TpmProtector' + + # Setup common Mocks + Mock -CommandName CheckForPreReqs -Verifiable + + Describe 'MSFT_xBLBitlocker\Get-TargetResource' -Tag 'Get' { + AfterEach { + Assert-VerifiableMock + } + + Context 'When Get-TargetResource is called' { + It 'Should return a Hashtable with the input MountPoint' { + $getResult = Get-TargetResource -MountPoint $testMountPoint -PrimaryProtector $testPrimaryProtector + $getResult | Should -Be -Not $null + $getResult.MountPoint | Should -Be $testMountPoint + + } + } + } + + Describe 'MSFT_xBLBitlocker\Set-TargetResource' -Tag 'Set' { + AfterEach { + Assert-VerifiableMock + } + + Context 'When Set-TargetResource is called' { + It 'Should call EnableBitlocker' { + Mock -CommandName EnableBitlocker -Verifiable + + Set-TargetResource -MountPoint $testMountPoint -PrimaryProtector $testPrimaryProtector + } + } + } + + Describe 'MSFT_xBLBitlocker\Test-TargetResource' -Tag 'Test' { + AfterEach { + Assert-VerifiableMock + } + + Context 'When TestBitlocker returns True' { + It 'Should return True' { + Mock -CommandName TestBitlocker -Verifiable -MockWith { return $true } + + Test-TargetResource -MountPoint $testMountPoint -PrimaryProtector $testPrimaryProtector | Should -Be $true + } + } + + Context 'When TestBitlocker returns False' { + It 'Should return False' { + Mock -CommandName TestBitlocker -Verifiable -MockWith { return $false } + + Test-TargetResource -MountPoint $testMountPoint -PrimaryProtector $testPrimaryProtector | Should -Be $false + } + } + } + } +} +finally +{ + Invoke-TestCleanup +} diff --git a/Tests/Unit/unit_template.ps1 b/Tests/Unit/unit_template.ps1 deleted file mode 100644 index 368a2e3..0000000 --- a/Tests/Unit/unit_template.ps1 +++ /dev/null @@ -1,110 +0,0 @@ -<# - .SYNOPSIS - Template for creating DSC Resource Unit Tests - .DESCRIPTION - To Use: - 1. Copy to \Tests\Unit\ folder and rename .tests.ps1 (e.g. MSFT_xFirewall.tests.ps1) - 2. Customize TODO sections. - 3. Delete all template comments (TODOs, etc.) - - .NOTES - There are multiple methods for writing unit tests. This template provides a few examples - which you are welcome to follow but depending on your resource, you may want to - design it differently. Read through our TestsGuidelines.md file for an intro on how to - write unit tests for DSC resources: https://github.com/PowerShell/DscResources/blob/master/TestsGuidelines.md -#> - -#region HEADER - -# Unit Test Template Version: 1.2.1 -$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 - -# TODO: Insert the correct and for your resource -$TestEnvironment = Initialize-TestEnvironment ` - -DSCModuleName '' ` - -DSCResourceName '' ` - -TestType Unit - -#endregion HEADER - -function Invoke-TestSetup { - # TODO: Optional init code goes here... -} - -function Invoke-TestCleanup { - Restore-TestEnvironment -TestEnvironment $TestEnvironment - - # TODO: Other Optional Cleanup Code Goes Here... -} - -# Begin Testing -try -{ - Invoke-TestSetup - - InModuleScope '' { - # TODO: Optionally create any variables here for use by your tests - - # TODO: Complete the Describe blocks below and add more as needed. - # The most common method for unit testing is to test by function. For more information - # check out this introduction to writing unit tests in Pester: - # https://www.simple-talk.com/sysadmin/powershell/practical-powershell-unit-testing-getting-started/#eleventh - # You may also follow one of the patterns provided in the TestsGuidelines.md file: - # https://github.com/PowerShell/DscResources/blob/master/TestsGuidelines.md - - Describe '' { - BeforeEach { - # per-test-initialization - } - - AfterEach { - # per-test-cleanup - } - - Context 'Context-description' { - BeforeEach { - # per-test-initialization - } - - AfterEach { - # per-test-cleanup - } - - It 'Should...test-description' { - # test-code - } - - It 'Should...test-description' { - # test-code - } - } - - Context 'Context-description' { - It 'Should ....test-description' { - # test-code - } - } - } - - Describe '' { - Context '' { - It 'Should ...test-description' { - # test-code - } - } - } - - # TODO: add more Describe blocks as needed - } -} -finally -{ - Invoke-TestCleanup -}