mirror of
https://gitea.com/gitea/runner.git
synced 2026-11-16 04:43:42 +00:00
refactor: remove dead code and consolidate tests (#1335)
Removes dead code and redundant logic and consolidates tests, so there is less to read and maintain. No behavior change. 1. **act/runner**: inline single-use wrappers, compile regexes once instead of per call, embed the trampoline as a string, drop the unused legacy result of `parseActionCommand` 2. **act/container**: `sanitizeConfig` and `mergeContainerConfigs` stop returning the configs they already mutate in place, drop never-set option fields and the unused `user` parameter of `ExecWithCmdLine` 3. **act/common and cache server**: drop the unused `Trace`/`Tracef` loggers and the short SHA of `FindGitRevision`, `RegisterJob` releases through `RevokeJob` 4. **daemon, poller and reporter**: one shutdown wait for both poll modes, drop an unreachable fetch timeout branch, guard the reporter's last report time with its existing lock instead of an atomic 5. **build**: drop unused Makefile and release workflow variables, collapse the goreleaser ignore list to the same build targets 6. **tests**: fold near-duplicate tests into tables with shared setup Throughout, hand-rolled logic gives way to `cmp.Or`, `strings.Cut`, `slices` and `min`/`max`. Reviewed-on: https://gitea.com/gitea/runner/pulls/1335 Reviewed-by: bircni <[email protected]> Co-authored-by: silverwind <[email protected]>
This commit is contained in:
1 parent
923a0bfb59
commit
d882311ec6
107 files changed
+2408
-6500
No files matched your search
@@ -33,7 +33,7 @@ func (r *Runner) checkConfiguredHealth(ctx context.Context) (bool, string) {
|
||||
}
|
||||
|
||||
env := processEnvironment()
|
||||
maps.Copy(env, r.cloneEnvs())
|
||||
maps.Copy(env, r.envs)
|
||||
env["GITEA_RUNNER_HEALTH_CHECK"] = "true"
|
||||
env["GITEA_RUNNER_NAME"] = r.name
|
||||
if r.client != nil {
|
||||
@@ -84,9 +84,6 @@ func executeHealthCheck(ctx context.Context, script string, timeout time.Duratio
|
||||
if errors.Is(checkCtx.Err(), context.DeadlineExceeded) {
|
||||
return fmt.Errorf("timed out after %s", timeout)
|
||||
}
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if exitErr, ok := errors.AsType[*exec.ExitError](err); ok {
|
||||
return fmt.Errorf("exited with code %d", exitErr.ExitCode())
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ package run
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
@@ -69,15 +68,9 @@ func (r *Runner) runPostTaskScript(ctx context.Context, reporter *report.Reporte
|
||||
// copiers have finished (cmd.Wait, bounded by WaitDelay above, guarantees it).
|
||||
common.FlushWriter(stdout)
|
||||
common.FlushWriter(stderr)
|
||||
if err != nil {
|
||||
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
||||
logging.Warnf("post-task script %q for task %d: %v", script, task.Id, err)
|
||||
return
|
||||
}
|
||||
if exitErr, ok := errors.AsType[*exec.ExitError](err); ok {
|
||||
logging.Warnf("post-task script %q for task %d exited with code %d", script, task.Id, exitErr.ExitCode())
|
||||
return
|
||||
}
|
||||
if exitErr, ok := errors.AsType[*exec.ExitError](err); ok {
|
||||
logging.Warnf("post-task script %q for task %d exited with code %d", script, task.Id, exitErr.ExitCode())
|
||||
} else if err != nil {
|
||||
logging.Warnf("post-task script %q for task %d: %v", script, task.Id, err)
|
||||
}
|
||||
}
|
||||
@@ -111,8 +104,8 @@ func (r *Runner) postTaskScriptEnv(reporter *report.Reporter, task *runnerv1.Tas
|
||||
|
||||
func envListFromMap(env map[string]string) []string {
|
||||
envList := make([]string, 0, len(env))
|
||||
for k, v := range env {
|
||||
envList = append(envList, fmt.Sprintf("%s=%s", k, v))
|
||||
for key, value := range env {
|
||||
envList = append(envList, key+"="+value)
|
||||
}
|
||||
return envList
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ func proxyPasswords() []string {
|
||||
var passwords []string
|
||||
for _, raw := range []string{cfg.HTTPProxy, cfg.HTTPSProxy} {
|
||||
parsed, err := url.Parse(raw)
|
||||
if err != nil || parsed.User == nil {
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if password, ok := parsed.User.Password(); ok && password != "" && !slices.Contains(passwords, password) {
|
||||
@@ -152,11 +152,8 @@ func appendNoProxy(noProxy string, hosts ...string) string {
|
||||
// hostOf returns the host of a URL without its port, the form a no_proxy entry takes. It is
|
||||
// empty for anything without a network host, such as a unix socket.
|
||||
func hostOf(raw string) string {
|
||||
if raw == "" {
|
||||
return ""
|
||||
}
|
||||
parsed, err := url.Parse(strings.TrimSuffix(raw, "/"))
|
||||
if err != nil || parsed.Hostname() == "" {
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return parsed.Hostname()
|
||||
|
||||
+10
-33
@@ -99,7 +99,7 @@ func NewRunner(cfg *config.Config, reg *config.Registration, cli client.Client)
|
||||
envs := make(map[string]string, len(cfg.Runner.Envs))
|
||||
maps.Copy(envs, cfg.Runner.Envs)
|
||||
var cacheHandler *artifactcache.Handler
|
||||
if cacheEnabled(cfg) {
|
||||
if cfg.Cache.Enabled == nil || *cfg.Cache.Enabled {
|
||||
warnIgnoredCachePolicy(cfg)
|
||||
warnIgnoredCacheSecret(cfg)
|
||||
handler, err := artifactcache.StartHandler(artifactcache.Options{
|
||||
@@ -121,8 +121,7 @@ func NewRunner(cfg *config.Config, reg *config.Registration, cli client.Client)
|
||||
}
|
||||
|
||||
// set artifact gitea api
|
||||
artifactGiteaAPI := strings.TrimSuffix(cli.Address(), "/") + "/api/actions_pipeline/"
|
||||
envs["ACTIONS_RUNTIME_URL"] = artifactGiteaAPI
|
||||
envs["ACTIONS_RUNTIME_URL"] = strings.TrimSuffix(cli.Address(), "/") + "/api/actions_pipeline/"
|
||||
envs["ACTIONS_RESULTS_URL"] = strings.TrimSuffix(cli.Address(), "/")
|
||||
|
||||
// Set specific environments to distinguish between Gitea and GitHub
|
||||
@@ -169,9 +168,7 @@ func (r *Runner) OnIdle(ctx context.Context) {
|
||||
}
|
||||
// Bind-workdir mode: reclaim stale per-task workspace dirs (numeric task IDs).
|
||||
if r.cfg.Container.BindWorkdir {
|
||||
workdirParent := strings.TrimLeft(r.cfg.Container.WorkdirParent, "/")
|
||||
workdirRoot := filepath.FromSlash("/" + workdirParent)
|
||||
r.cleanupStaleDirs(ctx, workdirRoot, isTaskIDDir)
|
||||
r.cleanupStaleDirs(ctx, filepath.FromSlash("/"+strings.TrimLeft(r.cfg.Container.WorkdirParent, "/")), isTaskIDDir)
|
||||
}
|
||||
// Host mode: reclaim per-job scratch dirs left behind when HostEnvironment
|
||||
// cleanup timed out (e.g. a delete stalled by an AV/EDR filter driver). They
|
||||
@@ -270,10 +267,7 @@ func (r *Runner) cleanupStaleDirs(ctx context.Context, root string, match func(n
|
||||
if err := ctx.Err(); err != nil {
|
||||
return
|
||||
}
|
||||
if !entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
if !match(entry.Name()) {
|
||||
if !entry.IsDir() || !match(entry.Name()) {
|
||||
continue
|
||||
}
|
||||
info, err := entry.Info()
|
||||
@@ -452,7 +446,9 @@ func (r *Runner) run(ctx context.Context, task *runnerv1.Task, reporter *report.
|
||||
}()
|
||||
|
||||
workflowCallInputs := restoreCalledWorkflowContext(task)
|
||||
r.reportSetup(reporter, task)
|
||||
for _, line := range r.setupLines(task) {
|
||||
reporter.Logf("%s", line)
|
||||
}
|
||||
|
||||
workflow, jobID, err := generateWorkflow(task)
|
||||
if err != nil {
|
||||
@@ -511,11 +507,7 @@ func (r *Runner) run(ctx context.Context, task *runnerv1.Task, reporter *report.
|
||||
telemetry.AddJobEnv(ctx, envs)
|
||||
|
||||
preset := model.GithubContextFromMap(task.Context.AsMap())
|
||||
if t := task.Secrets["GITEA_TOKEN"]; t != "" {
|
||||
preset.Token = t
|
||||
} else if t := task.Secrets["GITHUB_TOKEN"]; t != "" {
|
||||
preset.Token = t
|
||||
}
|
||||
preset.Token = cmp.Or(task.Secrets["GITEA_TOKEN"], task.Secrets["GITHUB_TOKEN"], preset.Token)
|
||||
|
||||
if actionsIDTokenRequestURL := taskContext["actions_id_token_request_url"].GetStringValue(); actionsIDTokenRequestURL != "" {
|
||||
envs["ACTIONS_ID_TOKEN_REQUEST_URL"] = actionsIDTokenRequestURL
|
||||
@@ -589,7 +581,6 @@ func (r *Runner) run(ctx context.Context, task *runnerv1.Task, reporter *report.
|
||||
|
||||
ForcePull: r.cfg.Container.ForcePull,
|
||||
ForceRebuild: r.cfg.Container.ForceRebuild,
|
||||
JSONLogger: false,
|
||||
Env: envs,
|
||||
ProxyEnv: proxyEnv,
|
||||
Secrets: task.Secrets,
|
||||
@@ -672,18 +663,8 @@ func (r *Runner) builtInCacheURL() string {
|
||||
return r.envs["ACTIONS_CACHE_URL"]
|
||||
}
|
||||
|
||||
func cacheEnabled(cfg *config.Config) bool {
|
||||
return cfg.Cache.Enabled == nil || *cfg.Cache.Enabled
|
||||
}
|
||||
|
||||
// cacheServiceV2 reports whether jobs are told the cache service speaks v2. It is all cache.v2
|
||||
// turns off: the bundle edit that reaches it is what the artifact actions need too.
|
||||
func (r *Runner) cacheServiceV2() bool {
|
||||
return r.cfg.Cache.V2 == nil || *r.cfg.Cache.V2
|
||||
}
|
||||
|
||||
func (r *Runner) setResultsService(envs map[string]string, resultsURL string) {
|
||||
v2 := resultsURL != "" && r.cacheServiceV2()
|
||||
v2 := resultsURL != "" && (r.cfg.Cache.V2 == nil || *r.cfg.Cache.V2) // all cache.v2 turns off, the bundle edit reaching it is what the artifact actions need too
|
||||
if v2 {
|
||||
envs[runner.CacheServiceV2Env] = "true"
|
||||
} else {
|
||||
@@ -902,11 +883,7 @@ func warnIgnoredCachePolicy(cfg *config.Config) {
|
||||
|
||||
// warnIgnoredCacheSecret flags an external cache server secret configured on a runner that uses the built-in cache server.
|
||||
func warnIgnoredCacheSecret(cfg *config.Config) {
|
||||
if cfg.Cache.ExternalServer != "" {
|
||||
return
|
||||
}
|
||||
// Not using an external cache server, so any configured secret is ignored.
|
||||
if cfg.Cache.ExternalSecret == "" {
|
||||
if cfg.Cache.ExternalServer != "" || cfg.Cache.ExternalSecret == "" {
|
||||
return
|
||||
}
|
||||
// LoadDefault resolves external_secret_file into ExternalSecret, so report whichever key the operator actually wrote.
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
package run
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json/v2"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -22,53 +21,34 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func emptyCfg() *config.Config { return &config.Config{} }
|
||||
|
||||
func TestRunner_registerCacheForTask_NoOps(t *testing.T) {
|
||||
for token, handler := range map[string]*artifactcache.Handler{"tok": nil, "": {}} {
|
||||
r := &Runner{cfg: emptyCfg(), cacheHandler: handler}
|
||||
r := &Runner{cacheHandler: handler}
|
||||
unregister, _ := r.registerCacheForTask(token, "owner/repo", "", nil)
|
||||
require.NotNil(t, unregister, token)
|
||||
unregister()
|
||||
}
|
||||
}
|
||||
|
||||
// Locks in @actions/cache's wire protocol: bearer on reserve/upload/commit
|
||||
// /find, no auth on the signed archiveLocation download.
|
||||
func TestRunner_CacheFullFlow_MatchesToolkit(t *testing.T) {
|
||||
dir := filepath.Join(t.TempDir(), "artifactcache")
|
||||
handler, err := artifactcache.StartHandler(artifactcache.Options{Dir: dir, OutboundIP: "127.0.0.1"})
|
||||
require.NoError(t, err)
|
||||
defer handler.Close()
|
||||
|
||||
r := &Runner{cfg: emptyCfg(), cacheHandler: handler, envs: map[string]string{"ACTIONS_RESULTS_URL": "https://gitea.example"}}
|
||||
r := &Runner{cfg: &config.Config{}, cacheHandler: handler, envs: map[string]string{"ACTIONS_RESULTS_URL": "https://gitea.example"}}
|
||||
const publicURL = "http://a1b2c3d4e5f6:8088"
|
||||
token := "full-flow-token"
|
||||
unregister, resultsURL := r.registerCacheForTask(token, "owner/repo", publicURL, nil)
|
||||
assert.Equal(t, publicURL, resultsURL)
|
||||
|
||||
base := handler.ExternalURL() + "/_apis/artifactcache"
|
||||
do := func(method, url, contentType, contentRange, body string) *http.Response {
|
||||
req, err := http.NewRequest(method, url, strings.NewReader(body))
|
||||
require.NoError(t, err)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
if contentType != "" {
|
||||
req.Header.Set("Content-Type", contentType)
|
||||
}
|
||||
if contentRange != "" {
|
||||
req.Header.Set("Content-Range", contentRange)
|
||||
}
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
return resp
|
||||
}
|
||||
|
||||
key := "toolkit-flow"
|
||||
version := "c19da02a2bd7e77277f1ac29ab45c09b7d46a4ee758284e26bb3045ad11d9d20"
|
||||
body := `hello-cache-body`
|
||||
|
||||
// reserve
|
||||
resp := do(http.MethodPost, base+"/caches", "application/json", "",
|
||||
resp := cacheRequest(t, token, http.MethodPost, base+"/caches", "application/json", "",
|
||||
fmt.Sprintf(`{"key":"%s","version":"%s","cacheSize":%d}`, key, version, len(body)))
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
var reserved struct {
|
||||
@@ -77,19 +57,16 @@ func TestRunner_CacheFullFlow_MatchesToolkit(t *testing.T) {
|
||||
require.NoError(t, decodeJSON(resp, &reserved))
|
||||
require.NotZero(t, reserved.CacheID)
|
||||
|
||||
// upload
|
||||
resp = do(http.MethodPatch, fmt.Sprintf("%s/caches/%d", base, reserved.CacheID),
|
||||
resp = cacheRequest(t, token, http.MethodPatch, fmt.Sprintf("%s/caches/%d", base, reserved.CacheID),
|
||||
"application/octet-stream", fmt.Sprintf("bytes 0-%d/*", len(body)-1), body)
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
resp.Body.Close()
|
||||
|
||||
// commit
|
||||
resp = do(http.MethodPost, fmt.Sprintf("%s/caches/%d", base, reserved.CacheID), "", "", "")
|
||||
resp = cacheRequest(t, token, http.MethodPost, fmt.Sprintf("%s/caches/%d", base, reserved.CacheID), "", "", "")
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
resp.Body.Close()
|
||||
|
||||
// find — @actions/cache always sends comma-separated keys here
|
||||
resp = do(http.MethodGet,
|
||||
resp = cacheRequest(t, token, http.MethodGet,
|
||||
fmt.Sprintf("%s/cache?keys=%s,fallback&version=%s", base, key, version), "", "", "")
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
var hit struct {
|
||||
@@ -100,8 +77,6 @@ func TestRunner_CacheFullFlow_MatchesToolkit(t *testing.T) {
|
||||
require.Equal(t, key, hit.CacheKey)
|
||||
require.True(t, strings.HasPrefix(hit.ArchiveLocation, publicURL+"/"), hit.ArchiveLocation)
|
||||
|
||||
// download — toolkit does NOT attach Authorization here; the signature
|
||||
// in the URL must be enough.
|
||||
dl, err := http.Get(strings.Replace(hit.ArchiveLocation, publicURL, handler.ExternalURL(), 1))
|
||||
require.NoError(t, err)
|
||||
defer dl.Body.Close()
|
||||
@@ -111,18 +86,32 @@ func TestRunner_CacheFullFlow_MatchesToolkit(t *testing.T) {
|
||||
assert.Equal(t, body, string(got[:n]))
|
||||
|
||||
unregister()
|
||||
resp = do(http.MethodGet, fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version), "", "", "")
|
||||
resp = cacheRequest(t, token, http.MethodGet, fmt.Sprintf("%s/cache?keys=%s&version=%s", base, key, version), "", "", "")
|
||||
resp.Body.Close()
|
||||
assert.Equal(t, http.StatusUnauthorized, resp.StatusCode)
|
||||
}
|
||||
|
||||
func cacheRequest(t *testing.T, token, method, url, contentType, contentRange, body string) *http.Response {
|
||||
t.Helper()
|
||||
req, err := http.NewRequest(method, url, strings.NewReader(body))
|
||||
require.NoError(t, err)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
if contentType != "" {
|
||||
req.Header.Set("Content-Type", contentType)
|
||||
}
|
||||
if contentRange != "" {
|
||||
req.Header.Set("Content-Range", contentRange)
|
||||
}
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
return resp
|
||||
}
|
||||
|
||||
func decodeJSON(resp *http.Response, v any) error {
|
||||
defer resp.Body.Close()
|
||||
return json.UnmarshalRead(resp.Body, v)
|
||||
}
|
||||
|
||||
// End-to-end through a runner to a remote cache-server: token unknown → 401, register →
|
||||
// reserve/upload/commit/find/download all OK, revoke → 401 again.
|
||||
func TestRunner_ExternalCacheServer_RegisterRevoke(t *testing.T) {
|
||||
const secret = "shared-secret-for-tests"
|
||||
remote, err := artifactcache.StartHandler(artifactcache.Options{Dir: t.TempDir(), OutboundIP: "127.0.0.1", InternalSecret: secret})
|
||||
@@ -154,10 +143,7 @@ func TestRunner_ExternalCacheServer_RegisterRevoke(t *testing.T) {
|
||||
repo := "owner/repoX"
|
||||
base := r.envs["ACTIONS_CACHE_URL"] + "_apis/artifactcache"
|
||||
probe := func() int {
|
||||
req, _ := http.NewRequest(http.MethodGet, base+"/cache?keys=k&version=v", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
resp := cacheRequest(t, token, http.MethodGet, base+"/cache?keys=k&version=v", "", "", "")
|
||||
resp.Body.Close()
|
||||
return resp.StatusCode
|
||||
}
|
||||
@@ -181,15 +167,9 @@ func TestRunner_ExternalCacheServer_RegisterRevoke(t *testing.T) {
|
||||
require.NoError(t, decodeJSON(forwarded, &artifactResult))
|
||||
assert.True(t, artifactResult["ok"])
|
||||
|
||||
// Full reserve→upload→commit→find→download cycle, identical to what
|
||||
// @actions/cache does, through the runner to the remote server.
|
||||
body := []byte("payload-from-task")
|
||||
reserveBody, _ := json.Marshal(&artifactcache.Request{Key: "ext-key", Version: "v", Size: int64(len(body))})
|
||||
req, _ := http.NewRequest(http.MethodPost, base+"/caches", bytes.NewReader(reserveBody))
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
resp := cacheRequest(t, token, http.MethodPost, base+"/caches", "application/json", "", string(reserveBody))
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
var reserved struct {
|
||||
CacheID uint64 `json:"cacheId"`
|
||||
@@ -197,25 +177,16 @@ func TestRunner_ExternalCacheServer_RegisterRevoke(t *testing.T) {
|
||||
require.NoError(t, decodeJSON(resp, &reserved))
|
||||
require.NotZero(t, reserved.CacheID)
|
||||
|
||||
req, _ = http.NewRequest(http.MethodPatch, fmt.Sprintf("%s/caches/%d", base, reserved.CacheID), bytes.NewReader(body))
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
req.Header.Set("Content-Range", fmt.Sprintf("bytes 0-%d/*", len(body)-1))
|
||||
resp, err = http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
resp = cacheRequest(t, token, http.MethodPatch, fmt.Sprintf("%s/caches/%d", base, reserved.CacheID),
|
||||
"", fmt.Sprintf("bytes 0-%d/*", len(body)-1), string(body))
|
||||
resp.Body.Close()
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
req, _ = http.NewRequest(http.MethodPost, fmt.Sprintf("%s/caches/%d", base, reserved.CacheID), nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
resp, err = http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
resp = cacheRequest(t, token, http.MethodPost, fmt.Sprintf("%s/caches/%d", base, reserved.CacheID), "", "", "")
|
||||
resp.Body.Close()
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
|
||||
req, _ = http.NewRequest(http.MethodGet, base+"/cache?keys=ext-key&version=v", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
resp, err = http.DefaultClient.Do(req)
|
||||
require.NoError(t, err)
|
||||
resp = cacheRequest(t, token, http.MethodGet, base+"/cache?keys=ext-key&version=v", "", "", "")
|
||||
require.Equal(t, http.StatusOK, resp.StatusCode)
|
||||
var hit struct {
|
||||
ArchiveLocation string `json:"archiveLocation"`
|
||||
|
||||
@@ -8,31 +8,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestRunnerCloneEnvsReturnsTaskLocalCopy(t *testing.T) {
|
||||
r := &Runner{
|
||||
envs: map[string]string{
|
||||
"ACTIONS_CACHE_URL": "http://cache.example",
|
||||
"ACTIONS_RUNTIME_URL": "http://runner.example",
|
||||
},
|
||||
}
|
||||
|
||||
cloned := r.cloneEnvs()
|
||||
require.Equal(t, r.envs, cloned)
|
||||
|
||||
cloned["ACTIONS_RUNTIME_TOKEN"] = "task-token"
|
||||
cloned["ACTIONS_ID_TOKEN_REQUEST_URL"] = "http://oidc.example"
|
||||
|
||||
assert.NotContains(t, r.envs, "ACTIONS_RUNTIME_TOKEN")
|
||||
assert.NotContains(t, r.envs, "ACTIONS_ID_TOKEN_REQUEST_URL")
|
||||
assert.Equal(t, "http://cache.example", r.envs["ACTIONS_CACHE_URL"])
|
||||
}
|
||||
|
||||
// Regression test for #958: concurrent tasks writing task-specific env keys
|
||||
// used to race on the shared r.envs map and crash the runner with
|
||||
// "fatal error: concurrent map writes". Each task must mutate its own clone.
|
||||
func TestRunnerCloneEnvsConcurrentMutation(t *testing.T) {
|
||||
r := &Runner{
|
||||
envs: map[string]string{
|
||||
@@ -48,6 +25,7 @@ func TestRunnerCloneEnvsConcurrentMutation(t *testing.T) {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
envs := r.cloneEnvs()
|
||||
assert.Equal(t, r.envs, envs)
|
||||
envs["ACTIONS_RUNTIME_TOKEN"] = "task-token"
|
||||
envs["ACTIONS_ID_TOKEN_REQUEST_URL"] = "http://oidc.example"
|
||||
envs["ACTIONS_ID_TOKEN_REQUEST_TOKEN"] = "oidc-token"
|
||||
@@ -55,7 +33,8 @@ func TestRunnerCloneEnvsConcurrentMutation(t *testing.T) {
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
assert.NotContains(t, r.envs, "ACTIONS_RUNTIME_TOKEN")
|
||||
assert.NotContains(t, r.envs, "ACTIONS_ID_TOKEN_REQUEST_URL")
|
||||
assert.NotContains(t, r.envs, "ACTIONS_ID_TOKEN_REQUEST_TOKEN")
|
||||
assert.Equal(t, map[string]string{
|
||||
"ACTIONS_CACHE_URL": "http://cache.example",
|
||||
"ACTIONS_RUNTIME_URL": "http://runner.example",
|
||||
}, r.envs)
|
||||
}
|
||||
@@ -12,7 +12,6 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gitea.com/gitea/runner/internal/pkg/report"
|
||||
"gitea.com/gitea/runner/internal/pkg/ver"
|
||||
|
||||
runnerv1 "gitea.dev/actionslib/runner/v1"
|
||||
@@ -23,14 +22,6 @@ import (
|
||||
// falls back to the Go runtime alone. A var so tests can point it at a fixture.
|
||||
var osReleasePath = "/etc/os-release"
|
||||
|
||||
// reportSetup opens the job log the way actions/runner opens its "Set up job" step. The action
|
||||
// downloads and the closing job name are written later, as the job starts.
|
||||
func (r *Runner) reportSetup(reporter *report.Reporter, task *runnerv1.Task) {
|
||||
for _, line := range r.setupLines(task) {
|
||||
reporter.Logf("%s", line)
|
||||
}
|
||||
}
|
||||
|
||||
// setupLines names the runner, then reports what it was asked to run and the host it runs on, each
|
||||
// in its own group.
|
||||
func (r *Runner) setupLines(task *runnerv1.Task) []string {
|
||||
@@ -52,8 +43,10 @@ func (r *Runner) setupLines(task *runnerv1.Task) []string {
|
||||
)
|
||||
lines = append(lines, inputLines(fields)...)
|
||||
lines = append(lines, "::group::Operating System")
|
||||
lines = append(lines, osInfo()...)
|
||||
return append(lines, "::endgroup::")
|
||||
if name := prettyOSName(); name != "" {
|
||||
lines = append(lines, name)
|
||||
}
|
||||
return append(lines, fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH), "::endgroup::")
|
||||
}
|
||||
|
||||
// inputLines lists the inputs of the job: those Gitea resolved for a called workflow's job,
|
||||
@@ -75,15 +68,6 @@ func inputLines(fields map[string]*structpb.Value) []string {
|
||||
return append(lines, "::endgroup::")
|
||||
}
|
||||
|
||||
// osInfo describes the host the runner executes on.
|
||||
func osInfo() []string {
|
||||
lines := make([]string, 0, 2)
|
||||
if name := prettyOSName(); name != "" {
|
||||
lines = append(lines, name)
|
||||
}
|
||||
return append(lines, fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH))
|
||||
}
|
||||
|
||||
// prettyOSName reads PRETTY_NAME (e.g. "Ubuntu 24.04.4 LTS") from os-release, or "" when absent.
|
||||
func prettyOSName() string {
|
||||
data, err := os.ReadFile(osReleasePath)
|
||||
|
||||
@@ -96,33 +96,23 @@ func TestSetupLinesInputs(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPrettyOSName(t *testing.T) {
|
||||
tests := map[string]struct {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
osRelease string
|
||||
want string
|
||||
missing bool
|
||||
}{
|
||||
"quoted value": {
|
||||
osRelease: "NAME=\"Ubuntu\"\nVERSION_ID=\"24.04\"\nPRETTY_NAME=\"Ubuntu 24.04.4 LTS\"\n",
|
||||
want: "Ubuntu 24.04.4 LTS",
|
||||
},
|
||||
"unquoted value": {
|
||||
osRelease: "PRETTY_NAME=Alpine Linux v3.21\n",
|
||||
want: "Alpine Linux v3.21",
|
||||
},
|
||||
"no pretty name": {
|
||||
osRelease: "NAME=\"Ubuntu\"\nVERSION_ID=\"24.04\"\n",
|
||||
want: "",
|
||||
},
|
||||
// A key that merely ends in PRETTY_NAME must not be mistaken for it.
|
||||
"similar key": {
|
||||
osRelease: "IMAGE_PRETTY_NAME=\"Ubuntu Core 24\"\n",
|
||||
want: "",
|
||||
},
|
||||
}
|
||||
|
||||
for name, tt := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
{"quoted value", "NAME=\"Ubuntu\"\nVERSION_ID=\"24.04\"\nPRETTY_NAME=\"Ubuntu 24.04.4 LTS\"\n", "Ubuntu 24.04.4 LTS", false},
|
||||
{"unquoted value", "PRETTY_NAME=Alpine Linux v3.21\n", "Alpine Linux v3.21", false},
|
||||
{"no pretty name", "NAME=\"Ubuntu\"\nVERSION_ID=\"24.04\"\n", "", false},
|
||||
{"similar key", "IMAGE_PRETTY_NAME=\"Ubuntu Core 24\"\n", "", false},
|
||||
{"missing file", "", "", true},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "os-release")
|
||||
require.NoError(t, os.WriteFile(path, []byte(tt.osRelease), 0o600))
|
||||
if !tt.missing {
|
||||
require.NoError(t, os.WriteFile(path, []byte(tt.osRelease), 0o600))
|
||||
}
|
||||
|
||||
original := osReleasePath
|
||||
osReleasePath = path
|
||||
@@ -131,12 +121,4 @@ func TestPrettyOSName(t *testing.T) {
|
||||
assert.Equal(t, tt.want, prettyOSName())
|
||||
})
|
||||
}
|
||||
|
||||
t.Run("missing file", func(t *testing.T) {
|
||||
original := osReleasePath
|
||||
osReleasePath = filepath.Join(t.TempDir(), "absent")
|
||||
defer func() { osReleasePath = original }()
|
||||
|
||||
assert.Empty(t, prettyOSName())
|
||||
})
|
||||
}
|
||||
@@ -6,7 +6,7 @@ package run
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"sort"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"gitea.dev/actionslib/pkg/model"
|
||||
@@ -26,27 +26,21 @@ func generateWorkflow(task *runnerv1.Task) (*model.Workflow, string, error) {
|
||||
}
|
||||
jobID := jobIDs[0]
|
||||
|
||||
needJobIDs := make([]string, 0, len(task.Needs))
|
||||
rawNeeds := yaml.Node{
|
||||
Kind: yaml.SequenceNode,
|
||||
Content: make([]*yaml.Node, 0, len(task.Needs)),
|
||||
}
|
||||
for id, need := range task.Needs {
|
||||
needJobIDs = append(needJobIDs, id)
|
||||
needJob := &model.Job{
|
||||
workflow.Jobs[id] = &model.Job{
|
||||
Outputs: need.Outputs,
|
||||
Result: strings.ToLower(strings.TrimPrefix(need.Result.String(), "RESULT_")),
|
||||
}
|
||||
workflow.Jobs[id] = needJob
|
||||
}
|
||||
sort.Strings(needJobIDs)
|
||||
|
||||
rawNeeds := yaml.Node{
|
||||
Kind: yaml.SequenceNode,
|
||||
Content: make([]*yaml.Node, 0, len(needJobIDs)),
|
||||
}
|
||||
for _, id := range needJobIDs {
|
||||
rawNeeds.Content = append(rawNeeds.Content, &yaml.Node{
|
||||
Kind: yaml.ScalarNode,
|
||||
Value: id,
|
||||
})
|
||||
}
|
||||
slices.SortFunc(rawNeeds.Content, func(left, right *yaml.Node) int { return strings.Compare(left.Value, right.Value) })
|
||||
|
||||
workflow.Jobs[jobID].RawNeeds = rawNeeds
|
||||
|
||||
|
||||
@@ -13,21 +13,17 @@ import (
|
||||
)
|
||||
|
||||
func Test_generateWorkflow(t *testing.T) {
|
||||
type args struct {
|
||||
task *runnerv1.Task
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
payload string
|
||||
needs map[string]*runnerv1.TaskNeed
|
||||
assert func(t *testing.T, wf *model.Workflow)
|
||||
want1 string
|
||||
jobID string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "has needs",
|
||||
args: args{
|
||||
task: &runnerv1.Task{
|
||||
WorkflowPayload: []byte(`
|
||||
payload: `
|
||||
name: Build and deploy
|
||||
on: push
|
||||
|
||||
@@ -39,34 +35,29 @@ jobs:
|
||||
- uses: actions/checkout@v3
|
||||
- run: ./deploy --build ${{ needs.job1.outputs.output1 }}
|
||||
- run: ./deploy --build ${{ needs.job2.outputs.output2 }}
|
||||
`),
|
||||
Needs: map[string]*runnerv1.TaskNeed{
|
||||
"job1": {
|
||||
Outputs: map[string]string{
|
||||
"output1": "output1 value",
|
||||
},
|
||||
Result: runnerv1.Result_RESULT_SUCCESS,
|
||||
},
|
||||
"job2": {
|
||||
Outputs: map[string]string{
|
||||
"output2": "output2 value",
|
||||
},
|
||||
Result: runnerv1.Result_RESULT_SUCCESS,
|
||||
},
|
||||
`,
|
||||
needs: map[string]*runnerv1.TaskNeed{
|
||||
"job1": {
|
||||
Outputs: map[string]string{
|
||||
"output1": "output1 value",
|
||||
},
|
||||
Result: runnerv1.Result_RESULT_SUCCESS,
|
||||
},
|
||||
"job2": {
|
||||
Outputs: map[string]string{
|
||||
"output2": "output2 value",
|
||||
},
|
||||
Result: runnerv1.Result_RESULT_SUCCESS,
|
||||
},
|
||||
},
|
||||
assert: func(t *testing.T, wf *model.Workflow) {
|
||||
require.Equal(t, []string{"job1", "job2"}, wf.GetJob("job9").Needs())
|
||||
},
|
||||
want1: "job9",
|
||||
wantErr: false,
|
||||
jobID: "job9",
|
||||
},
|
||||
{
|
||||
name: "no needs",
|
||||
args: args{
|
||||
task: &runnerv1.Task{
|
||||
WorkflowPayload: []byte(`
|
||||
payload: `
|
||||
name: Simple workflow
|
||||
on: push
|
||||
|
||||
@@ -76,23 +67,18 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- run: echo "hello"
|
||||
`),
|
||||
Needs: map[string]*runnerv1.TaskNeed{},
|
||||
},
|
||||
},
|
||||
`,
|
||||
needs: map[string]*runnerv1.TaskNeed{},
|
||||
assert: func(t *testing.T, wf *model.Workflow) {
|
||||
job := wf.GetJob("test")
|
||||
require.Equal(t, []string{}, job.Needs())
|
||||
require.Len(t, job.Steps, 2)
|
||||
},
|
||||
want1: "test",
|
||||
wantErr: false,
|
||||
jobID: "test",
|
||||
},
|
||||
{
|
||||
name: "needs list",
|
||||
args: args{
|
||||
task: &runnerv1.Task{
|
||||
WorkflowPayload: []byte(`
|
||||
payload: `
|
||||
name: Workflow with list needs
|
||||
on: push
|
||||
|
||||
@@ -102,40 +88,34 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- run: echo "deploying"
|
||||
`),
|
||||
Needs: map[string]*runnerv1.TaskNeed{
|
||||
"build": {
|
||||
Outputs: map[string]string{},
|
||||
Result: runnerv1.Result_RESULT_SUCCESS,
|
||||
},
|
||||
"test": {
|
||||
Outputs: map[string]string{
|
||||
"coverage": "80%",
|
||||
},
|
||||
Result: runnerv1.Result_RESULT_SUCCESS,
|
||||
},
|
||||
"lint": {
|
||||
Outputs: map[string]string{},
|
||||
Result: runnerv1.Result_RESULT_FAILURE,
|
||||
},
|
||||
`,
|
||||
needs: map[string]*runnerv1.TaskNeed{
|
||||
"build": {
|
||||
Outputs: map[string]string{},
|
||||
Result: runnerv1.Result_RESULT_SUCCESS,
|
||||
},
|
||||
"test": {
|
||||
Outputs: map[string]string{
|
||||
"coverage": "80%",
|
||||
},
|
||||
Result: runnerv1.Result_RESULT_SUCCESS,
|
||||
},
|
||||
"lint": {
|
||||
Outputs: map[string]string{},
|
||||
Result: runnerv1.Result_RESULT_FAILURE,
|
||||
},
|
||||
},
|
||||
assert: func(t *testing.T, wf *model.Workflow) {
|
||||
job := wf.GetJob("deploy")
|
||||
needs := job.Needs()
|
||||
require.Equal(t, []string{"build", "lint", "test"}, needs)
|
||||
require.Equal(t, []string{"build", "lint", "test"}, job.Needs())
|
||||
require.Equal(t, "80%", wf.Jobs["test"].Outputs["coverage"])
|
||||
require.Equal(t, "failure", wf.Jobs["lint"].Result)
|
||||
},
|
||||
want1: "deploy",
|
||||
wantErr: false,
|
||||
jobID: "deploy",
|
||||
},
|
||||
{
|
||||
name: "workflow env and defaults",
|
||||
args: args{
|
||||
task: &runnerv1.Task{
|
||||
WorkflowPayload: []byte(`
|
||||
payload: `
|
||||
name: Complex workflow
|
||||
on:
|
||||
push:
|
||||
@@ -159,25 +139,19 @@ jobs:
|
||||
node-version: "18"
|
||||
- run: npm ci
|
||||
- run: npm run build
|
||||
`),
|
||||
Needs: map[string]*runnerv1.TaskNeed{},
|
||||
},
|
||||
},
|
||||
`,
|
||||
needs: map[string]*runnerv1.TaskNeed{},
|
||||
assert: func(t *testing.T, wf *model.Workflow) {
|
||||
require.Equal(t, "Complex workflow", wf.Name)
|
||||
require.Equal(t, "production", wf.Env["NODE_ENV"])
|
||||
require.Equal(t, "true", wf.Env["CI"])
|
||||
job := wf.GetJob("build")
|
||||
require.Len(t, job.Steps, 4)
|
||||
require.Len(t, wf.GetJob("build").Steps, 4)
|
||||
},
|
||||
want1: "build",
|
||||
wantErr: false,
|
||||
jobID: "build",
|
||||
},
|
||||
{
|
||||
name: "job with container and services",
|
||||
args: args{
|
||||
task: &runnerv1.Task{
|
||||
WorkflowPayload: []byte(`
|
||||
payload: `
|
||||
name: Integration tests
|
||||
on: push
|
||||
|
||||
@@ -192,24 +166,18 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- run: npm test
|
||||
`),
|
||||
Needs: map[string]*runnerv1.TaskNeed{},
|
||||
},
|
||||
},
|
||||
`,
|
||||
needs: map[string]*runnerv1.TaskNeed{},
|
||||
assert: func(t *testing.T, wf *model.Workflow) {
|
||||
job := wf.GetJob("integration")
|
||||
container := job.Container()
|
||||
require.Equal(t, "node:18", container.Image)
|
||||
require.Equal(t, "node:18", job.Container().Image)
|
||||
require.Equal(t, "postgres:15", job.Services["postgres"].Image)
|
||||
},
|
||||
want1: "integration",
|
||||
wantErr: false,
|
||||
jobID: "integration",
|
||||
},
|
||||
{
|
||||
name: "job with matrix strategy",
|
||||
args: args{
|
||||
task: &runnerv1.Task{
|
||||
WorkflowPayload: []byte(`
|
||||
payload: `
|
||||
name: Matrix build
|
||||
on: push
|
||||
|
||||
@@ -222,40 +190,28 @@ jobs:
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- run: go test ./...
|
||||
`),
|
||||
Needs: map[string]*runnerv1.TaskNeed{},
|
||||
},
|
||||
},
|
||||
`,
|
||||
needs: map[string]*runnerv1.TaskNeed{},
|
||||
assert: func(t *testing.T, wf *model.Workflow) {
|
||||
job := wf.GetJob("test")
|
||||
matrixes, err := job.GetMatrixes()
|
||||
matrixes, err := wf.GetJob("test").GetMatrixes()
|
||||
require.NoError(t, err)
|
||||
require.Len(t, matrixes, 2)
|
||||
},
|
||||
want1: "test",
|
||||
wantErr: false,
|
||||
jobID: "test",
|
||||
},
|
||||
{
|
||||
name: "special yaml characters in values",
|
||||
args: args{
|
||||
task: &runnerv1.Task{
|
||||
WorkflowPayload: []byte("name: \"Special: characters & test\"\non: push\n\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: 'echo \"hello: world\"'\n - run: 'echo \"quotes & ampersands\"'\n - run: |\n echo \"multiline\"\n echo \"script\"\n"),
|
||||
Needs: map[string]*runnerv1.TaskNeed{},
|
||||
},
|
||||
},
|
||||
name: "special yaml characters in values",
|
||||
payload: "name: \"Special: characters & test\"\non: push\n\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: 'echo \"hello: world\"'\n - run: 'echo \"quotes & ampersands\"'\n - run: |\n echo \"multiline\"\n echo \"script\"\n",
|
||||
needs: map[string]*runnerv1.TaskNeed{},
|
||||
assert: func(t *testing.T, wf *model.Workflow) {
|
||||
require.Equal(t, "Special: characters & test", wf.Name)
|
||||
job := wf.GetJob("test")
|
||||
require.Len(t, job.Steps, 3)
|
||||
require.Len(t, wf.GetJob("test").Steps, 3)
|
||||
},
|
||||
want1: "test",
|
||||
wantErr: false,
|
||||
jobID: "test",
|
||||
},
|
||||
{
|
||||
name: "invalid yaml",
|
||||
args: args{
|
||||
task: &runnerv1.Task{
|
||||
WorkflowPayload: []byte(`
|
||||
payload: `
|
||||
name: Bad workflow
|
||||
on: push
|
||||
jobs:
|
||||
@@ -264,25 +220,21 @@ jobs:
|
||||
steps:
|
||||
- run: echo "ok"
|
||||
bad-indent: true
|
||||
`),
|
||||
Needs: map[string]*runnerv1.TaskNeed{},
|
||||
},
|
||||
},
|
||||
assert: nil,
|
||||
want1: "",
|
||||
`,
|
||||
needs: map[string]*runnerv1.TaskNeed{},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, got1, err := generateWorkflow(tt.args.task)
|
||||
got, got1, err := generateWorkflow(&runnerv1.Task{WorkflowPayload: []byte(tt.payload), Needs: tt.needs})
|
||||
if tt.wantErr {
|
||||
require.Error(t, err)
|
||||
return
|
||||
}
|
||||
require.NoError(t, err)
|
||||
tt.assert(t, got)
|
||||
require.Equal(t, tt.want1, got1)
|
||||
require.Equal(t, tt.jobID, got1)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -321,7 +273,6 @@ func Test_yamlV4NodeRoundTrip(t *testing.T) {
|
||||
})
|
||||
|
||||
t.Run("node kind constants", func(t *testing.T) {
|
||||
// Verify yaml/v4 node kind constants are usable (same API as v3)
|
||||
require.NotEqual(t, yaml.ScalarNode, yaml.SequenceNode)
|
||||
require.NotEqual(t, yaml.SequenceNode, yaml.MappingNode)
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user