extern alias web;
using System;
using System.Runtime.CompilerServices;
using NewLife.Security;
using Stardust.Data;
using Stardust.Data.Deployment;
using Stardust.Data.Nodes;
using Stardust.Models;
using Xunit;
using webDeploy = web::Stardust.Web.Services.DeployService;
namespace ServerTest.Deployment;
/// <summary>install 下å‘回填部署版本 + 应用程åºç‰ˆæœ¬å›žå¡«æµ‹è¯•(2026-11-12 方案C/新增 AppVersion å—æ®µï¼‰</summary>
/// <remarks>
/// 方案C:点击「å‘布ã€ï¼ˆinstall)å³å›žå¡« AppDeployNode.Version = 本次下å‘的版本(多版本å–å¹³å°åŒ¹é…包,å•版本å–部署集版本);
/// AppVersion:应用在线实例上报的应用入å£ç¨‹åºé›† FileVersionï¼Œç» Fill(AppOnline)/Fill(AppInfo) 回填,éžç©ºå®ˆå«é˜²æ¸…值。
/// 版本匹é…共享实现已下沉 AppDeploy.GetDeployVersion(DeployVersionMatchTests 覆盖匹é…ç»†èŠ‚ï¼Œæœ¬æ–‡ä»¶åªæµ‹å›žå¡«æŽ¥çº¿ï¼‰ã€‚
/// </remarks>
public class InstallVersionBackfillTests
{
/// <summary>创建åªç”¨äºŽ Install 回填的æœåŠ¡å®žä¾‹ã€‚Install ä¸ä¾èµ–æ³¨å…¥å—æ®µ</summary>
private static webDeploy CreateWebDeployService() => (webDeploy)RuntimeHelpers.GetUninitializedObject(typeof(webDeploy));
private static Node CreateNode(OSKinds osKind, String arch, String frameworks = null) => new()
{
Code = "nd_" + Rand.NextString(8),
Name = "node-" + Rand.NextString(4),
Enable = true,
OSKind = osKind,
Architecture = arch,
Frameworks = frameworks,
};
[Fact(DisplayName = "install 回填:多版本部署集按平å°åŒ¹é…版本回填节点版本")]
public void Install_MultiVersion_BackfillsMatchedVersion()
{
var app = new AppDeploy { Name = "dep_" + Rand.NextString(6), Enable = true, MultiVersion = true };
app.Insert();
var node = CreateNode(OSKinds.Ubuntu, "x64");
node.Insert();
var win = new AppDeployVersion { DeployId = app.Id, Version = "1.0.0", OS = OSKind.Windows, Arch = CpuArch.X64, Enable = true };
win.Insert();
var linux = new AppDeployVersion { DeployId = app.Id, Version = "1.0.1", OS = OSKind.Linux, Arch = CpuArch.X64, Enable = true };
linux.Insert();
try
{
var dn = new AppDeployNode { DeployId = app.Id, NodeId = node.ID, Enable = true };
dn.Insert();
CreateWebDeployService().Install(dn);
// Ubuntu 节点应拿到 Linux åŒ…çš„ç‰ˆæœ¬ä¸²ï¼Œè€Œä¸æ˜¯éƒ¨ç½²é›†/别的包
Assert.True(dn.Enable);
Assert.Equal("1.0.1", dn.Version);
}
finally
{
win.Delete();
linux.Delete();
node.Delete();
app.Delete();
}
}
[Fact(DisplayName = "install 回填:å•ç‰ˆæœ¬éƒ¨ç½²é›†ä¸”ç‰ˆæœ¬è¡¨æ— åŒ¹é…行时兜底部署集版本")]
public void Install_NoMatchedVersion_FallsBackToAppVersion()
{
var app = new AppDeploy { Name = "dep_" + Rand.NextString(6), Enable = true, MultiVersion = true, Version = "2.0.0" };
app.Insert();
var node = CreateNode(OSKinds.Ubuntu, "x64");
node.Insert();
try
{
var dn = new AppDeployNode { DeployId = app.Id, NodeId = node.ID, Enable = true };
dn.Insert();
CreateWebDeployService().Install(dn);
// ç‰ˆæœ¬è¡¨æ— åŒ¹é…行(与æœåŠ¡ç«¯ BuildDeployInfo æ— æ¡ä»¶ app.Version åŒä¸€å£å¾„的兜底)
Assert.Equal("2.0.0", dn.Version);
}
finally
{
node.Delete();
app.Delete();
}
}
[Fact(DisplayName = "install å›žå¡«ï¼šéƒ¨ç½²é›†æ— ç‰ˆæœ¬æ—¶ä¸å†™å…¥ï¼ˆä¿æŒåŽŸå€¼ï¼‰")]
public void Install_NoVersionAnywhere_KeepsOriginalValue()
{
var app = new AppDeploy { Name = "dep_" + Rand.NextString(6), Enable = true, MultiVersion = false };
app.Insert();
var node = CreateNode(OSKinds.Ubuntu, "x64");
node.Insert();
try
{
var dn = new AppDeployNode { DeployId = app.Id, NodeId = node.ID, Enable = true, Version = "keep" };
dn.Insert();
CreateWebDeployService().Install(dn);
Assert.True(dn.Enable);
Assert.Equal("keep", dn.Version);
}
finally
{
node.Delete();
app.Delete();
}
}
[Fact(DisplayName = "Fill(AppOnline):在线实例版本回填 AppVersionï¼Œç©ºå€¼ä¸æ¸…已有版本(守å«ï¼‰")]
public void FillOnline_AppVersion_Guarded()
{
var dn = new AppDeployNode { DeployId = 1, NodeId = 1, AppVersion = "9.9.9.9" };
dn.Fill(new AppOnline { IP = "127.0.0.1", Version = "1.2.3.0" });
Assert.Equal("1.2.3.0", dn.AppVersion);
// 心跳/在线刷新未带版本(如 StarAgent 代上报)时ä¸å¾—清空已登记的应用程åºç‰ˆæœ¬
dn.Fill(new AppOnline { IP = "127.0.0.1" });
Assert.Equal("1.2.3.0", dn.AppVersion);
}
[Fact(DisplayName = "Fill(AppInfo):进程上报的客户端文件版本回填 AppVersion,空值守å«")]
public void FillAppInfo_AppVersion_Guarded()
{
var dn = new AppDeployNode { DeployId = 1, NodeId = 1 };
dn.Fill(new AppInfo { Id = 123, Name = "test", Version = "2.0.0.0" });
Assert.Equal("2.0.0.0", dn.AppVersion);
dn.Fill(new AppInfo { Id = 123, Name = "test" });
Assert.Equal("2.0.0.0", dn.AppVersion);
}
}
|