Skip to content

Commit 60b7e40

Browse files
committed
pidfile: add the unit testing
Signed-off-by: Tonghao Zhang <tonghao@bamaicloud.com>
1 parent 5ce8808 commit 60b7e40

36 files changed

Lines changed: 12584 additions & 0 deletions

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ require (
2727
github.com/safchain/ethtool v0.6.2
2828
github.com/shirou/gopsutil v2.21.11+incompatible
2929
github.com/sirupsen/logrus v1.9.3
30+
github.com/stretchr/testify v1.10.0
3031
github.com/tklauser/numcpus v0.6.1
3132
github.com/urfave/cli/v2 v2.27.4
3233
github.com/vishvananda/netlink v1.3.1
@@ -58,6 +59,7 @@ require (
5859
github.com/coreos/go-semver v0.3.0 // indirect
5960
github.com/coreos/go-systemd/v22 v22.5.0 // indirect
6061
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
62+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
6163
github.com/dennwc/varint v1.0.0 // indirect
6264
github.com/distribution/reference v0.6.0 // indirect
6365
github.com/docker/go-connections v0.5.0 // indirect
@@ -129,6 +131,7 @@ require (
129131
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
130132
github.com/pierrec/lz4/v4 v4.1.21 // indirect
131133
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
134+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
132135
github.com/prometheus/client_model v0.6.1 // indirect
133136
github.com/prometheus/common v0.62.0 // indirect
134137
github.com/prometheus/prometheus v0.302.1 // indirect

internal/pidfile/pidfile_test.go

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
// Copyright 2026 The HuaTuo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package pidfile
16+
17+
import (
18+
"os"
19+
"path/filepath"
20+
"strconv"
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
25+
)
26+
27+
func TestPath(t *testing.T) {
28+
tests := []struct {
29+
name string
30+
expected string
31+
}{
32+
{"app", "/var/run/app.pid"},
33+
{"nginx", "/var/run/nginx.pid"},
34+
{"", "/var/run/.pid"},
35+
}
36+
37+
for _, tt := range tests {
38+
t.Run(tt.name, func(t *testing.T) {
39+
assert.Equal(t, tt.expected, path(tt.name))
40+
})
41+
}
42+
}
43+
44+
func TestLock_Success(t *testing.T) {
45+
tmpDir := t.TempDir()
46+
oldDefault := defaultDirPath
47+
defaultDirPath = tmpDir
48+
t.Cleanup(func() { defaultDirPath = oldDefault })
49+
50+
name := "testapp"
51+
pidPath := path(name)
52+
53+
err := Lock(name)
54+
require.NoError(t, err)
55+
t.Cleanup(func() { UnLock(name) })
56+
57+
_, err = os.Stat(pidPath)
58+
require.NoError(t, err)
59+
60+
data, err := os.ReadFile(pidPath)
61+
require.NoError(t, err)
62+
pid, err := strconv.Atoi(string(data))
63+
require.NoError(t, err)
64+
assert.Equal(t, os.Getpid(), pid)
65+
}
66+
67+
func TestLock_AlreadyLocked(t *testing.T) {
68+
tmpDir := t.TempDir()
69+
oldDefault := defaultDirPath
70+
defaultDirPath = tmpDir
71+
t.Cleanup(func() { defaultDirPath = oldDefault })
72+
73+
name := "test-locked"
74+
75+
err := Lock(name)
76+
require.NoError(t, err)
77+
t.Cleanup(func() { UnLock(name) })
78+
79+
err = Lock(name)
80+
assert.Error(t, err)
81+
assert.Contains(t, err.Error(), "running")
82+
}
83+
84+
func TestUnLock(t *testing.T) {
85+
tmpDir := t.TempDir()
86+
oldDefault := defaultDirPath
87+
defaultDirPath = tmpDir
88+
t.Cleanup(func() { defaultDirPath = oldDefault })
89+
90+
name := "toremove"
91+
pidPath := path(name)
92+
93+
err := os.WriteFile(pidPath, []byte("12345"), 0o600)
94+
require.NoError(t, err)
95+
96+
UnLock(name)
97+
98+
_, err = os.Stat(pidPath)
99+
assert.True(t, os.IsNotExist(err))
100+
}
101+
102+
func TestRead(t *testing.T) {
103+
tmp := t.TempDir()
104+
pidPath := filepath.Join(tmp, "test.pid")
105+
106+
tests := []struct {
107+
name string
108+
content string
109+
wantPID int
110+
wantErrKind bool // 是否期望有错误
111+
}{
112+
{"normal", "12345", 12345, false},
113+
{"with space", " 67890 \n", 67890, false},
114+
{"negative", "-1", -1, false},
115+
{"empty", "", 0, true},
116+
{"invalid", "abc", 0, true},
117+
}
118+
119+
for _, tt := range tests {
120+
t.Run(tt.name, func(t *testing.T) {
121+
err := os.WriteFile(pidPath, []byte(tt.content), 0o600)
122+
require.NoError(t, err)
123+
124+
got, err := Read(pidPath)
125+
if tt.wantErrKind {
126+
assert.Error(t, err)
127+
assert.Zero(t, got)
128+
} else {
129+
assert.NoError(t, err)
130+
assert.Equal(t, tt.wantPID, got)
131+
}
132+
})
133+
}
134+
}
135+
136+
func TestRead_NotExist(t *testing.T) {
137+
_, err := Read("/this/file/does/not/exist.pid")
138+
assert.Error(t, err)
139+
assert.True(t, os.IsNotExist(err))
140+
}

vendor/github.com/davecgh/go-spew/LICENSE

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/davecgh/go-spew/spew/bypass.go

Lines changed: 145 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/davecgh/go-spew/spew/bypasssafe.go

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)