修复Linux启动就绪通知并隔离命令行测试目录
通过本机Unix套接字报告HTTPS监听状态,同步服务配置与安装模板,保持初始化业务就绪隔离。补充生命周期回归并修复旧CLI测试误用正式目录的问题。更新Linux原生测试证据,明确新候选尚未部署、生产验收未放行。
This commit is contained in:
1 parent
b6b964203d
commit
0b74886c45
24 files changed
+647
-21
No files matched your search
@@ -0,0 +1,107 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/localreset"
|
||||
)
|
||||
|
||||
type readyTestHTTP struct {
|
||||
applicationTestHTTP
|
||||
entered chan struct{}
|
||||
}
|
||||
|
||||
func (h *readyTestHTTP) ListenAndServe() error {
|
||||
close(h.entered)
|
||||
return h.applicationTestHTTP.ListenAndServe()
|
||||
}
|
||||
|
||||
func TestApplicationReadyLifecycle(t *testing.T) {
|
||||
for _, scenario := range []string{"running", "notify-failure", "startup-failure", "cancel-starting"} {
|
||||
t.Run(scenario, func(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
web := &readyTestHTTP{
|
||||
applicationTestHTTP: applicationTestHTTP{ready: make(chan struct{}), exit: make(chan error, 2)},
|
||||
entered: make(chan struct{}),
|
||||
}
|
||||
t.Cleanup(func() { web.exit <- http.ErrServerClosed })
|
||||
control := &applicationTestControl{serve: func(ctx context.Context, _ localreset.Factory) error {
|
||||
<-ctx.Done()
|
||||
return nil
|
||||
}}
|
||||
expected := errors.New("启动检查失败")
|
||||
var notifications atomic.Int32
|
||||
var renewals atomic.Int32
|
||||
renewed := make(chan struct{})
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := runApplicationServices(ctx, web, func(context.Context) {
|
||||
renewals.Add(1)
|
||||
if notifications.Load() != 1 {
|
||||
t.Error("未完成监听通知就启动了续期")
|
||||
}
|
||||
close(renewed)
|
||||
}, func() error {
|
||||
notifications.Add(1)
|
||||
if scenario == "notify-failure" {
|
||||
return expected
|
||||
}
|
||||
return nil
|
||||
}, applicationTestFactory, func() (applicationControl, error) { return control, nil })
|
||||
done <- err
|
||||
}()
|
||||
select {
|
||||
case <-web.entered:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("网络服务未启动")
|
||||
}
|
||||
if notifications.Load() != 0 {
|
||||
t.Fatal("尚未进入监听就报告成功")
|
||||
}
|
||||
switch scenario {
|
||||
case "startup-failure":
|
||||
web.exit <- expected
|
||||
case "cancel-starting":
|
||||
cancel()
|
||||
default:
|
||||
close(web.ready)
|
||||
}
|
||||
if scenario == "running" {
|
||||
select {
|
||||
case <-renewed:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("通知完成后未启动续期")
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
select {
|
||||
case err := <-done:
|
||||
if scenario == "notify-failure" || scenario == "startup-failure" {
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatalf("丢失启动失败原因:%v", err)
|
||||
}
|
||||
} else if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("启动失败或取消后未退出")
|
||||
}
|
||||
if control.closed.Load() != 1 || (scenario != "running" && renewals.Load() != 0) {
|
||||
t.Fatal("启动失败后的资源或续期状态不正确")
|
||||
}
|
||||
want := int32(0)
|
||||
if scenario == "running" || scenario == "notify-failure" {
|
||||
want = 1
|
||||
}
|
||||
if notifications.Load() != want {
|
||||
t.Fatal("就绪通知次数不正确")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ func runApplicationServices(
|
||||
ctx context.Context,
|
||||
web applicationHTTP,
|
||||
startRenewal func(context.Context),
|
||||
onReady func() error,
|
||||
factory localreset.Factory,
|
||||
openControl func() (applicationControl, error),
|
||||
) (_ bool, resultErr error) {
|
||||
@@ -60,6 +61,22 @@ func runApplicationServices(
|
||||
if ctx.Err() != nil {
|
||||
return false, nil
|
||||
}
|
||||
select {
|
||||
case err := <-controlDone:
|
||||
controlFinished = true
|
||||
return false, applicationControlExit(ctx, err)
|
||||
case err := <-httpDone:
|
||||
return false, applicationHTTPExit(err)
|
||||
default:
|
||||
}
|
||||
if onReady != nil {
|
||||
if ctx.Err() != nil {
|
||||
return false, nil
|
||||
}
|
||||
if err := onReady(); err != nil {
|
||||
return false, fmt.Errorf("报告服务监听状态失败:%w", err)
|
||||
}
|
||||
}
|
||||
if startRenewal != nil {
|
||||
startRenewal(ctx)
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ func applicationTestFactory(context.Context, localreset.Peer) (localreset.Sessio
|
||||
func TestApplicationControlFailurePreventsHTTPStartup(t *testing.T) {
|
||||
web := &applicationTestHTTP{ready: make(chan struct{}), exit: make(chan error, 1)}
|
||||
expected := errors.New("test-control-bind-failure")
|
||||
_, err := runApplicationServices(t.Context(), web, nil, applicationTestFactory, func() (applicationControl, error) {
|
||||
_, err := runApplicationServices(t.Context(), web, nil, nil, applicationTestFactory, func() (applicationControl, error) {
|
||||
return nil, expected
|
||||
})
|
||||
if !errors.Is(err, expected) || web.started.Load() != 0 {
|
||||
@@ -56,7 +56,7 @@ func TestApplicationCancellationBeforeStartupDoesNotOpenControl(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
cancel()
|
||||
opened := false
|
||||
_, err := runApplicationServices(ctx, nil, nil, nil, func() (applicationControl, error) {
|
||||
_, err := runApplicationServices(ctx, nil, nil, nil, nil, func() (applicationControl, error) {
|
||||
opened = true
|
||||
return nil, nil
|
||||
})
|
||||
@@ -98,7 +98,7 @@ func TestApplicationWaitsForControlWorkBeforeReturning(t *testing.T) {
|
||||
renewal := make(chan struct{})
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := runApplicationServices(ctx, web, func(context.Context) { close(renewal) }, applicationTestFactory, func() (applicationControl, error) { return control, nil })
|
||||
_, err := runApplicationServices(ctx, web, func(context.Context) { close(renewal) }, nil, applicationTestFactory, func() (applicationControl, error) { return control, nil })
|
||||
done <- err
|
||||
}()
|
||||
<-entered
|
||||
@@ -163,7 +163,7 @@ func TestApplicationControlUnexpectedExitFailsClosed(t *testing.T) {
|
||||
renewal := make(chan struct{})
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
_, err := runApplicationServices(t.Context(), web, func(context.Context) { close(renewal) }, applicationTestFactory, func() (applicationControl, error) { return control, nil })
|
||||
_, err := runApplicationServices(t.Context(), web, func(context.Context) { close(renewal) }, nil, applicationTestFactory, func() (applicationControl, error) { return control, nil })
|
||||
done <- err
|
||||
}()
|
||||
if ready {
|
||||
|
||||
@@ -17,7 +17,7 @@ import (
|
||||
|
||||
func TestCreateBackupIncludesSecretsCertificatesAndSQLiteSnapshot(t *testing.T) {
|
||||
base := t.TempDir()
|
||||
cfg := config.Default(base)
|
||||
cfg := isolatedCLIConfig(base)
|
||||
for _, directory := range []string{cfg.Paths.Config, cfg.Paths.Data, cfg.Paths.Certificate, cfg.Paths.Backup} {
|
||||
if err := os.MkdirAll(directory, 0o700); err != nil {
|
||||
t.Fatalf("创建测试目录失败:%v", err)
|
||||
@@ -134,7 +134,7 @@ func TestCreateBackupDoesNotChangeExistingParentPermissions(t *testing.T) {
|
||||
if err := os.Mkdir(parent, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := config.Default(filepath.Join(base, "scert"))
|
||||
cfg := isolatedCLIConfig(filepath.Join(base, "scert"))
|
||||
for _, directory := range []string{cfg.Paths.Config, cfg.Paths.Data} {
|
||||
if err := os.MkdirAll(directory, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -161,7 +161,7 @@ func TestCreateBackupDoesNotChangeExistingParentPermissions(t *testing.T) {
|
||||
|
||||
func TestCreateBackupRequiresSecretsWhenInitialized(t *testing.T) {
|
||||
base := t.TempDir()
|
||||
cfg := config.Default(base)
|
||||
cfg := isolatedCLIConfig(base)
|
||||
for _, directory := range []string{cfg.Paths.Config, cfg.Paths.Data} {
|
||||
if err := os.MkdirAll(directory, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -182,7 +182,7 @@ func TestCreateBackupRequiresSecretsWhenInitialized(t *testing.T) {
|
||||
|
||||
func TestRestoreAdditionalAssetsPreservesCertificateRoot(t *testing.T) {
|
||||
base := t.TempDir()
|
||||
cfg := config.Default(filepath.Join(base, "target"))
|
||||
cfg := isolatedCLIConfig(filepath.Join(base, "target"))
|
||||
input := filepath.Join(base, "backup")
|
||||
if err := os.MkdirAll(filepath.Join(input, "certificates", "example.com"), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -241,7 +241,7 @@ func TestRestoreAdditionalAssetsPreservesCertificateRoot(t *testing.T) {
|
||||
|
||||
func TestRestoreAdditionalAssetsRestoresAbsentState(t *testing.T) {
|
||||
base := t.TempDir()
|
||||
cfg := config.Default(filepath.Join(base, "target"))
|
||||
cfg := isolatedCLIConfig(filepath.Join(base, "target"))
|
||||
input := filepath.Join(base, "backup")
|
||||
if err := os.MkdirAll(input, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -330,7 +330,7 @@ func TestRestoreDirectoryWorksWhenCertificateParentIsNotWritable(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestValidateRestoreCompatibilityRejectsCrossDatabase(t *testing.T) {
|
||||
current := config.Default(t.TempDir())
|
||||
current := isolatedCLIConfig(t.TempDir())
|
||||
backup := current
|
||||
backup.Database.DSN = filepath.Join(t.TempDir(), "other.db")
|
||||
if err := validateRestoreCompatibility(current, backup); err == nil || !strings.Contains(err.Error(), "跨数据库") {
|
||||
@@ -463,7 +463,7 @@ func createSQLiteTestDatabase(path, action string) error {
|
||||
func prepareRestoreCommandFixture(t *testing.T, blockedCertificate bool) (config.Config, string, string) {
|
||||
t.Helper()
|
||||
base := t.TempDir()
|
||||
cfg := config.Default(filepath.Join(base, "target"))
|
||||
cfg := isolatedCLIConfig(filepath.Join(base, "target"))
|
||||
for _, directory := range []string{cfg.Paths.Config, cfg.Paths.Data, cfg.Paths.Backup} {
|
||||
if err := os.MkdirAll(directory, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
)
|
||||
|
||||
// isolatedCLIConfig 明确覆盖 Linux 的正式安装路径,测试不得访问现有服务数据。
|
||||
func isolatedCLIConfig(root string) config.Config {
|
||||
cfg := config.Default(root)
|
||||
cfg.Paths = config.PathsConfig{
|
||||
Install: root,
|
||||
Config: filepath.Join(root, "conf"),
|
||||
Data: filepath.Join(root, "data"),
|
||||
Certificate: filepath.Join(root, "ssl"),
|
||||
Log: filepath.Join(root, "log"),
|
||||
Backup: filepath.Join(root, "backups"),
|
||||
}
|
||||
cfg.Database.DSN = filepath.Join(cfg.Paths.Data, "scert.db")
|
||||
return cfg
|
||||
}
|
||||
|
||||
func TestCLIConfigStaysInTempRoot(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
cfg := isolatedCLIConfig(root)
|
||||
for _, path := range []string{cfg.Paths.Install, cfg.Paths.Config, cfg.Paths.Data, cfg.Paths.Certificate, cfg.Paths.Log, cfg.Paths.Backup, cfg.Database.DSN} {
|
||||
relative, err := filepath.Rel(root, path)
|
||||
if err != nil || filepath.IsAbs(relative) || relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) {
|
||||
t.Fatalf("测试配置逃逸出临时目录:%s", path)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -129,7 +129,7 @@ func TestDNSCheckReportsLiveResultWithoutPersistingConfiguration(t *testing.T) {
|
||||
func TestDNSCheckDoesNotCreateMissingMasterKey(t *testing.T) {
|
||||
oldPath := configPath
|
||||
t.Cleanup(func() { configPath = oldPath })
|
||||
cfg := config.Default(t.TempDir())
|
||||
cfg := isolatedCLIConfig(t.TempDir())
|
||||
cfg.DNS.Provider, cfg.DNS.CredentialRef = "aliyun", "secret://dns/check"
|
||||
configPath = filepath.Join(cfg.Paths.Config, "scert.json")
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
@@ -149,7 +149,7 @@ func prepareDNSCheckConfig(t *testing.T) config.Config {
|
||||
t.Helper()
|
||||
oldPath := configPath
|
||||
t.Cleanup(func() { configPath = oldPath })
|
||||
cfg := config.Default(t.TempDir())
|
||||
cfg := isolatedCLIConfig(t.TempDir())
|
||||
cfg.DNS.Provider, cfg.DNS.CredentialRef = "aliyun", "secret://dns/check"
|
||||
configPath = filepath.Join(cfg.Paths.Config, "scert.json")
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
|
||||
@@ -26,7 +26,7 @@ import (
|
||||
func initTestConfig(t *testing.T) config.Config {
|
||||
t.Helper()
|
||||
root := t.TempDir()
|
||||
cfg := config.Default(root)
|
||||
cfg := isolatedCLIConfig(root)
|
||||
cfg.Paths.Config = filepath.Join(root, "conf")
|
||||
cfg.Paths.Certificate = filepath.Join(root, "certificates")
|
||||
cfg.Paths.Backup = filepath.Join(root, "backups")
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/app"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/localreset"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/serviceready"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -89,7 +90,10 @@ func runApplication(ctx context.Context, cfg config.Config, path string) (bool,
|
||||
if service.Renewal != nil {
|
||||
startRenewal = service.Renewal.Start
|
||||
}
|
||||
return runApplicationServices(ctx, service.HTTP, startRenewal, service.HTTP.LocalResetFactory(), func() (applicationControl, error) {
|
||||
onReady := func() error {
|
||||
return serviceready.Ready("正式 HTTPS 服务已监听;业务状态请检查 /readyz")
|
||||
}
|
||||
return runApplicationServices(ctx, service.HTTP, startRenewal, onReady, service.HTTP.LocalResetFactory(), func() (applicationControl, error) {
|
||||
return localreset.Listen("")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -6,12 +6,11 @@ import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/database"
|
||||
)
|
||||
|
||||
func TestSQLiteSnapshotWhileWriterIsActive(t *testing.T) {
|
||||
cfg := config.Default(t.TempDir())
|
||||
cfg := isolatedCLIConfig(t.TempDir())
|
||||
db, err := database.Open(t.Context(), cfg.Database)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
Reference in new issue
Block a user