|
| 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