原生版以非 root 的 havline 用户运行,而 app.New 会无条件调用 updater.EnsureToken, 去写默认的 /run/havline-updater/updater.token:MkdirAll 因权限不足失败 → app.New 返回错误 → main 里 log.Fatalf → 进程秒退,飞牛应用中心报「本地应用启动失败」。 该逻辑由 v1.9.2 引入,v1.9.1 及之前不受影响。 - 新增 updater.EnsureTokenIfEnabled:updater 未启用(socket 为空)时直接返回, 完全不触碰 Token 文件 - app.New 改用该函数,只有真正启用一键升级 sidecar 时才准备共享 Token - 补充回归测试:socket 为空不创建文件、socket 非空照旧创建
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package updater
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -105,3 +106,36 @@ func TestWaitForTokenTimesOut(t *testing.T) {
|
||||
t.Fatal("文件不存在时应等待超时")
|
||||
}
|
||||
}
|
||||
|
||||
// updater 未启用(socket 为空)时不能去碰 Token 文件:
|
||||
// 原生版以非 root 运行,写 /run/havline-updater 会失败并拖垮整个启动。
|
||||
func TestEnsureTokenIfEnabledSkipsWhenSocketDisabled(t *testing.T) {
|
||||
tokenFile := filepath.Join(t.TempDir(), "nested", "updater.token")
|
||||
|
||||
token, source, err := EnsureTokenIfEnabled("", "", tokenFile)
|
||||
if err != nil {
|
||||
t.Fatalf("socket 未启用时不应报错:%v", err)
|
||||
}
|
||||
if token != "" || source != "" {
|
||||
t.Fatalf("socket 未启用时应返回空 Token,实际 token=%q source=%q", token, source)
|
||||
}
|
||||
if _, statErr := os.Stat(tokenFile); !errors.Is(statErr, os.ErrNotExist) {
|
||||
t.Fatalf("socket 未启用时不应创建 Token 文件(statErr=%v)", statErr)
|
||||
}
|
||||
}
|
||||
|
||||
// 对照组:updater 启用(socket 非空)时仍照旧生成并落盘。
|
||||
func TestEnsureTokenIfEnabledCreatesWhenSocketSet(t *testing.T) {
|
||||
tokenFile := filepath.Join(t.TempDir(), "nested", "updater.token")
|
||||
|
||||
token, source, err := EnsureTokenIfEnabled("/run/havline-updater/updater.sock", "", tokenFile)
|
||||
if err != nil {
|
||||
t.Fatalf("socket 启用时应生成 Token:%v", err)
|
||||
}
|
||||
if len(token) != 64 || source != tokenFile {
|
||||
t.Fatalf("生成的 Token 不正确,实际 token=%q source=%q", token, source)
|
||||
}
|
||||
if _, statErr := os.Stat(tokenFile); statErr != nil {
|
||||
t.Fatalf("socket 启用时应写入 Token 文件:%v", statErr)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user