节点在线、应用在线、配置在线使用令牌查询
大石头 authored at 2021-12-16 19:49:30
16.04 KiB
Stardust
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using NewLife;
using StarAgent.Managers;
using Stardust.Models;
using Xunit;

namespace Stardust.Tests;

/// <summary>部署策略测试。覆盖工厂映射、模式能力开关与解压行为(PUB-18)</summary>
public class DeployStrategyTests
{
    /// <summary>创建临时工作目录</summary>
    private static String CreateTempDir()
    {
        var dir = Path.Combine(Path.GetTempPath(), "stardust-deploy-" + Guid.NewGuid().ToString("n")[..8]);
        Directory.CreateDirectory(dir);
        return dir;
    }

    /// <summary>创建包含指定文件的压缩包</summary>
    private static String CreateZip(String dir, params String[] files)
    {
        var zip = Path.Combine(dir, "app-" + Guid.NewGuid().ToString("n")[..8] + ".zip");
        using var fs = File.Create(zip);
        using var za = new ZipArchive(fs, ZipArchiveMode.Create);
        foreach (var item in files)
        {
            var entry = za.CreateEntry(item);
            using var es = entry.Open();
            using var sw = new StreamWriter(es);
            sw.Write("content of " + item);
        }

        return zip;
    }

    private static DeployContext CreateContext(String name, String workDir, String? zipFile, ServiceInfo? service = null, DeployInfo? deploy = null) => new()
    {
        Name = name,
        Service = service ?? new ServiceInfo { Name = name, FileName = name + ".dll", Enable = true },
        WorkingDirectory = workDir,
        ZipFile = zipFile,
        Deploy = deploy,
    };

    /// <summary>创建包含指定文件与内容的压缩包</summary>
    private static String CreateZipFiles(String dir, params (String Name, String Content)[] files)
    {
        var zip = Path.Combine(dir, "app-" + Guid.NewGuid().ToString("n")[..8] + ".zip");
        using var fs = File.Create(zip);
        using var za = new ZipArchive(fs, ZipArchiveMode.Create);
        foreach (var item in files)
        {
            var entry = za.CreateEntry(item.Name);
            using var es = entry.Open();
            using var sw = new StreamWriter(es);
            sw.Write(item.Content);
        }

        return zip;
    }

    [Fact(DisplayName = "工厂按新版模式创建对应策略")]
    public void Factory_NewModes()
    {
        Assert.IsType<StandardDeployStrategy>(DeployStrategyFactory.Create(DeployMode.Standard));
        Assert.IsType<ShadowDeployStrategy>(DeployStrategyFactory.Create(DeployMode.Shadow));
        Assert.IsType<HostedStrategy>(DeployStrategyFactory.Create(DeployMode.Hosted));
        Assert.IsType<TaskStrategy>(DeployStrategyFactory.Create(DeployMode.Task));
    }

    [Fact(DisplayName = "旧版模式值按兼容规则映射到新策略")]
    public void Factory_LegacyModes()
    {
        Assert.IsType<ShadowDeployStrategy>(DeployStrategyFactory.Create((DeployMode)0));
        Assert.IsType<HostedStrategy>(DeployStrategyFactory.Create((DeployMode)1));
        Assert.IsType<StandardDeployStrategy>(DeployStrategyFactory.Create((DeployMode)2));
        Assert.IsType<TaskStrategy>(DeployStrategyFactory.Create((DeployMode)3));
        Assert.IsType<ShadowDeployStrategy>(DeployStrategyFactory.Create((DeployMode)4));

        // 未知新版值兜底标准模式
        Assert.IsType<StandardDeployStrategy>(DeployStrategyFactory.Create((DeployMode)99));
    }

    [Fact(DisplayName = "服务信息为空时按标准模式处理")]
    public void Factory_NullService_Standard()
    {
        Assert.IsType<StandardDeployStrategy>(DeployStrategyFactory.Create((ServiceInfo)null));

        var service = new ServiceInfo { Name = "app", FileName = "app.dll", Mode = DeployMode.Task };
        Assert.IsType<TaskStrategy>(DeployStrategyFactory.Create(service));
    }

