Skip to content

Commit 6717f19

Browse files
committed
feat: Add Windows guest helper tests and update Makefile
- Introduced Pester tests for the Windows guest helper script (`filerestore.bat`). - Updated Makefile to include a new target for running Windows tests. - Created `TESTING.md` for Windows guest helper with instructions on running tests. - Refactored `filerestore.bat` to support testing and improve argument parsing. - Added `TestHelper.psm1` for shared functions used in Pester tests. - Implemented comprehensive Pester tests covering various scenarios for the Windows guest helper. Signed-off-by: Emanuele Prella <eprella@redhat.com>
1 parent ceec1be commit 6717f19

6 files changed

Lines changed: 792 additions & 160 deletions

File tree

Makefile

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,27 @@ test-e2e: manifests generate fmt vet ## Run e2e tests (requires kubevirtci clust
168168
##@ Script Tests
169169

170170
CONTAINER_ENGINE ?= $(CONTAINER_TOOL)
171+
# mcr.microsoft.com/powershell is deprecated; the .NET SDK image is the
172+
# recommended replacement and includes pwsh.
173+
# https://learn.microsoft.com/en-us/powershell/scripting/install/powershell-in-docker
174+
PWSH_IMAGE ?= mcr.microsoft.com/dotnet/sdk:9.0
175+
PESTER_VERSION ?= 5.7.1
171176
BATS_IMAGE ?= bats/bats:1.13.0
172177
# :Z relabels for SELinux; use empty value on Docker Desktop (macOS/Windows)
173178
VOLUME_OPTS ?= :Z
174179

175180
.PHONY: test-scripts
176-
test-scripts: test-scripts-linux ## Run guest-helper script tests
181+
test-scripts: test-scripts-windows test-scripts-linux ## Run guest-helper script tests
182+
183+
.PHONY: test-scripts-windows
184+
test-scripts-windows: ## Run Pester tests for Windows guest helper (containerized)
185+
$(CONTAINER_ENGINE) run --rm \
186+
-v $(shell pwd):/workspace$(VOLUME_OPTS) \
187+
-w /workspace \
188+
$(PWSH_IMAGE) \
189+
pwsh -NoProfile -Command " \
190+
Install-Module Pester -RequiredVersion $(PESTER_VERSION) -Force -SkipPublisherCheck -Scope CurrentUser; \
191+
Invoke-Pester -Path 'guest-helpers/windows/test/' -Output Detailed -CI"
177192

178193
.PHONY: test-scripts-linux
179194
test-scripts-linux: ## Run BATS tests for Linux guest helper (containerized)

guest-helpers/linux/TESTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ Unit tests for the Linux guest helper script (`filerestore.sh`).
77
Run script tests (requires Docker or Podman):
88

99
```bash
10-
make test-scripts
10+
make test-scripts-linux
1111
```
1212

1313
By default, `CONTAINER_ENGINE` inherits from `CONTAINER_TOOL`, which defaults to `docker`. To override:
1414

1515
```bash
16-
CONTAINER_ENGINE=docker make test-scripts
16+
CONTAINER_ENGINE=docker make test-scripts-linux
1717
```
1818

1919
## Prerequisites

guest-helpers/windows/TESTING.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Guest Helper Script Tests
2+
3+
Unit tests for the Windows guest helper script (`filerestore.bat`).
4+
5+
## Quick Start
6+
7+
Run script tests (requires Docker or Podman):
8+
9+
```bash
10+
make test-scripts-windows
11+
```
12+
13+
By default, `CONTAINER_ENGINE` inherits from `CONTAINER_TOOL`, which defaults to `docker`. To override:
14+
15+
```bash
16+
CONTAINER_ENGINE=docker make test-scripts-windows
17+
```
18+
19+
## Prerequisites
20+
21+
Only a container runtime (Docker or Podman) is needed:
22+
23+
- [`mcr.microsoft.com/dotnet/sdk`](https://learn.microsoft.com/en-us/powershell/scripting/install/powershell-in-docker) with [Pester v5](https://pester.dev/) installed at runtime (pinned version; requires network access to the PowerShell Gallery)
24+
25+
No local installation of PowerShell or Pester is required (the container downloads Pester on demand).
26+
27+
## Test Structure
28+
29+
```
30+
guest-helpers/windows/
31+
filerestore.bat
32+
test/
33+
filerestore.Tests.ps1 # Pester test cases
34+
TestHelper.psm1 # Script extraction, Windows cmdlet stubs
35+
```
36+
37+
## How the Tests Work
38+
39+
The test helper extracts the PowerShell portion from the bat/PS polyglot file
40+
by stripping the batch preamble (everything up to the closing `#>` block
41+
comment marker). The extracted script is dot-sourced, which triggers a
42+
`$MyInvocation.InvocationName` guard that loads all function definitions
43+
without executing the main entry point.
44+
45+
Windows-only cmdlets (`Get-Disk`, `Set-Disk`, `Get-Partition`, etc.) are
46+
defined as stub functions with proper parameter declarations, enabling Pester's
47+
`Mock` and `Should -Invoke` to work on Linux.

0 commit comments

Comments
 (0)