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.

195 lines
5.5 KiB

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