Skip to content

Commit 5582d99

Browse files
committed
testserver: add Git server
1 parent 96a76c2 commit 5582d99

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/go-logr/logr v0.1.0
99
github.com/onsi/ginkgo v1.11.0
1010
github.com/onsi/gomega v1.8.1
11+
github.com/sosedoff/gitkit v0.2.1-0.20191202022816-7182d43c6254
1112
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073
1213
helm.sh/helm/v3 v3.1.2
1314
k8s.io/api v0.17.2
@@ -17,3 +18,7 @@ require (
1718
sigs.k8s.io/controller-runtime v0.5.0
1819
sigs.k8s.io/yaml v1.1.0
1920
)
21+
22+
// TODO(hidde): drop when PR is accepted:
23+
// https://github.com/sosedoff/gitkit/pull/21
24+
replace github.com/sosedoff/gitkit => github.com/hiddeco/gitkit v0.2.1-0.20200422093229-4355fec70348

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
301301
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
302302
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
303303
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
304+
github.com/hiddeco/gitkit v0.2.1-0.20200422093229-4355fec70348 h1:m5YZOS7599S/K4swKGL4cDHbo2zqNhZKU2Z1leL7vuY=
305+
github.com/hiddeco/gitkit v0.2.1-0.20200422093229-4355fec70348/go.mod h1:CrGdGLdRnoHSdSLqDVwmNbgUD5BmyapU+w3Z9r+s8xY=
304306
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
305307
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
306308
github.com/huandu/xstrings v1.2.0 h1:yPeWdRnmynF7p+lLYz0H2tthW9lqhMJrQV/U7yy4wX0=
@@ -451,6 +453,7 @@ github.com/remyoudompheng/bigfft v0.0.0-20170806203942-52369c62f446/go.mod h1:uY
451453
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
452454
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
453455
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
456+
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
454457
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
455458
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
456459
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=

internal/testserver/git.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
Copyright 2020 The Flux CD contributors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package testserver
18+
19+
import (
20+
"io/ioutil"
21+
"net/http/httptest"
22+
"path/filepath"
23+
24+
"github.com/sosedoff/gitkit"
25+
)
26+
27+
// NewTempGitServer returns a GitServer with a newly created temp
28+
// dir as repository docroot.
29+
func NewTempGitServer() (*GitServer, error) {
30+
tmpDir, err := ioutil.TempDir("", "git-server-test-")
31+
if err != nil {
32+
return nil, err
33+
}
34+
srv := NewGitServer(tmpDir)
35+
return srv, nil
36+
}
37+
38+
// NewGitServer returns a GitServer with the given repository docroot
39+
// set.
40+
func NewGitServer(docroot string) *GitServer {
41+
root, err := filepath.Abs(docroot)
42+
if err != nil {
43+
panic(err)
44+
}
45+
return &GitServer{
46+
config: gitkit.Config{Dir: root},
47+
}
48+
}
49+
50+
// GitServer is a git server for testing purposes.
51+
// It can serve git repositories over HTTP and SSH.
52+
type GitServer struct {
53+
config gitkit.Config
54+
httpServer *httptest.Server
55+
sshServer *gitkit.SSH
56+
}
57+
58+
// StartHTTP starts a new HTTP git server with the current configuration.
59+
func (s *GitServer) StartHTTP() error {
60+
s.StopHTTP()
61+
service := gitkit.New(s.config)
62+
if err := service.Setup(); err != nil {
63+
return err
64+
}
65+
s.httpServer = httptest.NewServer(service)
66+
return nil
67+
}
68+
69+
// StopHTTP stops the HTTP git server.
70+
func (s *GitServer) StopHTTP() {
71+
if s.httpServer != nil {
72+
s.httpServer.Close()
73+
}
74+
return
75+
}
76+
77+
// StartSSH starts a new SSH git server with the current configuration.
78+
func (s *GitServer) StartSSH() error {
79+
_ = s.StopSSH()
80+
s.sshServer = gitkit.NewSSH(s.config)
81+
// :0 should result in an OS assigned free port
82+
return s.sshServer.ListenAndServe(":0")
83+
}
84+
85+
// StopSSH stops the SSH git server.
86+
func (s *GitServer) StopSSH() error {
87+
if s.sshServer != nil {
88+
return s.sshServer.Stop()
89+
}
90+
return nil
91+
}
92+
93+
// Root returns the repositories root directory.
94+
func (s *GitServer) Root() string {
95+
return s.config.Dir
96+
}
97+
98+
// HTTPAddress returns the address of the HTTP git server.
99+
func (s *GitServer) HTTPAddress() string {
100+
if s.httpServer != nil {
101+
return s.httpServer.URL
102+
}
103+
return ""
104+
}
105+
106+
// SSHAddress returns the address of the SSH git server.
107+
func (s *GitServer) SSHAddress() string {
108+
if s.sshServer != nil {
109+
return s.sshServer.Address()
110+
}
111+
return ""
112+
}

0 commit comments

Comments
 (0)