| 1 | #requires -Version 5.1 |
| 2 | # Simulate a Hyper-V host which defaults to dynamic memory on New-VM. |
| 3 | $ErrorActionPreference = 'Stop' |
| 4 | $path = Join-Path $PSScriptRoot 'new-windows-test-vm.ps1' |
| 5 | $errors = $null |
| 6 | $ast = [Management.Automation.Language.Parser]::ParseFile($path, [ref]$null, [ref]$errors) |
| 7 | if ($errors.Count) { throw $errors[0] } |
| 8 | $function = $ast.Find({ |
| 9 | param($node) |
| 10 | $node -is [Management.Automation.Language.FunctionDefinitionAst] -and $node.Name -eq 'Set-TestVMResources' |
| 11 | }, $true) |
| 12 | . ([scriptblock]::Create($function.Extent.Text)) |
| 13 | |
| 14 | $vm = [pscustomobject]@{ DynamicMemoryEnabled = $true; Startup = 4GB } |
| 15 | function Set-VM { |
| 16 | param($VM, $ProcessorCount, [switch]$StaticMemory, $MemoryStartupBytes, $AutomaticStartAction, $AutomaticCheckpointsEnabled) |
| 17 | if (!$script:ignoreStaticMode -and $StaticMemory) { $VM.DynamicMemoryEnabled = $false } |
| 18 | $VM.Startup = $MemoryStartupBytes |
| 19 | } |
| 20 | function Get-VMMemory { param($VM) $VM } |
| 21 | Set-TestVMResources $vm 4 8GB |
| 22 | if ($vm.DynamicMemoryEnabled -or $vm.Startup -ne 8GB) { throw 'Guest can exceed the requested memory.' } |
| 23 | |
| 24 | $vm.DynamicMemoryEnabled = $true |
| 25 | $script:ignoreStaticMode = $true |
| 26 | try { |
| 27 | Set-TestVMResources $vm 4 8GB |
| 28 | } catch { |
| 29 | if (!$_.Exception.Message.Contains('requested fixed budget')) { throw } |
| 30 | Write-Output 'Passed fixed guest memory budgeting and rejection of inconsistent Hyper-V readback.' |
| 31 | return |
| 32 | } |
| 33 | throw 'Ignored static-memory request was reported as successful.' |