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

git_config_test.go « bundleuri « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c14c3625b82ae0f4f4dd557a5b2a063a5f49b13c (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
package bundleuri

import (
	"context"
	"io"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
	"gitlab.com/gitlab-org/gitaly/v16/internal/backup"
	"gitlab.com/gitlab-org/gitaly/v16/internal/featureflag"
	"gitlab.com/gitlab-org/gitaly/v16/internal/git"
	"gitlab.com/gitlab-org/gitaly/v16/internal/git/gittest"
	"gitlab.com/gitlab-org/gitaly/v16/internal/git/localrepo"
	"gitlab.com/gitlab-org/gitaly/v16/internal/gitaly/storage"
	"gitlab.com/gitlab-org/gitaly/v16/internal/structerr"
	"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper"
	"gitlab.com/gitlab-org/gitaly/v16/internal/testhelper/testcfg"
)

func TestUploadPackGitConfig(t *testing.T) {
	testhelper.NewFeatureSets(featureflag.BundleURI).
		Run(t, testUploadPackGitConfig)
}

func testUploadPackGitConfig(t *testing.T, ctx context.Context) {
	t.Parallel()

	cfg := testcfg.Build(t)
	repoProto, _ := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{
		SkipCreationViaService: true,
	})
	repo := localrepo.NewTestRepo(t, cfg, repoProto)

	for _, tc := range []struct {
		desc           string
		findLatestFunc func(context.Context, storage.Repository) (*backup.Backup, error)
		signedURLFunc  func(context.Context, string, time.Duration) (string, error)
		expectedConfig []git.ConfigPair
	}{
		{
			desc: "no backup locator nor sink",
		},
		{
			desc: "no backup locator",
			signedURLFunc: func(context.Context, string, time.Duration) (string, error) {
				return "", structerr.NewNotFound("not signed")
			},
		},
		{
			desc: "no backup sink",
			findLatestFunc: func(context.Context, storage.Repository) (*backup.Backup, error) {
				return nil, structerr.NewNotFound("no backup found")
			},
		},
		{
			desc: "no backup found",
			findLatestFunc: func(context.Context, storage.Repository) (*backup.Backup, error) {
				return nil, structerr.NewNotFound("no backup found")
			},
			signedURLFunc: func(context.Context, string, time.Duration) (string, error) {
				return "", structerr.NewNotFound("not signed")
			},
		},
		{
			desc: "backup has no steps",
			findLatestFunc: func(context.Context, storage.Repository) (*backup.Backup, error) {
				return &backup.Backup{}, nil
			},
			signedURLFunc: func(context.Context, string, time.Duration) (string, error) {
				return "", structerr.NewNotFound("not signed")
			},
		},
		{
			desc: "backup step is incremental",
			findLatestFunc: func(context.Context, storage.Repository) (*backup.Backup, error) {
				return &backup.Backup{
					Steps: []backup.Step{{PreviousRefPath: "not-nil"}},
				}, nil
			},
			signedURLFunc: func(context.Context, string, time.Duration) (string, error) {
				return "", structerr.NewNotFound("not signed")
			},
		},
		{
			desc: "sign failed",
			findLatestFunc: func(context.Context, storage.Repository) (*backup.Backup, error) {
				return &backup.Backup{
					Steps: []backup.Step{{BundlePath: "not-nil"}},
				}, nil
			},
			signedURLFunc: func(context.Context, string, time.Duration) (string, error) {
				return "", structerr.NewNotFound("not signed")
			},
		},
		{
			desc: "success",
			findLatestFunc: func(context.Context, storage.Repository) (*backup.Backup, error) {
				return &backup.Backup{
					Steps: []backup.Step{{BundlePath: "not-nil"}},
				}, nil
			},
			signedURLFunc: func(context.Context, string, time.Duration) (string, error) {
				return "https://example.com/bundle.git?signed=ok", nil
			},
			expectedConfig: []git.ConfigPair{
				{
					Key:   "uploadpack.advertiseBundleURIs",
					Value: "true",
				},
				{
					Key:   "bundle.version",
					Value: "1",
				},
				{
					Key:   "bundle.mode",
					Value: "any",
				},
				{
					Key:   "bundle.some.uri",
					Value: "https://example.com/bundle.git?signed=ok",
				},
			},
		},
	} {
		tc := tc

		t.Run(tc.desc, func(t *testing.T) {
			t.Parallel()

			var locator backup.Locator
			if tc.findLatestFunc != nil {
				locator = dummyLocator{findLatestFunc: tc.findLatestFunc}
			}

			var sink backup.Sink
			if tc.signedURLFunc != nil {
				sink = dummySink{signedURLFunc: tc.signedURLFunc}
			}

			actual := UploadPackGitConfig(ctx, locator, sink, repo)

			if featureflag.BundleURI.IsEnabled(ctx) && tc.expectedConfig != nil {
				require.Equal(t, tc.expectedConfig, actual)
			} else {
				require.Empty(t, actual)
			}
		})
	}
}

type dummyLocator struct {
	findLatestFunc func(context.Context, storage.Repository) (*backup.Backup, error)
}

// BeginFull is not supported by this dummyLocator.
func (l dummyLocator) BeginFull(ctx context.Context, repo storage.Repository, backupID string) *backup.Backup {
	return nil
}

// BeginIncremental is not supported by this dummyLocator.
func (l dummyLocator) BeginIncremental(ctx context.Context, repo storage.Repository, backupID string) (*backup.Backup, error) {
	return nil, structerr.NewUnimplemented("BeginIncremental not implemented for dummyLocator")
}

// Commit is not supported by this dummyLocator.
func (l dummyLocator) Commit(ctx context.Context, backup *backup.Backup) error {
	return structerr.NewUnimplemented("Commit not implemented for dummyLocator")
}

// FindLatest calls the findLatestFunc of the dummyLocator.
func (l dummyLocator) FindLatest(ctx context.Context, repo storage.Repository) (*backup.Backup, error) {
	return l.findLatestFunc(ctx, repo)
}

// Find is not supported by this dummyLocator.
func (l dummyLocator) Find(ctx context.Context, repo storage.Repository, backupID string) (*backup.Backup, error) {
	return nil, structerr.NewUnimplemented("Find not implemented for dummyLocator")
}

type dummySink struct {
	signedURLFunc func(context.Context, string, time.Duration) (string, error)
}

// Close the dummySink.
func (s dummySink) Close() error {
	return nil
}

// GetWriter is not supported by this dummySink.
func (s dummySink) GetWriter(ctx context.Context, relativePath string) (io.WriteCloser, error) {
	return nil, structerr.NewUnimplemented("GetWriter not implemented for dummySink")
}

// GetReader is not supported by this dummySink.
func (s dummySink) GetReader(ctx context.Context, relativePath string) (io.ReadCloser, error) {
	return nil, structerr.NewUnimplemented("GetReader not implemented for dummySink")
}

// SignedURL calls the signedURLFunc of the dummySink.
func (s dummySink) SignedURL(ctx context.Context, relativePath string, expiry time.Duration) (string, error) {
	return s.signedURLFunc(ctx, relativePath, expiry)
}