Skip to content

Commit f7b152f

Browse files
authored
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 <[email protected]>
1 parent 92b715e commit f7b152f

8 files changed

+211
-67
lines changed

integrations/mssql.ini.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ PROVIDER_CONFIG = integrations/gitea-integration-mssql/data/sessions
8282

8383
[log]
8484
MODE = test,file
85-
ROOT_PATH = mssql-log
85+
ROOT_PATH = {{REPO_TEST_DIR}}mssql-log
8686
ROUTER = ,
8787
XORM = file
8888
ENABLE_SSH_LOG = true

integrations/mysql.ini.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ PROVIDER_CONFIG = integrations/gitea-integration-mysql/data/sessions
101101

102102
[log]
103103
MODE = test,file
104-
ROOT_PATH = mysql-log
104+
ROOT_PATH = {{REPO_TEST_DIR}}mysql-log
105105
ROUTER = ,
106106
XORM = file
107107
ENABLE_SSH_LOG = true

integrations/mysql8.ini.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ PROVIDER_CONFIG = integrations/gitea-integration-mysql8/data/sessions
7979

8080
[log]
8181
MODE = test,file
82-
ROOT_PATH = mysql8-log
82+
ROOT_PATH = {{REPO_TEST_DIR}}mysql8-log
8383
ROUTER = ,
8484
XORM = file
8585
ENABLE_SSH_LOG = true

integrations/pgsql.ini.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ PROVIDER_CONFIG = integrations/gitea-integration-pgsql/data/sessions
8383

8484
[log]
8585
MODE = test,file
86-
ROOT_PATH = pgsql-log
86+
ROOT_PATH = {{REPO_TEST_DIR}}pgsql-log
8787
ROUTER = ,
8888
XORM = file
8989
ENABLE_SSH_LOG = true

integrations/sqlite.ini.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ PROVIDER_CONFIG = integrations/gitea-integration-sqlite/data/sessions
7878

7979
[log]
8080
MODE = test,file
81-
ROOT_PATH = sqlite-log
81+
ROOT_PATH = {{REPO_TEST_DIR}}sqlite-log
8282
ROUTER = ,
8383
XORM = file
8484
ENABLE_SSH_LOG = true

modules/git/commit_info_test.go

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,24 @@ import (
1717
)
1818

1919
const (
20-
testReposDir = "tests/repos/"
21-
benchmarkReposDir = "benchmark/repos/"
20+
testReposDir = "tests/repos/"
2221
)
2322

