fakegit.go 3.3 KB

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