refactor: use nginx -s reload instead of systemd
- doc: add comment
This commit is contained in:
@@ -9,10 +9,12 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestControllerSwitchesAndReloadsExactService(t *testing.T) {
|
||||
// TestControllerSwitchesAndReloadsNginx 验证 Switch 能将活动端口切到 8080,
|
||||
// 且实际执行了 nginx -t 与 nginx -s reload 命令。
|
||||
func TestControllerSwitchesAndReloadsNginx(t *testing.T) {
|
||||
configPath := writeNginxConfig(t, serverConfiguration)
|
||||
runner := &recordingRunner{}
|
||||
controller, err := newController(configPath, "/usr/sbin/nginx", "/bin/systemctl", "nginx.service", runner)
|
||||
controller, err := newController(configPath, "/usr/sbin/nginx", runner)
|
||||
if err != nil {
|
||||
t.Fatalf("create host Nginx controller: %v", err)
|
||||
}
|
||||
@@ -30,17 +32,19 @@ func TestControllerSwitchesAndReloadsExactService(t *testing.T) {
|
||||
}
|
||||
wantCalls := [][]string{
|
||||
{"/usr/sbin/nginx", "-t"},
|
||||
{"/bin/systemctl", "reload", "--", "nginx.service"},
|
||||
{"/usr/sbin/nginx", "-s", "reload"},
|
||||
}
|
||||
if !slices.EqualFunc(runner.calls, wantCalls, slices.Equal) {
|
||||
t.Fatalf("unexpected host Nginx commands: %+v", runner.calls)
|
||||
}
|
||||
}
|
||||
|
||||
// TestControllerRestoresConfigurationWhenValidationFails 验证 nginx -t 校验失败时 Switch 返回错误,
|
||||
// 并把配置回滚为原始内容,同时执行了校验、回滚校验与回滚重载三次命令。
|
||||
func TestControllerRestoresConfigurationWhenValidationFails(t *testing.T) {
|
||||
configPath := writeNginxConfig(t, serverConfiguration)
|
||||
runner := &recordingRunner{errors: []error{errors.New("nginx test failed"), nil, nil}}
|
||||
controller, err := newController(configPath, "/usr/sbin/nginx", "/bin/systemctl", "nginx.service", runner)
|
||||
controller, err := newController(configPath, "/usr/sbin/nginx", runner)
|
||||
if err != nil {
|
||||
t.Fatalf("create host Nginx controller: %v", err)
|
||||
}
|
||||
@@ -57,10 +61,12 @@ func TestControllerRestoresConfigurationWhenValidationFails(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestControllerRestoreUsesCompleteSnapshot 验证 Restore 能使用 Switch 返回的完整快照
|
||||
// 把配置恢复到切换前的原始内容与活动端口。
|
||||
func TestControllerRestoreUsesCompleteSnapshot(t *testing.T) {
|
||||
configPath := writeNginxConfig(t, serverConfiguration)
|
||||
runner := &recordingRunner{}
|
||||
controller, err := newController(configPath, "/usr/sbin/nginx", "/bin/systemctl", "nginx.service", runner)
|
||||
controller, err := newController(configPath, "/usr/sbin/nginx", runner)
|
||||
if err != nil {
|
||||
t.Fatalf("create host Nginx controller: %v", err)
|
||||
}
|
||||
@@ -77,6 +83,8 @@ func TestControllerRestoreUsesCompleteSnapshot(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// writeNginxConfig 将 content 写入临时目录下的 nginx.conf 并返回其绝对路径,
|
||||
// 供 Controller 测试使用。写入失败会直接终止测试。
|
||||
func writeNginxConfig(t *testing.T, content string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "nginx.conf")
|
||||
@@ -86,11 +94,15 @@ func writeNginxConfig(t *testing.T, content string) string {
|
||||
return path
|
||||
}
|
||||
|
||||
// recordingRunner commandRunner 的测试实现,记录每次调用并依次返回预设的错误。
|
||||
type recordingRunner struct {
|
||||
calls [][]string
|
||||
// calls 记录每次执行的命令(含参数)序列。
|
||||
calls [][]string
|
||||
// errors 依次返回的预设错误,取完后续调用返回 nil。
|
||||
errors []error
|
||||
}
|
||||
|
||||
// Run 记录本次调用命令,并按 errors 中的顺序返回下一个预设错误。
|
||||
func (r *recordingRunner) Run(_ context.Context, executable string, arguments ...string) error {
|
||||
call := append([]string{executable}, arguments...)
|
||||
r.calls = append(r.calls, call)
|
||||
|
||||
Reference in New Issue
Block a user