-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjournald_test.go
More file actions
225 lines (198 loc) · 6.89 KB
/
journald_test.go
File metadata and controls
225 lines (198 loc) · 6.89 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestMoveFile_Success(t *testing.T) {
dir := t.TempDir()
src := filepath.Join(dir, "src.txt")
dest := filepath.Join(dir, "dest.txt")
content := []byte("hello world")
err := os.WriteFile(src, content, 0644)
assert.NoError(t, err)
err = moveFile(src, dest)
assert.NoError(t, err)
// Source should not exist
_, err = os.Stat(src)
assert.True(t, os.IsNotExist(err))
// Dest should exist and have correct content
got, err := os.ReadFile(dest)
assert.NoError(t, err)
assert.Equal(t, content, got)
// Permissions should be preserved
info, err := os.Stat(dest)
assert.NoError(t, err)
assert.Equal(t, fs.FileMode(0644), info.Mode().Perm())
}
func TestMoveFile_OverwriteExistingFile(t *testing.T) {
dir := t.TempDir()
src := filepath.Join(dir, "src.txt")
dest := filepath.Join(dir, "dest.txt")
err := os.WriteFile(src, []byte("src data"), 0600)
assert.NoError(t, err)
err = os.WriteFile(dest, []byte("old dest data"), 0644)
assert.NoError(t, err)
err = moveFile(src, dest)
assert.NoError(t, err)
// Source should not exist
_, err = os.Stat(src)
assert.True(t, os.IsNotExist(err))
// Dest should have new content
got, err := os.ReadFile(dest)
assert.NoError(t, err)
assert.Equal(t, []byte("src data"), got)
}
func TestMoveFile_DestIsDirectory(t *testing.T) {
dir := t.TempDir()
src := filepath.Join(dir, "src.txt")
destDir := filepath.Join(dir, "destdir")
err := os.WriteFile(src, []byte("data"), 0644)
assert.NoError(t, err)
err = os.Mkdir(destDir, 0755)
assert.NoError(t, err)
err = moveFile(src, destDir)
assert.Error(t, err)
assert.True(t, strings.Contains(err.Error(), "is a directory"))
}
func TestMoveFile_SrcDoesNotExist(t *testing.T) {
dir := t.TempDir()
src := filepath.Join(dir, "no_such_file.txt")
dest := filepath.Join(dir, "dest.txt")
err := moveFile(src, dest)
assert.Error(t, err)
assert.True(t, os.IsNotExist(err))
}
func TestMoveFile_DestNoWritePermission(t *testing.T) {
dir := t.TempDir()
src := filepath.Join(dir, "src.txt")
noPermDir := filepath.Join(dir, "no_perm")
dest := filepath.Join(noPermDir, "dest.txt")
err := os.WriteFile(src, []byte("data"), 0644)
assert.NoError(t, err)
err = os.Mkdir(noPermDir, 0500)
assert.NoError(t, err)
defer os.Chmod(noPermDir, 0755) // nolint:errcheck
err = moveFile(src, dest)
assert.Error(t, err)
}
func TestShouldTriggerError(t *testing.T) {
tests := []struct {
name string
drive string
message string
want bool
}{
{
name: "copy error with insufficientParentPermissions",
message: "ERROR : test: Failed to copy: googleapi: Error 403: Insufficient permissions for the specified parent., insufficientParentPermissions",
drive: "my_drive",
want: false,
},
{
name: "vfs cache upload error with insufficientParentPermissions",
message: "ERROR : test: vfs cache: failed to upload try #3, will retry in 40s: vfs cache: failed to transfer file from cache to remote: googleapi: Error 403: Insufficient permissions for the specified parent., insufficientParentPermissions",
drive: "my_drive",
want: true,
},
{
name: "make directory error with insufficientParentPermissions",
message: "ERROR : IO error: failed to make directory: googleapi: Error 403: Insufficient permissions for the specified parent., insufficientParentPermissions",
drive: "my_drive",
want: false,
},
{
name: "mkdir failed to create directory with insufficientParentPermissions",
message: "ERROR : /: Dir.Mkdir failed to create directory: failed to make directory: googleapi: Error 403: Insufficient permissions for the specified parent., insufficientParentPermissions",
drive: "my_drive",
want: false,
},
{
name: "vfs cache upload error without insufficientParentPermissions",
message: "ERROR : test: vfs cache: failed to upload try #3, will retry in 40s: vfs cache: failed to transfer file from cache to remote: some other error",
drive: "my_drive",
want: true,
},
{
name: "io error with cannotDownloadFile",
message: "ERROR : IO error: open file failed: googleapi: Error 403: This file cannot be downloaded by the user., cannotDownloadFile",
drive: "my_drive",
want: true,
},
{
name: "io error with cannotDownloadFile",
message: "ERROR : IO error: open file failed: googleapi: Error 403: This file cannot be downloaded by the user., cannotDownloadFile",
drive: "shared_with_me",
want: false,
},
{
name: "random error",
message: "ERROR : something else",
drive: "my_drive",
want: true,
},
{
name: "Delete non-empty directory error",
message: "2025/06/16 15:37:34 ERROR : Team Wolf/TEST/asdfasdf/: Dir.Remove not empty",
drive: "some_shared_drive",
want: false,
},
}
for _, tt := range tests {
entry := LogEntry{Message: tt.message}
got := shouldTriggerError(entry, tt.drive)
assert.Equalf(t, tt.want, got, "%s (%s): shouldTriggerError() = %v, want %v", tt.name, tt.drive, got, tt.want)
}
}
func TestFileNameFromEntry(t *testing.T) {
for _, test := range []struct {
name string
message string
}{
{
name: "test",
message: "ERROR : test: vfs cache: failed to upload try #3, will retry in 40s: vfs cache: failed to transfer file from cache to remote: googleapi: Error 403: Insufficient permissions for the specified parent., insufficientParentPermissions",
},
{
name: "/",
message: "ERROR : /: Dir.Mkdir failed to create directory: failed to make directory: googleapi: Error 403: Insufficient permissions for the specified parent., insufficientParentPermissions",
},
{
name: "blatest.test",
message: "Mai 21 11:26:12 psigma rclone[270244]: 2025/05/21 11:26:12 ERROR : blatest.test: vfs cache: failed to upload try #4, will retry in 1m20s: vfs cache: failed to transfer file from cache to remote: googleapi: Error 403: Insufficient permissions for the specified parent., insufficientParentPermissions",
},
{
name: "",
message: "something anything nothing",
},
} {
n := fileNameFromEntry(LogEntry{Message: test.message})
assert.Equal(t, test.name, n)
}
}
func TestShouldTriggerFileMove(t *testing.T) {
tests := []struct {
name string
message string
want bool
}{
{
name: "vfs cache upload error with insufficientParentPermissions",
message: "ERROR : test: vfs cache: failed to upload try #3, will retry in 40s: vfs cache: failed to transfer file from cache to remote: googleapi: Error 403: Insufficient permissions for the specified parent., insufficientParentPermissions",
want: true,
},
{
name: "random error",
message: "ERROR : something else",
want: false,
},
}
for _, tt := range tests {
entry := LogEntry{Message: tt.message}
got := shouldTriggerFileMove(entry)
assert.Equalf(t, tt.want, got, "%s: shouldTriggerFileMove() = %v, want %v", tt.name, got, tt.want)
}
}