Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: start with creating a full backend integration test framework #4117

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"go.lintTool": "golangci-lint",
"go.lintFlags": ["--fast"],
"go.buildTags": "test",
"go.buildTags": "test sqlite",
"eslint.workingDirectories": ["./web"],
"prettier.ignorePath": "./web/.prettierignore",
// Auto fix
Expand Down
4 changes: 2 additions & 2 deletions cmd/agent/core/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var (
shutdownCtx = context.Background()
)

func run(ctx context.Context, c *cli.Command, backends []types.Backend) error {
func Run(ctx context.Context, c *cli.Command, backends []types.Backend) error {
agentCtx, ctxCancel := context.WithCancelCause(ctx)
stopAgentFunc = func(err error) {
msg := "shutdown of whole agent"
Expand Down Expand Up @@ -300,7 +300,7 @@ func runWithRetry(backendEngines []types.Backend) func(ctx context.Context, c *c
retryDelay := c.Duration("connect-retry-delay")
var err error
for i := 0; i < retryCount; i++ {
if err = run(ctx, c, backendEngines); status.Code(err) == codes.Unavailable {
if err = Run(ctx, c, backendEngines); status.Code(err) == codes.Unavailable {
log.Warn().Err(err).Msg(fmt.Sprintf("cannot connect to server, retrying in %v", retryDelay))
time.Sleep(retryDelay)
} else {
Expand Down
120 changes: 120 additions & 0 deletions tests/integration/fake_forge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright 2024 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build test
// +build test

package integration

import (
"context"
"net/http"
"sync"
"testing"

"go.woodpecker-ci.org/woodpecker/v2/server/forge"
"go.woodpecker-ci.org/woodpecker/v2/server/forge/types"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
)

var (
forgeLock = sync.Mutex{}
currentForge forge.Forge = nil
)

func WithForge(t *testing.T, _forge forge.Forge, fn func()) {
forgeLock.Lock()
currentForge = _forge
defer forgeLock.Unlock()
fn()
currentForge = nil
}

type fakeForge struct{}

func (fakeForge) Name() string {
return currentForge.Name()
}

func (fakeForge) URL() string {
return currentForge.URL()
}

func (fakeForge) Login(ctx context.Context, r *types.OAuthRequest) (*model.User, string, error) {
return currentForge.Login(ctx, r)
}

func (fakeForge) Auth(ctx context.Context, token, secret string) (string, error) {
return currentForge.Auth(ctx, token, secret)
}

func (fakeForge) Teams(ctx context.Context, u *model.User) ([]*model.Team, error) {
return currentForge.Teams(ctx, u)
}

func (fakeForge) Repo(ctx context.Context, u *model.User, remoteID model.ForgeRemoteID, owner, name string) (*model.Repo, error) {
return currentForge.Repo(ctx, u, remoteID, owner, name)
}

func (fakeForge) Repos(ctx context.Context, u *model.User) ([]*model.Repo, error) {
return currentForge.Repos(ctx, u)
}

func (fakeForge) File(ctx context.Context, u *model.User, r *model.Repo, b *model.Pipeline, f string) ([]byte, error) {
return currentForge.File(ctx, u, r, b, f)
}

func (fakeForge) Dir(ctx context.Context, u *model.User, r *model.Repo, b *model.Pipeline, f string) ([]*types.FileMeta, error) {
return currentForge.Dir(ctx, u, r, b, f)
}

func (fakeForge) Status(ctx context.Context, u *model.User, r *model.Repo, b *model.Pipeline, p *model.Workflow) error {
return currentForge.Status(ctx, u, r, b, p)
}

func (fakeForge) Netrc(u *model.User, r *model.Repo) (*model.Netrc, error) {
return currentForge.Netrc(u, r)
}

func (fakeForge) Activate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
return currentForge.Activate(ctx, u, r, link)
}

func (fakeForge) Deactivate(ctx context.Context, u *model.User, r *model.Repo, link string) error {
return currentForge.Deactivate(ctx, u, r, link)
}

func (fakeForge) Branches(ctx context.Context, u *model.User, r *model.Repo, p *model.ListOptions) ([]string, error) {
return currentForge.Branches(ctx, u, r, p)
}

func (fakeForge) BranchHead(ctx context.Context, u *model.User, r *model.Repo, branch string) (*model.Commit, error) {
return currentForge.BranchHead(ctx, u, r, branch)
}

func (fakeForge) PullRequests(ctx context.Context, u *model.User, r *model.Repo, p *model.ListOptions) ([]*model.PullRequest, error) {
return currentForge.PullRequests(ctx, u, r, p)
}

func (fakeForge) Hook(ctx context.Context, r *http.Request) (repo *model.Repo, pipeline *model.Pipeline, err error) {
return currentForge.Hook(ctx, r)
}

func (fakeForge) OrgMembership(ctx context.Context, u *model.User, org string) (*model.OrgPerm, error) {
return currentForge.OrgMembership(ctx, u, org)
}

func (fakeForge) Org(ctx context.Context, u *model.User, org string) (*model.Org, error) {
return currentForge.Org(ctx, u, org)
}
75 changes: 75 additions & 0 deletions tests/integration/hello_world_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright 2024 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build test
// +build test

package integration

import (
"context"
"testing"

"github.com/stretchr/testify/mock"

forge_mocks "go.woodpecker-ci.org/woodpecker/v2/server/forge/mocks"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
)

func TestHelloWorldPipeline(t *testing.T) {
mockForge := forge_mocks.NewForge(t)
ctx := context.Background()

// Set up the mock forge expectations
repo := &model.Repo{
ID: 1,
UserID: 1,
Owner: "test-owner",
Name: "test-repo",
Config: ".woodpecker.yml",
}
user := &model.User{ID: 1, Login: "test-user"}

pipeline := &model.Pipeline{
RepoID: repo.ID,
Status: model.StatusPending,
}

config := `
pipeline:
hello:
image: alpine
commands:
- echo "Hello, World!"
`

mockForge.On("Repo", ctx, user, model.ForgeRemoteID(""), repo.Owner, repo.Name).Return(repo, nil)
mockForge.On("File", ctx, user, repo, pipeline, ".woodpecker.yml").Return([]byte(config), nil)
mockForge.On("Hook", ctx, mock.Anything).Return(repo, pipeline, nil)

// Use the fake forge with our mock
WithForge(t, mockForge, func() {
/* TODOs:
- login as user
- activate an repo
- emulate webhook of forge
- check if pipeline was created
- check of pipeline run result
- cleanup
*/
})

// Assert that all expectations were met
mockForge.AssertExpectations(t)
}
Loading