Repository navigation
Expand file tree
/
Copy pathfstat_test.go
More file actions
156 lines (134 loc) · 4.9 KB
/
Copy pathfstat_test.go
File metadata and controls
156 lines (134 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package followparser
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
)
func TestFileStat(t *testing.T) {
tmpDir := t.TempDir()
t.Run("regular file", func(t *testing.T) {
fname := filepath.Join(tmpDir, "test.log")
content := []byte("hello world\n")
err := os.WriteFile(fname, content, 0644)
require.NoError(t, err, "failed to create test file")
fs, err := fileStat(fname)
require.NoError(t, err, "fileStat should not return error for existing file")
require.NotNil(t, fs, "fileStat should return non-nil fStat")
require.NotZero(t, fs.Inode, "inode should not be zero")
require.NotZero(t, fs.Dev, "dev should not be zero")
require.Equal(t, int64(len(content)), fs.Size, "size should match file content length")
})
t.Run("file does not exist", func(t *testing.T) {
fname := filepath.Join(tmpDir, "nonexistent.log")
fs, err := fileStat(fname)
require.Error(t, err, "fileStat should return error for non-existent file")
require.Nil(t, fs, "fileStat should return nil fStat for non-existent file")
})
t.Run("directory", func(t *testing.T) {
dname := filepath.Join(tmpDir, "testdir")
err := os.Mkdir(dname, 0755)
require.NoError(t, err, "failed to create test directory")
fs, err := fileStat(dname)
require.NoError(t, err, "fileStat should not return error for directory")
require.NotNil(t, fs, "fileStat should return non-nil fStat for directory")
require.NotZero(t, fs.Inode, "inode should not be zero for directory")
})
}
func TestFStatIsNotRotated(t *testing.T) {
tests := []struct {
name string
fstat *fStat
lastFstat *fStat
expected bool
}{
{
name: "lastFstat is nil",
fstat: &fStat{Inode: 1, Dev: 2},
lastFstat: nil,
expected: true,
},
{
name: "same inode and dev",
fstat: &fStat{Inode: 1, Dev: 2},
lastFstat: &fStat{Inode: 1, Dev: 2},
expected: true,
},
{
name: "different inode same dev",
fstat: &fStat{Inode: 2, Dev: 2},
lastFstat: &fStat{Inode: 1, Dev: 2},
expected: false,
},
{
name: "same inode different dev",
fstat: &fStat{Inode: 1, Dev: 3},
lastFstat: &fStat{Inode: 1, Dev: 2},
expected: false,
},
{
name: "lastFstat inode is zero",
fstat: &fStat{Inode: 1, Dev: 2},
lastFstat: &fStat{Inode: 0, Dev: 2},
expected: true,
},
{
name: "lastFstat dev is zero",
fstat: &fStat{Inode: 1, Dev: 2},
lastFstat: &fStat{Inode: 1, Dev: 0},
expected: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
actual := tc.fstat.isNotRotated(tc.lastFstat)
require.Equal(t, tc.expected, actual, "isNotRotated result mismatch")
})
}
}
func TestFStatSearchFileByInode(t *testing.T) {
tmpDir := t.TempDir()
t.Run("find matching file", func(t *testing.T) {
fname := filepath.Join(tmpDir, "target.log")
err := os.WriteFile(fname, []byte("target content\n"), 0644)
require.NoError(t, err, "failed to create target file")
fs, err := fileStat(fname)
require.NoError(t, err, "failed to get file stat")
found, err := fs.searchFileByInode(tmpDir)
require.NoError(t, err, "searchFileByInode should find matching file")
require.Equal(t, fname, found, "found file path should match target")
})
t.Run("no matching file", func(t *testing.T) {
fs := &fStat{Inode: 999999999, Dev: 999999999}
_, err := fs.searchFileByInode(tmpDir)
require.Error(t, err, "searchFileByInode should return error when no matching file")
})
t.Run("directory does not exist", func(t *testing.T) {
fs := &fStat{Inode: 1, Dev: 2}
_, err := fs.searchFileByInode(filepath.Join(tmpDir, "nonexistent"))
require.Error(t, err, "searchFileByInode should return error for non-existent directory")
})
t.Run("skip directories", func(t *testing.T) {
dname := filepath.Join(tmpDir, "subdir")
err := os.Mkdir(dname, 0755)
require.NoError(t, err, "failed to create subdirectory")
fs := &fStat{Inode: 999999999, Dev: 999999999}
_, err = fs.searchFileByInode(tmpDir)
require.Error(t, err, "searchFileByInode should not match directories")
})
t.Run("ignore files that cannot be stated", func(t *testing.T) {
goodFile := filepath.Join(tmpDir, "good.log")
err := os.WriteFile(goodFile, []byte("good content\n"), 0644)
require.NoError(t, err, "failed to create good file")
// searchFileByInode ignores files that fail fileStat. Create a symlink
// to a non-existent target so that fileStat returns an error and is skipped.
symlink := filepath.Join(tmpDir, "broken_symlink")
err = os.Symlink(filepath.Join(tmpDir, "does_not_exist"), symlink)
require.NoError(t, err, "failed to create broken symlink")
fs, err := fileStat(goodFile)
require.NoError(t, err, "failed to get good file stat")
found, err := fs.searchFileByInode(tmpDir)
require.NoError(t, err, "searchFileByInode should find good file while ignoring broken symlink")
require.Equal(t, goodFile, found, "found file path should match good file")
})
}