This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathvcs_source_test.go
643 lines (554 loc) · 18.4 KB
/
vcs_source_test.go
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gps
import (
"context"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"sync"
"testing"
"github.com/golang/dep/internal/test"
)
// Parent test that executes all the slow vcs interaction tests in parallel.
func TestSlowVcs(t *testing.T) {
t.Run("write-deptree", testWriteDepTree)
t.Run("source-gateway", testSourceGateway)
t.Run("bzr-repo", testBzrRepo)
t.Run("bzr-source", testBzrSourceInteractions)
// TODO(kris-nova) re-enable syn-repo after gps is merged into dep
//t.Run("svn-repo", testSvnRepo)
// TODO(sdboyer) svn-source
t.Run("hg-repo", testHgRepo)
t.Run("hg-source", testHgSourceInteractions)
t.Run("git-repo", testGitRepo)
t.Run("git-source", testGitSourceInteractions)
t.Run("gopkgin-source", testGopkginSourceInteractions)
}
func testGitSourceInteractions(t *testing.T) {
t.Parallel()
// This test is slowish, skip it on -short
if testing.Short() {
t.Skip("Skipping git source version fetching test in short mode")
}
requiresBins(t, "git")
cpath, err := ioutil.TempDir("", "smcache")
if err != nil {
t.Errorf("Failed to create temp dir: %s", err)
}
defer func() {
if err := removeAll(cpath); err != nil {
t.Errorf("removeAll failed: %s", err)
}
}()
n := "github.com/sdboyer/gpkt"
un := "https://" + n
u, err := url.Parse(un)
if err != nil {
t.Fatalf("Error parsing URL %s: %s", un, err)
}
mb := maybeGitSource{
url: u,
}
ctx := context.Background()
superv := newSupervisor(ctx)
isrc, state, err := mb.try(ctx, cpath, newMemoryCache(), superv)
if err != nil {
t.Fatalf("Unexpected error while setting up gitSource for test repo: %s", err)
}
wantstate := sourceIsSetUp | sourceExistsUpstream | sourceHasLatestVersionList
if state != wantstate {
t.Errorf("Expected return state to be %v, got %v", wantstate, state)
}
err = isrc.initLocal(ctx)
if err != nil {
t.Fatalf("Error on cloning git repo: %s", err)
}
src, ok := isrc.(*gitSource)
if !ok {
t.Fatalf("Expected a gitSource, got a %T", isrc)
}
if un != src.upstreamURL() {
t.Errorf("Expected %s as source URL, got %s", un, src.upstreamURL())
}
pvlist, err := src.listVersions(ctx)
if err != nil {
t.Fatalf("Unexpected error getting version pairs from git repo: %s", err)
}
vlist := hidePair(pvlist)
// check that an expected rev is present
is, err := src.revisionPresentIn(Revision("4a54adf81c75375d26d376459c00d5ff9b703e5e"))
if err != nil {
t.Errorf("Unexpected error while checking revision presence: %s", err)
} else if !is {
t.Errorf("Revision that should exist was not present")
}
if len(vlist) != 7 {
t.Errorf("git test repo should've produced seven versions, got %v: vlist was %s", len(vlist), vlist)
} else {
SortForUpgrade(vlist)
evl := []Version{
NewVersion("v2.0.0").Pair(Revision("4a54adf81c75375d26d376459c00d5ff9b703e5e")),
NewVersion("v1.1.0").Pair(Revision("b2cb48dda625f6640b34d9ffb664533359ac8b91")),
NewVersion("v1.0.0").Pair(Revision("bf85021c0405edbc4f3648b0603818d641674f72")),
newDefaultBranch("master").Pair(Revision("bf85021c0405edbc4f3648b0603818d641674f72")),
NewBranch("v1").Pair(Revision("e3777f683305eafca223aefe56b4e8ecf103f467")),
NewBranch("v1.1").Pair(Revision("f1fbc520489a98306eb28c235204e39fa8a89c84")),
NewBranch("v3").Pair(Revision("4a54adf81c75375d26d376459c00d5ff9b703e5e")),
}
if !reflect.DeepEqual(vlist, evl) {
t.Errorf("Version list was not what we expected:\n\t(GOT): %s\n\t(WNT): %s", vlist, evl)
}
}
// recheck that rev is present, this time interacting with cache differently
is, err = src.revisionPresentIn(Revision("30605f6ac35fcb075ad0bfa9296f90a7d891523e"))
if err != nil {
t.Errorf("Unexpected error while re-checking revision presence: %s", err)
} else if !is {
t.Errorf("Revision that should exist was not present on re-check")
}
}
func testGopkginSourceInteractions(t *testing.T) {
t.Parallel()
// This test is slowish, skip it on -short
if testing.Short() {
t.Skip("Skipping gopkg.in source version fetching test in short mode")
}
requiresBins(t, "git")
cpath, err := ioutil.TempDir("", "smcache")
if err != nil {
t.Errorf("Failed to create temp dir: %s", err)
}
defer func() {
if err := removeAll(cpath); err != nil {
t.Errorf("removeAll failed: %s", err)
}
}()
tfunc := func(opath, n string, major uint64, evl []Version) {
un := "https://" + n
u, err := url.Parse(un)
if err != nil {
t.Errorf("URL was bad, lolwut? errtext: %s", err)
return
}
unstable := strings.HasSuffix(opath, gopkgUnstableSuffix)
mb := maybeGopkginSource{
opath: opath,
url: u,
major: major,
unstable: unstable,
}
ctx := context.Background()
superv := newSupervisor(ctx)
isrc, state, err := mb.try(ctx, cpath, newMemoryCache(), superv)
if err != nil {
t.Errorf("Unexpected error while setting up gopkginSource for test repo: %s", err)
return
}
wantstate := sourceIsSetUp | sourceExistsUpstream | sourceHasLatestVersionList
if state != wantstate {
t.Errorf("Expected return state to be %v, got %v", wantstate, state)
}
err = isrc.initLocal(ctx)
if err != nil {
t.Fatalf("Error on cloning git repo: %s", err)
}
src, ok := isrc.(*gopkginSource)
if !ok {
t.Errorf("Expected a gopkginSource, got a %T", isrc)
return
}
if un != src.upstreamURL() {
t.Errorf("Expected %s as source URL, got %s", un, src.upstreamURL())
}
if src.major != major {
t.Errorf("Expected %v as major version filter on gopkginSource, got %v", major, src.major)
}
// check that an expected rev is present
rev := evl[0].(PairedVersion).Revision()
is, err := src.revisionPresentIn(rev)
if err != nil {
t.Errorf("Unexpected error while checking revision presence: %s", err)
} else if !is {
t.Errorf("Revision %s that should exist was not present", rev)
}
pvlist, err := src.listVersions(ctx)
if err != nil {
t.Errorf("Unexpected error getting version pairs from hg repo: %s", err)
}
vlist := hidePair(pvlist)
if len(vlist) != len(evl) {
t.Errorf("gopkgin test repo should've produced %v versions, got %v", len(evl), len(vlist))
} else {
SortForUpgrade(vlist)
if !reflect.DeepEqual(vlist, evl) {
t.Errorf("Version list was not what we expected:\n\t(GOT): %s\n\t(WNT): %s", vlist, evl)
}
}
// Run again, this time to ensure cache outputs correctly
pvlist, err = src.listVersions(ctx)
if err != nil {
t.Errorf("Unexpected error getting version pairs from hg repo: %s", err)
}
vlist = hidePair(pvlist)
if len(vlist) != len(evl) {
t.Errorf("gopkgin test repo should've produced %v versions, got %v", len(evl), len(vlist))
} else {
SortForUpgrade(vlist)
if !reflect.DeepEqual(vlist, evl) {
t.Errorf("Version list was not what we expected:\n\t(GOT): %s\n\t(WNT): %s", vlist, evl)
}
}
// recheck that rev is present, this time interacting with cache differently
is, err = src.revisionPresentIn(rev)
if err != nil {
t.Errorf("Unexpected error while re-checking revision presence: %s", err)
} else if !is {
t.Errorf("Revision that should exist was not present on re-check")
}
}
// simultaneously run for v1, v2, and v3 filters of the target repo
wg := &sync.WaitGroup{}
wg.Add(4)
go func() {
tfunc("gopkg.in/sdboyer/gpkt.v1", "github.com/sdboyer/gpkt", 1, []Version{
NewVersion("v1.1.0").Pair(Revision("b2cb48dda625f6640b34d9ffb664533359ac8b91")),
NewVersion("v1.0.0").Pair(Revision("bf85021c0405edbc4f3648b0603818d641674f72")),
newDefaultBranch("v1.1").Pair(Revision("f1fbc520489a98306eb28c235204e39fa8a89c84")),
NewBranch("v1").Pair(Revision("e3777f683305eafca223aefe56b4e8ecf103f467")),
})
wg.Done()
}()
go func() {
tfunc("gopkg.in/sdboyer/gpkt.v2", "github.com/sdboyer/gpkt", 2, []Version{
NewVersion("v2.0.0").Pair(Revision("4a54adf81c75375d26d376459c00d5ff9b703e5e")),
})
wg.Done()
}()
go func() {
tfunc("gopkg.in/sdboyer/gpkt.v3", "github.com/sdboyer/gpkt", 3, []Version{
newDefaultBranch("v3").Pair(Revision("4a54adf81c75375d26d376459c00d5ff9b703e5e")),
})
wg.Done()
}()
go func() {
tfunc("github.com/sdboyer/gpkt2.v1-unstable", "github.com/sdboyer/gpkt2", 1, []Version{
newDefaultBranch("v1-unstable").Pair(Revision("24de0be8f4a0b8a44321562117749b257bfcef69")),
})
wg.Done()
}()
wg.Wait()
}
func testBzrSourceInteractions(t *testing.T) {
t.Parallel()
// This test is quite slow (ugh bzr), so skip it on -short
if testing.Short() {
t.Skip("Skipping bzr source version fetching test in short mode")
}
requiresBins(t, "bzr")
cpath, err := ioutil.TempDir("", "smcache")
if err != nil {
t.Errorf("Failed to create temp dir: %s", err)
}
defer func() {
if err := removeAll(cpath); err != nil {
t.Errorf("removeAll failed: %s", err)
}
}()
n := "launchpad.net/govcstestbzrrepo"
un := "https://" + n
u, err := url.Parse(un)
if err != nil {
t.Fatalf("Error parsing URL %s: %s", un, err)
}
mb := maybeBzrSource{
url: u,
}
ctx := context.Background()
superv := newSupervisor(ctx)
isrc, state, err := mb.try(ctx, cpath, newMemoryCache(), superv)
if err != nil {
t.Fatalf("Unexpected error while setting up bzrSource for test repo: %s", err)
}
wantstate := sourceIsSetUp | sourceExistsUpstream
if state != wantstate {
t.Errorf("Expected return state to be %v, got %v", wantstate, state)
}
err = isrc.initLocal(ctx)
if err != nil {
t.Fatalf("Error on cloning bzr repo: %s", err)
}
src, ok := isrc.(*bzrSource)
if !ok {
t.Fatalf("Expected a bzrSource, got a %T", isrc)
}
if state != wantstate {
t.Errorf("Expected return state to be %v, got %v", wantstate, state)
}
if un != src.upstreamURL() {
t.Errorf("Expected %s as source URL, got %s", un, src.upstreamURL())
}
evl := []Version{
NewVersion("1.0.0").Pair(Revision("[email protected]")),
newDefaultBranch("(default)").Pair(Revision("[email protected]")),
}
// check that an expected rev is present
is, err := src.revisionPresentIn(Revision("[email protected]"))
if err != nil {
t.Errorf("Unexpected error while checking revision presence: %s", err)
} else if !is {
t.Errorf("Revision that should exist was not present")
}
pvlist, err := src.listVersions(ctx)
if err != nil {
t.Errorf("Unexpected error getting version pairs from bzr repo: %s", err)
}
vlist := hidePair(pvlist)
if len(vlist) != 2 {
t.Errorf("bzr test repo should've produced two versions, got %v", len(vlist))
} else {
SortForUpgrade(vlist)
if !reflect.DeepEqual(vlist, evl) {
t.Errorf("bzr version list was not what we expected:\n\t(GOT): %s\n\t(WNT): %s", vlist, evl)
}
}
// Run again, this time to ensure cache outputs correctly
pvlist, err = src.listVersions(ctx)
if err != nil {
t.Errorf("Unexpected error getting version pairs from bzr repo: %s", err)
}
vlist = hidePair(pvlist)
if len(vlist) != 2 {
t.Errorf("bzr test repo should've produced two versions, got %v", len(vlist))
} else {
SortForUpgrade(vlist)
if !reflect.DeepEqual(vlist, evl) {
t.Errorf("bzr version list was not what we expected:\n\t(GOT): %s\n\t(WNT): %s", vlist, evl)
}
}
// recheck that rev is present, this time interacting with cache differently
is, err = src.revisionPresentIn(Revision("[email protected]"))
if err != nil {
t.Errorf("Unexpected error while re-checking revision presence: %s", err)
} else if !is {
t.Errorf("Revision that should exist was not present on re-check")
}
}
func testHgSourceInteractions(t *testing.T) {
t.Parallel()
// This test is slow, so skip it on -short
if testing.Short() {
t.Skip("Skipping hg source version fetching test in short mode")
}
requiresBins(t, "hg")
cpath, err := ioutil.TempDir("", "smcache")
if err != nil {
t.Errorf("Failed to create temp dir: %s", err)
}
defer func() {
if err := removeAll(cpath); err != nil {
t.Errorf("removeAll failed: %s", err)
}
}()
tfunc := func(n string, evl []Version) {
un := "https://" + n
u, err := url.Parse(un)
if err != nil {
t.Errorf("URL was bad, lolwut? errtext: %s", err)
return
}
mb := maybeHgSource{
url: u,
}
ctx := context.Background()
superv := newSupervisor(ctx)
isrc, state, err := mb.try(ctx, cpath, newMemoryCache(), superv)
if err != nil {
t.Errorf("Unexpected error while setting up hgSource for test repo: %s", err)
return
}
wantstate := sourceIsSetUp | sourceExistsUpstream
if state != wantstate {
t.Errorf("Expected return state to be %v, got %v", wantstate, state)
}
err = isrc.initLocal(ctx)
if err != nil {
t.Fatalf("Error on cloning hg repo: %s", err)
}
src, ok := isrc.(*hgSource)
if !ok {
t.Errorf("Expected a hgSource, got a %T", isrc)
return
}
if state != wantstate {
t.Errorf("Expected return state to be %v, got %v", wantstate, state)
}
if un != src.upstreamURL() {
t.Errorf("Expected %s as source URL, got %s", un, src.upstreamURL())
}
// check that an expected rev is present
is, err := src.revisionPresentIn(Revision("103d1bddef2199c80aad7c42041223083d613ef9"))
if err != nil {
t.Errorf("Unexpected error while checking revision presence: %s", err)
} else if !is {
t.Errorf("Revision that should exist was not present")
}
pvlist, err := src.listVersions(ctx)
if err != nil {
t.Errorf("Unexpected error getting version pairs from hg repo: %s", err)
}
vlist := hidePair(pvlist)
if len(vlist) != len(evl) {
t.Errorf("hg test repo should've produced %v versions, got %v", len(evl), len(vlist))
} else {
SortForUpgrade(vlist)
if !reflect.DeepEqual(vlist, evl) {
t.Errorf("Version list was not what we expected:\n\t(GOT): %s\n\t(WNT): %s", vlist, evl)
}
}
// Run again, this time to ensure cache outputs correctly
pvlist, err = src.listVersions(ctx)
if err != nil {
t.Errorf("Unexpected error getting version pairs from hg repo: %s", err)
}
vlist = hidePair(pvlist)
if len(vlist) != len(evl) {
t.Errorf("hg test repo should've produced %v versions, got %v", len(evl), len(vlist))
} else {
SortForUpgrade(vlist)
if !reflect.DeepEqual(vlist, evl) {
t.Errorf("Version list was not what we expected:\n\t(GOT): %s\n\t(WNT): %s", vlist, evl)
}
}
// recheck that rev is present, this time interacting with cache differently
is, err = src.revisionPresentIn(Revision("103d1bddef2199c80aad7c42041223083d613ef9"))
if err != nil {
t.Errorf("Unexpected error while re-checking revision presence: %s", err)
} else if !is {
t.Errorf("Revision that should exist was not present on re-check")
}
}
// simultaneously run for both the repo with and without the magic bookmark
donech := make(chan struct{})
go func() {
tfunc("bitbucket.org/sdboyer/withbm", []Version{
NewVersion("v1.0.0").Pair(Revision("aa110802a0c64195d0a6c375c9f66668827c90b4")),
newDefaultBranch("@").Pair(Revision("b10d05d581e5401f383e48ccfeb84b48fde99d06")),
NewBranch("another").Pair(Revision("b10d05d581e5401f383e48ccfeb84b48fde99d06")),
NewBranch("default").Pair(Revision("3d466f437f6616da594bbab6446cc1cb4328d1bb")),
NewBranch("newbranch").Pair(Revision("5e2a01be9aee942098e44590ae545c7143da9675")),
})
close(donech)
}()
tfunc("bitbucket.org/sdboyer/nobm", []Version{
NewVersion("v1.0.0").Pair(Revision("aa110802a0c64195d0a6c375c9f66668827c90b4")),
newDefaultBranch("default").Pair(Revision("3d466f437f6616da594bbab6446cc1cb4328d1bb")),
NewBranch("another").Pair(Revision("b10d05d581e5401f383e48ccfeb84b48fde99d06")),
NewBranch("newbranch").Pair(Revision("5e2a01be9aee942098e44590ae545c7143da9675")),
})
<-donech
}
func Test_bzrSource_exportRevisionTo_removeVcsFiles(t *testing.T) {
t.Parallel()
// This test is slow, so skip it on -short
if testing.Short() {
t.Skip("Skipping hg source version fetching test in short mode")
}
requiresBins(t, "bzr")
h := test.NewHelper(t)
defer h.Cleanup()
h.TempDir("smcache")
cpath := h.Path("smcache")
repoPath := filepath.Join(h.Path("."), "repo")
rev := Revision("[email protected]")
n := "launchpad.net/govcstestbzrrepo"
un := "https://" + n
u, err := url.Parse(un)
if err != nil {
t.Errorf("URL was bad, lolwut? errtext: %s", err)
return
}
mb := maybeBzrSource{u}
ctx := context.Background()
superv := newSupervisor(ctx)
isrc, _, err := mb.try(ctx, cpath, newMemoryCache(), superv)
if err != nil {
t.Fatalf("unexpected error while setting up hgSource for test repo: %s", err)
}
err = isrc.initLocal(ctx)
if err != nil {
t.Fatalf("Error on cloning bzr repo: %s", err)
}
src, ok := isrc.(*bzrSource)
if !ok {
t.Fatalf("expected a bzrSource, got a %T", isrc)
}
if err := src.exportRevisionTo(ctx, rev, repoPath); err != nil {
t.Fatalf("unexpected error: %v", err)
}
_, err = os.Stat(filepath.Join(repoPath, ".bzr"))
if err == nil {
t.Fatal("expected .bzr/ to not exists")
} else if !os.IsNotExist(err) {
t.Fatalf("unexpected error: %v", err)
}
}
func Test_hgSource_exportRevisionTo_removeVcsFiles(t *testing.T) {
t.Parallel()
// This test is slow, so skip it on -short
if testing.Short() {
t.Skip("Skipping hg source version fetching test in short mode")
}
requiresBins(t, "hg")
h := test.NewHelper(t)
defer h.Cleanup()
h.TempDir("smcache")
cpath := h.Path("smcache")
repoPath := filepath.Join(h.Path("."), "repo")
rev := Revision("6f55e1f03d91f8a7cce35d1968eb60a2352e4d59")
n := "bitbucket.org/golang-dep/dep-test"
un := "https://" + n
u, err := url.Parse(un)
if err != nil {
t.Errorf("URL was bad, lolwut? errtext: %s", err)
return
}
mb := maybeHgSource{u}
ctx := context.Background()
superv := newSupervisor(ctx)
isrc, _, err := mb.try(ctx, cpath, newMemoryCache(), superv)
if err != nil {
t.Fatalf("unexpected error while setting up hgSource for test repo: %s", err)
}
err = isrc.initLocal(ctx)
if err != nil {
t.Fatalf("Error on cloning hg repo: %s", err)
}
src, ok := isrc.(*hgSource)
if !ok {
t.Fatalf("expected a hgSource, got a %T", isrc)
}
if err := src.exportRevisionTo(ctx, rev, repoPath); err != nil {
t.Fatalf("unexpected error: %v", err)
}
_, err = os.Stat(filepath.Join(repoPath, ".hg"))
if err == nil {
t.Fatal("expected .hg/ to not exists")
} else if !os.IsNotExist(err) {
t.Fatalf("unexpected error: %v", err)
}
}
// Fail a test if the specified binaries aren't installed.
func requiresBins(t *testing.T, bins ...string) {
for _, b := range bins {
_, err := exec.LookPath(b)
if err != nil {
t.Fatalf("%s is not installed", b)
}
}
}