github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/test/integration/cmd/fakegitserver/main.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 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 // fakegitserver serves Git repositories over HTTP for integration tests. 18 package main 19 20 import ( 21 "flag" 22 "fmt" 23 "net/http" 24 "os" 25 "sync" 26 "time" 27 28 "github.com/gorilla/mux" 29 "github.com/sirupsen/logrus" 30 31 "github.com/zppinho/prow/test/integration/internal/fakegitserver" 32 "sigs.k8s.io/prow/pkg/interrupts" 33 "sigs.k8s.io/prow/pkg/logrusutil" 34 "sigs.k8s.io/prow/pkg/pjutil" 35 ) 36 37 type options struct { 38 port int 39 portHttps int 40 gitBinary string 41 gitReposParentDir string 42 cert string 43 key string 44 } 45 46 func (o *options) validate() error { 47 return nil 48 } 49 50 // flagOptions defines default options. 51 func flagOptions() *options { 52 o := &options{} 53 flag.IntVar(&o.port, "port", 8888, "Port to listen on.") 54 flag.IntVar(&o.portHttps, "port-https", 4443, "Port to listen on for HTTPS traffic.") 55 flag.StringVar(&o.gitBinary, "git-binary", "/usr/bin/git", "Path to the `git` binary.") 56 flag.StringVar(&o.gitReposParentDir, "git-repos-parent-dir", "/git-repo", "Path to the parent folder containing all Git repos to serve over HTTP.") 57 flag.StringVar(&o.cert, "cert", "", "Path to the server cert file for HTTPS.") 58 flag.StringVar(&o.key, "key", "", "Path to the server key file for HTTPS.") 59 return o 60 } 61 62 func main() { 63 logrusutil.ComponentInit() 64 65 o := flagOptions() 66 flag.Parse() 67 if err := o.validate(); err != nil { 68 logrus.WithError(err).Fatal("Invalid arguments.") 69 } 70 defer interrupts.WaitForGracefulShutdown() 71 72 health := pjutil.NewHealth() 73 health.ServeReady() 74 75 r := mux.NewRouter() 76 77 // Only send requests under the /repo/... path to git-http-backend. This way 78 // we can have other paths (if necessary) to take in custom commands from 79 // integration tests (e.g., "/admin/reset" to reset all repos back to their 80 // original state). 81 r.PathPrefix("/repo").Handler(fakegitserver.GitCGIHandler(o.gitBinary, o.gitReposParentDir)) 82 // Set up repo might modify global git config, need to lock it to avoid 83 // errors caused by concurrent modifications. 84 var lock sync.Mutex 85 r.PathPrefix("/setup-repo").Handler(fakegitserver.SetupRepoHandler(o.gitReposParentDir, &lock)) 86 87 if err := os.MkdirAll(o.gitReposParentDir, os.ModePerm); err != nil { 88 logrus.Fatalf("could not create directory %q", o.gitReposParentDir) 89 } 90 91 server := &http.Server{ 92 Addr: fmt.Sprintf(":%d", o.port), 93 Handler: r, 94 } 95 96 // Serve HTTPS traffic. 97 if o.cert != "" && o.key != "" { 98 serverHTTPS := &http.Server{ 99 Addr: fmt.Sprintf(":%d", o.portHttps), 100 Handler: r, 101 } 102 logrus.Infof("Starting HTTPS server on port %d", o.portHttps) 103 interrupts.ListenAndServeTLS(serverHTTPS, o.cert, o.key, 5*time.Second) 104 } 105 106 // Serve HTTP traffic. 107 logrus.Infof("Starting HTTP server on port %d", o.port) 108 interrupts.ListenAndServe(server, 5*time.Second) 109 }