24-
func cloneRepo(url, dir, name string) (string, error) {
25-
repoDir := filepath.Join(dir, name)
26-
if _, err := os.Stat(repoDir); err == nil {
27-
return repoDir, nil
23+
func cloneRepo(url, name string) (string, error) {
24+
repoDir, err := os.MkdirTemp("", name)
25+
if err != nil {
26+
return "", err
2827
}
29-
return repoDir, Clone(DefaultContext, url, repoDir, CloneRepoOptions{
28+
if err := Clone(DefaultContext, url, repoDir, CloneRepoOptions{
3029
Mirror: false,
3130
Bare: false,
3231
Quiet: true,
3332
Timeout: 5 * time.Minute,
34-
})
33+
}); err != nil {
34+
_ = util.RemoveAll(repoDir)
35+
return "", err
36+
}
37+
return repoDir, nil
3538
}
3639

3740
func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
@@ -61,20 +64,35 @@ func testGetCommitsInfo(t *testing.T, repo1 *Repository) {
6164
}
6265
for _, testCase := range testCases {
6366
commit, err := repo1.GetCommit(testCase.CommitID)
64-
assert.NoError(t, err)
67+
if err != nil {
68+
assert.NoError(t, err, "Unable to get commit: %s from testcase due to error: %v", testCase.CommitID, err)
69+
// no point trying to do anything else for this test.
70+
continue
71+
}
6572
assert.NotNil(t, commit)
6673
assert.NotNil(t, commit.Tree)
6774
assert.NotNil(t, commit.Tree.repo)
6875

6976
tree, err := commit.Tree.SubTree(testCase.Path)
77+
if err != nil {
78+
assert.NoError(t, err, "Unable to get subtree: %s of commit: %s from testcase due to error: %v", testCase.Path, testCase.CommitID, err)
79+
// no point trying to do anything else for this test.
80+
continue
81+
}
82+
7083
assert.NotNil(t, tree, "tree is nil for testCase CommitID %s in Path %s", testCase.CommitID, testCase.Path)
7184
assert.NotNil(t, tree.repo, "repo is nil for testCase CommitID %s in Path %s", testCase.CommitID, testCase.Path)
7285

73-
assert.NoError(t, err)
7486
entries, err := tree.ListEntries()
75-
assert.NoError(t, err)
76-
commitsInfo, treeCommit, err := entries.GetCommitsInfo(context.Background(), commit, testCase.Path, nil)
77-
assert.NoError(t, err)
87+
if err != nil {
88+
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)
89+
// no point trying to do anything else for this test.
90+
continue
91+
}
92+
93+
// FIXME: Context.TODO() - if graceful has started we should use its Shutdown context otherwise use install signals in TestMain.
94+
commitsInfo, treeCommit, err := entries.GetCommitsInfo(context.TODO(), commit, testCase.Path, nil)
95+
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)
7896
if err != nil {
7997
t.FailNow()
8098
}
@@ -100,40 +118,52 @@ func TestEntries_GetCommitsInfo(t *testing.T) {
100118

101119
testGetCommitsInfo(t, bareRepo1)
102120

103-
clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1_TestEntries_GetCommitsInfo")
104-
assert.NoError(t, err)
121+
clonedPath, err := cloneRepo(bareRepo1Path, "repo1_TestEntries_GetCommitsInfo")
122+
if err != nil {
123+
assert.NoError(t, err)
124+
}
105125
defer util.RemoveAll(clonedPath)
106126
clonedRepo1, err := OpenRepository(clonedPath)
107-
assert.NoError(t, err)
127+
if err != nil {
128+
assert.NoError(t, err)
129+
}
108130
defer clonedRepo1.Close()
109131

110132
testGetCommitsInfo(t, clonedRepo1)
111133
}
112134

113135
func BenchmarkEntries_GetCommitsInfo(b *testing.B) {
114-
benchmarks := []struct {
136+
type benchmarkType struct {
115137
url string
116138
name string
117-
}{
139+
}
140+
141+
benchmarks := []benchmarkType{
118142
{url: "https://github.com/go-gitea/gitea.git", name: "gitea"},
119143
{url: "https://github.com/ethantkoenig/manyfiles.git", name: "manyfiles"},
120144
{url: "https://github.com/moby/moby.git", name: "moby"},
121145
{url: "https://github.com/golang/go.git", name: "go"},
122146
{url: "https://github.com/torvalds/linux.git", name: "linux"},
123147
}
124-
for _, benchmark := range benchmarks {
148+
149+
doBenchmark := func(benchmark benchmarkType) {
125150
var commit *Commit
126151
var entries Entries
127152
var repo *Repository
128-
if repoPath, err := cloneRepo(benchmark.url, benchmarkReposDir, benchmark.name); err != nil {
153+
repoPath, err := cloneRepo(benchmark.url, benchmark.name)
154+
if err != nil {
129155
b.Fatal(err)
130-
} else if repo, err = OpenRepository(repoPath); err != nil {
156+
}
157+
defer util.RemoveAll(repoPath)
158+
159+
if repo, err = OpenRepository(repoPath); err != nil {
131160
b.Fatal(err)
132-
} else if commit, err = repo.GetBranchCommit("master"); err != nil {
133-
repo.Close()
161+
}
162+
defer repo.Close()
163+
164+
if commit, err = repo.GetBranchCommit("master"); err != nil {
134165
b.Fatal(err)
135166
} else if entries, err = commit.Tree.ListEntries(); err != nil {
136-
repo.Close()
137167
b.Fatal(err)
138168
}
139169
entries.Sort()
@@ -146,6 +176,9 @@ func BenchmarkEntries_GetCommitsInfo(b *testing.B) {
146176
}
147177
}
148178
})
149-
repo.Close()
179+
}
180+
181+
for _, benchmark := range benchmarks {
182+
doBenchmark(benchmark)
150183
}
151184
}

modules/git/repo_compare_test.go

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,33 @@ import (
1717

1818
func TestGetFormatPatch(t *testing.T) {
1919
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
20-
clonedPath, err := cloneRepo(bareRepo1Path, testReposDir, "repo1_TestGetFormatPatch")
20+
clonedPath, err := cloneRepo(bareRepo1Path, "repo1_TestGetFormatPatch")
21+
if err != nil {
22+
assert.NoError(t, err)
23+
return
24+
}
2125
defer util.RemoveAll(clonedPath)
22-
assert.NoError(t, err)
26+
2327
repo, err := OpenRepository(clonedPath)
28+
if err != nil {
29+
assert.NoError(t, err)
30+
return
31+
}
2432
defer repo.Close()
25-
assert.NoError(t, err)
33+
2634
rd := &bytes.Buffer{}
2735
err = repo.GetPatch("8d92fc95^", "8d92fc95", rd)
28-
assert.NoError(t, err)
36+
if err != nil {
37+
assert.NoError(t, err)
38+
return
39+
}
40+
2941
patchb, err := io.ReadAll(rd)
30-
assert.NoError(t, err)
42+
if err != nil {
43+
assert.NoError(t, err)
44+
return
45+
}
46+
3147
patch := string(patchb)
3248
assert.Regexp(t, "^From 8d92fc95", patch)
3349
assert.Contains(t, patch, "Subject: [PATCH] Add file2.txt")
@@ -37,17 +53,25 @@ func TestReadPatch(t *testing.T) {
3753
// Ensure we can read the patch files
3854
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
3955
repo, err := OpenRepository(bareRepo1Path)
56+
if err != nil {
57+
assert.NoError(t, err)
58+
return
59+
}
4060
defer repo.Close()
41-
assert.NoError(t, err)
4261
// This patch doesn't exist
4362
noFile, err := repo.ReadPatchCommit(0)
4463
assert.Error(t, err)
64+
4565
// This patch is an empty one (sometimes it's a 404)
4666
noCommit, err := repo.ReadPatchCommit(1)
4767
assert.Error(t, err)
68+
4869
// This patch is legit and should return a commit
4970
oldCommit, err := repo.ReadPatchCommit(2)
50-
assert.NoError(t, err)
71+
if err != nil {
72+
assert.NoError(t, err)
73+
return
74+
}
5175

5276
assert.Empty(t, noFile)
5377
assert.Empty(t, noCommit)
@@ -58,23 +82,45 @@ func TestReadPatch(t *testing.T) {
5882
func TestReadWritePullHead(t *testing.T) {
5983
// Ensure we can write SHA1 head corresponding to PR and open them
6084
bareRepo1Path := filepath.Join(testReposDir, "repo1_bare")
61-
repo, err := OpenRepository(bareRepo1Path)
62-
assert.NoError(t, err)
85+
86+
// As we are writing we should clone the repository first
87+
clonedPath, err := cloneRepo(bareRepo1Path, "TestReadWritePullHead")
88+
if err != nil {
89+
assert.NoError(t, err)
90+
return
91+
}
92+
defer util.RemoveAll(clonedPath)
93+
94+
repo, err := OpenRepository(clonedPath)
95+
if err != nil {
96+
assert.NoError(t, err)
97+
return
98+
}
6399
defer repo.Close()
100+
64101
// Try to open non-existing Pull
65102
_, err = repo.GetRefCommitID(PullPrefix + "0/head")
66103
assert.Error(t, err)
104+
67105
// Write a fake sha1 with only 40 zeros
68106
newCommit := "feaf4ba6bc635fec442f46ddd4512416ec43c2c2"
69107
err = repo.SetReference(PullPrefix+"1/head", newCommit)
70-
assert.NoError(t, err)
71-
// Remove file after the test
72-
defer func() {
73-
_ = repo.RemoveReference(PullPrefix + "1/head")
74-
}()
108+
if err != nil {
109+
assert.NoError(t, err)
110+
return
111+
}
112+
75113
// Read the file created
76114
headContents, err := repo.GetRefCommitID(PullPrefix + "1/head")
77-
assert.NoError(t, err)
115+
if err != nil {
116+
assert.NoError(t, err)
117+
return
118+
}
119+
78120
assert.Len(t, string(headContents), 40)
79121
assert.True(t, string(headContents) == newCommit)
122+
123+
// Remove file after the test
124+
err = repo.RemoveReference(PullPrefix + "1/head")
125+
assert.NoError(t, err)
80126
}

0 commit comments

Comments
 (0)