Skip to content

Commit

Permalink
Ensure Get-VMHyperV returns correct num VMs
Browse files Browse the repository at this point in the history
  • Loading branch information
nyanhp committed Apr 20, 2023
1 parent 881cbbf commit f04fab3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
7 changes: 0 additions & 7 deletions source/DSCResources/DSC_VMHyperV/DSC_VMHyperV.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -907,13 +907,6 @@ function Test-TargetResource
}
}

# If IsClustered is set in configuration
if ($PSBoundParameters.ContainsKey('IsClustered') -and -not $vmObj.IsClustered)
{
Write-Verbose -Message ($script:localizedData.NotAddedToCluster -f $vmObj.Name)
return $false
}

return $true
}
else
Expand Down
32 changes: 17 additions & 15 deletions source/Modules/HyperVDsc.Common/HyperVDsc.Common.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ function ConvertFrom-TimeSpan
#>
function Get-VMHyperV
{
[OutputType([System.Collections.ArrayList])]
[CmdletBinding()]
param
(
Expand All @@ -321,44 +322,45 @@ function Get-VMHyperV

if (Get-Command -Name Get-Cluster -ErrorAction SilentlyContinue)
{
[bool] $isClustered = (Get-Cluster -WarningAction SilentlyContinue) -ne $null
[bool] $isClustered = $null -ne (Get-Cluster -ErrorAction SilentlyContinue -WarningAction SilentlyContinue)
}

$vms = [System.Collections.ArrayList]::new()

if ($isClustered)
{
$clusterVm = Get-ClusterGroup -Name $VMName -ErrorAction SilentlyContinue
if (-not $clusterVm -and $ThrowOnEmpty.IsPresent)

if ($clusterVm)
{
$errorMessage = $script:localizedData.NoVMExistsError -f $VMName
New-InvalidResultException -Message $errorMessage
[Microsoft.HyperV.PowerShell.VirtualMachine[]]$clusteredVm = Get-VM @vmParam -ClusterObject $clusterVm
}

if (-not $clusterVm)
if ($clusteredVm)
{
return
$vms.AddRange($clusteredVm)
}

$vmParam['ClusterObject'] = $clusterVm
}
else

[Microsoft.HyperV.PowerShell.VirtualMachine[]]$nonClusteredVm = Get-VM @vmParam -VMName $VMName | Where-Object IsClustered -eq $false

if ($nonClusteredVm)
{
$vmParam['Name'] = $VMName
$vms.AddRange($nonClusteredVm)
}

$vm = Get-VM @vmParam

# Check if 1 or 0 VM with name = $name exist
if ($vm.count -gt 1)
if ($vms.count -gt 1)
{
$errorMessage = $script:localizedData.MoreThanOneVMExistsError -f $VMName
New-InvalidResultException -Message $errorMessage
}

if (-not $vm -and $ThrowOnEmpty.IsPresent)
if (-not $vms -and $ThrowOnEmpty.IsPresent)
{
$errorMessage = $script:localizedData.NoVMExistsError -f $VMName
New-InvalidResultException -Message $errorMessage
}

return $vm
return $vms
} #end function Get-VMHyperV
4 changes: 0 additions & 4 deletions tests/Unit/DSC_VMHyperV.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,6 @@ try
$targetResource = Get-TargetResource -Name 'VMWithAutomaticCheckpoints' -VhdPath $stubVhdxDisk.Path
$targetResource.ContainsKey('AutomaticCheckpointsEnabled') | Should -Be $true
}
It 'Hash table contains key IsClustered' {
$targetResource = Get-TargetResource -Name 'ClusteredExistingVmInClusterPresent' -VhdPath $stubVhdxDisk.Path
$targetResource.ContainsKey('IsClustered') | Should -Be $true
}
It 'throws when Hyper-V Tools are not installed' {
# This test needs to be the last in the Context otherwise all subsequent Get-Module checks will fail
Mock -CommandName Get-Module -ParameterFilter { ($Name -eq 'Hyper-V') -and ($ListAvailable -eq $true) }
Expand Down
28 changes: 16 additions & 12 deletions tests/Unit/HyperVDsc.Common.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,9 @@ Describe 'HyperVDsc.Common\Get-VMHyperV' {

It 'Should not throw when one VM is found' {
Mock -CommandName Get-VM -ModuleName 'HyperVDsc.Common' -MockWith {
[PSCustomObject] @{
Name = $VMName
}
$mockVm = [Microsoft.HyperV.PowerShell.VirtualMachine]::CreateTypeInstance()
$mockVm.Name = $VMName
$mockVm
}

$result = Get-VMHyperV -VMName $mockVMName
Expand All @@ -313,12 +313,9 @@ Describe 'HyperVDsc.Common\Get-VMHyperV' {
It 'Should throw when more than one VM is found' {
Mock -CommandName Get-VM -ModuleName 'HyperVDsc.Common' -MockWith {
@(
[PSCustomObject] @{
Name = $VMName
},
[PSCustomObject] @{
Name = $VMName
}
$mockVm = [Microsoft.HyperV.PowerShell.VirtualMachine]::CreateTypeInstance()
$mockVm.Name = $VMName
@($mockVm, $mockVm)
)
}

Expand All @@ -340,21 +337,28 @@ Describe 'HyperVDsc.Common\Get-VMHyperV' {
[Microsoft.FailoverClusters.PowerShell.ClusterObject]::new()
}
Mock -CommandName Get-VM -ModuleName 'HyperVDsc.Common' -MockWith {
[PSCustomObject] @{
Name = $VMName
}
$mockVm = [Microsoft.HyperV.PowerShell.VirtualMachine]::CreateTypeInstance()
$mockVm.Name = $VMName
# Test in case local cluster node is holding the machine object
$mockVm.IsClustered = $true
$mockVm
}

$result = Get-VMHyperV -VMName $mockVMName
$result.Name | Should -Be $mockVMName
}

It 'Should not throw when no clustered VM is found' {
Mock -CommandName Get-ClusterGroup -ModuleName 'HyperVDsc.Common'
Mock -CommandName Get-VM -ModuleName 'HyperVDsc.Common'

$result = Get-VMHyperV -VMName $mockVMName
$result | Should -BeNullOrEmpty
}

It 'Should throw when no clustered VM is found and caller wants it to' {
Mock -CommandName Get-ClusterGroup -ModuleName 'HyperVDsc.Common'
Mock -CommandName Get-VM -ModuleName 'HyperVDsc.Common'
{ Get-VMHyperV -VMName $mockVMName -ThrowOnEmpty -ErrorAction Stop} | Should -Throw -ExpectedMessage "No VM with the name '$mockVMName' exists."
}
}
Expand Down

0 comments on commit f04fab3

Please sign in to comment.