Skip to content

Commit 0758249

Browse files
committed
fix: complete Gitea integration for e2e tests
Fixes multiple issues preventing e2e tests from running with in-cluster Gitea instead of external GitHub: - Set MustChangePassword to false when creating users to prevent auth errors - Use AdminCreateRepo to create repos under correct user account - Rewrite function initialization to avoid git/func init conflicts - Add git user configuration for commits - Use main branch instead of master for modern git compatibility - Improve error messages by using utils.Run for all git commands - Add function code to "not yet deployed" test scenario
1 parent 958b5b0 commit 0758249

3 files changed

Lines changed: 52 additions & 21 deletions

File tree

test/e2e/func_deploy_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,25 @@ var _ = Describe("Operator", Ordered, func() {
153153
})
154154
Context("with a not yet deployed function", func() {
155155
var repoURL string
156+
var repoDir string
156157
var functionName, functionNamespace string
157158

158159
BeforeEach(func() {
159160
var err error
160161

161-
// Create repository but don't deploy
162-
username, _, _, cleanup, err := repoProvider.CreateRandomUser()
162+
// Create repository with function code but don't deploy
163+
username, password, _, cleanup, err := repoProvider.CreateRandomUser()
163164
Expect(err).NotTo(HaveOccurred())
164165
DeferCleanup(cleanup)
165166

166167
_, repoURL, cleanup, err = repoProvider.CreateRandomRepo(username, false)
167168
Expect(err).NotTo(HaveOccurred())
168169
DeferCleanup(cleanup)
170+
171+
// Initialize repository with function code
172+
repoDir, err = InitializeRepoWithFunction(repoURL, username, password, "go")
173+
Expect(err).NotTo(HaveOccurred())
174+
DeferCleanup(os.RemoveAll, repoDir)
169175
})
170176

171177
AfterEach(func() {

test/e2e/gitea_helpers.go

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,52 @@ func buildAuthURL(repoURL, username, password string) string {
3232
fmt.Sprintf("http://%s:%s@", username, password), 1)
3333
}
3434

35-
// InitializeRepoWithFunction clones an empty Gitea repo, initializes a function, and pushes it
35+
// InitializeRepoWithFunction creates a function project and pushes it to the Gitea repo
3636
func InitializeRepoWithFunction(repoURL, username, password, language string) (repoDir string, err error) {
3737
repoDir = fmt.Sprintf("%s/func-test-%s", os.TempDir(), rand.String(10))
3838

3939
// Build authenticated URL
4040
authURL := buildAuthURL(repoURL, username, password)
4141

42-
// Clone empty repo
43-
cmd := exec.Command("git", "clone", authURL, repoDir)
42+
// Initialize function (func init creates the directory)
43+
cmd := exec.Command("func", "init", "-l", language, repoDir)
4444
if _, err = utils.Run(cmd); err != nil {
45-
return "", fmt.Errorf("failed to clone repo: %w", err)
45+
return "", fmt.Errorf("failed to init function: %w", err)
4646
}
4747

48-
// Initialize function
49-
cmd = exec.Command("func", "init", "-l", language)
50-
cmd.Dir = repoDir
48+
// Initialize git repo
49+
cmd = exec.Command("git", "-C", repoDir, "init")
5150
if _, err = utils.Run(cmd); err != nil {
52-
return "", fmt.Errorf("failed to init function: %w", err)
51+
return "", fmt.Errorf("failed to git init: %w", err)
52+
}
53+
54+
// Configure git user
55+
cmd = exec.Command("git", "-C", repoDir, "config", "user.name", "Test User")
56+
if _, err = utils.Run(cmd); err != nil {
57+
return "", fmt.Errorf("failed to set git user.name: %w", err)
58+
}
59+
cmd = exec.Command("git", "-C", repoDir, "config", "user.email", "test@example.com")
60+
if _, err = utils.Run(cmd); err != nil {
61+
return "", fmt.Errorf("failed to set git user.email: %w", err)
62+
}
63+
64+
// Add remote
65+
cmd = exec.Command("git", "-C", repoDir, "remote", "add", "origin", authURL)
66+
if _, err = utils.Run(cmd); err != nil {
67+
return "", fmt.Errorf("failed to add remote: %w", err)
5368
}
5469

5570
// Commit and push
56-
if err = exec.Command("git", "-C", repoDir, "add", ".").Run(); err != nil {
71+
cmd = exec.Command("git", "-C", repoDir, "add", ".")
72+
if _, err = utils.Run(cmd); err != nil {
5773
return "", fmt.Errorf("failed to git add: %w", err)
5874
}
59-
if err = exec.Command("git", "-C", repoDir, "commit", "-m", "Initial function").Run(); err != nil {
75+
cmd = exec.Command("git", "-C", repoDir, "commit", "-m", "Initial function")
76+
if _, err = utils.Run(cmd); err != nil {
6077
return "", fmt.Errorf("failed to git commit: %w", err)
6178
}
62-
if err = exec.Command("git", "-C", repoDir, "push").Run(); err != nil {
79+
cmd = exec.Command("git", "-C", repoDir, "push", "-u", "origin", "main")
80+
if _, err = utils.Run(cmd); err != nil {
6381
return "", fmt.Errorf("failed to push initial commit: %w", err)
6482
}
6583

@@ -70,24 +88,28 @@ func InitializeRepoWithFunction(repoURL, username, password, language string) (r
7088
// Requires at least one file to be specified
7189
func CommitAndPush(repoDir string, msg string, file string, otherFiles ...string) error {
7290
// Add first file
73-
if err := exec.Command("git", "-C", repoDir, "add", file).Run(); err != nil {
91+
cmd := exec.Command("git", "-C", repoDir, "add", file)
92+
if _, err := utils.Run(cmd); err != nil {
7493
return fmt.Errorf("failed to git add %s: %w", file, err)
7594
}
7695

7796
// Add other files if provided
7897
for _, f := range otherFiles {
79-
if err := exec.Command("git", "-C", repoDir, "add", f).Run(); err != nil {
98+
cmd = exec.Command("git", "-C", repoDir, "add", f)
99+
if _, err := utils.Run(cmd); err != nil {
80100
return fmt.Errorf("failed to git add %s: %w", f, err)
81101
}
82102
}
83103

84104
// Commit
85-
if err := exec.Command("git", "-C", repoDir, "commit", "-m", msg).Run(); err != nil {
105+
cmd = exec.Command("git", "-C", repoDir, "commit", "-m", msg)
106+
if _, err := utils.Run(cmd); err != nil {
86107
return fmt.Errorf("failed to git commit: %w", err)
87108
}
88109

89110
// Push
90-
if err := exec.Command("git", "-C", repoDir, "push").Run(); err != nil {
111+
cmd = exec.Command("git", "-C", repoDir, "push")
112+
if _, err := utils.Run(cmd); err != nil {
91113
return fmt.Errorf("failed to push: %w", err)
92114
}
93115

test/utils/gitea.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,12 @@ func NewGiteaClient() (*GiteaClient, error) {
101101

102102
// CreateUser creates a new Gitea user
103103
func (g *GiteaClient) CreateUser(username, password, email string) (cleanup func(), err error) {
104+
mustChangePassword := false
104105
_, _, err = g.client.AdminCreateUser(gitea.CreateUserOption{
105-
Username: username,
106-
Password: password,
107-
Email: email,
106+
Username: username,
107+
Password: password,
108+
Email: email,
109+
MustChangePassword: &mustChangePassword,
108110
})
109111
if err != nil {
110112
return nil, fmt.Errorf("failed to create user %s: %w", username, err)
@@ -137,7 +139,8 @@ func (g *GiteaClient) CreateRandomUser() (username, password, email string, clea
137139

138140
// CreateRepo creates a new repository and returns its URL
139141
func (g *GiteaClient) CreateRepo(owner, name string, private bool) (url string, cleanup func(), err error) {
140-
_, _, err = g.client.CreateRepo(gitea.CreateRepoOption{
142+
// Use admin client to create repo for the specified owner
143+
_, _, err = g.client.AdminCreateRepo(owner, gitea.CreateRepoOption{
141144
Name: name,
142145
Private: private,
143146
})

0 commit comments

Comments
 (0)