mirror of
https://github.com/go-gitea/gitea.git
synced 2026-05-16 14:47:48 +02:00
fixing tests
This commit is contained in:
parent
c16ad5643a
commit
3641d4569b
@ -4,6 +4,8 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"path"
|
||||
@ -36,77 +38,119 @@ func testGitSizeLimitInternal(t *testing.T, u *url.URL) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
repoName := "repo-git-under"
|
||||
ctx := NewAPITestContext(t, username, repoName, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
|
||||
|
||||
// Cleanup: reset limits and delete repository
|
||||
defer func() {
|
||||
setting.Repository.GitSizeMax = -1
|
||||
setting.Repository.LFSSizeMax = -1
|
||||
}()
|
||||
defer doAPIDeleteRepository(ctx)
|
||||
|
||||
doAPICreateRepository(ctx, false)(t)
|
||||
dstPath := t.TempDir()
|
||||
u.Path = ctx.GitPath()
|
||||
doGitClone(dstPath, u)(t)
|
||||
|
||||
// Phase 1: Push with no limit
|
||||
setting.Repository.GitSizeMax = -1
|
||||
doCommitAndPush(t, 1024, dstPath, "under-")
|
||||
setting.Repository.LFSSizeMax = -1
|
||||
doCommitAndPush(t, 1024, dstPath, "under-phase1-")
|
||||
|
||||
// Phase 2: Push with limit enabled but not exceeded
|
||||
setting.Repository.GitSizeMax = 50 * 1024 // 50 KiB
|
||||
doCommitAndPush(t, 1024, dstPath, "under-phase2-")
|
||||
})
|
||||
|
||||
t.Run("Over", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
repoName := "repo-git-over"
|
||||
ctx := NewAPITestContext(t, username, repoName, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
|
||||
|
||||
// Cleanup: reset limits and delete repository
|
||||
defer func() {
|
||||
setting.Repository.GitSizeMax = -1
|
||||
setting.Repository.LFSSizeMax = -1
|
||||
}()
|
||||
defer doAPIDeleteRepository(ctx)
|
||||
|
||||
doAPICreateRepository(ctx, false)(t)
|
||||
dstPath := t.TempDir()
|
||||
u.Path = ctx.GitPath()
|
||||
doGitClone(dstPath, u)(t)
|
||||
|
||||
// Set restrictive limit and attempt push
|
||||
setting.Repository.GitSizeMax = 100
|
||||
setting.Repository.LFSSizeMax = -1
|
||||
doCommitAndPushWithExpectedError(t, 1024, dstPath, "over-")
|
||||
setting.Repository.GitSizeMax = -1
|
||||
})
|
||||
|
||||
t.Run("UnderAfterResize", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
repoName := "repo-git-resize"
|
||||
ctx := NewAPITestContext(t, username, repoName, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
|
||||
|
||||
// Cleanup: reset limits and delete repository
|
||||
defer func() {
|
||||
setting.Repository.GitSizeMax = -1
|
||||
setting.Repository.LFSSizeMax = -1
|
||||
}()
|
||||
defer doAPIDeleteRepository(ctx)
|
||||
|
||||
doAPICreateRepository(ctx, false)(t)
|
||||
dstPath := t.TempDir()
|
||||
u.Path = ctx.GitPath()
|
||||
doGitClone(dstPath, u)(t)
|
||||
|
||||
setting.Repository.GitSizeMax = 1024 * 100
|
||||
doCommitAndPush(t, 1024, dstPath, "resize-")
|
||||
setting.Repository.GitSizeMax = -1
|
||||
// Attempt push with restrictive limit - should fail
|
||||
setting.Repository.GitSizeMax = 100
|
||||
setting.Repository.LFSSizeMax = -1
|
||||
doCommitAndPushWithExpectedError(t, 1024, dstPath, "resize-")
|
||||
|
||||
// Increase limit and retry same push - should succeed
|
||||
setting.Repository.GitSizeMax = 30 * 1024 // 30 KiB
|
||||
_, _, err := gitcmd.NewCommand("push", "origin", "master").WithDir(dstPath).RunStdString(t.Context())
|
||||
assert.NoError(t, err, "Push should succeed after limit increase")
|
||||
})
|
||||
|
||||
t.Run("Deletion", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
repoName := "repo-git-delete"
|
||||
ctx := NewAPITestContext(t, username, repoName, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
|
||||
doAPICreateRepository(ctx, false)(t)
|
||||
dstPath := t.TempDir()
|
||||
u.Path = ctx.GitPath()
|
||||
doGitClone(dstPath, u)(t)
|
||||
|
||||
setting.Repository.GitSizeMax = -1
|
||||
doCommitAndPush(t, 1024, dstPath, "delete-base-")
|
||||
bigFileName := doCommitAndPush(t, 1024*10, dstPath, "delete-big-")
|
||||
|
||||
lastCommitID := doGetAddCommitID(t, dstPath, bigFileName)
|
||||
doDeleteAndPush(t, dstPath, bigFileName)
|
||||
doRebaseCommitAndPush(t, dstPath, lastCommitID)
|
||||
})
|
||||
|
||||
t.Run("SoftEnforcement", func(t *testing.T) {
|
||||
t.Run("DeletionAndSoftEnforcement", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
repoName := "repo-git-soft"
|
||||
ctx := NewAPITestContext(t, username, repoName, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
|
||||
|
||||
// Cleanup: reset limits and delete repository
|
||||
defer func() {
|
||||
setting.Repository.GitSizeMax = -1
|
||||
setting.Repository.LFSSizeMax = -1
|
||||
}()
|
||||
defer doAPIDeleteRepository(ctx)
|
||||
|
||||
doAPICreateRepository(ctx, false)(t)
|
||||
dstPath := t.TempDir()
|
||||
u.Path = ctx.GitPath()
|
||||
doGitClone(dstPath, u)(t)
|
||||
|
||||
// Step 1: Push 1KB file with no limit
|
||||
setting.Repository.GitSizeMax = -1
|
||||
setting.Repository.LFSSizeMax = -1
|
||||
doCommitAndPush(t, 1024, dstPath, "soft-base-")
|
||||
|
||||
// Lenient pass for soft enforcement
|
||||
doCommitAndPush(t, 1024, dstPath, "soft-more-")
|
||||
// Step 2: Push 10KB file
|
||||
doCommitAndPush(t, 10*1024, dstPath, "soft-big-")
|
||||
|
||||
// Step 3: Delete big file using reset
|
||||
_, _, err := gitcmd.NewCommand("reset", "--hard", "HEAD~1").WithDir(dstPath).RunStdString(t.Context())
|
||||
assert.NoError(t, err, "Reset should succeed")
|
||||
|
||||
// Step 4: Set very restrictive limit
|
||||
setting.Repository.GitSizeMax = 10 // 10 bytes
|
||||
|
||||
// Step 5: Force push - should succeed (soft enforcement)
|
||||
_, _, err = gitcmd.NewCommand("push", "--force-with-lease", "origin", "master").WithDir(dstPath).RunStdString(t.Context())
|
||||
assert.NoError(t, err, "Force push should succeed with soft enforcement")
|
||||
|
||||
// Step 6: Try to push another 1KB file - should fail
|
||||
doCommitAndPushWithExpectedError(t, 1024, dstPath, "soft-new-")
|
||||
})
|
||||
setting.Repository.GitSizeMax = -1
|
||||
}
|
||||
|
||||
func testLFSSizeLimitInternal(t *testing.T, u *url.URL) {
|
||||
@ -125,58 +169,110 @@ func testLFSSizeLimitInternal(t *testing.T, u *url.URL) {
|
||||
assert.NoError(t, err)
|
||||
err = gitcmd.NewCommand("commit", "-m", "Track LFS").WithDir(dstPath).Run(t.Context())
|
||||
assert.NoError(t, err)
|
||||
err = gitcmd.NewCommand("push", "origin", "master").WithDir(dstPath).Run(t.Context())
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
t.Run("PushUnderLimit", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
repoName := "repo-lfs-under"
|
||||
ctx := NewAPITestContext(t, username, repoName, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
|
||||
|
||||
// Cleanup: reset limits and delete repository
|
||||
defer func() {
|
||||
setting.Repository.GitSizeMax = -1
|
||||
setting.Repository.LFSSizeMax = -1
|
||||
}()
|
||||
defer doAPIDeleteRepository(ctx)
|
||||
|
||||
doAPICreateRepository(ctx, false)(t)
|
||||
dstPath := t.TempDir()
|
||||
u.Path = ctx.GitPath()
|
||||
doGitClone(dstPath, u)(t)
|
||||
setupLFS(t, dstPath)
|
||||
|
||||
// Push with limit enabled but not exceeded
|
||||
setting.Repository.GitSizeMax = -1
|
||||
setting.Repository.LFSSizeMax = 10000
|
||||
doCommitAndPushWithData(t, dstPath, "data-under.dat", "some-content-under")
|
||||
setting.Repository.LFSSizeMax = -1
|
||||
})
|
||||
|
||||
t.Run("PushOverLimit", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
repoName := "repo-lfs-over"
|
||||
ctx := NewAPITestContext(t, username, repoName, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
|
||||
|
||||
// Cleanup: reset limits and delete repository
|
||||
defer func() {
|
||||
setting.Repository.GitSizeMax = -1
|
||||
setting.Repository.LFSSizeMax = -1
|
||||
}()
|
||||
defer doAPIDeleteRepository(ctx)
|
||||
|
||||
doAPICreateRepository(ctx, false)(t)
|
||||
dstPath := t.TempDir()
|
||||
u.Path = ctx.GitPath()
|
||||
doGitClone(dstPath, u)(t)
|
||||
setupLFS(t, dstPath)
|
||||
|
||||
// Push with restrictive limit - should fail
|
||||
setting.Repository.GitSizeMax = -1
|
||||
setting.Repository.LFSSizeMax = 5
|
||||
doCommitAndPushWithDataWithExpectedError(t, dstPath, "data-over.dat", "some-content-over-limit")
|
||||
setting.Repository.LFSSizeMax = -1
|
||||
})
|
||||
|
||||
t.Run("SoftEnforcement", func(t *testing.T) {
|
||||
defer tests.PrintCurrentTest(t)()
|
||||
repoName := "repo-lfs-soft"
|
||||
repoName := "repo-lfs-soft-enforce"
|
||||
ctx := NewAPITestContext(t, username, repoName, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
|
||||
|
||||
// Cleanup: reset limits and delete repository
|
||||
defer func() {
|
||||
setting.Repository.GitSizeMax = -1
|
||||
setting.Repository.LFSSizeMax = -1
|
||||
}()
|
||||
defer doAPIDeleteRepository(ctx)
|
||||
|
||||
doAPICreateRepository(ctx, false)(t)
|
||||
dstPath := t.TempDir()
|
||||
u.Path = ctx.GitPath()
|
||||
doGitClone(dstPath, u)(t)
|
||||
setupLFS(t, dstPath)
|
||||
|
||||
// Step 1 & 2: Init LFS and push 1024B file with random content (Commit 1)
|
||||
setting.Repository.GitSizeMax = -1
|
||||
setting.Repository.LFSSizeMax = -1
|
||||
doCommitAndPushWithData(t, dstPath, "data-soft.dat", "some-content-soft")
|
||||
doCommitAndPushLFSWithRandomData(t, dstPath, "data-soft-1.dat", 1024)
|
||||
|
||||
// Lenient pass for soft enforcement
|
||||
doCommitAndPushWithData(t, dstPath, "data-soft-more.dat", "some-more-content")
|
||||
// Step 3: Push 10240B LFS file with random content (Commit 2)
|
||||
doCommitAndPushLFSWithRandomData(t, dstPath, "data-soft-2.dat", 10240)
|
||||
|
||||
// Step 4: Set limit to 10 KiB (1 KiB below current ~11 KiB)
|
||||
setting.Repository.LFSSizeMax = 10 * 1024
|
||||
|
||||
// Step 5: Try to push 1KB LFS file - should fail (Commit 3, local only)
|
||||
err := os.WriteFile(path.Join(dstPath, "data-soft-3.dat"), generateRandomData(1024), 0o644)
|
||||
assert.NoError(t, err)
|
||||
err = gitcmd.NewCommand("add", "data-soft-3.dat").WithDir(dstPath).Run(t.Context())
|
||||
assert.NoError(t, err)
|
||||
err = gitcmd.NewCommand("commit", "-m", "Add data-soft-3.dat").WithDir(dstPath).Run(t.Context())
|
||||
assert.NoError(t, err)
|
||||
err = gitcmd.NewCommand("push", "origin", "master").WithDir(dstPath).Run(t.Context())
|
||||
assert.Error(t, err, "Push should fail when exceeding LFS limit")
|
||||
|
||||
// Step 6: Reset to Commit 1 (removes Commits 2 & 3)
|
||||
_, _, err = gitcmd.NewCommand("reset", "--hard", "HEAD~2").WithDir(dstPath).RunStdString(t.Context())
|
||||
assert.NoError(t, err, "Reset should succeed")
|
||||
_, _, err = gitcmd.NewCommand("push", "--force-with-lease", "origin", "master").WithDir(dstPath).RunStdString(t.Context())
|
||||
assert.NoError(t, err, "Force push should succeed with soft enforcement")
|
||||
|
||||
// Step 7: Try to push 1024B LFS file - should still fail
|
||||
doCommitAndPushLFSWithRandomDataWithExpectedError(t, dstPath, "data-soft-new.dat", 1024)
|
||||
})
|
||||
setting.Repository.LFSSizeMax = -1
|
||||
}
|
||||
|
||||
// Local helpers reuse existing logic but adapted for LFS tracking or direct data writing
|
||||
// Helper functions
|
||||
|
||||
func doCommitAndPushWithData(t *testing.T, repoPath, filename, content string) {
|
||||
err := os.WriteFile(path.Join(repoPath, filename), []byte(content), 0o644)
|
||||
assert.NoError(t, err)
|
||||
@ -199,4 +295,44 @@ func doCommitAndPushWithDataWithExpectedError(t *testing.T, repoPath, filename,
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
// Reuse global helpers for Git: doCommitAndPush, doCommitAndPushWithExpectedError, doDeleteAndPush, doRebaseCommitAndPush, doGetAddCommitID
|
||||
func generateRandomData(size int) []byte {
|
||||
data := make([]byte, size)
|
||||
_, _ = io.ReadFull(rand.Reader, data)
|
||||
return data
|
||||
}
|
||||
|
||||
func doCommitAndPushLFSWithRandomData(t *testing.T, repoPath, filename string, size int) {
|
||||
err := os.WriteFile(path.Join(repoPath, filename), generateRandomData(size), 0o644)
|
||||
assert.NoError(t, err)
|
||||
err = gitcmd.NewCommand("add").AddDynamicArguments(filename).WithDir(repoPath).Run(t.Context())
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Verify file is tracked by LFS
|
||||
stdout, _, err := gitcmd.NewCommand("lfs", "ls-files").WithDir(repoPath).RunStdString(t.Context())
|
||||
assert.NoError(t, err, "git lfs ls-files should succeed")
|
||||
assert.Contains(t, stdout, filename, "File %s should be tracked by LFS", filename)
|
||||
|
||||
err = gitcmd.NewCommand("commit", "-m").AddDynamicArguments("Add " + filename).WithDir(repoPath).Run(t.Context())
|
||||
assert.NoError(t, err)
|
||||
err = gitcmd.NewCommand("push", "origin", "master").WithDir(repoPath).Run(t.Context())
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func doCommitAndPushLFSWithRandomDataWithExpectedError(t *testing.T, repoPath, filename string, size int) {
|
||||
err := os.WriteFile(path.Join(repoPath, filename), generateRandomData(size), 0o644)
|
||||
assert.NoError(t, err)
|
||||
err = gitcmd.NewCommand("add").AddDynamicArguments(filename).WithDir(repoPath).Run(t.Context())
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Verify file is tracked by LFS
|
||||
stdout, _, err := gitcmd.NewCommand("lfs", "ls-files").WithDir(repoPath).RunStdString(t.Context())
|
||||
assert.NoError(t, err, "git lfs ls-files should succeed")
|
||||
assert.Contains(t, stdout, filename, "File %s should be tracked by LFS", filename)
|
||||
|
||||
err = gitcmd.NewCommand("commit", "-m").AddDynamicArguments("Add " + filename).WithDir(repoPath).Run(t.Context())
|
||||
assert.NoError(t, err)
|
||||
err = gitcmd.NewCommand("push", "origin", "master").WithDir(repoPath).Run(t.Context())
|
||||
assert.Error(t, err)
|
||||
}
|
||||
|
||||
// Reuse global helpers for Git: doCommitAndPush, doCommitAndPushWithExpectedError
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user