This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathexternal_service_test.go
More file actions
332 lines (299 loc) · 10.7 KB
/
external_service_test.go
File metadata and controls
332 lines (299 loc) · 10.7 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/assert"
"github.com/sourcegraph/sourcegraph/internal/extsvc"
"github.com/sourcegraph/sourcegraph/internal/gqltestutil"
)
func TestExternalService(t *testing.T) {
if len(*githubToken) == 0 {
t.Skip("Environment variable GITHUB_TOKEN is not set")
}
t.Run("repositoryPathPattern", func(t *testing.T) {
const repo = "sgtest/go-diff" // Tiny repo, fast to clone
const slug = "github.com/" + repo
// Set up external service
esID, err := client.AddExternalService(gqltestutil.AddExternalServiceInput{
Kind: extsvc.KindGitHub,
DisplayName: "gqltest-github-repoPathPattern",
Config: mustMarshalJSONString(struct {
URL string `json:"url"`
Token string `json:"token"`
Repos []string `json:"repos"`
RepositoryPathPattern string `json:"repositoryPathPattern"`
}{
URL: "https://ghe.sgdev.org/",
Token: *githubToken,
Repos: []string{repo},
RepositoryPathPattern: "github.com/{nameWithOwner}",
}),
})
// The repo-updater might not be up yet, but it will eventually catch up for the external
// service we just added, thus it is OK to ignore this transient error.
if err != nil && !strings.Contains(err.Error(), "/sync-external-service") {
t.Fatal(err)
}
removeExternalServiceAfterTest(t, esID)
err = client.WaitForReposToBeCloned(slug)
if err != nil {
t.Fatal(err)
}
// The request URL should be redirected to the new path
origURL := *baseURL + "/" + slug
resp, err := client.Get(origURL)
if err != nil {
t.Fatal(err)
}
defer func() { _ = resp.Body.Close() }()
wantURL := *baseURL + "/" + slug // <baseURL>/github.com/sgtest/go-diff
if diff := cmp.Diff(wantURL, resp.Request.URL.String()); diff != "" {
t.Fatalf("URL mismatch (-want +got):\n%s", diff)
}
})
}
func TestExternalService_AWSCodeCommit(t *testing.T) {
if len(*awsAccessKeyID) == 0 || len(*awsSecretAccessKey) == 0 ||
len(*awsCodeCommitUsername) == 0 || len(*awsCodeCommitPassword) == 0 {
t.Skip("Environment variable AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_CODE_COMMIT_USERNAME or AWS_CODE_COMMIT_PASSWORD is not set")
}
// Set up external service
esID, err := client.AddExternalService(gqltestutil.AddExternalServiceInput{
Kind: extsvc.KindAWSCodeCommit,
DisplayName: "gqltest-aws-code-commit",
Config: mustMarshalJSONString(struct {
Region string `json:"region"`
AccessKeyID string `json:"accessKeyID"`
SecretAccessKey string `json:"secretAccessKey"`
RepositoryPathPattern string `json:"repositoryPathPattern"`
GitCredentials map[string]string `json:"gitCredentials"`
}{
Region: "us-west-1",
AccessKeyID: *awsAccessKeyID,
SecretAccessKey: *awsSecretAccessKey,
RepositoryPathPattern: "aws/{name}",
GitCredentials: map[string]string{
"username": *awsCodeCommitUsername,
"password": *awsCodeCommitPassword,
},
}),
})
// The repo-updater might not be up yet, but it will eventually catch up for the external
// service we just added, thus it is OK to ignore this transient error.
if err != nil && !strings.Contains(err.Error(), "/sync-external-service") {
t.Fatal(err)
}
removeExternalServiceAfterTest(t, esID)
const repoName = "aws/test"
err = client.WaitForReposToBeCloned(repoName)
if err != nil {
t.Fatal(err)
}
blob, err := client.GitBlob(repoName, "master", "README")
if err != nil {
t.Fatal(err)
}
wantBlob := "README\n\nchange"
if diff := cmp.Diff(wantBlob, blob); diff != "" {
t.Fatalf("Blob mismatch (-want +got):\n%s", diff)
}
}
func TestExternalService_BitbucketServer(t *testing.T) {
if len(*bbsURL) == 0 || len(*bbsToken) == 0 || len(*bbsUsername) == 0 {
t.Skip("Environment variable BITBUCKET_SERVER_URL, BITBUCKET_SERVER_TOKEN, or BITBUCKET_SERVER_USERNAME is not set")
}
// Set up external service
esID, err := client.AddExternalService(gqltestutil.AddExternalServiceInput{
Kind: extsvc.KindBitbucketServer,
DisplayName: "gqltest-bitbucket-server",
Config: mustMarshalJSONString(struct {
URL string `json:"url"`
Token string `json:"token"`
Username string `json:"username"`
Repos []string `json:"repos"`
RepositoryPathPattern string `json:"repositoryPathPattern"`
}{
URL: *bbsURL,
Token: *bbsToken,
Username: *bbsUsername,
Repos: []string{"SOURCEGRAPH/jsonrpc2"},
RepositoryPathPattern: "bbs/{projectKey}/{repositorySlug}",
}),
})
// The repo-updater might not be up yet, but it will eventually catch up for the external
// service we just added, thus it is OK to ignore this transient error.
if err != nil && !strings.Contains(err.Error(), "/sync-external-service") {
t.Fatal(err)
}
removeExternalServiceAfterTest(t, esID)
const repoName = "bbs/SOURCEGRAPH/jsonrpc2"
err = client.WaitForReposToBeCloned(repoName)
if err != nil {
t.Fatal(err)
}
blob, err := client.GitBlob(repoName, "master", ".travis.yml")
if err != nil {
t.Fatal(err)
}
wantBlob := "language: go\ngo: \n - 1.x\n\nscript:\n - go test -race -v ./...\n"
if diff := cmp.Diff(wantBlob, blob); diff != "" {
t.Fatalf("Blob mismatch (-want +got):\n%s", diff)
}
}
func TestExternalService_Perforce(t *testing.T) {
for _, tc := range []struct {
name string
depot string
useFusion bool
blobPath string
wantBlob string
}{
{
name: "git p4",
depot: "test-perms",
useFusion: false,
blobPath: "README.md",
wantBlob: `This depot is used to test user and group permissions.
`,
},
{
name: "p4 fusion",
depot: "integration-test-depot",
useFusion: true,
blobPath: "path.txt",
wantBlob: `./
`,
},
} {
t.Run(tc.name, func(t *testing.T) {
repoName := "perforce/" + tc.depot
checkPerforceEnvironment(t)
cleanup := createPerforceExternalService(t, tc.depot, tc.useFusion)
t.Cleanup(cleanup)
err := client.WaitForReposToBeCloned(repoName)
if err != nil {
t.Fatal(err)
}
blob, err := client.GitBlob(repoName, "master", tc.blobPath)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, tc.wantBlob, blob)
})
}
}
func checkPerforceEnvironment(t *testing.T) {
if len(*perforcePort) == 0 || len(*perforceUser) == 0 || len(*perforcePassword) == 0 {
t.Skip("Environment variables PERFORCE_PORT, PERFORCE_USER or PERFORCE_PASSWORD are not set")
}
}
// createPerforceExternalService creates an Perforce external service that
// includes the supplied depot. It returns a function to cleanup after the test
// which will delete the depot from disk and remove the external service.
func createPerforceExternalService(t *testing.T, depot string, useP4Fusion bool) func() {
t.Helper()
type Authorization = struct {
SubRepoPermissions bool `json:"subRepoPermissions"`
}
type FusionClient = struct {
Enabled bool `json:"enabled"`
LookAhead int `json:"lookAhead,omitempty"`
}
// Set up external service
esID, err := client.AddExternalService(gqltestutil.AddExternalServiceInput{
Kind: extsvc.KindPerforce,
DisplayName: "gqltest-perforce-server",
Config: mustMarshalJSONString(struct {
P4Port string `json:"p4.port"`
P4User string `json:"p4.user"`
P4Password string `json:"p4.passwd"`
Depots []string `json:"depots"`
RepositoryPathPattern string `json:"repositoryPathPattern"`
FusionClient FusionClient `json:"fusionClient"`
Authorization Authorization `json:"authorization"`
}{
P4Port: *perforcePort,
P4User: *perforceUser,
P4Password: *perforcePassword,
Depots: []string{"//" + depot + "/"},
RepositoryPathPattern: "perforce/{depot}",
FusionClient: FusionClient{
Enabled: useP4Fusion,
LookAhead: 2000,
},
Authorization: Authorization{
SubRepoPermissions: true,
},
}),
})
// The repo-updater might not be up yet but it will eventually catch up for the
// external service we just added, thus it is OK to ignore this transient error.
if err != nil && !strings.Contains(err.Error(), "/sync-external-service") {
t.Fatal(err)
}
return func() {
if err := client.DeleteRepoFromDiskByName("perforce/" + depot); err != nil {
t.Fatalf("removing depot from disk: %v", err)
}
if err := client.DeleteExternalService(esID, false); err != nil {
t.Fatalf("removing external service: %v", err)
}
}
}
func TestExternalService_AsyncDeletion(t *testing.T) {
if len(*bbsURL) == 0 || len(*bbsToken) == 0 || len(*bbsUsername) == 0 {
t.Skip("Environment variable BITBUCKET_SERVER_URL, BITBUCKET_SERVER_TOKEN, or BITBUCKET_SERVER_USERNAME is not set")
}
// Set up external service
esID, err := client.AddExternalService(gqltestutil.AddExternalServiceInput{
Kind: extsvc.KindBitbucketServer,
DisplayName: "gqltest-bitbucket-server",
Config: mustMarshalJSONString(struct {
URL string `json:"url"`
Token string `json:"token"`
Username string `json:"username"`
Repos []string `json:"repos"`
RepositoryPathPattern string `json:"repositoryPathPattern"`
}{
URL: *bbsURL,
Token: *bbsToken,
Username: *bbsUsername,
Repos: []string{"SOURCEGRAPH/jsonrpc2"},
RepositoryPathPattern: "bbs/{projectKey}/{repositorySlug}",
}),
})
// The repo-updater might not be up yet, but it will eventually catch up for the external
// service we just added, thus it is OK to ignore this transient error.
if err != nil && !strings.Contains(err.Error(), "/sync-external-service") {
t.Fatal(err)
}
err = client.DeleteExternalService(esID, true)
if err != nil {
t.Fatal(err)
}
// This call should return not found error. Retrying for 5 seconds to wait for async deletion to finish
err = gqltestutil.Retry(5*time.Second, func() error {
_, err = client.UpdateExternalService(gqltestutil.UpdateExternalServiceInput{ID: esID})
if err == nil {
return gqltestutil.ErrContinueRetry
}
return err
})
if err == nil || err == gqltestutil.ErrContinueRetry {
t.Fatal("Deleted service should not be found")
}
if !strings.Contains(err.Error(), "external service not found") {
t.Fatalf("Not found error should be returned, got: %s", err.Error())
}
}
func removeExternalServiceAfterTest(t *testing.T, esID string) {
t.Helper()
t.Cleanup(func() {
err := client.DeleteExternalService(esID, false)
if err != nil {
t.Fatal(err)
}
})
}