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.

155 lines
4.4 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/filepath"
  14. "testing"
  15. "time"
  16. "code.gitea.io/gitea/modules/git"
  17. "code.gitea.io/gitea/modules/setting"
  18. "code.gitea.io/gitea/modules/ssh"
  19. "github.com/Unknwon/com"
  20. "github.com/stretchr/testify/assert"
  21. )
  22. func withKeyFile(t *testing.T, keyname string, callback func(string)) {
  23. keyFile := filepath.Join(setting.AppDataPath, keyname)
  24. err := ssh.GenKeyPair(keyFile)
  25. assert.NoError(t, err)
  26. //Setup ssh wrapper
  27. os.Setenv("GIT_SSH_COMMAND",
  28. "ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -i "+
  29. filepath.Join(setting.AppWorkPath, keyFile))
  30. os.Setenv("GIT_SSH_VARIANT", "ssh")
  31. callback(keyFile)
  32. defer os.RemoveAll(keyFile)
  33. defer os.RemoveAll(keyFile + ".pub")
  34. }
  35. func createSSHUrl(gitPath string, u *url.URL) *url.URL {
  36. u2 := *u
  37. u2.Scheme = "ssh"
  38. u2.User = url.User("git")
  39. u2.Host = fmt.Sprintf("%s:%d", setting.SSH.ListenHost, setting.SSH.ListenPort)
  40. u2.Path = gitPath
  41. return &u2
  42. }
  43. func onGiteaRun(t *testing.T, callback func(*testing.T, *url.URL)) {
  44. prepareTestEnv(t, 1)
  45. s := http.Server{
  46. Handler: mac,
  47. }
  48. u, err := url.Parse(setting.AppURL)
  49. assert.NoError(t, err)
  50. listener, err := net.Listen("tcp", u.Host)
  51. assert.NoError(t, err)
  52. defer func() {
  53. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
  54. s.Shutdown(ctx)
  55. cancel()
  56. }()
  57. go s.Serve(listener)
  58. //Started by config go ssh.Listen(setting.SSH.ListenHost, setting.SSH.ListenPort, setting.SSH.ServerCiphers, setting.SSH.ServerKeyExchanges, setting.SSH.ServerMACs)
  59. callback(t, u)
  60. }
  61. func doGitClone(dstLocalPath string, u *url.URL) func(*testing.T) {
  62. return func(t *testing.T) {
  63. assert.NoError(t, git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{}))
  64. assert.True(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
  65. }
  66. }
  67. func doGitCloneFail(dstLocalPath string, u *url.URL) func(*testing.T) {
  68. return func(t *testing.T) {
  69. assert.Error(t, git.Clone(u.String(), dstLocalPath, git.CloneRepoOptions{}))
  70. assert.False(t, com.IsExist(filepath.Join(dstLocalPath, "README.md")))
  71. }
  72. }
  73. func doGitInitTestRepository(dstPath string) func(*testing.T) {
  74. return func(t *testing.T) {
  75. // Init repository in dstPath
  76. assert.NoError(t, git.InitRepository(dstPath, false))
  77. assert.NoError(t, ioutil.WriteFile(filepath.Join(dstPath, "README.md"), []byte(fmt.Sprintf("# Testing Repository\n\nOriginally created in: %s", dstPath)), 0644))
  78. assert.NoError(t, git.AddChanges(dstPath, true))
  79. signature := git.Signature{
  80. Email: "test@example.com",
  81. Name: "test",
  82. When: time.Now(),
  83. }
  84. assert.NoError(t, git.CommitChanges(dstPath, git.CommitChangesOptions{
  85. Committer: &signature,
  86. Author: &signature,
  87. Message: "Initial Commit",
  88. }))
  89. }
  90. }
  91. func doGitAddRemote(dstPath, remoteName string, u *url.URL) func(*testing.T) {
  92. return func(t *testing.T) {
  93. _, err := git.NewCommand("remote", "add", remoteName, u.String()).RunInDir(dstPath)
  94. assert.NoError(t, err)
  95. }
  96. }
  97. func doGitPushTestRepository(dstPath string, args ...string) func(*testing.T) {
  98. return func(t *testing.T) {
  99. _, err := git.NewCommand(append([]string{"push", "-u"}, args...)...).RunInDir(dstPath)
  100. assert.NoError(t, err)
  101. }
  102. }
  103. func doGitPushTestRepositoryFail(dstPath string, args ...string) func(*testing.T) {
  104. return func(t *testing.T) {
  105. _, err := git.NewCommand(append([]string{"push"}, args...)...).RunInDir(dstPath)
  106. assert.Error(t, err)
  107. }
  108. }
  109. func doGitCreateBranch(dstPath, branch string) func(*testing.T) {
  110. return func(t *testing.T) {
  111. _, err := git.NewCommand("checkout", "-b", branch).RunInDir(dstPath)
  112. assert.NoError(t, err)
  113. }
  114. }
  115. func doGitCheckoutBranch(dstPath string, args ...string) func(*testing.T) {
  116. return func(t *testing.T) {
  117. _, err := git.NewCommand(append([]string{"checkout"}, args...)...).RunInDir(dstPath)
  118. assert.NoError(t, err)
  119. }
  120. }
  121. func doGitMerge(dstPath string, args ...string) func(*testing.T) {
  122. return func(t *testing.T) {
  123. _, err := git.NewCommand(append([]string{"merge"}, args...)...).RunInDir(dstPath)
  124. assert.NoError(t, err)
  125. }
  126. }
  127. func doGitPull(dstPath string, args ...string) func(*testing.T) {
  128. return func(t *testing.T) {
  129. _, err := git.NewCommand(append([]string{"pull"}, args...)...).RunInDir(dstPath)
  130. assert.NoError(t, err)
  131. }
  132. }