diff --git a/.MetaTestOptIn.json b/.MetaTestOptIn.json index 6f385f87..a5f60a34 100644 --- a/.MetaTestOptIn.json +++ b/.MetaTestOptIn.json @@ -6,5 +6,8 @@ "Common Tests - Required Script Analyzer Rules", "Common Tests - Flagged Script Analyzer Rules", "Common Tests - New Error-Level Script Analyzer Rules", - "Common Tests - Custom Script Analyzer Rules" + "Common Tests - Custom Script Analyzer Rules", + "Common Tests - Validate Example Files To Be Published", + "Common Tests - Validate Markdown Links", + "Common Tests - Relative Path Length" ] diff --git a/.vscode/analyzersettings.psd1 b/.vscode/analyzersettings.psd1 new file mode 100644 index 00000000..be415e4d --- /dev/null +++ b/.vscode/analyzersettings.psd1 @@ -0,0 +1,53 @@ +@{ + <# + For the custom rules to work, the DscResource.Tests repo must be + cloned. It is automatically clone as soon as any unit or + integration tests are run. + #> + CustomRulePath = '.\DSCResource.Tests\DscResource.AnalyzerRules' + + IncludeRules = @( + # DSC Resource Kit style guideline rules. + 'PSAvoidDefaultValueForMandatoryParameter', + 'PSAvoidDefaultValueSwitchParameter', + 'PSAvoidInvokingEmptyMembers', + 'PSAvoidNullOrEmptyHelpMessageAttribute', + 'PSAvoidUsingCmdletAliases', + 'PSAvoidUsingComputerNameHardcoded', + 'PSAvoidUsingDeprecatedManifestFields', + 'PSAvoidUsingEmptyCatchBlock', + 'PSAvoidUsingInvokeExpression', + 'PSAvoidUsingPositionalParameters', + 'PSAvoidShouldContinueWithoutForce', + 'PSAvoidUsingWMICmdlet', + 'PSAvoidUsingWriteHost', + 'PSDSCReturnCorrectTypesForDSCFunctions', + 'PSDSCStandardDSCFunctionsInResource', + 'PSDSCUseIdenticalMandatoryParametersForDSC', + 'PSDSCUseIdenticalParametersForDSC', + 'PSMisleadingBacktick', + 'PSMissingModuleManifestField', + 'PSPossibleIncorrectComparisonWithNull', + 'PSProvideCommentHelp', + 'PSReservedCmdletChar', + 'PSReservedParams', + 'PSUseApprovedVerbs', + 'PSUseCmdletCorrectly', + 'PSUseOutputTypeCorrectly', + 'PSAvoidGlobalVars', + 'PSAvoidUsingConvertToSecureStringWithPlainText', + 'PSAvoidUsingPlainTextForPassword', + 'PSAvoidUsingUsernameAndPasswordParams', + 'PSDSCUseVerboseMessageInDSCResource', + 'PSShouldProcess', + 'PSUseDeclaredVarsMoreThanAssignments', + 'PSUsePSCredentialType', + + <# + This is to test all the DSC Resource Kit custom rules. + The name of the function-blocks of each custom rule start + with 'Measure*'. + #> + 'Measure-*' + ) +} diff --git a/.vscode/settings.json b/.vscode/settings.json index cd3d82e8..43f01cf9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,5 +10,6 @@ "powershell.codeFormatting.ignoreOneLineBlock": false, "powershell.codeFormatting.preset": "Custom", "files.trimTrailingWhitespace": true, - "files.insertFinalNewline": true + "files.insertFinalNewline": true, + "powershell.scriptAnalysis.settingsPath": ".vscode\\analyzersettings.psd1" } diff --git a/CHANGELOG.md b/CHANGELOG.md index b06ea0ce..8a6e77bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,23 @@ ## Unreleased +## 4.2.0.0 + +- Disk: + - Added `PartitionStyle` parameter - Fixes [Issue #137](https://github.com/PowerShell/StorageDsc/issues/37). + - Changed MOF name from `MSFT_Disk` to `MSFTDSC_Disk` to remove conflict + with Windows built-in CIM class - Fixes [Issue #167](https://github.com/PowerShell/StorageDsc/issues/167). +- Opt-in to Common Tests: + - Common Tests - Validate Example Files To Be Published + - Common Tests - Validate Markdown Links + - Common Tests - Relative Path Length +- Added .VSCode settings for applying DSC PSSA rules - fixes [Issue #168](https://github.com/PowerShell/StorageDsc/issues/168). +- Disk: + - Added 'defragsvc' service conflict known issue to README.MD - fixes + [Issue #172](https://github.com/PowerShell/StorageDsc/issues/172). +- Corrected style violations in StorageDsc.Common module - fixes [Issue #153](https://github.com/PowerShell/StorageDsc/issues/153). +- Corrected style violations in StorageDsc.ResourceHelper module. + ## 4.1.0.0 - Enabled PSSA rule violations to fail build - Fixes [Issue #149](https://github.com/PowerShell/StorageDsc/issues/149). diff --git a/Modules/StorageDsc/DSCResources/MSFT_Disk/MSFT_Disk.psm1 b/Modules/StorageDsc/DSCResources/MSFTDSC_Disk/MSFTDSC_Disk.psm1 similarity index 84% rename from Modules/StorageDsc/DSCResources/MSFT_Disk/MSFT_Disk.psm1 rename to Modules/StorageDsc/DSCResources/MSFTDSC_Disk/MSFTDSC_Disk.psm1 index 1324aec7..7f18dfd0 100644 --- a/Modules/StorageDsc/DSCResources/MSFT_Disk/MSFT_Disk.psm1 +++ b/Modules/StorageDsc/DSCResources/MSFTDSC_Disk/MSFTDSC_Disk.psm1 @@ -17,7 +17,7 @@ Import-Module -Name (Join-Path -Path $modulePath ` # Import Localization Strings $localizedData = Get-LocalizedData ` - -ResourceName 'MSFT_Disk' ` + -ResourceName 'MSFTDSC_Disk' ` -ResourcePath (Split-Path -Parent $Script:MyInvocation.MyCommand.Path) <# @@ -33,23 +33,34 @@ $localizedData = Get-LocalizedData ` .PARAMETER DiskIdType Specifies the identifier type the DiskId contains. Defaults to Number. + .PARAMETER PartitionStyle + Specifies the partition style of the disk. Defaults to GPT. + This parameter is not used in Get-TargetResource. + .PARAMETER Size Specifies the size of new volume (use all available space on disk if not provided). + This parameter is not used in Get-TargetResource. .PARAMETER FSLabel Specifies the volume label to assign to the volume. + This parameter is not used in Get-TargetResource. .PARAMETER AllocationUnitSize Specifies the allocation unit size to use when formatting the volume. + This parameter is not used in Get-TargetResource. .PARAMETER FSFormat Specifies the file system format of the new volume. + This parameter is not used in Get-TargetResource. .PARAMETER AllowDestructive - Specifies if potentially destructive operations may occur + Specifies if potentially destructive operations may occur. + This parameter is not used in Get-TargetResource. .PARAMETER ClearDisk - Specifies if the disks partition schema should be removed entirely, even if data and oem partitions are present. Only possible with AllowDestructive enabled. + Specifies if the disks partition schema should be removed entirely, even if data and OEM + partitions are present. Only possible with AllowDestructive enabled. + This parameter is not used in Get-TargetResource. #> function Get-TargetResource { @@ -70,6 +81,11 @@ function Get-TargetResource [System.String] $DiskIdType = 'Number', + [Parameter()] + [ValidateSet('GPT', 'MBR')] + [System.String] + $PartitionStyle = 'GPT', + [Parameter()] [System.UInt64] $Size, @@ -117,24 +133,20 @@ function Get-TargetResource -DriveLetter $DriveLetter ` -ErrorAction SilentlyContinue - $fileSystem = $volume.FileSystem - $FSLabel = $volume.FileSystemLabel - $blockSize = (Get-CimInstance ` -Query "SELECT BlockSize from Win32_Volume WHERE DriveLetter = '$($DriveLetter):'" ` -ErrorAction SilentlyContinue).BlockSize - $returnValue = @{ + return @{ DiskId = $DiskId DiskIdType = $DiskIdType DriveLetter = $partition.DriveLetter + PartitionStyle = $disk.PartitionStyle Size = $partition.Size - FSLabel = $FSLabel + FSLabel = $volume.FileSystemLabel AllocationUnitSize = $blockSize - FSFormat = $fileSystem + FSFormat = $volume.FileSystem } - - $returnValue } # Get-TargetResource <# @@ -150,6 +162,9 @@ function Get-TargetResource .PARAMETER DiskIdType Specifies the identifier type the DiskId contains. Defaults to Number. + .PARAMETER PartitionStyle + Specifies the partition style of the disk. Defaults to GPT. + .PARAMETER Size Specifies the size of new volume. Leave empty to use the remaining free space. @@ -163,10 +178,11 @@ function Get-TargetResource Specifies the file system format of the new volume. .PARAMETER AllowDestructive - Specifies if potentially destructive operations may occur + Specifies if potentially destructive operations may occur. .PARAMETER ClearDisk - Specifies if the disks partition schema should be removed entirely, even if data and oem partitions are present. Only possible with AllowDestructive enabled. + Specifies if the disks partition schema should be removed entirely, even if data and OEM + partitions are present. Only possible with AllowDestructive enabled. #> function Set-TargetResource { @@ -188,6 +204,11 @@ function Set-TargetResource [System.String] $DiskIdType = 'Number', + [Parameter()] + [ValidateSet('GPT', 'MBR')] + [System.String] + $PartitionStyle = 'GPT', + [Parameter()] [System.UInt64] $Size, @@ -269,41 +290,35 @@ function Set-TargetResource -DiskIdType $DiskIdType } - switch ($disk.PartitionStyle) + if ($disk.PartitionStyle -eq 'RAW') { - 'RAW' - { - # The disk partition table is not yet initialized, so initialize it with GPT - Write-Verbose -Message ( @( - "$($MyInvocation.MyCommand): " - $($localizedData.InitializingDiskMessage -f $DiskIdType, $DiskId) - ) -join '' ) - - $disk | Initialize-Disk ` - -PartitionStyle 'GPT' - - break - } # 'RAW' + Write-Verbose -Message ( @( + "$($MyInvocation.MyCommand): " + $($localizedData.InitializingDiskMessage -f $DiskIdType, $DiskId, $PartitionStyle) + ) -join '' ) - 'GPT' + $disk | Initialize-Disk -PartitionStyle $PartitionStyle + } + else + { + if ($disk.PartitionStyle -eq $PartitionStyle) { - # The disk partition is already initialized with GPT. + # The disk partition is already initialized with the correct partition style Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " - $($localizedData.DiskAlreadyInitializedMessage -f $DiskIdType, $DiskId) + $($localizedData.DiskAlreadyInitializedMessage ` + -f $DiskIdType, $DiskId, $disk.PartitionStyle) ) -join '' ) - break - } # 'GPT' - - default + } + else { - # This disk is initialized but not as GPT - so raise an exception. + # This disk is initialized but with the incorrect partition style New-InvalidOperationException ` - -Message ($localizedData.DiskAlreadyInitializedError -f ` - $DiskIdType, $DiskId, $Disk.PartitionStyle) - } # default - } # switch + -Message ($localizedData.DiskInitializedWithWrongPartitionStyleError ` + -f $DiskIdType, $DiskId, $disk.PartitionStyle, $PartitionStyle) + } + } # Get the partitions on the disk $partition = $disk | Get-Partition -ErrorAction SilentlyContinue @@ -318,7 +333,8 @@ function Set-TargetResource # There is no partiton with this drive letter Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " - $($localizedData.DriveNotFoundOnPartitionMessage -f $DiskIdType, $DiskId, $DriveLetter) + $($localizedData.DriveNotFoundOnPartitionMessage ` + -f $DiskIdType, $DiskId, $DriveLetter) ) -join '' ) # Are there any partitions defined on this disk? @@ -335,14 +351,14 @@ function Set-TargetResource if ($partition) { # A partition matching the required size was found - Write-Verbose -Message ($localizedData.MatchingPartitionFoundMessage -f ` - $DiskIdType, $DiskId, $partition.PartitionNumber) + Write-Verbose -Message ($localizedData.MatchingPartitionFoundMessage ` + -f $DiskIdType, $DiskId, $partition.PartitionNumber) } else { # A partition matching the required size was not found - Write-Verbose -Message ($localizedData.MatchingPartitionNotFoundMessage -f ` - $DiskIdType, $DiskId) + Write-Verbose -Message ($localizedData.MatchingPartitionNotFoundMessage ` + -f $DiskIdType, $DiskId) } # if } else @@ -351,8 +367,8 @@ function Set-TargetResource No size specified, so see if there is a partition that has a volume matching the file system type that is not assigned to a drive letter. #> - Write-Verbose -Message ($localizedData.MatchingPartitionNoSizeMessage -f ` - $DiskIdType, $DiskId) + Write-Verbose -Message ($localizedData.MatchingPartitionNoSizeMessage ` + -f $DiskIdType, $DiskId) $searchPartitions = $partition | Where-Object -FilterScript { $_.Type -eq 'Basic' -and -not [System.Char]::IsLetter($_.DriveLetter) @@ -363,8 +379,8 @@ function Set-TargetResource foreach ($searchPartition in $searchPartitions) { # Look for the volume in the partition. - Write-Verbose -Message ($localizedData.SearchForVolumeMessage -f ` - $DiskIdType, $DiskId, $searchPartition.PartitionNumber, $FSFormat) + Write-Verbose -Message ($localizedData.SearchForVolumeMessage ` + -f $DiskIdType, $DiskId, $searchPartition.PartitionNumber, $FSFormat) $searchVolumes = $searchPartition | Get-Volume @@ -380,8 +396,8 @@ function Set-TargetResource #> $partition = $searchPartition - Write-Verbose -Message ($localizedData.VolumeFoundMessage -f ` - $DiskIdType, $DiskId, $searchPartition.PartitionNumber, $FSFormat) + Write-Verbose -Message ($localizedData.VolumeFoundMessage ` + -f $DiskIdType, $DiskId, $searchPartition.PartitionNumber, $FSFormat) break } # if @@ -433,7 +449,7 @@ function Set-TargetResource Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " ($localizedData.NewPartitionIsReadOnlyMessage ` - -f $DiskIdType, $DiskId, $partition.PartitionNumber) + -f $DiskIdType, $DiskId, $partition.PartitionNumber) ) -join '' ) Start-Sleep -Seconds 1 @@ -447,8 +463,8 @@ function Set-TargetResource { # The partition is still readonly - throw an exception New-InvalidOperationException ` - -Message ($localizedData.NewParitionIsReadOnlyError -f ` - $DiskIdType, $DiskId, $partition.PartitionNumber) + -Message ($localizedData.NewParitionIsReadOnlyError ` + -f $DiskIdType, $DiskId, $partition.PartitionNumber) } # if $assignDriveLetter = $true @@ -458,8 +474,8 @@ function Set-TargetResource # The disk already has a partition on it that is assigned to the Drive Letter Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " - $($localizedData.PartitionAlreadyAssignedMessage -f ` - $DriveLetter, $assignedPartition.PartitionNumber) + $($localizedData.PartitionAlreadyAssignedMessage ` + -f $DriveLetter, $assignedPartition.PartitionNumber) ) -join '' ) $assignDriveLetter = $false @@ -485,7 +501,7 @@ function Set-TargetResource Write-Warning -Message ( @( "$($MyInvocation.MyCommand): " $($localizedData.ResizeRefsNotPossibleMessage ` - -f $DriveLetter, $assignedPartition.Size, $Size) + -f $DriveLetter, $assignedPartition.Size, $Size) ) -join '' ) } @@ -494,7 +510,7 @@ function Set-TargetResource Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($localizedData.SizeMismatchCorrectionMessage ` - -f $DriveLetter, $assignedPartition.Size, $Size) + -f $DriveLetter, $assignedPartition.Size, $Size) ) -join '' ) if ($Size -gt $supportedSize.SizeMax) @@ -565,16 +581,16 @@ function Set-TargetResource # The file system format does not match Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " - $($localizedData.FileSystemFormatMismatch -f ` - $DriveLetter, $fileSystem, $FSFormat) + $($localizedData.FileSystemFormatMismatch ` + -f $DriveLetter, $fileSystem, $FSFormat) ) -join '' ) if ($AllowDestructive) { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " - $($localizedData.VolumeFormatInProgressMessage -f ` - $DriveLetter, $fileSystem, $FSFormat) + $($localizedData.VolumeFormatInProgressMessage ` + -f $DriveLetter, $fileSystem, $FSFormat) ) -join '' ) $formatParam = @{ @@ -602,7 +618,7 @@ function Set-TargetResource Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($localizedData.ChangingVolumeLabelMessage ` - -f $DriveLetter, $FSLabel) + -f $DriveLetter, $FSLabel) ) -join '' ) $volume | Set-Volume -NewFileSystemLabel $FSLabel @@ -635,6 +651,9 @@ function Set-TargetResource .PARAMETER DiskIdType Specifies the identifier type the DiskId contains. Defaults to Number. + .PARAMETER PartitionStyle + Specifies the partition style of the disk. Defaults to GPT. + .PARAMETER Size Specifies the size of new volume. Leave empty to use the remaining free space. @@ -648,10 +667,11 @@ function Set-TargetResource Specifies the file system format of the new volume. .PARAMETER AllowDestructive - Specifies if potentially destructive operations may occur + Specifies if potentially destructive operations may occur. .PARAMETER ClearDisk - Specifies if the disks partition schema should be removed entirely, even if data and oem partitions are present. Only possible with AllowDestructive enabled. + Specifies if the disks partition schema should be removed entirely, even if data and OEM + partitions are present. Only possible with AllowDestructive enabled. #> function Test-TargetResource { @@ -672,6 +692,11 @@ function Test-TargetResource [System.String] $DiskIdType = 'Number', + [Parameter()] + [ValidateSet('GPT', 'MBR')] + [System.String] + $PartitionStyle = 'GPT', + [Parameter()] [System.UInt64] $Size, @@ -740,20 +765,32 @@ function Test-TargetResource { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " - $($localizedData.DiskReadOnlyMessage -f $DiskIdType, $DiskId) + $($localizedData.DiskReadOnlyMessage ` + -f $DiskIdType, $DiskId) ) -join '' ) return $false } # if - if ($disk.PartitionStyle -ne 'GPT') + if ($disk.PartitionStyle -ne $PartitionStyle) { Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " - $($localizedData.DiskNotGPTMessage -f $DiskIdType, $DiskId, $Disk.PartitionStyle) + $($localizedData.DiskPartitionStyleNotMatchMessage ` + -f $DiskIdType, $DiskId, $disk.PartitionStyle, $PartitionStyle) ) -join '' ) - return $false + if ($disk.PartitionStyle -eq 'RAW' -or ($AllowDestructive -and $ClearDisk)) + { + return $false + } + else + { + # This disk is initialized but with the incorrect partition style + New-InvalidOperationException ` + -Message ($localizedData.DiskInitializedWithWrongPartitionStyleError ` + -f $DiskIdType, $DiskId, $disk.PartitionStyle, $PartitionStyle) + } } # if $partition = Get-Partition ` @@ -840,7 +877,7 @@ function Test-TargetResource Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($localizedData.FileSystemFormatMismatch ` - -f $DriveLetter, $fileSystem, $FSFormat) + -f $DriveLetter, $fileSystem, $FSFormat) ) -join '' ) if ($AllowDestructive) @@ -860,7 +897,7 @@ function Test-TargetResource Write-Verbose -Message ( @( "$($MyInvocation.MyCommand): " $($localizedData.DriveLabelMismatch ` - -f $DriveLetter, $label, $FSLabel) + -f $DriveLetter, $label, $FSLabel) ) -join '' ) return $false diff --git a/Modules/StorageDsc/DSCResources/MSFT_Disk/MSFT_Disk.schema.mof b/Modules/StorageDsc/DSCResources/MSFTDSC_Disk/MSFTDSC_Disk.schema.mof similarity index 76% rename from Modules/StorageDsc/DSCResources/MSFT_Disk/MSFT_Disk.schema.mof rename to Modules/StorageDsc/DSCResources/MSFTDSC_Disk/MSFTDSC_Disk.schema.mof index 0b0c77aa..5ab930f7 100644 --- a/Modules/StorageDsc/DSCResources/MSFT_Disk/MSFT_Disk.schema.mof +++ b/Modules/StorageDsc/DSCResources/MSFTDSC_Disk/MSFTDSC_Disk.schema.mof @@ -1,14 +1,16 @@ [ClassVersion("1.0.0.0"), FriendlyName("Disk")] -class MSFT_Disk : OMI_BaseResource +// This resource uses the MSFTDSC prefix to prevent conflict with built in MSFT_Disk CIM class. +class MSFTDSC_Disk : OMI_BaseResource { [Key, Description("Specifies the identifier for which disk to modify.")] String DriveLetter; [Required, Description("Specifies the disk identifier for the disk to modify.")] String DiskId; [Write, Description("Specifies the identifier type the DiskId contains. Defaults to Number."), ValueMap{"Number","UniqueId","Guid"}, Values{"Number","UniqueId","Guid"}] String DiskIdType; + [Write, Description("Specifies the partition style of the disk. Defaults to GPT."), ValueMap{"MBR","GPT"}, Values{"MBR","GPT"}] String PartitionStyle; [Write, Description("Specifies the size of new volume. Leave empty to use the remaining free space.")] Uint64 Size; [Write, Description("Define volume label if required.")] String FSLabel; [Write, Description("Specifies the allocation unit size to use when formatting the volume.")] Uint32 AllocationUnitSize; [Write, Description("Specifies the file system format of the new volume."), ValueMap{"NTFS","ReFS"}, Values{"NTFS","ReFS"}] String FSFormat; [Write, Description("Specifies if potentially destructive operations may occur.")] Boolean AllowDestructive; - [Write, Description("Specifies if the disks partition schema should be removed entirely, even if data and oem partitions are present. Only possible with AllowDestructive enabled.")] Boolean ClearDisk; + [Write, Description("Specifies if the disks partition schema should be removed entirely, even if data and OEM partitions are present. Only possible with AllowDestructive enabled.")] Boolean ClearDisk; }; diff --git a/Modules/StorageDsc/DSCResources/MSFTDSC_Disk/README.md b/Modules/StorageDsc/DSCResources/MSFTDSC_Disk/README.md new file mode 100644 index 00000000..501f3bfc --- /dev/null +++ b/Modules/StorageDsc/DSCResources/MSFTDSC_Disk/README.md @@ -0,0 +1,42 @@ +# Description + +The resource is used to initialize, format and mount the partition/volume as a drive +letter. +The disk to add the partition/volume to is selected by specifying the _DiskId_ and +optionally _DiskIdType_. +The _DiskId_ value can be a _Disk Number_, _Unique Id_ or _Guid_. + +**Important: The _Disk Number_ is not a reliable method of selecting a disk because +it has been shown to change between reboots in some environments. +It is recommended to use the _Unique Id_ if possible.** + +The _Disk Number_, _Unique Id_ and _Guid_ can be identified for a disk by using the +PowerShell command: + +```powershell +Get-Disk | Select-Object -Property FriendlyName,DiskNumber,UniqueId,Guid +``` + +Note: The _Guid_ identifier method of specifying disks is only supported as an +identifier for disks with `GPT` partition table format. If the disk is `RAW` +(e.g. the disk has been initialized) then the _Guid_ identifier method can not +be used. This is because the _Guid_ for a disk is only assigned once the partition +table for the disk has been created. + +## Known Issues + +The 'defragsvc' service ('Optimize Drives') may cause the following errors when +enabled with this resource. The following error may occur when testing the state +of the resource: + +```text +PartitionSupportedSize ++ CategoryInfo : NotSpecified: (StorageWMI:) [], CimException ++ FullyQualifiedErrorId : StorageWMI 4,Get-PartitionSupportedSize ++ PSComputerName : localhost +``` + +The 'defragsvc' service should be stopped and set to manual start up to prevent +this error. Use the `Service` resource in either the 'xPSDesiredStateConfgiuration' +or 'PSDSCResources' resource module to set the 'defragsvc' service is always +stopped and set to manual start up. diff --git a/Modules/StorageDsc/DSCResources/MSFT_Disk/en-us/MSFT_Disk.strings.psd1 b/Modules/StorageDsc/DSCResources/MSFTDSC_Disk/en-us/MSFTDSC_Disk.strings.psd1 similarity index 87% rename from Modules/StorageDsc/DSCResources/MSFT_Disk/en-us/MSFT_Disk.strings.psd1 rename to Modules/StorageDsc/DSCResources/MSFTDSC_Disk/en-us/MSFTDSC_Disk.strings.psd1 index afcee61b..cbdd331b 100644 --- a/Modules/StorageDsc/DSCResources/MSFT_Disk/en-us/MSFT_Disk.strings.psd1 +++ b/Modules/StorageDsc/DSCResources/MSFTDSC_Disk/en-us/MSFTDSC_Disk.strings.psd1 @@ -4,8 +4,8 @@ SetDiskOnlineMessage = Setting disk with {0} '{1}' online. SetDiskReadWriteMessage = Setting disk with {0} '{1}' to read/write. CheckingDiskPartitionStyleMessage = Checking disk with {0} '{1}' partition style. - InitializingDiskMessage = Initializing disk with {0} '{1}'. - DiskAlreadyInitializedMessage = Disk with {0} '{1}' is already initialized with GPT. + InitializingDiskMessage = Initializing disk with {0} '{1}' as '{2}'. + DiskAlreadyInitializedMessage = Disk with {0} '{1}' is already initialized with '{2}'. CreatingPartitionMessage = Creating partition on disk with {0} '{1}' with drive letter '{2}' using {3}. FormattingVolumeMessage = Formatting the volume as '{0}'. SuccessfullyInitializedMessage = Successfully initialized '{0}'. @@ -18,9 +18,10 @@ DiskNotFoundMessage = Disk with {0} '{1}' was not found. DiskNotOnlineMessage = Disk with {0} '{1}' is not online. DiskReadOnlyMessage = Disk with {0} '{1}' is readonly. - DiskNotGPTMessage = Disk with {0} '{1}' is initialized with '{2}' partition style. GPT required. + DiskPartitionStyleNotMatchMessage = Disk with {0} '{1}' is initialized with partition style '{2}' but '{3}' is required. + DiskInitializedWithWrongPartitionStyleError = Disk with {0} '{1}' is already initialized with partition style '{2}' but '{3}' is required. Set AllowDestructive and ClearDisk to $true to allow disk to be reinitialized. DriveLetterNotFoundMessage = Drive {0} was not found. - SizeMismatchMessage = Partition assigned to drive {0} has size {1}, which does not match expected size {2}. Set AllowDestructive to 'True' to enable resizing of partition. + SizeMismatchMessage = Partition assigned to drive {0} has size {1}, which does not match expected size {2}. Set AllowDestructive to $true to enable resizing of partition. SizeMismatchWithAllowDestructiveMessage = Partition assigned to drive {0} has size {1}, which does not match expected size {2}. AllocationUnitSizeMismatchMessage = Volume assigned to drive {0} has allocation unit size {1} KB does not match expected allocation unit size {2} KB. FileSystemFormatMismatch = Volume assigned to drive {0} filesystem format '{1}' does not match expected format '{2}'. @@ -33,11 +34,10 @@ MatchingPartitionFoundMessage = Disk with {0} '{1}' already contains partitions, and partition '{2}' matches required size. DriveNotFoundOnPartitionMessage = Disk with {0} '{1}' does not contain a partition assigned to drive letter '{2}'. ClearingDiskMessage = Clearing disk with {0} '{1}' of all existing partitions and volumes. - DiskAlreadyInitializedError = Disk with {0} '{1}' is already initialized with {2}. NewParitionIsReadOnlyError = New partition '{2}' on disk with {0} '{1}' did not become writable in the expected time. VolumeFormatInProgressMessage = Switch AllowDestructive is specified. Attempting to format volume on {0} with '{2}', was '{1}'. SizeMismatchCorrectionMessage = Switch AllowDestructive is specified. Attempting to resize partition {0} from {1} to {2}. FreeSpaceViolationError = Attempted to resize partition {0} from {1} to {2} while maximum allowed size was {3}. ResizeRefsNotPossibleMessage = Skipping resize of {0} from {1} to {2}. Resizing ReFS partitions is currently not possible. - ResizeNotAllowedMessage = Skipping resize of {0} from {1} to {2}. AllowDestructive is not set to 'True'. + ResizeNotAllowedMessage = Skipping resize of {0} from {1} to {2}. AllowDestructive is not set to $true. '@ diff --git a/Modules/StorageDsc/DSCResources/MSFT_Disk/README.md b/Modules/StorageDsc/DSCResources/MSFT_Disk/README.md deleted file mode 100644 index 523a7ce6..00000000 --- a/Modules/StorageDsc/DSCResources/MSFT_Disk/README.md +++ /dev/null @@ -1,22 +0,0 @@ -# Description - -The resource is used to initialize, format and mount the partition/volume as a drive -letter. -The disk to add the partition/volume to is selected by specifying the _DiskId_ and -optionally _DiskIdType_. -The _DiskId_ value can be a _Disk Number_, _Unique Id_ or _Guid_. - -**Important: The _Disk Number_ is not a reliable method of selecting a disk because -it has been shown to change between reboots in some environments. -It is recommended to use the _Unique Id_ if possible.** - -The _Disk Number_, _Unique Id_ and _Guid_ can be identified for a disk by using the -PowerShell command: - -```powershell -Get-Disk | Select-Object -Property FriendlyName,DiskNumber,UniqueId,Guid -``` - -Note: The _Guid_ for a disk is only assigned once the partition table for the disk -has been created (e.g. the disk has been initialized). Therefore to use this method -of disk selection the disk must have been initialized by some other method. diff --git a/Modules/StorageDsc/Modules/StorageDsc.Common/StorageDsc.Common.psm1 b/Modules/StorageDsc/Modules/StorageDsc.Common/StorageDsc.Common.psm1 index bf77ee7b..ed65249e 100644 --- a/Modules/StorageDsc/Modules/StorageDsc.Common/StorageDsc.Common.psm1 +++ b/Modules/StorageDsc/Modules/StorageDsc.Common/StorageDsc.Common.psm1 @@ -21,14 +21,15 @@ $localizedData = Get-LocalizedData ` function Assert-DriveLetterValid { [CmdletBinding()] - [OutputType([String])] + [OutputType([System.String])] param ( - [Parameter(Mandatory)] + [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $DriveLetter, + [Parameter()] [Switch] $Colon ) @@ -69,14 +70,15 @@ function Assert-DriveLetterValid function Assert-AccessPathValid { [CmdletBinding()] - [OutputType([String])] + [OutputType([System.String])] param ( - [Parameter(Mandatory)] + [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] [System.String] $AccessPath, + [Parameter()] [Switch] $Slash ) @@ -139,8 +141,7 @@ function Get-DiskByIdentifier if ($DiskIdType -eq 'Guid') { # The Disk Id requested uses a Guid so have to get all disks and filter - $disk = Get-Disk ` - -ErrorAction SilentlyContinue | + $disk = Get-Disk -ErrorAction SilentlyContinue | Where-Object -Property Guid -EQ $DiskId } else @@ -177,6 +178,7 @@ function Test-AccessPathAssignedToLocal ) $accessPathAssigned = $false + foreach ($path in $AccessPath) { if ($path -match '[a-zA-Z]:\\') diff --git a/Modules/StorageDsc/Modules/StorageDsc.ResourceHelper/StorageDsc.ResourceHelper.psm1 b/Modules/StorageDsc/Modules/StorageDsc.ResourceHelper/StorageDsc.ResourceHelper.psm1 index 5cb0d29a..bfd7f243 100644 --- a/Modules/StorageDsc/Modules/StorageDsc.ResourceHelper/StorageDsc.ResourceHelper.psm1 +++ b/Modules/StorageDsc/Modules/StorageDsc.ResourceHelper/StorageDsc.ResourceHelper.psm1 @@ -21,12 +21,18 @@ function Test-IsNanoServer <# .SYNOPSIS Tests if the the specified command is found. + + .PARAMETER Name + The name of the command to find. + #> function Test-Command { param ( - [String] $Name + [Parameter(Mandatory = $true)] + [System.String] + $Name ) return ($null -ne (Get-Command -Name $Name -ErrorAction Continue 2> $null)) @@ -49,21 +55,30 @@ function New-InvalidArgumentException ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $Message, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] + $ArgumentName + ) + + $argumentException = New-Object -TypeName 'ArgumentException' -ArgumentList @( + $Message $ArgumentName ) - $argumentException = New-Object -TypeName 'ArgumentException' -ArgumentList @( $Message, - $ArgumentName ) $newObjectParams = @{ TypeName = 'System.Management.Automation.ErrorRecord' - ArgumentList = @( $argumentException, $ArgumentName, 'InvalidArgument', $null ) + ArgumentList = @( + $argumentException + $ArgumentName + 'InvalidArgument' + $null + ) } + $errorRecord = New-Object @newObjectParams throw $errorRecord @@ -85,7 +100,7 @@ function New-InvalidOperationException param ( [ValidateNotNullOrEmpty()] - [String] + [System.String] $Message, [ValidateNotNull()] @@ -100,20 +115,29 @@ function New-InvalidOperationException elseif ($null -eq $ErrorRecord) { $invalidOperationException = - New-Object -TypeName 'InvalidOperationException' -ArgumentList @( $Message ) + New-Object -TypeName 'InvalidOperationException' -ArgumentList @( + $Message + ) } else { $invalidOperationException = - New-Object -TypeName 'InvalidOperationException' -ArgumentList @( $Message, - $ErrorRecord.Exception ) + New-Object -TypeName 'InvalidOperationException' -ArgumentList @( + $Message + $ErrorRecord.Exception + ) } $newObjectParams = @{ TypeName = 'System.Management.Automation.ErrorRecord' - ArgumentList = @( $invalidOperationException.ToString(), 'MachineStateIncorrect', - 'InvalidOperation', $null ) + ArgumentList = @( + $invalidOperationException.ToString() + 'MachineStateIncorrect' + 'InvalidOperation' + $null + ) } + $errorRecordToThrow = New-Object @newObjectParams throw $errorRecordToThrow } @@ -141,12 +165,12 @@ function Get-LocalizedData ( [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $ResourceName, [Parameter(Mandatory = $true)] [ValidateNotNullOrEmpty()] - [String] + [System.String] $ResourcePath ) @@ -166,5 +190,8 @@ function Get-LocalizedData return $localizedData } -Export-ModuleMember -Function @( 'Test-IsNanoServer', 'New-InvalidArgumentException', - 'New-InvalidOperationException', 'Get-LocalizedData' ) +Export-ModuleMember -Function ` + Test-IsNanoServer, ` + New-InvalidArgumentException, ` + New-InvalidOperationException, ` + Get-LocalizedData diff --git a/Modules/StorageDsc/StorageDsc.psd1 b/Modules/StorageDsc/StorageDsc.psd1 index 4709f1c4..812fc476 100644 --- a/Modules/StorageDsc/StorageDsc.psd1 +++ b/Modules/StorageDsc/StorageDsc.psd1 @@ -10,7 +10,7 @@ # RootModule = '' # Version number of this module. -moduleVersion = '4.1.0.0' +moduleVersion = '4.2.0.0' # ID used to uniquely identify this module GUID = '00d73ca1-58b5-46b7-ac1a-5bfcf5814faf' @@ -102,18 +102,20 @@ PrivateData = @{ # IconUri = '' # ReleaseNotes of this module - ReleaseNotes = '- Enabled PSSA rule violations to fail build - Fixes [Issue 149](https://github.com/PowerShell/StorageDsc/issues/149). -- Fixed markdown rule violations in CHANGELOG.MD. + ReleaseNotes = '- Disk: + - Added `PartitionStyle` parameter - Fixes [Issue 137](https://github.com/PowerShell/StorageDsc/issues/37). + - Changed MOF name from `MSFT_Disk` to `MSFTDSC_Disk` to remove conflict + with Windows built-in CIM class - Fixes [Issue 167](https://github.com/PowerShell/StorageDsc/issues/167). +- Opt-in to Common Tests: + - Common Tests - Validate Example Files To Be Published + - Common Tests - Validate Markdown Links + - Common Tests - Relative Path Length +- Added .VSCode settings for applying DSC PSSA rules - fixes [Issue 168](https://github.com/PowerShell/StorageDsc/issues/168). - Disk: - - Corrected message strings. - - Added message when partition resize required but `AllowDestructive` - parameter is not enabled. - - Fix error when `Size` not specified and `AllowDestructive` is `$true` - and partition can be expanded - Fixes [Issue 162](https://github.com/PowerShell/StorageDsc/issues/162). - - Fix incorrect error displaying when newly created partition is not - made Read/Write. - - Change verbose messages to show warnings when a partition resize would - have occured but the `AllowDestructive` flag is set to `$false`. + - Added "defragsvc" service conflict known issue to README.MD - fixes + [Issue 172](https://github.com/PowerShell/StorageDsc/issues/172). +- Corrected style violations in StorageDsc.Common module - fixes [Issue 153](https://github.com/PowerShell/StorageDsc/issues/153). +- Corrected style violations in StorageDsc.ResourceHelper module. ' @@ -139,3 +141,4 @@ PrivateData = @{ + diff --git a/Tests/Integration/MSFT_Disk.Integration.Tests.ps1 b/Tests/Integration/MSFTDSC_Disk.Integration.Tests.ps1 similarity index 55% rename from Tests/Integration/MSFT_Disk.Integration.Tests.ps1 rename to Tests/Integration/MSFTDSC_Disk.Integration.Tests.ps1 index ca508907..e5a268b9 100644 --- a/Tests/Integration/MSFT_Disk.Integration.Tests.ps1 +++ b/Tests/Integration/MSFTDSC_Disk.Integration.Tests.ps1 @@ -1,5 +1,5 @@ $script:DSCModuleName = 'StorageDsc' -$script:DSCResourceName = 'MSFT_Disk' +$script:DSCResourceName = 'MSFTDSC_Disk' Import-Module -Name (Join-Path -Path (Join-Path -Path (Split-Path $PSScriptRoot -Parent) -ChildPath 'TestHelpers') -ChildPath 'CommonTestHelper.psm1') -Global @@ -27,7 +27,7 @@ try Describe "$($script:DSCResourceName)_Integration" { #region Integration Tests for DiskNumber - Context 'Partition and format newly provisioned disk using Disk Number with two volumes and assign Drive Letters' { + Context 'When partitioning and formatting a newly provisioned disk using Disk Number with two volumes and assigning Drive Letters' { BeforeAll { # Create a VHD and attach it to the computer $VHDPath = Join-Path -Path $TestDrive ` @@ -45,19 +45,20 @@ try $driveLetterB = [char](([int][char]$lastDrive) + 2) } - Context "Create first volume on Disk Number $($disk.Number)" { + Context "When creating the first volume on Disk Number $($disk.Number)" { It 'Should compile and apply the MOF without throwing' { { # This is to pass to the Config $configData = @{ AllNodes = @( @{ - NodeName = 'localhost' - DriveLetter = $driveLetterA - DiskId = $disk.Number - DiskIdType = 'Number' - FSLabel = $FSLabelA - Size = 100MB + NodeName = 'localhost' + DriveLetter = $driveLetterA + DiskId = $disk.Number + DiskIdType = 'Number' + PartitionStyle = 'GPT' + FSLabel = $FSLabelA + Size = 100MB } ) } @@ -77,32 +78,34 @@ try } It 'Should be able to call Get-DscConfiguration without throwing' { - { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw + { $script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw } It 'Should have set the resource and all the parameters should match' { - $current = Get-DscConfiguration | Where-Object { + $current = $script:currentConfiguration | Where-Object -FilterScript { $_.ConfigurationName -eq "$($script:DSCResourceName)_Config" } - $current.DiskId | Should -Be $disk.Number - $current.DriveLetter | Should -Be $driveLetterA - $current.FSLabel | Should -Be $FSLabelA - $current.Size | Should -Be 100MB + $current.DiskId | Should -Be $disk.Number + $current.DriveLetter | Should -Be $driveLetterA + $current.PartitionStyle | Should -Be 'GPT' + $current.FSLabel | Should -Be $FSLabelA + $current.Size | Should -Be 100MB } } - Context "Create second volume on Disk Number $($disk.Number)" { + Context "When creating the second volume on Disk Number $($disk.Number)" { It 'Should compile and apply the MOF without throwing' { { # This is to pass to the Config $configData = @{ AllNodes = @( @{ - NodeName = 'localhost' - DriveLetter = $driveLetterB - DiskId = $disk.Number - DiskIdType = 'Number' - FSLabel = $FSLabelB + NodeName = 'localhost' + DriveLetter = $driveLetterB + DiskId = $disk.Number + DiskIdType = 'Number' + PartitionStyle = 'GPT' + FSLabel = $FSLabelB } ) } @@ -122,17 +125,18 @@ try } It 'Should be able to call Get-DscConfiguration without throwing' { - { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw + { $script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw } It 'Should have set the resource and all the parameters should match' { - $current = Get-DscConfiguration | Where-Object { + $current = $script:currentConfiguration | Where-Object -FilterScript { $_.ConfigurationName -eq "$($script:DSCResourceName)_Config" } - $current.DiskId | Should -Be $disk.Number - $current.DriveLetter | Should -Be $driveLetterB - $current.FSLabel | Should -Be $FSLabelB - $current.Size | Should -Be 935198720 + $current.DiskId | Should -Be $disk.Number + $current.DriveLetter | Should -Be $driveLetterB + $current.FSLabel | Should -Be $FSLabelB + $current.PartitionStyle | Should -Be 'GPT' + $current.Size | Should -Be 935198720 } } @@ -161,7 +165,7 @@ try } } - Context 'Partition and format newly provisioned disk using Disk Number with one volume and assign Drive Letters then resize' { + Context 'When partitioniong and formatting a newly provisioned disk using Disk Number with one volume and assigning Drive Letters then resizing' { BeforeAll { # Create a VHD and attach it to the computer $VHDPath = Join-Path -Path $TestDrive ` @@ -177,19 +181,20 @@ try $driveLetterA = [char](([int][char]$lastDrive) + 1) } - Context "Create volume on Disk Number $($disk.Number)" { + Context "When creating a volume on Disk Number $($disk.Number)" { It 'Should compile and apply the MOF without throwing' { { # This is to pass to the Config $configData = @{ AllNodes = @( @{ - NodeName = 'localhost' - DriveLetter = $driveLetterA - DiskId = $disk.Number - DiskIdType = 'Number' - FSLabel = $FSLabelA - Size = 50MB + NodeName = 'localhost' + DriveLetter = $driveLetterA + DiskId = $disk.Number + DiskIdType = 'Number' + PartitionStyle = 'GPT' + FSLabel = $FSLabelA + Size = 50MB } ) } @@ -209,34 +214,36 @@ try } It 'Should be able to call Get-DscConfiguration without throwing' { - { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw + { $script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw } It 'Should have set the resource and all the parameters should match' { - $current = Get-DscConfiguration | Where-Object { + $current = $script:currentConfiguration | Where-Object -FilterScript { $_.ConfigurationName -eq "$($script:DSCResourceName)_Config" } - $current.DiskId | Should -Be $disk.Number - $current.DriveLetter | Should -Be $driveLetterA - $current.FSLabel | Should -Be $FSLabelA - $current.FSFormat | Should -Be 'NTFS' - $current.Size | Should -Be 50MB + $current.DiskId | Should -Be $disk.Number + $current.DriveLetter | Should -Be $driveLetterA + $current.PartitionStyle | Should -Be 'GPT' + $current.FSLabel | Should -Be $FSLabelA + $current.FSFormat | Should -Be 'NTFS' + $current.Size | Should -Be 50MB } } - Context "Resize partition on Disk Number $($disk.Number) with AllowDestructive" { + Context "When resizing a partition on Disk Number $($disk.Number) to use all free space with AllowDestructive" { It 'Should compile and apply the MOF without throwing' { { # This is to pass to the Config $configData = @{ AllNodes = @( @{ - NodeName = 'localhost' - DriveLetter = $driveLetterA - DiskId = $disk.Number - DiskIdType = 'Number' - FSLabel = $FSLabelA - FSFormat = 'NTFS' + NodeName = 'localhost' + DriveLetter = $driveLetterA + DiskId = $disk.Number + DiskIdType = 'Number' + PartitionStyle = 'GPT' + FSLabel = $FSLabelA + FSFormat = 'NTFS' } ) } @@ -256,18 +263,155 @@ try } It 'Should be able to call Get-DscConfiguration without throwing' { - { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw + { $script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw } It 'Should have set the resource and all the parameters should match' { - $current = Get-DscConfiguration | Where-Object { + $current = $script:currentConfiguration | Where-Object -FilterScript { $_.ConfigurationName -eq "$($script:DSCResourceName)_ConfigAllowDestructive" } - $current.DiskId | Should -Be $disk.Number - $current.DriveLetter | Should -Be $driveLetterA - $current.FSLabel | Should -Be $FSLabelA - $current.FSFormat | Should -Be 'NTFS' - $current.Size | Should -Be 1040104960 + $current.DiskId | Should -Be $disk.Number + $current.DriveLetter | Should -Be $driveLetterA + $current.PartitionStyle | Should -Be 'GPT' + $current.FSLabel | Should -Be $FSLabelA + $current.FSFormat | Should -Be 'NTFS' + $current.Size | Should -Be 1040104960 + } + } + + # A system partition will have been added to the disk as well as the test partition + It 'Should have 2 partitions on disk' { + ($disk | Get-Partition).Count | Should -Be 2 + } + + <# + Get a list of all drives mounted - this works better on Windows Server 2012 R2 than + trying to get the drive mounted by name. + #> + $drives = Get-PSDrive + + It "Should have attached drive $driveLetterA" { + $drives | Where-Object -Property Name -eq $driveLetterA | Should -Not -BeNullOrEmpty + } + + AfterAll { + Dismount-DiskImage -ImagePath $VHDPath -StorageType VHD + Remove-Item -Path $VHDPath -Force + } + } + + Context 'When partitioning and formatting a newly provisioned disk using Disk Number with one volume using MBR then convert to GPT' { + BeforeAll { + # Create a VHD and attach it to the computer + $VHDPath = Join-Path -Path $TestDrive ` + -ChildPath 'TestDisk.vhd' + $null = New-VDisk -Path $VHDPath -SizeInMB 1024 + $null = Mount-DiskImage -ImagePath $VHDPath -StorageType VHD -NoDriveLetter + $diskImage = Get-DiskImage -ImagePath $VHDPath + $disk = Get-Disk -Number $diskImage.Number + $FSLabelA = 'TestDiskA' + + # Get a spare drive letters + $lastDrive = ((Get-Volume).DriveLetter | Sort-Object | Select-Object -Last 1) + $driveLetterA = [char](([int][char]$lastDrive) + 1) + } + + Context "When creating a volume on Disk Number $($disk.Number)" { + It 'Should compile and apply the MOF without throwing' { + { + # This is to pass to the Config + $configData = @{ + AllNodes = @( + @{ + NodeName = 'localhost' + DriveLetter = $driveLetterA + DiskId = $disk.Number + DiskIdType = 'Number' + PartitionStyle = 'MBR' + FSLabel = $FSLabelA + Size = 50MB + } + ) + } + + & "$($script:DSCResourceName)_Config" ` + -OutputPath $TestDrive ` + -ConfigurationData $configData + + Start-DscConfiguration ` + -Path $TestDrive ` + -ComputerName localhost ` + -Wait ` + -Verbose ` + -Force ` + -ErrorAction Stop + } | 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' { + $current = $script:currentConfiguration | Where-Object -FilterScript { + $_.ConfigurationName -eq "$($script:DSCResourceName)_Config" + } + $current.DiskId | Should -Be $disk.Number + $current.DriveLetter | Should -Be $driveLetterA + $current.PartitionStyle | Should -Be 'MBR' + $current.FSLabel | Should -Be $FSLabelA + $current.FSFormat | Should -Be 'NTFS' + $current.Size | Should -Be 50MB + } + } + + Context "When clearing Disk Number $($disk.Number) and changing the partition style to GPT and adding a 50MB partition" { + It 'Should compile and apply the MOF without throwing' { + { + # This is to pass to the Config + $configData = @{ + AllNodes = @( + @{ + NodeName = 'localhost' + DriveLetter = $driveLetterA + DiskId = $disk.Number + DiskIdType = 'Number' + PartitionStyle = 'GPT' + FSLabel = $FSLabelA + FSFormat = 'NTFS' + Size = 50MB + } + ) + } + + & "$($script:DSCResourceName)_ConfigClearDisk" ` + -OutputPath $TestDrive ` + -ConfigurationData $configData + + Start-DscConfiguration ` + -Path $TestDrive ` + -ComputerName localhost ` + -Wait ` + -Verbose ` + -Force ` + -ErrorAction Stop + } | 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' { + $current = $script:currentConfiguration | Where-Object -FilterScript { + $_.ConfigurationName -eq "$($script:DSCResourceName)_ConfigClearDisk" + } + $current.DiskId | Should -Be $disk.Number + $current.DriveLetter | Should -Be $driveLetterA + $current.FSLabel | Should -Be $FSLabelA + $current.PartitionStyle | Should -Be 'GPT' + $current.FSFormat | Should -Be 'NTFS' + $current.Size | Should -Be 52428800 } } @@ -294,7 +438,7 @@ try #endregion #region Integration Tests for Disk Unique Id - Context 'Partition and format newly provisioned disk using Unique Id with two volumes and assign Drive Letters' { + Context 'When partitioning and formatting a newly provisioned disk using Unique Id with two volumes and assigning Drive Letters' { BeforeAll { # Create a VHD and attach it to the computer $VHDPath = Join-Path -Path $TestDrive ` @@ -312,19 +456,20 @@ try $driveLetterB = [char](([int][char]$lastDrive) + 2) } - Context "Create first volume on Disk Unique Id $($disk.UniqueId)" { + Context "When creating the first volume on Disk Unique Id $($disk.UniqueId)" { It 'Should compile and apply the MOF without throwing' { { # This is to pass to the Config $configData = @{ AllNodes = @( @{ - NodeName = 'localhost' - DriveLetter = $driveLetterA - DiskId = $disk.UniqueId - DiskIdType = 'UniqueId' - FSLabel = $FSLabelA - Size = 100MB + NodeName = 'localhost' + DriveLetter = $driveLetterA + DiskId = $disk.UniqueId + DiskIdType = 'UniqueId' + PartitionStyle = 'GPT' + FSLabel = $FSLabelA + Size = 100MB } ) } @@ -344,34 +489,36 @@ try } It 'Should be able to call Get-DscConfiguration without throwing' { - { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw + { $script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw } It 'Should have set the resource and all the parameters should match' { - $current = Get-DscConfiguration | Where-Object { + $current = $script:currentConfiguration | Where-Object -FilterScript { $_.ConfigurationName -eq "$($script:DSCResourceName)_Config" } - $current.DiskId | Should -Be $disk.UniqueId - $current.DriveLetter | Should -Be $driveLetterA - $current.FSLabel | Should -Be $FSLabelA - $current.Size | Should -Be 100MB + $current.DiskId | Should -Be $disk.UniqueId + $current.DriveLetter | Should -Be $driveLetterA + $current.PartitionStyle | Should -Be 'GPT' + $current.FSLabel | Should -Be $FSLabelA + $current.Size | Should -Be 100MB } } - Context "Resize first volume on Disk Unique Id $($disk.UniqueId)" { + Context "When resizing the first volume on Disk Unique Id $($disk.UniqueId) and allowing the disk to be cleared" { It 'should compile and apply the MOF without throwing' { { # This is to pass to the Config $configData = @{ AllNodes = @( @{ - NodeName = 'localhost' - DriveLetter = $driveLetterA - DiskId = $disk.UniqueId - DiskIdType = 'UniqueId' - FSLabel = $FSLabelA - Size = 900MB - FSFormat = 'ReFS' + NodeName = 'localhost' + DriveLetter = $driveLetterA + DiskId = $disk.UniqueId + DiskIdType = 'UniqueId' + PartitionStyle = 'GPT' + FSLabel = $FSLabelA + Size = 900MB + FSFormat = 'ReFS' } ) } @@ -391,33 +538,35 @@ try } It 'should be able to call Get-DscConfiguration without throwing' { - { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw + { $script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw } It 'should have set the resource and all the parameters should match' { - $current = Get-DscConfiguration | Where-Object { + $current = $script:currentConfiguration | Where-Object -FilterScript { $_.ConfigurationName -eq "$($script:DSCResourceName)_ConfigClearDisk" } - $current.DiskId | Should -Be $disk.UniqueId - $current.DriveLetter | Should -Be $driveLetterA - $current.FSLabel | Should -Be $FSLabelA - $current.Size | Should -Be 900MB - $current.FSFormat | Should -Be 'ReFS' + $current.DiskId | Should -Be $disk.UniqueId + $current.DriveLetter | Should -Be $driveLetterA + $current.PartitionStyle | Should -Be 'GPT' + $current.FSLabel | Should -Be $FSLabelA + $current.Size | Should -Be 900MB + $current.FSFormat | Should -Be 'ReFS' } } - Context "Create second volume on Disk Unique Id $($disk.UniqueId)" { + Context "When creating second volume on Disk Unique Id $($disk.UniqueId)" { It 'Should compile and apply the MOF without throwing' { { # This is to pass to the Config $configData = @{ AllNodes = @( @{ - NodeName = 'localhost' - DriveLetter = $driveLetterB - DiskId = $disk.UniqueId - DiskIdType = 'UniqueId' - FSLabel = $FSLabelB + NodeName = 'localhost' + DriveLetter = $driveLetterB + DiskId = $disk.UniqueId + DiskIdType = 'UniqueId' + PartitionStyle = 'GPT' + FSLabel = $FSLabelB } ) } @@ -437,17 +586,18 @@ try } It 'Should be able to call Get-DscConfiguration without throwing' { - { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw + { $script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw } It 'Should have set the resource and all the parameters should match' { - $current = Get-DscConfiguration | Where-Object { + $current = $script:currentConfiguration | Where-Object -FilterScript { $_.ConfigurationName -eq "$($script:DSCResourceName)_Config" } - $current.DiskId | Should -Be $disk.UniqueId - $current.DriveLetter | Should -Be $driveLetterB - $current.FSLabel | Should -Be $FSLabelB - $current.Size | Should -Be 96337920 + $current.DiskId | Should -Be $disk.UniqueId + $current.PartitionStyle | Should -Be 'GPT' + $current.DriveLetter | Should -Be $driveLetterB + $current.FSLabel | Should -Be $FSLabelB + $current.Size | Should -Be 96337920 } } @@ -472,7 +622,7 @@ try #endregion #region Integration Tests for Disk Guid - Context 'Partition and format newly provisioned disk using Guid with two volumes and assign Drive Letters' { + Context 'When partitioning and formating a newly provisioned disk using Guid with two volumes and assign Drive Letters' { BeforeAll { # Create a VHD and attach it to the computer $VHDPath = Join-Path -Path $TestDrive ` @@ -490,19 +640,20 @@ try $driveLetterB = [char](([int][char]$lastDrive) + 2) } - Context "Create first volume on Disk Guid $($disk.Guid)" { + Context "When creating the first volume on Disk Guid $($disk.Guid)" { It 'Should compile and apply the MOF without throwing' { { # This is to pass to the Config $configData = @{ AllNodes = @( @{ - NodeName = 'localhost' - DriveLetter = $driveLetterA - DiskId = $disk.Guid - DiskIdType = 'Guid' - FSLabel = $FSLabelA - Size = 100MB + NodeName = 'localhost' + DriveLetter = $driveLetterA + DiskId = $disk.Guid + DiskIdType = 'Guid' + PartitionStyle = 'GPT' + FSLabel = $FSLabelA + Size = 100MB } ) } @@ -522,32 +673,34 @@ try } It 'Should be able to call Get-DscConfiguration without throwing' { - { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw + { $script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw } It 'Should have set the resource and all the parameters should match' { - $current = Get-DscConfiguration | Where-Object { + $current = $script:currentConfiguration | Where-Object -FilterScript { $_.ConfigurationName -eq "$($script:DSCResourceName)_Config" } - $current.DiskId | Should -Be $disk.Guid - $current.DriveLetter | Should -Be $driveLetterA - $current.FSLabel | Should -Be $FSLabelA - $current.Size | Should -Be 100MB + $current.DiskId | Should -Be $disk.Guid + $current.PartitionStyle | Should -Be 'GPT' + $current.DriveLetter | Should -Be $driveLetterA + $current.FSLabel | Should -Be $FSLabelA + $current.Size | Should -Be 100MB } } - Context "Create first volume on Disk Guid $($disk.Guid)" { + Context "When creating the first volume on Disk Guid $($disk.Guid)" { It 'Should compile and apply the MOF without throwing' { { # This is to pass to the Config $configData = @{ AllNodes = @( @{ - NodeName = 'localhost' - DriveLetter = $driveLetterB - DiskId = $disk.Guid - DiskIdType = 'Guid' - FSLabel = $FSLabelB + NodeName = 'localhost' + DriveLetter = $driveLetterB + DiskId = $disk.Guid + DiskIdType = 'Guid' + PartitionStyle = 'GPT' + FSLabel = $FSLabelB } ) } @@ -567,17 +720,18 @@ try } It 'Should be able to call Get-DscConfiguration without throwing' { - { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw + { $script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw } It 'Should have set the resource and all the parameters should match' { - $current = Get-DscConfiguration | Where-Object { + $current = $script:currentConfiguration | Where-Object -FilterScript { $_.ConfigurationName -eq "$($script:DSCResourceName)_Config" } - $current.DiskId | Should -Be $disk.Guid - $current.DriveLetter | Should -Be $driveLetterB - $current.FSLabel | Should -Be $FSLabelB - $current.Size | Should -Be 935198720 + $current.DiskId | Should -Be $disk.Guid + $current.PartitionStyle | Should -Be 'GPT' + $current.DriveLetter | Should -Be $driveLetterB + $current.FSLabel | Should -Be $FSLabelB + $current.Size | Should -Be 935198720 } } @@ -602,7 +756,7 @@ try #endregion #region Integration Tests for DiskNumber to test if a single disk with a volume using the whole disk can be remounted - Context 'Partition a disk using Disk Number with a single volume using the whole disk, dismount the volume then reprovision it' { + Context 'When partitioning a disk using Disk Number with a single volume using the whole disk, dismounting the volume then reprovisioning it' { BeforeAll { # Create a VHD and attach it to the computer $VHDPath = Join-Path -Path $TestDrive ` @@ -618,18 +772,19 @@ try $driveLetterA = [char](([int][char]$lastDrive) + 1) } - Context "Create first volume on Disk Number $($disk.Number)" { + Context "When creating the first volume on Disk Number $($disk.Number)" { It 'Should compile and apply the MOF without throwing' { { # This is to pass to the Config $configData = @{ AllNodes = @( @{ - NodeName = 'localhost' - DriveLetter = $driveLetterA - DiskId = $disk.Number - DiskIdType = 'Number' - FSLabel = $FSLabelA + NodeName = 'localhost' + DriveLetter = $driveLetterA + DiskId = $disk.Number + DiskIdType = 'Number' + PartitionStyle = 'GPT' + FSLabel = $FSLabelA } ) } @@ -649,16 +804,17 @@ try } It 'Should be able to call Get-DscConfiguration without throwing' { - { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw + { $script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw } It 'Should have set the resource and all the parameters should match' { - $current = Get-DscConfiguration | Where-Object { + $current = $script:currentConfiguration | Where-Object -FilterScript { $_.ConfigurationName -eq "$($script:DSCResourceName)_Config" } - $current.DiskId | Should -Be $disk.Number - $current.DriveLetter | Should -Be $driveLetterA - $current.FSLabel | Should -Be $FSLabelA + $current.DiskId | Should -Be $disk.Number + $current.PartitionStyle | Should -Be 'GPT' + $current.DriveLetter | Should -Be $driveLetterA + $current.FSLabel | Should -Be $FSLabelA } } @@ -668,18 +824,19 @@ try -PartitionNumber 2 ` -AccessPath "$($driveLetterA):" - Context "Attach first volume on Disk Number $($disk.Number)" { + Context "When attaching the first volume on Disk Number $($disk.Number)" { It 'Should compile and apply the MOF without throwing' { { # This is to pass to the Config $configData = @{ AllNodes = @( @{ - NodeName = 'localhost' - DriveLetter = $driveLetterA - DiskId = $disk.Number - DiskIdType = 'Number' - FSLabel = $FSLabelA + NodeName = 'localhost' + DriveLetter = $driveLetterA + DiskId = $disk.Number + DiskIdType = 'Number' + PartitionStyle = 'GPT' + FSLabel = $FSLabelA } ) } @@ -699,16 +856,17 @@ try } It 'Should be able to call Get-DscConfiguration without throwing' { - { Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw + { $script:currentConfiguration = Get-DscConfiguration -Verbose -ErrorAction Stop } | Should -Not -Throw } It 'Should have set the resource and all the parameters should match' { - $current = Get-DscConfiguration | Where-Object { + $current = $script:currentConfiguration | Where-Object -FilterScript { $_.ConfigurationName -eq "$($script:DSCResourceName)_Config" } - $current.DiskId | Should -Be $disk.Number - $current.DriveLetter | Should -Be $driveLetterA - $current.FSLabel | Should -Be $FSLabelA + $current.DiskId | Should -Be $disk.Number + $current.PartitionStyle | Should -Be 'GPT' + $current.DriveLetter | Should -Be $driveLetterA + $current.FSLabel | Should -Be $FSLabelA } } diff --git a/Tests/Integration/MSFT_Disk.config.ps1 b/Tests/Integration/MSFTDSC_Disk.config.ps1 similarity index 70% rename from Tests/Integration/MSFT_Disk.config.ps1 rename to Tests/Integration/MSFTDSC_Disk.config.ps1 index 7f6d8c14..97dcb1ff 100644 --- a/Tests/Integration/MSFT_Disk.config.ps1 +++ b/Tests/Integration/MSFTDSC_Disk.config.ps1 @@ -1,4 +1,4 @@ -configuration MSFT_Disk_Config { +configuration MSFTDSC_Disk_Config { Import-DscResource -ModuleName StorageDsc @@ -7,27 +7,29 @@ configuration MSFT_Disk_Config { { Disk Integration_Test { - DiskId = $Node.DiskId - DiskIdType = $Node.DiskIdType - DriveLetter = $Node.DriveLetter - FSLabel = $Node.FSLabel - Size = $Node.Size + DiskId = $Node.DiskId + DiskIdType = $Node.DiskIdType + PartitionStyle = $Node.PartitionStyle + DriveLetter = $Node.DriveLetter + FSLabel = $Node.FSLabel + Size = $Node.Size } } else { Disk Integration_Test { - DiskId = $Node.DiskId - DiskIdType = $Node.DiskIdType - DriveLetter = $Node.DriveLetter - FSLabel = $Node.FSLabel + DiskId = $Node.DiskId + DiskIdType = $Node.DiskIdType + PartitionStyle = $Node.PartitionStyle + DriveLetter = $Node.DriveLetter + FSLabel = $Node.FSLabel } } } } -configuration MSFT_Disk_ConfigAllowDestructive { +configuration MSFTDSC_Disk_ConfigAllowDestructive { Import-DscResource -ModuleName StorageDsc @@ -38,6 +40,7 @@ configuration MSFT_Disk_ConfigAllowDestructive { { DiskId = $Node.DiskId DiskIdType = $Node.DiskIdType + PartitionStyle = $Node.PartitionStyle DriveLetter = $Node.DriveLetter FSLabel = $Node.FSLabel Size = $Node.Size @@ -51,6 +54,7 @@ configuration MSFT_Disk_ConfigAllowDestructive { { DiskId = $Node.DiskId DiskIdType = $Node.DiskIdType + PartitionStyle = $Node.PartitionStyle DriveLetter = $Node.DriveLetter FSLabel = $Node.FSLabel FSFormat = $Node.FSFormat @@ -60,7 +64,7 @@ configuration MSFT_Disk_ConfigAllowDestructive { } } -configuration MSFT_Disk_ConfigClearDisk { +configuration MSFTDSC_Disk_ConfigClearDisk { Import-DscResource -ModuleName StorageDsc @@ -71,6 +75,7 @@ configuration MSFT_Disk_ConfigClearDisk { { DiskId = $Node.DiskId DiskIdType = $Node.DiskIdType + PartitionStyle = $Node.PartitionStyle DriveLetter = $Node.DriveLetter FSLabel = $Node.FSLabel Size = $Node.Size @@ -85,6 +90,7 @@ configuration MSFT_Disk_ConfigClearDisk { { DiskId = $Node.DiskId DiskIdType = $Node.DiskIdType + PartitionStyle = $Node.PartitionStyle DriveLetter = $Node.DriveLetter FSLabel = $Node.FSLabel FSFormat = $Node.FSFormat diff --git a/Tests/Unit/MSFT_Disk.Tests.ps1 b/Tests/Unit/MSFTDSC_Disk.Tests.ps1 similarity index 81% rename from Tests/Unit/MSFT_Disk.Tests.ps1 rename to Tests/Unit/MSFTDSC_Disk.Tests.ps1 index ea8b1c34..778ee63e 100644 --- a/Tests/Unit/MSFT_Disk.Tests.ps1 +++ b/Tests/Unit/MSFTDSC_Disk.Tests.ps1 @@ -1,5 +1,5 @@ $script:DSCModuleName = 'StorageDsc' -$script:DSCResourceName = 'MSFT_Disk' +$script:DSCResourceName = 'MSFTDSC_Disk' Import-Module -Name (Join-Path -Path (Join-Path -Path (Split-Path $PSScriptRoot -Parent) -ChildPath 'TestHelpers') -ChildPath 'CommonTestHelper.psm1') -Global @@ -32,9 +32,8 @@ try $script:testDiskNumber = 1 $script:testDiskUniqueId = 'TESTDISKUNIQUEID' $script:testDiskGptGuid = [guid]::NewGuid() - $script:testDiskMbrGuid = '123456' - $script:mockedDisk0 = [pscustomobject] @{ + $script:mockedDisk0Gpt = [pscustomobject] @{ Number = $script:testDiskNumber UniqueId = $script:testDiskUniqueId Guid = $script:testDiskGptGuid @@ -46,13 +45,22 @@ try $script:mockedDisk0Mbr = [pscustomobject] @{ Number = $script:testDiskNumber UniqueId = $script:testDiskUniqueId - Guid = $script:testDiskMbrGuid + Guid = '' IsOffline = $false IsReadOnly = $false PartitionStyle = 'MBR' } - $script:mockedDisk0Offline = [pscustomobject] @{ + $script:mockedDisk0Raw = [pscustomobject] @{ + Number = $script:testDiskNumber + UniqueId = $script:testDiskUniqueId + Guid = '' + IsOffline = $false + IsReadOnly = $false + PartitionStyle = 'RAW' + } + + $script:mockedDisk0GptOffline = [pscustomobject] @{ Number = $script:testDiskNumber UniqueId = $script:testDiskUniqueId Guid = $script:testDiskGptGuid @@ -61,16 +69,16 @@ try PartitionStyle = 'GPT' } - $script:mockedDisk0OfflineRaw = [pscustomobject] @{ + $script:mockedDisk0RawOffline = [pscustomobject] @{ Number = $script:testDiskNumber UniqueId = $script:testDiskUniqueId Guid = '' IsOffline = $true IsReadOnly = $false - PartitionStyle = 'Raw' + PartitionStyle = 'RAW' } - $script:mockedDisk0Readonly = [pscustomobject] @{ + $script:mockedDisk0GptReadonly = [pscustomobject] @{ Number = $script:testDiskNumber UniqueId = $script:testDiskUniqueId Guid = $script:testDiskGptGuid @@ -79,15 +87,6 @@ try PartitionStyle = 'GPT' } - $script:mockedDisk0Raw = [pscustomobject] @{ - Number = $script:testDiskNumber - UniqueId = $script:testDiskUniqueId - Guid = '' - IsOffline = $false - IsReadOnly = $false - PartitionStyle = 'Raw' - } - $script:mockedCim = [pscustomobject] @{BlockSize = 4096} $script:mockedPartitionSize = 1GB @@ -139,7 +138,7 @@ try } $script:parameterFilter_MockedDisk0Number = { - $DiskId -eq $script:mockedDisk0.Number -and $DiskIdType -eq 'Number' + $DiskId -eq $script:mockedDisk0Gpt.Number -and $DiskIdType -eq 'Number' } #endregion @@ -308,8 +307,8 @@ try #endregion #region Function Get-TargetResource - Describe 'MSFT_Disk\Get-TargetResource' { - Context 'Online GPT disk with a partition/volume and correct Drive Letter assigned using Disk Number' { + Describe 'MSFTDSC_Disk\Get-TargetResource' { + Context 'When online GPT disk with a partition/volume and correct Drive Letter assigned using Disk Number' { # verifiable (should be called) mocks Mock ` -CommandName Get-CimInstance ` @@ -319,7 +318,7 @@ try Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -333,12 +332,16 @@ try -Verifiable $resource = Get-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -DriveLetter $script:testDriveLetter ` -Verbose - It "Should return DiskId $($script:mockedDisk0.Number)" { - $resource.DiskId | Should -Be $script:mockedDisk0.Number + It "Should return DiskId $($script:mockedDisk0Gpt.Number)" { + $resource.DiskId | Should -Be $script:mockedDisk0Gpt.Number + } + + It "Should return PartitionStyle $($script:mockedDisk0Gpt.PartitionStyle)" { + $resource.PartitionStyle | Should -Be $script:mockedDisk0Gpt.PartitionStyle } It "Should return DriveLetter $($script:testDriveLetter)" { @@ -371,7 +374,7 @@ try } } - Context 'Online GPT disk with a partition/volume and correct Drive Letter assigned using Disk Unique Id' { + Context 'When online GPT disk with a partition/volume and correct Drive Letter assigned using Disk Unique Id' { # verifiable (should be called) mocks Mock ` -CommandName Get-CimInstance ` @@ -380,8 +383,8 @@ try Mock ` -CommandName Get-DiskByIdentifier ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0.UniqueId -and $DiskIdType -eq 'UniqueId' } ` - -MockWith { $script:mockedDisk0 } ` + -ParameterFilter { $DiskId -eq $script:mockedDisk0Gpt.UniqueId -and $DiskIdType -eq 'UniqueId' } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -395,13 +398,17 @@ try -Verifiable $resource = Get-TargetResource ` - -DiskId $script:mockedDisk0.UniqueId ` + -DiskId $script:mockedDisk0Gpt.UniqueId ` -DiskIdType 'UniqueId' ` -DriveLetter $script:testDriveLetter ` -Verbose - It "Should return DiskId $($script:mockedDisk0.UniqueId)" { - $resource.DiskId | Should -Be $script:mockedDisk0.UniqueId + It "Should return DiskId $($script:mockedDisk0Gpt.UniqueId)" { + $resource.DiskId | Should -Be $script:mockedDisk0Gpt.UniqueId + } + + It "Should return PartitionStyle $($script:mockedDisk0Gpt.PartitionStyle)" { + $resource.PartitionStyle | Should -Be $script:mockedDisk0Gpt.PartitionStyle } It "Should return DriveLetter $($script:testDriveLetter)" { @@ -428,13 +435,13 @@ try Assert-VerifiableMock Assert-MockCalled -CommandName Get-CimInstance -Exactly 1 Assert-MockCalled -CommandName Get-DiskByIdentifier -Exactly 1 ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0.UniqueId -and $DiskIdType -eq 'UniqueId' } + -ParameterFilter { $DiskId -eq $script:mockedDisk0Gpt.UniqueId -and $DiskIdType -eq 'UniqueId' } Assert-MockCalled -CommandName Get-Partition -Exactly 1 Assert-MockCalled -CommandName Get-Volume -Exactly 1 } } - Context 'Online GPT disk with a partition/volume and correct Drive Letter assigned using Disk Guid' { + Context 'When online GPT disk with a partition/volume and correct Drive Letter assigned using Disk Guid' { # verifiable (should be called) mocks Mock ` -CommandName Get-CimInstance ` @@ -443,8 +450,8 @@ try Mock ` -CommandName Get-DiskByIdentifier ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0.Guid -and $DiskIdType -eq 'Guid' } ` - -MockWith { $script:mockedDisk0 } ` + -ParameterFilter { $DiskId -eq $script:mockedDisk0Gpt.Guid -and $DiskIdType -eq 'Guid' } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -458,13 +465,17 @@ try -Verifiable $resource = Get-TargetResource ` - -DiskId $script:mockedDisk0.Guid ` + -DiskId $script:mockedDisk0Gpt.Guid ` -DiskIdType 'Guid' ` -DriveLetter $script:testDriveLetter ` -Verbose - It "Should return DiskId $($script:mockedDisk0.Guid)" { - $resource.DiskId | Should -Be $script:mockedDisk0.Guid + It "Should return DiskId $($script:mockedDisk0Gpt.Guid)" { + $resource.DiskId | Should -Be $script:mockedDisk0Gpt.Guid + } + + It "Should return PartitionStyle $($script:mockedDisk0Gpt.PartitionStyle)" { + $resource.PartitionStyle | Should -Be $script:mockedDisk0Gpt.PartitionStyle } It "Should return DriveLetter $($script:testDriveLetter)" { @@ -491,13 +502,13 @@ try Assert-VerifiableMock Assert-MockCalled -CommandName Get-CimInstance -Exactly 1 Assert-MockCalled -CommandName Get-DiskByIdentifier -Exactly 1 ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0.Guid -and $DiskIdType -eq 'Guid' } + -ParameterFilter { $DiskId -eq $script:mockedDisk0Gpt.Guid -and $DiskIdType -eq 'Guid' } Assert-MockCalled -CommandName Get-Partition -Exactly 1 Assert-MockCalled -CommandName Get-Volume -Exactly 1 } } - Context 'Online GPT disk with a partition/volume and correct Drive Letter assigned using Disk Guid' { + Context 'When online GPT disk with a partition/volume and correct Drive Letter assigned using Disk Guid' { # verifiable (should be called) mocks Mock ` -CommandName Get-CimInstance ` @@ -506,8 +517,8 @@ try Mock ` -CommandName Get-DiskByIdentifier ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0.Guid -and $DiskIdType -eq 'Guid' } ` - -MockWith { $script:mockedDisk0 } ` + -ParameterFilter { $DiskId -eq $script:mockedDisk0Gpt.Guid -and $DiskIdType -eq 'Guid' } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -521,13 +532,17 @@ try -Verifiable $resource = Get-TargetResource ` - -DiskId $script:mockedDisk0.Guid ` + -DiskId $script:mockedDisk0Gpt.Guid ` -DiskIdType 'Guid' ` -DriveLetter $script:testDriveLetter ` -Verbose - It "Should return DiskId $($script:mockedDisk0.Guid)" { - $resource.DiskId | Should -Be $script:mockedDisk0.Guid + It "Should return DiskId $($script:mockedDisk0Gpt.Guid)" { + $resource.DiskId | Should -Be $script:mockedDisk0Gpt.Guid + } + + It "Should return PartitionStyle $($script:mockedDisk0Gpt.PartitionStyle)" { + $resource.PartitionStyle | Should -Be $script:mockedDisk0Gpt.PartitionStyle } It "Should return DriveLetter $($script:testDriveLetter)" { @@ -554,13 +569,76 @@ try Assert-VerifiableMock Assert-MockCalled -CommandName Get-CimInstance -Exactly 1 Assert-MockCalled -CommandName Get-DiskByIdentifier -Exactly 1 ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0.Guid -and $DiskIdType -eq 'Guid' } + -ParameterFilter { $DiskId -eq $script:mockedDisk0Gpt.Guid -and $DiskIdType -eq 'Guid' } + Assert-MockCalled -CommandName Get-Partition -Exactly 1 + Assert-MockCalled -CommandName Get-Volume -Exactly 1 + } + } + + Context 'When online GPT disk with no partition using Disk Number' { + # verifiable (should be called) mocks + Mock ` + -CommandName Get-CimInstance ` + -Verifiable + + Mock ` + -CommandName Get-DiskByIdentifier ` + -ParameterFilter $script:parameterFilter_MockedDisk0Number ` + -MockWith { $script:mockedDisk0Gpt } ` + -Verifiable + + Mock ` + -CommandName Get-Partition ` + -Verifiable + + Mock ` + -CommandName Get-Volume ` + -Verifiable + + $resource = Get-TargetResource ` + -DiskId $script:mockedDisk0Gpt.Number ` + -DriveLetter $script:testDriveLetter ` + -Verbose + + It "Should return DiskId $($script:mockedDisk0Gpt.Number)" { + $resource.DiskId | Should -Be $script:mockedDisk0Gpt.Number + } + + It "Should return PartitionStyle $($script:mockedDisk0Gpt.PartitionStyle)" { + $resource.PartitionStyle | Should -Be $script:mockedDisk0Gpt.PartitionStyle + } + + It "Should return an empty drive letter" { + $resource.DriveLetter | Should -BeNullOrEmpty + } + + It "Should return a zero size" { + $resource.Size | Should -BeNullOrEmpty + } + + It "Should return no FSLabel" { + $resource.FSLabel | Should -BeNullOrEmpty + } + + It "Should return an AllocationUnitSize of 0" { + $resource.AllocationUnitSize | Should -BeNullOrEmpty + } + + It "Should return no FSFormat" { + $resource.FSFormat | Should -BeNullOrEmpty + } + + It 'Should call the correct mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Get-CimInstance -Exactly 1 + Assert-MockCalled -CommandName Get-DiskByIdentifier -Exactly 1 ` + -ParameterFilter $script:parameterFilter_MockedDisk0Number Assert-MockCalled -CommandName Get-Partition -Exactly 1 Assert-MockCalled -CommandName Get-Volume -Exactly 1 } } - Context 'Online GPT disk with no partition using Disk Number' { + Context 'When online MBR disk with no partition using Disk Number' { # verifiable (should be called) mocks Mock ` -CommandName Get-CimInstance ` @@ -569,7 +647,7 @@ try Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Mbr } ` -Verifiable Mock ` @@ -581,32 +659,99 @@ try -Verifiable $resource = Get-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Mbr.Number ` -DriveLetter $script:testDriveLetter ` -Verbose - It "Should return DiskId $($script:mockedDisk0.Number)" { - $resource.DiskId | Should -Be $script:mockedDisk0.Number + It "Should return DiskId $($script:mockedDisk0Mbr.Number)" { + $resource.DiskId | Should -Be $script:mockedDisk0Mbr.Number + } + + It "Should return PartitionStyle $($script:mockedDisk0Mbr.PartitionStyle)" { + $resource.PartitionStyle | Should -Be $script:mockedDisk0Mbr.PartitionStyle } It "Should return an empty drive letter" { - $resource.DriveLetter | Should -Be $null + $resource.DriveLetter | Should -BeNullOrEmpty } It "Should return a zero size" { - $resource.Size | Should -Be $null + $resource.Size | Should -BeNullOrEmpty } It "Should return no FSLabel" { - $resource.FSLabel | Should -Be '' + $resource.FSLabel | Should -BeNullOrEmpty } It "Should return an AllocationUnitSize of 0" { - $resource.AllocationUnitSize | Should -Be $null + $resource.AllocationUnitSize | Should -BeNullOrEmpty } It "Should return no FSFormat" { - $resource.FSFormat | Should -Be $null + $resource.FSFormat | Should -BeNullOrEmpty + } + + It 'Should call the correct mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Get-CimInstance -Exactly 1 + Assert-MockCalled -CommandName Get-DiskByIdentifier -Exactly 1 ` + -ParameterFilter $script:parameterFilter_MockedDisk0Number + Assert-MockCalled -CommandName Get-Partition -Exactly 1 + Assert-MockCalled -CommandName Get-Volume -Exactly 1 + } + } + + Context 'When online RAW disk with no partition using Disk Number' { + # verifiable (should be called) mocks + Mock ` + -CommandName Get-CimInstance ` + -Verifiable + + Mock ` + -CommandName Get-DiskByIdentifier ` + -ParameterFilter $script:parameterFilter_MockedDisk0Number ` + -MockWith { $script:mockedDisk0Raw } ` + -Verifiable + + Mock ` + -CommandName Get-Partition ` + -Verifiable + + Mock ` + -CommandName Get-Volume ` + -Verifiable + + $resource = Get-TargetResource ` + -DiskId $script:mockedDisk0Raw.Number ` + -DriveLetter $script:testDriveLetter ` + -Verbose + + It "Should return DiskId $($script:mockedDisk0Raw.Number)" { + $resource.DiskId | Should -Be $script:mockedDisk0Raw.Number + } + + It "Should return PartitionStyle $($script:mockedDisk0Raw.PartitionStyle)" { + $resource.PartitionStyle | Should -Be $script:mockedDisk0Raw.PartitionStyle + } + + It "Should return an empty drive letter" { + $resource.DriveLetter | Should -BeNullOrEmpty + } + + It "Should return a zero size" { + $resource.Size | Should -BeNullOrEmpty + } + + It "Should return no FSLabel" { + $resource.FSLabel | Should -BeNullOrEmpty + } + + It "Should return an AllocationUnitSize of 0" { + $resource.AllocationUnitSize | Should -BeNullOrEmpty + } + + It "Should return no FSFormat" { + $resource.FSFormat | Should -BeNullOrEmpty } It 'Should call the correct mocks' { @@ -622,13 +767,13 @@ try #endregion #region Function Set-TargetResource - Describe 'MSFT_Disk\Set-TargetResource' { - Context 'Offline GPT disk using Disk Number' { + Describe 'MSFTDSC_Disk\Set-TargetResource' { + Context 'When offline GPT disk using Disk Number' { # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0Offline.Number -and $DiskIdType -eq 'Number' } ` - -MockWith { $script:mockedDisk0Offline } ` + -ParameterFilter { $DiskId -eq $script:mockedDisk0GptOffline.Number -and $DiskIdType -eq 'Number' } ` + -MockWith { $script:mockedDisk0GptOffline } ` -Verifiable Mock ` @@ -664,7 +809,7 @@ try It 'Should not throw an exception' { { Set-TargetResource ` - -DiskId $script:mockedDisk0Offline.Number ` + -DiskId $script:mockedDisk0GptOffline.Number ` -Driveletter $script:testDriveLetter ` -Verbose } | Should -Not -Throw @@ -673,7 +818,7 @@ try It 'Should call the correct mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Get-DiskByIdentifier -Exactly -Times 1 ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0Offline.Number -and $DiskIdType -eq 'Number' } + -ParameterFilter { $DiskId -eq $script:mockedDisk0GptOffline.Number -and $DiskIdType -eq 'Number' } Assert-MockCalled -CommandName Set-Disk -Exactly -Times 1 Assert-MockCalled -CommandName Initialize-Disk -Exactly -Times 0 Assert-MockCalled -CommandName Get-Partition -Exactly -Times 1 @@ -685,12 +830,12 @@ try } } - Context 'Offline GPT disk using Disk Unique Id' { + Context 'When offline GPT disk using Disk Unique Id' { # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0Offline.UniqueId -and $DiskIdType -eq 'UniqueId' } ` - -MockWith { $script:mockedDisk0Offline } ` + -ParameterFilter { $DiskId -eq $script:mockedDisk0GptOffline.UniqueId -and $DiskIdType -eq 'UniqueId' } ` + -MockWith { $script:mockedDisk0GptOffline } ` -Verifiable Mock ` @@ -728,7 +873,7 @@ try It 'Should not throw an exception' { { Set-TargetResource ` - -DiskId $script:mockedDisk0Offline.UniqueId ` + -DiskId $script:mockedDisk0GptOffline.UniqueId ` -DiskIdType 'UniqueId' ` -Driveletter $script:testDriveLetter ` -Verbose @@ -738,7 +883,7 @@ try It 'Should call the correct mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Get-DiskByIdentifier -Exactly -Times 1 ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0Offline.UniqueId -and $DiskIdType -eq 'UniqueId' } + -ParameterFilter { $DiskId -eq $script:mockedDisk0GptOffline.UniqueId -and $DiskIdType -eq 'UniqueId' } Assert-MockCalled -CommandName Set-Disk -Exactly -Times 1 Assert-MockCalled -CommandName Initialize-Disk -Exactly -Times 0 Assert-MockCalled -CommandName Get-Partition -Exactly -Times 1 @@ -752,12 +897,12 @@ try } } - Context 'Offline GPT disk using Disk Guid' { + Context 'When offline GPT disk using Disk Guid' { # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0Offline.Guid -and $DiskIdType -eq 'Guid' } ` - -MockWith { $script:mockedDisk0Offline } ` + -ParameterFilter { $DiskId -eq $script:mockedDisk0GptOffline.Guid -and $DiskIdType -eq 'Guid' } ` + -MockWith { $script:mockedDisk0GptOffline } ` -Verifiable Mock ` @@ -795,7 +940,7 @@ try It 'Should not throw an exception' { { Set-TargetResource ` - -DiskId $script:mockedDisk0Offline.Guid ` + -DiskId $script:mockedDisk0GptOffline.Guid ` -DiskIdType 'Guid' ` -Driveletter $script:testDriveLetter ` -Verbose @@ -805,7 +950,7 @@ try It 'Should call the correct mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Get-DiskByIdentifier -Exactly -Times 1 ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0Offline.Guid -and $DiskIdType -eq 'Guid' } + -ParameterFilter { $DiskId -eq $script:mockedDisk0GptOffline.Guid -and $DiskIdType -eq 'Guid' } Assert-MockCalled -CommandName Set-Disk -Exactly -Times 1 Assert-MockCalled -CommandName Initialize-Disk -Exactly -Times 0 Assert-MockCalled -CommandName Get-Partition -Exactly -Times 1 @@ -819,12 +964,12 @@ try } } - Context 'Readonly GPT disk using Disk Number' { + Context 'When readonly GPT disk using Disk Number' { # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0Readonly } ` + -MockWith { $script:mockedDisk0GptReadonly } ` -Verifiable Mock ` @@ -862,7 +1007,7 @@ try It 'Should not throw an exception' { { Set-TargetResource ` - -DiskId $script:mockedDisk0Readonly.Number ` + -DiskId $script:mockedDisk0GptReadonly.Number ` -Driveletter $script:testDriveLetter ` -Verbose } | Should -Not -Throw @@ -885,12 +1030,12 @@ try } } - Context 'Offline RAW disk using Disk Number' { + Context 'When offline RAW disk using Disk Number' { # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0OfflineRaw } ` + -MockWith { $script:mockedDisk0RawOffline } ` -Verifiable Mock ` @@ -929,7 +1074,7 @@ try It 'Should not throw an exception' { { Set-TargetResource ` - -DiskId $script:mockedDisk0OfflineRaw.Number ` + -DiskId $script:mockedDisk0RawOffline.Number ` -Driveletter $script:testDriveLetter ` -Verbose } | Should -Not -Throw @@ -952,7 +1097,7 @@ try } } - Context 'Online RAW disk with Size using Disk Number' { + Context 'When online RAW disk with Size using Disk Number' { # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` @@ -1021,12 +1166,12 @@ try } } - Context 'Online GPT disk with no partitions using Disk Number' { + Context 'When online GPT disk with no partitions using Disk Number' { # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -1061,7 +1206,7 @@ try It 'Should not throw an exception' { { Set-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -Driveletter $script:testDriveLetter ` -Verbose } | Should -Not -Throw @@ -1084,12 +1229,12 @@ try } } - Context 'Online GPT disk with no partitions using Disk Number, partition fails to become writeable' { + Context 'When online GPT disk with no partitions using Disk Number, partition fails to become writeable' { # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -1122,7 +1267,7 @@ try It 'Should throw NewParitionIsReadOnlyError' { { Set-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -Driveletter $script:testDriveLetter ` -Verbose } | Should -Throw $errorRecord @@ -1152,7 +1297,7 @@ try } } - Context 'Online MBR disk using Disk Number' { + Context 'When online MBR disk using Disk Number' { # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` @@ -1170,10 +1315,10 @@ try Mock -CommandName Set-Partition $errorRecord = Get-InvalidOperationRecord ` - -Message ($LocalizedData.DiskAlreadyInitializedError -f ` - 'Number', $script:mockedDisk0Mbr.Number, $script:mockedDisk0Mbr.PartitionStyle) + -Message ($LocalizedData.DiskInitializedWithWrongPartitionStyleError -f ` + 'Number', $script:mockedDisk0Mbr.Number, $script:mockedDisk0Mbr.PartitionStyle, 'GPT') - It 'Should throw DiskAlreadyInitializedError' { + It 'Should not throw DiskInitializedWithWrongPartitionStyleError' { { Set-TargetResource ` -DiskId $script:mockedDisk0Mbr.Number ` @@ -1196,7 +1341,7 @@ try } } - Context 'Online MBR disk using Disk Unique Id' { + Context 'When online MBR disk using Disk Unique Id but GPT required and AllowDestructive and ClearDisk are false' { # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` @@ -1213,10 +1358,10 @@ try Mock -CommandName Set-Partition $errorRecord = Get-InvalidOperationRecord ` - -Message ($LocalizedData.DiskAlreadyInitializedError -f ` - 'UniqueId', $script:mockedDisk0Mbr.UniqueId, $script:mockedDisk0Mbr.PartitionStyle) + -Message ($LocalizedData.DiskInitializedWithWrongPartitionStyleError -f ` + 'UniqueId', $script:mockedDisk0Mbr.UniqueId, $script:mockedDisk0Mbr.PartitionStyle, 'GPT') - It 'Should throw DiskAlreadyInitializedError' { + It 'Should throw DiskInitializedWithWrongPartitionStyleError' { { Set-TargetResource ` -DiskId $script:mockedDisk0Mbr.UniqueId ` @@ -1239,55 +1384,12 @@ try } } - Context 'Online MBR disk using Disk Guid' { - # verifiable (should be called) mocks - Mock ` - -CommandName Get-DiskByIdentifier ` - -MockWith { $script:mockedDisk0Mbr } ` - -Verifiable - - # mocks that should not be called - Mock -CommandName Set-Disk - Mock -CommandName Initialize-Disk - Mock -CommandName Get-Partition - Mock -CommandName New-Partition - Mock -CommandName Format-Volume - Mock -CommandName Get-Volume - Mock -CommandName Set-Partition - - $errorRecord = Get-InvalidOperationRecord ` - -Message ($LocalizedData.DiskAlreadyInitializedError -f ` - 'Guid', $script:mockedDisk0Mbr.Guid, $script:mockedDisk0Mbr.PartitionStyle) - - It 'Should throw DiskAlreadyInitializedError' { - { - Set-TargetResource ` - -DiskId $script:mockedDisk0Mbr.Guid ` - -DiskIdType 'Guid' ` - -Driveletter $script:testDriveLetter ` - -Verbose - } | Should -Throw $errorRecord - } - - It 'Should call the correct mocks' { - Assert-VerifiableMock - Assert-MockCalled -CommandName Get-DiskByIdentifier -Exactly -Times 1 - Assert-MockCalled -CommandName Set-Disk -Exactly -Times 0 - Assert-MockCalled -CommandName Initialize-Disk -Exactly -Times 0 - Assert-MockCalled -CommandName Get-Partition -Exactly -Times 0 - Assert-MockCalled -CommandName Get-Volume -Exactly -Times 0 - Assert-MockCalled -CommandName New-Partition -Exactly -Times 0 - Assert-MockCalled -CommandName Format-Volume -Exactly -Times 0 - Assert-MockCalled -CommandName Set-Partition -Exactly -Times 0 - } - } - - Context 'Online GPT disk with partition/volume already assigned using Disk Number' { + Context 'When online GPT disk with partition/volume already assigned using Disk Number' { # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -1310,7 +1412,7 @@ try It 'Should not throw an exception' { { Set-targetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -DriveLetter $script:testDriveLetter ` -Verbose } | Should -Not -Throw @@ -1330,12 +1432,12 @@ try } } - Context 'Online GPT disk containing matching partition but not assigned using Disk Number' { + Context 'When online GPT disk containing matching partition but not assigned using Disk Number' { # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -1361,7 +1463,7 @@ try It 'Should not throw an exception' { { Set-targetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -DriveLetter $script:testDriveLetter ` -Size $script:mockedPartitionSize ` -Verbose @@ -1382,12 +1484,12 @@ try } } - Context 'Online GPT disk with a partition/volume and wrong Drive Letter assigned using Disk Number' { + Context 'When online GPT disk with a partition/volume and wrong Drive Letter assigned using Disk Number' { # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -1421,7 +1523,7 @@ try It 'Should not throw an exception' { { Set-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -Driveletter 'H' ` -Verbose } | Should -Not -Throw @@ -1441,12 +1543,12 @@ try } } - Context 'Online GPT disk with a partition/volume and no Drive Letter assigned using Disk Number' { + Context 'When online GPT disk with a partition/volume and no Drive Letter assigned using Disk Number' { # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -1472,7 +1574,7 @@ try It 'Should not throw an exception' { { Set-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -Driveletter 'H' ` -Verbose } | Should -Not -Throw @@ -1492,12 +1594,12 @@ try } } - Context 'Online GPT disk with a partition/volume and wrong Volume Label assigned using Disk Number' { + Context 'When online GPT disk with a partition/volume and wrong Volume Label assigned using Disk Number' { # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -1524,7 +1626,7 @@ try It 'Should not throw an exception' { { Set-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -Driveletter $script:testDriveLetter ` -FSLabel 'NewLabel' ` -Verbose @@ -1551,7 +1653,7 @@ try Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -1584,7 +1686,7 @@ try It 'Should not throw an exception' { { Set-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -Driveletter $script:testDriveLetter ` -Size ($script:mockedPartitionSize + 1024) ` -AllowDestructive $true ` @@ -1613,7 +1715,7 @@ try Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -1649,7 +1751,7 @@ try It 'Should throw FreeSpaceViolationError' { { Set-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -Driveletter $script:testDriveLetter ` -Size ($script:mockedPartitionSize + 1024) ` -AllowDestructive $true ` @@ -1680,7 +1782,7 @@ try Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -1721,7 +1823,7 @@ try It 'Should not throw' { { Set-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -Driveletter $script:testDriveLetter ` -AllowDestructive $true ` -FSLabel 'NewLabel' ` @@ -1751,7 +1853,7 @@ try Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -1790,7 +1892,7 @@ try It 'Should not throw an exception' { { Set-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -Driveletter $script:testDriveLetter ` -Size ($script:mockedPartitionSize + 1024) ` -AllowDestructive $true ` @@ -1822,7 +1924,7 @@ try Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -1852,7 +1954,7 @@ try It 'Should not throw an exception' { { Set-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -Driveletter $script:testDriveLetter ` -Size $script:mockedPartitionSize ` -FSFormat 'ReFS' ` @@ -1876,12 +1978,12 @@ try } } - Context 'When AllowDestructive enabled with Online GPT disk containing arbitrary partitions' { + Context 'When AllowDestructive and ClearDisk enabled with Online GPT disk containing arbitrary partitions' { # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -1897,6 +1999,7 @@ try Mock ` -CommandName Set-Volume ` -Verifiable + Mock ` -CommandName Clear-Disk ` -Verifiable @@ -1911,7 +2014,7 @@ try It 'Should not throw an exception' { { Set-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -Driveletter $script:testDriveLetter ` -Size $script:mockedPartitionSize ` -FSLabel 'NewLabel' ` @@ -1935,11 +2038,102 @@ try Assert-MockCalled -CommandName Set-Volume -Exactly -Times 1 } } + + Context 'When AllowDestructive and ClearDisk enabled with Online MBR disk containing arbitrary partitions but GPT required' { + <# + This variable is so that we can change the behavior of the + Get-DiskByIdentifier mock after the first time it is called + in the Set-TargetResource function. + #> + $script:getDiskByIdentifierCalled = $false + + $script:parameterFilter_MockedDisk0Number = { + $DiskId -eq $script:mockedDisk0Gpt.Number -and $DiskIdType -eq 'Number' + } + + # verifiable (should be called) mocks + Mock ` + -CommandName Get-DiskByIdentifier ` + -ParameterFilter { + $DiskId -eq $script:mockedDisk0Gpt.Number ` + -and $DiskIdType -eq 'Number' ` + -and $script:getDiskByIdentifierCalled -eq $false + } ` + -MockWith { + $script:getDiskByIdentifierCalled = $true + return $script:mockedDisk0Mbr + } ` + -Verifiable + + Mock ` + -CommandName Get-DiskByIdentifier ` + -ParameterFilter { + $DiskId -eq $script:mockedDisk0Gpt.Number ` + -and $DiskIdType -eq 'Number' ` + -and $script:getDiskByIdentifierCalled -eq $true + } ` + -MockWith { + return $script:mockedDisk0Raw + } ` + -Verifiable + + Mock ` + -CommandName Get-Partition ` + -MockWith { $script:mockedPartition } ` + -Verifiable + + Mock ` + -CommandName Get-Volume ` + -MockWith { $script:mockedVolume } ` + -Verifiable + + Mock ` + -CommandName Set-Volume ` + -Verifiable + + Mock ` + -CommandName Clear-Disk ` + -Verifiable + + # mocks that should not be called + Mock -CommandName Set-Disk + Mock -CommandName Initialize-Disk + Mock -CommandName New-Partition + Mock -CommandName Format-Volume + Mock -CommandName Set-Partition + + It 'Should not throw an exception' { + { + Set-TargetResource ` + -DiskId $script:mockedDisk0Gpt.Number ` + -Driveletter $script:testDriveLetter ` + -Size $script:mockedPartitionSize ` + -FSLabel 'NewLabel' ` + -AllowDestructive $true ` + -ClearDisk $true ` + -Verbose + } | Should -Not -Throw + } + + It 'Should call the correct mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Get-DiskByIdentifier -Exactly -Times 2 ` + -ParameterFilter $script:parameterFilter_MockedDisk0Number + Assert-MockCalled -CommandName Set-Disk -Exactly -Times 0 + Assert-MockCalled -CommandName Initialize-Disk -Exactly -Times 1 + Assert-MockCalled -CommandName Get-Partition -Exactly -Times 1 + Assert-MockCalled -CommandName Get-Volume -Exactly -Times 1 + Assert-MockCalled -CommandName New-Partition -Exactly -Times 0 + Assert-MockCalled -CommandName Format-Volume -Exactly -Times 0 + Assert-MockCalled -CommandName Set-Partition -Exactly -Times 0 + Assert-MockCalled -CommandName Set-Volume -Exactly -Times 1 + } + } } #endregion #region Function Test-TargetResource - Describe 'MSFT_Disk\Test-TargetResource' { + Describe 'MSFTDSC_Disk\Test-TargetResource' { Mock ` -CommandName Get-CimInstance ` -MockWith { $script:mockedCim } @@ -1949,7 +2143,7 @@ try Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0Offline } ` + -MockWith { $script:mockedDisk0GptOffline } ` -Verifiable # mocks that should not be called @@ -1962,7 +2156,7 @@ try It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -DiskId $script:mockedDisk0Offline.Number ` + -DiskId $script:mockedDisk0GptOffline.Number ` -DriveLetter $script:testDriveLetter ` -AllocationUnitSize 4096 ` -Verbose @@ -1987,8 +2181,8 @@ try # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0Offline.Number -and $DiskIdType -eq 'Number' } ` - -MockWith { $script:mockedDisk0Offline } ` + -ParameterFilter { $DiskId -eq $script:mockedDisk0GptOffline.Number -and $DiskIdType -eq 'Number' } ` + -MockWith { $script:mockedDisk0GptOffline } ` -Verifiable # mocks that should not be called @@ -2001,7 +2195,7 @@ try It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -DiskId $script:mockedDisk0Offline.Number ` + -DiskId $script:mockedDisk0GptOffline.Number ` -DriveLetter $script:testDriveLetter ` -AllocationUnitSize 4096 ` -Verbose @@ -2015,7 +2209,7 @@ try It 'Should call the correct mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Get-DiskByIdentifier -Exactly -Times 1 ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0Offline.Number -and $DiskIdType -eq 'Number' } + -ParameterFilter { $DiskId -eq $script:mockedDisk0GptOffline.Number -and $DiskIdType -eq 'Number' } Assert-MockCalled -CommandName Get-Partition -Exactly -Times 0 Assert-MockCalled -CommandName Get-Volume -Exactly -Times 0 Assert-MockCalled -CommandName Get-CimInstance -Exactly -Times 0 @@ -2026,8 +2220,8 @@ try # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0.UniqueId -and $DiskIdType -eq 'UniqueId' } ` - -MockWith { $script:mockedDisk0Offline } ` + -ParameterFilter { $DiskId -eq $script:mockedDisk0Gpt.UniqueId -and $DiskIdType -eq 'UniqueId' } ` + -MockWith { $script:mockedDisk0GptOffline } ` -Verifiable # mocks that should not be called @@ -2040,7 +2234,7 @@ try It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -DiskId $script:mockedDisk0Offline.UniqueId ` + -DiskId $script:mockedDisk0GptOffline.UniqueId ` -DiskIdType 'UniqueId' ` -DriveLetter $script:testDriveLetter ` -AllocationUnitSize 4096 ` @@ -2055,7 +2249,7 @@ try It 'Should call the correct mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Get-DiskByIdentifier -Exactly -Times 1 ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0Offline.UniqueId -and $DiskIdType -eq 'UniqueId' } + -ParameterFilter { $DiskId -eq $script:mockedDisk0GptOffline.UniqueId -and $DiskIdType -eq 'UniqueId' } Assert-MockCalled -CommandName Get-Partition -Exactly -Times 0 Assert-MockCalled -CommandName Get-Volume -Exactly -Times 0 Assert-MockCalled -CommandName Get-CimInstance -Exactly -Times 0 @@ -2066,8 +2260,8 @@ try # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0.Guid -and $DiskIdType -eq 'Guid' } ` - -MockWith { $script:mockedDisk0Offline } ` + -ParameterFilter { $DiskId -eq $script:mockedDisk0Gpt.Guid -and $DiskIdType -eq 'Guid' } ` + -MockWith { $script:mockedDisk0GptOffline } ` -Verifiable # mocks that should not be called @@ -2080,7 +2274,7 @@ try It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -DiskId $script:mockedDisk0Offline.Guid ` + -DiskId $script:mockedDisk0GptOffline.Guid ` -DiskIdType 'Guid' ` -DriveLetter $script:testDriveLetter ` -AllocationUnitSize 4096 ` @@ -2095,7 +2289,7 @@ try It 'Should call the correct mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Get-DiskByIdentifier -Exactly -Times 1 ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0Offline.Guid -and $DiskIdType -eq 'Guid' } + -ParameterFilter { $DiskId -eq $script:mockedDisk0GptOffline.Guid -and $DiskIdType -eq 'Guid' } Assert-MockCalled -CommandName Get-Partition -Exactly -Times 0 Assert-MockCalled -CommandName Get-Volume -Exactly -Times 0 Assert-MockCalled -CommandName Get-CimInstance -Exactly -Times 0 @@ -2106,8 +2300,8 @@ try # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0Readonly.Number -and $DiskIdType -eq 'Number' } ` - -MockWith { $script:mockedDisk0Readonly } ` + -ParameterFilter { $DiskId -eq $script:mockedDisk0GptReadonly.Number -and $DiskIdType -eq 'Number' } ` + -MockWith { $script:mockedDisk0GptReadonly } ` -Verifiable # mocks that should not be called @@ -2120,7 +2314,7 @@ try It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -DiskId $script:mockedDisk0Readonly.Number ` + -DiskId $script:mockedDisk0GptReadonly.Number ` -DriveLetter $script:testDriveLetter ` -AllocationUnitSize 4096 ` -Verbose @@ -2134,7 +2328,7 @@ try It 'Should call the correct mocks' { Assert-VerifiableMock Assert-MockCalled -CommandName Get-DiskByIdentifier -Exactly -Times 1 ` - -ParameterFilter { $DiskId -eq $script:mockedDisk0Readonly.Number -and $DiskIdType -eq 'Number' } + -ParameterFilter { $DiskId -eq $script:mockedDisk0GptReadonly.Number -and $DiskIdType -eq 'Number' } Assert-MockCalled -CommandName Get-Partition -Exactly -Times 0 Assert-MockCalled -CommandName Get-Volume -Exactly -Times 0 Assert-MockCalled -CommandName Get-CimInstance -Exactly -Times 0 @@ -2180,12 +2374,129 @@ try } } + Context 'When testing online disk using Disk Number with partition style GPT but requiring MBR' { + # verifiable (should be called) mocks + Mock ` + -CommandName Get-DiskByIdentifier ` + -ParameterFilter $script:parameterFilter_MockedDisk0Number ` + -MockWith { $script:mockedDisk0Mbr } ` + -Verifiable + + # mocks that should not be called + Mock -CommandName Get-Volume + Mock -CommandName Get-Partition + Mock -CommandName Get-CimInstance + + $errorRecord = Get-InvalidOperationRecord ` + -Message ($LocalizedData.DiskInitializedWithWrongPartitionStyleError -f ` + 'Number', $script:mockedDisk0Mbr.Number, $script:mockedDisk0Mbr.PartitionStyle, 'GPT') + + It 'Should throw DiskInitializedWithWrongPartitionStyleError' { + { + Test-TargetResource ` + -DiskId $script:mockedDisk0Mbr.Number ` + -DriveLetter $script:testDriveLetter ` + -AllocationUnitSize 4096 ` + -Verbose + } | Should -Throw $errorRecord + } + + It 'Should call the correct mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Get-DiskByIdentifier -Exactly -Times 1 ` + -ParameterFilter $script:parameterFilter_MockedDisk0Number + Assert-MockCalled -CommandName Get-Partition -Exactly -Times 0 + Assert-MockCalled -CommandName Get-Volume -Exactly -Times 0 + Assert-MockCalled -CommandName Get-CimInstance -Exactly -Times 0 + } + } + + Context 'When testing online disk using Disk Number with partition style MBR but requiring GPT' { + # verifiable (should be called) mocks + Mock ` + -CommandName Get-DiskByIdentifier ` + -ParameterFilter $script:parameterFilter_MockedDisk0Number ` + -MockWith { $script:mockedDisk0Gpt } ` + -Verifiable + + # mocks that should not be called + Mock -CommandName Get-Volume + Mock -CommandName Get-Partition + Mock -CommandName Get-CimInstance + + $errorRecord = Get-InvalidOperationRecord ` + -Message ($LocalizedData.DiskInitializedWithWrongPartitionStyleError -f ` + 'Number', $script:mockedDisk0Gpt.Number, $script:mockedDisk0Gpt.PartitionStyle, 'MBR') + + It 'Should throw DiskInitializedWithWrongPartitionStyleError' { + { + Test-TargetResource ` + -DiskId $script:mockedDisk0Gpt.Number ` + -DriveLetter $script:testDriveLetter ` + -AllocationUnitSize 4096 ` + -PartitionStyle 'MBR' ` + -Verbose + } | Should -Throw $errorRecord + } + + It 'Should call the correct mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Get-DiskByIdentifier -Exactly -Times 1 ` + -ParameterFilter $script:parameterFilter_MockedDisk0Number + Assert-MockCalled -CommandName Get-Partition -Exactly -Times 0 + Assert-MockCalled -CommandName Get-Volume -Exactly -Times 0 + Assert-MockCalled -CommandName Get-CimInstance -Exactly -Times 0 + } + } + + Context 'When testing online disk using Disk Number with partition style MBR but requiring GPT and AllowDestructive and ClearDisk is True' { + # verifiable (should be called) mocks + Mock ` + -CommandName Get-DiskByIdentifier ` + -ParameterFilter $script:parameterFilter_MockedDisk0Number ` + -MockWith { $script:mockedDisk0Gpt } ` + -Verifiable + + # mocks that should not be called + Mock -CommandName Get-Volume + Mock -CommandName Get-Partition + Mock -CommandName Get-CimInstance + + $script:result = $null + + It 'Should not throw an exception' { + { + $script:result = Test-TargetResource ` + -DiskId $script:mockedDisk0Gpt.Number ` + -DriveLetter $script:testDriveLetter ` + -AllocationUnitSize 4096 ` + -PartitionStyle 'MBR' ` + -AllowDestructive $true ` + -ClearDisk $true ` + -Verbose + } | Should -Not -Throw + } + + It 'Should be false' { + $script:result | Should -Be $false + } + + It 'Should call the correct mocks' { + Assert-VerifiableMock + Assert-MockCalled -CommandName Get-DiskByIdentifier -Exactly -Times 1 ` + -ParameterFilter $script:parameterFilter_MockedDisk0Number + Assert-MockCalled -CommandName Get-Partition -Exactly -Times 0 + Assert-MockCalled -CommandName Get-Volume -Exactly -Times 0 + Assert-MockCalled -CommandName Get-CimInstance -Exactly -Times 0 + } + } + Context 'When testing mismatching partition size using Disk Number' { # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -2208,7 +2519,7 @@ try It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -DriveLetter $script:testDriveLetter ` -AllocationUnitSize 4096 ` -Size ($script:mockedPartitionSize + 1MB) ` @@ -2235,7 +2546,7 @@ try Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -2253,7 +2564,7 @@ try It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -DriveLetter $script:testDriveLetter ` -AllocationUnitSize 4096 ` -Size ($script:mockedPartitionSize + 1MB) ` @@ -2282,7 +2593,7 @@ try Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -2315,7 +2626,7 @@ try It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -DriveLetter $script:testDriveLetter ` -AllocationUnitSize 4096 ` -Verbose @@ -2342,7 +2653,7 @@ try Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -2369,7 +2680,7 @@ try It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -DriveLetter $script:testDriveLetter ` -AllocationUnitSize 4096 ` -AllowDestructive $true ` @@ -2396,7 +2707,7 @@ try # verifiable (should be called) mocks Mock ` -CommandName Get-DiskByIdentifier ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -2417,7 +2728,7 @@ try It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -DriveLetter $script:testDriveLetter ` -AllocationUnitSize 4097 ` -AllowDestructive $true ` @@ -2443,7 +2754,7 @@ try Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -2466,7 +2777,7 @@ try It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -DriveLetter $script:testDriveLetter ` -FSFormat 'ReFS' ` -Verbose @@ -2492,7 +2803,7 @@ try Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -2515,7 +2826,7 @@ try It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -DriveLetter $script:testDriveLetter ` -FSFormat 'ReFS' ` -AllowDestructive $true ` @@ -2542,7 +2853,7 @@ try Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -2565,7 +2876,7 @@ try It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -DriveLetter $script:testDriveLetter ` -FSLabel 'NewLabel' ` -Verbose @@ -2591,7 +2902,7 @@ try Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -2612,7 +2923,7 @@ try It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -DriveLetter 'Z' ` -Verbose } | Should -Not -Throw @@ -2637,7 +2948,7 @@ try Mock ` -CommandName Get-DiskByIdentifier ` -ParameterFilter $script:parameterFilter_MockedDisk0Number ` - -MockWith { $script:mockedDisk0 } ` + -MockWith { $script:mockedDisk0Gpt } ` -Verifiable Mock ` @@ -2660,7 +2971,7 @@ try It 'Should not throw an exception' { { $script:result = Test-TargetResource ` - -DiskId $script:mockedDisk0.Number ` + -DiskId $script:mockedDisk0Gpt.Number ` -DriveLetter $script:testDriveLetter ` -AllocationUnitSize 4096 ` -Size $script:mockedPartition.Size `