Documentation
¶
Overview ¶
Package testutil provides shared test helpers — primarily a deterministic fake of connection.Runner that lets typed-client tests exercise the JSON contract without needing a real Hyper-V host or even a real pwsh binary.
Index ¶
Constants ¶
const ImageFileFixtureJSON = `` /* 157-byte string literal not displayed */
ImageFileFixtureJSON is the canonical three-field shape that image_file/{get,new}.ps1 emit. SizeBytes is deliberately above 2^31 (5 GiB) so int64 round-tripping is exercised -- a default-precision JSON number would land in float64 and lose precision above 2^53, but a careless int32 decode would overflow well before that.
const VHDDifferencingFixtureJSON = `` /* 241-byte string literal not displayed */
VHDDifferencingFixtureJSON exercises the parent-path round-trip and the "size inherited from parent" semantic (SizeBytes matches the parent's declared size; FileSize is small because the child has no writes yet).
const VHDDynamicFixtureJSON = `` /* 214-byte string literal not displayed */
VHDDynamicFixtureJSON is the canonical eight-field shape vhd/{get,new,set}.ps1 emit for a sparse dynamic VHDX. Size is the declared 32 GiB; FileSize is the actual sparse on-disk size after creation (tiny). ParentPath is empty because dynamic disks have no parent. Format is uppercase "VHDX" because that's what Get-VHD's VhdFormat enum's ToString() emits on a real host (verified against Server 2019 in the M4 smoke test); the Pester _test_helpers stub mirrors this.
const VMGen1FixtureJSON = `` /* 489-byte string literal not displayed */
VMGen1FixtureJSON exercises the gen-1 case: SecureBootEnabled is null because Get-VMFirmware doesn't apply on gen 1 (BIOS, not UEFI).
const VMGen2FixtureJSON = `` /* 499-byte string literal not displayed */
VMGen2FixtureJSON is the canonical shape vm/{get,new,set}.ps1 emit for a small gen 2 VM. SecureBootEnabled is the gen-2-only field -- always non-null here to exercise the *bool unmarshal. HardDiskDrives is the always-array shape the script's @() wrapper guarantees on the wire, so empty here decodes into an empty (non-nil) slice on the Go side.
const VMHostFixtureJSON = `` /* 255-byte string literal not displayed */
VMHostFixtureJSON is the canonical Get-VMHost output captured by spike #2 against a real Server 2022 host. Single source of truth for tests across internal/hyperv and internal/datasources/host — drift between packages would otherwise let one suite pass against stale shape data while the other catches the change. See docs/spikes/02-json-contract.md.
const VMSwitchExternalFixtureJSON = `` /* 217-byte string literal not displayed */
VMSwitchExternalFixtureJSON is the canonical six-field shape that vswitch/{get,new,set}.ps1 emit, locked by the Pester contract tests. Single source of truth across the typed-client and resource-layer suites.
const VMSwitchPrivateFixtureJSON = `` /* 186-byte string literal not displayed */
VMSwitchPrivateFixtureJSON is the Private-switch variant -- no NIC description, no AllowManagementOS toggle in practice (the cmdlet ignores it). Useful for resource-layer tests that need a non-External shape.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FakeRunner ¶
type FakeRunner struct {
// contains filtered or unexported fields
}
FakeRunner is a deterministic, table-driven implementation of connection.Runner. Tests register canned responses keyed by a script identifier (typically the script's filename or a substring); RunScript looks them up by substring match against the caller's `script` argument.
Usage:
fr := testutil.NewFakeRunner().
On("vswitch/get.ps1").Return(`{"Name":"foo","SwitchType":"Internal"}`, "", 0).
On("vswitch/new.ps1").Return("", `{"category":"ResourceUnavailable"}`, 1)
client := hyperv.NewClient(fr) // typed client takes a Runner
Substring matching keeps tests resilient to script-content changes while pinning to script identity. The `script` argument the typed client will pass embeds the script's path as a header comment; see hyperv/script.go.
func NewFakeRunner ¶
func NewFakeRunner() *FakeRunner
NewFakeRunner returns an empty fake. Register responses with On(...).
func (*FakeRunner) Calls ¶
func (f *FakeRunner) Calls() []Call
Calls returns a copy of the recorded invocations, in order. Useful for asserting that a test caused the expected sequence of script runs.
func (*FakeRunner) On ¶
func (f *FakeRunner) On(scriptSubstring string) *responseBuilder
On registers a stub for any RunScript whose `script` argument contains the given substring. Returns a builder for fluent .Return(...).
func (*FakeRunner) RunScript ¶
func (f *FakeRunner) RunScript(_ context.Context, script string, stdinJSON []byte) (connection.Result, error)
RunScript implements connection.Runner. Looks up a registered response by substring match. If multiple substrings match, the longest match wins (more specific keys override broader ones). If none match, returns a clear error so tests fail loudly rather than silently dispatching to a "default" response.