Files
PSProxmoxVE/tests/PSProxmoxVE.Core.Tests/Services/NodeServiceTests.cs
T
goodolclint-claude[bot] 0026ef2b57 refactor: typed models for the remaining dictionary-returning services (#157) (#229)
NodeService.GetNodeConfig/GetNodeDns, ClusterConfigService.GetClusterConfig,
BackupService.GetNotBackedUp and VmService.GetGuestExecStatus returned raw
Dictionary/List<Dictionary> instead of a Pve* model, per issue #157. Each now
has a typed model under Models/{Nodes,Cluster,Backup,Vms}/ with [JsonProperty]
for documented fields and a [JsonExtensionData]-backed AdditionalProperties
catch-all, following the PveVmConfig pattern. The five consuming cmdlets and
their [OutputType] attributes are updated to match.

GetClusterConfig also fixes a latent bug: GET /cluster/config returns a JSON
array (a directory index), but the old code did `data is JObject obj ? ... :
empty dict`, which silently always returned an empty dictionary since data was
a JArray. PveClusterConfigEntry decodes the array correctly and exposes a
typed Name property (the array items' schema documents no named fields, but
the endpoint's "links" metadata gives the child-URL template as "{name}").

The guest-exec poll loop in InvokePveVmGuestExecCmdlet keeps its exact
Stopwatch + Thread.Sleep(1000) structure (ADR 0001 accepted exception); only
the type it reads from changed. A TolerantBooleanConverter was added so
PveGuestExecStatus.Exited keeps accepting PVE's boolean/integer/string forms,
matching what ApiValueHelper.IsExited already tolerated for the old
dictionary path.

Reviewed with codex-rescue, correctness-reviewer and api-compat-reviewer
before commit; both real findings above (the name/subdir key and the Exited
string-form regression) came from that pass and are mutation-tested.

Co-authored-by: goodolclint-claude[bot] <323206664+goodolclint-claude[bot]@users.noreply.github.com>
2026-09-03 19:16:29 +00:00

293 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Moq;
using Newtonsoft.Json.Linq;
using Xunit;
using PSProxmoxVE.Core.Authentication;
using PSProxmoxVE.Core.Client;
using PSProxmoxVE.Core.Services;
namespace PSProxmoxVE.Core.Tests.Services
{
public class NodeServiceTests
{
private static PveSession CreateSession()
{
return new PveSession("pve.example.com", 8006, false,
"root@pam!testtoken=aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee");
}
[Fact]
public void GetNodes_ReturnsArrayOfPveNode()
{
// Arrange
var json = @"{""data"": [
{""node"": ""pve1"", ""status"": ""online"", ""maxcpu"": 16, ""maxmem"": 68719476736},
{""node"": ""pve2"", ""status"": ""online"", ""maxcpu"": 8, ""maxmem"": 34359738368}
]}";
var mockClient = new Mock<IPveHttpClient>();
mockClient.Setup(c => c.GetAsync("nodes")).ReturnsAsync(json);
var service = new NodeService(mockClient.Object);
// Act
var nodes = service.GetNodes(CreateSession());
// Assert
Assert.Equal(2, nodes.Length);
Assert.Equal("pve1", nodes[0].Name);
Assert.Equal("online", nodes[0].Status);
Assert.Equal(16, nodes[0].CpuCount);
Assert.Equal(68719476736L, nodes[0].MemoryTotal);
Assert.Equal("pve2", nodes[1].Name);
Assert.Equal(8, nodes[1].CpuCount);
mockClient.Verify(c => c.GetAsync("nodes"), Times.Once);
}
[Fact]
public void GetNodes_MissingDataField_Throws()
{
// Arrange
var mockClient = new Mock<IPveHttpClient>();
mockClient.Setup(c => c.GetAsync("nodes")).ReturnsAsync("{}");
var service = new NodeService(mockClient.Object);
// Act & Assert
Assert.Throws<InvalidOperationException>(() => service.GetNodes(CreateSession()));
}
[Fact]
public void GetNodeStatus_ReturnsPveNodeStatus()
{
// Arrange
var json = @"{""data"": {
""node"": ""pve1"", ""status"": ""online"", ""maxcpu"": 16,
""maxmem"": 68719476736, ""mem"": 17179869184,
""uptime"": 864000, ""cpu"": 0.125
}}";
var mockClient = new Mock<IPveHttpClient>();
mockClient.Setup(c => c.GetAsync("nodes/pve1/status")).ReturnsAsync(json);
var service = new NodeService(mockClient.Object);
// Act
var status = service.GetNodeStatus(CreateSession(), "pve1");
// Assert
Assert.Equal("pve1", status.Node);
Assert.Equal("online", status.Status);
Assert.Equal(0.125, status.CpuUsage);
Assert.Equal(68719476736L, status.MemoryTotal);
Assert.Equal(17179869184L, status.MemoryUsed);
Assert.Equal(864000L, status.Uptime);
mockClient.Verify(c => c.GetAsync("nodes/pve1/status"), Times.Once);
}
[Fact]
public void GetNodeStatus_StampsNodeWhenResponseOmitsIt()
{
// Arrange
var json = @"{""data"": {""status"": ""online"", ""maxcpu"": 16}}";
var mockClient = new Mock<IPveHttpClient>();
mockClient.Setup(c => c.GetAsync("nodes/pve1/status")).ReturnsAsync(json);
var service = new NodeService(mockClient.Object);
// Act
var status = service.GetNodeStatus(CreateSession(), "pve1");
// Assert
Assert.Equal("pve1", status.Node);
mockClient.Verify(c => c.GetAsync("nodes/pve1/status"), Times.Once);
}
[Fact]
public void GetNodeStatus_EscapesNodeInPath()
{
// Arrange
var json = @"{""data"": {""node"": ""pve node"", ""status"": ""online""}}";
string? capturedPath = null;
var calls = 0;
var mockClient = new Mock<IPveHttpClient>();
mockClient.Setup(c => c.GetAsync(It.IsAny<string>()))
.Callback<string>(path =>
{
capturedPath = path;
calls++;
})
.ReturnsAsync(json);
var service = new NodeService(mockClient.Object);
// Act
var status = service.GetNodeStatus(CreateSession(), "pve node");
// Assert
Assert.Equal("nodes/pve%20node/status", capturedPath);
Assert.Equal(1, calls);
Assert.Equal("pve node", status.Node);
}
[Fact]
public void GetNodeStatus_MissingDataField_Throws()
{
// Arrange
var mockClient = new Mock<IPveHttpClient>();
mockClient.Setup(c => c.GetAsync("nodes/pve1/status")).ReturnsAsync("{}");
var service = new NodeService(mockClient.Object);
// Act & Assert
Assert.Throws<InvalidOperationException>(() => service.GetNodeStatus(CreateSession(), "pve1"));
}
[Fact]
public void GetNodeConfig_ReturnsTypedModelWithUnknownKeyInAdditionalProperties()
{
// Arrange
var json = @"{""data"": {""description"": ""Primary node"", ""wakeonlan"": ""AA:BB:CC:DD:EE:FF"", ""acmedomain0"": ""example.com,plugin=dns""}}";
var mockClient = new Mock<IPveHttpClient>();
mockClient.Setup(c => c.GetAsync("nodes/pve1/config")).ReturnsAsync(json);
var service = new NodeService(mockClient.Object);
// Act
var config = service.GetNodeConfig(CreateSession(), "pve1");
// Assert
Assert.NotNull(config);
Assert.Equal("Primary node", config.Description);
Assert.Equal("AA:BB:CC:DD:EE:FF", config.WakeOnLan);
Assert.Equal("example.com,plugin=dns", config.AdditionalProperties["acmedomain0"]);
mockClient.Verify(c => c.GetAsync("nodes/pve1/config"), Times.Once);
}
[Fact]
public void SetNodeConfig_CallsPutAsyncWithCorrectResource()
{
// Arrange
var mockClient = new Mock<IPveHttpClient>();
mockClient.Setup(c => c.PutAsync(
"nodes/pve1/config",
It.IsAny<Dictionary<string, string>>()))
.ReturnsAsync("{}");
var service = new NodeService(mockClient.Object);
var config = new Dictionary<string, string>
{
["description"] = "Updated node"
};
// Act
service.SetNodeConfig(CreateSession(), "pve1", config);
// Assert
mockClient.Verify(c => c.PutAsync(
"nodes/pve1/config",
It.Is<Dictionary<string, string>>(d =>
d["description"] == "Updated node")),
Times.Once);
}
[Fact]
public void GetNodeDns_ReturnsTypedModelWithUnknownKeyInAdditionalProperties()
{
// Arrange
var json = @"{""data"": {""dns1"": ""8.8.8.8"", ""dns2"": ""8.8.4.4"", ""search"": ""example.com"", ""dns4"": ""1.1.1.1""}}";
var mockClient = new Mock<IPveHttpClient>();
mockClient.Setup(c => c.GetAsync("nodes/pve1/dns")).ReturnsAsync(json);
var service = new NodeService(mockClient.Object);
// Act
var dns = service.GetNodeDns(CreateSession(), "pve1");
// Assert
Assert.NotNull(dns);
Assert.Equal("8.8.8.8", dns.Dns1);
Assert.Equal("8.8.4.4", dns.Dns2);
Assert.Equal("example.com", dns.Search);
Assert.Equal("1.1.1.1", dns.AdditionalProperties["dns4"]);
mockClient.Verify(c => c.GetAsync("nodes/pve1/dns"), Times.Once);
}
[Fact]
public void SetNodeDns_CallsPutAsyncWithCorrectResource()
{
// Arrange
var mockClient = new Mock<IPveHttpClient>();
mockClient.Setup(c => c.PutAsync(
"nodes/pve1/dns",
It.IsAny<Dictionary<string, string>>()))
.ReturnsAsync("{}");
var service = new NodeService(mockClient.Object);
var config = new Dictionary<string, string>
{
["dns1"] = "1.1.1.1",
["search"] = "lab.local"
};
// Act
service.SetNodeDns(CreateSession(), "pve1", config);
// Assert
mockClient.Verify(c => c.PutAsync(
"nodes/pve1/dns",
It.Is<Dictionary<string, string>>(d =>
d["dns1"] == "1.1.1.1" && d["search"] == "lab.local")),
Times.Once);
}
[Fact]
public void StartAll_CallsPostAsync()
{
// Arrange
var json = @"{""data"": ""UPID:pve1:000ABC:00000001:5F1234AB:startall::root@pam:""}";
var mockClient = new Mock<IPveHttpClient>();
mockClient.Setup(c => c.PostAsync(
"nodes/pve1/startall",
It.IsAny<Dictionary<string, string>>()))
.ReturnsAsync(json);
var service = new NodeService(mockClient.Object);
// Act
var task = service.StartAll(CreateSession(), "pve1");
// Assert
Assert.NotNull(task);
Assert.Contains("startall", task.Upid);
Assert.Equal("pve1", task.Node);
Assert.Equal("running", task.Status);
mockClient.Verify(c => c.PostAsync(
"nodes/pve1/startall",
It.IsAny<Dictionary<string, string>>()),
Times.Once);
}
[Fact]
public void StopAll_CallsPostAsync()
{
// Arrange
var json = @"{""data"": ""UPID:pve1:000DEF:00000002:5F1234AC:stopall::root@pam:""}";
var mockClient = new Mock<IPveHttpClient>();
mockClient.Setup(c => c.PostAsync(
"nodes/pve1/stopall",
It.IsAny<Dictionary<string, string>>()))
.ReturnsAsync(json);
var service = new NodeService(mockClient.Object);
// Act
var task = service.StopAll(CreateSession(), "pve1");
// Assert
Assert.NotNull(task);
Assert.Contains("stopall", task.Upid);
Assert.Equal("pve1", task.Node);
Assert.Equal("running", task.Status);
mockClient.Verify(c => c.PostAsync(
"nodes/pve1/stopall",
It.IsAny<Dictionary<string, string>>()),
Times.Once);
}
[Fact]
public void Constructor_NullClient_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => new NodeService(null!));
}
}
}