refactor: use nginx -s reload instead of systemd

- doc: add comment
This commit is contained in:
2026-08-17 10:10:14 +08:00
parent 79f18fcdea
commit f536987a7e
62 changed files with 2216 additions and 598 deletions
+39
View File
@@ -7,6 +7,7 @@ import (
"testing"
)
// validNativeConfig 一份满足原生后端部署契约的示例 TOML 配置,供各测试用例共享。
const validNativeConfig = `[daemon]
environment = "dev"
@@ -27,6 +28,7 @@ jar = "/home/yms/lib/glory-soft-yms-8081.jar"
health_endpoint = "http://127.0.0.1:8081/yms/actuator/health"
`
// validContainerConfig 一份满足容器后端部署契约的示例 TOML 配置,供各测试用例共享。
const validContainerConfig = `[daemon]
environment = "dev"
@@ -43,6 +45,8 @@ container_name = "backend-8081"
health_endpoint = "http://127.0.0.1:8081/yms/actuator/health"
`
// TestLoadValidNativeConfiguration 验证合法原生配置能被完整加载,
// 且解析出的 daemon、backend 与 8080/8081 槽位取值都与契约常量精确一致。
func TestLoadValidNativeConfiguration(t *testing.T) {
path := writeConfig(t, validNativeConfig)
@@ -72,6 +76,8 @@ func TestLoadValidNativeConfiguration(t *testing.T) {
}
}
// TestLoadValidContainerConfiguration 验证合法容器配置能被加载,
// 且两个槽位的 container_name 取值与契约精确一致。
func TestLoadValidContainerConfiguration(t *testing.T) {
config, err := Load(writeConfig(t, validContainerConfig))
if err != nil {
@@ -85,6 +91,8 @@ func TestLoadValidContainerConfiguration(t *testing.T) {
}
}
// TestLoadRejectsChangedContainerName 验证容器槽位的 container_name 被篡改时会被拒绝,
// 且错误信息精确指向 backend.slot.8081.container_name 字段。
func TestLoadRejectsChangedContainerName(t *testing.T) {
content := strings.Replace(validContainerConfig, `container_name = "backend-8081"`, `container_name = "backend"`, 1)
_, err := Load(writeConfig(t, content))
@@ -93,6 +101,8 @@ func TestLoadRejectsChangedContainerName(t *testing.T) {
}
}
// TestPackagedNativeConfigurationMatchesContract 验证随发行包打包的原生部署配置
// 与代码内置契约一致,确保打包产物不会被意外改坏。
func TestPackagedNativeConfigurationMatchesContract(t *testing.T) {
path, err := filepath.Abs(filepath.Join("..", "..", "packaging", "etc", "yms-daemon", "yms-daemon.toml"))
if err != nil {
@@ -103,6 +113,8 @@ func TestPackagedNativeConfigurationMatchesContract(t *testing.T) {
}
}
// TestLoadRejectsUnknownField 验证配置中出现的未知字段(大小写不一致)会被拒绝,
// 且错误信息精确包含该字段名 SystemctlPath。
func TestLoadRejectsUnknownField(t *testing.T) {
content := strings.Replace(validNativeConfig, "systemctl_path = \"/bin/systemctl\"", "systemctl_path = \"/bin/systemctl\"\nSystemctlPath = \"/usr/bin/systemctl\"", 1)
_, err := Load(writeConfig(t, content))
@@ -111,6 +123,8 @@ func TestLoadRejectsUnknownField(t *testing.T) {
}
}
// TestLoadAcceptsProductionEnvironment 验证 environment 取值为 prod 时能被接受,
// 并正确解析到 EnvironmentProd。
func TestLoadAcceptsProductionEnvironment(t *testing.T) {
content := strings.Replace(validNativeConfig, `environment = "dev"`, `environment = "prod"`, 1)
config, err := Load(writeConfig(t, content))
@@ -122,6 +136,8 @@ func TestLoadAcceptsProductionEnvironment(t *testing.T) {
}
}
// TestLoadRejectsMissingDaemonTable 验证缺失 daemon 顶层表时会被拒绝,
// 且错误信息提示 daemon 表是必需的。
func TestLoadRejectsMissingDaemonTable(t *testing.T) {
content := strings.Replace(validNativeConfig, "[daemon]\nenvironment = \"dev\"\n\n", "", 1)
_, err := Load(writeConfig(t, content))
@@ -130,6 +146,8 @@ func TestLoadRejectsMissingDaemonTable(t *testing.T) {
}
}
// TestLoadRejectsChangedEnvironment 验证 environment 被篡改为非法取值时会被拒绝,
// 且错误信息精确指向 daemon.environment 字段。
func TestLoadRejectsChangedEnvironment(t *testing.T) {
content := strings.Replace(validNativeConfig, `environment = "dev"`, `environment = "development"`, 1)
_, err := Load(writeConfig(t, content))
@@ -138,6 +156,8 @@ func TestLoadRejectsChangedEnvironment(t *testing.T) {
}
}
// TestLoadRejectsUnknownDaemonField 验证 daemon 表中出现的未知字段(大小写不一致)
// 会被拒绝,且错误信息精确包含该字段名 daemon.Environment。
func TestLoadRejectsUnknownDaemonField(t *testing.T) {
content := strings.Replace(validNativeConfig, `environment = "dev"`, "environment = \"dev\"\nEnvironment = \"dev\"", 1)
_, err := Load(writeConfig(t, content))
@@ -146,6 +166,8 @@ func TestLoadRejectsUnknownDaemonField(t *testing.T) {
}
}
// TestLoadRejectsWrongTableCase 验证表名大小写不一致([Backend])时会被拒绝,
// 且错误信息精确包含该表名 Backend。
func TestLoadRejectsWrongTableCase(t *testing.T) {
content := strings.Replace(validNativeConfig, "[backend]", "[Backend]", 1)
_, err := Load(writeConfig(t, content))
@@ -154,6 +176,8 @@ func TestLoadRejectsWrongTableCase(t *testing.T) {
}
}
// TestLoadRejectsMissingNativeField 验证原生配置缺失 active_jar 字段时会被拒绝,
// 且错误信息精确指向 backend.active_jar 字段。
func TestLoadRejectsMissingNativeField(t *testing.T) {
content := strings.Replace(validNativeConfig, "active_jar = \"/home/yms/lib/glory-soft-yms.jar\"\n", "", 1)
_, err := Load(writeConfig(t, content))
@@ -162,6 +186,8 @@ func TestLoadRejectsMissingNativeField(t *testing.T) {
}
}
// TestLoadRejectsUnknownSlot 验证配置中出现未受支持的槽位(9090)时会被拒绝,
// 且错误信息精确包含该端口号 9090。
func TestLoadRejectsUnknownSlot(t *testing.T) {
content := validNativeConfig + `
[backend.slot.9090]
@@ -175,6 +201,8 @@ health_endpoint = "http://127.0.0.1:9090/yms/actuator/health"
}
}
// TestLoadRejectsChangedSlotEndpoint 验证槽位健康端点被篡改时会被拒绝,
// 且错误信息精确指向 backend.slot.8080.health_endpoint 字段。
func TestLoadRejectsChangedSlotEndpoint(t *testing.T) {
content := strings.Replace(validNativeConfig, nativeHealthURL8080, nativeHealthURL8081, 1)
_, err := Load(writeConfig(t, content))
@@ -183,6 +211,8 @@ func TestLoadRejectsChangedSlotEndpoint(t *testing.T) {
}
}
// TestLoadAcceptsExplicitUsrBinSystemctlPath 验证显式指定的 /usr/bin/systemctl 路径
// 能被接受并原样解析,说明 systemctl_path 只要求绝对路径而不绑定单一取值。
func TestLoadAcceptsExplicitUsrBinSystemctlPath(t *testing.T) {
content := strings.Replace(validNativeConfig, "/bin/systemctl", "/usr/bin/systemctl", 1)
config, err := Load(writeConfig(t, content))
@@ -194,6 +224,8 @@ func TestLoadAcceptsExplicitUsrBinSystemctlPath(t *testing.T) {
}
}
// TestLoadRejectsChangedBackendTypeCase 验证 backend.type 大小写不一致(Native)时会被拒绝,
// 且错误信息精确指向 backend.type 字段。
func TestLoadRejectsChangedBackendTypeCase(t *testing.T) {
content := strings.Replace(validNativeConfig, `type = "native"`, `type = "Native"`, 1)
_, err := Load(writeConfig(t, content))
@@ -202,6 +234,8 @@ func TestLoadRejectsChangedBackendTypeCase(t *testing.T) {
}
}
// TestLoadRejectsDuplicateField 验证 TOML 中重复出现的字段会被拒绝,
// 确保配置没有歧义。
func TestLoadRejectsDuplicateField(t *testing.T) {
content := strings.Replace(validNativeConfig, "type = \"native\"", "type = \"native\"\ntype = \"native\"", 1)
_, err := Load(writeConfig(t, content))
@@ -210,6 +244,8 @@ func TestLoadRejectsDuplicateField(t *testing.T) {
}
}
// TestLoadRequiresAbsoluteConfigurationPath 验证 Load 要求传入绝对路径,
// 相对路径会被拒绝且错误信息提示 absolute path。
func TestLoadRequiresAbsoluteConfigurationPath(t *testing.T) {
_, err := Load("yms-daemon.toml")
if err == nil || !strings.Contains(err.Error(), "absolute path") {
@@ -217,6 +253,7 @@ func TestLoadRequiresAbsoluteConfigurationPath(t *testing.T) {
}
}
// TestSlotForPortRejectsUnsupportedPort 验证 SlotForPort 对未受支持端口返回错误。
func TestSlotForPortRejectsUnsupportedPort(t *testing.T) {
config, err := Load(writeConfig(t, validNativeConfig))
if err != nil {
@@ -227,6 +264,8 @@ func TestSlotForPortRejectsUnsupportedPort(t *testing.T) {
}
}
// writeConfig 将 content 写入临时目录下的 yms-daemon.toml 并返回其绝对路径,
// 供 Load 测试使用。写入失败会直接终止测试。
func writeConfig(t *testing.T, content string) string {
t.Helper()
path := filepath.Join(t.TempDir(), "yms-daemon.toml")