fix: deliver Invoke-PveVmGuestExec -Args to the guest as argv

ExecuteGuestCommand JSON-serialized the args array into the agent/exec
'input-data' field — which is the process's STDIN, not its arguments. So
guest commands ran with no argv: cmd.exe started interactively and the
JSON blob ['/c','echo',...] arrived at its prompt.

PVE's agent/exec 'command' parameter is itself an array (element 0 = the
executable, the rest = argv) sent as repeated form keys. The low-level
client couldn't express repeated keys (Dictionary<string,string> only),
so:

- Add PostAsync(string, IEnumerable<KeyValuePair<string,string>>) to
  IPveHttpClient/PveHttpClient; BuildFormContent now emits one key=value
  field per pair, so a key may repeat.
- ExecuteGuestCommand builds command = [exe] + args as repeated 'command'
  fields and no longer touches input-data.

Tests: form-encoder repeated-key + per-value encoding cases; VmService
tests asserting the command array, order, and absence of input-data; an
integration regression guard that echoes an arg and checks it round-trips
as stdout.

Tracked as F091. Closes #68.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Clint Branham
2026-05-22 14:32:04 -05:00
parent 5266accfae
commit bc71ed4a12
7 changed files with 199 additions and 10 deletions
@@ -79,6 +79,46 @@ namespace PSProxmoxVE.Core.Tests.Client
Assert.Contains("ide2=nas:iso/win.iso,media%3Dcdrom", handler.LastBody);
}
[Fact]
public async Task PostAsync_RepeatedKeys_EmitOneFieldPerValue()
{
var (client, handler) = NewCapturingClient();
using (client)
{
// PVE array params (e.g. guest-exec command) are sent as repeated keys.
var data = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("command", "cmd.exe"),
new KeyValuePair<string, string>("command", "/c"),
new KeyValuePair<string, string>("command", "echo"),
new KeyValuePair<string, string>("command", "WLMARK42"),
};
await client.PostAsync("nodes/pve/qemu/100/agent/exec", data);
}
Assert.Equal("command=cmd.exe&command=/c&command=echo&command=WLMARK42", handler.LastBody);
}
[Fact]
public async Task PostAsync_RepeatedKeys_EncodeEachValueIndependently()
{
var (client, handler) = NewCapturingClient();
using (client)
{
var data = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("command", "powershell.exe"),
new KeyValuePair<string, string>("command", "-Command"),
new KeyValuePair<string, string>("command", "echo a=b;c"),
};
await client.PostAsync("nodes/pve/qemu/100/agent/exec", data);
}
// The '=' and ';' inside an arg must be encoded so they don't split the body,
// while spaces follow the form convention ('+').
Assert.Equal("command=powershell.exe&command=-Command&command=echo+a%3Db%3Bc", handler.LastBody);
}
private sealed class CapturingHandler : HttpMessageHandler
{
public string LastBody { get; private set; } = string.Empty;
@@ -0,0 +1,81 @@
using System.Collections.Generic;
using System.Linq;
using Moq;
using PSProxmoxVE.Core.Authentication;
using PSProxmoxVE.Core.Client;
using PSProxmoxVE.Core.Services;
using Xunit;
namespace PSProxmoxVE.Core.Tests.Services
{
public class VmServiceTests
{
private const string TestNode = "pve1";
private const int TestVmId = 100;
private static PveSession CreateSession() =>
new PveSession("pve1.example.com", 8006, true, "PVE:root@pam:TEST_TOKEN");
[Fact]
public void ExecuteGuestCommand_SendsCommandAndArgsAsRepeatedCommandArray()
{
List<KeyValuePair<string, string>>? captured = null;
var mockClient = new Mock<IPveHttpClient>();
mockClient
.Setup(c => c.PostAsync(It.IsAny<string>(), It.IsAny<IEnumerable<KeyValuePair<string, string>>>()))
.Callback<string, IEnumerable<KeyValuePair<string, string>>>((_, data) => captured = data.ToList())
.ReturnsAsync("{\"data\":{\"pid\":4242}}");
var service = new VmService(mockClient.Object);
var pid = service.ExecuteGuestCommand(CreateSession(), TestNode, TestVmId,
"cmd.exe", new[] { "/c", "echo", "WLMARK42" });
Assert.Equal(4242, pid);
Assert.NotNull(captured);
// Every element (exe + each arg) is its own "command" entry, in order.
Assert.All(captured!, kvp => Assert.Equal("command", kvp.Key));
Assert.Equal(
new[] { "cmd.exe", "/c", "echo", "WLMARK42" },
captured!.Select(kvp => kvp.Value).ToArray());
}
[Fact]
public void ExecuteGuestCommand_DoesNotUseInputDataForArgs()
{
List<KeyValuePair<string, string>>? captured = null;
var mockClient = new Mock<IPveHttpClient>();
mockClient
.Setup(c => c.PostAsync(It.IsAny<string>(), It.IsAny<IEnumerable<KeyValuePair<string, string>>>()))
.Callback<string, IEnumerable<KeyValuePair<string, string>>>((_, data) => captured = data.ToList())
.ReturnsAsync("{\"data\":{\"pid\":1}}");
var service = new VmService(mockClient.Object);
service.ExecuteGuestCommand(CreateSession(), TestNode, TestVmId,
"powershell.exe", new[] { "-NoProfile", "-Command", "echo hi" });
Assert.NotNull(captured);
// Args are argv, not STDIN — "input-data" must never be emitted.
Assert.DoesNotContain(captured!, kvp => kvp.Key == "input-data");
}
[Fact]
public void ExecuteGuestCommand_NoArgs_SendsSingleCommandEntry()
{
List<KeyValuePair<string, string>>? captured = null;
var mockClient = new Mock<IPveHttpClient>();
mockClient
.Setup(c => c.PostAsync(It.IsAny<string>(), It.IsAny<IEnumerable<KeyValuePair<string, string>>>()))
.Callback<string, IEnumerable<KeyValuePair<string, string>>>((_, data) => captured = data.ToList())
.ReturnsAsync("{\"data\":{\"pid\":7}}");
var service = new VmService(mockClient.Object);
service.ExecuteGuestCommand(CreateSession(), TestNode, TestVmId, "whoami", null);
Assert.NotNull(captured);
var only = Assert.Single(captured!);
Assert.Equal("command", only.Key);
Assert.Equal("whoami", only.Value);
}
}
}
@@ -157,6 +157,18 @@ Describe 'Linux VM — Integration' -Tag 'Integration' {
$result.ExitCode | Should -Be 0
$result.Stdout | Should -Not -BeNullOrEmpty
}
It 'Should pass -Args to the guest as argv (Invoke-PveVmGuestExec)' {
if (Skip-IfNoLinuxVm) { return }
# Regression guard for #68: args must reach the process as argv, not STDIN.
# With the old bug, echo got no arguments and stdout was empty.
$marker = 'PSPROXMOXVE_ARG_TEST'
$result = Invoke-PveVmGuestExec -Node $script:Node -VmId $script:LinuxVmId `
-Command 'echo' -Args @($marker)
$result.ExitCode | Should -Be 0
$result.Stdout.Trim() | Should -Be $marker
}
}
Context 'Guest Agent VM — Lifecycle' {