    [Fact(DisplayName = "各模式守护与接管能力符合约定")]
    public void Strategy_Capabilities()
    {
        var standard = new StandardDeployStrategy();
        Assert.True(standard.NeedGuardian);
        Assert.True(standard.AllowTakeOver);

        var shadow = new ShadowDeployStrategy();
        Assert.True(shadow.NeedGuardian);

        var hosted = new HostedStrategy();
        Assert.False(hosted.NeedGuardian);

        var task = new TaskStrategy();
        Assert.False(task.NeedGuardian);
        Assert.False(task.AllowTakeOver);
    }

    [Fact(DisplayName = "多实例判定兼容新属性与旧模式值")]
    public void Factory_IsMultipleAllowed()
    {
        Assert.False(DeployStrategyFactory.IsMultipleAllowed(null));
        Assert.True(DeployStrategyFactory.IsMultipleAllowed(new ServiceInfo { AllowMultiple = true }));
        Assert.True(DeployStrategyFactory.IsMultipleAllowed(new ServiceInfo { Mode = (DeployMode)4 }));
        Assert.False(DeployStrategyFactory.IsMultipleAllowed(new ServiceInfo { Mode = DeployMode.Standard }));
    }

    [Fact(DisplayName = "标准模式解压压缩包到工作目录并定位可执行文件")]
    public void Standard_Extract_ToWorkingDirectory()
    {
        var dir = CreateTempDir();
        try
        {
            var name = "demoapp";
            var zip = CreateZip(dir, name + ".dll", "appsettings.json");
            var workDir = Path.Combine(dir, "work");

            var ctx = CreateContext(name, workDir, zip);
            var rs = new StandardDeployStrategy().Extract(ctx);

            Assert.True(rs);
            Assert.True(File.Exists(Path.Combine(workDir, name + ".dll")));
            Assert.True(File.Exists(Path.Combine(workDir, "appsettings.json")));
            Assert.Equal(name + ".dll", Path.GetFileName(ctx.ExecuteFile));
        }
        finally
        {
            Directory.Delete(dir, true);
        }
    }

    [Fact(DisplayName = "影子模式解压到独立影子目录,配置文件复制到工作目录")]
    public void Shadow_Extract_ToShadowDirectory()
    {
        var dir = CreateTempDir();
        try
        {
            var name = "shadowapp";
            var zip = CreateZip(dir, name + ".dll", "appsettings.json");
            var workDir = Path.Combine(dir, "work");
            Directory.CreateDirectory(workDir);

            var ctx = CreateContext(name, workDir, zip);
            var rs = new ShadowDeployStrategy().Extract(ctx);

            Assert.True(rs);
            Assert.False(ctx.Shadow.IsNullOrEmpty());

            // 可执行文件在影子目录,配置文件被复制到工作目录
            Assert.True(File.Exists(Path.Combine(ctx.Shadow, name + ".dll")));
            Assert.Equal(name + ".dll", Path.GetFileName(ctx.ExecuteFile));
            Assert.True(File.Exists(Path.Combine(workDir, "appsettings.json")));
            Assert.False(File.Exists(Path.Combine(workDir, name + ".dll")));
        }
        finally
        {
            Directory.Delete(dir, true);
        }
    }

    [Fact(DisplayName = "标准模式保留语言包等子目录结构")]
    public void Standard_Extract_NestedDirectory()
    {
        var dir = CreateTempDir();
        try
        {
            var name = "localeapp";
            var zip = CreateZip(dir, name + ".dll", $"zh-Hans/{name}.resources.dll");
            var workDir = Path.Combine(dir, "work");

            var ctx = CreateContext(name, workDir, zip);
            var rs = new StandardDeployStrategy().Extract(ctx);

            Assert.True(rs);
            Assert.True(File.Exists(Path.Combine(workDir, "zh-Hans", name + ".resources.dll")));
        }
        finally
        {
            Directory.Delete(dir, true);
        }
    }

    [Fact(DisplayName = "托管模式保留语言包等子目录结构")]
    public void Hosted_Extract_NestedDirectory()
    {
        var dir = CreateTempDir();
        try
        {
            var name = "hostedapp";
            var zip = CreateZip(dir, name + ".dll", $"zh-Hans/{name}.resources.dll");
            var workDir = Path.Combine(dir, "work");

            var ctx = CreateContext(name, workDir, zip);
            var rs = new HostedStrategy().Extract(ctx);

            Assert.True(rs);
            Assert.True(File.Exists(Path.Combine(workDir, name + ".dll")));
            Assert.True(File.Exists(Path.Combine(workDir, "zh-Hans", name + ".resources.dll")));
        }
        finally
        {
            Directory.Delete(dir, true);
        }
    }

    [Fact(DisplayName = "影子模式保留语言包等子目录结构")]
    public void Shadow_Extract_NestedDirectory()
    {
        var dir = CreateTempDir();
        try
        {
            var name = "shadowlocale";
            var zip = CreateZip(dir, name + ".dll", $"zh-Hans/{name}.resources.dll");
            var workDir = Path.Combine(dir, "work");
            Directory.CreateDirectory(workDir);

            var ctx = CreateContext(name, workDir, zip);
            var rs = new ShadowDeployStrategy().Extract(ctx);

            Assert.True(rs);
            Assert.False(ctx.Shadow.IsNullOrEmpty());
            Assert.True(File.Exists(Path.Combine(ctx.Shadow, "zh-Hans", name + ".resources.dll")));
        }
        finally
        {
            Directory.Delete(dir, true);
        }
    }

    [Fact(DisplayName = "缺少压缩包时标准模式直接使用工作目录")]
    public void Standard_NoZip_UseWorkingDirectory()
    {
        var dir = CreateTempDir();
        try
        {
            var name = "nozipapp";
            File.WriteAllText(Path.Combine(dir, name + ".dll"), "binary");

            var ctx = CreateContext(name, dir, null);
            var rs = new StandardDeployStrategy().Extract(ctx);

            Assert.True(rs);
            Assert.Equal(name + ".dll", Path.GetFileName(ctx.ExecuteFile));
        }
        finally
        {
            Directory.Delete(dir, true);
        }
    }

    [Fact(DisplayName = "压缩包不存在且工作目录无可执行文件时解压失败")]
    public void Extract_MissingZip_Fail()
    {
        var dir = CreateTempDir();
        try
        {
            // 带路径分隔符的 FileName 不会当作系统命令,文件不存在则失败
            var service = new ServiceInfo { Name = "missingapp", FileName = "bin/missingapp.dll", Enable = true };
            var ctx = CreateContext("missingapp", Path.Combine(dir, "work"), Path.Combine(dir, "not-exist.zip"), service);
            var rs = new StandardDeployStrategy().Extract(ctx);

            Assert.False(rs);
            Assert.True(ctx.ExecuteFile.IsNullOrEmpty());
        }
        finally
        {
            Directory.Delete(dir, true);
        }
    }

    [Fact(DisplayName = "标准包清理旧可执行文件并保留本地配置")]
    public void Standard_Mode_ClearExecutables_KeepConfig()
    {
        var dir = CreateTempDir();
        try
        {
            var name = "stdapp";
            var workDir = Path.Combine(dir, "work");
            Directory.CreateDirectory(workDir);
            File.WriteAllText(Path.Combine(workDir, "stale.dll"), "old");
            File.WriteAllText(Path.Combine(workDir, "appsettings.json"), "local");
            File.WriteAllText(Path.Combine(workDir, "deploy.txt"), "keep");

            var zip = CreateZipFiles(dir, (name + ".dll", "new"), ("appsettings.json", "packaged"));
            var ctx = CreateContext(name, workDir, zip, deploy: new DeployInfo { Mode = DeployModes.Standard });
            var rs = new StandardDeployStrategy().Extract(ctx);

            Assert.True(rs);
            Assert.False(File.Exists(Path.Combine(workDir, "stale.dll")));
            Assert.Equal("local", File.ReadAllText(Path.Combine(workDir, "appsettings.json")));
            Assert.Equal("keep", File.ReadAllText(Path.Combine(workDir, "deploy.txt")));
            Assert.Equal("new", File.ReadAllText(Path.Combine(workDir, name + ".dll")));
        }
        finally
        {
            Directory.Delete(dir, true);
        }
    }

