testutil

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2026 License: MPL-2.0 Imports: 4 Imported by: 0

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

View Source
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.

View Source
const NatStaticMappingTCPFixtureJSON = `` /* 330-byte string literal not displayed */

NatStaticMappingTCPFixtureJSON is the canonical eleven-field shape that nat_static_mapping/{get,new,set}.ps1 emit. Locked by the Pester contract; single source of truth across the typed-client and resource-layer suites. Composite Id encodes (NatName:Protocol:ExternalIP:ExternalPort) with lowercase protocol; the structured Protocol field is uppercase because that's what Get-NetNatStaticMapping reports natively.

View Source
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).

View Source
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.

View Source
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).

View Source
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.

View Source
const VMHostFixtureJSON = `` /* 255-byte string literal not displayed */

VMHostFixtureJSON is the canonical Get-VMHost output captured against a real Windows 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 field data while the other catches the change.

View Source
const VMSwitchExternalFixtureJSON = `` /* 289-byte string literal not displayed */

VMSwitchExternalFixtureJSON is the canonical nine-field shape (six base + three NAT) 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. NAT fields are empty strings on non-NAT switches.

View Source
const VMSwitchNATFixtureJSON = `` /* 289-byte string literal not displayed */

VMSwitchNATFixtureJSON is the NAT-switch variant. SwitchType is the synthesized "NAT" string (not Hyper-V's underlying Internal enum value); NatName / NatInternalAddressPrefix / NatHostAddress are populated. Used by typed-client tests to lock the joined Get-VMSwitch + Get-NetNat + Get-NetIPAddress shape get.ps1 emits when nat_name is supplied.

View Source
const VMSwitchPrivateFixtureJSON = `` /* 258-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 Call

type Call struct {
	Script    string
	StdinJSON []byte
}

Call captures one RunScript invocation for after-the-fact assertions.

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.

StreamFile calls are recorded separately (see StreamCalls) and return whatever error has been registered via SetStreamFileErr (default nil). The fake never touches the local or remote filesystem -- tests asserting "the resource asked to stream foo.iso to C:/iso/foo.iso" should inspect the recorded StreamCall, not look on disk.

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.

func (*FakeRunner) SetStreamFileErr added in v0.2.0

func (f *FakeRunner) SetStreamFileErr(err error) *FakeRunner

SetStreamFileErr makes subsequent StreamFile calls return the given error. Useful for asserting that a resource maps a transport-level stream failure to the expected diagnostic. Pass nil to reset.

func (*FakeRunner) StreamCalls added in v0.2.0

func (f *FakeRunner) StreamCalls() []StreamCall

StreamCalls returns a copy of the recorded StreamFile invocations.

func (*FakeRunner) StreamFile added in v0.2.0

func (f *FakeRunner) StreamFile(_ context.Context, localPath, remotePath string) error

StreamFile implements connection.Runner. Records the call and returns the error set via SetStreamFileErr (nil by default). Doesn't touch the filesystem -- localPath need not exist for the fake to accept the call.

type Response

type Response struct {
	Stdout   string
	Stderr   string
	ExitCode int
	// Err is returned as the transport-level error. Use to simulate
	// connection-refused, ctx-canceled, etc.
	Err error
}

Response is a single canned answer.

type StreamCall added in v0.2.0

type StreamCall struct {
	LocalPath  string
	RemotePath string
}

StreamCall captures one StreamFile invocation for after-the-fact assertions. The fake doesn't touch the filesystem; tests that need to confirm the resource asked for the right paths inspect this record.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL