testserver: add Git server

This commit is contained in:
Hidde Beydals 2020-04-22 12:21:12 +02:00
parent 96a76c2ad8
commit 5582d99187
3 changed files with 120 additions and 0 deletions

5
go.mod
View File

@ -8,6 +8,7 @@ require (
github.com/go-logr/logr v0.1.0 github.com/go-logr/logr v0.1.0
github.com/onsi/ginkgo v1.11.0 github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.8.1 github.com/onsi/gomega v1.8.1
github.com/sosedoff/gitkit v0.2.1-0.20191202022816-7182d43c6254
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073
helm.sh/helm/v3 v3.1.2 helm.sh/helm/v3 v3.1.2
k8s.io/api v0.17.2 k8s.io/api v0.17.2
@ -17,3 +18,7 @@ require (
sigs.k8s.io/controller-runtime v0.5.0 sigs.k8s.io/controller-runtime v0.5.0
sigs.k8s.io/yaml v1.1.0 sigs.k8s.io/yaml v1.1.0
) )
// TODO(hidde): drop when PR is accepted:
// https://github.com/sosedoff/gitkit/pull/21
replace github.com/sosedoff/gitkit => github.com/hiddeco/gitkit v0.2.1-0.20200422093229-4355fec70348

3
go.sum
View File

@ -301,6 +301,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU= github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hiddeco/gitkit v0.2.1-0.20200422093229-4355fec70348 h1:m5YZOS7599S/K4swKGL4cDHbo2zqNhZKU2Z1leL7vuY=
github.com/hiddeco/gitkit v0.2.1-0.20200422093229-4355fec70348/go.mod h1:CrGdGLdRnoHSdSLqDVwmNbgUD5BmyapU+w3Z9r+s8xY=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/huandu/xstrings v1.2.0 h1:yPeWdRnmynF7p+lLYz0H2tthW9lqhMJrQV/U7yy4wX0= 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
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo= github.com/russross/blackfriday v1.5.2 h1:HyvC0ARfnZBqnXwABFeSZHpKvJHJJfPz81GNueLj0oo=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=

112
internal/testserver/git.go Normal file
View File

@ -0,0 +1,112 @@
/*
Copyright 2020 The Flux CD contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package testserver
import (
"io/ioutil"
"net/http/httptest"
"path/filepath"
"github.com/sosedoff/gitkit"
)
// NewTempGitServer returns a GitServer with a newly created temp
// dir as repository docroot.
func NewTempGitServer() (*GitServer, error) {
tmpDir, err := ioutil.TempDir("", "git-server-test-")
if err != nil {
return nil, err
}
srv := NewGitServer(tmpDir)
return srv, nil
}
// NewGitServer returns a GitServer with the given repository docroot
// set.
func NewGitServer(docroot string) *GitServer {
root, err := filepath.Abs(docroot)
if err != nil {
panic(err)
}
return &GitServer{
config: gitkit.Config{Dir: root},
}
}
// GitServer is a git server for testing purposes.
// It can serve git repositories over HTTP and SSH.
type GitServer struct {
config gitkit.Config
httpServer *httptest.Server
sshServer *gitkit.SSH
}
// StartHTTP starts a new HTTP git server with the current configuration.
func (s *GitServer) StartHTTP() error {
s.StopHTTP()
service := gitkit.New(s.config)
if err := service.Setup(); err != nil {
return err
}
s.httpServer = httptest.NewServer(service)
return nil
}
// StopHTTP stops the HTTP git server.
func (s *GitServer) StopHTTP() {
if s.httpServer != nil {
s.httpServer.Close()
}
return
}
// StartSSH starts a new SSH git server with the current configuration.
func (s *GitServer) StartSSH() error {
_ = s.StopSSH()
s.sshServer = gitkit.NewSSH(s.config)
// :0 should result in an OS assigned free port
return s.sshServer.ListenAndServe(":0")
}
// StopSSH stops the SSH git server.
func (s *GitServer) StopSSH() error {
if s.sshServer != nil {
return s.sshServer.Stop()
}
return nil
}
// Root returns the repositories root directory.
func (s *GitServer) Root() string {
return s.config.Dir
}
// HTTPAddress returns the address of the HTTP git server.
func (s *GitServer) HTTPAddress() string {
if s.httpServer != nil {
return s.httpServer.URL
}
return ""
}
// SSHAddress returns the address of the SSH git server.
func (s *GitServer) SSHAddress() string {
if s.sshServer != nil {
return s.sshServer.Address()
}
return ""
}