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_helper_for_declarative_test.go 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2019 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. "context"
  7. "fmt"
  8. "io/ioutil"
  9. "net"
  10. "net/http"
  11. "net/url"
  12. "os"
  13. "path"
  14. "path/filepath"
  15. "strings"
  16. "testing"
  17. "time"
  18. "code.gitea.io/gitea/modules/git"
  19. "code.gitea.io/gitea/modules/setting"
  20. "code.gitea.io/gitea/modules/ssh"
  21. "github.com/stretchr/testify/assert"
  22. "github.com/unknwon/com"
  23. )
  24. func withKeyFile(t *testing.T, keyname string, callback func(string)) {
  25. tmpDir, err := ioutil.TempDir("", "key-file")
  26. assert.NoError(t, err)
  27. defer os.RemoveAll(tmpDir)
  28. err = os.Chmod(tmpDir, 0700)
  29. assert.NoError(t, err)
  30. keyFile := filepath.Join(tmpDir, keyname)
  31. err = ssh.GenKeyPair(keyFile)
  32. assert.NoError(t, err)
  33. err = ioutil.WriteFile(path.Join(tmpDir, "ssh"), []byte("#!/bin/bash\n"+
  34. "ssh -o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\" -o \"IdentitiesOnly=yes\" -i \""+keyFile+"\" \"$@\""), 0700)
  35. assert.NoError(t, err)
  36. //Setup ssh wrapper
  37. os.Setenv("GIT_SSH", path.Join(tmpDir, "ssh"))
  38. os.Setenv("GIT_SSH_COMMAND",
  39. "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o IdentitiesOnly=yes -i \""+keyFile+"\"")
  40. os.Setenv("GIT_SSH_VARIANT", "ssh")
  41. callback(keyFile)
  42. }
  43. func createSSHUrl(gitPath string, u *url.URL) *url.URL {
  44. u2 := *u
  45. u2.Scheme = "ssh"
  46. u2.User = url.User("git")
  47. u2.Host = fmt.Sprintf("%s:%d", setting.SSH.ListenHost, setting.SSH.ListenPort)
  48. u2.Path = gitPath
  49. return &u2
  50. }
  51. func allowLFSFilters() []string {
  52. // Now here we should explicitly allow lfs filters to run
  53. globalArgs := git.GlobalCommandArgs
  54. filteredLFSGlobalArgs := make([]string, len(git.GlobalCommandArgs))
  55. j := 0
  56. for _, arg := range git.GlobalCommandArgs {
  57. if strings.Contains(arg, "lfs") {
  58. j--
  59. } else {
  60. filteredLFSGlobalArgs[j] = arg
  61. j++
  62. }
  63. }
  64. filteredLFSGlobalArgs = filteredLFSGlobalArgs[:j]
  65. git.GlobalCommandArgs = filteredLFSGlobalArgs
  66. return globalArgs
  67. }
  68. func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bool) {
  69. if len(prepare) == 0 || prepare[0] {
  70. prepareTestEnv(t, 1)
  71. }
  72. s := http.Server{
  73. Handler: mac,
  74. }
  75. u, err := url.Parse(setting.AppURL)
  76. assert.NoError(t, err)
  77. listener, err := net.Listen("tcp", u.Host)
  78. assert.NoError(t, err)
  79. defer func() {
  80. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
  81. s.Shutdown(ctx)
  82. cancel()
  83. }()
  84. go s.Serve(listener)
  85. //Started by config go ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
  86. callback(t, u)
  87. }
  88. func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
  89. return func(t *testing.T) {
  90. oldGlobals := allowLFSFilters()
  91. assert.NoError(t, git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{}))
  92. git.GlobalCommandArgs = oldGlobals
  93. assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
  94. }
  95. }
  96. func doGitCloneFail(dstLocalPath string, u *url.URL) func(*testing.T) {
  97. return func(t *testing.T) {
  98. assert.Error(t, git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{}))
  99. assert.False(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
  100. }
  101. }
  102. func doGitInitTestRepository(dstPath string) func(*testing.T) {
  103. return func(t *testing.T) {
  104. // Init repository in dstPath
  105. assert.NoError(t, git.InitRepository(dstPath, false))
  106. assert.NoError(t, ioutil.WriteFile(filepath.Join(dstPath, "README.md"), []byte(fmt.Sprintf("# Testing Repository\n\nOriginally created in: %s", dstPath)), 0644))
  107. assert.NoError(t, git.AddChanges(dstPath, true))
  108. signature := git.Signature{
  109. Email: "test@example.com",
  110. Name: "test",
  111. When: time.Now(),
  112. }
  113. assert.NoError(t, git.CommitChanges(dstPath, git.CommitChangesOptions{
  114. Committer: &signature,
  115. Author: &signature,
  116. Message: "Initial Commit",
  117. }))
  118. }
  119. }
  120. func doGitAddRemote(dstPath, remoteName string, u *url.URL) func(*testing.T) {
  121. return func(t *testing.T) {
  122. _, err := git.NewCommand("remote", "add", remoteName, u.String()).RunInDir(dstPath)
  123. assert.NoError(t, err)
  124. }
  125. }
  126. func doGitPushTestRepository(dstPath string, args ...string) func(*testing.T) {
  127. return func(t *testing.T) {
  128. _, err := git.NewCommand(append([]string{"push", "-u"}, args...)...).RunInDir(dstPath)
  129. assert.NoError(t, err)
  130. }
  131. }
  132. func doGitPushTestRepositoryFail(dstPath string, args ...string) func(*testing.T) {
  133. return func(t *testing.T) {
  134. _, err := git.NewCommand(append([]string{"push"}, args...)...).RunInDir(dstPath)
  135. assert.Error(t, err)
  136. }
  137. }
  138. func doGitCreateBranch(dstPath, branch string) func(*testing.T) {
  139. return func(t *testing.T) {
  140. _, err := git.NewCommand("checkout", "-b", branch).RunInDir(dstPath)
  141. assert.NoError(t, err)
  142. }
  143. }
  144. func doGitCheckoutBranch(dstPath string, args ...string) func(*testing.T) {
  145. return func(t *testing.T) {
  146. oldGlobals := allowLFSFilters()
  147. _, err := git.NewCommand(append([]string{"checkout"}, args...)...).RunInDir(dstPath)
  148. git.GlobalCommandArgs = oldGlobals
  149. assert.NoError(t, err)
  150. }
  151. }
  152. func doGitMerge(dstPath string, args ...string) func(*testing.T) {
  153. return func(t *testing.T) {
  154. _, err := git.NewCommand(append([]string{"merge"}, args...)...).RunInDir(dstPath)
  155. assert.NoError(t, err)
  156. }
  157. }
  158. func doGitPull(dstPath string, args ...string) func(*testing.T) {
  159. return func(t *testing.T) {
  160. oldGlobals := allowLFSFilters()
  161. _, err := git.NewCommand(append([]string{"pull"}, args...)...).RunInDir(dstPath)
  162. git.GlobalCommandArgs = oldGlobals
  163. assert.NoError(t, err)
  164. }
  165. }