Files
PSProxmoxVE/tests/PSProxmoxVE.Core.Tests/Services/CloudInitServiceTests.cs
T
Clint Branham 68dadbfdc0 fix: remediate findings F045, F047, F048, F064, F070, F071, F076-F079
Phase 1 — Trivial fixes:
- F071: Add Uri.EscapeDataString to GetPveTemplateCmdlet node path
- F077: Add ValidateRange(100, 999999999) to GetPveTaskListCmdlet.VmId
- F076: Create .github/dependabot.yml (nuget + github-actions, weekly)
- F079: Fix unit-tests.yml dotnet SDK from 9.0.x to 10.0.x
- F048: Mark wont_fix — sync-over-async accepted for PS 5.1 compat

Phase 2 — Framework targeting (D009 compliance):
- F047: Reduce publishable csproj to netstandard2.0 only, remove all
  #if NET48/NETSTANDARD2_0 conditionals from PveHttpClient.cs,
  restructure build.yml for netstandard2.0 publish + net10.0/net48 tests
- F064: Resolved by F047 — SMA 7.5.0 ItemGroup removed with net10.0 TFM
- F070: Add PS 5.1 smoke-test job to publish.yml (windows-latest)

Phase 3 — IPveHttpClient interface extraction (F045):
- Extract IPveHttpClient interface from PveHttpClient
- Add constructor injection to all 14 service classes
- Services use injected client when available, create+dispose when not

Phase 4 — Service unit tests (F078):
- 196 new xUnit tests across 10 service test files
- All services tested via Moq-mocked IPveHttpClient
- Total test count: 382 (was 186)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 13:51:37 -05:00

162 lines
5.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Moq;
using Xunit;
using PSProxmoxVE.Core.Authentication;
using PSProxmoxVE.Core.Client;
using PSProxmoxVE.Core.Services;
namespace PSProxmoxVE.Core.Tests.Services
{
public class CloudInitServiceTests
{
private static PveSession CreateSession()
{
return new PveSession("pve.example.com", 8006, false,
"root@pam!testtoken=aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee");
}
[Fact]
public void GetCloudInitConfig_ReturnsConfig()
{
// Arrange
var json = @"{""data"": {
""ciuser"": ""ubuntu"",
""ipconfig0"": ""ip=dhcp"",
""nameserver"": ""8.8.8.8"",
""searchdomain"": ""example.com"",
""sshkeys"": ""ssh-rsa%20AAAA...%20user%40host"",
""boot"": ""order=scsi0;net0"",
""cores"": 4,
""memory"": 8192
}}";
var mockClient = new Mock<IPveHttpClient>();
mockClient.Setup(c => c.GetAsync("nodes/pve1/qemu/100/config")).ReturnsAsync(json);
var service = new CloudInitService(mockClient.Object);
// Act
var config = service.GetCloudInitConfig(CreateSession(), "pve1", 100);
// Assert
Assert.NotNull(config);
Assert.Equal("ubuntu", config.CiUser);
Assert.Equal("ip=dhcp", config.IpConfig0);
Assert.Equal("8.8.8.8", config.Nameserver);
Assert.Equal("example.com", config.Searchdomain);
Assert.Equal("ssh-rsa%20AAAA...%20user%40host", config.SshKeys);
mockClient.Verify(c => c.GetAsync("nodes/pve1/qemu/100/config"), Times.Once);
}
[Fact]
public void GetCloudInitConfig_ExcludesNonCiFields()
{
// Arrange — response includes non-CI fields that should be ignored
var json = @"{""data"": {
""ciuser"": ""admin"",
""cores"": 4,
""memory"": 8192,
""boot"": ""order=scsi0""
}}";
var mockClient = new Mock<IPveHttpClient>();
mockClient.Setup(c => c.GetAsync("nodes/pve1/qemu/200/config")).ReturnsAsync(json);
var service = new CloudInitService(mockClient.Object);
// Act
var config = service.GetCloudInitConfig(CreateSession(), "pve1", 200);
// Assert
Assert.Equal("admin", config.CiUser);
// Non-CI fields should not appear in the result
Assert.Null(config.IpConfig0);
Assert.Null(config.Nameserver);
}
[Fact]
public void SetCloudInitConfig_CallsPutAsyncWithCorrectResource()
{
// Arrange
var mockClient = new Mock<IPveHttpClient>();
mockClient.Setup(c => c.PutAsync(
"nodes/pve1/qemu/100/config",
It.IsAny<Dictionary<string, string>>()))
.ReturnsAsync("{}");
var service = new CloudInitService(mockClient.Object);
var config = new Dictionary<string, object>
{
["ciuser"] = "ubuntu",
["ipconfig0"] = "ip=dhcp"
};
// Act
service.SetCloudInitConfig(CreateSession(), "pve1", 100, config);
// Assert
mockClient.Verify(c => c.PutAsync(
"nodes/pve1/qemu/100/config",
It.Is<Dictionary<string, string>>(d =>
d["ciuser"] == "ubuntu" && d["ipconfig0"] == "ip=dhcp")),
Times.Once);
}
[Fact]
public void RegenerateCloudInitImage_CallsGetAsyncAndReturnsData()
{
// Arrange
var json = @"{""data"": ""#cloud-config\nuser: ubuntu\npassword: secret\n""}";
var mockClient = new Mock<IPveHttpClient>();
mockClient.Setup(c => c.GetAsync("nodes/pve1/qemu/100/cloudinit/dump?type=user"))
.ReturnsAsync(json);
var service = new CloudInitService(mockClient.Object);
// Act
var result = service.RegenerateCloudInitImage(CreateSession(), "pve1", 100);
// Assert
Assert.Contains("#cloud-config", result);
Assert.Contains("ubuntu", result);
mockClient.Verify(c => c.GetAsync("nodes/pve1/qemu/100/cloudinit/dump?type=user"), Times.Once);
}
[Fact]
public void GetCloudInitConfig_NullSession_ThrowsArgumentNullException()
{
// Arrange
var mockClient = new Mock<IPveHttpClient>();
var service = new CloudInitService(mockClient.Object);
// Act & Assert
Assert.Throws<ArgumentNullException>(() => service.GetCloudInitConfig(null!, "pve1", 100));
}
[Fact]
public void SetCloudInitConfig_NullSession_ThrowsArgumentNullException()
{
// Arrange
var mockClient = new Mock<IPveHttpClient>();
var service = new CloudInitService(mockClient.Object);
var config = new Dictionary<string, object> { ["ciuser"] = "test" };
// Act & Assert
Assert.Throws<ArgumentNullException>(() => service.SetCloudInitConfig(null!, "pve1", 100, config));
}
[Fact]
public void RegenerateCloudInitImage_NullSession_ThrowsArgumentNullException()
{
// Arrange
var mockClient = new Mock<IPveHttpClient>();
var service = new CloudInitService(mockClient.Object);
// Act & Assert
Assert.Throws<ArgumentNullException>(() => service.RegenerateCloudInitImage(null!, "pve1", 100));
}
[Fact]
public void Constructor_NullClient_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => new CloudInitService(null!));
}
}
}