Files
mc-gateway/cmd/gateway/log_pid_test.go
tursom 935fe4b808
Some checks failed
Go / build (.exe, 386, windows, windows-386) (push) Has been cancelled
Go / build (.exe, amd64, windows, windows-amd64) (push) Has been cancelled
Go / build (.exe, arm64, windows, windows-arm64) (push) Has been cancelled
Go / build (386, freebsd, freebsd-386) (push) Has been cancelled
Go / build (386, linux, linux-386) (push) Has been cancelled
Go / build (386, netbsd, netbsd-386) (push) Has been cancelled
Go / build (386, openbsd, openbsd-386) (push) Has been cancelled
Go / build (386, plan9, plan9-386) (push) Has been cancelled
Go / build (amd64, darwin, darwin-amd64) (push) Has been cancelled
Go / build (amd64, dragonfly, dragonfly-amd64) (push) Has been cancelled
Go / build (amd64, freebsd, freebsd-amd64) (push) Has been cancelled
Go / build (amd64, illumos, illumos-amd64) (push) Has been cancelled
Go / build (amd64, linux, linux-amd64) (push) Has been cancelled
Go / build (amd64, netbsd, netbsd-amd64) (push) Has been cancelled
Go / build (amd64, openbsd, openbsd-amd64) (push) Has been cancelled
Go / build (amd64, plan9, plan9-amd64) (push) Has been cancelled
Go / build (amd64, solaris, solaris-amd64) (push) Has been cancelled
Go / build (arm, 6, linux, linux-armv6) (push) Has been cancelled
Go / build (arm, 7, linux, linux-armv7) (push) Has been cancelled
Go / build (arm, freebsd, freebsd-arm) (push) Has been cancelled
Go / build (arm, netbsd, netbsd-arm) (push) Has been cancelled
Go / build (arm, openbsd, openbsd-arm) (push) Has been cancelled
Go / build (arm, plan9, plan9-arm) (push) Has been cancelled
Go / build (arm64, darwin, darwin-arm64) (push) Has been cancelled
Go / build (arm64, freebsd, freebsd-arm64) (push) Has been cancelled
Go / build (arm64, linux, linux-arm64) (push) Has been cancelled
Go / build (arm64, netbsd, netbsd-arm64) (push) Has been cancelled
Go / build (arm64, openbsd, openbsd-arm64) (push) Has been cancelled
Go / build (loong64, linux, linux-loong64) (push) Has been cancelled
Go / build (mips, linux, linux-mips) (push) Has been cancelled
Go / build (mips64, linux, linux-mips64) (push) Has been cancelled
Go / build (mips64le, linux, linux-mips64le) (push) Has been cancelled
Go / build (mipsle, linux, linux-mipsle) (push) Has been cancelled
Go / build (ppc64, aix, aix-ppc64) (push) Has been cancelled
Go / build (ppc64, linux, linux-ppc64) (push) Has been cancelled
Go / build (ppc64, openbsd, openbsd-ppc64) (push) Has been cancelled
Go / build (ppc64le, linux, linux-ppc64le) (push) Has been cancelled
Go / build (riscv64, freebsd, freebsd-riscv64) (push) Has been cancelled
Go / build (riscv64, linux, linux-riscv64) (push) Has been cancelled
Go / build (riscv64, openbsd, openbsd-riscv64) (push) Has been cancelled
Go / build (s390x, linux, linux-s390x) (push) Has been cancelled
Go / merge-artifacts (push) Has been cancelled
test: 补全单元测试
2026-06-23 13:06:31 +08:00

