mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-10-30 03:46:33 +08:00
Without a docker daemon six `act/runner` tests failed instead of passing. A dry run still created and removed job networks, the job teardown dropped the dry-run flag along with every other context value, and the container mocks asked the daemon for the runner arch. This also speeds up the tests and CI without dropping assertions. Most of the time went to git and docker round trips, which are now pinned, batched or coalesced, and the dind and e2e steps cache or overlap more of their setup. On CI, `act/runner` went from 77s to 55s and `test-dind` from 108s to 55s. Locally, `act/runner` without docker went from 53s to 7s. Split out of https://gitea.com/gitea/runner/pulls/1348, which adds Windows CI on top. The go cache changes moved to https://gitea.com/gitea/runner/pulls/1350. Written by Claude Code. --------- Co-authored-by: bircni <bircni@icloud.com> Reviewed-on: https://gitea.com/gitea/runner/pulls/1349 Reviewed-by: bircni <bircni@icloud.com> Co-authored-by: silverwind <me@silverwind.io>
253 lines
6.0 KiB
Go
253 lines
6.0 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// Copyright 2022 The nektos/act Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package ghcontext
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
"testing"
|
|
|
|
"gitea.dev/actionslib/pkg/model"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestSetRef(t *testing.T) {
|
|
oldFindGitRef := findGitRef
|
|
defer func() { findGitRef = oldFindGitRef }()
|
|
|
|
findGitRef = func(_ctx context.Context, _file string) (string, error) {
|
|
return "refs/heads/master", nil
|
|
}
|
|
|
|
tables := []struct {
|
|
eventName string
|
|
event map[string]any
|
|
ref string
|
|
refName string
|
|
}{
|
|
{
|
|
eventName: "pull_request_target",
|
|
event: map[string]any{},
|
|
ref: "refs/heads/master",
|
|
refName: "master",
|
|
},
|
|
{
|
|
eventName: "pull_request",
|
|
event: map[string]any{
|
|
"number": 1234.,
|
|
},
|
|
ref: "refs/pull/1234/merge",
|
|
refName: "1234/merge",
|
|
},
|
|
{
|
|
eventName: "pull_request_review",
|
|
event: map[string]any{"pull_request": map[string]any{"number": 1234.}},
|
|
ref: "refs/pull/1234/merge",
|
|
refName: "1234/merge",
|
|
},
|
|
{
|
|
eventName: "pull_request",
|
|
event: map[string]any{"number": 1234., "action": "closed", "pull_request": map[string]any{"merged": true}},
|
|
ref: "refs/heads/master",
|
|
refName: "master",
|
|
},
|
|
{
|
|
eventName: "pull_request",
|
|
event: map[string]any{"number": 1234., "action": "closed", "pull_request": map[string]any{"merged": false}},
|
|
ref: "refs/pull/1234/merge",
|
|
refName: "1234/merge",
|
|
},
|
|
{
|
|
eventName: "pull_request_review_comment",
|
|
event: map[string]any{"pull_request": map[string]any{"number": 1234.}},
|
|
ref: "refs/pull/1234/merge",
|
|
refName: "1234/merge",
|
|
},
|
|
{
|
|
eventName: "deployment",
|
|
event: map[string]any{
|
|
"deployment": map[string]any{
|
|
"ref": "refs/heads/somebranch",
|
|
},
|
|
},
|
|
ref: "refs/heads/somebranch",
|
|
refName: "somebranch",
|
|
},
|
|
{
|
|
eventName: "release",
|
|
event: map[string]any{
|
|
"release": map[string]any{
|
|
"tag_name": "v1.0.0",
|
|
},
|
|
},
|
|
ref: "refs/tags/v1.0.0",
|
|
refName: "v1.0.0",
|
|
},
|
|
{
|
|
eventName: "push",
|
|
event: map[string]any{
|
|
"ref": "refs/heads/somebranch",
|
|
},
|
|
ref: "refs/heads/somebranch",
|
|
refName: "somebranch",
|
|
},
|
|
{
|
|
eventName: "create",
|
|
event: map[string]any{"ref": "feature/new", "ref_type": "branch"},
|
|
ref: "refs/heads/feature/new",
|
|
refName: "feature/new",
|
|
},
|
|
{
|
|
eventName: "create",
|
|
event: map[string]any{"ref": "v1", "ref_type": "tag"},
|
|
ref: "refs/tags/v1",
|
|
refName: "v1",
|
|
},
|
|
{
|
|
eventName: "create",
|
|
event: map[string]any{"ref": "refs/tags/v1", "ref_type": "tag"},
|
|
ref: "refs/tags/v1",
|
|
refName: "v1",
|
|
},
|
|
{
|
|
eventName: "unknown",
|
|
event: map[string]any{
|
|
"repository": map[string]any{
|
|
"default_branch": "main",
|
|
},
|
|
},
|
|
ref: "refs/heads/main",
|
|
refName: "main",
|
|
},
|
|
{
|
|
eventName: "no-event",
|
|
event: map[string]any{},
|
|
ref: "refs/heads/master",
|
|
refName: "master",
|
|
},
|
|
}
|
|
|
|
for _, table := range tables {
|
|
t.Run(table.eventName, func(t *testing.T) {
|
|
ghc := &model.GithubContext{
|
|
EventName: table.eventName,
|
|
BaseRef: "master",
|
|
Event: table.event,
|
|
}
|
|
|
|
SetRef(t.Context(), ghc, "/some/dir")
|
|
ghc.SetRefTypeAndName()
|
|
|
|
assert.Equal(t, table.ref, ghc.Ref)
|
|
assert.Equal(t, table.refName, ghc.RefName)
|
|
})
|
|
}
|
|
|
|
t.Run("no-default-branch", func(t *testing.T) {
|
|
findGitRef = func(_ctx context.Context, _file string) (string, error) {
|
|
return "", errors.New("no default branch")
|
|
}
|
|
|
|
ghc := &model.GithubContext{
|
|
EventName: "no-default-branch",
|
|
Event: map[string]any{},
|
|
}
|
|
|
|
SetRef(t.Context(), ghc, "/some/dir")
|
|
|
|
assert.Equal(t, "refs/heads/master", ghc.Ref)
|
|
})
|
|
}
|
|
|
|
func TestSetSha(t *testing.T) {
|
|
oldFindGitRef := findGitRef
|
|
oldFindGitRevision := findGitRevision
|
|
oldFindGithubRepo := findGithubRepo
|
|
defer func() { findGitRef = oldFindGitRef }()
|
|
defer func() { findGitRevision = oldFindGitRevision }()
|
|
defer func() { findGithubRepo = oldFindGithubRepo }()
|
|
|
|
findGitRevision = func(_ctx context.Context, _file string) (string, error) {
|
|
return "1234fakesha", nil
|
|
}
|
|
|
|
tables := []struct {
|
|
eventName string
|
|
event map[string]any
|
|
sha string
|
|
}{
|
|
{
|
|
eventName: "pull_request_target",
|
|
event: map[string]any{
|
|
"pull_request": map[string]any{
|
|
"base": map[string]any{
|
|
"sha": "pr-base-sha",
|
|
},
|
|
},
|
|
},
|
|
sha: "pr-base-sha",
|
|
},
|
|
{
|
|
eventName: "pull_request",
|
|
event: map[string]any{
|
|
"number": 1234.,
|
|
},
|
|
sha: "1234fakesha",
|
|
},
|
|
{
|
|
eventName: "deployment",
|
|
event: map[string]any{
|
|
"deployment": map[string]any{
|
|
"sha": "deployment-sha",
|
|
},
|
|
},
|
|
sha: "deployment-sha",
|
|
},
|
|
{
|
|
eventName: "push",
|
|
event: map[string]any{
|
|
"after": "push-sha",
|
|
"deleted": false,
|
|
},
|
|
sha: "push-sha",
|
|
},
|
|
{
|
|
eventName: "unknown",
|
|
event: map[string]any{},
|
|
sha: "1234fakesha",
|
|
},
|
|
}
|
|
|
|
for _, table := range tables {
|
|
t.Run(table.eventName, func(t *testing.T) {
|
|
ghc := &model.GithubContext{
|
|
EventName: table.eventName,
|
|
Event: table.event,
|
|
}
|
|
|
|
SetSha(t.Context(), ghc, "/some/dir")
|
|
|
|
assert.Equal(t, table.sha, ghc.Sha)
|
|
})
|
|
}
|
|
|
|
t.Run("pinned checkout keeps the first sha, ref and repository", func(t *testing.T) {
|
|
ctx := WithPinnedCheckout(t.Context(), &sync.Map{})
|
|
for _, head := range []string{"first", "moved"} {
|
|
findGitRevision = func(context.Context, string) (string, error) { return head, nil }
|
|
findGitRef = func(context.Context, string) (string, error) { return "refs/heads/" + head, nil }
|
|
findGithubRepo = func(context.Context, string) (string, error) { return "owner/" + head, nil }
|
|
ghc := &model.GithubContext{Event: map[string]any{}}
|
|
SetSha(ctx, ghc, "/some/dir")
|
|
SetRef(ctx, ghc, "/some/dir")
|
|
SetRepositoryAndOwner(ctx, ghc, "/some/dir")
|
|
assert.Equal(t, "first", ghc.Sha)
|
|
assert.Equal(t, "refs/heads/first", ghc.Ref)
|
|
assert.Equal(t, "owner/first", ghc.Repository)
|
|
}
|
|
})
|
|
}
|