补齐MySQL独立发行程序备份恢复实机验收

This commit is contained in:
xuans committed 2026-10-01 18:21:34 +08:00
1 parent 7da903494f
commit be6e2059f4
6 files changed
+100

No files matched your search

@@ -19,6 +19,10 @@ import (
// 在同一进程调用正式命令;测试 TLS 注册名不能用于独立发行二进制验收。
func TestMySQLRealBackupRestoreCommands(t *testing.T) {
testMySQLBackupRestoreCommands(t, "")
}
func testMySQLBackupRestoreCommands(t *testing.T, binary string) {
server := mysqltest.New(t)
for _, scenario := range []string{"success", "sql_failure", "certificate_failure"} {
t.Run(scenario, func(t *testing.T) {
@@ -52,6 +56,9 @@ func TestMySQLRealBackupRestoreCommands(t *testing.T) {
}
username, password := parsed.User, parsed.Passwd
parsed.User, parsed.Passwd = "", ""
if binary != "" {
parsed.TLSConfig = "true"
}
cfg.Database = connection
cfg.Database.DSN = parsed.FormatDSN()
cfg.Database.CredentialRef = "secret://database/maintenance"
@@ -90,6 +97,9 @@ func TestMySQLRealBackupRestoreCommands(t *testing.T) {
t.Cleanup(func() { configPath = previous })
run := func(command *cobra.Command, args ...string) (string, error) {
t.Helper()
if binary != "" {
return runMySQLReleaseCLI(t, binary, current, filepath.Join(server.Root(), "server.crt"), password, append([]string{command.Name()}, args...)...)
}
var output bytes.Buffer
command.SetArgs(args)
command.SetContext(t.Context())
@@ -0,0 +1,50 @@
package cli
import (
"context"
"errors"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)
// 独立发行程序不依赖测试进程的 TLS 注册表;仅向子进程提供隔离实例的信任证书。
func TestMySQLRealReleaseBackupRestoreCommands(t *testing.T) {
binary := os.Getenv("SCERT_MYSQL_TEST_CLI")
if binary == "" {
t.Skip("未启用 MySQL 独立发行程序验收")
}
if runtime.GOOS != "linux" {
t.Fatal("独立发行程序验收要求 Linux 系统证书加载语义")
}
info, err := os.Stat(binary)
if err != nil || !filepath.IsAbs(binary) || !info.Mode().IsRegular() || info.Mode().Perm()&0o111 == 0 {
t.Fatal("必须提供可执行的正式 Linux 发行程序绝对路径")
}
t.Logf("独立发行程序:%s;使用严格 TLS 和加密数据库凭据", binary)
testMySQLBackupRestoreCommands(t, binary)
}
func runMySQLReleaseCLI(t *testing.T, binary, configuration, certificate, password string, args ...string) (string, error) {
t.Helper()
ctx, cancel := context.WithTimeout(t.Context(), time.Minute)
defer cancel()
command := exec.CommandContext(ctx, binary, append([]string{"--config", configuration}, args...)...)
command.Env = append(os.Environ(), "SSL_CERT_FILE="+certificate, "SSL_CERT_DIR="+filepath.Dir(certificate))
command.WaitDelay = 2 * time.Second
output, err := command.CombinedOutput()
if strings.Contains(string(output), password) {
t.Fatal("独立发行程序输出不得泄漏数据库密码")
}
if err != nil {
if ctx.Err() != nil {
t.Fatalf("独立发行程序执行超时或取消:%v", ctx.Err())
}
return string(output), errors.New(string(output))
}
return string(output), nil
}