- Create MigrationTool console app for exporting DB config to K8s YAML - Support dry-run mode and validation - Add Npgsql and YamlDotNet dependencies
139 lines
3.2 KiB
C#
139 lines
3.2 KiB
C#
using Fengling.Gateway.Plugin.Abstractions;
|
||
using Xunit;
|
||
using YarpGateway.Plugins;
|
||
|
||
namespace YarpGateway.Tests.Unit.Plugins;
|
||
|
||
public class PluginHostTests : IDisposable
|
||
{
|
||
private readonly string _testDir;
|
||
|
||
public PluginHostTests()
|
||
{
|
||
_testDir = Path.Combine(Path.GetTempPath(), "plugin-host-test-" + Guid.NewGuid());
|
||
Directory.CreateDirectory(_testDir);
|
||
}
|
||
|
||
public void Dispose()
|
||
{
|
||
try
|
||
{
|
||
Directory.Delete(_testDir, true);
|
||
}
|
||
catch { }
|
||
}
|
||
|
||
[Fact]
|
||
public void Constructor_ShouldInitialize()
|
||
{
|
||
// Act
|
||
var host = new PluginHost(_testDir);
|
||
|
||
// Assert
|
||
Assert.NotNull(host);
|
||
}
|
||
|
||
[Fact]
|
||
public void GetPlugins_Empty_ShouldReturnEmpty()
|
||
{
|
||
// Arrange
|
||
var host = new PluginHost(_testDir);
|
||
|
||
// Act
|
||
var plugins = host.GetPlugins().ToList();
|
||
|
||
// Assert
|
||
Assert.Empty(plugins);
|
||
}
|
||
|
||
[Fact]
|
||
public void LoadAllAsync_EmptyDirectory_ShouldReturnZero()
|
||
{
|
||
// Arrange
|
||
var host = new PluginHost(_testDir);
|
||
|
||
// Act
|
||
var count = host.LoadAllAsync().Result;
|
||
|
||
// Assert
|
||
Assert.Equal(0, count);
|
||
}
|
||
|
||
[Fact]
|
||
public async Task GetPluginInfo_AfterLoad_ShouldReturnPlugins()
|
||
{
|
||
// Arrange
|
||
var host = new PluginHost(_testDir);
|
||
|
||
// 创建测试插件目录
|
||
var pluginDir = Path.Combine(_testDir, "test-plugin");
|
||
Directory.CreateDirectory(pluginDir);
|
||
|
||
// 创建 plugin.json
|
||
var manifest = new
|
||
{
|
||
id = "test-plugin",
|
||
name = "Test Plugin",
|
||
version = "1.0.0",
|
||
entryPoint = "TestPlugin.TestPlugin"
|
||
};
|
||
File.WriteAllText(Path.Combine(pluginDir, "plugin.json"), System.Text.Json.JsonSerializer.Serialize(manifest));
|
||
|
||
// 创建 DLL(占位符,不会真正加载)
|
||
File.WriteAllText(Path.Combine(pluginDir, "test-plugin.dll"), "dummy");
|
||
|
||
// Act
|
||
var count = await host.LoadAllAsync();
|
||
|
||
// Assert - 加载会失败因为 DLL 是无效的,但应该返回 0
|
||
Assert.Equal(0, count);
|
||
}
|
||
|
||
[Fact]
|
||
public async Task UnloadAsync_NotLoaded_ShouldNotThrow()
|
||
{
|
||
// Arrange
|
||
var host = new PluginHost(_testDir);
|
||
|
||
// Act & Assert - 不应抛出异常
|
||
await host.UnloadAsync("non-existent");
|
||
}
|
||
|
||
[Fact]
|
||
public async Task UnloadAllAsync_Empty_ShouldNotThrow()
|
||
{
|
||
// Arrange
|
||
var host = new PluginHost(_testDir);
|
||
|
||
// Act & Assert - 不应抛出异常
|
||
await host.UnloadAllAsync();
|
||
}
|
||
|
||
[Fact]
|
||
public void VerifyUnload_AliveObject_ShouldReturnFalse()
|
||
{
|
||
// Arrange
|
||
var obj = new object();
|
||
var weakRef = new WeakReference(obj);
|
||
|
||
// Act
|
||
var result = PluginHost.VerifyUnload(weakRef, maxAttempts: 1);
|
||
|
||
// Assert
|
||
Assert.False(result);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 测试用插件实现
|
||
/// </summary>
|
||
public class TestPlugin : IGatewayPlugin
|
||
{
|
||
public string Name => "TestPlugin";
|
||
public string Version => "1.0.0";
|
||
public string? Description => "A test plugin";
|
||
|
||
public Task OnLoadAsync() => Task.CompletedTask;
|
||
public Task OnUnloadAsync() => Task.CompletedTask;
|
||
}
|