-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathgit_test.go
169 lines (156 loc) · 4.71 KB
/
git_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
package envbuilder_test
import (
"context"
"fmt"
"io"
"net/http/httptest"
"net/url"
"os"
"regexp"
"testing"
"github.com/coder/envbuilder"
"github.com/coder/envbuilder/testutil/gittest"
"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
githttp "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/stretchr/testify/require"
)
func TestCloneRepo(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
srvUsername string
srvPassword string
username string
password string
mungeURL func(*string)
expectError string
expectClone bool
}{
{
name: "no auth",
expectClone: true,
},
{
name: "auth",
srvUsername: "user",
srvPassword: "password",
username: "user",
password: "password",
expectClone: true,
},
{
name: "auth but no creds",
srvUsername: "user",
srvPassword: "password",
expectClone: false,
expectError: "authentication required",
},
{
name: "invalid auth",
srvUsername: "user",
srvPassword: "password",
username: "notuser",
password: "notpassword",
expectClone: false,
expectError: "authentication required",
},
{
name: "tokenish username",
srvUsername: "tokentokentoken",
srvPassword: "",
username: "tokentokentoken",
password: "",
expectClone: true,
},
} {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
// We do not overwrite a repo if one is already present.
t.Run("AlreadyCloned", func(t *testing.T) {
srvFS := memfs.New()
_ = gittest.NewRepo(t, srvFS, gittest.Commit(t, "README.md", "Hello, world!", "Wow!"))
authMW := gittest.BasicAuthMW(tc.srvUsername, tc.srvPassword)
srv := httptest.NewServer(authMW(gittest.NewServer(srvFS)))
clientFS := memfs.New()
// A repo already exists!
_ = gittest.NewRepo(t, clientFS)
cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{
Path: "/",
RepoURL: srv.URL,
Storage: clientFS,
})
require.NoError(t, err)
require.False(t, cloned)
})
// Basic Auth
t.Run("BasicAuth", func(t *testing.T) {
t.Parallel()
srvFS := memfs.New()
_ = gittest.NewRepo(t, srvFS, gittest.Commit(t, "README.md", "Hello, world!", "Wow!"))
authMW := gittest.BasicAuthMW(tc.srvUsername, tc.srvPassword)
srv := httptest.NewServer(authMW(gittest.NewServer(srvFS)))
clientFS := memfs.New()
cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{
Path: "/workspace",
RepoURL: srv.URL,
Storage: clientFS,
RepoAuth: &githttp.BasicAuth{
Username: tc.username,
Password: tc.password,
},
})
require.Equal(t, tc.expectClone, cloned)
if tc.expectError != "" {
require.ErrorContains(t, err, tc.expectError)
return
}
require.NoError(t, err)
require.True(t, cloned)
readme := mustRead(t, clientFS, "/workspace/README.md")
require.Equal(t, "Hello, world!", readme)
gitConfig := mustRead(t, clientFS, "/workspace/.git/config")
// Ensure we do not modify the git URL that folks pass in.
require.Regexp(t, fmt.Sprintf(`(?m)^\s+url\s+=\s+%s\s*$`, regexp.QuoteMeta(srv.URL)), gitConfig)
})
// In-URL-style auth e.g. http://user:password@host:port
t.Run("InURL", func(t *testing.T) {
t.Parallel()
srvFS := memfs.New()
_ = gittest.NewRepo(t, srvFS, gittest.Commit(t, "README.md", "Hello, world!", "Wow!"))
authMW := gittest.BasicAuthMW(tc.srvUsername, tc.srvPassword)
srv := httptest.NewServer(authMW(gittest.NewServer(srvFS)))
authURL, err := url.Parse(srv.URL)
require.NoError(t, err)
authURL.User = url.UserPassword(tc.username, tc.password)
clientFS := memfs.New()
cloned, err := envbuilder.CloneRepo(context.Background(), envbuilder.CloneRepoOptions{
Path: "/workspace",
RepoURL: authURL.String(),
Storage: clientFS,
})
require.Equal(t, tc.expectClone, cloned)
if tc.expectError != "" {
require.ErrorContains(t, err, tc.expectError)
return
}
require.NoError(t, err)
require.True(t, cloned)
readme := mustRead(t, clientFS, "/workspace/README.md")
require.Equal(t, "Hello, world!", readme)
gitConfig := mustRead(t, clientFS, "/workspace/.git/config")
// Ensure we do not modify the git URL that folks pass in.
require.Regexp(t, fmt.Sprintf(`(?m)^\s+url\s+=\s+%s\s*$`, regexp.QuoteMeta(authURL.String())), gitConfig)
})
})
}
}
func mustRead(t *testing.T, fs billy.Filesystem, path string) string {
t.Helper()
f, err := fs.OpenFile(path, os.O_RDONLY, 0o644)
require.NoError(t, err)
content, err := io.ReadAll(f)
require.NoError(t, err)
return string(content)
}