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
|
package repository
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/internal/testhelper"
pb "gitlab.com/gitlab-org/gitaly-proto/go"
)
func TestCleanupDeletesRefsLocks(t *testing.T) {
server, serverSocketPath := runRepoServer(t)
defer server.Stop()
client, conn := newRepositoryClient(t, serverSocketPath)
defer conn.Close()
testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t)
defer cleanupFn()
ctx, cancel := testhelper.Context()
defer cancel()
req := &pb.CleanupRequest{Repository: testRepo}
refsPath := filepath.Join(testRepoPath, "refs")
keepRefPath := filepath.Join(refsPath, "heads", "keepthis")
createFileWithTimes(keepRefPath, freshTime)
keepOldRefPath := filepath.Join(refsPath, "heads", "keepthisalso")
createFileWithTimes(keepOldRefPath, oldTime)
keepDeceitfulRef := filepath.Join(refsPath, "heads", " .lock.not-actually-a-lock.lock ")
createFileWithTimes(keepDeceitfulRef, oldTime)
keepLockPath := filepath.Join(refsPath, "heads", "keepthis.lock")
createFileWithTimes(keepLockPath, freshTime)
deleteLockPath := filepath.Join(refsPath, "heads", "deletethis.lock")
createFileWithTimes(deleteLockPath, oldTime)
c, err := client.Cleanup(ctx, req)
assert.NoError(t, err)
assert.NotNil(t, c)
// Sanity checks
assert.FileExists(t, keepRefPath)
assert.FileExists(t, keepOldRefPath)
assert.FileExists(t, keepDeceitfulRef)
assert.FileExists(t, keepLockPath)
testhelper.AssertFileNotExists(t, deleteLockPath)
}
func TestCleanupDeletesPackedRefsLock(t *testing.T) {
server, serverSocketPath := runRepoServer(t)
defer server.Stop()
client, conn := newRepositoryClient(t, serverSocketPath)
defer conn.Close()
testCases := []struct {
desc string
lockTime *time.Time
shouldExist bool
}{
{
desc: "with a recent lock",
lockTime: &freshTime,
shouldExist: true,
},
{
desc: "with an old lock",
lockTime: &oldTime,
shouldExist: false,
},
{
desc: "with a non-existing lock",
lockTime: nil,
shouldExist: false,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t)
defer cleanupFn()
// Force the packed-refs file to have an old time to test that even
// in that case it doesn't get deleted
packedRefsPath := filepath.Join(testRepoPath, "packed-refs")
os.Chtimes(packedRefsPath, oldTime, oldTime)
req := &pb.CleanupRequest{Repository: testRepo}
lockPath := filepath.Join(testRepoPath, "packed-refs.lock")
if tc.lockTime != nil {
createFileWithTimes(lockPath, *tc.lockTime)
}
ctx, cancel := testhelper.Context()
defer cancel()
c, err := client.Cleanup(ctx, req)
// Sanity checks
assert.FileExists(t, filepath.Join(testRepoPath, "HEAD")) // For good measure
assert.FileExists(t, packedRefsPath)
if tc.shouldExist {
assert.FileExists(t, lockPath)
} else {
assert.NoError(t, err)
assert.NotNil(t, c)
testhelper.AssertFileNotExists(t, lockPath)
}
})
}
}
func TestCleanupDeletesStaleWorktrees(t *testing.T) {
server, serverSocketPath := runRepoServer(t)
defer server.Stop()
client, conn := newRepositoryClient(t, serverSocketPath)
defer conn.Close()
testCases := []struct {
desc string
worktreeTime time.Time
shouldExist bool
}{
{
desc: "with a recent worktree",
worktreeTime: freshTime,
shouldExist: true,
},
{
desc: "with a slightly old worktree",
worktreeTime: oldTime,
shouldExist: true,
},
{
desc: "with an old worktree",
worktreeTime: oldTreeTime,
shouldExist: false,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t)
defer cleanupFn()
req := &pb.CleanupRequest{Repository: testRepo}
testhelper.AddWorktree(t, testRepoPath, "test-worktree")
basePath := filepath.Join(testRepoPath, "worktrees")
worktreePath := filepath.Join(basePath, "test-worktree")
require.NoError(t, os.Chtimes(worktreePath, tc.worktreeTime, tc.worktreeTime))
ctx, cancel := testhelper.Context()
defer cancel()
c, err := client.Cleanup(ctx, req)
// Sanity check
assert.FileExists(t, filepath.Join(testRepoPath, "HEAD")) // For good measure
if tc.shouldExist {
assert.DirExists(t, basePath)
assert.DirExists(t, worktreePath)
} else {
assert.NoError(t, err)
assert.NotNil(t, c)
testhelper.AssertFileNotExists(t, worktreePath)
}
})
}
}
func TestCleanupFileLocks(t *testing.T) {
server, serverSocketPath := runRepoServer(t)
defer server.Stop()
client, conn := newRepositoryClient(t, serverSocketPath)
defer conn.Close()
testRepo, testRepoPath, cleanupFn := testhelper.NewTestRepo(t)
defer cleanupFn()
ctx, cancel := testhelper.Context()
defer cancel()
req := &pb.CleanupRequest{Repository: testRepo}
for _, fileName := range lockFiles {
lockPath := filepath.Join(testRepoPath, fileName)
// No file on the lock path
_, err := client.Cleanup(ctx, req)
assert.NoError(t, err)
// Fresh lock should remain
createFileWithTimes(lockPath, freshTime)
_, err = client.Cleanup(ctx, req)
assert.NoError(t, err)
assert.FileExists(t, lockPath)
// Old lock should be removed
createFileWithTimes(lockPath, oldTime)
_, err = client.Cleanup(ctx, req)
assert.NoError(t, err)
testhelper.AssertFileNotExists(t, lockPath)
}
}
|