修复Linux测试目录隔离并通过HTTP全量实机复验
This commit is contained in:
1 parent
5a96623854
commit
b866b135b7
4 files changed
+128
-16
No files matched your search
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -39,7 +40,7 @@ func TestSettingsAPIUpdatesSafeFieldsWithoutReturningSecrets(t *testing.T) {
|
||||
cfg.Admin.Email = "admin@example.test"
|
||||
cfg.Admin.PasswordHash = mustTestPasswordHash(t, "Password123")
|
||||
cfg.Database.DSN = "mysql://secret-user:secret-password@localhost/scert"
|
||||
configPath := dir + "\\config\\scert.json"
|
||||
configPath := filepath.Join(dir, "config", "scert.json")
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
t.Fatalf("保存测试配置失败:%v", err)
|
||||
}
|
||||
@@ -99,7 +100,7 @@ func TestSettingsAPIWebAddressChangeDoesNotRequireRestart(t *testing.T) {
|
||||
cfg.Admin.Username = "admin"
|
||||
cfg.Admin.Email = "admin@example.test"
|
||||
cfg.Admin.PasswordHash = mustTestPasswordHash(t, "Password123")
|
||||
configPath := dir + "\\config\\scert.json"
|
||||
configPath := filepath.Join(dir, "config", "scert.json")
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
t.Fatalf("保存测试配置失败:%v", err)
|
||||
}
|
||||
@@ -141,7 +142,7 @@ func TestSettingsAPIWebEnabledChangeRequiresRestart(t *testing.T) {
|
||||
cfg.Admin.Username = "admin"
|
||||
cfg.Admin.Email = "admin@example.test"
|
||||
cfg.Admin.PasswordHash = mustTestPasswordHash(t, "Password123")
|
||||
configPath := dir + "\\config\\scert.json"
|
||||
configPath := filepath.Join(dir, "config", "scert.json")
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
t.Fatalf("保存测试配置失败:%v", err)
|
||||
}
|
||||
@@ -191,7 +192,7 @@ func TestSettingsAPIReturnsAndUpdatesRenewalPolicy(t *testing.T) {
|
||||
cfg.Admin.Username = "admin"
|
||||
cfg.Admin.Email = "admin@example.test"
|
||||
cfg.Admin.PasswordHash = mustTestPasswordHash(t, "Password123")
|
||||
configPath := dir + "\\config\\scert.json"
|
||||
configPath := filepath.Join(dir, "config", "scert.json")
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
t.Fatalf("保存测试配置失败:%v", err)
|
||||
}
|
||||
@@ -275,7 +276,7 @@ func TestSettingsAPIRejectsInvalidRenewalPolicy(t *testing.T) {
|
||||
cfg.Admin.Username = "admin"
|
||||
cfg.Admin.Email = "admin@example.test"
|
||||
cfg.Admin.PasswordHash = mustTestPasswordHash(t, "Password123")
|
||||
configPath := dir + "\\config\\scert.json"
|
||||
configPath := filepath.Join(dir, "config", "scert.json")
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
t.Fatalf("保存测试配置失败:%v", err)
|
||||
}
|
||||
@@ -316,7 +317,7 @@ func TestSettingsAPIReturnsChineseNotificationCredentialError(t *testing.T) {
|
||||
cfg.Admin.Username = "admin"
|
||||
cfg.Admin.Email = "admin@example.test"
|
||||
cfg.Admin.PasswordHash = mustTestPasswordHash(t, "Password123")
|
||||
configPath := dir + "\\config\\scert.json"
|
||||
configPath := filepath.Join(dir, "config", "scert.json")
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
t.Fatalf("保存测试配置失败:%v", err)
|
||||
}
|
||||
@@ -430,7 +431,7 @@ func TestSettingsAPIRollsBackCredentialsWhenValidationFails(t *testing.T) {
|
||||
cfg.Admin.Username = "admin"
|
||||
cfg.Admin.Email = "admin@example.test"
|
||||
cfg.Admin.PasswordHash = mustTestPasswordHash(t, "Password123")
|
||||
configPath := dir + "\\config\\scert.json"
|
||||
configPath := filepath.Join(dir, "config", "scert.json")
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
t.Fatalf("save test config: %v", err)
|
||||
}
|
||||
@@ -468,7 +469,7 @@ func TestSettingsAPICleansReplacedNotificationCredential(t *testing.T) {
|
||||
cfg.Admin.PasswordHash = mustTestPasswordHash(t, "Password123")
|
||||
cfg.Notification.HTTPAuthMode = "bearer"
|
||||
cfg.Notification.HTTPCredentialRef = "secret://notification/old"
|
||||
configPath := dir + "\\config\\scert.json"
|
||||
configPath := filepath.Join(dir, "config", "scert.json")
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
t.Fatalf("save test config: %v", err)
|
||||
}
|
||||
@@ -512,7 +513,7 @@ func TestSettingsAPIRollsBackEarlierCredentialWhenLaterCredentialFails(t *testin
|
||||
cfg.Admin.Email = "admin@example.test"
|
||||
cfg.Admin.PasswordHash = mustTestPasswordHash(t, "Password123")
|
||||
cfg.Notification.SMTPUsername = "smtp-user"
|
||||
configPath := dir + "\\config\\scert.json"
|
||||
configPath := filepath.Join(dir, "config", "scert.json")
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
t.Fatalf("save test config: %v", err)
|
||||
}
|
||||
@@ -552,7 +553,7 @@ func TestSettingsAPIDNSRequiresCompleteVerification(t *testing.T) {
|
||||
cfg.Admin.Username = "admin"
|
||||
cfg.Admin.Email = "admin@example.test"
|
||||
cfg.Admin.PasswordHash = mustTestPasswordHash(t, "Password123")
|
||||
configPath := dir + "\\config\\scert.json"
|
||||
configPath := filepath.Join(dir, "config", "scert.json")
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
t.Fatalf("保存测试配置失败:%v", err)
|
||||
}
|
||||
@@ -588,7 +589,7 @@ func TestSettingsAPIDNSProviderChangeRequiresReverification(t *testing.T) {
|
||||
cfg.Admin.Username = "admin"
|
||||
cfg.Admin.Email = "admin@example.test"
|
||||
cfg.Admin.PasswordHash = mustTestPasswordHash(t, "Password123")
|
||||
configPath := dir + "\\config\\scert.json"
|
||||
configPath := filepath.Join(dir, "config", "scert.json")
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
t.Fatalf("保存测试配置失败:%v", err)
|
||||
}
|
||||
@@ -621,7 +622,7 @@ func TestSettingsAPISavesVerifiedDNSCredentialWithoutReturningSecret(t *testing.
|
||||
cfg.Admin.Username = "admin"
|
||||
cfg.Admin.Email = "admin@example.test"
|
||||
cfg.Admin.PasswordHash = mustTestPasswordHash(t, "Password123")
|
||||
configPath := dir + "\\config\\scert.json"
|
||||
configPath := filepath.Join(dir, "config", "scert.json")
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
t.Fatalf("保存测试配置失败:%v", err)
|
||||
}
|
||||
@@ -686,7 +687,7 @@ func TestSettingsAPIDNSCheckFailureRollsBackCredential(t *testing.T) {
|
||||
cfg.Admin.Username = "admin"
|
||||
cfg.Admin.Email = "admin@example.test"
|
||||
cfg.Admin.PasswordHash = mustTestPasswordHash(t, "Password123")
|
||||
configPath := dir + "\\config\\scert.json"
|
||||
configPath := filepath.Join(dir, "config", "scert.json")
|
||||
if err := cfg.Save(configPath); err != nil {
|
||||
t.Fatalf("保存测试配置失败:%v", err)
|
||||
}
|
||||
@@ -732,11 +733,11 @@ func TestSettingsAPIDNSSaveFailureRollsBackCredential(t *testing.T) {
|
||||
cfg.Admin.Username = "admin"
|
||||
cfg.Admin.Email = "admin@example.test"
|
||||
cfg.Admin.PasswordHash = mustTestPasswordHash(t, "Password123")
|
||||
initialConfigPath := dir + "\\config\\initial.json"
|
||||
initialConfigPath := filepath.Join(dir, "config", "initial.json")
|
||||
if err := cfg.Save(initialConfigPath); err != nil {
|
||||
t.Fatalf("保存测试配置失败:%v", err)
|
||||
}
|
||||
blockedPath := dir + "\\blocked"
|
||||
blockedPath := filepath.Join(dir, "blocked")
|
||||
if err := os.WriteFile(blockedPath, []byte("not a directory"), 0o600); err != nil {
|
||||
t.Fatalf("创建保存失败测试文件失败:%v", err)
|
||||
}
|
||||
@@ -747,7 +748,7 @@ func TestSettingsAPIDNSSaveFailureRollsBackCredential(t *testing.T) {
|
||||
secrets := &fakeSetupSecretWriter{values: map[string]map[string]string{
|
||||
newCredentialRef: {"api_token": "new-token"},
|
||||
}}
|
||||
server := New(Dependencies{Config: cfg, ConfigPath: blockedPath + "\\scert.json", SetupDNS: verifier, Secrets: secrets})
|
||||
server := New(Dependencies{Config: cfg, ConfigPath: filepath.Join(blockedPath, "scert.json"), SetupDNS: verifier, Secrets: secrets})
|
||||
cookie := loginForSettingsTest(t, server)
|
||||
|
||||
request := httptest.NewRequest(http.MethodPut, "/api/v1/settings", strings.NewReader(`{
|
||||
|
||||
@@ -3,6 +3,7 @@ package httpapi
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/repository"
|
||||
@@ -10,6 +11,13 @@ import (
|
||||
|
||||
func readyTestConfig(directory string) config.Config {
|
||||
cfg := config.Default(directory)
|
||||
// Linux 默认目录属于正式安装,测试必须显式隔离所有可写路径。
|
||||
cfg.Paths = config.PathsConfig{
|
||||
Install: directory, Config: filepath.Join(directory, "conf"),
|
||||
Data: filepath.Join(directory, "data"), Log: filepath.Join(directory, "logs"),
|
||||
Certificate: filepath.Join(directory, "ssl"), Backup: filepath.Join(directory, "backups"),
|
||||
}
|
||||
cfg.Database.DSN = filepath.Join(cfg.Paths.Data, "scert.db")
|
||||
cfg.SetupReady = true
|
||||
return cfg
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/database"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/repository"
|
||||
)
|
||||
|
||||
func TestReadyTestConfigIsolatesRuntimePaths(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
cfg := readyTestConfig(root)
|
||||
for name, path := range map[string]string{
|
||||
"install": cfg.Paths.Install, "config": cfg.Paths.Config,
|
||||
"data": cfg.Paths.Data, "log": cfg.Paths.Log,
|
||||
"certificate": cfg.Paths.Certificate, "backup": cfg.Paths.Backup,
|
||||
"database": cfg.Database.DSN,
|
||||
} {
|
||||
relative, err := filepath.Rel(root, path)
|
||||
if err != nil || !filepath.IsLocal(relative) {
|
||||
t.Fatalf("测试路径越过临时目录:%s=%s,错误=%v", name, path, err)
|
||||
}
|
||||
}
|
||||
if !cfg.SetupReady || cfg.Database.Driver != "sqlite" {
|
||||
t.Fatal("隔离配置必须保留已初始化状态和 SQLite 数据库")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadyTestConfigCreatesIndependentDatabases(t *testing.T) {
|
||||
for range 2 {
|
||||
cfg := readyTestConfig(t.TempDir())
|
||||
db, err := database.Open(t.Context(), cfg.Database)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
if err := database.Migrate(t.Context(), db.DB, cfg.Database.Driver); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var sequence int
|
||||
var name, path string
|
||||
if err := db.DB.QueryRowContext(t.Context(), "PRAGMA database_list").Scan(&sequence, &name, &path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if filepath.Clean(path) != filepath.Clean(cfg.Database.DSN) {
|
||||
t.Fatalf("测试实际连接了错误的数据库:%s", path)
|
||||
}
|
||||
tasks := repository.NewSQLTaskRepository(db.DB)
|
||||
items, err := tasks.ListTasks(t.Context(), "", 10)
|
||||
if err != nil || len(items) != 0 {
|
||||
t.Fatalf("新测试数据库不为空:%+v,错误=%v", items, err)
|
||||
}
|
||||
if err := tasks.CreateTask(t.Context(), repository.Task{
|
||||
ID: "isolation-task", DomainID: "management-certificate",
|
||||
TaskType: "management_renewal", Status: "running", Stage: "preflight",
|
||||
}); err != nil {
|
||||
t.Fatalf("测试数据库被其他用例复用或不可写:%v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user