保存核心服务启动修复及初始化联动回归检查点
This commit is contained in:
1 parent
7d374dc10d
commit
16428f05b3
8 files changed
+87
-19
No files matched your search
@@ -22,11 +22,19 @@ import (
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/app"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/firewall"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/secretstore"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/setup"
|
||||
)
|
||||
|
||||
func TestHTTPSBootstrapCompletesSQLiteAndStartsApplication(t *testing.T) {
|
||||
for _, mode := range []string{"automatic-fixture", "external"} {
|
||||
t.Run(mode, func(t *testing.T) { testHTTPSBootstrapLifecycle(t, mode) })
|
||||
}
|
||||
}
|
||||
|
||||
func testHTTPSBootstrapLifecycle(t *testing.T, mode string) {
|
||||
t.Helper()
|
||||
fixture := newCompletionFixture(t, "authorized.example.test")
|
||||
store, err := secretstore.Open(fixture.base.Paths.Config)
|
||||
if err != nil {
|
||||
@@ -38,10 +46,17 @@ func TestHTTPSBootstrapCompletesSQLiteAndStartsApplication(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var firewallManager firewall.Manager = fixture.firewall
|
||||
if mode == "external" {
|
||||
firewallManager, err = firewall.NewManagerWithMode(mode)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
completer := NewCompleter(CompleteDependencies{
|
||||
BaseConfig: fixture.base, ConfigPath: fixture.configPath,
|
||||
DatabaseSessions: databases, DNSSessions: dns, Credentials: store,
|
||||
Sessions: sessions, Firewall: fixture.firewall, Certificate: fixture.issuer,
|
||||
Sessions: sessions, Firewall: firewallManager, Certificate: fixture.issuer,
|
||||
})
|
||||
router := NewRouter(sessions, databases, dns, completer)
|
||||
handler, closeFiles := bootstrapHandler(router, fixture.base)
|
||||
@@ -120,6 +135,9 @@ func TestHTTPSBootstrapCompletesSQLiteAndStartsApplication(t *testing.T) {
|
||||
if !completed.Completed || completed.ManagementURL != "https://"+net.JoinHostPort(input.WebPublicIP, strconv.Itoa(input.ServerPort))+"/" {
|
||||
t.Fatal("初始化未返回正确管理地址")
|
||||
}
|
||||
if mode == "external" && (len(completed.Warnings) != 1 || !strings.Contains(completed.Warnings[0], "未检查或修改")) {
|
||||
t.Fatal("真实 HTTPS 完成响应缺少外部防火墙提示")
|
||||
}
|
||||
select {
|
||||
case err := <-result:
|
||||
if err != nil {
|
||||
|
||||
@@ -684,12 +684,7 @@ func newServeCommand() *cobra.Command {
|
||||
if !cmd.Flags().Changed("bootstrap-public-ip") {
|
||||
advertisedIP = os.Getenv("SCERT_BOOTSTRAP_PUBLIC_IP")
|
||||
}
|
||||
selectedFirewallMode := firewallMode
|
||||
if !cmd.Flags().Changed("bootstrap-firewall-mode") {
|
||||
if value, ok := os.LookupEnv("SCERT_BOOTSTRAP_FIREWALL_MODE"); ok {
|
||||
selectedFirewallMode = value
|
||||
}
|
||||
}
|
||||
selectedFirewallMode := bootstrapFirewallMode(cmd, firewallMode)
|
||||
return serveLifecycle(ctx, cfg, reload,
|
||||
func(ctx context.Context, initial config.Config) (bool, error) {
|
||||
return bootstrap.Run(ctx, bootstrap.ServerOptions{
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/app"
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/config"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type serveStage func(context.Context, config.Config) (bool, error)
|
||||
@@ -61,13 +62,20 @@ func serveLifecycle(ctx context.Context, cfg config.Config, reload func() (confi
|
||||
if ctx.Err() != nil {
|
||||
return nil
|
||||
}
|
||||
if !cfg.Web.Enabled {
|
||||
return errors.New("管理页面已关闭")
|
||||
}
|
||||
// 关闭 Web 静态页面不等于停用核心 API、桌面管理和后台续期服务。
|
||||
_, err := applicationStage(ctx, cfg)
|
||||
return err
|
||||
}
|
||||
|
||||
func bootstrapFirewallMode(command *cobra.Command, value string) string {
|
||||
if !command.Flags().Changed("bootstrap-firewall-mode") {
|
||||
if environment, ok := os.LookupEnv("SCERT_BOOTSTRAP_FIREWALL_MODE"); ok {
|
||||
return environment
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func runApplication(ctx context.Context, cfg config.Config, path string) (bool, error) {
|
||||
service, err := app.NewWithConfigPath(ctx, cfg, path)
|
||||
if err != nil {
|
||||
|
||||
@@ -38,7 +38,7 @@ func TestLoadServeConfigFailsClosed(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestServeLifecycle(t *testing.T) {
|
||||
for _, name := range []string{"missing", "ready", "cancel", "bootstrap-error", "reload-error", "not-ready", "path-changed", "web-disabled", "application-error"} {
|
||||
for _, name := range []string{"missing", "ready", "cancel", "bootstrap-error", "reload-error", "not-ready", "path-changed", "web-disabled", "bootstrap-web-disabled", "application-error"} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
cfg := config.Default(t.TempDir())
|
||||
cfg.SetupReady = name == "ready" || name == "web-disabled" || name == "application-error"
|
||||
@@ -53,6 +53,9 @@ func TestServeLifecycle(t *testing.T) {
|
||||
}
|
||||
next := cfg
|
||||
next.SetupReady = name != "not-ready"
|
||||
if name == "bootstrap-web-disabled" {
|
||||
next.Web.Enabled = false
|
||||
}
|
||||
if name == "path-changed" {
|
||||
next.Paths.Data += "-changed"
|
||||
}
|
||||
@@ -72,17 +75,20 @@ func TestServeLifecycle(t *testing.T) {
|
||||
if !ready.SetupReady {
|
||||
t.Fatal("未初始化时构造业务应用")
|
||||
}
|
||||
if (name == "web-disabled" || name == "bootstrap-web-disabled") && ready.Web.Enabled {
|
||||
t.Fatal("关闭静态页面的配置不能被启动流程覆盖")
|
||||
}
|
||||
if name == "application-error" {
|
||||
return false, errors.New("test")
|
||||
}
|
||||
return false, nil
|
||||
})
|
||||
wantError := name != "missing" && name != "ready" && name != "cancel"
|
||||
wantError := name != "missing" && name != "ready" && name != "cancel" && name != "web-disabled" && name != "bootstrap-web-disabled"
|
||||
if (err != nil) != wantError {
|
||||
t.Fatalf("错误边界不符:%v", err)
|
||||
}
|
||||
wantApp := 0
|
||||
if name == "missing" || name == "ready" || name == "application-error" {
|
||||
if name == "missing" || name == "ready" || name == "application-error" || name == "web-disabled" || name == "bootstrap-web-disabled" {
|
||||
wantApp = 1
|
||||
}
|
||||
if appCalls != wantApp {
|
||||
@@ -97,3 +103,34 @@ func TestServeLifecycle(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootstrapFirewallModeFlagOverridesEnvironment(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
environment string
|
||||
flag string
|
||||
want string
|
||||
}{
|
||||
{name: "environment", environment: "external", want: "external"},
|
||||
{name: "flag", environment: "external", flag: "auto", want: "auto"},
|
||||
{name: "external-flag", environment: "auto", flag: "external", want: "external"},
|
||||
{name: "invalid-environment", environment: "invalid", want: "invalid"},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Setenv("SCERT_BOOTSTRAP_FIREWALL_MODE", test.environment)
|
||||
command := newServeCommand()
|
||||
if test.flag != "" {
|
||||
if err := command.ParseFlags([]string{"--bootstrap-firewall-mode", test.flag}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
value, err := command.Flags().GetString("bootstrap-firewall-mode")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := bootstrapFirewallMode(command, value); got != test.want {
|
||||
t.Fatalf("本机策略优先级错误:得到 %q,期望 %q", got, test.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -70,4 +70,13 @@ func TestFrontendRespectsWebEnabled(t *testing.T) {
|
||||
if response.Code != http.StatusNotFound {
|
||||
t.Fatalf("关闭 Web 后根路径应返回 404:%d", response.Code)
|
||||
}
|
||||
for path, expected := range map[string]int{
|
||||
"/healthz": http.StatusOK, "/api/v1/auth/session": http.StatusUnauthorized,
|
||||
} {
|
||||
response := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(response, httptest.NewRequest(http.MethodGet, path, nil))
|
||||
if response.Code != expected {
|
||||
t.Fatalf("关闭静态页面不能关闭核心 API:%s 状态 %d", path, response.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user