Welcome to mirror list, hosted at ThFree Co, Russian Federation.

updateref_test.go « updateref « git « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0165e536e42829fe011ab24b9fadff1a03defe2e (plain)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package updateref

import (
	"context"
	"fmt"
	"strings"
	"testing"

	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/internal/git"
	"gitlab.com/gitlab-org/gitaly/internal/git/gittest"
	"gitlab.com/gitlab-org/gitaly/internal/git/localrepo"
	"gitlab.com/gitlab-org/gitaly/internal/gitaly/config"
	"gitlab.com/gitlab-org/gitaly/internal/testhelper"
	"gitlab.com/gitlab-org/gitaly/internal/testhelper/testcfg"
)

func TestMain(m *testing.M) {
	testhelper.Run(m)
}

func setupUpdater(t *testing.T, ctx context.Context) (config.Cfg, *localrepo.Repo, *Updater) {
	t.Helper()

	cfg, protoRepo, _ := testcfg.BuildWithRepo(t)

	repo := localrepo.NewTestRepo(t, cfg, protoRepo, git.WithSkipHooks())

	updater, err := New(ctx, repo)
	require.NoError(t, err)

	return cfg, repo, updater
}

func TestCreate(t *testing.T) {
	ctx := testhelper.Context(t)

	_, repo, updater := setupUpdater(t, ctx)

	headCommit, err := repo.ReadCommit(ctx, "HEAD")
	require.NoError(t, err)

	ref := git.ReferenceName("refs/heads/_create")
	sha := headCommit.Id

	require.NoError(t, updater.Create(ref, sha))
	require.NoError(t, updater.Commit())

	// check the ref was created
	commit, logErr := repo.ReadCommit(ctx, ref.Revision())
	require.NoError(t, logErr)
	require.Equal(t, commit.Id, sha, "reference was created with the wrong SHA")
}

func TestUpdate(t *testing.T) {
	ctx := testhelper.Context(t)

	_, repo, updater := setupUpdater(t, ctx)

	headCommit, err := repo.ReadCommit(ctx, "HEAD")
	require.NoError(t, err)

	ref := git.ReferenceName("refs/heads/feature")
	sha := headCommit.Id

	// Sanity check: ensure the ref exists before we start
	commit, logErr := repo.ReadCommit(ctx, ref.Revision())
	require.NoError(t, logErr)
	require.NotEqual(t, commit.Id, sha, "%s points to HEAD: %s in the test repository", ref.String(), sha)

	require.NoError(t, updater.Update(ref, sha, ""))
	require.NoError(t, updater.Prepare())
	require.NoError(t, updater.Commit())

	// check the ref was updated
	commit, logErr = repo.ReadCommit(ctx, ref.Revision())
	require.NoError(t, logErr)
	require.Equal(t, commit.Id, sha, "reference was not updated")

	// since ref has been updated to HEAD, we know that it does not point to HEAD^. So, HEAD^ is an invalid "old value" for updating ref
	parentCommit, err := repo.ReadCommit(ctx, "HEAD^")
	require.NoError(t, err)
	require.Error(t, updater.Update(ref, parentCommit.Id, parentCommit.Id))

	// check the ref was not updated
	commit, logErr = repo.ReadCommit(ctx, ref.Revision())
	require.NoError(t, logErr)
	require.NotEqual(t, commit.Id, parentCommit.Id, "reference was updated when it shouldn't have been")
}

func TestDelete(t *testing.T) {
	ctx := testhelper.Context(t)

	_, repo, updater := setupUpdater(t, ctx)

	ref := git.ReferenceName("refs/heads/feature")

	require.NoError(t, updater.Delete(ref))
	require.NoError(t, updater.Commit())

	// check the ref was removed
	_, err := repo.ReadCommit(ctx, ref.Revision())
	require.Equal(t, localrepo.ErrObjectNotFound, err, "expected 'not found' error got %v", err)
}

func TestUpdater_prepareLocksTransaction(t *testing.T) {
	ctx := testhelper.Context(t)

	_, repo, updater := setupUpdater(t, ctx)

	commit, logErr := repo.ReadCommit(ctx, "refs/heads/master")
	require.NoError(t, logErr)

	require.NoError(t, updater.Update("refs/heads/feature", commit.Id, ""))
	require.NoError(t, updater.Prepare())
	require.NoError(t, updater.Update("refs/heads/feature", commit.Id, ""))

	err := updater.Commit()
	require.Error(t, err, "cannot update after prepare")
	require.Contains(t, err.Error(), "fatal: prepared transactions can only be closed")
}

func TestUpdater_concurrentLocking(t *testing.T) {
	t.Parallel()
	ctx := testhelper.Context(t)

	cfg, protoRepo, _ := testcfg.BuildWithRepo(t)
	repo := localrepo.NewTestRepo(t, cfg, protoRepo, git.WithSkipHooks())

	commit, logErr := repo.ReadCommit(ctx, "refs/heads/master")
	require.NoError(t, logErr)

	firstUpdater, err := New(ctx, repo)
	require.NoError(t, err)
	require.NoError(t, firstUpdater.Update("refs/heads/master", "", commit.Id))
	require.NoError(t, firstUpdater.Prepare())

	secondUpdater, err := New(ctx, repo)
	require.NoError(t, err)
	require.NoError(t, secondUpdater.Update("refs/heads/master", "", commit.Id))

	// With flushing, we're able to detect concurrent locking at prepare time already instead of
	// at commit time.
	if gitSupportsStatusFlushing(t, ctx, cfg) {
		err := secondUpdater.Prepare()
		require.Error(t, err)
		require.Contains(t, err.Error(), "fatal: prepare: cannot lock ref 'refs/heads/master'")

		require.NoError(t, firstUpdater.Commit())
	} else {
		require.NoError(t, secondUpdater.Prepare())
		require.NoError(t, firstUpdater.Commit())

		err := secondUpdater.Commit()
		require.Error(t, err)
		require.Contains(t, err.Error(), "fatal: prepare: cannot lock ref 'refs/heads/master'")
	}
}

