Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f828b17

Browse files
committedApr 25, 2024
chore: refactor: add helpers to create commits
1 parent dcc3677 commit f828b17

File tree

3 files changed

+112
-115
lines changed

3 files changed

+112
-115
lines changed
 

‎git_test.go

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@ import (
99
"os"
1010
"regexp"
1111
"testing"
12-
"time"
1312

1413
"github.com/coder/envbuilder"
1514
"github.com/coder/envbuilder/testutil/gittest"
1615
"github.com/go-git/go-billy/v5"
1716
"github.com/go-git/go-billy/v5/memfs"
18-
"github.com/go-git/go-git/v5"
19-
"github.com/go-git/go-git/v5/plumbing/object"
2017
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
2118
"github.com/stretchr/testify/require"
2219
)
@@ -77,13 +74,16 @@ func TestCloneRepo(t *testing.T) {
7774

7875
// We do not overwrite a repo if one is already present.
7976
t.Run("AlreadyCloned", func(t *testing.T) {
80-
srvURL := setupGit(t, tc.srvUsername, tc.srvPassword)
77+
srvFS := memfs.New()
78+
_ = gittest.NewRepo(t, srvFS, gittest.Commit(t, "README.md", "Hello, world!", "Wow!"))
79+
authMW := gittest.BasicAuthMW(tc.srvUsername, tc.srvPassword)
80+
srv := httptest.NewServer(authMW(gittest.NewServer(srvFS)))
8181
clientFS := memfs.New()
8282
// A repo already exists!
8383
_ = gittest.NewRepo(t, clientFS)
8484
cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{
8585
Path: "/",
86-
RepoURL: srvURL,
86+
RepoURL: srv.URL,
8787
Storage: clientFS,
8888
})
8989
require.NoError(t, err)
@@ -93,12 +93,15 @@ func TestCloneRepo(t *testing.T) {
9393
// Basic Auth
9494
t.Run("BasicAuth", func(t *testing.T) {
9595
t.Parallel()
96-
srvURL := setupGit(t, tc.srvUsername, tc.srvPassword)
96+
srvFS := memfs.New()
97+
_ = gittest.NewRepo(t, srvFS, gittest.Commit(t, "README.md", "Hello, world!", "Wow!"))
98+
authMW := gittest.BasicAuthMW(tc.srvUsername, tc.srvPassword)
99+
srv := httptest.NewServer(authMW(gittest.NewServer(srvFS)))
97100
clientFS := memfs.New()
98101

99102
cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{
100103
Path: "/workspace",
101-
RepoURL: srvURL,
104+
RepoURL: srv.URL,
102105
Storage: clientFS,
103106
RepoAuth: &githttp.BasicAuth{
104107
Username: tc.username,
@@ -117,14 +120,18 @@ func TestCloneRepo(t *testing.T) {
117120
require.Equal(t, "Hello, world!", readme)
118121
gitConfig := mustRead(t, clientFS, "/workspace/.git/config")
119122
// Ensure we do not modify the git URL that folks pass in.
120-
require.Regexp(t, fmt.Sprintf(`(?m)^\s+url\s+=\s+%s\s*$`, regexp.QuoteMeta(srvURL)), gitConfig)
123+
require.Regexp(t, fmt.Sprintf(`(?m)^\s+url\s+=\s+%s\s*$`, regexp.QuoteMeta(srv.URL)), gitConfig)
121124
})
122125

123126
// In-URL-style auth e.g. http://user:password@host:port
124127
t.Run("InURL", func(t *testing.T) {
125128
t.Parallel()
126-
srvURL := setupGit(t, tc.srvUsername, tc.srvPassword)
127-
authURL, err := url.Parse(srvURL)
129+
srvFS := memfs.New()
130+
_ = gittest.NewRepo(t, srvFS, gittest.Commit(t, "README.md", "Hello, world!", "Wow!"))
131+
authMW := gittest.BasicAuthMW(tc.srvUsername, tc.srvPassword)
132+
srv := httptest.NewServer(authMW(gittest.NewServer(srvFS)))
133+
134+
authURL, err := url.Parse(srv.URL)
128135
require.NoError(t, err)
129136
authURL.User = url.UserPassword(tc.username, tc.password)
130137
clientFS := memfs.New()
@@ -160,28 +167,3 @@ func mustRead(t *testing.T, fs billy.Filesystem, path string) string {
160167
require.NoError(t, err)
161168
return string(content)
162169
}
163-
164-
func setupGit(t *testing.T, user, pass string) (url string) {
165-
serverFS := memfs.New()
166-
repo := gittest.NewRepo(t, serverFS)
167-
tree, err := repo.Worktree()
168-
require.NoError(t, err)
169-
170-
gittest.WriteFile(t, serverFS, "README.md", "Hello, world!")
171-
_, err = tree.Add("README.md")
172-
require.NoError(t, err)
173-
commit, err := tree.Commit("Wow!", &git.CommitOptions{
174-
Author: &object.Signature{
175-
Name: "Example",
176-
Email: "in@tests.com",
177-
When: time.Now(),
178-
},
179-
})
180-
require.NoError(t, err)
181-
_, err = repo.CommitObject(commit)
182-
require.NoError(t, err)
183-
184-
authMW := gittest.BasicAuthMW(user, pass)
185-
srv := httptest.NewServer(authMW(gittest.NewServer(serverFS)))
186-
return srv.URL
187-
}

‎integration/integration_test.go

Lines changed: 62 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"path/filepath"
2020
"strings"
2121
"testing"
22-
"time"
2322

2423
"github.com/coder/envbuilder"
2524
"github.com/coder/envbuilder/devcontainer/features"
@@ -32,8 +31,6 @@ import (
3231
"github.com/docker/docker/client"
3332
"github.com/docker/docker/pkg/stdcopy"
3433
"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"
3734
"github.com/google/go-containerregistry/pkg/authn"
3835
"github.com/google/go-containerregistry/pkg/name"
3936
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
@@ -49,50 +46,50 @@ const (
4946

5047
func TestFailsGitAuth(t *testing.T) {
5148
t.Parallel()
52-
url := createGitServer(t, gitServerOptions{
49+
srv := createGitServer(t, gitServerOptions{
5350
files: map[string]string{
5451
"Dockerfile": "FROM " + testImageAlpine,
5552
},
5653
username: "kyle",
5754
password: "testing",
5855
})
5956
_, err := runEnvbuilder(t, options{env: []string{
60-
"GIT_URL=" + url,
57+
"GIT_URL=" + srv.URL,
6158
}})
6259
require.ErrorContains(t, err, "authentication required")
6360
}
6461

6562
func TestSucceedsGitAuth(t *testing.T) {
6663
t.Parallel()
67-
url := createGitServer(t, gitServerOptions{
64+
srv := createGitServer(t, gitServerOptions{
6865
files: map[string]string{
6966
"Dockerfile": "FROM " + testImageAlpine,
7067
},
7168
username: "kyle",
7269
password: "testing",
7370
})
7471
ctr, err := runEnvbuilder(t, options{env: []string{
75-
"GIT_URL=" + url,
72+
"GIT_URL=" + srv.URL,
7673
"DOCKERFILE_PATH=Dockerfile",
7774
"GIT_USERNAME=kyle",
7875
"GIT_PASSWORD=testing",
7976
}})
8077
require.NoError(t, err)
8178
gitConfig := execContainer(t, ctr, "cat /workspaces/.git/config")
82-
require.Contains(t, gitConfig, url)
79+
require.Contains(t, gitConfig, srv.URL)
8380
}
8481

8582
func TestSucceedsGitAuthInURL(t *testing.T) {
8683
t.Parallel()
87-
gitURL := createGitServer(t, gitServerOptions{
84+
srv := createGitServer(t, gitServerOptions{
8885
files: map[string]string{
8986
"Dockerfile": "FROM " + testImageAlpine,
9087
},
9188
username: "kyle",
9289
password: "testing",
9390
})
9491

95-
u, err := url.Parse(gitURL)
92+
u, err := url.Parse(srv.URL)
9693
require.NoError(t, err)
9794
u.User = url.UserPassword("kyle", "testing")
9895
ctr, err := runEnvbuilder(t, options{env: []string{
@@ -149,7 +146,7 @@ func TestBuildFromDevcontainerWithFeatures(t *testing.T) {
149146
require.NoError(t, err)
150147

151148
// Ensures that a Git repository with a devcontainer.json is cloned and built.
152-
url := createGitServer(t, gitServerOptions{
149+
srv := createGitServer(t, gitServerOptions{
153150
files: map[string]string{
154151
".devcontainer/devcontainer.json": `{
155152
"name": "Test",
@@ -174,7 +171,7 @@ func TestBuildFromDevcontainerWithFeatures(t *testing.T) {
174171
},
175172
})
176173
ctr, err := runEnvbuilder(t, options{env: []string{
177-
"GIT_URL=" + url,
174+
"GIT_URL=" + srv.URL,
178175
}})
179176
require.NoError(t, err)
180177

@@ -190,13 +187,13 @@ func TestBuildFromDevcontainerWithFeatures(t *testing.T) {
190187

191188
func TestBuildFromDockerfile(t *testing.T) {
192189
// Ensures that a Git repository with a Dockerfile is cloned and built.
193-
url := createGitServer(t, gitServerOptions{
190+
srv := createGitServer(t, gitServerOptions{
194191
files: map[string]string{
195192
"Dockerfile": "FROM " + testImageAlpine,
196193
},
197194
})
198195
ctr, err := runEnvbuilder(t, options{env: []string{
199-
"GIT_URL=" + url,
196+
"GIT_URL=" + srv.URL,
200197
"DOCKERFILE_PATH=Dockerfile",
201198
}})
202199
require.NoError(t, err)
@@ -207,13 +204,13 @@ func TestBuildFromDockerfile(t *testing.T) {
207204

208205
func TestBuildPrintBuildOutput(t *testing.T) {
209206
// Ensures that a Git repository with a Dockerfile is cloned and built.
210-
url := createGitServer(t, gitServerOptions{
207+
srv := createGitServer(t, gitServerOptions{
211208
files: map[string]string{
212209
"Dockerfile": "FROM " + testImageAlpine + "\nRUN echo hello",
213210
},
214211
})
215212
ctr, err := runEnvbuilder(t, options{env: []string{
216-
"GIT_URL=" + url,
213+
"GIT_URL=" + srv.URL,
217214
"DOCKERFILE_PATH=Dockerfile",
218215
}})
219216
require.NoError(t, err)
@@ -235,7 +232,7 @@ func TestBuildPrintBuildOutput(t *testing.T) {
235232

236233
func TestBuildIgnoreVarRunSecrets(t *testing.T) {
237234
// Ensures that a Git repository with a Dockerfile is cloned and built.
238-
url := createGitServer(t, gitServerOptions{
235+
srv := createGitServer(t, gitServerOptions{
239236
files: map[string]string{
240237
"Dockerfile": "FROM " + testImageAlpine,
241238
},
@@ -245,7 +242,7 @@ func TestBuildIgnoreVarRunSecrets(t *testing.T) {
245242
require.NoError(t, err)
246243
ctr, err := runEnvbuilder(t, options{
247244
env: []string{
248-
"GIT_URL=" + url,
245+
"GIT_URL=" + srv.URL,
249246
"DOCKERFILE_PATH=Dockerfile",
250247
},
251248
binds: []string{fmt.Sprintf("%s:/var/run/secrets", dir)},
@@ -258,13 +255,13 @@ func TestBuildIgnoreVarRunSecrets(t *testing.T) {
258255

259256
func TestBuildWithSetupScript(t *testing.T) {
260257
// Ensures that a Git repository with a Dockerfile is cloned and built.
261-
url := createGitServer(t, gitServerOptions{
258+
srv := createGitServer(t, gitServerOptions{
262259
files: map[string]string{
263260
"Dockerfile": "FROM " + testImageAlpine,
264261
},
265262
})
266263
ctr, err := runEnvbuilder(t, options{env: []string{
267-
"GIT_URL=" + url,
264+
"GIT_URL=" + srv.URL,
268265
"DOCKERFILE_PATH=Dockerfile",
269266
"SETUP_SCRIPT=echo \"INIT_ARGS=-c 'echo hi > /wow && sleep infinity'\" >> $ENVBUILDER_ENV",
270267
}})
@@ -278,7 +275,7 @@ func TestBuildFromDevcontainerInCustomPath(t *testing.T) {
278275
t.Parallel()
279276

280277
// Ensures that a Git repository with a devcontainer.json is cloned and built.
281-
url := createGitServer(t, gitServerOptions{
278+
srv := createGitServer(t, gitServerOptions{
282279
files: map[string]string{
283280
".devcontainer/custom/devcontainer.json": `{
284281
"name": "Test",
@@ -290,7 +287,7 @@ func TestBuildFromDevcontainerInCustomPath(t *testing.T) {
290287
},
291288
})
292289
ctr, err := runEnvbuilder(t, options{env: []string{
293-
"GIT_URL=" + url,
290+
"GIT_URL=" + srv.URL,
294291
"DEVCONTAINER_DIR=.devcontainer/custom",
295292
}})
296293
require.NoError(t, err)
@@ -303,7 +300,7 @@ func TestBuildFromDevcontainerInSubfolder(t *testing.T) {
303300
t.Parallel()
304301

305302
// Ensures that a Git repository with a devcontainer.json is cloned and built.
306-
url := createGitServer(t, gitServerOptions{
303+
srv := createGitServer(t, gitServerOptions{
307304
files: map[string]string{
308305
".devcontainer/subfolder/devcontainer.json": `{
309306
"name": "Test",
@@ -315,7 +312,7 @@ func TestBuildFromDevcontainerInSubfolder(t *testing.T) {
315312
},
316313
})
317314
ctr, err := runEnvbuilder(t, options{env: []string{
318-
"GIT_URL=" + url,
315+
"GIT_URL=" + srv.URL,
319316
}})
320317
require.NoError(t, err)
321318

@@ -326,7 +323,7 @@ func TestBuildFromDevcontainerInRoot(t *testing.T) {
326323
t.Parallel()
327324

328325
// Ensures that a Git repository with a devcontainer.json is cloned and built.
329-
url := createGitServer(t, gitServerOptions{
326+
srv := createGitServer(t, gitServerOptions{
330327
files: map[string]string{
331328
"devcontainer.json": `{
332329
"name": "Test",
@@ -338,7 +335,7 @@ func TestBuildFromDevcontainerInRoot(t *testing.T) {
338335
},
339336
})
340337
ctr, err := runEnvbuilder(t, options{env: []string{
341-
"GIT_URL=" + url,
338+
"GIT_URL=" + srv.URL,
342339
}})
343340
require.NoError(t, err)
344341

@@ -347,11 +344,12 @@ func TestBuildFromDevcontainerInRoot(t *testing.T) {
347344
}
348345

349346
func TestBuildCustomCertificates(t *testing.T) {
350-
srv := httptest.NewTLSServer(createGitHandler(t, gitServerOptions{
347+
srv := createGitServer(t, gitServerOptions{
351348
files: map[string]string{
352349
"Dockerfile": "FROM " + testImageAlpine,
353350
},
354-
}))
351+
tls: true,
352+
})
355353
ctr, err := runEnvbuilder(t, options{env: []string{
356354
"GIT_URL=" + srv.URL,
357355
"DOCKERFILE_PATH=Dockerfile",
@@ -368,13 +366,13 @@ func TestBuildCustomCertificates(t *testing.T) {
368366

369367
func TestBuildStopStartCached(t *testing.T) {
370368
// Ensures that a Git repository with a Dockerfile is cloned and built.
371-
url := createGitServer(t, gitServerOptions{
369+
srv := createGitServer(t, gitServerOptions{
372370
files: map[string]string{
373371
"Dockerfile": "FROM " + testImageAlpine,
374372
},
375373
})
376374
ctr, err := runEnvbuilder(t, options{env: []string{
377-
"GIT_URL=" + url,
375+
"GIT_URL=" + srv.URL,
378376
"DOCKERFILE_PATH=Dockerfile",
379377
"SKIP_REBUILD=true",
380378
}})
@@ -416,13 +414,13 @@ func TestBuildFailsFallback(t *testing.T) {
416414
t.Run("BadDockerfile", func(t *testing.T) {
417415
t.Parallel()
418416
// Ensures that a Git repository with a Dockerfile is cloned and built.
419-
url := createGitServer(t, gitServerOptions{
417+
srv := createGitServer(t, gitServerOptions{
420418
files: map[string]string{
421419
"Dockerfile": "bad syntax",
422420
},
423421
})
424422
_, err := runEnvbuilder(t, options{env: []string{
425-
"GIT_URL=" + url,
423+
"GIT_URL=" + srv.URL,
426424
"DOCKERFILE_PATH=Dockerfile",
427425
}})
428426
require.ErrorContains(t, err, envbuilder.ErrNoFallbackImage.Error())
@@ -431,40 +429,40 @@ func TestBuildFailsFallback(t *testing.T) {
431429
t.Run("FailsBuild", func(t *testing.T) {
432430
t.Parallel()
433431
// Ensures that a Git repository with a Dockerfile is cloned and built.
434-
url := createGitServer(t, gitServerOptions{
432+
srv := createGitServer(t, gitServerOptions{
435433
files: map[string]string{
436434
"Dockerfile": `FROM ` + testImageAlpine + `
437435
RUN exit 1`,
438436
},
439437
})
440438
_, err := runEnvbuilder(t, options{env: []string{
441-
"GIT_URL=" + url,
439+
"GIT_URL=" + srv.URL,
442440
"DOCKERFILE_PATH=Dockerfile",
443441
}})
444442
require.ErrorContains(t, err, envbuilder.ErrNoFallbackImage.Error())
445443
})
446444
t.Run("BadDevcontainer", func(t *testing.T) {
447445
t.Parallel()
448446
// Ensures that a Git repository with a Dockerfile is cloned and built.
449-
url := createGitServer(t, gitServerOptions{
447+
srv := createGitServer(t, gitServerOptions{
450448
files: map[string]string{
451449
".devcontainer/devcontainer.json": "not json",
452450
},
453451
})
454452
_, err := runEnvbuilder(t, options{env: []string{
455-
"GIT_URL=" + url,
453+
"GIT_URL=" + srv.URL,
456454
}})
457455
require.ErrorContains(t, err, envbuilder.ErrNoFallbackImage.Error())
458456
})
459457
t.Run("NoImageOrDockerfile", func(t *testing.T) {
460458
t.Parallel()
461-
url := createGitServer(t, gitServerOptions{
459+
srv := createGitServer(t, gitServerOptions{
462460
files: map[string]string{
463461
".devcontainer/devcontainer.json": "{}",
464462
},
465463
})
466464
ctr, err := runEnvbuilder(t, options{env: []string{
467-
"GIT_URL=" + url,
465+
"GIT_URL=" + srv.URL,
468466
"FALLBACK_IMAGE=" + testImageAlpine,
469467
}})
470468
require.NoError(t, err)
@@ -476,13 +474,13 @@ RUN exit 1`,
476474

477475
func TestExitBuildOnFailure(t *testing.T) {
478476
t.Parallel()
479-
url := createGitServer(t, gitServerOptions{
477+
srv := createGitServer(t, gitServerOptions{
480478
files: map[string]string{
481479
"Dockerfile": "bad syntax",
482480
},
483481
})
484482
_, err := runEnvbuilder(t, options{env: []string{
485-
"GIT_URL=" + url,
483+
"GIT_URL=" + srv.URL,
486484
"DOCKERFILE_PATH=Dockerfile",
487485
"FALLBACK_IMAGE=" + testImageAlpine,
488486
// Ensures that the fallback doesn't work when an image is specified.
@@ -495,7 +493,7 @@ func TestContainerEnv(t *testing.T) {
495493
t.Parallel()
496494

497495
// Ensures that a Git repository with a devcontainer.json is cloned and built.
498-
url := createGitServer(t, gitServerOptions{
496+
srv := createGitServer(t, gitServerOptions{
499497
files: map[string]string{
500498
".devcontainer/devcontainer.json": `{
501499
"name": "Test",
@@ -516,7 +514,7 @@ func TestContainerEnv(t *testing.T) {
516514
},
517515
})
518516
ctr, err := runEnvbuilder(t, options{env: []string{
519-
"GIT_URL=" + url,
517+
"GIT_URL=" + srv.URL,
520518
"EXPORT_ENV_FILE=/env",
521519
}})
522520
require.NoError(t, err)
@@ -534,7 +532,7 @@ func TestLifecycleScripts(t *testing.T) {
534532
t.Parallel()
535533

536534
// Ensures that a Git repository with a devcontainer.json is cloned and built.
537-
url := createGitServer(t, gitServerOptions{
535+
srv := createGitServer(t, gitServerOptions{
538536
files: map[string]string{
539537
".devcontainer/devcontainer.json": `{
540538
"name": "Test",
@@ -553,7 +551,7 @@ func TestLifecycleScripts(t *testing.T) {
553551
},
554552
})
555553
ctr, err := runEnvbuilder(t, options{env: []string{
556-
"GIT_URL=" + url,
554+
"GIT_URL=" + srv.URL,
557555
}})
558556
require.NoError(t, err)
559557

@@ -570,7 +568,7 @@ func TestPostStartScript(t *testing.T) {
570568
t.Parallel()
571569

572570
// Ensures that a Git repository with a devcontainer.json is cloned and built.
573-
url := createGitServer(t, gitServerOptions{
571+
srv := createGitServer(t, gitServerOptions{
574572
files: map[string]string{
575573
".devcontainer/devcontainer.json": `{
576574
"name": "Test",
@@ -592,7 +590,7 @@ USER nobody`,
592590
},
593591
})
594592
ctr, err := runEnvbuilder(t, options{env: []string{
595-
"GIT_URL=" + url,
593+
"GIT_URL=" + srv.URL,
596594
"POST_START_SCRIPT_PATH=/tmp/post-start.sh",
597595
"INIT_COMMAND=/bin/init.sh",
598596
}})
@@ -620,13 +618,13 @@ func TestPrivateRegistry(t *testing.T) {
620618
})
621619

622620
// Ensures that a Git repository with a Dockerfile is cloned and built.
623-
url := createGitServer(t, gitServerOptions{
621+
srv := createGitServer(t, gitServerOptions{
624622
files: map[string]string{
625623
"Dockerfile": "FROM " + image,
626624
},
627625
})
628626
_, err := runEnvbuilder(t, options{env: []string{
629-
"GIT_URL=" + url,
627+
"GIT_URL=" + srv.URL,
630628
"DOCKERFILE_PATH=Dockerfile",
631629
}})
632630
require.ErrorContains(t, err, "Unauthorized")
@@ -639,7 +637,7 @@ func TestPrivateRegistry(t *testing.T) {
639637
})
640638

641639
// Ensures that a Git repository with a Dockerfile is cloned and built.
642-
url := createGitServer(t, gitServerOptions{
640+
srv := createGitServer(t, gitServerOptions{
643641
files: map[string]string{
644642
"Dockerfile": "FROM " + image,
645643
},
@@ -655,7 +653,7 @@ func TestPrivateRegistry(t *testing.T) {
655653
require.NoError(t, err)
656654

657655
_, err = runEnvbuilder(t, options{env: []string{
658-
"GIT_URL=" + url,
656+
"GIT_URL=" + srv.URL,
659657
"DOCKERFILE_PATH=Dockerfile",
660658
"DOCKER_CONFIG_BASE64=" + base64.StdEncoding.EncodeToString(config),
661659
}})
@@ -671,7 +669,7 @@ func TestPrivateRegistry(t *testing.T) {
671669
})
672670

673671
// Ensures that a Git repository with a Dockerfile is cloned and built.
674-
url := createGitServer(t, gitServerOptions{
672+
srv := createGitServer(t, gitServerOptions{
675673
files: map[string]string{
676674
"Dockerfile": "FROM " + image,
677675
},
@@ -687,7 +685,7 @@ func TestPrivateRegistry(t *testing.T) {
687685
require.NoError(t, err)
688686

689687
_, err = runEnvbuilder(t, options{env: []string{
690-
"GIT_URL=" + url,
688+
"GIT_URL=" + srv.URL,
691689
"DOCKERFILE_PATH=Dockerfile",
692690
"DOCKER_CONFIG_BASE64=" + base64.StdEncoding.EncodeToString(config),
693691
}})
@@ -810,11 +808,11 @@ COPY %s .`, testImageAlpine, inclFile)
810808
tc := tc
811809

812810
t.Run(tc.name, func(t *testing.T) {
813-
url := createGitServer(t, gitServerOptions{
811+
srv := createGitServer(t, gitServerOptions{
814812
files: tc.files,
815813
})
816814
_, err := runEnvbuilder(t, options{env: []string{
817-
"GIT_URL=" + url,
815+
"GIT_URL=" + srv.URL,
818816
"DOCKERFILE_PATH=" + tc.dockerfilePath,
819817
"BUILD_CONTEXT_PATH=" + tc.buildContextPath,
820818
}})
@@ -858,41 +856,26 @@ type gitServerOptions struct {
858856
username string
859857
password string
860858
authMW func(http.Handler) http.Handler
859+
tls bool
861860
}
862861

863862
// createGitServer creates a git repository with an in-memory filesystem
864863
// 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 {
866865
t.Helper()
867866
if opts.authMW == nil {
868867
opts.authMW = gittest.BasicAuthMW(opts.username, opts.password)
869868
}
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+
}
876873
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)))
884877
}
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)))
896879
}
897880

898881
// cleanOldEnvbuilders removes any old envbuilder containers.

‎testutil/gittest/gittest.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import (
55
"net/http"
66
"os"
77
"testing"
8+
"time"
89

910
"github.com/go-git/go-billy/v5"
1011
"github.com/go-git/go-git/v5"
1112
"github.com/go-git/go-git/v5/plumbing"
1213
"github.com/go-git/go-git/v5/plumbing/cache"
1314
"github.com/go-git/go-git/v5/plumbing/format/pktline"
15+
"github.com/go-git/go-git/v5/plumbing/object"
1416
"github.com/go-git/go-git/v5/plumbing/protocol/packp"
1517
"github.com/go-git/go-git/v5/plumbing/transport"
1618
"github.com/go-git/go-git/v5/plumbing/transport/server"
@@ -95,8 +97,34 @@ func NewServer(fs billy.Filesystem) http.Handler {
9597
return mux
9698
}
9799

100+
// CommitFunc commits to a repo.
101+
type CommitFunc func(billy.Filesystem, *git.Repository)
102+
103+
// Commit is a test helper for committing a single file to a repo.
104+
func Commit(t *testing.T, path, content, msg string) CommitFunc {
105+
return func(fs billy.Filesystem, repo *git.Repository) {
106+
t.Helper()
107+
tree, err := repo.Worktree()
108+
require.NoError(t, err)
109+
WriteFile(t, fs, path, content)
110+
_, err = tree.Add(path)
111+
require.NoError(t, err)
112+
commit, err := tree.Commit(msg, &git.CommitOptions{
113+
Author: &object.Signature{
114+
Name: "Example",
115+
Email: "test@example.com",
116+
When: time.Now(),
117+
},
118+
})
119+
require.NoError(t, err)
120+
_, err = repo.CommitObject(commit)
121+
require.NoError(t, err)
122+
}
123+
}
124+
98125
// NewRepo returns a new Git repository.
99-
func NewRepo(t *testing.T, fs billy.Filesystem) *git.Repository {
126+
func NewRepo(t *testing.T, fs billy.Filesystem, commits ...CommitFunc) *git.Repository {
127+
t.Helper()
100128
storage := filesystem.NewStorage(fs, cache.NewObjectLRU(cache.DefaultMaxSize))
101129
repo, err := git.Init(storage, fs)
102130
require.NoError(t, err)
@@ -106,11 +134,15 @@ func NewRepo(t *testing.T, fs billy.Filesystem) *git.Repository {
106134
err = storage.SetReference(h)
107135
require.NoError(t, err)
108136

137+
for _, commit := range commits {
138+
commit(fs, repo)
139+
}
109140
return repo
110141
}
111142

112143
// WriteFile writes a file to the filesystem.
113144
func WriteFile(t *testing.T, fs billy.Filesystem, path, content string) {
145+
t.Helper()
114146
file, err := fs.OpenFile(path, os.O_CREATE|os.O_RDWR, 0644)
115147
require.NoError(t, err)
116148
_, err = file.Write([]byte(content))

0 commit comments

Comments
 (0)
Please sign in to comment.