feat: backend native executor implement

This commit is contained in:
2026-08-16 01:27:30 +08:00
parent fbce91d797
commit 390e0565d3
44 changed files with 6430 additions and 31 deletions
+99
View File
@@ -0,0 +1,99 @@
package backendupdate
import (
"context"
"os"
"path/filepath"
"testing"
"yms-daemon/internal/systemd"
"yms-daemon/internal/transaction"
)
func TestPathOperationReplacesRegularCompatibilityJarAndRestoresIt(t *testing.T) {
root := t.TempDir()
activePath := filepath.Join(root, "glory-soft-yms.jar")
original := []byte("original backend JAR")
if err := os.WriteFile(activePath, original, 0o644); err != nil {
t.Fatalf("write original compatibility JAR: %v", err)
}
before, err := snapshotPath(activePath, filepath.Join(root, "transaction", "active.before"))
if err != nil {
t.Fatalf("snapshot compatibility JAR: %v", err)
}
installedPath := filepath.Join(root, "releases", "new.jar")
if err := os.MkdirAll(filepath.Dir(installedPath), 0o750); err != nil {
t.Fatalf("create release directory: %v", err)
}
if err := os.WriteFile(installedPath, []byte("new backend JAR"), 0o640); err != nil {
t.Fatalf("write new backend JAR: %v", err)
}
apply := &pathOperation{path: activePath, before: before, desired: pathState{Kind: pathKindSymlink, Target: installedPath}}
if err := apply.Apply(context.Background()); err != nil {
t.Fatalf("replace compatibility JAR with link: %v", err)
}
if target, err := os.Readlink(activePath); err != nil || target != installedPath {
t.Fatalf("unexpected compatibility link: target=%q err=%v", target, err)
}
restore := &pathOperation{path: activePath, before: pathState{Kind: pathKindSymlink, Target: installedPath}, desired: before}
if err := restore.Apply(context.Background()); err != nil {
t.Fatalf("restore compatibility JAR: %v", err)
}
actual, err := os.ReadFile(activePath)
if err != nil || string(actual) != string(original) {
t.Fatalf("unexpected restored compatibility JAR: content=%q err=%v", actual, err)
}
}
func TestResolveCurrentUnitSupportsFirstLegacyMigrationAndTemplateRotation(t *testing.T) {
tests := []struct {
name string
configured string
legacy string
want string
}{
{name: "legacy", configured: "inactive", legacy: "activating", want: legacyUnit8081},
{name: "template", configured: "active", legacy: "failed", want: "yms-backend@8081.service"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
units := &unitStateManager{units: map[string]systemd.Unit{
"yms-backend@8081.service": {Name: "yms-backend@8081.service", LoadState: "loaded", ActiveState: test.configured},
legacyUnit8081: {Name: legacyUnit8081, LoadState: "loaded", ActiveState: test.legacy},
}}
updater := &Updater{units: units}
actual, err := updater.resolveCurrentUnit(context.Background(), 8081, "yms-backend@8081.service")
if err != nil || actual != test.want {
t.Fatalf("unexpected current unit: unit=%q err=%v", actual, err)
}
})
}
}
func TestUnitStopOperationAcceptsSystemdFailedAsStopped(t *testing.T) {
units := &unitStateManager{units: map[string]systemd.Unit{
legacyUnit8081: {Name: legacyUnit8081, LoadState: "loaded", ActiveState: "failed"},
}}
operation := &unitStopOperation{units: units, name: legacyUnit8081}
inspection, err := operation.Inspect(context.Background())
if err != nil {
t.Fatalf("inspect stopped legacy unit: %v", err)
}
if inspection.Status != transaction.InspectionApplied {
t.Fatalf("unexpected stopped legacy unit inspection: %+v", inspection)
}
}
type unitStateManager struct {
units map[string]systemd.Unit
}
func (m *unitStateManager) Inspect(_ context.Context, name string) (systemd.Unit, error) {
return m.units[name], nil
}
func (m *unitStateManager) Start(context.Context, string) error { return nil }
func (m *unitStateManager) Stop(context.Context, string) error { return nil }