138 lines
3.5 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func TestWritePIDFileCreatesAndSwitchesFiles(t *testing.T) {
defer saveGatewayState(t)()
tmpDir := t.TempDir()
firstPID := filepath.Join(tmpDir, "first.pid")
secondPID := filepath.Join(tmpDir, "second.pid")
config.PidFile = firstPID
if err := writePIDFile(); err != nil {
t.Fatalf("writePIDFile(first) error = %v", err)
}
assertPIDFile(t, firstPID)
config.PidFile = secondPID
if err := writePIDFile(); err != nil {
t.Fatalf("writePIDFile(second) error = %v", err)
}
if _, err := os.Stat(firstPID); !os.IsNotExist(err) {
t.Fatalf("first pid file still exists or stat failed: %v", err)
}
assertPIDFile(t, secondPID)
removePIDFile()
if _, err := os.Stat(secondPID); !os.IsNotExist(err) {
t.Fatalf("second pid file still exists or stat failed: %v", err)
}
}
func TestWritePIDFileNoopsWhenPathUnchanged(t *testing.T) {
defer saveGatewayState(t)()
pidPath := filepath.Join(t.TempDir(), "gateway.pid")
config.PidFile = pidPath
if err := writePIDFile(); err != nil {
t.Fatalf("writePIDFile() error = %v", err)
}
firstStat, err := os.Stat(pidPath)
if err != nil {
t.Fatalf("Stat(first) error = %v", err)
}
if err := writePIDFile(); err != nil {
t.Fatalf("writePIDFile() second error = %v", err)
}
secondStat, err := os.Stat(pidPath)
if err != nil {
t.Fatalf("Stat(second) error = %v", err)
}
if !firstStat.ModTime().Equal(secondStat.ModTime()) {
t.Fatalf("pid file mod time changed: %v -> %v", firstStat.ModTime(), secondStat.ModTime())
}
}
func TestLoadLoggerDefaultLevelAndInvalidLevel(t *testing.T) {
defer saveGatewayState(t)()
if err := loadLogger(); err != nil {
t.Fatalf("loadLogger() error = %v", err)
}
if config.Log.Level != "info" {
t.Fatalf("default log level = %q, want info", config.Log.Level)
}
if got := log.Logger.GetLevel(); got != zerolog.InfoLevel {
t.Fatalf("logger level = %v, want %v", got, zerolog.InfoLevel)
}
config.Log.Level = "not-a-level"
if err := loadLogger(); err == nil {
t.Fatal("loadLogger() error = nil, want invalid level error")
}
}
func TestLoadLoggerCreatesLogFile(t *testing.T) {
defer saveGatewayState(t)()
logPath := filepath.Join(t.TempDir(), "nested", "gateway.log")
config.Log.Level = "debug"
config.Log.File = logPath
if err := loadLogger(); err != nil {
t.Fatalf("loadLogger() error = %v", err)
}
if currentLogFile != logPath {
t.Fatalf("currentLogFile = %q, want %q", currentLogFile, logPath)
}
if _, err := os.Stat(logPath); err != nil {
t.Fatalf("Stat(log file) error = %v", err)
}
if got := log.Logger.GetLevel(); got != zerolog.DebugLevel {
t.Fatalf("logger level = %v, want %v", got, zerolog.DebugLevel)
}
}
func TestReopenLogFile(t *testing.T) {
defer saveGatewayState(t)()
if err := reopenLogFile(); err != nil {
t.Fatalf("reopenLogFile(empty) error = %v", err)
}
logPath := filepath.Join(t.TempDir(), "gateway.log")
config.Log.File = logPath
if err := reopenLogFile(); err != nil {
t.Fatalf("reopenLogFile() error = %v", err)
}
if currentLogFile != logPath {
t.Fatalf("currentLogFile = %q, want %q", currentLogFile, logPath)
}
if _, err := os.Stat(logPath); err != nil {
t.Fatalf("Stat(log file) error = %v", err)
}
}
func assertPIDFile(t *testing.T, path string) {
t.Helper()
got, err := os.ReadFile(path)
if err != nil {
t.Fatalf("ReadFile(%s) error = %v", path, err)
}
want := fmt.Sprintf("%d\n", os.Getpid())
if string(got) != want {
t.Fatalf("pid file %s = %q, want %q", path, string(got), want)
}
}