Skip to content

Commit a000d8b

Browse files
Paulo Gomesdarkowlzz
authored andcommitted
Add tests for experimental libgit2 transport
Signed-off-by: Paulo Gomes <[email protected]>
1 parent aa32881 commit a000d8b

File tree

3 files changed

+347
-27
lines changed

3 files changed

+347
-27
lines changed

hack/ci/e2e.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,19 @@ echo "Run large Git repo tests"
139139
kubectl -n source-system apply -f "${ROOT_DIR}/config/testdata/git/large-repo.yaml"
140140
kubectl -n source-system wait gitrepository/large-repo-go-git --for=condition=ready --timeout=2m15s
141141
kubectl -n source-system wait gitrepository/large-repo-libgit2 --for=condition=ready --timeout=2m15s
142+
143+
144+
# Test experimental libgit2 transport. Any tests against the default transport must
145+
# either run before this, or patch the deployment again to disable this, as once enabled
146+
# only the managed transport will be used.
147+
kubectl -n source-system patch deployment source-controller \
148+
--patch '{"spec": {"template": {"spec": {"containers": [{"name": "manager","env": [{"name": "EXPERIMENTAL_GIT_TRANSPORT", "value": "true"}]}]}}}}'
149+
150+
# wait until the patch took effect and the new source-controller is running
151+
sleep 20s
152+
153+
kubectl -n source-system wait --for=condition=ready --timeout=1m -l app=source-controller pod
154+
155+
echo "Re-run large libgit2 repo test with managed transport"
156+
kubectl -n source-system wait gitrepository/large-repo-libgit2 --for=condition=ready --timeout=2m15s
157+
kubectl -n source-system exec deploy/source-controller -- printenv | grep EXPERIMENTAL_GIT_TRANSPORT=true

pkg/git/libgit2/managed/http.go

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ type httpSmartSubtransport struct {
8585
}
8686

8787
func (t *httpSmartSubtransport) Action(targetUrl string, action git2go.SmartServiceAction) (git2go.SmartSubtransportStream, error) {
88-
var req *http.Request
89-
var err error
90-
9188
var proxyFn func(*http.Request) (*url.URL, error)
9289
proxyOpts, err := t.transport.SmartProxyOptions()
9390
if err != nil {
@@ -125,26 +122,50 @@ func (t *httpSmartSubtransport) Action(targetUrl string, action git2go.SmartServ
125122
ExpectContinueTimeout: 1 * time.Second,
126123
}
127124

128-
finalUrl := targetUrl
129-
opts, found := transportOptions(targetUrl)
130-
if found && opts.TargetUrl != "" {
131-
// override target URL only if options are found and a new targetURL
132-
// is provided.
133-
finalUrl = opts.TargetUrl
125+
client, req, err := createClientRequest(targetUrl, action, httpTransport)
126+
if err != nil {
127+
return nil, err
128+
}
129+
130+
stream := newManagedHttpStream(t, req, client)
131+
if req.Method == "POST" {
132+
stream.recvReply.Add(1)
133+
stream.sendRequestBackground()
134134
}
135135

136-
// Add any provided certificate to the http transport.
137-
if len(opts.CABundle) > 0 {
138-
cap := x509.NewCertPool()
139-
if ok := cap.AppendCertsFromPEM(opts.CABundle); !ok {
140-
return nil, fmt.Errorf("failed to use certificate from PEM")
136+
return stream, nil
137+
}
138+
139+
func createClientRequest(targetUrl string, action git2go.SmartServiceAction, t *http.Transport) (*http.Client, *http.Request, error) {
140+
var req *http.Request
141+
var err error
142+
143+
if t == nil {
144+
return nil, nil, fmt.Errorf("failed to create client: transport cannot be nil")
145+
}
146+
147+
finalUrl := targetUrl
148+
opts, found := transportOptions(targetUrl)
149+
if found {
150+
if opts.TargetUrl != "" {
151+
// override target URL only if options are found and a new targetURL
152+
// is provided.
153+
finalUrl = opts.TargetUrl
141154
}
142-
httpTransport.TLSClientConfig = &tls.Config{
143-
RootCAs: cap,
155+
156+
// Add any provided certificate to the http transport.
157+
if len(opts.CABundle) > 0 {
158+
cap := x509.NewCertPool()
159+
if ok := cap.AppendCertsFromPEM(opts.CABundle); !ok {
160+
return nil, nil, fmt.Errorf("failed to use certificate from PEM")
161+
}
162+
t.TLSClientConfig = &tls.Config{
163+
RootCAs: cap,
164+
}
144165
}
145166
}
146167

147-
client := &http.Client{Transport: httpTransport, Timeout: fullHttpClientTimeOut}
168+
client := &http.Client{Transport: t, Timeout: fullHttpClientTimeOut}
148169

149170
switch action {
150171
case git2go.SmartServiceActionUploadpackLs:
@@ -172,18 +193,11 @@ func (t *httpSmartSubtransport) Action(targetUrl string, action git2go.SmartServ
172193
}
173194

174195
if err != nil {
175-
return nil, err
196+
return nil, nil, err
176197
}
177198

178-
req.Header.Set("User-Agent", "git/2.0 (git2go)")
179-
180-
stream := newManagedHttpStream(t, req, client)
181-
if req.Method == "POST" {
182-
stream.recvReply.Add(1)
183-
stream.sendRequestBackground()
184-
}
185-
186-
return stream, nil
199+
req.Header.Set("User-Agent", "git/2.0 (flux-libgit2)")
200+
return client, req, nil
187201
}
188202

189203
func (t *httpSmartSubtransport) Close() error {

0 commit comments

Comments
 (0)