You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

git_test.go 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "crypto/rand"
  7. "fmt"
  8. "io/ioutil"
  9. "net/url"
  10. "os"
  11. "path/filepath"
  12. "testing"
  13. "time"
  14. "code.gitea.io/git"
  15. "github.com/stretchr/testify/assert"
  16. )
  17. const (
  18. littleSize = 1024 //1ko
  19. bigSize = 128 * 1024 * 1024 //128Mo
  20. )
  21. func TestGit(t *testing.T) {
  22. onGiteaRun(t, testGit)
  23. }
  24. func testGit(t *testing.T, u *url.URL) {
  25. username := "user2"
  26. baseAPITestContext := NewAPITestContext(t, username, "repo1")
  27. u.Path = baseAPITestContext.GitPath()
  28. t.Run("HTTP", func(t *testing.T) {
  29. httpContext := baseAPITestContext
  30. httpContext.Reponame = "repo-tmp-17"
  31. dstPath, err := ioutil.TempDir("", httpContext.Reponame)
  32. assert.NoError(t, err)
  33. defer os.RemoveAll(dstPath)
  34. t.Run("Standard", func(t *testing.T) {
  35. ensureAnonymousClone(t, u)
  36. t.Run("CreateRepo", doAPICreateRepository(httpContext, false))
  37. u.Path = httpContext.GitPath()
  38. u.User = url.UserPassword(username, userPassword)
  39. t.Run("Clone", doGitClone(dstPath, u))
  40. t.Run("PushCommit", func(t *testing.T) {
  41. t.Run("Little", func(t *testing.T) {
  42. commitAndPush(t, littleSize, dstPath)
  43. })
  44. t.Run("Big", func(t *testing.T) {
  45. commitAndPush(t, bigSize, dstPath)
  46. })
  47. })
  48. })
  49. t.Run("LFS", func(t *testing.T) {
  50. t.Run("PushCommit", func(t *testing.T) {
  51. //Setup git LFS
  52. _, err = git.NewCommand("lfs").AddArguments("install").RunInDir(dstPath)
  53. assert.NoError(t, err)
  54. _, err = git.NewCommand("lfs").AddArguments("track", "data-file-*").RunInDir(dstPath)
  55. assert.NoError(t, err)
  56. err = git.AddChanges(dstPath, false, ".gitattributes")
  57. assert.NoError(t, err)
  58. t.Run("Little", func(t *testing.T) {
  59. commitAndPush(t, littleSize, dstPath)
  60. })
  61. t.Run("Big", func(t *testing.T) {
  62. commitAndPush(t, bigSize, dstPath)
  63. })
  64. })
  65. t.Run("Locks", func(t *testing.T) {
  66. lockTest(t, u.String(), dstPath)
  67. })
  68. })
  69. })
  70. t.Run("SSH", func(t *testing.T) {
  71. sshContext := baseAPITestContext
  72. sshContext.Reponame = "repo-tmp-18"
  73. keyname := "my-testing-key"
  74. //Setup key the user ssh key
  75. withKeyFile(t, keyname, func(keyFile string) {
  76. t.Run("CreateUserKey", doAPICreateUserKey(sshContext, "test-key", keyFile))
  77. //Setup remote link
  78. sshURL := createSSHUrl(sshContext.GitPath(), u)
  79. //Setup clone folder
  80. dstPath, err := ioutil.TempDir("", sshContext.Reponame)
  81. assert.NoError(t, err)
  82. defer os.RemoveAll(dstPath)
  83. t.Run("Standard", func(t *testing.T) {
  84. t.Run("CreateRepo", doAPICreateRepository(sshContext, false))
  85. //TODO get url from api
  86. t.Run("Clone", doGitClone(dstPath, sshURL))
  87. //time.Sleep(5 * time.Minute)
  88. t.Run("PushCommit", func(t *testing.T) {
  89. t.Run("Little", func(t *testing.T) {
  90. commitAndPush(t, littleSize, dstPath)
  91. })
  92. t.Run("Big", func(t *testing.T) {
  93. commitAndPush(t, bigSize, dstPath)
  94. })
  95. })
  96. })
  97. t.Run("LFS", func(t *testing.T) {
  98. t.Run("PushCommit", func(t *testing.T) {
  99. //Setup git LFS
  100. _, err = git.NewCommand("lfs").AddArguments("install").RunInDir(dstPath)
  101. assert.NoError(t, err)
  102. _, err = git.NewCommand("lfs").AddArguments("track", "data-file-*").RunInDir(dstPath)
  103. assert.NoError(t, err)
  104. err = git.AddChanges(dstPath, false, ".gitattributes")
  105. assert.NoError(t, err)
  106. t.Run("Little", func(t *testing.T) {
  107. commitAndPush(t, littleSize, dstPath)
  108. })
  109. t.Run("Big", func(t *testing.T) {
  110. commitAndPush(t, bigSize, dstPath)
  111. })
  112. })
  113. t.Run("Locks", func(t *testing.T) {
  114. lockTest(t, u.String(), dstPath)
  115. })
  116. })
  117. })
  118. })
  119. }
  120. func ensureAnonymousClone(t *testing.T, u *url.URL) {
  121. dstLocalPath, err := ioutil.TempDir("", "repo1")
  122. assert.NoError(t, err)
  123. defer os.RemoveAll(dstLocalPath)
  124. t.Run("CloneAnonymous", doGitClone(dstLocalPath, u))
  125. }
  126. func lockTest(t *testing.T, remote, repoPath string) {
  127. _, err := git.NewCommand("remote").AddArguments("set-url", "origin", remote).RunInDir(repoPath) //TODO add test ssh git-lfs-creds
  128. assert.NoError(t, err)
  129. _, err = git.NewCommand("lfs").AddArguments("locks").RunInDir(repoPath)
  130. assert.NoError(t, err)
  131. _, err = git.NewCommand("lfs").AddArguments("lock", "README.md").RunInDir(repoPath)
  132. assert.NoError(t, err)
  133. _, err = git.NewCommand("lfs").AddArguments("locks").RunInDir(repoPath)
  134. assert.NoError(t, err)
  135. _, err = git.NewCommand("lfs").AddArguments("unlock", "README.md").RunInDir(repoPath)
  136. assert.NoError(t, err)
  137. }
  138. func commitAndPush(t *testing.T, size int, repoPath string) {
  139. err := generateCommitWithNewData(size, repoPath, "user2@example.com", "User Two")
  140. assert.NoError(t, err)
  141. _, err = git.NewCommand("push").RunInDir(repoPath) //Push
  142. assert.NoError(t, err)
  143. }
  144. func generateCommitWithNewData(size int, repoPath, email, fullName string) error {
  145. //Generate random file
  146. data := make([]byte, size)
  147. _, err := rand.Read(data)
  148. if err != nil {
  149. return err
  150. }
  151. tmpFile, err := ioutil.TempFile(repoPath, "data-file-")
  152. if err != nil {
  153. return err
  154. }
  155. defer tmpFile.Close()
  156. _, err = tmpFile.Write(data)
  157. if err != nil {
  158. return err
  159. }
  160. //Commit
  161. err = git.AddChanges(repoPath, false, filepath.Base(tmpFile.Name()))
  162. if err != nil {
  163. return err
  164. }
  165. err = git.CommitChanges(repoPath, git.CommitChangesOptions{
  166. Committer: &git.Signature{
  167. Email: email,
  168. Name: fullName,
  169. When: time.Now(),
  170. },
  171. Author: &git.Signature{
  172. Email: email,
  173. Name: fullName,
  174. When: time.Now(),
  175. },
  176. Message: fmt.Sprintf("Testing commit @ %v", time.Now()),
  177. })
  178. return err
  179. }