    [Fact(DisplayName = "完整包清理顶层文件后按包内容重建")]
    public void Full_Mode_ClearAllFiles()
    {
        var dir = CreateTempDir();
        try
        {
            var name = "fullapp";
            var workDir = Path.Combine(dir, "work");
            Directory.CreateDirectory(workDir);
            File.WriteAllText(Path.Combine(workDir, "stale.dll"), "old");
            File.WriteAllText(Path.Combine(workDir, "appsettings.json"), "local");
            File.WriteAllText(Path.Combine(workDir, "deploy.txt"), "keep");

            var zip = CreateZipFiles(dir, (name + ".dll", "new"), ("appsettings.json", "packaged"));
            var ctx = CreateContext(name, workDir, zip, deploy: new DeployInfo { Mode = DeployModes.Full });
            var rs = new StandardDeployStrategy().Extract(ctx);

            Assert.True(rs);
            Assert.False(File.Exists(Path.Combine(workDir, "stale.dll")));
            Assert.False(File.Exists(Path.Combine(workDir, "deploy.txt")));
            Assert.Equal("packaged", File.ReadAllText(Path.Combine(workDir, "appsettings.json")));
            Assert.Equal("new", File.ReadAllText(Path.Combine(workDir, name + ".dll")));
        }
        finally
        {
            Directory.Delete(dir, true);
        }
    }

    [Fact(DisplayName = "增量包保留已有配置文件")]
    public void Partial_Mode_KeepExistingConfig()
    {
        var dir = CreateTempDir();
        try
        {
            var name = "partapp";
            var workDir = Path.Combine(dir, "work");
            Directory.CreateDirectory(workDir);
            File.WriteAllText(Path.Combine(workDir, "appsettings.json"), "local");

            var zip = CreateZipFiles(dir, (name + ".dll", "new"), ("appsettings.json", "packaged"));
            var ctx = CreateContext(name, workDir, zip, deploy: new DeployInfo { Mode = DeployModes.Partial });
            var rs = new StandardDeployStrategy().Extract(ctx);

            Assert.True(rs);
            Assert.Equal("local", File.ReadAllText(Path.Combine(workDir, "appsettings.json")));
            Assert.Equal("new", File.ReadAllText(Path.Combine(workDir, name + ".dll")));
        }
        finally
        {
            Directory.Delete(dir, true);
        }
    }

    [Fact(DisplayName = "覆盖清单只覆盖命中的已有文件")]
    public void Overwrite_List_RestrictExistingFiles()
    {
        var dir = CreateTempDir();
        try
        {
            var name = "ovapp";
            var workDir = Path.Combine(dir, "work");
            Directory.CreateDirectory(workDir);
            File.WriteAllText(Path.Combine(workDir, "a.txt"), "old-a");
            File.WriteAllText(Path.Combine(workDir, "b.txt"), "old-b");

            var zip = CreateZipFiles(dir, (name + ".dll", "new"), ("a.txt", "new-a"), ("b.txt", "new-b"));
            var ctx = CreateContext(name, workDir, zip, deploy: new DeployInfo { Mode = DeployModes.Partial, Overwrite = "a.txt" });
            var rs = new StandardDeployStrategy().Extract(ctx);

            Assert.True(rs);
            Assert.Equal("new-a", File.ReadAllText(Path.Combine(workDir, "a.txt")));
            Assert.Equal("old-b", File.ReadAllText(Path.Combine(workDir, "b.txt")));
        }
        finally
        {
            Directory.Delete(dir, true);
        }
    }

    [Fact(DisplayName = "覆盖清单支持目录前缀匹配")]
    public void Overwrite_List_DirectoryPattern()
    {
        var dir = CreateTempDir();
        try
        {
            var name = "ovdir";
            var workDir = Path.Combine(dir, "work");
            Directory.CreateDirectory(Path.Combine(workDir, "lib"));
            Directory.CreateDirectory(Path.Combine(workDir, "other"));
            File.WriteAllText(Path.Combine(workDir, "lib", "x.dll"), "old-x");
            File.WriteAllText(Path.Combine(workDir, "other", "y.dll"), "old-y");

            var zip = CreateZipFiles(dir, (name + ".dll", "new"), ("lib/x.dll", "new-x"), ("other/y.dll", "new-y"));
            var ctx = CreateContext(name, workDir, zip, deploy: new DeployInfo { Mode = DeployModes.Partial, Overwrite = "lib/*" });
            var rs = new StandardDeployStrategy().Extract(ctx);

            Assert.True(rs);
            Assert.Equal("new-x", File.ReadAllText(Path.Combine(workDir, "lib", "x.dll")));
            Assert.Equal("old-y", File.ReadAllText(Path.Combine(workDir, "other", "y.dll")));
        }
        finally
        {
            Directory.Delete(dir, true);
        }
    }
}