Skip to content

Commit d112242

Browse files
committed
test(internal/procfs): add unit test for fs
1 parent b924b9b commit d112242

1 file changed

Lines changed: 130 additions & 0 deletions

File tree

internal/procfs/fs_test.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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 procfs
16+
17+
import (
18+
"os"
19+
"path/filepath"
20+
"strings"
21+
"testing"
22+
23+
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
25+
)
26+
27+
func rightCheck(t *testing.T, fs FS, err error) {
28+
assert.NoError(t, err)
29+
assert.NotNil(t, fs)
30+
}
31+
32+
func TestRootPrefixUpdatesMountPoints(t *testing.T) {
33+
tmpRoot := t.TempDir()
34+
originalPrefix := strings.TrimSuffix(DefaultPath(), "proc")
35+
RootPrefix(tmpRoot)
36+
defer func() { RootPrefix(originalPrefix) }()
37+
expectedProc := filepath.Join(tmpRoot, "/proc")
38+
expectedSys := filepath.Join(tmpRoot, "/sys")
39+
expectedDev := filepath.Join(tmpRoot, "/dev")
40+
41+
assert.Equal(t, expectedProc, defaultProcMountPoint)
42+
assert.Equal(t, expectedSys, defaultSysMountPoint)
43+
assert.Equal(t, expectedDev, defaultDevMountPoint)
44+
}
45+
46+
func TestNewDefaultFS_Filesystem(t *testing.T) {
47+
tmpRoot := t.TempDir()
48+
require.NoError(t, os.MkdirAll(filepath.Join(tmpRoot, "proc"), 0o755))
49+
originalPrefix := strings.TrimSuffix(DefaultPath(), "proc")
50+
RootPrefix(tmpRoot)
51+
defer func() { RootPrefix(originalPrefix) }()
52+
fs, err := NewDefaultFS()
53+
rightCheck(t, fs, err)
54+
}
55+
56+
func TestNewFS(t *testing.T) {
57+
tmpRoot := t.TempDir()
58+
tests := []struct {
59+
name string
60+
setup func(*testing.T) string
61+
validate func(*testing.T, FS, error)
62+
}{
63+
{
64+
name: "valid proc directory",
65+
setup: func(t *testing.T) string {
66+
procPath := filepath.Join(tmpRoot, "proc")
67+
require.NoError(t, os.MkdirAll(procPath, 0o755))
68+
return procPath
69+
},
70+
validate: rightCheck,
71+
},
72+
{
73+
name: "invalid file path",
74+
setup: func(t *testing.T) string {
75+
procPath := filepath.Join(tmpRoot, "file")
76+
require.NoError(t, os.WriteFile(procPath, []byte(""), 0o600))
77+
return procPath
78+
},
79+
validate: func(t *testing.T, fs FS, err error) { assert.Error(t, err) },
80+
},
81+
{
82+
name: "non-existent path",
83+
setup: func(t *testing.T) string {
84+
return "/nonexistent/path/"
85+
},
86+
validate: func(t *testing.T, fs FS, err error) { assert.Error(t, err) },
87+
},
88+
}
89+
90+
for _, tt := range tests {
91+
t.Run(tt.name, func(t *testing.T) {
92+
procPath := tt.setup(t)
93+
fs, err := NewFS(procPath)
94+
tt.validate(t, fs, err)
95+
})
96+
}
97+
}
98+
99+
func TestPath(t *testing.T) {
100+
tempRoot := t.TempDir()
101+
originalPrefix := strings.TrimSuffix(DefaultPath(), "proc")
102+
RootPrefix(tempRoot)
103+
defer func() { RootPrefix(originalPrefix) }()
104+
105+
expectedBase := filepath.Join(tempRoot, "/proc")
106+
assert.Equal(t, expectedBase, DefaultPath())
107+
108+
expectedPath := filepath.Join(expectedBase, "dira", "dirb")
109+
assert.Equal(t, Path("dira", "dirb"), expectedPath)
110+
}
111+
112+
func TestDefaultPathByType(t *testing.T) {
113+
defaultMounts := []string{"/proc", "/sys", "/dev"}
114+
for _, mount := range defaultMounts {
115+
typ := strings.TrimPrefix(mount, "/")
116+
assert.Equal(t, mount, DefaultPathByType(typ))
117+
}
118+
assert.Equal(t, "", DefaultPathByType("unknown"))
119+
}
120+
121+
// Integration Tests (Real Environment)
122+
// TEST_INTEGRATION=true go test -v ./internal/procfs/...
123+
func TestNewDefaultFS_Integration(t *testing.T) {
124+
if os.Getenv("TEST_INTEGRATION") == "" {
125+
t.Skip("Set TEST_INTEGRATION=true to run integration tests")
126+
}
127+
128+
fs, err := NewDefaultFS()
129+
rightCheck(t, fs, err)
130+
}

0 commit comments

Comments
 (0)