Ensure git tag tests and others create test repos in tmpdir (#18447)

* Ensure git tag tests and other create test repos in tmpdir

There are a few places where tests appear to reuse testing repos which
causes random CI failures.

This PR simply changes these tests to ensure that cloning always happens
into new temporary directories.

Fix #18444

* Change log root for integration tests to use the REPO_TEST_DIR

There is a potential race in the drone integration tests whereby test-mysql etc
will start writing to log files causing make test-check fail.

Fix #18077

Signed-off-by: Andrew Thornton <art27@cantab.net>
This commit is contained in:
zeripath 2022-01-29 12:41:44 +00:00 committed by GitHub
parent 92b715e0f2
commit f7b152f126
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 211 additions and 67 deletions

View file

@ -82,7 +82,7 @@ PROVIDER_CONFIG = integrations/gitea-integration-mssql/data/sessions
[log] [log]
MODE = test,file MODE = test,file
ROOT_PATH = mssql-log ROOT_PATH = {{REPO_TEST_DIR}}mssql-log
ROUTER = , ROUTER = ,
XORM = file XORM = file
ENABLE_SSH_LOG = true ENABLE_SSH_LOG = true

View file

@ -101,7 +101,7 @@ PROVIDER_CONFIG = integrations/gitea-integration-mysql/data/sessions
[log] [log]
MODE = test,file MODE = test,file
ROOT_PATH = mysql-log ROOT_PATH = {{REPO_TEST_DIR}}mysql-log
ROUTER = , ROUTER = ,
XORM = file XORM = file
ENABLE_SSH_LOG = true ENABLE_SSH_LOG = true

View file

@ -79,7 +79,7 @@ PROVIDER_CONFIG = integrations/gitea-integration-mysql8/data/sessions
[log] [log]
MODE = test,file MODE = test,file
ROOT_PATH = mysql8-log ROOT_PATH = {{REPO_TEST_DIR}}mysql8-log
ROUTER = , ROUTER = ,
XORM = file XORM = file
ENABLE_SSH_LOG = true ENABLE_SSH_LOG = true

View file

@ -83,7 +83,7 @@ PROVIDER_CONFIG = integrations/gitea-integration-pgsql/data/sessions
[log] [log]
MODE = test,file MODE = test,file
ROOT_PATH = pgsql-log ROOT_PATH = {{REPO_TEST_DIR}}pgsql-log
ROUTER = , ROUTER = ,
XORM = file XORM = file
ENABLE_SSH_LOG = true ENABLE_SSH_LOG = true

View file

@ -78,7 +78,7 @@ PROVIDER_CONFIG = integrations/gitea-integration-sqlite/data/sessions
[log] [log]
MODE = test,file MODE = test,file
ROOT_PATH = sqlite-log ROOT_PATH = {{REPO_TEST_DIR}}sqlite-log
ROUTER = , ROUTER = ,
XORM = file XORM = file
ENABLE_SSH_LOG = true ENABLE_SSH_LOG = true

View file

@ -17,21 +17,24 @@ import (
) )
const ( const (
testReposDir = "tests/repos/" testReposDir = "tests/repos/"
benchmarkReposDir = "benchmark/repos/"
) )
func cloneRepo(url, dir, name string) (string, error) { func cloneRepo(url, name string) (string, error) {
repoDir := filepath.Join(dir, name) repoDir, err := os.MkdirTemp("", name)
if _, err := os.Stat(repoDir); err == nil { if err != nil {
return repoDir, nil return "", err
} }
return repoDir, Clone(DefaultContext, url, repoDir, CloneRepoOptions{ if err := Clone(DefaultContext, url, repoDir, CloneRepoOptions{
Mirror: false, Mirror: false,
Bare: false, Bare: false,
Quiet: true, Quiet: true,
Timeout: 5 * time.Minute, Timeout: 5 * time.Minute,
}) }); err != nil {
_ = util.RemoveAll(repoDir)
return "", err
}
return repoDir, nil
} }
func testGetCommitsInfo(t *testing.T, repo1 *Repository) { func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
@ -61,20 +64,35 @@ func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
} }
for _, testCase := range testCases { for _, testCase := range testCases {
commit, err := repo1.GetCommit(testCase.CommitID) commit, err := repo1.GetCommit(testCase.CommitID)
assert.NoError(t, err) if err != nil {
assert.NoError(t, err, "Unable to get commit: %s from testcase due to error: %v", testCase.CommitID, err)
// no point trying to do anything else for this test.
continue
}
assert.NotNil(t, commit) assert.NotNil(t, commit)
assert.NotNil(t, commit.Tree) assert.NotNil(t, commit.Tree)
assert.NotNil(t, commit.Tree.repo) assert.NotNil(t, commit.Tree.repo)
tree, err := commit.Tree.SubTree(testCase.Path) tree, err := commit.Tree.SubTree(testCase.Path)
if err != nil {
assert.NoError(t, err, "Unable to get subtree: %s of commit: %s from testcase due to error: %v", testCase.Path, testCase.CommitID, err)
// no point trying to do anything else for this test.
continue
}
assert.NotNil(t, tree, "tree is nil for testCase CommitID %s in Path %s", testCase.CommitID, testCase.Path) assert.NotNil(t, tree, "tree is nil for testCase CommitID %s in Path %s", testCase.CommitID, testCase.Path)
assert.NotNil(t, tree.repo, "repo is nil for testCase CommitID %s in Path %s", testCase.CommitID, testCase.Path) assert.NotNil(t, tree.repo, "repo is nil for testCase CommitID %s in Path %s", testCase.CommitID, testCase.Path)
assert.NoError(t, err)
entries, err := tree.ListEntries() entries, err := tree.ListEntries()
assert.NoError(t, err) if err != nil {
commitsInfo, treeCommit, err := entries.GetCommitsInfo(context.Background(), commit, testCase.Path, nil) assert.NoError(t, err, "Unable to get entries of subtree: %s in commit: %s from testcase due to error: %v", testCase.Path, testCase.CommitID, err)
assert.NoError(t, err) // no point trying to do anything else for this test.
continue
}
// FIXME: Context.TODO() - if graceful has started we should use its Shutdown context otherwise use install signals in TestMain.
commitsInfo, treeCommit, err := entries.GetCommitsInfo(context.TODO(), commit, testCase.Path, nil)
assert.NoError(t, err, "Unable to get commit information for entries of subtree: %s in commit: %s from testcase due to error: %v", testCase.Path, testCase.CommitID, err)
if err != nil { if err != nil {
t.FailNow() t.FailNow()
} }
@ -100,40 +118,52 @@ func TestEntries_GetCommitsInfo(t *testing.T) {
testGetCommitsInfo(t, bareRepo1) testGetCommitsInfo(t, bareRepo1)
clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1_TestEntries_GetCommitsInfo") clonedPath, err := cloneRepo(bareRepo1Path, "repo1_TestEntries_GetCommitsInfo")
assert.NoError(t, err) if err != nil {
assert.NoError(t, err)
}
defer util.RemoveAll(clonedPath) defer util.RemoveAll(clonedPath)
clonedRepo1, err := OpenRepository(clonedPath) clonedRepo1, err := OpenRepository(clonedPath)
assert.NoError(t, err) if err != nil {
assert.NoError(t, err)
}
defer clonedRepo1.Close() defer clonedRepo1.Close()
testGetCommitsInfo(t, clonedRepo1) testGetCommitsInfo(t, clonedRepo1)
} }
func BenchmarkEntries_GetCommitsInfo(b *testing.B) { func BenchmarkEntries_GetCommitsInfo(b *testing.B) {
benchmarks := []struct { type benchmarkType struct {
url string url string
name string name string
}{ }
benchmarks := []benchmarkType{
{url: "https://github.com/go-gitea/gitea.git", name: "gitea"}, {url: "https://github.com/go-gitea/gitea.git", name: "gitea"},
{url: "https://github.com/ethantkoenig/manyfiles.git", name: "manyfiles"}, {url: "https://github.com/ethantkoenig/manyfiles.git", name: "manyfiles"},
{url: "https://github.com/moby/moby.git", name: "moby"}, {url: "https://github.com/moby/moby.git", name: "moby"},
{url: "https://github.com/golang/go.git", name: "go"}, {url: "https://github.com/golang/go.git", name: "go"},
{url: "https://github.com/torvalds/linux.git", name: "linux"}, {url: "https://github.com/torvalds/linux.git", name: "linux"},
} }
for _, benchmark := range benchmarks {
doBenchmark := func(benchmark benchmarkType) {
var commit *Commit var commit *Commit
var entries Entries var entries Entries
var repo *Repository var repo *Repository
if repoPath, err := cloneRepo(benchmark.url, benchmarkReposDir, benchmark.name); err != nil { repoPath, err := cloneRepo(benchmark.url, benchmark.name)
if err != nil {
b.Fatal(err) b.Fatal(err)
} else if repo, err = OpenRepository(repoPath); err != nil { }
defer util.RemoveAll(repoPath)
if repo, err = OpenRepository(repoPath); err != nil {
b.Fatal(err) b.Fatal(err)
} else if commit, err = repo.GetBranchCommit("master"); err != nil { }
repo.Close() defer repo.Close()
if commit, err = repo.GetBranchCommit("master"); err != nil {
b.Fatal(err) b.Fatal(err)
} else if entries, err = commit.Tree.ListEntries(); err != nil { } else if entries, err = commit.Tree.ListEntries(); err != nil {
repo.Close()
b.Fatal(err) b.Fatal(err)
} }
entries.Sort() entries.Sort()
@ -146,6 +176,9 @@ func BenchmarkEntries_GetCommitsInfo(b *testing.B) {
} }
} }
}) })
repo.Close() }
for _, benchmark := range benchmarks {
doBenchmark(benchmark)
} }
} }

