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
|
package repository
import (
"bytes"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/require"
"gitlab.com/gitlab-org/gitaly/v14/internal/git"
"gitlab.com/gitlab-org/gitaly/v14/internal/git/gittest"
"gitlab.com/gitlab-org/gitaly/v14/internal/git/stats"
"gitlab.com/gitlab-org/gitaly/v14/internal/testhelper"
"gitlab.com/gitlab-org/gitaly/v14/proto/go/gitalypb"
"google.golang.org/grpc/codes"
)
func getNewestPackfileModtime(t *testing.T, repoPath string) time.Time {
t.Helper()
packFiles, err := filepath.Glob(filepath.Join(repoPath, "objects", "pack", "*.pack"))
require.NoError(t, err)
if len(packFiles) == 0 {
t.Error("no packfiles exist")
}
var newestPackfileModtime time.Time
for _, packFile := range packFiles {
info, err := os.Stat(packFile)
require.NoError(t, err)
if info.ModTime().After(newestPackfileModtime) {
newestPackfileModtime = info.ModTime()
}
}
return newestPackfileModtime
}
func TestOptimizeRepository(t *testing.T) {
cfg, repoProto, repoPath, client := setupRepositoryService(t)
gittest.Exec(t, cfg, "-C", repoPath, "repack", "-A", "-b")
ctx, cancel := testhelper.Context()
defer cancel()
hasBitmap, err := stats.HasBitmap(repoPath)
require.NoError(t, err)
require.True(t, hasBitmap, "expect a bitmap since we just repacked with -b")
// get timestamp of latest packfile
newestsPackfileTime := getNewestPackfileModtime(t, repoPath)
gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch("master"))
gittest.Exec(t, cfg, "-C", repoPath, "config", "http.http://localhost:51744/60631c8695bf041a808759a05de53e36a73316aacb502824fabbb0c6055637c1.git.extraHeader", "Authorization: Basic secret-password")
gittest.Exec(t, cfg, "-C", repoPath, "config", "http.http://localhost:51744/60631c8695bf041a808759a05de53e36a73316aacb502824fabbb0c6055637c2.git.extraHeader", "Authorization: Basic secret-password")
gittest.Exec(t, cfg, "-C", repoPath, "config", "randomStart-http.http://localhost:51744/60631c8695bf041a808759a05de53e36a73316aacb502824fabbb0c6055637c3.git.extraHeader", "Authorization: Basic secret-password")
gittest.Exec(t, cfg, "-C", repoPath, "config", "http.http://localhost:51744/60631c8695bf041a808759a05de53e36a73316aacb502824fabbb0c6055637c4.git.extraHeader-randomEnd", "Authorization: Basic secret-password")
gittest.Exec(t, cfg, "-C", repoPath, "config", "hTTp.http://localhost:51744/60631c8695bf041a808759a05de53e36a73316aacb502824fabbb0c6055637c5.git.ExtrAheaDeR", "Authorization: Basic secret-password")
gittest.Exec(t, cfg, "-C", repoPath, "config", "http.http://extraHeader/extraheader/EXTRAHEADER.git.extraHeader", "Authorization: Basic secret-password")
gittest.Exec(t, cfg, "-C", repoPath, "config", "https.https://localhost:51744/60631c8695bf041a808759a05de53e36a73316aacb502824fabbb0c6055637c5.git.extraHeader", "Authorization: Basic secret-password")
confFileData := testhelper.MustReadFile(t, filepath.Join(repoPath, "config"))
require.True(t, bytes.Contains(confFileData, []byte("http://localhost:51744/60631c8695bf041a808759a05de53e36a73316aacb502824fabbb0c6055637c1.git")))
require.True(t, bytes.Contains(confFileData, []byte("http://localhost:51744/60631c8695bf041a808759a05de53e36a73316aacb502824fabbb0c6055637c2.git")))
require.True(t, bytes.Contains(confFileData, []byte("http://localhost:51744/60631c8695bf041a808759a05de53e36a73316aacb502824fabbb0c6055637c3")))
require.True(t, bytes.Contains(confFileData, []byte("http://localhost:51744/60631c8695bf041a808759a05de53e36a73316aacb502824fabbb0c6055637c4.git")))
require.True(t, bytes.Contains(confFileData, []byte("http://localhost:51744/60631c8695bf041a808759a05de53e36a73316aacb502824fabbb0c6055637c5.git")))
require.True(t, bytes.Contains(confFileData, []byte("http://extraHeader/extraheader/EXTRAHEADER.git")))
require.True(t, bytes.Contains(confFileData, []byte("https://localhost:51744/60631c8695bf041a808759a05de53e36a73316aacb502824fabbb0c6055637c5.git")))
_, err = client.OptimizeRepository(ctx, &gitalypb.OptimizeRepositoryRequest{Repository: repoProto})
require.NoError(t, err)
confFileData = testhelper.MustReadFile(t, filepath.Join(repoPath, "config"))
require.False(t, bytes.Contains(confFileData, []byte("http://localhost:51744/60631c8695bf041a808759a05de53e36a73316aacb502824fabbb0c6055637c1.git")))
require.False(t, bytes.Contains(confFileData, []byte("http://localhost:51744/60631c8695bf041a808759a05de53e36a73316aacb502824fabbb0c6055637c2.git")))
require.True(t, bytes.Contains(confFileData, []byte("http://localhost:51744/60631c8695bf041a808759a05de53e36a73316aacb502824fabbb0c6055637c3")))
require.True(t, bytes.Contains(confFileData, []byte("http://localhost:51744/60631c8695bf041a808759a05de53e36a73316aacb502824fabbb0c6055637c4.git")))
require.False(t, bytes.Contains(confFileData, []byte("http://localhost:51744/60631c8695bf041a808759a05de53e36a73316aacb502824fabbb0c6055637c5.git")))
require.False(t, bytes.Contains(confFileData, []byte("http://extraHeader/extraheader/EXTRAHEADER.git.extraHeader")))
require.True(t, bytes.Contains(confFileData, []byte("https://localhost:51744/60631c8695bf041a808759a05de53e36a73316aacb502824fabbb0c6055637c5.git")))
require.Equal(t, getNewestPackfileModtime(t, repoPath), newestsPackfileTime, "there should not have been a new packfile created")
testRepoProto, testRepoPath, cleanupBare := gittest.InitBareRepoAt(t, cfg, cfg.Storages[0])
t.Cleanup(cleanupBare)
blobs := 10
blobIDs := gittest.WriteBlobs(t, cfg, testRepoPath, blobs)
for _, blobID := range blobIDs {
gittest.WriteCommit(t, cfg, testRepoPath,
gittest.WithTreeEntries(gittest.TreeEntry{
OID: git.ObjectID(blobID), Mode: "100644", Path: "blob",
}),
gittest.WithBranch(blobID),
gittest.WithParents(),
)
}
bitmaps, err := filepath.Glob(filepath.Join(testRepoPath, "objects", "pack", "*.bitmap"))
require.NoError(t, err)
require.Empty(t, bitmaps)
mrRefs := filepath.Join(testRepoPath, "refs/merge-requests")
emptyRef := filepath.Join(mrRefs, "1")
require.NoError(t, os.MkdirAll(emptyRef, 0755))
require.DirExists(t, emptyRef, "sanity check for empty ref dir existence")
// optimize repository on a repository without a bitmap should call repack full
_, err = client.OptimizeRepository(ctx, &gitalypb.OptimizeRepositoryRequest{Repository: testRepoProto})
require.NoError(t, err)
bitmaps, err = filepath.Glob(filepath.Join(testRepoPath, "objects", "pack", "*.bitmap"))
require.NoError(t, err)
require.NotEmpty(t, bitmaps)
// Empty directories should exist because they're too recent.
require.DirExists(t, emptyRef)
require.DirExists(t, mrRefs)
require.FileExists(t,
filepath.Join(testRepoPath, "refs/heads", blobIDs[0]),
"unpacked refs should never be removed",
)
// Change the modification time to me older than a day and retry the call. Empty
// directories must now be deleted.
oneDayAgo := time.Now().Add(-24 * time.Hour)
require.NoError(t, os.Chtimes(emptyRef, oneDayAgo, oneDayAgo))
require.NoError(t, os.Chtimes(mrRefs, oneDayAgo, oneDayAgo))
_, err = client.OptimizeRepository(ctx, &gitalypb.OptimizeRepositoryRequest{Repository: testRepoProto})
require.NoError(t, err)
require.NoFileExists(t, emptyRef)
require.NoFileExists(t, mrRefs)
}
func TestOptimizeRepositoryValidation(t *testing.T) {
_, repo, _, client := setupRepositoryService(t)
testCases := []struct {
desc string
repo *gitalypb.Repository
expectedErrorCode codes.Code
}{
{
desc: "empty repository",
repo: nil,
expectedErrorCode: codes.InvalidArgument,
},
{
desc: "invalid repository storage",
repo: &gitalypb.Repository{StorageName: "non-existent", RelativePath: repo.GetRelativePath()},
expectedErrorCode: codes.InvalidArgument,
},
{
desc: "invalid repository path",
repo: &gitalypb.Repository{StorageName: repo.GetStorageName(), RelativePath: "/path/not/exist"},
expectedErrorCode: codes.NotFound,
},
}
ctx, cancel := testhelper.Context()
defer cancel()
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
_, err := client.OptimizeRepository(ctx, &gitalypb.OptimizeRepositoryRequest{Repository: tc.repo})
require.Error(t, err)
testhelper.RequireGrpcError(t, err, tc.expectedErrorCode)
})
}
_, err := client.OptimizeRepository(ctx, &gitalypb.OptimizeRepositoryRequest{Repository: repo})
require.NoError(t, err)
}
|