Skip to content

Commit cf45363

Browse files
committed
feat: add arp cache unit testing
Signed-off-by: Tonghao Zhang <tonghao@bamaicloud.com>
1 parent 7a9e730 commit cf45363

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
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+
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
func TestNetArpCache(t *testing.T) {
26+
// Create temporary procfs
27+
tempDir, err := os.MkdirTemp("", "procfs-test-")
28+
require.NoError(t, err)
29+
defer os.RemoveAll(tempDir)
30+
31+
arpDir := filepath.Join(tempDir, "proc/net/stat")
32+
err = os.MkdirAll(arpDir, 0o755)
33+
require.NoError(t, err)
34+
35+
arpCachePath := filepath.Join(arpDir, "arp_cache")
36+
37+
RootPrefix(tempDir)
38+
defer RootPrefix("/")
39+
40+
t.Run("SuccessfulParse", func(t *testing.T) {
41+
content := `entries allocs destroys hash_grows lookups hits dests
42+
0a 0b 0c 0d 0e 0f 10`
43+
44+
err = os.WriteFile(arpCachePath, []byte(content), 0o600)
45+
require.NoError(t, err)
46+
47+
stats, err := NetArpCache()
48+
require.NoError(t, err)
49+
require.NotNil(t, stats)
50+
require.Len(t, stats.Stats, 7)
51+
require.Equal(t, uint64(10), stats.Stats["entries"])
52+
require.Equal(t, uint64(11), stats.Stats["allocs"])
53+
require.Equal(t, uint64(12), stats.Stats["destroys"])
54+
require.Equal(t, uint64(13), stats.Stats["hash_grows"])
55+
require.Equal(t, uint64(14), stats.Stats["lookups"])
56+
require.Equal(t, uint64(15), stats.Stats["hits"])
57+
require.Equal(t, uint64(16), stats.Stats["dests"])
58+
})
59+
60+
t.Run("FileNotFound", func(t *testing.T) {
61+
_ = os.Remove(arpCachePath)
62+
63+
stats, err := NetArpCache()
64+
require.Error(t, err)
65+
require.Nil(t, stats)
66+
})
67+
68+
t.Run("InvalidHex", func(t *testing.T) {
69+
content := `entries
70+
invalid`
71+
72+
err = os.WriteFile(arpCachePath, []byte(content), 0o600)
73+
require.NoError(t, err)
74+
75+
stats, err := NetArpCache()
76+
require.Error(t, err)
77+
require.Contains(t, err.Error(), "invalid syntax")
78+
require.Nil(t, stats)
79+
})
80+
81+
t.Run("MissingSecondLine", func(t *testing.T) {
82+
content := `entries allocs`
83+
84+
err = os.WriteFile(arpCachePath, []byte(content), 0o600)
85+
require.NoError(t, err)
86+
87+
stats, err := NetArpCache()
88+
require.NoError(t, err)
89+
require.NotNil(t, stats)
90+
require.Empty(t, stats.Stats)
91+
})
92+
93+
t.Run("EmptyFile", func(t *testing.T) {
94+
content := ``
95+
96+
err = os.WriteFile(arpCachePath, []byte(content), 0o600)
97+
require.NoError(t, err)
98+
99+
stats, err := NetArpCache()
100+
require.NoError(t, err)
101+
require.NotNil(t, stats)
102+
require.Empty(t, stats.Stats)
103+
})
104+
105+
t.Run("MismatchedFieldsFewerValues", func(t *testing.T) {
106+
content := `entries allocs destroys
107+
0a 0b`
108+
err = os.WriteFile(arpCachePath, []byte(content), 0o600)
109+
require.NoError(t, err)
110+
111+
stats, err := NetArpCache()
112+
require.NoError(t, err)
113+
require.Len(t, stats.Stats, 2)
114+
require.Equal(t, uint64(10), stats.Stats["entries"])
115+
require.Equal(t, uint64(11), stats.Stats["allocs"])
116+
})
117+
}

0 commit comments

Comments
 (0)