refactor: use nginx -s reload instead of systemd
- doc: add comment
This commit is contained in:
@@ -20,14 +20,21 @@ import (
|
||||
"yms-daemon/internal/updatepackage"
|
||||
)
|
||||
|
||||
// TestUpdaterMigratesLegacyBackendAndCommits 验证通过 repack 包更新时,能把仍在运行的旧版后端单元
|
||||
// (ymsback.service)迁移到模板单元并完成提交。
|
||||
func TestUpdaterMigratesLegacyBackendAndCommits(t *testing.T) {
|
||||
testUpdaterMigratesLegacyBackendAndCommits(t, false)
|
||||
}
|
||||
|
||||
// TestUpdaterCommitsDirectNativeJAR 验证直接安装 native JAR 时能完成提交,
|
||||
// 并进一步验证重启事务的创建、恢复与槽位旋转。
|
||||
func TestUpdaterCommitsDirectNativeJAR(t *testing.T) {
|
||||
testUpdaterMigratesLegacyBackendAndCommits(t, true)
|
||||
}
|
||||
|
||||
// testUpdaterMigratesLegacyBackendAndCommits 上述两个测试共用的驱动函数。
|
||||
// 当 direct 为 false 时走 repack 更新路径,为 true 时走直接 JAR 安装路径,
|
||||
// 并额外验证重启事务的创建、恢复以及槽位旋转行为。
|
||||
func testUpdaterMigratesLegacyBackendAndCommits(t *testing.T, direct bool) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
@@ -164,6 +171,7 @@ func testUpdaterMigratesLegacyBackendAndCommits(t *testing.T, direct bool) {
|
||||
}
|
||||
}
|
||||
|
||||
// serverConfiguration8081 测试用的 host Nginx 配置片段,其活跃后端端口为 8081。
|
||||
const serverConfiguration8081 = `http {
|
||||
upstream yms-server {
|
||||
# yms-update managed upstream begin
|
||||
@@ -174,16 +182,20 @@ const serverConfiguration8081 = `http {
|
||||
}
|
||||
`
|
||||
|
||||
// memoryGateway 网关控制器的测试替身,在内存中保存 host Nginx 快照,
|
||||
// 记录 Apply 调用次数,并支持在应用前注入失败以模拟切换故障。
|
||||
type memoryGateway struct {
|
||||
snapshot hostnginx.Snapshot
|
||||
applyCount int
|
||||
beforeApply func(hostnginx.Snapshot) error
|
||||
}
|
||||
|
||||
// Read 返回当前保存的网关快照副本。
|
||||
func (g *memoryGateway) Read() (hostnginx.Snapshot, error) {
|
||||
return hostnginx.Snapshot{Content: append([]byte(nil), g.snapshot.Content...), ActivePort: g.snapshot.ActivePort}, nil
|
||||
}
|
||||
|
||||
// Apply 在 beforeApply 钩子通过后,将给定快照保存为当前状态并累计应用次数。
|
||||
func (g *memoryGateway) Apply(_ context.Context, snapshot hostnginx.Snapshot) error {
|
||||
if g.beforeApply != nil {
|
||||
if err := g.beforeApply(snapshot); err != nil {
|
||||
@@ -195,15 +207,19 @@ func (g *memoryGateway) Apply(_ context.Context, snapshot hostnginx.Snapshot) er
|
||||
return nil
|
||||
}
|
||||
|
||||
// updateUnitManager systemd 单元管理器的测试替身,在内存中维护单元状态,
|
||||
// 并记录最近一次被停止的单元名称。
|
||||
type updateUnitManager struct {
|
||||
units map[string]systemd.Unit
|
||||
stopped string
|
||||
}
|
||||
|
||||
// Inspect 返回指定名称的单元状态。
|
||||
func (m *updateUnitManager) Inspect(_ context.Context, name string) (systemd.Unit, error) {
|
||||
return m.units[name], nil
|
||||
}
|
||||
|
||||
// Start 将指定单元置为 active。
|
||||
func (m *updateUnitManager) Start(_ context.Context, name string) error {
|
||||
unit := m.units[name]
|
||||
unit.ActiveState = "active"
|
||||
@@ -211,6 +227,7 @@ func (m *updateUnitManager) Start(_ context.Context, name string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Stop 将指定单元置为 failed 并记录其名称。
|
||||
func (m *updateUnitManager) Stop(_ context.Context, name string) error {
|
||||
unit := m.units[name]
|
||||
unit.ActiveState = "failed"
|
||||
@@ -219,12 +236,16 @@ func (m *updateUnitManager) Stop(_ context.Context, name string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// preparingExecutor 原生后端执行器的测试替身:它把制品写入发布目录、
|
||||
// 建立槽位软链接、启动目标单元并推进事务状态,模拟执行器在真实环境中的工作。
|
||||
type preparingExecutor struct {
|
||||
store *transaction.Store
|
||||
releaseDir string
|
||||
units *updateUnitManager
|
||||
}
|
||||
|
||||
// Run 将请求中的制品落盘到发布目录,创建槽位软链接,启动目标单元,
|
||||
// 并把事务依次推进到 Starting 状态,用于驱动后续切换与提交逻辑。
|
||||
func (e *preparingExecutor) Run(ctx context.Context, transactionID string, request nativebackendexecutor.Request) error {
|
||||
if err := os.MkdirAll(e.releaseDir, 0o750); err != nil {
|
||||
return err
|
||||
@@ -259,6 +280,8 @@ func (e *preparingExecutor) Run(ctx context.Context, transactionID string, reque
|
||||
return nil
|
||||
}
|
||||
|
||||
// writeNativeBackendPackage 生成一个包含 artifact-selection.json 清单和
|
||||
// glory-soft-yms-test.jar 制品的 repack ZIP 包,返回其路径。
|
||||
func writeNativeBackendPackage(t *testing.T, jar []byte) string {
|
||||
t.Helper()
|
||||
digest := sha256.Sum256(jar)
|
||||
@@ -314,6 +337,8 @@ func writeNativeBackendPackage(t *testing.T, jar []byte) string {
|
||||
return packagePath
|
||||
}
|
||||
|
||||
// writeDirectBackendJAR 生成一个包含 BOOT-INF/classes/application.properties 的直接 native JAR,
|
||||
// 返回其路径,供直接安装更新路径使用。
|
||||
func writeDirectBackendJAR(t *testing.T, content []byte) string {
|
||||
t.Helper()
|
||||
jarPath := filepath.Join(t.TempDir(), "glory-soft-yms.jar")
|
||||
|
||||
Reference in New Issue
Block a user