fakegit.go 3.2 KB

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