mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-27 04:43:59 +01:00
chore: improve slow tests
- Optimize generting random files. - Reduce big file of 128MiB to 32MiB (git was never made for large files anyways, but simply tests that it works). - Reduce looped git operations from 100 iterations to 10. - Add extra print statements to know what a slow test is doing, this also helps to see if a particular piece of code in a slow test is the culprit or if the test is just very extensive. - Set `[ui.notification].EVENT_SOURCE_UPDATE_TIME` to 1s to speed up `TestEventSourceManagerRun`. - Sneaked in some general test improvements.
This commit is contained in:
parent
9e95f80d94
commit
75a8b83946
13 changed files with 94 additions and 113 deletions
|
@ -302,12 +302,11 @@ jobs:
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(testCase.onType, func(t *testing.T) {
|
t.Run(testCase.onType, func(t *testing.T) {
|
||||||
|
defer tests.PrintCurrentTest(t)()
|
||||||
defer func() {
|
defer func() {
|
||||||
// cleanup leftovers, start from scratch
|
// cleanup leftovers, start from scratch
|
||||||
_, err = db.DeleteByBean(db.DefaultContext, actions_model.ActionRun{RepoID: baseRepo.ID})
|
unittest.AssertSuccessfulDelete(t, &actions_model.ActionRun{RepoID: baseRepo.ID})
|
||||||
require.NoError(t, err)
|
unittest.AssertSuccessfulDelete(t, &actions_model.ActionRunJob{RepoID: baseRepo.ID})
|
||||||
_, err = db.DeleteByBean(db.DefaultContext, actions_model.ActionRunJob{RepoID: baseRepo.ID})
|
|
||||||
require.NoError(t, err)
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// trigger the onType event
|
// trigger the onType event
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"code.gitea.io/gitea/models/db"
|
"code.gitea.io/gitea/models/db"
|
||||||
"code.gitea.io/gitea/models/unittest"
|
"code.gitea.io/gitea/models/unittest"
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
|
"code.gitea.io/gitea/tests"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -44,6 +45,7 @@ func Test_Cmd_AdminUser(t *testing.T) {
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(testCase.name, func(t *testing.T) {
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
|
defer tests.PrintCurrentTest(t)()
|
||||||
name := "testuser"
|
name := "testuser"
|
||||||
|
|
||||||
options := []string{"user", "create", "--username", name, "--password", "password", "--email", name + "@example.com"}
|
options := []string{"user", "create", "--username", name, "--password", "password", "--email", name + "@example.com"}
|
||||||
|
|
|
@ -15,6 +15,7 @@ import (
|
||||||
repo_model "code.gitea.io/gitea/models/repo"
|
repo_model "code.gitea.io/gitea/models/repo"
|
||||||
"code.gitea.io/gitea/models/unittest"
|
"code.gitea.io/gitea/models/unittest"
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
|
"code.gitea.io/gitea/tests"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -67,6 +68,7 @@ func Test_CmdForgejo_Actions(t *testing.T) {
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
t.Run(testCase.testName, func(t *testing.T) {
|
t.Run(testCase.testName, func(t *testing.T) {
|
||||||
|
defer tests.PrintCurrentTest(t)()
|
||||||
output, err := runMainApp("forgejo-cli", "actions", "register", "--secret", testCase.secret, "--scope", testCase.scope)
|
output, err := runMainApp("forgejo-cli", "actions", "register", "--secret", testCase.secret, "--scope", testCase.scope)
|
||||||
assert.EqualValues(t, "", output)
|
assert.EqualValues(t, "", output)
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,6 @@ import (
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/git"
|
"code.gitea.io/gitea/modules/git"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/ssh"
|
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
"code.gitea.io/gitea/tests"
|
"code.gitea.io/gitea/tests"
|
||||||
|
|
||||||
|
@ -33,8 +32,10 @@ func withKeyFile(t *testing.T, keyname string, callback func(string)) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
keyFile := filepath.Join(tmpDir, keyname)
|
keyFile := filepath.Join(tmpDir, keyname)
|
||||||
err = ssh.GenKeyPair(keyFile)
|
pubkey, privkey, err := util.GenerateSSHKeypair()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, os.WriteFile(keyFile, privkey, 0o600))
|
||||||
|
require.NoError(t, os.WriteFile(keyFile+".pub", pubkey, 0o600))
|
||||||
|
|
||||||
err = os.WriteFile(path.Join(tmpDir, "ssh"), []byte("#!/bin/bash\n"+
|
err = os.WriteFile(path.Join(tmpDir, "ssh"), []byte("#!/bin/bash\n"+
|
||||||
"ssh -o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\" -o \"IdentitiesOnly=yes\" -i \""+keyFile+"\" \"$@\""), 0o700)
|
"ssh -o \"UserKnownHostsFile=/dev/null\" -o \"StrictHostKeyChecking=no\" -o \"IdentitiesOnly=yes\" -i \""+keyFile+"\" \"$@\""), 0o700)
|
||||||
|
|
|
@ -19,6 +19,7 @@ import (
|
||||||
repo_module "code.gitea.io/gitea/modules/repository"
|
repo_module "code.gitea.io/gitea/modules/repository"
|
||||||
"code.gitea.io/gitea/modules/test"
|
"code.gitea.io/gitea/modules/test"
|
||||||
repo_service "code.gitea.io/gitea/services/repository"
|
repo_service "code.gitea.io/gitea/services/repository"
|
||||||
|
"code.gitea.io/gitea/tests"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -40,7 +41,7 @@ func testGitPush(t *testing.T, u *url.URL) {
|
||||||
forEachObjectFormat(t, func(t *testing.T, objectFormat git.ObjectFormat) {
|
forEachObjectFormat(t, func(t *testing.T, objectFormat git.ObjectFormat) {
|
||||||
t.Run("Push branches at once", func(t *testing.T) {
|
t.Run("Push branches at once", func(t *testing.T) {
|
||||||
runTestGitPush(t, u, objectFormat, func(t *testing.T, gitPath string) (pushed, deleted []string) {
|
runTestGitPush(t, u, objectFormat, func(t *testing.T, gitPath string) (pushed, deleted []string) {
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
branchName := fmt.Sprintf("branch-%d", i)
|
branchName := fmt.Sprintf("branch-%d", i)
|
||||||
pushed = append(pushed, branchName)
|
pushed = append(pushed, branchName)
|
||||||
doGitCreateBranch(gitPath, branchName)(t)
|
doGitCreateBranch(gitPath, branchName)(t)
|
||||||
|
@ -88,7 +89,7 @@ func testGitPush(t *testing.T, u *url.URL) {
|
||||||
|
|
||||||
t.Run("Push branches one by one", func(t *testing.T) {
|
t.Run("Push branches one by one", func(t *testing.T) {
|
||||||
runTestGitPush(t, u, objectFormat, func(t *testing.T, gitPath string) (pushed, deleted []string) {
|
runTestGitPush(t, u, objectFormat, func(t *testing.T, gitPath string) (pushed, deleted []string) {
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
branchName := fmt.Sprintf("branch-%d", i)
|
branchName := fmt.Sprintf("branch-%d", i)
|
||||||
doGitCreateBranch(gitPath, branchName)(t)
|
doGitCreateBranch(gitPath, branchName)(t)
|
||||||
doGitPushTestRepository(gitPath, "origin", branchName)(t)
|
doGitPushTestRepository(gitPath, "origin", branchName)(t)
|
||||||
|
@ -103,7 +104,7 @@ func testGitPush(t *testing.T, u *url.URL) {
|
||||||
doGitPushTestRepository(gitPath, "origin", "master")(t) // make sure master is the default branch instead of a branch we are going to delete
|
doGitPushTestRepository(gitPath, "origin", "master")(t) // make sure master is the default branch instead of a branch we are going to delete
|
||||||
pushed = append(pushed, "master")
|
pushed = append(pushed, "master")
|
||||||
|
|
||||||
for i := 0; i < 100; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
branchName := fmt.Sprintf("branch-%d", i)
|
branchName := fmt.Sprintf("branch-%d", i)
|
||||||
pushed = append(pushed, branchName)
|
pushed = append(pushed, branchName)
|
||||||
doGitCreateBranch(gitPath, branchName)(t)
|
doGitCreateBranch(gitPath, branchName)(t)
|
||||||
|
@ -139,6 +140,7 @@ func testGitPush(t *testing.T, u *url.URL) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func runTestGitPush(t *testing.T, u *url.URL, objectFormat git.ObjectFormat, gitOperation func(t *testing.T, gitPath string) (pushed, deleted []string)) {
|
func runTestGitPush(t *testing.T, u *url.URL, objectFormat git.ObjectFormat, gitOperation func(t *testing.T, gitPath string) (pushed, deleted []string)) {
|
||||||
|
defer tests.PrintCurrentTest(t, 1)()
|
||||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||||
repo, err := repo_service.CreateRepository(db.DefaultContext, user, user, repo_service.CreateRepoOptions{
|
repo, err := repo_service.CreateRepository(db.DefaultContext, user, user, repo_service.CreateRepoOptions{
|
||||||
Name: "repo-to-push",
|
Name: "repo-to-push",
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
@ -39,8 +40,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
littleSize = 1024 // 1ko
|
littleSize = 1024 // 1KiB
|
||||||
bigSize = 128 * 1024 * 1024 // 128Mo
|
bigSize = 32 * 1024 * 1024 // 32MiB
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGit(t *testing.T) {
|
func TestGit(t *testing.T) {
|
||||||
|
@ -299,53 +300,26 @@ func lockFileTest(t *testing.T, filename, repoPath string) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func doCommitAndPush(t *testing.T, size int, repoPath, prefix string) string {
|
func doCommitAndPush(t *testing.T, size int64, repoPath, prefix string) string {
|
||||||
name, err := generateCommitWithNewData(size, repoPath, "user2@example.com", "User Two", prefix)
|
name := generateCommitWithNewData(t, size, repoPath, "user2@example.com", "User Two", prefix)
|
||||||
require.NoError(t, err)
|
_, _, err := git.NewCommand(git.DefaultContext, "push", "origin", "master").RunStdString(&git.RunOpts{Dir: repoPath}) // Push
|
||||||
_, _, err = git.NewCommand(git.DefaultContext, "push", "origin", "master").RunStdString(&git.RunOpts{Dir: repoPath}) // Push
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
return name
|
return name
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateCommitWithNewData(size int, repoPath, email, fullName, prefix string) (string, error) {
|
func generateCommitWithNewData(t *testing.T, size int64, repoPath, email, fullName, prefix string) string {
|
||||||
// Generate random file
|
t.Helper()
|
||||||
bufSize := 4 * 1024
|
|
||||||
if bufSize > size {
|
|
||||||
bufSize = size
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer := make([]byte, bufSize)
|
|
||||||
|
|
||||||
tmpFile, err := os.CreateTemp(repoPath, prefix)
|
tmpFile, err := os.CreateTemp(repoPath, prefix)
|
||||||
if err != nil {
|
require.NoError(t, err)
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer tmpFile.Close()
|
defer tmpFile.Close()
|
||||||
written := 0
|
_, err = io.CopyN(tmpFile, rand.Reader, size)
|
||||||
for written < size {
|
require.NoError(t, err)
|
||||||
n := size - written
|
|
||||||
if n > bufSize {
|
|
||||||
n = bufSize
|
|
||||||
}
|
|
||||||
_, err := rand.Read(buffer[:n])
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
n, err = tmpFile.Write(buffer[:n])
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
written += n
|
|
||||||
}
|
|
||||||
|
|
||||||
// Commit
|
// Commit
|
||||||
// Now here we should explicitly allow lfs filters to run
|
// Now here we should explicitly allow lfs filters to run
|
||||||
globalArgs := git.AllowLFSFiltersArgs()
|
globalArgs := git.AllowLFSFiltersArgs()
|
||||||
err = git.AddChangesWithArgs(repoPath, globalArgs, false, filepath.Base(tmpFile.Name()))
|
require.NoError(t, git.AddChangesWithArgs(repoPath, globalArgs, false, filepath.Base(tmpFile.Name())))
|
||||||
if err != nil {
|
require.NoError(t, git.CommitChangesWithArgs(repoPath, globalArgs, git.CommitChangesOptions{
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
err = git.CommitChangesWithArgs(repoPath, globalArgs, git.CommitChangesOptions{
|
|
||||||
Committer: &git.Signature{
|
Committer: &git.Signature{
|
||||||
Email: email,
|
Email: email,
|
||||||
Name: fullName,
|
Name: fullName,
|
||||||
|
@ -357,8 +331,8 @@ func generateCommitWithNewData(size int, repoPath, email, fullName, prefix strin
|
||||||
When: time.Now(),
|
When: time.Now(),
|
||||||
},
|
},
|
||||||
Message: fmt.Sprintf("Testing commit @ %v", time.Now()),
|
Message: fmt.Sprintf("Testing commit @ %v", time.Now()),
|
||||||
})
|
}))
|
||||||
return filepath.Base(tmpFile.Name()), err
|
return filepath.Base(tmpFile.Name())
|
||||||
}
|
}
|
||||||
|
|
||||||
func doBranchProtect(baseCtx *APITestContext, dstPath string) func(t *testing.T) {
|
func doBranchProtect(baseCtx *APITestContext, dstPath string) func(t *testing.T) {
|
||||||
|
@ -370,6 +344,7 @@ func doBranchProtect(baseCtx *APITestContext, dstPath string) func(t *testing.T)
|
||||||
ctx := NewAPITestContext(t, baseCtx.Username, baseCtx.Reponame, auth_model.AccessTokenScopeWriteRepository)
|
ctx := NewAPITestContext(t, baseCtx.Username, baseCtx.Reponame, auth_model.AccessTokenScopeWriteRepository)
|
||||||
|
|
||||||
t.Run("PushToNewProtectedBranch", func(t *testing.T) {
|
t.Run("PushToNewProtectedBranch", func(t *testing.T) {
|
||||||
|
defer tests.PrintCurrentTest(t)()
|
||||||
t.Run("CreateBranchProtected", doGitCreateBranch(dstPath, "before-create-1"))
|
t.Run("CreateBranchProtected", doGitCreateBranch(dstPath, "before-create-1"))
|
||||||
t.Run("ProtectProtectedBranch", doProtectBranch(ctx, "before-create-1", parameterProtectBranch{
|
t.Run("ProtectProtectedBranch", doProtectBranch(ctx, "before-create-1", parameterProtectBranch{
|
||||||
"enable_push": "all",
|
"enable_push": "all",
|
||||||
|
@ -378,8 +353,7 @@ func doBranchProtect(baseCtx *APITestContext, dstPath string) func(t *testing.T)
|
||||||
t.Run("PushProtectedBranch", doGitPushTestRepository(dstPath, "origin", "before-create-1"))
|
t.Run("PushProtectedBranch", doGitPushTestRepository(dstPath, "origin", "before-create-1"))
|
||||||
|
|
||||||
t.Run("GenerateCommit", func(t *testing.T) {
|
t.Run("GenerateCommit", func(t *testing.T) {
|
||||||
_, err := generateCommitWithNewData(littleSize, dstPath, "user2@example.com", "User Two", "protected-file-data-")
|
generateCommitWithNewData(t, littleSize, dstPath, "user2@example.com", "User Two", "protected-file-data-")
|
||||||
require.NoError(t, err)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("ProtectProtectedBranch", doProtectBranch(ctx, "before-create-2", parameterProtectBranch{
|
t.Run("ProtectProtectedBranch", doProtectBranch(ctx, "before-create-2", parameterProtectBranch{
|
||||||
|
@ -392,11 +366,11 @@ func doBranchProtect(baseCtx *APITestContext, dstPath string) func(t *testing.T)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("FailToPushToProtectedBranch", func(t *testing.T) {
|
t.Run("FailToPushToProtectedBranch", func(t *testing.T) {
|
||||||
|
defer tests.PrintCurrentTest(t)()
|
||||||
t.Run("ProtectProtectedBranch", doProtectBranch(ctx, "protected"))
|
t.Run("ProtectProtectedBranch", doProtectBranch(ctx, "protected"))
|
||||||
t.Run("Create modified-protected-branch", doGitCheckoutBranch(dstPath, "-b", "modified-protected-branch", "protected"))
|
t.Run("Create modified-protected-branch", doGitCheckoutBranch(dstPath, "-b", "modified-protected-branch", "protected"))
|
||||||
t.Run("GenerateCommit", func(t *testing.T) {
|
t.Run("GenerateCommit", func(t *testing.T) {
|
||||||
_, err := generateCommitWithNewData(littleSize, dstPath, "user2@example.com", "User Two", "branch-data-file-")
|
generateCommitWithNewData(t, littleSize, dstPath, "user2@example.com", "User Two", "branch-data-file-")
|
||||||
require.NoError(t, err)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
doGitPushTestRepositoryFail(dstPath, "origin", "modified-protected-branch:protected")(t)
|
doGitPushTestRepositoryFail(dstPath, "origin", "modified-protected-branch:protected")(t)
|
||||||
|
@ -405,10 +379,10 @@ func doBranchProtect(baseCtx *APITestContext, dstPath string) func(t *testing.T)
|
||||||
t.Run("PushToUnprotectedBranch", doGitPushTestRepository(dstPath, "origin", "modified-protected-branch:unprotected"))
|
t.Run("PushToUnprotectedBranch", doGitPushTestRepository(dstPath, "origin", "modified-protected-branch:unprotected"))
|
||||||
|
|
||||||
t.Run("FailToPushProtectedFilesToProtectedBranch", func(t *testing.T) {
|
t.Run("FailToPushProtectedFilesToProtectedBranch", func(t *testing.T) {
|
||||||
|
defer tests.PrintCurrentTest(t)()
|
||||||
t.Run("Create modified-protected-file-protected-branch", doGitCheckoutBranch(dstPath, "-b", "modified-protected-file-protected-branch", "protected"))
|
t.Run("Create modified-protected-file-protected-branch", doGitCheckoutBranch(dstPath, "-b", "modified-protected-file-protected-branch", "protected"))
|
||||||
t.Run("GenerateCommit", func(t *testing.T) {
|
t.Run("GenerateCommit", func(t *testing.T) {
|
||||||
_, err := generateCommitWithNewData(littleSize, dstPath, "user2@example.com", "User Two", "protected-file-")
|
generateCommitWithNewData(t, littleSize, dstPath, "user2@example.com", "User Two", "protected-file-")
|
||||||
require.NoError(t, err)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("ProtectedFilePathsApplyToAdmins", doProtectBranch(ctx, "protected"))
|
t.Run("ProtectedFilePathsApplyToAdmins", doProtectBranch(ctx, "protected"))
|
||||||
|
@ -419,13 +393,13 @@ func doBranchProtect(baseCtx *APITestContext, dstPath string) func(t *testing.T)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("PushUnprotectedFilesToProtectedBranch", func(t *testing.T) {
|
t.Run("PushUnprotectedFilesToProtectedBranch", func(t *testing.T) {
|
||||||
|
defer tests.PrintCurrentTest(t)()
|
||||||
t.Run("Create modified-unprotected-file-protected-branch", doGitCheckoutBranch(dstPath, "-b", "modified-unprotected-file-protected-branch", "protected"))
|
t.Run("Create modified-unprotected-file-protected-branch", doGitCheckoutBranch(dstPath, "-b", "modified-unprotected-file-protected-branch", "protected"))
|
||||||
t.Run("UnprotectedFilePaths", doProtectBranch(ctx, "protected", parameterProtectBranch{
|
t.Run("UnprotectedFilePaths", doProtectBranch(ctx, "protected", parameterProtectBranch{
|
||||||
"unprotected_file_patterns": "unprotected-file-*",
|
"unprotected_file_patterns": "unprotected-file-*",
|
||||||
}))
|
}))
|
||||||
t.Run("GenerateCommit", func(t *testing.T) {
|
t.Run("GenerateCommit", func(t *testing.T) {
|
||||||
_, err := generateCommitWithNewData(littleSize, dstPath, "user2@example.com", "User Two", "unprotected-file-")
|
generateCommitWithNewData(t, littleSize, dstPath, "user2@example.com", "User Two", "unprotected-file-")
|
||||||
require.NoError(t, err)
|
|
||||||
})
|
})
|
||||||
doGitPushTestRepository(dstPath, "origin", "modified-unprotected-file-protected-branch:protected")(t)
|
doGitPushTestRepository(dstPath, "origin", "modified-unprotected-file-protected-branch:protected")(t)
|
||||||
doGitCheckoutBranch(dstPath, "protected")(t)
|
doGitCheckoutBranch(dstPath, "protected")(t)
|
||||||
|
@ -441,19 +415,19 @@ func doBranchProtect(baseCtx *APITestContext, dstPath string) func(t *testing.T)
|
||||||
}))
|
}))
|
||||||
|
|
||||||
t.Run("WhitelistedUserFailToForcePushToProtectedBranch", func(t *testing.T) {
|
t.Run("WhitelistedUserFailToForcePushToProtectedBranch", func(t *testing.T) {
|
||||||
|
defer tests.PrintCurrentTest(t)()
|
||||||
t.Run("Create toforce", doGitCheckoutBranch(dstPath, "-b", "toforce", "master"))
|
t.Run("Create toforce", doGitCheckoutBranch(dstPath, "-b", "toforce", "master"))
|
||||||
t.Run("GenerateCommit", func(t *testing.T) {
|
t.Run("GenerateCommit", func(t *testing.T) {
|
||||||
_, err := generateCommitWithNewData(littleSize, dstPath, "user2@example.com", "User Two", "branch-data-file-")
|
generateCommitWithNewData(t, littleSize, dstPath, "user2@example.com", "User Two", "branch-data-file-")
|
||||||
require.NoError(t, err)
|
|
||||||
})
|
})
|
||||||
doGitPushTestRepositoryFail(dstPath, "-f", "origin", "toforce:protected")(t)
|
doGitPushTestRepositoryFail(dstPath, "-f", "origin", "toforce:protected")(t)
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("WhitelistedUserPushToProtectedBranch", func(t *testing.T) {
|
t.Run("WhitelistedUserPushToProtectedBranch", func(t *testing.T) {
|
||||||
|
defer tests.PrintCurrentTest(t)()
|
||||||
t.Run("Create topush", doGitCheckoutBranch(dstPath, "-b", "topush", "protected"))
|
t.Run("Create topush", doGitCheckoutBranch(dstPath, "-b", "topush", "protected"))
|
||||||
t.Run("GenerateCommit", func(t *testing.T) {
|
t.Run("GenerateCommit", func(t *testing.T) {
|
||||||
_, err := generateCommitWithNewData(littleSize, dstPath, "user2@example.com", "User Two", "branch-data-file-")
|
generateCommitWithNewData(t, littleSize, dstPath, "user2@example.com", "User Two", "branch-data-file-")
|
||||||
require.NoError(t, err)
|
|
||||||
})
|
})
|
||||||
doGitPushTestRepository(dstPath, "origin", "topush:protected")(t)
|
doGitPushTestRepository(dstPath, "origin", "topush:protected")(t)
|
||||||
})
|
})
|
||||||
|
@ -693,8 +667,7 @@ func doAutoPRMerge(baseCtx *APITestContext, dstPath string) func(t *testing.T) {
|
||||||
t.Run("CheckoutProtected", doGitCheckoutBranch(dstPath, "protected"))
|
t.Run("CheckoutProtected", doGitCheckoutBranch(dstPath, "protected"))
|
||||||
t.Run("PullProtected", doGitPull(dstPath, "origin", "protected"))
|
t.Run("PullProtected", doGitPull(dstPath, "origin", "protected"))
|
||||||
t.Run("GenerateCommit", func(t *testing.T) {
|
t.Run("GenerateCommit", func(t *testing.T) {
|
||||||
_, err := generateCommitWithNewData(littleSize, dstPath, "user2@example.com", "User Two", "branch-data-file-")
|
generateCommitWithNewData(t, littleSize, dstPath, "user2@example.com", "User Two", "branch-data-file-")
|
||||||
require.NoError(t, err)
|
|
||||||
})
|
})
|
||||||
t.Run("PushToUnprotectedBranch", doGitPushTestRepository(dstPath, "origin", "protected:unprotected3"))
|
t.Run("PushToUnprotectedBranch", doGitPushTestRepository(dstPath, "origin", "protected:unprotected3"))
|
||||||
var pr api.PullRequest
|
var pr api.PullRequest
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
package integration
|
package integration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -21,9 +20,10 @@ import (
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/structs"
|
"code.gitea.io/gitea/modules/structs"
|
||||||
|
"code.gitea.io/gitea/modules/test"
|
||||||
"code.gitea.io/gitea/modules/translation"
|
"code.gitea.io/gitea/modules/translation"
|
||||||
"code.gitea.io/gitea/services/migrations"
|
"code.gitea.io/gitea/services/migrations"
|
||||||
"code.gitea.io/gitea/services/repository"
|
"code.gitea.io/gitea/tests"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -58,16 +58,8 @@ func TestMigrateLocalPath(t *testing.T) {
|
||||||
|
|
||||||
func TestMigrate(t *testing.T) {
|
func TestMigrate(t *testing.T) {
|
||||||
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
||||||
AllowLocalNetworks := setting.Migrations.AllowLocalNetworks
|
defer test.MockVariableValue(&setting.Migrations.AllowLocalNetworks, true)()
|
||||||
setting.Migrations.AllowLocalNetworks = true
|
defer test.MockVariableValue(&setting.AppVer, "1.16.0")()
|
||||||
AppVer := setting.AppVer
|
|
||||||
// Gitea SDK (go-sdk) need to parse the AppVer from server response, so we must set it to a valid version string.
|
|
||||||
setting.AppVer = "1.16.0"
|
|
||||||
defer func() {
|
|
||||||
setting.Migrations.AllowLocalNetworks = AllowLocalNetworks
|
|
||||||
setting.AppVer = AppVer
|
|
||||||
migrations.Init()
|
|
||||||
}()
|
|
||||||
require.NoError(t, migrations.Init())
|
require.NoError(t, migrations.Init())
|
||||||
|
|
||||||
ownerName := "user2"
|
ownerName := "user2"
|
||||||
|
@ -82,42 +74,40 @@ func TestMigrate(t *testing.T) {
|
||||||
{svc: structs.GiteaService},
|
{svc: structs.GiteaService},
|
||||||
{svc: structs.ForgejoService},
|
{svc: structs.ForgejoService},
|
||||||
} {
|
} {
|
||||||
// Step 0: verify the repo is available
|
t.Run(s.svc.Name(), func(t *testing.T) {
|
||||||
req := NewRequestf(t, "GET", "/%s/%s", ownerName, repoName)
|
defer tests.PrintCurrentTest(t)()
|
||||||
_ = session.MakeRequest(t, req, http.StatusOK)
|
// Step 0: verify the repo is available
|
||||||
// Step 1: get the Gitea migration form
|
req := NewRequestf(t, "GET", "/%s/%s", ownerName, repoName)
|
||||||
req = NewRequestf(t, "GET", "/repo/migrate/?service_type=%d", s.svc)
|
_ = session.MakeRequest(t, req, http.StatusOK)
|
||||||
resp := session.MakeRequest(t, req, http.StatusOK)
|
// Step 1: get the Gitea migration form
|
||||||
// Step 2: load the form
|
req = NewRequestf(t, "GET", "/repo/migrate/?service_type=%d", s.svc)
|
||||||
htmlDoc := NewHTMLParser(t, resp.Body)
|
resp := session.MakeRequest(t, req, http.StatusOK)
|
||||||
// Check form title
|
// Step 2: load the form
|
||||||
title := htmlDoc.doc.Find("title").Text()
|
htmlDoc := NewHTMLParser(t, resp.Body)
|
||||||
assert.Contains(t, title, translation.NewLocale("en-US").TrString("new_migrate.title"))
|
// Check form title
|
||||||
// Get the link of migration button
|
title := htmlDoc.doc.Find("title").Text()
|
||||||
link, exists := htmlDoc.doc.Find(`form.ui.form[action^="/repo/migrate"]`).Attr("action")
|
assert.Contains(t, title, translation.NewLocale("en-US").TrString("new_migrate.title"))
|
||||||
assert.True(t, exists, "The template has changed")
|
// Get the link of migration button
|
||||||
// Step 4: submit the migration to only migrate issues
|
link, exists := htmlDoc.doc.Find(`form.ui.form[action^="/repo/migrate"]`).Attr("action")
|
||||||
migratedRepoName := "otherrepo"
|
assert.True(t, exists, "The template has changed")
|
||||||
req = NewRequestWithValues(t, "POST", link, map[string]string{
|
// Step 4: submit the migration to only migrate issues
|
||||||
"_csrf": htmlDoc.GetCSRF(),
|
migratedRepoName := "otherrepo-" + s.svc.Name()
|
||||||
"service": fmt.Sprintf("%d", s.svc),
|
req = NewRequestWithValues(t, "POST", link, map[string]string{
|
||||||
"clone_addr": fmt.Sprintf("%s%s/%s", u, ownerName, repoName),
|
"_csrf": htmlDoc.GetCSRF(),
|
||||||
"auth_token": token,
|
"service": fmt.Sprintf("%d", s.svc),
|
||||||
"issues": "on",
|
"clone_addr": fmt.Sprintf("%s%s/%s", u, ownerName, repoName),
|
||||||
"repo_name": migratedRepoName,
|
"auth_token": token,
|
||||||
"description": "",
|
"issues": "on",
|
||||||
"uid": fmt.Sprintf("%d", repoOwner.ID),
|
"repo_name": migratedRepoName,
|
||||||
|
"description": "",
|
||||||
|
"uid": fmt.Sprintf("%d", repoOwner.ID),
|
||||||
|
})
|
||||||
|
resp = session.MakeRequest(t, req, http.StatusSeeOther)
|
||||||
|
// Step 5: a redirection displays the migrated repository
|
||||||
|
assert.EqualValues(t, fmt.Sprintf("/%s/%s", ownerName, migratedRepoName), test.RedirectURL(resp))
|
||||||
|
// Step 6: check the repo was created
|
||||||
|
unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: migratedRepoName})
|
||||||
})
|
})
|
||||||
resp = session.MakeRequest(t, req, http.StatusSeeOther)
|
|
||||||
// Step 5: a redirection displays the migrated repository
|
|
||||||
loc := resp.Header().Get("Location")
|
|
||||||
assert.EqualValues(t, fmt.Sprintf("/%s/%s", ownerName, migratedRepoName), loc)
|
|
||||||
// Step 6: check the repo was created
|
|
||||||
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: migratedRepoName})
|
|
||||||
|
|
||||||
// Step 7: delete the repository, so we can test with other services
|
|
||||||
err := repository.DeleteRepository(context.Background(), repoOwner, repo, false)
|
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -822,6 +822,7 @@ func TestPullMergeBranchProtect(t *testing.T) {
|
||||||
}
|
}
|
||||||
for _, withAPIOrWeb := range []string{"api", "web"} {
|
for _, withAPIOrWeb := range []string{"api", "web"} {
|
||||||
t.Run(testCase.name+" "+withAPIOrWeb, func(t *testing.T) {
|
t.Run(testCase.name+" "+withAPIOrWeb, func(t *testing.T) {
|
||||||
|
defer tests.PrintCurrentTest(t)()
|
||||||
branch := testCase.name + "-" + withAPIOrWeb
|
branch := testCase.name + "-" + withAPIOrWeb
|
||||||
unprotected := branch + "-unprotected"
|
unprotected := branch + "-unprotected"
|
||||||
doGitCheckoutBranch(dstPath, "master")(t)
|
doGitCheckoutBranch(dstPath, "master")(t)
|
||||||
|
@ -834,8 +835,7 @@ func TestPullMergeBranchProtect(t *testing.T) {
|
||||||
ctx = NewAPITestContext(t, testCase.doer, "not used", auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
|
ctx = NewAPITestContext(t, testCase.doer, "not used", auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
|
||||||
ctx.Username = owner
|
ctx.Username = owner
|
||||||
ctx.Reponame = repo
|
ctx.Reponame = repo
|
||||||
_, err := generateCommitWithNewData(littleSize, dstPath, "user2@example.com", "User Two", testCase.filename)
|
generateCommitWithNewData(t, littleSize, dstPath, "user2@example.com", "User Two", testCase.filename)
|
||||||
require.NoError(t, err)
|
|
||||||
doGitPushTestRepository(dstPath, "origin", branch+":"+unprotected)(t)
|
doGitPushTestRepository(dstPath, "origin", branch+":"+unprotected)(t)
|
||||||
pr, err := doAPICreatePullRequest(ctx, owner, repo, branch, unprotected)(t)
|
pr, err := doAPICreatePullRequest(ctx, owner, repo, branch, unprotected)(t)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -1015,6 +1015,7 @@ func TestPullAutoMergeAfterCommitStatusSucceed(t *testing.T) {
|
||||||
// perform all tests with API and web routes
|
// perform all tests with API and web routes
|
||||||
for _, withAPI := range []bool{false, true} {
|
for _, withAPI := range []bool{false, true} {
|
||||||
t.Run(testCase.name, func(t *testing.T) {
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
|
defer tests.PrintCurrentTest(t)()
|
||||||
protectedBranch := parameterProtectBranch{
|
protectedBranch := parameterProtectBranch{
|
||||||
"enable_push": "true",
|
"enable_push": "true",
|
||||||
"enable_status_check": "true",
|
"enable_status_check": "true",
|
||||||
|
|
|
@ -316,6 +316,7 @@ func assertInput(t testing.TB, form *goquery.Selection, name string) string {
|
||||||
|
|
||||||
func testWebhookForms(name string, session *TestSession, validFields map[string]string, invalidPatches ...map[string]string) func(t *testing.T) {
|
func testWebhookForms(name string, session *TestSession, validFields map[string]string, invalidPatches ...map[string]string) func(t *testing.T) {
|
||||||
return func(t *testing.T) {
|
return func(t *testing.T) {
|
||||||
|
defer tests.PrintCurrentTest(t)()
|
||||||
t.Run("repo1", func(t *testing.T) {
|
t.Run("repo1", func(t *testing.T) {
|
||||||
testWebhookFormsShared(t, "/user2/repo1/settings/hooks", name, session, validFields, invalidPatches...)
|
testWebhookFormsShared(t, "/user2/repo1/settings/hooks", name, session, validFields, invalidPatches...)
|
||||||
})
|
})
|
||||||
|
|
|
@ -98,6 +98,7 @@ func (countTest *userCountTest) getCount(doc *goquery.Document, name string) (in
|
||||||
|
|
||||||
func (countTest *userCountTest) TestPage(t *testing.T, page string, orgLink bool) {
|
func (countTest *userCountTest) TestPage(t *testing.T, page string, orgLink bool) {
|
||||||
t.Run(page, func(t *testing.T) {
|
t.Run(page, func(t *testing.T) {
|
||||||
|
defer tests.PrintCurrentTest(t)()
|
||||||
var userLink string
|
var userLink string
|
||||||
|
|
||||||
if orgLink {
|
if orgLink {
|
||||||
|
|
|
@ -113,3 +113,6 @@ REPLY_TO_ADDRESS = incoming+%{token}@localhost
|
||||||
|
|
||||||
[actions]
|
[actions]
|
||||||
ENABLED = true
|
ENABLED = true
|
||||||
|
|
||||||
|
[ui.notification]
|
||||||
|
EVENT_SOURCE_UPDATE_TIME = 1s
|
||||||
|
|
|
@ -127,3 +127,6 @@ ENABLED = true
|
||||||
|
|
||||||
[actions]
|
[actions]
|
||||||
ENABLED = true
|
ENABLED = true
|
||||||
|
|
||||||
|
[ui.notification]
|
||||||
|
EVENT_SOURCE_UPDATE_TIME = 1s
|
||||||
|
|
|
@ -113,3 +113,6 @@ RENDER_CONTENT_MODE=sanitized
|
||||||
|
|
||||||
[actions]
|
[actions]
|
||||||
ENABLED = true
|
ENABLED = true
|
||||||
|
|
||||||
|
[ui.notification]
|
||||||
|
EVENT_SOURCE_UPDATE_TIME = 1s
|
||||||
|
|
Loading…
Reference in a new issue