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.

194 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. "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. filteredLFSGlobalArgs := make([]string, len(git.GlobalCommandArgs))
  54. j := 0
  55. for _, arg := range git.GlobalCommandArgs {
  56. if strings.Contains(arg, "lfs") {
  57. j--
  58. } else {
  59. filteredLFSGlobalArgs[j] = arg
  60. j++
  61. }
  62. }
  63. return filteredLFSGlobalArgs[:j]
  64. }
  65. func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL), prepare ...bool) {
  66. if len(prepare) == 0 || prepare[0] {
  67. defer prepareTestEnv(t, 1)()
  68. }
  69. s := http.Server{
  70. Handler: mac,
  71. }
  72. u, err := url.Parse(setting.AppURL)
  73. assert.NoError(t, err)
  74. listener, err := net.Listen("tcp", u.Host)
  75. i := 0
  76. for err != nil && i <= 10 {
  77. time.Sleep(100 * time.Millisecond)
  78. listener, err = net.Listen("tcp", u.Host)
  79. i++
  80. }
  81. assert.NoError(t, err)
  82. u.Host = listener.Addr().String()
  83. defer func() {
  84. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
  85. s.Shutdown(ctx)
  86. cancel()
  87. }()
  88. go s.Serve(listener)
  89. //Started by config go ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
  90. callback(t, u)
  91. }
  92. func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
  93. return func(t *testing.T) {
  94. assert.NoError(t, git.CloneWithArgs(u.String(), dstLocalPath, allowLFSFilters(), git.CloneRepoOptions{}))
  95. assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
  96. }
  97. }
  98. func doGitCloneFail(u *url.URL) func(*testing.T) {
  99. return func(t *testing.T) {
  100. tmpDir, err := ioutil.TempDir("", "doGitCloneFail")
  101. assert.NoError(t, err)
  102. defer os.RemoveAll(tmpDir)
  103. assert.Error(t, git.Clone(u.String(), tmpDir, git.CloneRepoOptions{}))
  104. assert.False(t, com.IsExist(filepath.Join(tmpDir, "README.md")))
  105. }
  106. }
  107. func doGitInitTestRepository(dstPath string) func(*testing.T) {
  108. return func(t *testing.T) {
  109. // Init repository in dstPath
  110. assert.NoError(t, git.InitRepository(dstPath, false))
  111. assert.NoError(t, ioutil.WriteFile(filepath.Join(dstPath, "README.md"), []byte(fmt.Sprintf("# Testing Repository\n\nOriginally created in: %s", dstPath)), 0644))
  112. assert.NoError(t, git.AddChanges(dstPath, true))
  113. signature := git.Signature{
  114. Email: "test@example.com",
  115. Name: "test",
  116. When: time.Now(),
  117. }
  118. assert.NoError(t, git.CommitChanges(dstPath, git.CommitChangesOptions{
  119. Committer: &signature,
  120. Author: &signature,
  121. Message: "Initial Commit",
  122. }))
  123. }
  124. }
  125. func doGitAddRemote(dstPath, remoteName string, u *url.URL) func(*testing.T) {
  126. return func(t *testing.T) {
  127. _, err := git.NewCommand("remote", "add", remoteName, u.String()).RunInDir(dstPath)
  128. assert.NoError(t, err)
  129. }
  130. }
  131. func doGitPushTestRepository(dstPath string, args ...string) func(*testing.T) {
  132. return func(t *testing.T) {
  133. _, err := git.NewCommand(append([]string{"push", "-u"}, args...)...).RunInDir(dstPath)
  134. assert.NoError(t, err)
  135. }
  136. }
  137. func doGitPushTestRepositoryFail(dstPath string, args ...string) func(*testing.T) {
  138. return func(t *testing.T) {
  139. _, err := git.NewCommand(append([]string{"push"}, args...)...).RunInDir(dstPath)
  140. assert.Error(t, err)
  141. }
  142. }
  143. func doGitCreateBranch(dstPath, branch string) func(*testing.T) {
  144. return func(t *testing.T) {
  145. _, err := git.NewCommand("checkout", "-b", branch).RunInDir(dstPath)
  146. assert.NoError(t, err)
  147. }
  148. }
  149. func doGitCheckoutBranch(dstPath string, args ...string) func(*testing.T) {
  150. return func(t *testing.T) {
  151. _, err := git.NewCommandNoGlobals(append(append(allowLFSFilters(), "checkout"), args...)...).RunInDir(dstPath)
  152. assert.NoError(t, err)
  153. }
  154. }
  155. func doGitMerge(dstPath string, args ...string) func(*testing.T) {
  156. return func(t *testing.T) {
  157. _, err := git.NewCommand(append([]string{"merge"}, args...)...).RunInDir(dstPath)
  158. assert.NoError(t, err)
  159. }
  160. }
  161. func doGitPull(dstPath string, args ...string) func(*testing.T) {
  162. return func(t *testing.T) {
  163. _, err := git.NewCommandNoGlobals(append(append(allowLFSFilters(), "pull"), args...)...).RunInDir(dstPath)
  164. assert.NoError(t, err)
  165. }
  166. }