@@ -19,7 +19,6 @@ import (
19
19
"path/filepath"
20
20
"strings"
21
21
"testing"
22
- "time"
23
22
24
23
"github.com/coder/envbuilder"
25
24
"github.com/coder/envbuilder/devcontainer/features"
@@ -32,8 +31,6 @@ import (
32
31
"github.com/docker/docker/client"
33
32
"github.com/docker/docker/pkg/stdcopy"
34
33
"github.com/go-git/go-billy/v5/memfs"
35
- "github.com/go-git/go-git/v5"
36
- "github.com/go-git/go-git/v5/plumbing/object"
37
34
"github.com/google/go-containerregistry/pkg/authn"
38
35
"github.com/google/go-containerregistry/pkg/name"
39
36
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
@@ -49,50 +46,50 @@ const (
49
46
50
47
func TestFailsGitAuth (t * testing.T ) {
51
48
t .Parallel ()
52
- url := createGitServer (t , gitServerOptions {
49
+ srv := createGitServer (t , gitServerOptions {
53
50
files : map [string ]string {
54
51
"Dockerfile" : "FROM " + testImageAlpine ,
55
52
},
56
53
username : "kyle" ,
57
54
password : "testing" ,
58
55
})
59
56
_ , err := runEnvbuilder (t , options {env : []string {
60
- "GIT_URL=" + url ,
57
+ "GIT_URL=" + srv . URL ,
61
58
}})
62
59
require .ErrorContains (t , err , "authentication required" )
63
60
}
64
61
65
62
func TestSucceedsGitAuth (t * testing.T ) {
66
63
t .Parallel ()
67
- url := createGitServer (t , gitServerOptions {
64
+ srv := createGitServer (t , gitServerOptions {
68
65
files : map [string ]string {
69
66
"Dockerfile" : "FROM " + testImageAlpine ,
70
67
},
71
68
username : "kyle" ,
72
69
password : "testing" ,
73
70
})
74
71
ctr , err := runEnvbuilder (t , options {env : []string {
75
- "GIT_URL=" + url ,
72
+ "GIT_URL=" + srv . URL ,
76
73
"DOCKERFILE_PATH=Dockerfile" ,
77
74
"GIT_USERNAME=kyle" ,
78
75
"GIT_PASSWORD=testing" ,
79
76
}})
80
77
require .NoError (t , err )
81
78
gitConfig := execContainer (t , ctr , "cat /workspaces/.git/config" )
82
- require .Contains (t , gitConfig , url )
79
+ require .Contains (t , gitConfig , srv . URL )
83
80
}
84
81
85
82
func TestSucceedsGitAuthInURL (t * testing.T ) {
86
83
t .Parallel ()
87
- gitURL := createGitServer (t , gitServerOptions {
84
+ srv := createGitServer (t , gitServerOptions {
88
85
files : map [string ]string {
89
86
"Dockerfile" : "FROM " + testImageAlpine ,
90
87
},
91
88
username : "kyle" ,
92
89
password : "testing" ,
93
90
})
94
91
95
- u , err := url .Parse (gitURL )
92
+ u , err := url .Parse (srv . URL )
96
93
require .NoError (t , err )
97
94
u .User = url .UserPassword ("kyle" , "testing" )
98
95
ctr , err := runEnvbuilder (t , options {env : []string {
@@ -149,7 +146,7 @@ func TestBuildFromDevcontainerWithFeatures(t *testing.T) {
149
146
require .NoError (t , err )
150
147
151
148
// Ensures that a Git repository with a devcontainer.json is cloned and built.
152
- url := createGitServer (t , gitServerOptions {
149
+ srv := createGitServer (t , gitServerOptions {
153
150
files : map [string ]string {
154
151
".devcontainer/devcontainer.json" : `{
155
152
"name": "Test",
@@ -174,7 +171,7 @@ func TestBuildFromDevcontainerWithFeatures(t *testing.T) {
174
171
},
175
172
})
176
173
ctr , err := runEnvbuilder (t , options {env : []string {
177
- "GIT_URL=" + url ,
174
+ "GIT_URL=" + srv . URL ,
178
175
}})
179
176
require .NoError (t , err )
180
177
@@ -190,13 +187,13 @@ func TestBuildFromDevcontainerWithFeatures(t *testing.T) {
190
187
191
188
func TestBuildFromDockerfile (t * testing.T ) {
192
189
// Ensures that a Git repository with a Dockerfile is cloned and built.
193
- url := createGitServer (t , gitServerOptions {
190
+ srv := createGitServer (t , gitServerOptions {
194
191
files : map [string ]string {
195
192
"Dockerfile" : "FROM " + testImageAlpine ,
196
193
},
197
194
})
198
195
ctr , err := runEnvbuilder (t , options {env : []string {
199
- "GIT_URL=" + url ,
196
+ "GIT_URL=" + srv . URL ,
200
197
"DOCKERFILE_PATH=Dockerfile" ,
201
198
}})
202
199
require .NoError (t , err )
@@ -207,13 +204,13 @@ func TestBuildFromDockerfile(t *testing.T) {
207
204
208
205
func TestBuildPrintBuildOutput (t * testing.T ) {
209
206
// Ensures that a Git repository with a Dockerfile is cloned and built.
210
- url := createGitServer (t , gitServerOptions {
207
+ srv := createGitServer (t , gitServerOptions {
211
208
files : map [string ]string {
212
209
"Dockerfile" : "FROM " + testImageAlpine + "\n RUN echo hello" ,
213
210
},
214
211
})
215
212
ctr , err := runEnvbuilder (t , options {env : []string {
216
- "GIT_URL=" + url ,
213
+ "GIT_URL=" + srv . URL ,
217
214
"DOCKERFILE_PATH=Dockerfile" ,
218
215
}})
219
216
require .NoError (t , err )
@@ -235,7 +232,7 @@ func TestBuildPrintBuildOutput(t *testing.T) {
235
232
236
233
func TestBuildIgnoreVarRunSecrets (t * testing.T ) {
237
234
// Ensures that a Git repository with a Dockerfile is cloned and built.
238
- url := createGitServer (t , gitServerOptions {
235
+ srv := createGitServer (t , gitServerOptions {
239
236
files : map [string ]string {
240
237
"Dockerfile" : "FROM " + testImageAlpine ,
241
238
},
@@ -245,7 +242,7 @@ func TestBuildIgnoreVarRunSecrets(t *testing.T) {
245
242
require .NoError (t , err )
246
243
ctr , err := runEnvbuilder (t , options {
247
244
env : []string {
248
- "GIT_URL=" + url ,
245
+ "GIT_URL=" + srv . URL ,
249
246
"DOCKERFILE_PATH=Dockerfile" ,
250
247
},
251
248
binds : []string {fmt .Sprintf ("%s:/var/run/secrets" , dir )},
@@ -258,13 +255,13 @@ func TestBuildIgnoreVarRunSecrets(t *testing.T) {
258
255
259
256
func TestBuildWithSetupScript (t * testing.T ) {
260
257
// Ensures that a Git repository with a Dockerfile is cloned and built.
261
- url := createGitServer (t , gitServerOptions {
258
+ srv := createGitServer (t , gitServerOptions {
262
259
files : map [string ]string {
263
260
"Dockerfile" : "FROM " + testImageAlpine ,
264
261
},
265
262
})
266
263
ctr , err := runEnvbuilder (t , options {env : []string {
267
- "GIT_URL=" + url ,
264
+ "GIT_URL=" + srv . URL ,
268
265
"DOCKERFILE_PATH=Dockerfile" ,
269
266
"SETUP_SCRIPT=echo \" INIT_ARGS=-c 'echo hi > /wow && sleep infinity'\" >> $ENVBUILDER_ENV" ,
270
267
}})
@@ -278,7 +275,7 @@ func TestBuildFromDevcontainerInCustomPath(t *testing.T) {
278
275
t .Parallel ()
279
276
280
277
// Ensures that a Git repository with a devcontainer.json is cloned and built.
281
- url := createGitServer (t , gitServerOptions {
278
+ srv := createGitServer (t , gitServerOptions {
282
279
files : map [string ]string {
283
280
".devcontainer/custom/devcontainer.json" : `{
284
281
"name": "Test",
@@ -290,7 +287,7 @@ func TestBuildFromDevcontainerInCustomPath(t *testing.T) {
290
287
},
291
288
})
292
289
ctr , err := runEnvbuilder (t , options {env : []string {
293
- "GIT_URL=" + url ,
290
+ "GIT_URL=" + srv . URL ,
294
291
"DEVCONTAINER_DIR=.devcontainer/custom" ,
295
292
}})
296
293
require .NoError (t , err )
@@ -303,7 +300,7 @@ func TestBuildFromDevcontainerInSubfolder(t *testing.T) {
303
300
t .Parallel ()
304
301
305
302
// Ensures that a Git repository with a devcontainer.json is cloned and built.
306
- url := createGitServer (t , gitServerOptions {
303
+ srv := createGitServer (t , gitServerOptions {
307
304
files : map [string ]string {
308
305
".devcontainer/subfolder/devcontainer.json" : `{
309
306
"name": "Test",
@@ -315,7 +312,7 @@ func TestBuildFromDevcontainerInSubfolder(t *testing.T) {
315
312
},
316
313
})
317
314
ctr , err := runEnvbuilder (t , options {env : []string {
318
- "GIT_URL=" + url ,
315
+ "GIT_URL=" + srv . URL ,
319
316
}})
320
317
require .NoError (t , err )
321
318
@@ -326,7 +323,7 @@ func TestBuildFromDevcontainerInRoot(t *testing.T) {
326
323
t .Parallel ()
327
324
328
325
// Ensures that a Git repository with a devcontainer.json is cloned and built.
329
- url := createGitServer (t , gitServerOptions {
326
+ srv := createGitServer (t , gitServerOptions {
330
327
files : map [string ]string {
331
328
"devcontainer.json" : `{
332
329
"name": "Test",
@@ -338,7 +335,7 @@ func TestBuildFromDevcontainerInRoot(t *testing.T) {
338
335
},
339
336
})
340
337
ctr , err := runEnvbuilder (t , options {env : []string {
341
- "GIT_URL=" + url ,
338
+ "GIT_URL=" + srv . URL ,
342
339
}})
343
340
require .NoError (t , err )
344
341
@@ -347,11 +344,12 @@ func TestBuildFromDevcontainerInRoot(t *testing.T) {
347
344
}
348
345
349
346
func TestBuildCustomCertificates (t * testing.T ) {
350
- srv := httptest . NewTLSServer ( createGitHandler (t , gitServerOptions {
347
+ srv := createGitServer (t , gitServerOptions {
351
348
files : map [string ]string {
352
349
"Dockerfile" : "FROM " + testImageAlpine ,
353
350
},
354
- }))
351
+ tls : true ,
352
+ })
355
353
ctr , err := runEnvbuilder (t , options {env : []string {
356
354
"GIT_URL=" + srv .URL ,
357
355
"DOCKERFILE_PATH=Dockerfile" ,
@@ -368,13 +366,13 @@ func TestBuildCustomCertificates(t *testing.T) {
368
366
369
367
func TestBuildStopStartCached (t * testing.T ) {
370
368
// Ensures that a Git repository with a Dockerfile is cloned and built.
371
- url := createGitServer (t , gitServerOptions {
369
+ srv := createGitServer (t , gitServerOptions {
372
370
files : map [string ]string {
373
371
"Dockerfile" : "FROM " + testImageAlpine ,
374
372
},
375
373
})
376
374
ctr , err := runEnvbuilder (t , options {env : []string {
377
- "GIT_URL=" + url ,
375
+ "GIT_URL=" + srv . URL ,
378
376
"DOCKERFILE_PATH=Dockerfile" ,
379
377
"SKIP_REBUILD=true" ,
380
378
}})
@@ -416,13 +414,13 @@ func TestBuildFailsFallback(t *testing.T) {
416
414
t .Run ("BadDockerfile" , func (t * testing.T ) {
417
415
t .Parallel ()
418
416
// Ensures that a Git repository with a Dockerfile is cloned and built.
419
- url := createGitServer (t , gitServerOptions {
417
+ srv := createGitServer (t , gitServerOptions {
420
418
files : map [string ]string {
421
419
"Dockerfile" : "bad syntax" ,
422
420
},
423
421
})
424
422
_ , err := runEnvbuilder (t , options {env : []string {
425
- "GIT_URL=" + url ,
423
+ "GIT_URL=" + srv . URL ,
426
424
"DOCKERFILE_PATH=Dockerfile" ,
427
425
}})
428
426
require .ErrorContains (t , err , envbuilder .ErrNoFallbackImage .Error ())
@@ -431,40 +429,40 @@ func TestBuildFailsFallback(t *testing.T) {
431
429
t .Run ("FailsBuild" , func (t * testing.T ) {
432
430
t .Parallel ()
433
431
// Ensures that a Git repository with a Dockerfile is cloned and built.
434
- url := createGitServer (t , gitServerOptions {
432
+ srv := createGitServer (t , gitServerOptions {
435
433
files : map [string ]string {
436
434
"Dockerfile" : `FROM ` + testImageAlpine + `
437
435
RUN exit 1` ,
438
436
},
439
437
})
440
438
_ , err := runEnvbuilder (t , options {env : []string {
441
- "GIT_URL=" + url ,
439
+ "GIT_URL=" + srv . URL ,
442
440
"DOCKERFILE_PATH=Dockerfile" ,
443
441
}})
444
442
require .ErrorContains (t , err , envbuilder .ErrNoFallbackImage .Error ())
445
443
})
446
444
t .Run ("BadDevcontainer" , func (t * testing.T ) {
447
445
t .Parallel ()
448
446
// Ensures that a Git repository with a Dockerfile is cloned and built.
449
- url := createGitServer (t , gitServerOptions {
447
+ srv := createGitServer (t , gitServerOptions {
450
448
files : map [string ]string {
451
449
".devcontainer/devcontainer.json" : "not json" ,
452
450
},
453
451
})
454
452
_ , err := runEnvbuilder (t , options {env : []string {
455
- "GIT_URL=" + url ,
453
+ "GIT_URL=" + srv . URL ,
456
454
}})
457
455
require .ErrorContains (t , err , envbuilder .ErrNoFallbackImage .Error ())
458
456
})
459
457
t .Run ("NoImageOrDockerfile" , func (t * testing.T ) {
460
458
t .Parallel ()
461
- url := createGitServer (t , gitServerOptions {
459
+ srv := createGitServer (t , gitServerOptions {
462
460
files : map [string ]string {
463
461
".devcontainer/devcontainer.json" : "{}" ,
464
462
},
465
463
})
466
464
ctr , err := runEnvbuilder (t , options {env : []string {
467
- "GIT_URL=" + url ,
465
+ "GIT_URL=" + srv . URL ,
468
466
"FALLBACK_IMAGE=" + testImageAlpine ,
469
467
}})
470
468
require .NoError (t , err )
@@ -476,13 +474,13 @@ RUN exit 1`,
476
474
477
475
func TestExitBuildOnFailure (t * testing.T ) {
478
476
t .Parallel ()
479
- url := createGitServer (t , gitServerOptions {
477
+ srv := createGitServer (t , gitServerOptions {
480
478
files : map [string ]string {
481
479
"Dockerfile" : "bad syntax" ,
482
480
},
483
481
})
484
482
_ , err := runEnvbuilder (t , options {env : []string {
485
- "GIT_URL=" + url ,
483
+ "GIT_URL=" + srv . URL ,
486
484
"DOCKERFILE_PATH=Dockerfile" ,
487
485
"FALLBACK_IMAGE=" + testImageAlpine ,
488
486
// Ensures that the fallback doesn't work when an image is specified.
@@ -495,7 +493,7 @@ func TestContainerEnv(t *testing.T) {
495
493
t .Parallel ()
496
494
497
495
// Ensures that a Git repository with a devcontainer.json is cloned and built.
498
- url := createGitServer (t , gitServerOptions {
496
+ srv := createGitServer (t , gitServerOptions {
499
497
files : map [string ]string {
500
498
".devcontainer/devcontainer.json" : `{
501
499
"name": "Test",
@@ -516,7 +514,7 @@ func TestContainerEnv(t *testing.T) {
516
514
},
517
515
})
518
516
ctr , err := runEnvbuilder (t , options {env : []string {
519
- "GIT_URL=" + url ,
517
+ "GIT_URL=" + srv . URL ,
520
518
"EXPORT_ENV_FILE=/env" ,
521
519
}})
522
520
require .NoError (t , err )
@@ -534,7 +532,7 @@ func TestLifecycleScripts(t *testing.T) {
534
532
t .Parallel ()
535
533
536
534
// Ensures that a Git repository with a devcontainer.json is cloned and built.
537
- url := createGitServer (t , gitServerOptions {
535
+ srv := createGitServer (t , gitServerOptions {
538
536
files : map [string ]string {
539
537
".devcontainer/devcontainer.json" : `{
540
538
"name": "Test",
@@ -553,7 +551,7 @@ func TestLifecycleScripts(t *testing.T) {
553
551
},
554
552
})
555
553
ctr , err := runEnvbuilder (t , options {env : []string {
556
- "GIT_URL=" + url ,
554
+ "GIT_URL=" + srv . URL ,
557
555
}})
558
556
require .NoError (t , err )
559
557
@@ -570,7 +568,7 @@ func TestPostStartScript(t *testing.T) {
570
568
t .Parallel ()
571
569
572
570
// Ensures that a Git repository with a devcontainer.json is cloned and built.
573
- url := createGitServer (t , gitServerOptions {
571
+ srv := createGitServer (t , gitServerOptions {
574
572
files : map [string ]string {
575
573
".devcontainer/devcontainer.json" : `{
576
574
"name": "Test",
@@ -592,7 +590,7 @@ USER nobody`,
592
590
},
593
591
})
594
592
ctr , err := runEnvbuilder (t , options {env : []string {
595
- "GIT_URL=" + url ,
593
+ "GIT_URL=" + srv . URL ,
596
594
"POST_START_SCRIPT_PATH=/tmp/post-start.sh" ,
597
595
"INIT_COMMAND=/bin/init.sh" ,
598
596
}})
@@ -620,13 +618,13 @@ func TestPrivateRegistry(t *testing.T) {
620
618
})
621
619
622
620
// Ensures that a Git repository with a Dockerfile is cloned and built.
623
- url := createGitServer (t , gitServerOptions {
621
+ srv := createGitServer (t , gitServerOptions {
624
622
files : map [string ]string {
625
623
"Dockerfile" : "FROM " + image ,
626
624
},
627
625
})
628
626
_ , err := runEnvbuilder (t , options {env : []string {
629
- "GIT_URL=" + url ,
627
+ "GIT_URL=" + srv . URL ,
630
628
"DOCKERFILE_PATH=Dockerfile" ,
631
629
}})
632
630
require .ErrorContains (t , err , "Unauthorized" )
@@ -639,7 +637,7 @@ func TestPrivateRegistry(t *testing.T) {
639
637
})
640
638
641
639
// Ensures that a Git repository with a Dockerfile is cloned and built.
642
- url := createGitServer (t , gitServerOptions {
640
+ srv := createGitServer (t , gitServerOptions {
643
641
files : map [string ]string {
644
642
"Dockerfile" : "FROM " + image ,
645
643
},
@@ -655,7 +653,7 @@ func TestPrivateRegistry(t *testing.T) {
655
653
require .NoError (t , err )
656
654
657
655
_ , err = runEnvbuilder (t , options {env : []string {
658
- "GIT_URL=" + url ,
656
+ "GIT_URL=" + srv . URL ,
659
657
"DOCKERFILE_PATH=Dockerfile" ,
660
658
"DOCKER_CONFIG_BASE64=" + base64 .StdEncoding .EncodeToString (config ),
661
659
}})
@@ -671,7 +669,7 @@ func TestPrivateRegistry(t *testing.T) {
671
669
})
672
670
673
671
// Ensures that a Git repository with a Dockerfile is cloned and built.
674
- url := createGitServer (t , gitServerOptions {
672
+ srv := createGitServer (t , gitServerOptions {
675
673
files : map [string ]string {
676
674
"Dockerfile" : "FROM " + image ,
677
675
},
@@ -687,7 +685,7 @@ func TestPrivateRegistry(t *testing.T) {
687
685
require .NoError (t , err )
688
686
689
687
_ , err = runEnvbuilder (t , options {env : []string {
690
- "GIT_URL=" + url ,
688
+ "GIT_URL=" + srv . URL ,
691
689
"DOCKERFILE_PATH=Dockerfile" ,
692
690
"DOCKER_CONFIG_BASE64=" + base64 .StdEncoding .EncodeToString (config ),
693
691
}})
@@ -810,11 +808,11 @@ COPY %s .`, testImageAlpine, inclFile)
810
808
tc := tc
811
809
812
810
t .Run (tc .name , func (t * testing.T ) {
813
- url := createGitServer (t , gitServerOptions {
811
+ srv := createGitServer (t , gitServerOptions {
814
812
files : tc .files ,
815
813
})
816
814
_ , err := runEnvbuilder (t , options {env : []string {
817
- "GIT_URL=" + url ,
815
+ "GIT_URL=" + srv . URL ,
818
816
"DOCKERFILE_PATH=" + tc .dockerfilePath ,
819
817
"BUILD_CONTEXT_PATH=" + tc .buildContextPath ,
820
818
}})
@@ -858,41 +856,26 @@ type gitServerOptions struct {
858
856
username string
859
857
password string
860
858
authMW func (http.Handler ) http.Handler
859
+ tls bool
861
860
}
862
861
863
862
// createGitServer creates a git repository with an in-memory filesystem
864
863
// and serves it over HTTP using a httptest.Server.
865
- func createGitServer (t * testing.T , opts gitServerOptions ) string {
864
+ func createGitServer (t * testing.T , opts gitServerOptions ) * httptest. Server {
866
865
t .Helper ()
867
866
if opts .authMW == nil {
868
867
opts .authMW = gittest .BasicAuthMW (opts .username , opts .password )
869
868
}
870
- srv := httptest .NewServer (opts .authMW (createGitHandler (t , opts )))
871
- return srv .URL
872
- }
873
-
874
- func createGitHandler (t * testing.T , opts gitServerOptions ) http.Handler {
875
- t .Helper ()
869
+ commits := make ([]gittest.CommitFunc , 0 )
870
+ for path , content := range opts .files {
871
+ commits = append (commits , gittest .Commit (t , path , content , "my test commit" ))
872
+ }
876
873
fs := memfs .New ()
877
- repo := gittest .NewRepo (t , fs )
878
- w , err := repo .Worktree ()
879
- require .NoError (t , err )
880
- for key , value := range opts .files {
881
- gittest .WriteFile (t , fs , key , value )
882
- _ , err = w .Add (key )
883
- require .NoError (t , err )
874
+ _ = gittest .NewRepo (t , fs , commits ... )
875
+ if opts .tls {
876
+ return httptest .NewTLSServer (opts .authMW (gittest .NewServer (fs )))
884
877
}
885
- commit , err := w .Commit ("my test commit" , & git.CommitOptions {
886
- Author : & object.Signature {
887
- Name : "Example" ,
888
- Email : "in@tests.com" ,
889
- When : time .Now (),
890
- },
891
- })
892
- require .NoError (t , err )
893
- _ , err = repo .CommitObject (commit )
894
- require .NoError (t , err )
895
- return gittest .NewServer (fs )
878
+ return httptest .NewServer (opts .authMW (gittest .NewServer (fs )))
896
879
}
897
880
898
881
// cleanOldEnvbuilders removes any old envbuilder containers.
0 commit comments