github.com/rita33cool1/iot-system-gateway@v0.0.0-20200911033302-e65bde238cc5/docker-engine/internal/test/fakegit/fakegit.go (about) 1 package fakegit // import "github.com/docker/docker/internal/test/fakegit" 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "net/http" 7 "net/http/httptest" 8 "os" 9 "os/exec" 10 "path/filepath" 11 12 "github.com/docker/docker/internal/test/fakecontext" 13 "github.com/docker/docker/internal/test/fakestorage" 14 "github.com/gotestyourself/gotestyourself/assert" 15 ) 16 17 type testingT interface { 18 assert.TestingT 19 logT 20 skipT 21 Fatal(args ...interface{}) 22 Fatalf(string, ...interface{}) 23 } 24 25 type logT interface { 26 Logf(string, ...interface{}) 27 } 28 29 type skipT interface { 30 Skip(reason string) 31 } 32 33 type gitServer interface { 34 URL() string 35 Close() error 36 } 37 38 type localGitServer struct { 39 *httptest.Server 40 } 41 42 func (r *localGitServer) Close() error { 43 r.Server.Close() 44 return nil 45 } 46 47 func (r *localGitServer) URL() string { 48 return r.Server.URL 49 } 50 51 // FakeGit is a fake git server 52 type FakeGit struct { 53 root string 54 server gitServer 55 RepoURL string 56 } 57 58 // Close closes the server, implements Closer interface 59 func (g *FakeGit) Close() { 60 g.server.Close() 61 os.RemoveAll(g.root) 62 } 63 64 // New create a fake git server that can be used for git related tests 65 func New(c testingT, name string, files map[string]string, enforceLocalServer bool) *FakeGit { 66 ctx := fakecontext.New(c, "", fakecontext.WithFiles(files)) 67 defer ctx.Close() 68 curdir, err := os.Getwd() 69 if err != nil { 70 c.Fatal(err) 71 } 72 defer os.Chdir(curdir) 73 74 if output, err := exec.Command("git", "init", ctx.Dir).CombinedOutput(); err != nil { 75 c.Fatalf("error trying to init repo: %s (%s)", err, output) 76 } 77 err = os.Chdir(ctx.Dir) 78 if err != nil { 79 c.Fatal(err) 80 } 81 if output, err := exec.Command("git", "config", "user.name", "Fake User").CombinedOutput(); err != nil { 82 c.Fatalf("error trying to set 'user.name': %s (%s)", err, output) 83 } 84 if output, err := exec.Command("git", "config", "user.email", "fake.user@example.com").CombinedOutput(); err != nil { 85 c.Fatalf("error trying to set 'user.email': %s (%s)", err, output) 86 } 87 if output, err := exec.Command("git", "add", "*").CombinedOutput(); err != nil { 88 c.Fatalf("error trying to add files to repo: %s (%s)", err, output) 89 } 90 if output, err := exec.Command("git", "commit", "-a", "-m", "Initial commit").CombinedOutput(); err != nil { 91 c.Fatalf("error trying to commit to repo: %s (%s)", err, output) 92 } 93 94 root, err := ioutil.TempDir("", "docker-test-git-repo") 95 if err != nil { 96 c.Fatal(err) 97 } 98 repoPath := filepath.Join(root, name+".git") 99 if output, err := exec.Command("git", "clone", "--bare", ctx.Dir, repoPath).CombinedOutput(); err != nil { 100 os.RemoveAll(root) 101 c.Fatalf("error trying to clone --bare: %s (%s)", err, output) 102 } 103 err = os.Chdir(repoPath) 104 if err != nil { 105 os.RemoveAll(root) 106 c.Fatal(err) 107 } 108 if output, err := exec.Command("git", "update-server-info").CombinedOutput(); err != nil { 109 os.RemoveAll(root) 110 c.Fatalf("error trying to git update-server-info: %s (%s)", err, output) 111 } 112 err = os.Chdir(curdir) 113 if err != nil { 114 os.RemoveAll(root) 115 c.Fatal(err) 116 } 117 118 var server gitServer 119 if !enforceLocalServer { 120 // use fakeStorage server, which might be local or remote (at test daemon) 121 server = fakestorage.New(c, root) 122 } else { 123 // always start a local http server on CLI test machine 124 httpServer := httptest.NewServer(http.FileServer(http.Dir(root))) 125 server = &localGitServer{httpServer} 126 } 127 return &FakeGit{ 128 root: root, 129 server: server, 130 RepoURL: fmt.Sprintf("%s/%s.git", server.URL(), name), 131 } 132 }