Files
StoreWeb/Tests/PluginLoadingSmoke/Program.cs
T

53 lines
3.1 KiB
C#

using System.Reflection;
using MAX.Framework.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Controllers;
// Run each scenario in a fresh process: default ALC intentionally cannot unload plugins.
var finderType = typeof(ServiceCollectionExtensions).Assembly.GetType("MAX.Framework.DependencyInjection.StoreAppTypeFinder", true)!;
var finder = finderType.GetProperty("Current")!.GetValue(null);
var method = finderType.GetMethod("GetAssemblies", new[] { typeof(string) })!;
var mode = args.ElementAtOrDefault(1) ?? "plugins";
List<Assembly> assemblies;
try
{
assemblies = (List<Assembly>)method.Invoke(finder, new object[] { args[0] == "-" ? null : args[0] })!;
}
catch (TargetInvocationException ex) when (mode == "failure" && ex.InnerException is InvalidOperationException)
{
Console.WriteLine("PASS expected loading failure: " + ex.InnerException.Message);
return;
}
if (mode == "failure") throw new Exception("Expected loading failure");
var plugins = assemblies.Where(a => a.GetName().Name is "Store.CloudProduct" or "Store.ImageCache").ToArray();
if (mode == "empty")
{
if (plugins.Length != 0) throw new Exception("Unexpected plugins");
Console.WriteLine("PASS empty/missing directory");
return;
}
if (plugins.Length != (mode == "image" ? 1 : 2)) throw new Exception("Unexpected plugin count");
if (assemblies.Select(a => a.FullName).Distinct().Count() != assemblies.Count) throw new Exception("Duplicate assemblies");
var mvc = new ServiceCollection().AddControllers();
foreach (var plugin in plugins) mvc.AddApplicationPart(plugin);
var feature = new ControllerFeature();
mvc.PartManager.PopulateFeature(feature);
if (!feature.Controllers.Any(t => t.Name == "ImageCacheController")) throw new Exception("ImageCache controller missing");
if (mode != "image" && !feature.Controllers.Any(t => t.Name == "GoodsController")) throw new Exception("Goods controller missing");
if (mode == "image")
{
var host = Host.CreateApplicationBuilder();
var core = typeof(ServiceCollectionExtensions).GetMethod("AddStoreCore", BindingFlags.Static | BindingFlags.NonPublic)!;
core.Invoke(null, new object[] { host, plugins, new Dictionary<Type, object>() });
if (string.IsNullOrEmpty(host.Configuration["ImageCache:ConnectionString"])) throw new Exception("Plugin configuration missing");
if (!host.Services.Any(d => d.ServiceType.Name == "ImageCacheService")) throw new Exception("Plugin DI registration missing");
if (!host.Services.Any(d => d.ServiceKey is string key && key == "ImageCacheConnection")) throw new Exception("Startup registration missing");
var registeredMvc = host.Services.AddControllers();
var registered = new ControllerFeature();
registeredMvc.PartManager.PopulateFeature(registered);
if (!registered.Controllers.Any(t => t.Name == "ImageCacheController")) throw new Exception("Core MVC registration missing");
}
Console.WriteLine($"PASS plugins={plugins.Length}, controllers={feature.Controllers.Count}; dependencies/types loaded" + (mode == "image" ? "; configuration/DI/Startup/MVC registered" : ""));