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 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/http"
  10. "net/url"
  11. "os"
  12. "path"
  13. "path/filepath"
  14. "testing"
  15. "time"
  16. "code.gitea.io/git"
  17. "code.gitea.io/gitea/models"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. const (
  21. littleSize = 1024 //1ko
  22. bigSize = 128 * 1024 * 1024 //128Mo
  23. )
  24. func TestGit(t *testing.T) {
  25. onGiteaRun(t, testGit)
  26. }
  27. func testGit(t *testing.T, u *url.URL) {
  28. username := "user2"
  29. baseAPITestContext := NewAPITestContext(t, username, "repo1")
  30. u.Path = baseAPITestContext.GitPath()
  31. t.Run("HTTP", func(t *testing.T) {
  32. httpContext := baseAPITestContext
  33. httpContext.Reponame = "repo-tmp-17"
  34. dstPath, err := ioutil.TempDir("", httpContext.Reponame)
  35. var little, big, littleLFS, bigLFS string
  36. assert.NoError(t, err)
  37. defer os.RemoveAll(dstPath)
  38. t.Run("Standard", func(t *testing.T) {
  39. ensureAnonymousClone(t, u)
  40. t.Run("CreateRepo", doAPICreateRepository(httpContext, false))
  41. u.Path = httpContext.GitPath()
  42. u.User = url.UserPassword(username, userPassword)
  43. t.Run("Clone", doGitClone(dstPath, u))
  44. t.Run("PushCommit", func(t *testing.T) {
  45. t.Run("Little", func(t *testing.T) {
  46. little = commitAndPush(t, littleSize, dstPath)
  47. })
  48. t.Run("Big", func(t *testing.T) {
  49. big = commitAndPush(t, bigSize, dstPath)
  50. })
  51. })
  52. })
  53. t.Run("LFS", func(t *testing.T) {
  54. t.Run("PushCommit", func(t *testing.T) {
  55. //Setup git LFS
  56. _, err = git.NewCommand("lfs").AddArguments("install").RunInDir(dstPath)
  57. assert.NoError(t, err)
  58. _, err = git.NewCommand("lfs").AddArguments("track", "data-file-*").RunInDir(dstPath)
  59. assert.NoError(t, err)
  60. err = git.AddChanges(dstPath, false, ".gitattributes")
  61. assert.NoError(t, err)
  62. t.Run("Little", func(t *testing.T) {
  63. littleLFS = commitAndPush(t, littleSize, dstPath)
  64. })
  65. t.Run("Big", func(t *testing.T) {
  66. bigLFS = commitAndPush(t, bigSize, dstPath)
  67. })
  68. })
  69. t.Run("Locks", func(t *testing.T) {
  70. lockTest(t, u.String(), dstPath)
  71. })
  72. })
  73. t.Run("Raw", func(t *testing.T) {
  74. session := loginUser(t, "user2")
  75. // Request raw paths
  76. req := NewRequest(t, "GET", path.Join("/user2/repo-tmp-17/raw/branch/master/", little))
  77. resp := session.MakeRequest(t, req, http.StatusOK)
  78. assert.Equal(t, littleSize, resp.Body.Len())
  79. req = NewRequest(t, "GET", path.Join("/user2/repo-tmp-17/raw/branch/master/", big))
  80. nilResp := session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
  81. assert.Equal(t, bigSize, nilResp.Length)
  82. req = NewRequest(t, "GET", path.Join("/user2/repo-tmp-17/raw/branch/master/", littleLFS))
  83. resp = session.MakeRequest(t, req, http.StatusOK)
  84. assert.NotEqual(t, littleSize, resp.Body.Len())
  85. assert.Contains(t, resp.Body.String(), models.LFSMetaFileIdentifier)
  86. req = NewRequest(t, "GET", path.Join("/user2/repo-tmp-17/raw/branch/master/", bigLFS))
  87. resp = session.MakeRequest(t, req, http.StatusOK)
  88. assert.NotEqual(t, bigSize, resp.Body.Len())
  89. assert.Contains(t, resp.Body.String(), models.LFSMetaFileIdentifier)
  90. })
  91. t.Run("Media", func(t *testing.T) {
  92. session := loginUser(t, "user2")
  93. // Request media paths
  94. req := NewRequest(t, "GET", path.Join("/user2/repo-tmp-17/media/branch/master/", little))
  95. resp := session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
  96. assert.Equal(t, littleSize, resp.Length)
  97. req = NewRequest(t, "GET", path.Join("/user2/repo-tmp-17/media/branch/master/", big))
  98. resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
  99. assert.Equal(t, bigSize, resp.Length)
  100. req = NewRequest(t, "GET", path.Join("/user2/repo-tmp-17/media/branch/master/", littleLFS))
  101. resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
  102. assert.Equal(t, littleSize, resp.Length)
  103. req = NewRequest(t, "GET", path.Join("/user2/repo-tmp-17/media/branch/master/", bigLFS))
  104. resp = session.MakeRequestNilResponseRecorder(t, req, http.StatusOK)
  105. assert.Equal(t, bigSize, resp.Length)
  106. })
  107. })
  108. t.Run("SSH", func(t *testing.T) {
  109. sshContext := baseAPITestContext
  110. sshContext.Reponame = "repo-tmp-18"
  111. keyname := "my-testing-key"
  112. //Setup key the user ssh key
  113. withKeyFile(t, keyname, func(keyFile string) {
  114. t.Run("CreateUserKey", doAPICreateUserKey(sshContext, "test-key", keyFile))
  115. //Setup remote link
  116. sshURL := createSSHUrl(sshContext.GitPath(), u)
  117. //Setup clone folder
  118. dstPath, err := ioutil.TempDir("", sshContext.Reponame)
  119. assert.NoError(t, err)
  120. defer os.RemoveAll(dstPath)
  121. var little, big, littleLFS, bigLFS string
  122. t.Run("Standard", func(t *testing.T) {
  123. t.Run("CreateRepo", doAPICreateRepository(sshContext, false))
  124. //TODO get url from api
  125. t.Run("Clone", doGitClone(dstPath, sshURL))
  126. //time.Sleep(5 * time.Minute)
  127. t.Run("PushCommit", func(t *testing.T) {
  128. t.Run("Little", func(t *testing.T) {
  129. little = commitAndPush(t, littleSize, dstPath)
  130. })
  131. t.Run("Big", func(t *testing.T) {
  132. big = commitAndPush(t, bigSize, dstPath)
  133. })
  134. })
  135. })
  136. t.Run("LFS", func(t *testing.T) {
  137. t.Run("PushCommit", func(t *testing.T) {
  138. //Setup git LFS
  139. _, err = git.NewCommand("lfs").AddArguments("install").RunInDir(dstPath)
  140. assert.NoError(t, err)
  141. _, err = git.NewCommand("lfs").AddArguments("track", "data-file-*").RunInDir(dstPath)
  142. assert.NoError(t, err)
  143. err = git.AddChanges(dstPath, false, ".gitattributes")
  144. assert.NoError(t, err)
  145. t.Run("Little", func(t *testing.T) {
  146. littleLFS = commitAndPush(t, littleSize, dstPath)
  147. })
  148. t.Run("Big", func(t *testing.T) {
  149. bigLFS = commitAndPush(t, bigSize, dstPath)
  150. })
  151. })
  152. t.Run("Locks", func(t *testing.T) {
  153. lockTest(t, u.String(), dstPath)
  154. })
  155. })
  156. t.Run("Raw", func(t *testing.T) {
  157. session := loginUser(t, "user2")
  158. // Request raw paths
  159. req := NewRequest(t, "GET", path.Join("/user2/repo-tmp-18/raw/branch/master/", little))
  160. resp := session.MakeRequest(t, req, http.StatusOK)
  161. assert.Equal(t, littleSize, resp.Body.Len())
  162. req = NewRequest(t, "GET", path.Join("/user2/repo-tmp-18/raw/branch/master/", big))
  163. resp = session.MakeRequest(t, req, http.StatusOK)
  164. assert.Equal(t, bigSize, resp.Body.Len())
  165. req = NewRequest(t, "GET", path.Join("/user2/repo-tmp-18/raw/branch/master/", littleLFS))
  166. resp = session.MakeRequest(t, req, http.StatusOK)
  167. assert.NotEqual(t, littleSize, resp.Body.Len())
  168. assert.Contains(t, resp.Body.String(), models.LFSMetaFileIdentifier)
  169. req = NewRequest(t, "GET", path.Join("/user2/repo-tmp-18/raw/branch/master/", bigLFS))
  170. resp = session.MakeRequest(t, req, http.StatusOK)
  171. assert.NotEqual(t, bigSize, resp.Body.Len())
  172. assert.Contains(t, resp.Body.String(), models.LFSMetaFileIdentifier)
  173. })
  174. t.Run("Media", func(t *testing.T) {
  175. session := loginUser(t, "user2")
  176. // Request media paths
  177. req := NewRequest(t, "GET", path.Join("/user2/repo-tmp-18/media/branch/master/", little))
  178. resp := session.MakeRequest(t, req, http.StatusOK)
  179. assert.Equal(t, littleSize, resp.Body.Len())
  180. req = NewRequest(t, "GET", path.Join("/user2/repo-tmp-18/media/branch/master/", big))
  181. resp = session.MakeRequest(t, req, http.StatusOK)
  182. assert.Equal(t, bigSize, resp.Body.Len())
  183. req = NewRequest(t, "GET", path.Join("/user2/repo-tmp-18/media/branch/master/", littleLFS))
  184. resp = session.MakeRequest(t, req, http.StatusOK)
  185. assert.Equal(t, littleSize, resp.Body.Len())
  186. req = NewRequest(t, "GET", path.Join("/user2/repo-tmp-18/media/branch/master/", bigLFS))
  187. resp = session.MakeRequest(t, req, http.StatusOK)
  188. assert.Equal(t, bigSize, resp.Body.Len())
  189. })
  190. })
  191. })
  192. }
  193. func ensureAnonymousClone(t *testing.T, u *url.URL) {
  194. dstLocalPath, err := ioutil.TempDir("", "repo1")
  195. assert.NoError(t, err)
  196. defer os.RemoveAll(dstLocalPath)
  197. t.Run("CloneAnonymous", doGitClone(dstLocalPath, u))
  198. }
  199. func lockTest(t *testing.T, remote, repoPath string) {
  200. _, err := git.NewCommand("remote").AddArguments("set-url", "origin", remote).RunInDir(repoPath) //TODO add test ssh git-lfs-creds
  201. assert.NoError(t, err)
  202. _, err = git.NewCommand("lfs").AddArguments("locks").RunInDir(repoPath)
  203. assert.NoError(t, err)
  204. _, err = git.NewCommand("lfs").AddArguments("lock", "README.md").RunInDir(repoPath)
  205. assert.NoError(t, err)
  206. _, err = git.NewCommand("lfs").AddArguments("locks").RunInDir(repoPath)
  207. assert.NoError(t, err)
  208. _, err = git.NewCommand("lfs").AddArguments("unlock", "README.md").RunInDir(repoPath)
  209. assert.NoError(t, err)
  210. }
  211. func commitAndPush(t *testing.T, size int, repoPath string) string {
  212. name, err := generateCommitWithNewData(size, repoPath, "user2@example.com", "User Two")
  213. assert.NoError(t, err)
  214. _, err = git.NewCommand("push").RunInDir(repoPath) //Push
  215. assert.NoError(t, err)
  216. return name
  217. }
  218. func generateCommitWithNewData(size int, repoPath, email, fullName string) (string, error) {
  219. //Generate random file
  220. data := make([]byte, size)
  221. _, err := rand.Read(data)
  222. if err != nil {
  223. return "", err
  224. }
  225. tmpFile, err := ioutil.TempFile(repoPath, "data-file-")
  226. if err != nil {
  227. return "", err
  228. }
  229. defer tmpFile.Close()
  230. _, err = tmpFile.Write(data)
  231. if err != nil {
  232. return "", err
  233. }
  234. //Commit
  235. err = git.AddChanges(repoPath, false, filepath.Base(tmpFile.Name()))
  236. if err != nil {
  237. return "", err
  238. }
  239. err = git.CommitChanges(repoPath, git.CommitChangesOptions{
  240. Committer: &git.Signature{
  241. Email: email,
  242. Name: fullName,
  243. When: time.Now(),
  244. },
  245. Author: &git.Signature{
  246. Email: email,
  247. Name: fullName,
  248. When: time.Now(),
  249. },
  250. Message: fmt.Sprintf("Testing commit @ %v", time.Now()),
  251. })
  252. return filepath.Base(tmpFile.Name()), err
  253. }