View file

@ -17,17 +17,33 @@ import (
func TestGetFormatPatch(t *testing.T) { func TestGetFormatPatch(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1_TestGetFormatPatch") clonedPath, err := cloneRepo(bareRepo1Path, "repo1_TestGetFormatPatch")
if err != nil {
assert.NoError(t, err)
return
}
defer util.RemoveAll(clonedPath) defer util.RemoveAll(clonedPath)
assert.NoError(t, err)
repo, err := OpenRepository(clonedPath) repo, err := OpenRepository(clonedPath)
if err != nil {
assert.NoError(t, err)
return
}
defer repo.Close() defer repo.Close()
assert.NoError(t, err)
rd := &bytes.Buffer{} rd := &bytes.Buffer{}
err = repo.GetPatch("8d92fc95^", "8d92fc95", rd) err = repo.GetPatch("8d92fc95^", "8d92fc95", rd)
assert.NoError(t, err) if err != nil {
assert.NoError(t, err)
return
}
patchb, err := io.ReadAll(rd) patchb, err := io.ReadAll(rd)
assert.NoError(t, err) if err != nil {
assert.NoError(t, err)
return
}
patch := string(patchb) patch := string(patchb)
assert.Regexp(t, "^From 8d92fc95", patch) assert.Regexp(t, "^From 8d92fc95", patch)
assert.Contains(t, patch, "Subject: [PATCH] Add file2.txt") assert.Contains(t, patch, "Subject: [PATCH] Add file2.txt")
@ -37,17 +53,25 @@ func TestReadPatch(t *testing.T) {
// Ensure we can read the patch files // Ensure we can read the patch files
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
repo, err := OpenRepository(bareRepo1Path) repo, err := OpenRepository(bareRepo1Path)
if err != nil {
assert.NoError(t, err)
return
}
defer repo.Close() defer repo.Close()
assert.NoError(t, err)
// This patch doesn't exist // This patch doesn't exist
noFile, err := repo.ReadPatchCommit(0) noFile, err := repo.ReadPatchCommit(0)
assert.Error(t, err) assert.Error(t, err)
// This patch is an empty one (sometimes it's a 404) // This patch is an empty one (sometimes it's a 404)
noCommit, err := repo.ReadPatchCommit(1) noCommit, err := repo.ReadPatchCommit(1)
assert.Error(t, err) assert.Error(t, err)
// This patch is legit and should return a commit // This patch is legit and should return a commit
oldCommit, err := repo.ReadPatchCommit(2) oldCommit, err := repo.ReadPatchCommit(2)
assert.NoError(t, err) if err != nil {
assert.NoError(t, err)
return
}
assert.Empty(t, noFile) assert.Empty(t, noFile)
assert.Empty(t, noCommit) assert.Empty(t, noCommit)
@ -58,23 +82,45 @@ func TestReadPatch(t *testing.T) {
func TestReadWritePullHead(t *testing.T) { func TestReadWritePullHead(t *testing.T) {
// Ensure we can write SHA1 head corresponding to PR and open them // Ensure we can write SHA1 head corresponding to PR and open them
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
repo, err := OpenRepository(bareRepo1Path)
assert.NoError(t, err) // As we are writing we should clone the repository first
clonedPath, err := cloneRepo(bareRepo1Path, "TestReadWritePullHead")
if err != nil {
assert.NoError(t, err)
return
}
defer util.RemoveAll(clonedPath)
repo, err := OpenRepository(clonedPath)
if err != nil {
assert.NoError(t, err)
return
}
defer repo.Close() defer repo.Close()
// Try to open non-existing Pull // Try to open non-existing Pull
_, err = repo.GetRefCommitID(PullPrefix + "0/head") _, err = repo.GetRefCommitID(PullPrefix + "0/head")
assert.Error(t, err) assert.Error(t, err)
// Write a fake sha1 with only 40 zeros // Write a fake sha1 with only 40 zeros
newCommit := "feaf4ba6bc635fec442f46ddd4512416ec43c2c2" newCommit := "feaf4ba6bc635fec442f46ddd4512416ec43c2c2"
err = repo.SetReference(PullPrefix+"1/head", newCommit) err = repo.SetReference(PullPrefix+"1/head", newCommit)
assert.NoError(t, err) if err != nil {
// Remove file after the test assert.NoError(t, err)
defer func() { return
_ = repo.RemoveReference(PullPrefix + "1/head") }
}()
// Read the file created // Read the file created
headContents, err := repo.GetRefCommitID(PullPrefix + "1/head") headContents, err := repo.GetRefCommitID(PullPrefix + "1/head")
assert.NoError(t, err) if err != nil {
assert.NoError(t, err)
return
}
assert.Len(t, string(headContents), 40) assert.Len(t, string(headContents), 40)
assert.True(t, string(headContents) == newCommit) assert.True(t, string(headContents) == newCommit)
// Remove file after the test
err = repo.RemoveReference(PullPrefix + "1/head")
assert.NoError(t, err)
} }

View file

@ -16,11 +16,17 @@ import (
func TestRepository_GetTags(t *testing.T) { func TestRepository_GetTags(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
bareRepo1, err := OpenRepository(bareRepo1Path) bareRepo1, err := OpenRepository(bareRepo1Path)
assert.NoError(t, err) if err != nil {
assert.NoError(t, err)
return
}
defer bareRepo1.Close() defer bareRepo1.Close()
tags, total, err := bareRepo1.GetTagInfos(0, 0) tags, total, err := bareRepo1.GetTagInfos(0, 0)
assert.NoError(t, err) if err != nil {
assert.NoError(t, err)
return
}
assert.Len(t, tags, 1) assert.Len(t, tags, 1)
assert.Equal(t, len(tags), total) assert.Equal(t, len(tags), total)
assert.EqualValues(t, "test", tags[0].Name) assert.EqualValues(t, "test", tags[0].Name)
@ -31,40 +37,75 @@ func TestRepository_GetTags(t *testing.T) {
func TestRepository_GetTag(t *testing.T) { func TestRepository_GetTag(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1_TestRepository_GetTag") clonedPath, err := cloneRepo(bareRepo1Path, "TestRepository_GetTag")
assert.NoError(t, err) if err != nil {
assert.NoError(t, err)
return
}
defer util.RemoveAll(clonedPath) defer util.RemoveAll(clonedPath)
bareRepo1, err := OpenRepository(clonedPath) bareRepo1, err := OpenRepository(clonedPath)
assert.NoError(t, err) if err != nil {
assert.NoError(t, err)
return
}
defer bareRepo1.Close() defer bareRepo1.Close()
// LIGHTWEIGHT TAGS
lTagCommitID := "6fbd69e9823458e6c4a2fc5c0f6bc022b2f2acd1" lTagCommitID := "6fbd69e9823458e6c4a2fc5c0f6bc022b2f2acd1"
lTagName := "lightweightTag" lTagName := "lightweightTag"
bareRepo1.CreateTag(lTagName, lTagCommitID)
aTagCommitID := "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0" // Create the lightweight tag
aTagName := "annotatedTag" err = bareRepo1.CreateTag(lTagName, lTagCommitID)
aTagMessage := "my annotated message \n - test two line" if err != nil {
bareRepo1.CreateAnnotatedTag(aTagName, aTagMessage, aTagCommitID) assert.NoError(t, err, "Unable to create the lightweight tag: %s for ID: %s. Error: %v", lTagName, lTagCommitID, err)
aTagID, _ := bareRepo1.GetTagID(aTagName) return
}
// and try to get the Tag for lightweight tag
lTag, err := bareRepo1.GetTag(lTagName) lTag, err := bareRepo1.GetTag(lTagName)
assert.NoError(t, err) if err != nil {
assert.NotNil(t, lTag) assert.NoError(t, err)
return
}
if lTag == nil { if lTag == nil {
assert.NotNil(t, lTag)
assert.FailNow(t, "nil lTag: %s", lTagName) assert.FailNow(t, "nil lTag: %s", lTagName)
return
} }
assert.EqualValues(t, lTagName, lTag.Name) assert.EqualValues(t, lTagName, lTag.Name)
assert.EqualValues(t, lTagCommitID, lTag.ID.String()) assert.EqualValues(t, lTagCommitID, lTag.ID.String())
assert.EqualValues(t, lTagCommitID, lTag.Object.String()) assert.EqualValues(t, lTagCommitID, lTag.Object.String())
assert.EqualValues(t, "commit", lTag.Type) assert.EqualValues(t, "commit", lTag.Type)
// ANNOTATED TAGS
aTagCommitID := "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0"
aTagName := "annotatedTag"
aTagMessage := "my annotated message \n - test two line"
// Create the annotated tag
err = bareRepo1.CreateAnnotatedTag(aTagName, aTagMessage, aTagCommitID)
if err != nil {
assert.NoError(t, err, "Unable to create the annotated tag: %s for ID: %s. Error: %v", aTagName, aTagCommitID, err)
return
}
// Now try to get the tag for the annotated Tag
aTagID, err := bareRepo1.GetTagID(aTagName)
if err != nil {
assert.NoError(t, err)
return
}
aTag, err := bareRepo1.GetTag(aTagName) aTag, err := bareRepo1.GetTag(aTagName)
assert.NoError(t, err) if err != nil {
assert.NotNil(t, aTag) assert.NoError(t, err)
return
}
if aTag == nil { if aTag == nil {
assert.NotNil(t, aTag)
assert.FailNow(t, "nil aTag: %s", aTagName) assert.FailNow(t, "nil aTag: %s", aTagName)
return
} }
assert.EqualValues(t, aTagName, aTag.Name) assert.EqualValues(t, aTagName, aTag.Name)
assert.EqualValues(t, aTagID, aTag.ID.String()) assert.EqualValues(t, aTagID, aTag.ID.String())
@ -72,26 +113,47 @@ func TestRepository_GetTag(t *testing.T) {
assert.EqualValues(t, aTagCommitID, aTag.Object.String()) assert.EqualValues(t, aTagCommitID, aTag.Object.String())
assert.EqualValues(t, "tag", aTag.Type) assert.EqualValues(t, "tag", aTag.Type)
// RELEASE TAGS
rTagCommitID := "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0" rTagCommitID := "8006ff9adbf0cb94da7dad9e537e53817f9fa5c0"
rTagName := "release/" + lTagName rTagName := "release/" + lTagName
bareRepo1.CreateTag(rTagName, rTagCommitID)
err = bareRepo1.CreateTag(rTagName, rTagCommitID)
if err != nil {
assert.NoError(t, err, "Unable to create the tag: %s for ID: %s. Error: %v", rTagName, rTagCommitID, err)
return
}
rTagID, err := bareRepo1.GetTagID(rTagName) rTagID, err := bareRepo1.GetTagID(rTagName)
assert.NoError(t, err) if err != nil {
assert.NoError(t, err)
return
}
assert.EqualValues(t, rTagCommitID, rTagID) assert.EqualValues(t, rTagCommitID, rTagID)
oTagID, err := bareRepo1.GetTagID(lTagName) oTagID, err := bareRepo1.GetTagID(lTagName)
assert.NoError(t, err) if err != nil {
assert.NoError(t, err)
return
}
assert.EqualValues(t, lTagCommitID, oTagID) assert.EqualValues(t, lTagCommitID, oTagID)
} }
func TestRepository_GetAnnotatedTag(t *testing.T) { func TestRepository_GetAnnotatedTag(t *testing.T) {
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare") bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1_TestRepository_GetTag") clonedPath, err := cloneRepo(bareRepo1Path, "TestRepository_GetAnnotatedTag")
assert.NoError(t, err) if err != nil {
assert.NoError(t, err)
return
}
defer util.RemoveAll(clonedPath) defer util.RemoveAll(clonedPath)
bareRepo1, err := OpenRepository(clonedPath) bareRepo1, err := OpenRepository(clonedPath)
assert.NoError(t, err) if err != nil {
assert.NoError(t, err)
return
}
defer bareRepo1.Close() defer bareRepo1.Close()
lTagCommitID := "6fbd69e9823458e6c4a2fc5c0f6bc022b2f2acd1" lTagCommitID := "6fbd69e9823458e6c4a2fc5c0f6bc022b2f2acd1"
@ -106,7 +168,10 @@ func TestRepository_GetAnnotatedTag(t *testing.T) {
// Try an annotated tag // Try an annotated tag
tag, err := bareRepo1.GetAnnotatedTag(aTagID) tag, err := bareRepo1.GetAnnotatedTag(aTagID)
assert.NoError(t, err) if err != nil {
assert.NoError(t, err)
return
}
assert.NotNil(t, tag) assert.NotNil(t, tag)
assert.EqualValues(t, aTagName, tag.Name) assert.EqualValues(t, aTagName, tag.Name)
assert.EqualValues(t, aTagID, tag.ID.String()) assert.EqualValues(t, aTagID, tag.ID.String())