fengling-gateway/tests/YarpGateway.Tests/Unit/Plugins/PluginLoaderTests.cs
movingsam 52eba07097 feat: add MigrationTool for gateway config migration (IMPL-7)
- Create MigrationTool console app for exporting DB config to K8s YAML
- Support dry-run mode and validation
- Add Npgsql and YamlDotNet dependencies
2026-03-08 00:35:04 +08:00

135 lines
3.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Fengling.Gateway.Plugin.Abstractions;
using Xunit;
using YarpGateway.Plugins;
namespace YarpGateway.Tests.Unit.Plugins;
public class PluginLoaderTests
{
private string _testBaseDir = null!;
public PluginLoaderTests()
{
_testBaseDir = Path.Combine(Path.GetTempPath(), "plugin-test-" + Guid.NewGuid());
}
[Fact]
public void DiscoverPlugins_EmptyDirectory_ShouldReturnEmpty()
{
// Arrange
var loader = new PluginLoader();
var emptyDir = Path.Combine(_testBaseDir, "empty");
Directory.CreateDirectory(emptyDir);
try
{
// Act
var plugins = loader.DiscoverPlugins(emptyDir).ToList();
// Assert
Assert.Empty(plugins);
}
finally
{
Directory.Delete(_testBaseDir, true);
}
}
[Fact]
public void DiscoverPlugins_ValidPlugin_ShouldReturnPlugin()
{
// Arrange
var loader = new PluginLoader();
var pluginDir = Path.Combine(_testBaseDir, "test-plugin");
Directory.CreateDirectory(pluginDir);
// 创建 plugin.json
var manifest = new
{
id = "test-plugin",
name = "Test Plugin",
version = "1.0.0",
entryPoint = "TestPlugin.TestPlugin",
description = "A test plugin"
};
var manifestJson = System.Text.Json.JsonSerializer.Serialize(manifest);
File.WriteAllText(Path.Combine(pluginDir, "plugin.json"), manifestJson);
// 创建一个占位 DLL
File.WriteAllText(Path.Combine(pluginDir, "test-plugin.dll"), "dummy");
try
{
// Act
var plugins = loader.DiscoverPlugins(_testBaseDir).ToList();
// Assert
Assert.Single(plugins);
var plugin = plugins[0];
Assert.Equal("test-plugin", plugin.Id);
Assert.Equal("Test Plugin", plugin.Name);
Assert.Equal("1.0.0", plugin.Version);
}
finally
{
Directory.Delete(_testBaseDir, true);
}
}
[Fact]
public void DiscoverPlugins_NoManifest_ShouldSkipPlugin()
{
// Arrange
var loader = new PluginLoader();
var pluginDir = Path.Combine(_testBaseDir, "test-plugin");
Directory.CreateDirectory(pluginDir);
// 只创建 DLL没有 plugin.json
File.WriteAllText(Path.Combine(pluginDir, "test-plugin.dll"), "dummy");
try
{
// Act
var plugins = loader.DiscoverPlugins(_testBaseDir).ToList();
// Assert
Assert.Empty(plugins);
}
finally
{
Directory.Delete(_testBaseDir, true);
}
}
[Fact]
public void CreateShadowCopy_ShouldCopyFiles()
{
// Arrange
var sourceDir = Path.Combine(_testBaseDir, "source");
var shadowDir = Path.Combine(_testBaseDir, "shadow");
Directory.CreateDirectory(sourceDir);
// 创建源 DLL
var dllPath = Path.Combine(sourceDir, "TestPlugin.dll");
File.WriteAllText(dllPath, "dummy dll");
// 创建 deps.json
var depsPath = dllPath + ".deps.json";
File.WriteAllText(depsPath, "{}");
try
{
// Act
var shadowPath = PluginLoader.CreateShadowCopy(dllPath, shadowDir);
// Assert
Assert.True(File.Exists(shadowPath));
Assert.True(File.Exists(Path.Combine(shadowDir, "TestPlugin.dll.deps.json")));
}
finally
{
Directory.Delete(_testBaseDir, true);
}
}
}