mirror of
https://github.com/GoodOlClint/PSProxmoxVE.git
synced 2026-09-07 12:35:41 +00:00
0026ef2b57
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>
343 lines
12 KiB
C#
343 lines
12 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 BackupServiceTests
|
|
{
|
|
private const string Node = "pve1";
|
|
|
|
private static PveSession CreateSession()
|
|
{
|
|
return new PveSession("pve.example.com", 8006, false,
|
|
"root@pam!testtoken=aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee");
|
|
}
|
|
|
|
// ---------------------------------------------------------------
|
|
// CreateBackup
|
|
// ---------------------------------------------------------------
|
|
|
|
[Fact]
|
|
public void CreateBackup_CallsPostAsync_ReturnsUpid()
|
|
{
|
|
// Arrange
|
|
const string upid = "UPID:pve1:000ABC:00000001:5F1234AB:vzdump:100:root@pam:";
|
|
var json = $@"{{""data"": ""{upid}""}}";
|
|
var mockClient = new Mock<IPveHttpClient>();
|
|
mockClient.Setup(c => c.PostAsync(It.IsAny<string>(), It.IsAny<Dictionary<string, string>>()))
|
|
.ReturnsAsync(json);
|
|
|
|
var config = new Dictionary<string, string>
|
|
{
|
|
["vmid"] = "100",
|
|
["storage"] = "local",
|
|
["mode"] = "snapshot",
|
|
["compress"] = "zstd"
|
|
};
|
|
|
|
var service = new BackupService(mockClient.Object);
|
|
|
|
// Act
|
|
var task = service.CreateBackup(CreateSession(), Node, config);
|
|
|
|
// Assert
|
|
Assert.Equal(upid, task.Upid);
|
|
Assert.Equal(Node, task.Node);
|
|
Assert.Equal("running", task.Status);
|
|
mockClient.Verify(c => c.PostAsync(
|
|
$"nodes/{Node}/vzdump",
|
|
It.Is<Dictionary<string, string>>(d => d["vmid"] == "100" && d["storage"] == "local")),
|
|
Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public void CreateBackup_NullConfig_ThrowsArgumentNullException()
|
|
{
|
|
var service = new BackupService(new Mock<IPveHttpClient>().Object);
|
|
|
|
Assert.Throws<ArgumentNullException>("config", () => service.CreateBackup(CreateSession(), Node, null!));
|
|
}
|
|
|
|
// ---------------------------------------------------------------
|
|
// GetBackupJobs
|
|
// ---------------------------------------------------------------
|
|
|
|
[Fact]
|
|
public void GetBackupJobs_ReturnsJobArray()
|
|
{
|
|
// Arrange
|
|
var json = @"{
|
|
""data"": [
|
|
{
|
|
""id"": ""backup-001"",
|
|
""type"": ""vzdump"",
|
|
""enabled"": 1,
|
|
""schedule"": ""0 2 * * *"",
|
|
""storage"": ""pbs-store"",
|
|
""mode"": ""snapshot"",
|
|
""vmid"": ""100,101,102"",
|
|
""compress"": ""zstd""
|
|
},
|
|
{
|
|
""id"": ""backup-002"",
|
|
""type"": ""vzdump"",
|
|
""enabled"": 0,
|
|
""schedule"": ""0 3 * * 0"",
|
|
""storage"": ""local"",
|
|
""mode"": ""stop"",
|
|
""all"": 1,
|
|
""compress"": ""lzo""
|
|
}
|
|
]
|
|
}";
|
|
var mockClient = new Mock<IPveHttpClient>();
|
|
mockClient.Setup(c => c.GetAsync("cluster/backup"))
|
|
.ReturnsAsync(json);
|
|
|
|
var service = new BackupService(mockClient.Object);
|
|
|
|
// Act
|
|
var jobs = service.GetBackupJobs(CreateSession());
|
|
|
|
// Assert
|
|
Assert.Equal(2, jobs.Length);
|
|
|
|
Assert.Equal("backup-001", jobs[0].Id);
|
|
Assert.Equal("vzdump", jobs[0].Type);
|
|
Assert.Equal(1, jobs[0].Enabled);
|
|
Assert.Equal("0 2 * * *", jobs[0].Schedule);
|
|
Assert.Equal("pbs-store", jobs[0].Storage);
|
|
Assert.Equal("snapshot", jobs[0].Mode);
|
|
Assert.Equal("100,101,102", jobs[0].VmId);
|
|
|
|
Assert.Equal("backup-002", jobs[1].Id);
|
|
Assert.Equal(0, jobs[1].Enabled);
|
|
Assert.Equal(1, jobs[1].All);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetBackupJobs_EmptyData_ReturnsEmptyArray()
|
|
{
|
|
// Arrange
|
|
var json = @"{""data"": []}";
|
|
var mockClient = new Mock<IPveHttpClient>();
|
|
mockClient.Setup(c => c.GetAsync("cluster/backup"))
|
|
.ReturnsAsync(json);
|
|
|
|
var service = new BackupService(mockClient.Object);
|
|
|
|
// Act
|
|
var jobs = service.GetBackupJobs(CreateSession());
|
|
|
|
// Assert
|
|
Assert.Empty(jobs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------
|
|
// GetBackupJob (single)
|
|
// ---------------------------------------------------------------
|
|
|
|
[Fact]
|
|
public void GetBackupJob_ReturnsJob()
|
|
{
|
|
// Arrange
|
|
var json = @"{
|
|
""data"": {
|
|
""id"": ""backup-001"",
|
|
""type"": ""vzdump"",
|
|
""enabled"": 1,
|
|
""schedule"": ""0 2 * * *"",
|
|
""storage"": ""pbs-store"",
|
|
""mode"": ""snapshot"",
|
|
""vmid"": ""100""
|
|
}
|
|
}";
|
|
var mockClient = new Mock<IPveHttpClient>();
|
|
mockClient.Setup(c => c.GetAsync("cluster/backup/backup-001"))
|
|
.ReturnsAsync(json);
|
|
|
|
var service = new BackupService(mockClient.Object);
|
|
|
|
// Act
|
|
var job = service.GetBackupJob(CreateSession(), "backup-001");
|
|
|
|
// Assert
|
|
Assert.NotNull(job);
|
|
Assert.Equal("backup-001", job!.Id);
|
|
Assert.Equal("snapshot", job.Mode);
|
|
Assert.Equal("pbs-store", job.Storage);
|
|
}
|
|
|
|
// ---------------------------------------------------------------
|
|
// CreateBackupJob
|
|
// ---------------------------------------------------------------
|
|
|
|
[Fact]
|
|
public void CreateBackupJob_CallsPostAsync()
|
|
{
|
|
// Arrange
|
|
var json = @"{""data"": null}";
|
|
var mockClient = new Mock<IPveHttpClient>();
|
|
mockClient.Setup(c => c.PostAsync(It.IsAny<string>(), It.IsAny<Dictionary<string, string>>()))
|
|
.ReturnsAsync(json);
|
|
|
|
var config = new Dictionary<string, string>
|
|
{
|
|
["schedule"] = "0 2 * * *",
|
|
["storage"] = "local",
|
|
["mode"] = "snapshot",
|
|
["vmid"] = "100",
|
|
["compress"] = "zstd"
|
|
};
|
|
|
|
var service = new BackupService(mockClient.Object);
|
|
|
|
// Act
|
|
service.CreateBackupJob(CreateSession(), config);
|
|
|
|
// Assert
|
|
mockClient.Verify(c => c.PostAsync(
|
|
"cluster/backup",
|
|
It.Is<Dictionary<string, string>>(d =>
|
|
d["schedule"] == "0 2 * * *" &&
|
|
d["storage"] == "local")),
|
|
Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public void CreateBackupJob_NullConfig_ThrowsArgumentNullException()
|
|
{
|
|
var service = new BackupService(new Mock<IPveHttpClient>().Object);
|
|
|
|
Assert.Throws<ArgumentNullException>("config", () => service.CreateBackupJob(CreateSession(), null!));
|
|
}
|
|
|
|
// ---------------------------------------------------------------
|
|
// UpdateBackupJob
|
|
// ---------------------------------------------------------------
|
|
|
|
[Fact]
|
|
public void UpdateBackupJob_CallsPutAsync()
|
|
{
|
|
// Arrange
|
|
var json = @"{""data"": null}";
|
|
var mockClient = new Mock<IPveHttpClient>();
|
|
mockClient.Setup(c => c.PutAsync(It.IsAny<string>(), It.IsAny<Dictionary<string, string>>()))
|
|
.ReturnsAsync(json);
|
|
|
|
var config = new Dictionary<string, string>
|
|
{
|
|
["schedule"] = "0 4 * * *",
|
|
["enabled"] = "1"
|
|
};
|
|
|
|
var service = new BackupService(mockClient.Object);
|
|
|
|
// Act
|
|
service.UpdateBackupJob(CreateSession(), "backup-001", config);
|
|
|
|
// Assert
|
|
mockClient.Verify(c => c.PutAsync(
|
|
"cluster/backup/backup-001",
|
|
It.Is<Dictionary<string, string>>(d => d["schedule"] == "0 4 * * *")),
|
|
Times.Once);
|
|
}
|
|
|
|
[Fact]
|
|
public void UpdateBackupJob_NullConfig_ThrowsArgumentNullException()
|
|
{
|
|
var service = new BackupService(new Mock<IPveHttpClient>().Object);
|
|
|
|
Assert.Throws<ArgumentNullException>("config", () => service.UpdateBackupJob(CreateSession(), "id", null!));
|
|
}
|
|
|
|
// ---------------------------------------------------------------
|
|
// RemoveBackupJob
|
|
// ---------------------------------------------------------------
|
|
|
|
[Fact]
|
|
public void RemoveBackupJob_CallsDeleteAsync()
|
|
{
|
|
// Arrange
|
|
var json = @"{""data"": null}";
|
|
var mockClient = new Mock<IPveHttpClient>();
|
|
mockClient.Setup(c => c.DeleteAsync(It.IsAny<string>()))
|
|
.ReturnsAsync(json);
|
|
|
|
var service = new BackupService(mockClient.Object);
|
|
|
|
// Act
|
|
service.RemoveBackupJob(CreateSession(), "backup-001");
|
|
|
|
// Assert
|
|
mockClient.Verify(c => c.DeleteAsync("cluster/backup/backup-001"), Times.Once);
|
|
}
|
|
|
|
// ---------------------------------------------------------------
|
|
// GetNotBackedUp
|
|
// ---------------------------------------------------------------
|
|
|
|
[Fact]
|
|
public void GetNotBackedUp_ReturnsTypedEntriesWithUnknownKeyInAdditionalProperties()
|
|
{
|
|
// Arrange
|
|
var json = @"{
|
|
""data"": [
|
|
{ ""vmid"": 100, ""name"": ""webserver"", ""type"": ""qemu"", ""comment"": ""prod"" },
|
|
{ ""vmid"": 200, ""name"": ""database"", ""type"": ""lxc"" }
|
|
]
|
|
}";
|
|
var mockClient = new Mock<IPveHttpClient>();
|
|
mockClient.Setup(c => c.GetAsync("cluster/backup-info/not-backed-up"))
|
|
.ReturnsAsync(json);
|
|
|
|
var service = new BackupService(mockClient.Object);
|
|
|
|
// Act
|
|
var result = service.GetNotBackedUp(CreateSession());
|
|
|
|
// Assert
|
|
Assert.Equal(2, result.Count);
|
|
Assert.Equal(100, result[0].VmId);
|
|
Assert.Equal("webserver", result[0].Name);
|
|
Assert.Equal("qemu", result[0].Type);
|
|
Assert.Equal("prod", result[0].AdditionalProperties["comment"]);
|
|
}
|
|
|
|
[Fact]
|
|
public void GetNotBackedUp_EmptyData_ReturnsEmptyList()
|
|
{
|
|
// Arrange
|
|
var json = @"{""data"": []}";
|
|
var mockClient = new Mock<IPveHttpClient>();
|
|
mockClient.Setup(c => c.GetAsync("cluster/backup-info/not-backed-up"))
|
|
.ReturnsAsync(json);
|
|
|
|
var service = new BackupService(mockClient.Object);
|
|
|
|
// Act
|
|
var result = service.GetNotBackedUp(CreateSession());
|
|
|
|
// Assert
|
|
Assert.Empty(result);
|
|
}
|
|
|
|
// ---------------------------------------------------------------
|
|
// Constructor
|
|
// ---------------------------------------------------------------
|
|
|
|
[Fact]
|
|
public void Constructor_NullClient_ThrowsArgumentNullException()
|
|
{
|
|
Assert.Throws<ArgumentNullException>("client", () => new BackupService(null!));
|
|
}
|
|
}
|
|
}
|