Skip to content

Commit bcbf19e

Browse files
committed
test(internal/procfs/sysfs): add unit test for fs
1 parent 3cceb68 commit bcbf19e

1 file changed

Lines changed: 139 additions & 0 deletions

File tree

internal/procfs/sysfs/fs_test.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+
package sysfs
15+
16+
import (
17+
"os"
18+
"path/filepath"
19+
"strings"
20+
"testing"
21+
22+
"huatuo-bamai/internal/procfs"
23+
24+
"github.com/stretchr/testify/assert"
25+
"github.com/stretchr/testify/require"
26+
)
27+
28+
func TestDefaultPath(t *testing.T) {
29+
got := DefaultPath()
30+
assert.Equal(t, "/sys", got)
31+
}
32+
33+
func TestNewDefaultFS_Filesystem(t *testing.T) {
34+
tests := []struct {
35+
name string
36+
setup func(t *testing.T) string
37+
wantErr bool
38+
errContains string
39+
}{
40+
{
41+
name: "valid path directory",
42+
setup: func(t *testing.T) string {
43+
tmpDir := t.TempDir()
44+
sysPath := filepath.Join(tmpDir, "sys")
45+
require.NoError(t, os.MkdirAll(sysPath, 0o755))
46+
return tmpDir
47+
},
48+
wantErr: false,
49+
},
50+
{
51+
name: "invalid file path",
52+
setup: func(t *testing.T) string {
53+
tmpDir := t.TempDir()
54+
sysPath := filepath.Join(tmpDir, "file")
55+
require.NoError(t, os.WriteFile(sysPath, []byte("invalid file"), 0o600))
56+
return sysPath
57+
},
58+
wantErr: true,
59+
},
60+
{
61+
name: "non-existent path",
62+
setup: func(t *testing.T) string {
63+
return "/nonexistent/path/xyz"
64+
},
65+
wantErr: true,
66+
},
67+
{
68+
name: "valid --sys!! path",
69+
setup: func(t *testing.T) string {
70+
tmpDir := t.TempDir()
71+
sysPath := filepath.Join(tmpDir, "--sys!!")
72+
require.NoError(t, os.MkdirAll(filepath.Join(sysPath, "sys"), 0o755))
73+
return sysPath
74+
},
75+
wantErr: false,
76+
},
77+
}
78+
originalPrefix := strings.TrimSuffix(DefaultPath(), "sys")
79+
defer func() { procfs.RootPrefix(originalPrefix) }()
80+
for _, tt := range tests {
81+
t.Run(tt.name, func(t *testing.T) {
82+
procfs.RootPrefix(tt.setup(t))
83+
84+
fs, err := NewDefaultFS()
85+
if tt.wantErr {
86+
assert.Error(t, err)
87+
assert.Equal(t, FS{}, fs)
88+
} else {
89+
assert.NoError(t, err)
90+
assert.NotNil(t, fs)
91+
}
92+
})
93+
}
94+
}
95+
96+
func TestPath(t *testing.T) {
97+
tests := []struct {
98+
name string
99+
paths []string
100+
want string
101+
}{
102+
{
103+
name: "special single component",
104+
paths: []string{"//dev"},
105+
want: "/sys/dev",
106+
},
107+
{
108+
name: "deep path",
109+
paths: []string{"class", "net", "eth0"},
110+
want: "/sys/class/net/eth0",
111+
},
112+
{
113+
name: "empty paths",
114+
paths: []string{},
115+
want: "/sys",
116+
},
117+
}
118+
119+
for _, tt := range tests {
120+
t.Run(tt.name, func(t *testing.T) {
121+
got := Path(tt.paths...)
122+
assert.Equal(t, tt.want, got)
123+
})
124+
}
125+
}
126+
127+
// Integration Tests (Real Environment)
128+
// TEST_INTEGRATION=true go test -v ./internal/procfs/sysfs/...
129+
func TestNewDefaultFS_Integration(t *testing.T) {
130+
if os.Getenv("TEST_INTEGRATION") == "" {
131+
t.Skip("Set TEST_INTEGRATION=true to run integration tests")
132+
}
133+
134+
fs, err := NewDefaultFS()
135+
if err != nil {
136+
t.Skipf("/sys not available: %v", err)
137+
}
138+
assert.NotNil(t, fs)
139+
}

0 commit comments

Comments
 (0)