2021-06-14 13:20:43 -04:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2024-08-04 14:46:05 -04:00
|
|
|
// Copyright 2024 The Forgejo Authors. All rights reserved.
|
2022-11-27 13:20:29 -05:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-14 13:20:43 -04:00
|
|
|
|
2022-09-02 15:18:23 -04:00
|
|
|
package integration
|
2021-06-14 13:20:43 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-08-04 14:46:05 -04:00
|
|
|
"net"
|
2021-06-14 13:20:43 -04:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2024-08-04 14:46:05 -04:00
|
|
|
"os"
|
2024-08-26 02:54:16 -04:00
|
|
|
"os/exec"
|
2024-08-04 14:46:05 -04:00
|
|
|
"path/filepath"
|
2021-12-20 22:12:27 -05:00
|
|
|
"strconv"
|
2021-06-14 13:20:43 -04:00
|
|
|
"testing"
|
2024-08-04 14:46:05 -04:00
|
|
|
"time"
|
2021-06-14 13:20:43 -04:00
|
|
|
|
2024-08-04 14:46:05 -04:00
|
|
|
asymkey_model "code.gitea.io/gitea/models/asymkey"
|
2022-07-30 12:45:59 -04:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-12-09 20:27:50 -05:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2024-08-04 14:46:05 -04:00
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-16 03:53:21 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2021-06-14 13:20:43 -04:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-27 15:09:51 -05:00
|
|
|
"code.gitea.io/gitea/modules/gitrepo"
|
2024-08-04 14:46:05 -04:00
|
|
|
"code.gitea.io/gitea/modules/optional"
|
2021-06-14 13:20:43 -04:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-08-04 14:46:05 -04:00
|
|
|
"code.gitea.io/gitea/modules/test"
|
2024-02-27 02:12:22 -05:00
|
|
|
gitea_context "code.gitea.io/gitea/services/context"
|
2023-12-01 08:07:36 -05:00
|
|
|
doctor "code.gitea.io/gitea/services/doctor"
|
2021-11-20 04:34:05 -05:00
|
|
|
"code.gitea.io/gitea/services/migrations"
|
2021-06-14 13:20:43 -04:00
|
|
|
mirror_service "code.gitea.io/gitea/services/mirror"
|
2023-09-06 08:08:51 -04:00
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2022-09-02 15:18:23 -04:00
|
|
|
"code.gitea.io/gitea/tests"
|
2021-06-14 13:20:43 -04:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 15:41:10 -04:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-06-14 13:20:43 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMirrorPush(t *testing.T) {
|
|
|
|
onGiteaRun(t, testMirrorPush)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testMirrorPush(t *testing.T, u *url.URL) {
|
2022-09-02 15:18:23 -04:00
|
|
|
defer tests.PrepareTestEnv(t)()
|
2024-08-04 14:46:05 -04:00
|
|
|
defer test.MockVariableValue(&setting.Migrations.AllowLocalNetworks, true)()
|
2021-06-14 13:20:43 -04:00
|
|
|
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, migrations.Init())
|
2021-06-14 13:20:43 -04:00
|
|
|
|
2022-08-15 22:22:25 -04:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
srcRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
2021-06-14 13:20:43 -04:00
|
|
|
|
2023-09-08 00:51:15 -04:00
|
|
|
mirrorRepo, err := repo_service.CreateRepositoryDirectly(db.DefaultContext, user, user, repo_service.CreateRepoOptions{
|
2021-06-14 13:20:43 -04:00
|
|
|
Name: "test-push-mirror",
|
|
|
|
})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2021-06-14 13:20:43 -04:00
|
|
|
|
|
|
|
ctx := NewAPITestContext(t, user.LowerName, srcRepo.Name)
|
|
|
|
|
|
|
|
doCreatePushMirror(ctx, fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(ctx.Username), url.PathEscape(mirrorRepo.Name)), user.LowerName, userPassword)(t)
|
2023-12-01 08:07:36 -05:00
|
|
|
doCreatePushMirror(ctx, fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(ctx.Username), url.PathEscape("does-not-matter")), user.LowerName, userPassword)(t)
|
2021-06-14 13:20:43 -04:00
|
|
|
|
2022-07-30 12:45:59 -04:00
|
|
|
mirrors, _, err := repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo.ID, db.ListOptions{})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-12-01 08:07:36 -05:00
|
|
|
assert.Len(t, mirrors, 2)
|
2021-06-14 13:20:43 -04:00
|
|
|
|
|
|
|
ok := mirror_service.SyncPushMirror(context.Background(), mirrors[0].ID)
|
|
|
|
assert.True(t, ok)
|
|
|
|
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-27 15:09:51 -05:00
|
|
|
srcGitRepo, err := gitrepo.OpenRepository(git.DefaultContext, srcRepo)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2021-06-14 13:20:43 -04:00
|
|
|
defer srcGitRepo.Close()
|
|
|
|
|
|
|
|
srcCommit, err := srcGitRepo.GetBranchCommit("master")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2021-06-14 13:20:43 -04:00
|
|
|
|
Simplify how git repositories are opened (#28937)
## Purpose
This is a refactor toward building an abstraction over managing git
repositories.
Afterwards, it does not matter anymore if they are stored on the local
disk or somewhere remote.
## What this PR changes
We used `git.OpenRepository` everywhere previously.
Now, we should split them into two distinct functions:
Firstly, there are temporary repositories which do not change:
```go
git.OpenRepository(ctx, diskPath)
```
Gitea managed repositories having a record in the database in the
`repository` table are moved into the new package `gitrepo`:
```go
gitrepo.OpenRepository(ctx, repo_model.Repo)
```
Why is `repo_model.Repository` the second parameter instead of file
path?
Because then we can easily adapt our repository storage strategy.
The repositories can be stored locally, however, they could just as well
be stored on a remote server.
## Further changes in other PRs
- A Git Command wrapper on package `gitrepo` could be created. i.e.
`NewCommand(ctx, repo_model.Repository, commands...)`. `git.RunOpts{Dir:
repo.RepoPath()}`, the directory should be empty before invoking this
method and it can be filled in the function only. #28940
- Remove the `RepoPath()`/`WikiPath()` functions to reduce the
possibility of mistakes.
---------
Co-authored-by: delvh <dev.lh@web.de>
2024-01-27 15:09:51 -05:00
|
|
|
mirrorGitRepo, err := gitrepo.OpenRepository(git.DefaultContext, mirrorRepo)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2021-06-14 13:20:43 -04:00
|
|
|
defer mirrorGitRepo.Close()
|
|
|
|
|
|
|
|
mirrorCommit, err := mirrorGitRepo.GetBranchCommit("master")
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2021-06-14 13:20:43 -04:00
|
|
|
|
|
|
|
assert.Equal(t, srcCommit.ID, mirrorCommit.ID)
|
2021-12-20 22:12:27 -05:00
|
|
|
|
2023-12-01 08:07:36 -05:00
|
|
|
// Test that we can "repair" push mirrors where the remote doesn't exist in git's state.
|
|
|
|
// To do that, we artificially remove the remote...
|
|
|
|
cmd := git.NewCommand(db.DefaultContext, "remote", "rm").AddDynamicArguments(mirrors[0].RemoteName)
|
|
|
|
_, _, err = cmd.RunStdString(&git.RunOpts{Dir: srcRepo.RepoPath()})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-12-01 08:07:36 -05:00
|
|
|
|
|
|
|
// ...then ensure that trying to get its remote address fails
|
|
|
|
_, err = repo_model.GetPushMirrorRemoteAddress(srcRepo.OwnerName, srcRepo.Name, mirrors[0].RemoteName)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.Error(t, err)
|
2023-12-01 08:07:36 -05:00
|
|
|
|
|
|
|
// ...and that we can fix it.
|
|
|
|
err = doctor.FixPushMirrorsWithoutGitRemote(db.DefaultContext, nil, true)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-12-01 08:07:36 -05:00
|
|
|
|
|
|
|
// ...and after fixing, we only have one remote
|
|
|
|
mirrors, _, err = repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo.ID, db.ListOptions{})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-12-01 08:07:36 -05:00
|
|
|
assert.Len(t, mirrors, 1)
|
|
|
|
|
|
|
|
// ...one we can get the address of, and it's not the one we removed
|
|
|
|
remoteAddress, err := repo_model.GetPushMirrorRemoteAddress(srcRepo.OwnerName, srcRepo.Name, mirrors[0].RemoteName)
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
2023-12-01 08:07:36 -05:00
|
|
|
assert.Contains(t, remoteAddress, "does-not-matter")
|
|
|
|
|
2021-12-20 22:12:27 -05:00
|
|
|
// Cleanup
|
|
|
|
doRemovePushMirror(ctx, fmt.Sprintf("%s%s/%s", u.String(), url.PathEscape(ctx.Username), url.PathEscape(mirrorRepo.Name)), user.LowerName, userPassword, int(mirrors[0].ID))(t)
|
2022-07-30 12:45:59 -04:00
|
|
|
mirrors, _, err = repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo.ID, db.ListOptions{})
|
2024-07-30 15:41:10 -04:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Empty(t, mirrors)
|
2021-06-14 13:20:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func doCreatePushMirror(ctx APITestContext, address, username, password string) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
csrf := GetCSRF(t, ctx.Session, fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)))
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)), map[string]string{
|
|
|
|
"_csrf": csrf,
|
|
|
|
"action": "push-mirror-add",
|
|
|
|
"push_mirror_address": address,
|
|
|
|
"push_mirror_username": username,
|
|
|
|
"push_mirror_password": password,
|
|
|
|
"push_mirror_interval": "0",
|
|
|
|
})
|
2022-03-23 00:54:07 -04:00
|
|
|
ctx.Session.MakeRequest(t, req, http.StatusSeeOther)
|
2021-06-14 13:20:43 -04:00
|
|
|
|
2023-04-13 15:45:33 -04:00
|
|
|
flashCookie := ctx.Session.GetCookie(gitea_context.CookieNameFlash)
|
2021-06-14 13:20:43 -04:00
|
|
|
assert.NotNil(t, flashCookie)
|
|
|
|
assert.Contains(t, flashCookie.Value, "success")
|
|
|
|
}
|
|
|
|
}
|
2021-12-20 22:12:27 -05:00
|
|
|
|
|
|
|
func doRemovePushMirror(ctx APITestContext, address, username, password string, pushMirrorID int) func(t *testing.T) {
|
|
|
|
return func(t *testing.T) {
|
|
|
|
csrf := GetCSRF(t, ctx.Session, fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)))
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/%s/settings", url.PathEscape(ctx.Username), url.PathEscape(ctx.Reponame)), map[string]string{
|
|
|
|
"_csrf": csrf,
|
|
|
|
"action": "push-mirror-remove",
|
|
|
|
"push_mirror_id": strconv.Itoa(pushMirrorID),
|
|
|
|
"push_mirror_address": address,
|
|
|
|
"push_mirror_username": username,
|
|
|
|
"push_mirror_password": password,
|
|
|
|
"push_mirror_interval": "0",
|
|
|
|
})
|
2022-03-23 00:54:07 -04:00
|
|
|
ctx.Session.MakeRequest(t, req, http.StatusSeeOther)
|
2021-12-20 22:12:27 -05:00
|
|
|
|
2023-04-13 15:45:33 -04:00
|
|
|
flashCookie := ctx.Session.GetCookie(gitea_context.CookieNameFlash)
|
2021-12-20 22:12:27 -05:00
|
|
|
assert.NotNil(t, flashCookie)
|
|
|
|
assert.Contains(t, flashCookie.Value, "success")
|
|
|
|
}
|
|
|
|
}
|
2024-08-04 14:46:05 -04:00
|
|
|
|
|
|
|
func TestSSHPushMirror(t *testing.T) {
|
2024-08-26 02:54:16 -04:00
|
|
|
_, err := exec.LookPath("ssh")
|
|
|
|
if err != nil {
|
|
|
|
t.Skip("SSH executable not present")
|
|
|
|
}
|
|
|
|
|
2024-08-04 14:46:05 -04:00
|
|
|
onGiteaRun(t, func(t *testing.T, _ *url.URL) {
|
|
|
|
defer test.MockVariableValue(&setting.Migrations.AllowLocalNetworks, true)()
|
|
|
|
defer test.MockVariableValue(&setting.Mirror.Enabled, true)()
|
|
|
|
defer test.MockVariableValue(&setting.SSH.RootPath, t.TempDir())()
|
|
|
|
require.NoError(t, migrations.Init())
|
|
|
|
|
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
srcRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
|
|
|
assert.False(t, srcRepo.HasWiki())
|
|
|
|
sess := loginUser(t, user.Name)
|
2024-08-24 20:47:35 -04:00
|
|
|
pushToRepo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{
|
2024-08-04 14:46:05 -04:00
|
|
|
Name: optional.Some("push-mirror-test"),
|
|
|
|
AutoInit: optional.Some(false),
|
|
|
|
EnabledUnits: optional.Some([]unit.Type{unit.TypeCode}),
|
|
|
|
})
|
|
|
|
defer f()
|
|
|
|
|
|
|
|
sshURL := fmt.Sprintf("ssh://%s@%s/%s.git", setting.SSH.User, net.JoinHostPort(setting.SSH.ListenHost, strconv.Itoa(setting.SSH.ListenPort)), pushToRepo.FullName())
|
|
|
|
t.Run("Mutual exclusive", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/settings", srcRepo.FullName()), map[string]string{
|
|
|
|
"_csrf": GetCSRF(t, sess, fmt.Sprintf("/%s/settings", srcRepo.FullName())),
|
|
|
|
"action": "push-mirror-add",
|
|
|
|
"push_mirror_address": sshURL,
|
|
|
|
"push_mirror_username": "username",
|
|
|
|
"push_mirror_password": "password",
|
|
|
|
"push_mirror_use_ssh": "true",
|
|
|
|
"push_mirror_interval": "0",
|
|
|
|
})
|
|
|
|
resp := sess.MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
|
|
|
|
errMsg := htmlDoc.Find(".ui.negative.message").Text()
|
|
|
|
assert.Contains(t, errMsg, "Cannot use public key and password based authentication in combination.")
|
|
|
|
})
|
|
|
|
|
2024-08-26 02:54:16 -04:00
|
|
|
inputSelector := `input[id="push_mirror_use_ssh"]`
|
|
|
|
|
|
|
|
t.Run("SSH not available", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
defer test.MockVariableValue(&git.HasSSHExecutable, false)()
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/settings", srcRepo.FullName()), map[string]string{
|
|
|
|
"_csrf": GetCSRF(t, sess, fmt.Sprintf("/%s/settings", srcRepo.FullName())),
|
|
|
|
"action": "push-mirror-add",
|
|
|
|
"push_mirror_address": sshURL,
|
|
|
|
"push_mirror_use_ssh": "true",
|
|
|
|
"push_mirror_interval": "0",
|
|
|
|
})
|
|
|
|
resp := sess.MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
|
|
|
|
errMsg := htmlDoc.Find(".ui.negative.message").Text()
|
|
|
|
assert.Contains(t, errMsg, "SSH authentication isn't available.")
|
|
|
|
|
|
|
|
htmlDoc.AssertElement(t, inputSelector, false)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("SSH available", func(t *testing.T) {
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/%s/settings", srcRepo.FullName()))
|
|
|
|
resp := sess.MakeRequest(t, req, http.StatusOK)
|
|
|
|
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
htmlDoc.AssertElement(t, inputSelector, true)
|
|
|
|
})
|
|
|
|
|
2024-08-04 14:46:05 -04:00
|
|
|
t.Run("Normal", func(t *testing.T) {
|
|
|
|
var pushMirror *repo_model.PushMirror
|
|
|
|
t.Run("Adding", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/settings", srcRepo.FullName()), map[string]string{
|
|
|
|
"_csrf": GetCSRF(t, sess, fmt.Sprintf("/%s/settings", srcRepo.FullName())),
|
|
|
|
"action": "push-mirror-add",
|
|
|
|
"push_mirror_address": sshURL,
|
|
|
|
"push_mirror_use_ssh": "true",
|
|
|
|
"push_mirror_interval": "0",
|
|
|
|
})
|
|
|
|
sess.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
|
|
|
|
flashCookie := sess.GetCookie(gitea_context.CookieNameFlash)
|
|
|
|
assert.NotNil(t, flashCookie)
|
|
|
|
assert.Contains(t, flashCookie.Value, "success")
|
|
|
|
|
|
|
|
pushMirror = unittest.AssertExistsAndLoadBean(t, &repo_model.PushMirror{RepoID: srcRepo.ID})
|
|
|
|
assert.NotEmpty(t, pushMirror.PrivateKey)
|
|
|
|
assert.NotEmpty(t, pushMirror.PublicKey)
|
|
|
|
})
|
|
|
|
|
|
|
|
publickey := ""
|
|
|
|
t.Run("Publickey", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/%s/settings", srcRepo.FullName()))
|
|
|
|
resp := sess.MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
|
|
|
|
publickey = htmlDoc.Find(".ui.table td a[data-clipboard-text]").AttrOr("data-clipboard-text", "")
|
|
|
|
assert.EqualValues(t, publickey, pushMirror.GetPublicKey())
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Add deploy key", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/settings/keys", pushToRepo.FullName()), map[string]string{
|
|
|
|
"_csrf": GetCSRF(t, sess, fmt.Sprintf("/%s/settings/keys", pushToRepo.FullName())),
|
|
|
|
"title": "push mirror key",
|
|
|
|
"content": publickey,
|
|
|
|
"is_writable": "true",
|
|
|
|
})
|
|
|
|
sess.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &asymkey_model.DeployKey{Name: "push mirror key", RepoID: pushToRepo.ID})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Synchronize", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/settings", srcRepo.FullName()), map[string]string{
|
|
|
|
"_csrf": GetCSRF(t, sess, fmt.Sprintf("/%s/settings", srcRepo.FullName())),
|
|
|
|
"action": "push-mirror-sync",
|
|
|
|
"push_mirror_id": strconv.FormatInt(pushMirror.ID, 10),
|
|
|
|
})
|
|
|
|
sess.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Check mirrored content", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
shortSHA := "1032bbf17f"
|
|
|
|
|
|
|
|
req := NewRequest(t, "GET", fmt.Sprintf("/%s", srcRepo.FullName()))
|
|
|
|
resp := sess.MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
|
|
|
|
|
|
|
assert.Contains(t, htmlDoc.Find(".shortsha").Text(), shortSHA)
|
|
|
|
|
|
|
|
assert.Eventually(t, func() bool {
|
|
|
|
req = NewRequest(t, "GET", fmt.Sprintf("/%s", pushToRepo.FullName()))
|
|
|
|
resp = sess.MakeRequest(t, req, http.StatusOK)
|
|
|
|
htmlDoc = NewHTMLParser(t, resp.Body)
|
|
|
|
|
|
|
|
return htmlDoc.Find(".shortsha").Text() == shortSHA
|
|
|
|
}, time.Second*30, time.Second)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Check known host keys", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
|
|
|
knownHosts, err := os.ReadFile(filepath.Join(setting.SSH.RootPath, "known_hosts"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
publicKey, err := os.ReadFile(setting.SSH.ServerHostKeys[0] + ".pub")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Contains(t, string(knownHosts), string(publicKey))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2024-11-02 10:06:27 -04:00
|
|
|
|
|
|
|
func TestPushMirrorSettings(t *testing.T) {
|
|
|
|
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
|
|
|
defer test.MockVariableValue(&setting.Migrations.AllowLocalNetworks, true)()
|
|
|
|
defer test.MockVariableValue(&setting.Mirror.Enabled, true)()
|
|
|
|
require.NoError(t, migrations.Init())
|
|
|
|
|
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
|
|
srcRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
|
|
|
srcRepo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 3})
|
|
|
|
assert.False(t, srcRepo.HasWiki())
|
|
|
|
sess := loginUser(t, user.Name)
|
|
|
|
pushToRepo, _, f := tests.CreateDeclarativeRepoWithOptions(t, user, tests.DeclarativeRepoOptions{
|
|
|
|
Name: optional.Some("push-mirror-test"),
|
|
|
|
AutoInit: optional.Some(false),
|
|
|
|
EnabledUnits: optional.Some([]unit.Type{unit.TypeCode}),
|
|
|
|
})
|
|
|
|
defer f()
|
|
|
|
|
|
|
|
t.Run("Adding", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/settings", srcRepo2.FullName()), map[string]string{
|
|
|
|
"_csrf": GetCSRF(t, sess, fmt.Sprintf("/%s/settings", srcRepo2.FullName())),
|
|
|
|
"action": "push-mirror-add",
|
|
|
|
"push_mirror_address": u.String() + pushToRepo.FullName(),
|
|
|
|
"push_mirror_interval": "0",
|
|
|
|
})
|
|
|
|
sess.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
|
|
|
|
req = NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/settings", srcRepo.FullName()), map[string]string{
|
|
|
|
"_csrf": GetCSRF(t, sess, fmt.Sprintf("/%s/settings", srcRepo.FullName())),
|
|
|
|
"action": "push-mirror-add",
|
|
|
|
"push_mirror_address": u.String() + pushToRepo.FullName(),
|
|
|
|
"push_mirror_interval": "0",
|
|
|
|
})
|
|
|
|
sess.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
|
|
|
|
flashCookie := sess.GetCookie(gitea_context.CookieNameFlash)
|
|
|
|
assert.NotNil(t, flashCookie)
|
|
|
|
assert.Contains(t, flashCookie.Value, "success")
|
|
|
|
})
|
|
|
|
|
|
|
|
mirrors, _, err := repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo.ID, db.ListOptions{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, mirrors, 1)
|
|
|
|
mirrorID := mirrors[0].ID
|
|
|
|
|
|
|
|
mirrors, _, err = repo_model.GetPushMirrorsByRepoID(db.DefaultContext, srcRepo2.ID, db.ListOptions{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, mirrors, 1)
|
|
|
|
|
|
|
|
t.Run("Interval", func(t *testing.T) {
|
|
|
|
defer tests.PrintCurrentTest(t)()
|
|
|
|
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &repo_model.PushMirror{ID: mirrorID - 1})
|
|
|
|
|
|
|
|
req := NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/settings", srcRepo.FullName()), map[string]string{
|
|
|
|
"_csrf": GetCSRF(t, sess, fmt.Sprintf("/%s/settings", srcRepo.FullName())),
|
|
|
|
"action": "push-mirror-update",
|
|
|
|
"push_mirror_id": strconv.FormatInt(mirrorID-1, 10),
|
|
|
|
"push_mirror_interval": "10m0s",
|
|
|
|
})
|
|
|
|
sess.MakeRequest(t, req, http.StatusNotFound)
|
|
|
|
|
|
|
|
req = NewRequestWithValues(t, "POST", fmt.Sprintf("/%s/settings", srcRepo.FullName()), map[string]string{
|
|
|
|
"_csrf": GetCSRF(t, sess, fmt.Sprintf("/%s/settings", srcRepo.FullName())),
|
|
|
|
"action": "push-mirror-update",
|
|
|
|
"push_mirror_id": strconv.FormatInt(mirrorID, 10),
|
|
|
|
"push_mirror_interval": "10m0s",
|
|
|
|
})
|
|
|
|
sess.MakeRequest(t, req, http.StatusSeeOther)
|
|
|
|
|
|
|
|
flashCookie := sess.GetCookie(gitea_context.CookieNameFlash)
|
|
|
|
assert.NotNil(t, flashCookie)
|
|
|
|
assert.Contains(t, flashCookie.Value, "success")
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|