func TestBulkOperation(t *testing.T) {
	ctx := testhelper.Context(t)

	_, repo, updater := setupUpdater(t, ctx)

	headCommit, err := repo.ReadCommit(ctx, "HEAD")
	require.NoError(t, err)

	for i := 0; i < 1000; i++ {
		ref := fmt.Sprintf("refs/head/_test_%d", i)
		require.NoError(t, updater.Create(git.ReferenceName(ref), headCommit.Id), "Failed to create ref %d", i)
	}

	require.NoError(t, updater.Commit())

	refs, err := repo.GetReferences(ctx, "refs/")
	require.NoError(t, err)
	require.Greater(t, len(refs), 1000, "At least 1000 refs should be present")
}

func TestContextCancelAbortsRefChanges(t *testing.T) {
	ctx := testhelper.Context(t)

	cfg, repo, _ := setupUpdater(t, ctx)

	headCommit, err := repo.ReadCommit(ctx, "HEAD")
	require.NoError(t, err)

	childCtx, childCancel := context.WithCancel(ctx)
	localRepo := localrepo.NewTestRepo(t, cfg, repo)
	updater, err := New(childCtx, localRepo)
	require.NoError(t, err)

	ref := git.ReferenceName("refs/heads/_shouldnotexist")

	require.NoError(t, updater.Create(ref, headCommit.Id))

	// Force the update-ref process to terminate early
	childCancel()
	require.Error(t, updater.Commit())

	// check the ref doesn't exist
	_, err = repo.ReadCommit(ctx, ref.Revision())
	require.Equal(t, localrepo.ErrObjectNotFound, err, "expected 'not found' error got %v", err)
}

func TestUpdater_cancel(t *testing.T) {
	ctx := testhelper.Context(t)

	_, repo, updater := setupUpdater(t, ctx)

	require.NoError(t, updater.Delete(git.ReferenceName("refs/heads/master")))
	require.NoError(t, updater.Prepare())

	// A concurrent update shouldn't be allowed.
	concurrentUpdater, err := New(ctx, repo)
	require.NoError(t, err)
	require.NoError(t, concurrentUpdater.Delete(git.ReferenceName("refs/heads/master")))
	err = concurrentUpdater.Commit()
	require.NotNil(t, err)
	require.Contains(t, err.Error(), "fatal: commit: cannot lock ref 'refs/heads/master'")

	// We now cancel the initial updater. Afterwards, it should be possible again to update the
	// ref because locks should have been released.
	require.NoError(t, updater.Cancel())

	concurrentUpdater, err = New(ctx, repo)
	require.NoError(t, err)
	require.NoError(t, concurrentUpdater.Delete(git.ReferenceName("refs/heads/master")))
	require.NoError(t, concurrentUpdater.Commit())
}

func TestUpdater_closingStdinAbortsChanges(t *testing.T) {
	ctx := testhelper.Context(t)

	_, repo, updater := setupUpdater(t, ctx)

	headCommit, err := repo.ReadCommit(ctx, "HEAD")
	require.NoError(t, err)

	ref := git.ReferenceName("refs/heads/shouldnotexist")

	require.NoError(t, updater.Create(ref, headCommit.Id))

	// Note that we call `Wait()` on the command, not on the updater. This
	// circumvents our usual semantics of sending "commit" and thus
	// emulates that the command somehow terminates correctly without us
	// terminating it intentionally. Previous to our use of the "start"
	// verb, this would've caused the reference to be created...
	require.NoError(t, updater.cmd.Wait())

	// ... but as we now use explicit transactional behaviour, this is no
	// longer the case.
	_, err = repo.ReadCommit(ctx, ref.Revision())
	require.Equal(t, localrepo.ErrObjectNotFound, err, "expected 'not found' error got %v", err)
}

func TestUpdater_capturesStderr(t *testing.T) {
	t.Parallel()
	ctx := testhelper.Context(t)

	cfg, _, updater := setupUpdater(t, ctx)

	ref := "refs/heads/a"
	newValue := strings.Repeat("1", 40)
	oldValue := git.ZeroOID.String()

	require.NoError(t, updater.Update(git.ReferenceName(ref), newValue, oldValue))

	var expectedErr string
	if gitSupportsStatusFlushing(t, ctx, cfg) {
		expectedErr = fmt.Sprintf("state update to \"commit\" failed: EOF, stderr: \"fatal: commit: cannot update ref '%s': "+
			"trying to write ref '%s' with nonexistent object %s\\n\"", ref, ref, newValue)
	} else {
		expectedErr = fmt.Sprintf("git update-ref: exit status 128, stderr: "+
			"\"fatal: commit: cannot update ref '%s': "+
			"trying to write ref '%s' with nonexistent object %s\\n\"", ref, ref, newValue)
	}

	err := updater.Commit()
	require.NotNil(t, err)
	require.Equal(t, err.Error(), expectedErr)
}

func gitSupportsStatusFlushing(t *testing.T, ctx context.Context, cfg config.Cfg) bool {
	version, err := gittest.NewCommandFactory(t, cfg).GitVersion(ctx)
	require.NoError(t, err)
	return version.FlushesUpdaterefStatus()
}