跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

pidfile_test.go

141 行 · 4.6 KB历史下载
1// SPDX-FileCopyrightText: Copyright The Lima Authors
2// SPDX-License-Identifier: Apache-2.0
3
4package store
5
6import (
7 "errors"
8 "fmt"
9 "os"
10 "path/filepath"
11 "testing"
12
13 "gotest.tools/v3/assert"
14
15 "github.com/lima-vm/lima/v2/pkg/limatype"
16 "github.com/lima-vm/lima/v2/pkg/limatype/filenames"
17 "github.com/lima-vm/lima/v2/pkg/osutil"
18)
19
20// fakeBootSessionID replaces the source of the boot session ID for the duration of the test.
21func fakeBootSessionID(t *testing.T, id string, err error) {
22 t.Helper()
23 orig := bootSessionID
24 bootSessionID = func() (string, error) { return id, err }
25 t.Cleanup(func() { bootSessionID = orig })
26}
27
28func TestWritePIDFile(t *testing.T) {
29 fakeBootSessionID(t, "boot-1", nil)
30 dir := t.TempDir()
31 path := filepath.Join(dir, filenames.HostAgentPID)
32 assert.NilError(t, WritePIDFile(path, os.Getpid()))
33
34 assert.Assert(t, osutil.FileExists(filepath.Join(dir, filenames.BootSession)))
35
36 pid, err := ReadPIDFile(path)
37 assert.NilError(t, err)
38 assert.Equal(t, pid, os.Getpid())
39}
40
41func TestWritePIDFileRemovesPIDFilesFromPreviousBoot(t *testing.T) {
42 fakeBootSessionID(t, "boot-1", nil)
43 dir := t.TempDir()
44 haPath := filepath.Join(dir, filenames.HostAgentPID)
45 driverPath := filepath.Join(dir, filenames.PIDFile(limatype.QEMU))
46 assert.NilError(t, WritePIDFile(haPath, os.Getpid()))
47 assert.NilError(t, WritePIDFile(driverPath, os.Getpid()))
48
49 // Writing a PID file during a new boot voids every PID file of the directory, so that
50 // refreshing the marker cannot make a PID file of the previous boot look current.
51 fakeBootSessionID(t, "boot-2", nil)
52 assert.NilError(t, WritePIDFile(haPath, os.Getpid()))
53
54 _, err := os.Stat(driverPath)
55 assert.Assert(t, errors.Is(err, os.ErrNotExist))
56
57 pid, err := ReadPIDFile(haPath)
58 assert.NilError(t, err)
59 assert.Equal(t, pid, os.Getpid())
60}
61
62func TestReadPIDFileFromPreviousBoot(t *testing.T) {
63 fakeBootSessionID(t, "boot-1", nil)
64 dir := t.TempDir()
65 path := filepath.Join(dir, filenames.HostAgentPID)
66 assert.NilError(t, WritePIDFile(path, os.Getpid()))
67
68 // After a reboot the PID may belong to an unrelated process, so the PID file has to be
69 // ignored even though the PID is alive.
70 fakeBootSessionID(t, "boot-2", nil)
71
72 pid, err := ReadPIDFile(path)
73 assert.NilError(t, err)
74 assert.Equal(t, pid, 0)
75 // Removing it is left to the next writer, so that a PID file that another process is
76 // writing at this very moment cannot be removed by a reader.
77 assert.Assert(t, osutil.FileExists(path))
78}
79
80func TestReadPIDFileCorruptedFromPreviousBoot(t *testing.T) {
81 fakeBootSessionID(t, "boot-1", nil)
82 dir := t.TempDir()
83 path := filepath.Join(dir, filenames.HostAgentPID)
84 assert.NilError(t, WritePIDFile(path, os.Getpid()))
85 // A PID file that was truncated when the host was powered off.
86 assert.NilError(t, os.WriteFile(path, nil, 0o644))
87
88 fakeBootSessionID(t, "boot-2", nil)
89
90 pid, err := ReadPIDFile(path)
91 assert.NilError(t, err)
92 assert.Equal(t, pid, 0)
93}
94
95func TestReadPIDFileWithoutBootSessionMarker(t *testing.T) {
96 // PID files written by an older version of Lima have no marker, and are handled as
97 // before: only the liveness of the process matters.
98 fakeBootSessionID(t, "boot-1", nil)
99 dir := t.TempDir()
100 path := filepath.Join(dir, filenames.HostAgentPID)
101 assert.NilError(t, os.WriteFile(path, fmt.Appendf(nil, "%d\n", os.Getpid()), 0o644))
102
103 pid, err := ReadPIDFile(path)
104 assert.NilError(t, err)
105 assert.Equal(t, pid, os.Getpid())
106}
107
108func TestWritePIDFileWhenBootSessionIDFails(t *testing.T) {
109 // The PID file must not be written when it cannot be tied to the current boot:
110 // a marker of a previous boot would otherwise void a PID file that is current.
111 fakeBootSessionID(t, "", errors.New("sysctl failed"))
112 dir := t.TempDir()
113 path := filepath.Join(dir, filenames.HostAgentPID)
114
115 assert.ErrorContains(t, WritePIDFile(path, os.Getpid()), "sysctl failed")
116 assert.Assert(t, !osutil.FileExists(path))
117}
118
119func TestReadPIDFileWhenBootSessionIDFails(t *testing.T) {
120 fakeBootSessionID(t, "boot-1", nil)
121 dir := t.TempDir()
122 path := filepath.Join(dir, filenames.HostAgentPID)
123 assert.NilError(t, WritePIDFile(path, os.Getpid()))
124
125 fakeBootSessionID(t, "", errors.New("sysctl failed"))
126 _, err := ReadPIDFile(path)
127 assert.ErrorContains(t, err, "sysctl failed")
128}
129
130func TestWritePIDFileWithoutBootSessionSupport(t *testing.T) {
131 fakeBootSessionID(t, "", osutil.ErrBootSessionNotSupported)
132 dir := t.TempDir()
133 path := filepath.Join(dir, filenames.HostAgentPID)
134 assert.NilError(t, WritePIDFile(path, os.Getpid()))
135
136 assert.Assert(t, !osutil.FileExists(filepath.Join(dir, filenames.BootSession)))
137
138 pid, err := ReadPIDFile(path)
139 assert.NilError(t, err)
140 assert.Equal(t, pid, os.Getpid())
141}