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:
silverwind authored and silverwind committed 2026-10-22 19:41:10 +00:00
1 parent 923a0bfb59
commit d882311ec6
107 files changed
+2408 -6500

No files matched your search

-1
View File
@@ -11,7 +11,6 @@ on:
env: env:
DOCKER_ORG: gitea DOCKER_ORG: gitea
DOCKER_LATEST: nightly
jobs: jobs:
goreleaser: goreleaser:
-1
View File
@@ -69,7 +69,6 @@ jobs:
image: catthehacker/ubuntu:act-latest image: catthehacker/ubuntu:act-latest
env: env:
DOCKER_ORG: gitea DOCKER_ORG: gitea
DOCKER_LATEST: latest
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
-20
View File
@@ -28,38 +28,18 @@ builds:
ignore: ignore:
- goos: darwin - goos: darwin
goarch: arm goarch: arm
- goos: darwin
goarch: ppc64le
- goos: darwin - goos: darwin
goarch: s390x goarch: s390x
- goos: windows
goarch: ppc64le
- goos: windows - goos: windows
goarch: s390x goarch: s390x
- goos: windows - goos: windows
goarch: arm goarch: arm
goarm: "5"
- goos: windows
goarch: arm
goarm: "6"
- goos: windows
goarch: arm
goarm: "7"
- goos: windows - goos: windows
goarch: arm64 goarch: arm64
- goos: freebsd
goarch: ppc64le
- goos: freebsd - goos: freebsd
goarch: s390x goarch: s390x
- goos: freebsd - goos: freebsd
goarch: arm goarch: arm
goarm: "5"
- goos: freebsd
goarch: arm
goarm: "6"
- goos: freebsd
goarch: arm
goarm: "7"
- goos: freebsd - goos: freebsd
goarch: arm64 goarch: arm64
flags: flags:
-4
View File
@@ -14,7 +14,6 @@ GOFILES := $(shell find . -type f -name "*.go" -o -name "go.mod" ! -name "genera
DOCKER_IMAGE ?= gitea/runner DOCKER_IMAGE ?= gitea/runner
DOCKER_TAG ?= nightly DOCKER_TAG ?= nightly
DOCKER_REF := $(DOCKER_IMAGE):$(DOCKER_TAG) DOCKER_REF := $(DOCKER_IMAGE):$(DOCKER_TAG)
DOCKER_ROOTLESS_REF := $(DOCKER_IMAGE):$(DOCKER_TAG)-dind-rootless
GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/v2/cmd/[email protected] # renovate: datasource=go GOLANGCI_LINT_PACKAGE ?= github.com/golangci/golangci-lint/v2/cmd/[email protected] # renovate: datasource=go
GOVULNCHECK_PACKAGE ?= golang.org/x/vuln/cmd/[email protected] # renovate: datasource=go GOVULNCHECK_PACKAGE ?= golang.org/x/vuln/cmd/[email protected] # renovate: datasource=go
@@ -34,15 +33,12 @@ endif
ifeq ($(OS), Windows_NT) ifeq ($(OS), Windows_NT)
GOFLAGS := -v -buildmode=exe GOFLAGS := -v -buildmode=exe
EXECUTABLE ?= $(EXECUTABLE).exe
GO_ENV_WINDOWS := set GOOS=windows&& GO_ENV_WINDOWS := set GOOS=windows&&
else ifeq ($(OS), Windows) else ifeq ($(OS), Windows)
GOFLAGS := -v -buildmode=exe GOFLAGS := -v -buildmode=exe
EXECUTABLE ?= $(EXECUTABLE).exe
GO_ENV_WINDOWS := set GOOS=windows&& GO_ENV_WINDOWS := set GOOS=windows&&
else else
GOFLAGS := -v GOFLAGS := -v
EXECUTABLE ?= $(EXECUTABLE)
GO_ENV_WINDOWS := GOOS=windows GO_ENV_WINDOWS := GOOS=windows
endif endif
+11 -34
View File
@@ -81,7 +81,6 @@ type Handler struct {
dir string dir string
storage cacheStorage storage cacheStorage
s3 *s3Storage s3 *s3Storage
router *http.ServeMux
listener net.Listener listener net.Listener
port int port int
server *http.Server server *http.Server
@@ -200,11 +199,9 @@ func StartHandler(opts Options) (*Handler, error) {
h.storage = h.s3 h.storage = h.s3
} }
secret, err := loadOrCreateSecret(dir) if h.secret, err = loadOrCreateSecret(dir); err != nil {
if err != nil {
return nil, err return nil, err
} }
h.secret = secret
router := http.NewServeMux() router := http.NewServeMux()
router.HandleFunc("GET "+apiPath+"/cache", h.bearerAuth(h.find)) router.HandleFunc("GET "+apiPath+"/cache", h.bearerAuth(h.find))
@@ -223,8 +220,6 @@ func StartHandler(opts Options) (*Handler, error) {
h.registerV2Routes(router) h.registerV2Routes(router)
router.HandleFunc(artifactServicePath, h.forwardOrNotFound) router.HandleFunc(artifactServicePath, h.forwardOrNotFound)
h.router = router
h.gcCache() h.gcCache()
if err := h.serve(opts.Port, router); err != nil { if err := h.serve(opts.Port, router); err != nil {
@@ -333,16 +328,7 @@ func (h *Handler) RegisterJob(token string, cred JobCredential) func() {
} }
} }
h.credMu.Unlock() h.credMu.Unlock()
return func() { return func() { h.RevokeJob(token) }
h.credMu.Lock()
if entry, ok := h.creds[token]; ok {
entry.refs--
if entry.refs <= 0 {
delete(h.creds, token)
}
}
h.credMu.Unlock()
}
} }
// RevokeJob explicitly revokes one registration of token, mirroring one call // RevokeJob explicitly revokes one registration of token, mirroring one call
@@ -405,10 +391,7 @@ func (h *Handler) Close() error {
} }
var retErr error var retErr error
if h.server != nil { if h.server != nil {
err := h.server.Close() retErr = h.server.Close()
if err != nil {
retErr = err
}
h.server = nil h.server = nil
} }
if h.listener != nil { if h.listener != nil {
@@ -774,7 +757,7 @@ func (h *Handler) upload(w http.ResponseWriter, r *http.Request) {
return return
} }
db.Close() db.Close()
start, _, err := parseContentRange(r.Header.Get("Content-Range")) start, err := parseContentRange(r.Header.Get("Content-Range"))
if err != nil { if err != nil {
h.responseJSON(w, r, 400, err) h.responseJSON(w, r, 400, err)
return return
@@ -1041,10 +1024,8 @@ func bearerToken(r *http.Request) string {
} }
func credFromContext(ctx context.Context) JobCredential { func credFromContext(ctx context.Context) JobCredential {
if cred, ok := ctx.Value(credKey{}).(JobCredential); ok { cred, _ := ctx.Value(credKey{}).(JobCredential)
return cred return cred
}
return JobCredential{}
} }
// computeSignature signs a URL for one cache entry and expiry. purpose is mixed into the // computeSignature signs a URL for one cache entry and expiry. purpose is mixed into the
@@ -1209,9 +1190,6 @@ func (p Policy) withDefaults() Policy {
} }
func (h *Handler) gcCache() { func (h *Handler) gcCache() {
if h.gcing.Load() {
return
}
if !h.gcing.CompareAndSwap(false, true) { if !h.gcing.CompareAndSwap(false, true) {
return return
} }
@@ -1458,18 +1436,17 @@ func (h *Handler) responseJSON(w http.ResponseWriter, r *http.Request, code int,
_, _ = w.Write(data) _, _ = w.Write(data)
} }
func parseContentRange(s string) (int64, int64, error) { func parseContentRange(s string) (int64, error) {
// support the format like "bytes 11-22/*" only // support the format like "bytes 11-22/*" only
s, _, _ = strings.Cut(strings.TrimPrefix(s, "bytes "), "/") s, _, _ = strings.Cut(strings.TrimPrefix(s, "bytes "), "/")
s1, s2, _ := strings.Cut(s, "-") s1, s2, _ := strings.Cut(s, "-")
start, err := strconv.ParseInt(s1, 10, 64) start, err := strconv.ParseInt(s1, 10, 64)
if err != nil { if err != nil {
return 0, 0, fmt.Errorf("parse %q: %w", s, err) return 0, fmt.Errorf("parse %q: %w", s, err)
} }
stop, err := strconv.ParseInt(s2, 10, 64) if _, err := strconv.ParseInt(s2, 10, 64); err != nil {
if err != nil { return 0, fmt.Errorf("parse %q: %w", s, err)
return 0, 0, fmt.Errorf("parse %q: %w", s, err)
} }
return start, stop, nil return start, nil
} }
File diff suppressed because it is too large. Load diff
+4 -36
View File
@@ -19,8 +19,6 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// v2Call posts a twirp request to the cache service and returns the decoded response.
// Field names are the proto ones, which is what the toolkit's client sends.
func v2Call(t *testing.T, handler *Handler, client *http.Client, method string, request any) map[string]any { func v2Call(t *testing.T, handler *Handler, client *http.Client, method string, request any) map[string]any {
t.Helper() t.Helper()
@@ -37,7 +35,6 @@ func v2Call(t *testing.T, handler *Handler, client *http.Client, method string,
return got return got
} }
// putBlob uploads to a signed URL and returns the status, so a test can assert a refusal.
func putBlob(t *testing.T, url string, content []byte) int { func putBlob(t *testing.T, url string, content []byte) int {
t.Helper() t.Helper()
@@ -47,8 +44,6 @@ func putBlob(t *testing.T, url string, content []byte) int {
require.NoError(t, err) require.NoError(t, err)
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode == http.StatusCreated { if resp.StatusCode == http.StatusCreated {
// The Azure SDK client dereferences this header without checking, so a blob upload that
// omits it panics the caller rather than failing it.
require.NotEmpty(t, resp.Header.Get("x-ms-request-id")) require.NotEmpty(t, resp.Header.Get("x-ms-request-id"))
} }
return resp.StatusCode return resp.StatusCode
@@ -67,8 +62,6 @@ func getURL(t *testing.T, url string) []byte {
return body return body
} }
// saveV2 runs the reserve/upload/finalize sequence and returns the finalize response along
// with the upload URL it used.
func saveV2(t *testing.T, handler *Handler, key, version string, content []byte) (finalized map[string]any, uploadURL string) { func saveV2(t *testing.T, handler *Handler, key, version string, content []byte) (finalized map[string]any, uploadURL string) {
t.Helper() t.Helper()
@@ -84,9 +77,6 @@ func saveV2(t *testing.T, handler *Handler, key, version string, content []byte)
}), uploadURL }), uploadURL
} }
// The whole round trip an actions/cache v2 client makes, plus the guarantees on the signed
// URLs it is handed: unsigned requests are refused, an upload URL cannot be replayed to read
// or to replace a finalized entry.
func TestCacheServiceV2RoundTrip(t *testing.T) { func TestCacheServiceV2RoundTrip(t *testing.T) {
handler := newTestHandler(t, Policy{}) handler := newTestHandler(t, Policy{})
content := []byte("the cached archive") content := []byte("the cached archive")
@@ -98,10 +88,8 @@ func TestCacheServiceV2RoundTrip(t *testing.T) {
require.Equal(t, true, finalized["ok"]) require.Equal(t, true, finalized["ok"])
assert.NotEmpty(t, finalized["entry_id"]) assert.NotEmpty(t, finalized["entry_id"])
// The upload URL outlives the finalize call, so replaying it must not poison the entry,
// and it is an upload URL only: nothing reads a blob back through it.
assert.Equal(t, http.StatusBadRequest, putBlob(t, uploadURL, []byte("poisoned"))) assert.Equal(t, http.StatusBadRequest, putBlob(t, uploadURL, []byte("poisoned")))
resp, err := http.Get(uploadURL) //nolint:noctx // the URL is the server under test resp, err := http.Get(uploadURL)
require.NoError(t, err) require.NoError(t, err)
defer resp.Body.Close() defer resp.Body.Close()
assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode) assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode)
@@ -114,8 +102,6 @@ func TestCacheServiceV2RoundTrip(t *testing.T) {
assert.Equal(t, content, getURL(t, downloadURL)) assert.Equal(t, content, getURL(t, downloadURL))
} }
// A large archive is staged as blocks and only put in order by the final block list, so
// blocks that arrive out of order must still be assembled the way the client asked.
func TestCacheServiceV2BlockUpload(t *testing.T) { func TestCacheServiceV2BlockUpload(t *testing.T) {
handler := newTestHandler(t, Policy{}) handler := newTestHandler(t, Policy{})
@@ -130,7 +116,6 @@ func TestCacheServiceV2BlockUpload(t *testing.T) {
blocks[blockID] = []byte(part) blocks[blockID] = []byte(part)
order = append(order, blockID) order = append(order, blockID)
} }
// Upload in an order that is not the block list order.
for _, blockID := range []string{order[2], order[0], order[1]} { for _, blockID := range []string{order[2], order[0], order[1]} {
require.Equal(t, http.StatusCreated, putBlob(t, uploadURL+"&comp=block&blockid="+blockID, blocks[blockID])) require.Equal(t, http.StatusCreated, putBlob(t, uploadURL+"&comp=block&blockid="+blockID, blocks[blockID]))
} }
@@ -174,8 +159,6 @@ func TestCacheServiceV2Lookups(t *testing.T) {
assert.Equal(t, false, got["ok"]) assert.Equal(t, false, got["ok"])
}) })
// The toolkit serialises with the proto field names; the camelCase spellings of the same
// proto JSON mapping are accepted alongside them.
for _, field := range []string{"restore_keys", "restoreKeys"} { for _, field := range []string{"restore_keys", "restoreKeys"} {
t.Run("restore keys match by prefix, spelled "+field, func(t *testing.T) { t.Run("restore keys match by prefix, spelled "+field, func(t *testing.T) {
got := v2Call(t, handler, testClient, "GetCacheEntryDownloadURL", map[string]any{ got := v2Call(t, handler, testClient, "GetCacheEntryDownloadURL", map[string]any{
@@ -191,9 +174,6 @@ func TestCacheServiceV2Lookups(t *testing.T) {
assert.Equal(t, false, again["ok"]) assert.Equal(t, false, again["ok"])
}) })
// A key that is only a prefix of an existing one is a different entry, so the
// reservation check must be exact and not a restore-key prefix match, or the shorter
// key would be reported as existing and silently never saved.
t.Run("a prefix of an existing key is still reserved", func(t *testing.T) { t.Run("a prefix of an existing key is still reserved", func(t *testing.T) {
reserved := v2Call(t, handler, testClient, "CreateCacheEntry", map[string]any{"key": "deps", "version": "v1"}) reserved := v2Call(t, handler, testClient, "CreateCacheEntry", map[string]any{"key": "deps", "version": "v1"})
require.Equal(t, true, reserved["ok"]) require.Equal(t, true, reserved["ok"])
@@ -219,7 +199,6 @@ func TestCacheServiceV2Lookups(t *testing.T) {
assert.Equal(t, false, got["ok"]) assert.Equal(t, false, got["ok"])
}) })
// The size the client declares is what Commit validates the assembled archive against.
t.Run("finalizing with the wrong size is not ok", func(t *testing.T) { t.Run("finalizing with the wrong size is not ok", func(t *testing.T) {
created := v2Call(t, handler, testClient, "CreateCacheEntry", map[string]any{"key": "wrong-size", "version": "v1"}) created := v2Call(t, handler, testClient, "CreateCacheEntry", map[string]any{"key": "wrong-size", "version": "v1"})
require.Equal(t, http.StatusCreated, putBlob(t, created["signed_upload_url"].(string), []byte("four"))) require.Equal(t, http.StatusCreated, putBlob(t, created["signed_upload_url"].(string), []byte("four")))
@@ -230,8 +209,6 @@ func TestCacheServiceV2Lookups(t *testing.T) {
assert.Equal(t, false, got["ok"]) assert.Equal(t, false, got["ok"])
}) })
// Both API versions are served from one store, so an entry written through v2 is a hit for
// a v1 client asking for the same key and version.
t.Run("a v1 client sees an entry written through v2", func(t *testing.T) { t.Run("a v1 client sees an entry written through v2", func(t *testing.T) {
resp, err := testClient.Get(fmt.Sprintf("%s%s/cache?keys=deps-abc&version=v1", handler.ExternalURL(), apiPath)) resp, err := testClient.Get(fmt.Sprintf("%s%s/cache?keys=deps-abc&version=v1", handler.ExternalURL(), apiPath))
require.NoError(t, err) require.NoError(t, err)
@@ -244,7 +221,6 @@ func TestCacheServiceV2Lookups(t *testing.T) {
assert.NotEmpty(t, got["archiveLocation"]) assert.NotEmpty(t, got["archiveLocation"])
}) })
// The cache of one repository must stay invisible to another, as it does for the v1 API.
t.Run("another repository sees nothing", func(t *testing.T) { t.Run("another repository sees nothing", func(t *testing.T) {
handler.RegisterJob("other-runtime-token", JobCredential{Repo: "other/repo"}) handler.RegisterJob("other-runtime-token", JobCredential{Repo: "other/repo"})
otherClient := &http.Client{Transport: &bearerTransport{token: "other-runtime-token"}} otherClient := &http.Client{Transport: &bearerTransport{token: "other-runtime-token"}}
@@ -321,20 +297,12 @@ func TestCacheServiceV2ConcurrentSaveAndRetries(t *testing.T) {
"another job's upload is not this job's to finalize") "another job's upload is not this job's to finalize")
base := handler.ExternalURL() + apiPath base := handler.ExternalURL() + apiPath
reserveV1, err := json.Marshal(&Request{Key: "v1-made", Version: "v1", Size: int64(len(content))}) madeByV1 := reserveCache(t, testClient, base, "v1-made", "v1", int64(len(content)))
require.NoError(t, err)
resp, err := testClient.Post(base+"/caches", "application/json", bytes.NewReader(reserveV1))
require.NoError(t, err)
var madeByV1 struct {
CacheID uint64 `json:"cacheId"`
}
require.NoError(t, json.UnmarshalRead(resp.Body, &madeByV1))
resp.Body.Close()
upload, err := http.NewRequestWithContext(t.Context(), http.MethodPatch, upload, err := http.NewRequestWithContext(t.Context(), http.MethodPatch,
fmt.Sprintf("%s/caches/%d", base, madeByV1.CacheID), bytes.NewReader(content)) fmt.Sprintf("%s/caches/%d", base, madeByV1), bytes.NewReader(content))
require.NoError(t, err) require.NoError(t, err)
upload.Header.Set("Content-Range", fmt.Sprintf("bytes 0-%d/*", len(content)-1)) upload.Header.Set("Content-Range", fmt.Sprintf("bytes 0-%d/*", len(content)-1))
resp, err = testClient.Do(upload) resp, err := testClient.Do(upload)
require.NoError(t, err) require.NoError(t, err)
resp.Body.Close() resp.Body.Close()
require.Equal(t, http.StatusOK, resp.StatusCode) require.Equal(t, http.StatusOK, resp.StatusCode)
+2 -8
View File
@@ -4,6 +4,7 @@
package artifactcache package artifactcache
import ( import (
"cmp"
"crypto/tls" "crypto/tls"
"errors" "errors"
"fmt" "fmt"
@@ -74,14 +75,7 @@ func (h *Handler) forwardOrNotFound(w http.ResponseWriter, r *http.Request) {
// A protocol switch loses the bearer or the agent, and an untrusted instance needs skipped verification. // A protocol switch loses the bearer or the agent, and an untrusted instance needs skipped verification.
func (h *Handler) mustProxy(r *http.Request, cred JobCredential, target *url.URL) bool { func (h *Handler) mustProxy(r *http.Request, cred JobCredential, target *url.URL) bool {
if cred.InsecureTLS && target.Scheme == "https" { return cred.InsecureTLS && target.Scheme == "https" || cmp.Or(r.Header.Get("X-Forwarded-Proto"), "http") != target.Scheme
return true
}
scheme := r.Header.Get("X-Forwarded-Proto")
if scheme == "" {
scheme = "http"
}
return scheme != target.Scheme
} }
func mergeQuery(target, request string) string { func mergeQuery(target, request string) string {
+5 -10
View File
@@ -32,13 +32,11 @@ func NewStorage(rootDir string) (*Storage, error) {
} }
func (s *Storage) Exist(id uint64) (bool, error) { func (s *Storage) Exist(id uint64) (bool, error) {
name := s.filename(id) _, err := os.Stat(s.filename(id))
if _, err := os.Stat(name); os.IsNotExist(err) { if os.IsNotExist(err) {
return false, nil return false, nil
} else if err != nil {
return false, err
} }
return true, nil return err == nil, err
} }
func (s *Storage) Write(id uint64, offset int64, reader io.Reader) error { func (s *Storage) Write(id uint64, offset int64, reader io.Reader) error {
@@ -92,9 +90,7 @@ func (s *Storage) OrderBlocks(id uint64, blockIDs []string) error {
} }
func (s *Storage) Commit(id uint64, size int64) (int64, error) { func (s *Storage) Commit(id uint64, size int64) (int64, error) {
defer func() { defer func() { _ = os.RemoveAll(s.tempDir(id)) }()
_ = os.RemoveAll(s.tempDir(id))
}()
name := s.filename(id) name := s.filename(id)
tempNames, err := s.tempNames(id) tempNames, err := s.tempNames(id)
@@ -153,8 +149,7 @@ func assemble(name string, tempNames []string) (int64, error) {
} }
func (s *Storage) Serve(w http.ResponseWriter, r *http.Request, id uint64) { func (s *Storage) Serve(w http.ResponseWriter, r *http.Request, id uint64) {
name := s.filename(id) http.ServeFile(w, r, s.filename(id))
http.ServeFile(w, r, name)
} }
// Remove deletes an entry's blob and any staged parts. It reports failure so the caller can // Remove deletes an entry's blob and any staged parts. It reports failure so the caller can
+4 -11
View File
@@ -79,8 +79,7 @@ func uploads(router *http.ServeMux, baseDir string) {
itemPath += gzipExtension itemPath += gzipExtension
} }
safeRunPath := safeResolve(baseDir, req.PathValue("runId")) safePath := safeResolve(safeResolve(baseDir, req.PathValue("runId")), itemPath)
safePath := safeResolve(safeRunPath, itemPath)
if err := os.MkdirAll(filepath.Dir(safePath), os.ModePerm); err != nil { if err := os.MkdirAll(filepath.Dir(safePath), os.ModePerm); err != nil {
panic(err) panic(err)
@@ -120,9 +119,7 @@ func downloads(router *http.ServeMux, baseDir string) {
router.HandleFunc("GET /_apis/pipelines/workflows/{runId}/artifacts", func(w http.ResponseWriter, req *http.Request) { router.HandleFunc("GET /_apis/pipelines/workflows/{runId}/artifacts", func(w http.ResponseWriter, req *http.Request) {
runID := req.PathValue("runId") runID := req.PathValue("runId")
safePath := safeResolve(baseDir, runID) entries, err := os.ReadDir(safeResolve(baseDir, runID))
entries, err := os.ReadDir(safePath)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@@ -156,15 +153,11 @@ func downloads(router *http.ServeMux, baseDir string) {
// if it was upload as gzip // if it was upload as gzip
rel = strings.TrimSuffix(rel, gzipExtension) rel = strings.TrimSuffix(rel, gzipExtension)
path := filepath.Join(itemPath, rel)
rel = filepath.ToSlash(rel)
path = filepath.ToSlash(path)
files = append(files, ContainerItem{ files = append(files, ContainerItem{
Path: path, Path: filepath.ToSlash(filepath.Join(itemPath, rel)),
ItemType: "file", ItemType: "file",
ContentLocation: fmt.Sprintf("http://%s/artifact/%s/%s/%s", req.Host, container, itemPath, rel), ContentLocation: fmt.Sprintf("http://%s/artifact/%s/%s/%s", req.Host, container, itemPath, filepath.ToSlash(rel)),
}) })
} }
return nil return nil
+2 -7
View File
@@ -14,13 +14,8 @@ const dryrunContextKeyVal = dryrunContextKey("dryrun")
// Dryrun returns true if the current context is dryrun // Dryrun returns true if the current context is dryrun
func Dryrun(ctx context.Context) bool { func Dryrun(ctx context.Context) bool {
val := ctx.Value(dryrunContextKeyVal) dryrun, _ := ctx.Value(dryrunContextKeyVal).(bool)
if val != nil { return dryrun
if dryrun, ok := val.(bool); ok {
return dryrun
}
}
return false
} }
// WithDryrun adds a value to the context for dryrun // WithDryrun adds a value to the context for dryrun
+7 -16
View File
@@ -21,8 +21,7 @@ type Conditional func(ctx context.Context) bool
// NewInfoExecutor is an executor that logs messages // NewInfoExecutor is an executor that logs messages
func NewInfoExecutor(format string, args ...any) Executor { func NewInfoExecutor(format string, args ...any) Executor {
return func(ctx context.Context) error { return func(ctx context.Context) error {
logger := Logger(ctx) Logger(ctx).Infof(format, args...)
logger.Infof(format, args...)
return nil return nil
} }
} }
@@ -30,8 +29,7 @@ func NewInfoExecutor(format string, args ...any) Executor {
// NewDebugExecutor is an executor that logs messages // NewDebugExecutor is an executor that logs messages
func NewDebugExecutor(format string, args ...any) Executor { func NewDebugExecutor(format string, args ...any) Executor {
return func(ctx context.Context) error { return func(ctx context.Context) error {
logger := Logger(ctx) Logger(ctx).Debugf(format, args...)
logger.Debugf(format, args...)
return nil return nil
} }
} }
@@ -39,9 +37,7 @@ func NewDebugExecutor(format string, args ...any) Executor {
// NewPipelineExecutor creates a new executor from a series of other executors // NewPipelineExecutor creates a new executor from a series of other executors
func NewPipelineExecutor(executors ...Executor) Executor { func NewPipelineExecutor(executors ...Executor) Executor {
if len(executors) == 0 { if len(executors) == 0 {
return func(ctx context.Context) error { return NewErrorExecutor(nil)
return nil
}
} }
var rtn Executor var rtn Executor
for _, executor := range executors { for _, executor := range executors {
@@ -87,8 +83,8 @@ func NewParallelExecutor(parallel int, executors ...Executor) Executor {
logging.Infof("NewParallelExecutor: Creating %d workers for %d executors", parallel, len(executors)) logging.Infof("NewParallelExecutor: Creating %d workers for %d executors", parallel, len(executors))
for i := 0; i < parallel; i++ { for workerID := range parallel {
go func(workerID int, work <-chan Executor, errs chan<- error) { go func() {
logging.Debugf("Worker %d started", workerID) logging.Debugf("Worker %d started", workerID)
taskCount := 0 taskCount := 0
for executor := range work { for executor := range work {
@@ -103,7 +99,7 @@ func NewParallelExecutor(parallel int, executors ...Executor) Executor {
}() }()
} }
logging.Debugf("Worker %d finished (%d tasks executed)", workerID, taskCount) logging.Debugf("Worker %d finished (%d tasks executed)", workerID, taskCount)
}(i, work, errs) }()
} }
for i := range executors { for i := range executors {
@@ -152,12 +148,7 @@ func (e Executor) If(conditional Conditional) Executor {
// IfNot only runs this executor if conditional is true // IfNot only runs this executor if conditional is true
func (e Executor) IfNot(conditional Conditional) Executor { func (e Executor) IfNot(conditional Conditional) Executor {
return func(ctx context.Context) error { return e.If(func(ctx context.Context) bool { return !conditional(ctx) })
if !conditional(ctx) {
return e(ctx)
}
return nil
}
} }
// IfBool only runs this executor if conditional is true // IfBool only runs this executor if conditional is true
+54 -100
View File
@@ -7,8 +7,6 @@ package common
import ( import (
"context" "context"
"errors" "errors"
"reflect"
"strings"
"sync" "sync"
"sync/atomic" "sync/atomic"
"testing" "testing"
@@ -18,42 +16,25 @@ import (
) )
func TestNewWorkflow(t *testing.T) { func TestNewWorkflow(t *testing.T) {
assert := assert.New(t)
ctx := t.Context() ctx := t.Context()
require.NoError(t, NewPipelineExecutor()(ctx))
require.EqualError(t, NewErrorExecutor(errors.New("test error"))(ctx), "test error")
// empty
emptyWorkflow := NewPipelineExecutor()
assert.NoError(emptyWorkflow(ctx)) //nolint:testifylint // pre-existing issue from nektos/act
// error case
errorWorkflow := NewErrorExecutor(errors.New("test error"))
assert.Error(errorWorkflow(ctx)) //nolint:testifylint // pre-existing issue from nektos/act
// multiple success case
runcount := 0 runcount := 0
successWorkflow := NewPipelineExecutor( increment := func(_ctx context.Context) error {
func(ctx context.Context) error { runcount++
runcount++ return nil
return nil }
}, require.NoError(t, NewPipelineExecutor(increment, increment)(ctx))
func(ctx context.Context) error { assert.Equal(t, 2, runcount)
runcount++
return nil
})
assert.NoError(successWorkflow(ctx)) //nolint:testifylint // pre-existing issue from nektos/act
assert.Equal(2, runcount)
} }
// concurrencyProbe returns an executor recording the peak number of concurrent copies. Copies
// block until wantActive are in flight so the peak is exact without sleeping, and later copies
// find the gate already open so the last one still finishes with no partner left.
func concurrencyProbe(wantActive int32) (exec Executor, count, maxActive *atomic.Int32) { func concurrencyProbe(wantActive int32) (exec Executor, count, maxActive *atomic.Int32) {
var counted, active, peak atomic.Int32 var counted, active, peak atomic.Int32
var once sync.Once var once sync.Once
reached := make(chan struct{}) reached := make(chan struct{})
return func(ctx context.Context) error { return func(_ctx context.Context) error {
counted.Add(1) counted.Add(1)
running := active.Add(1) running := active.Add(1)
for { for {
@@ -72,18 +53,14 @@ func concurrencyProbe(wantActive int32) (exec Executor, count, maxActive *atomic
} }
func TestNewParallelExecutor(t *testing.T) { func TestNewParallelExecutor(t *testing.T) {
ctx := t.Context() for name, parallel := range map[string]int{"parallel": 2, "below minimum": 0} {
t.Run(name, func(t *testing.T) {
exec, count, maxActive := concurrencyProbe(2) exec, count, maxActive := concurrencyProbe(int32(max(1, parallel)))
require.NoError(t, NewParallelExecutor(2, exec, exec, exec)(ctx)) require.NoError(t, NewParallelExecutor(parallel, exec, exec, exec)(t.Context()))
assert.Equal(t, int32(3), count.Load(), "should run all 3 executors") assert.Equal(t, int32(3), count.Load())
assert.Equal(t, int32(2), maxActive.Load(), "should run at most 2 executors in parallel") assert.Equal(t, int32(max(1, parallel)), maxActive.Load())
})
// parallelism below 1 falls back to a single worker }
exec, count, maxActive = concurrencyProbe(1)
require.NoError(t, NewParallelExecutor(0, exec, exec, exec)(ctx))
assert.Equal(t, int32(3), count.Load(), "should run all 3 executors")
assert.Equal(t, int32(1), maxActive.Load(), "should run at most 1 executor in parallel")
} }
func TestNewParallelExecutorEmpty(t *testing.T) { func TestNewParallelExecutorEmpty(t *testing.T) {
@@ -99,66 +76,49 @@ func TestNewParallelExecutorEmpty(t *testing.T) {
assert.ErrorIs(err, context.Canceled) assert.ErrorIs(err, context.Canceled)
} }
func TestNewParallelExecutorFailed(t *testing.T) { func TestNewParallelExecutorRunsAllTasksAfterFailureOrCancellation(t *testing.T) {
assert := assert.New(t) for name, tc := range map[string]struct {
canceled bool
ctx, cancel := context.WithCancel(t.Context()) parallel, tasks int
cancel() }{
"failure": {false, 2, 5},
count := 0 "canceled": {true, 2, 5},
errorWorkflow := NewPipelineExecutor(func(ctx context.Context) error { "canceled single task": {true, 1, 1},
count++ } {
return errors.New("fake error") t.Run(name, func(t *testing.T) {
}) ctx, cancel := context.WithCancel(t.Context())
err := NewParallelExecutor(1, errorWorkflow)(ctx) defer cancel()
assert.Equal(1, count) if tc.canceled {
assert.ErrorIs(context.Canceled, err) cancel()
} }
var calls atomic.Int32
func TestNewParallelExecutorCanceled(t *testing.T) { executors := make([]Executor, tc.tasks)
assert := assert.New(t) for i := range executors {
executors[i] = func(_ctx context.Context) error {
ctx, cancel := context.WithCancel(t.Context()) calls.Add(1)
cancel() if i == tc.tasks/2 {
return errors.New("fake error")
errExpected := errors.New("fake error") }
return nil
var count atomic.Int32 }
successWorkflow := NewPipelineExecutor(func(ctx context.Context) error {
count.Add(1)
return nil
})
errorWorkflow := NewPipelineExecutor(func(ctx context.Context) error {
count.Add(1)
return errExpected
})
err := NewParallelExecutor(3, errorWorkflow, successWorkflow, successWorkflow)(ctx)
assert.Equal(int32(3), count.Load())
assert.Error(errExpected, err) //nolint:testifylint // pre-existing issue from nektos/act
}
func TestNewParallelExecutorRunsRemainingAfterFailure(t *testing.T) {
var successCount atomic.Int32
executors := make([]Executor, 5)
for i := range executors {
executors[i] = func(ctx context.Context) error {
if i == 2 {
return errors.New("fake error")
} }
successCount.Add(1)
return nil
}
}
require.Error(t, NewParallelExecutor(2, executors...)(t.Context())) err := NewParallelExecutor(tc.parallel, executors...)(ctx)
assert.Equal(t, int32(4), successCount.Load(), "a failing executor must not stop the others") if tc.canceled {
require.ErrorIs(t, err, context.Canceled)
} else {
require.EqualError(t, err, "fake error")
}
assert.Equal(t, int32(tc.tasks), calls.Load())
})
}
} }
func TestExecutorConditionalsAndFinally(t *testing.T) { func TestExecutorConditionalsAndFinally(t *testing.T) {
ctx := t.Context() ctx := t.Context()
var calls []string var calls []string
record := func(name string) Executor { record := func(name string) Executor {
return func(ctx context.Context) error { return func(_ctx context.Context) error {
calls = append(calls, name) calls = append(calls, name)
return nil return nil
} }
@@ -170,10 +130,7 @@ func TestExecutorConditionalsAndFinally(t *testing.T) {
require.NoError(t, record("if-bool").IfBool(true)(ctx)) require.NoError(t, record("if-bool").IfBool(true)(ctx))
require.NoError(t, record("main").Finally(record("finally"))(ctx)) require.NoError(t, record("main").Finally(record("finally"))(ctx))
want := []string{"if-true", "if-not", "if-bool", "main", "finally"} assert.Equal(t, []string{"if-true", "if-not", "if-bool", "main", "finally"}, calls)
if !reflect.DeepEqual(calls, want) {
t.Fatalf("calls = %v, want %v", calls, want)
}
} }
func TestExecutorFinallyReturnsFinallyErrorWithOriginal(t *testing.T) { func TestExecutorFinallyReturnsFinallyErrorWithOriginal(t *testing.T) {
@@ -181,8 +138,5 @@ func TestExecutorFinallyReturnsFinallyErrorWithOriginal(t *testing.T) {
finalErr := errors.New("cleanup failed") finalErr := errors.New("cleanup failed")
err := NewErrorExecutor(mainErr).Finally(NewErrorExecutor(finalErr))(t.Context()) err := NewErrorExecutor(mainErr).Finally(NewErrorExecutor(finalErr))(t.Context())
require.Error(t, err) require.EqualError(t, err, "error occurred running finally: cleanup failed (original error: main failed)")
if !strings.Contains(err.Error(), "cleanup failed") || !strings.Contains(err.Error(), "main failed") {
t.Fatalf("finally error = %q, want both cleanup and original error", err)
}
} }
+6 -6
View File
@@ -79,25 +79,25 @@ func (e *Error) Unwrap() error { return e.err }
func (e *Error) Commit() string { return e.commit } func (e *Error) Commit() string { return e.commit }
// FindGitRevision get the current git revision // FindGitRevision get the current git revision
func FindGitRevision(ctx context.Context, file string) (shortSHA, sha string, err error) { func FindGitRevision(ctx context.Context, file string) (string, error) {
logger := common.Logger(ctx) logger := common.Logger(ctx)
sha, err = gitOutput(ctx, file, "rev-parse", "--verify", "HEAD^{commit}") sha, err := gitOutput(ctx, file, "rev-parse", "--verify", "HEAD^{commit}")
if err != nil { if err != nil {
logger.WithError(err).Error("path", file, "not located inside a git repository") logger.WithError(err).Error("path", file, "not located inside a git repository")
return "", "", err return "", err
} }
if len(sha) < 7 { if len(sha) < 7 {
return "", "", errors.New("HEAD could not be resolved") return "", errors.New("HEAD could not be resolved")
} }
logger.Debugf("Found revision: %s", sha) logger.Debugf("Found revision: %s", sha)
return sha[:7], sha, nil return sha, nil
} }
// FindGitRef get the current git ref // FindGitRef get the current git ref
func FindGitRef(ctx context.Context, file string) (string, error) { func FindGitRef(ctx context.Context, file string) (string, error) {
logger := common.Logger(ctx) logger := common.Logger(ctx)
logger.Debugf("Loading revision from git directory") logger.Debugf("Loading revision from git directory")
_, revision, err := FindGitRevision(ctx, file) revision, err := FindGitRevision(ctx, file)
if err != nil { if err != nil {
return "", err return "", err
} }
+67 -150
View File
@@ -67,42 +67,20 @@ func TestErrorWrapsCommitAndCause(t *testing.T) {
require.Equal(t, "abc123", err.Commit()) require.Equal(t, "abc123", err.Commit())
} }
func cleanGitHooks(dir string) error {
hooksDir := filepath.Join(dir, ".git", "hooks")
files, err := os.ReadDir(hooksDir)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
for _, f := range files {
if f.IsDir() {
continue
}
relName := filepath.Join(hooksDir, f.Name())
if err := os.Remove(relName); err != nil {
return err
}
}
return nil
}
func TestFindGitMetadataOfSHA256RepositoryUsesOrigin(t *testing.T) { func TestFindGitMetadataOfSHA256RepositoryUsesOrigin(t *testing.T) {
t.Parallel() t.Parallel()
basedir := t.TempDir() basedir := t.TempDir()
const remoteURL = "https://github.com/owner/repo.git" const remoteURL = "https://github.com/owner/repo.git"
require.NoError(t, gitCmd("init", "--object-format=sha256", "--initial-branch=main", basedir)) require.NoError(t, gitCmd("init", "--object-format=sha256", "--initial-branch=main", basedir))
require.NoError(t, cleanGitHooks(basedir)) require.NoError(t, os.RemoveAll(filepath.Join(basedir, ".git", "hooks")))
require.NoError(t, gitCmd("-C", basedir, "commit", "--allow-empty", "-m", "init")) require.NoError(t, gitCmd("-C", basedir, "commit", "--allow-empty", "-m", "init"))
require.NoError(t, gitCmd("-C", basedir, "remote", "add", "origin", remoteURL)) require.NoError(t, gitCmd("-C", basedir, "remote", "add", "origin", remoteURL))
require.NoError(t, gitCmd("-C", basedir, "config", "--add", "remote.origin.url", "https://github.com/other/repo.git")) require.NoError(t, gitCmd("-C", basedir, "config", "--add", "remote.origin.url", "https://github.com/other/repo.git"))
shortSHA, sha, err := FindGitRevision(t.Context(), basedir) sha, err := FindGitRevision(t.Context(), basedir)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, gitRevParse(t, basedir, "HEAD"), sha) require.Equal(t, gitRevParse(t, basedir, "HEAD"), sha)
require.Len(t, sha, 64) require.Len(t, sha, 64)
require.Equal(t, sha[:7], shortSHA)
ref, err := FindGitRef(t.Context(), basedir) ref, err := FindGitRef(t.Context(), basedir)
require.NoError(t, err) require.NoError(t, err)
@@ -122,89 +100,66 @@ func TestGitFindRef(t *testing.T) {
basedir := t.TempDir() basedir := t.TempDir()
for name, tt := range map[string]struct { for name, tt := range map[string]struct {
Prepare func(t *testing.T, dir string) commands [][]string
Assert func(t *testing.T, ref string, err error) ref string
}{ }{
"new_repo": { "new_repo": {},
Prepare: func(t *testing.T, dir string) {},
Assert: func(t *testing.T, ref string, err error) {
require.Error(t, err)
},
},
"new_repo_with_commit": { "new_repo_with_commit": {
Prepare: func(t *testing.T, dir string) { commands: [][]string{{"commit", "--allow-empty", "-m", "msg"}},
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "msg")) ref: "refs/heads/master",
},
Assert: func(t *testing.T, ref string, err error) {
require.NoError(t, err)
require.Equal(t, "refs/heads/master", ref)
},
}, },
"current_head_is_tag": { "current_head_is_tag": {
Prepare: func(t *testing.T, dir string) { commands: [][]string{
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "commit msg")) {"commit", "--allow-empty", "-m", "commit msg"},
require.NoError(t, gitCmd("-C", dir, "tag", "v1.2.3")) {"tag", "v1.2.3"},
require.NoError(t, gitCmd("-C", dir, "checkout", "v1.2.3")) {"checkout", "v1.2.3"},
},
Assert: func(t *testing.T, ref string, err error) {
require.NoError(t, err)
require.Equal(t, "refs/tags/v1.2.3", ref)
}, },
ref: "refs/tags/v1.2.3",
}, },
"current_head_is_same_as_tag": { "current_head_is_same_as_tag": {
Prepare: func(t *testing.T, dir string) { commands: [][]string{
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "1.4.2 release")) {"commit", "--allow-empty", "-m", "1.4.2 release"},
require.NoError(t, gitCmd("-C", dir, "tag", "v1.4.2")) {"tag", "v1.4.2"},
},
Assert: func(t *testing.T, ref string, err error) {
require.NoError(t, err)
require.Equal(t, "refs/tags/v1.4.2", ref)
}, },
ref: "refs/tags/v1.4.2",
}, },
"current_head_is_not_tag": { "current_head_is_not_tag": {
Prepare: func(t *testing.T, dir string) { commands: [][]string{
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "msg")) {"commit", "--allow-empty", "-m", "msg"},
require.NoError(t, gitCmd("-C", dir, "tag", "v1.4.2")) {"tag", "v1.4.2"},
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "msg2")) {"commit", "--allow-empty", "-m", "msg2"},
},
Assert: func(t *testing.T, ref string, err error) {
require.NoError(t, err)
require.Equal(t, "refs/heads/master", ref)
}, },
ref: "refs/heads/master",
}, },
"current_head_is_another_branch": { "current_head_is_another_branch": {
Prepare: func(t *testing.T, dir string) { commands: [][]string{
require.NoError(t, gitCmd("-C", dir, "checkout", "-b", "mybranch")) {"checkout", "-b", "mybranch"},
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "msg")) {"commit", "--allow-empty", "-m", "msg"},
},
Assert: func(t *testing.T, ref string, err error) {
require.NoError(t, err)
require.Equal(t, "refs/heads/mybranch", ref)
}, },
ref: "refs/heads/mybranch",
}, },
} { } {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
dir := filepath.Join(basedir, name) dir := filepath.Join(basedir, name)
require.NoError(t, os.MkdirAll(dir, 0o755)) require.NoError(t, os.MkdirAll(dir, 0o755))
require.NoError(t, gitCmd("-C", dir, "init", "--initial-branch=master")) require.NoError(t, gitCmd("-C", dir, "init", "--initial-branch=master"))
require.NoError(t, cleanGitHooks(dir)) require.NoError(t, os.RemoveAll(filepath.Join(dir, ".git", "hooks")))
tt.Prepare(t, dir) for _, command := range tt.commands {
require.NoError(t, gitCmd(append([]string{"-C", dir}, command...)...))
}
ref, err := FindGitRef(t.Context(), dir) ref, err := FindGitRef(t.Context(), dir)
tt.Assert(t, ref, err) if tt.ref == "" {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, tt.ref, ref)
}
}) })
} }
} }
func TestGitCloneExecutor(t *testing.T) { func TestGitCloneExecutor(t *testing.T) {
// Build a local bare "remote" so this runs offline and fast. The cases below mirror remoteDir, workDir := newGitRemote(t)
// the tag/branch/sha/short-sha ref paths the executor handles, formerly exercised by
// cloning actions/checkout and anchore/scan-action over the network.
remoteDir := t.TempDir()
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
workDir := t.TempDir()
require.NoError(t, gitCmd("clone", remoteDir, workDir))
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "main"))
require.NoError(t, os.WriteFile(filepath.Join(workDir, "action.sh"), []byte("#!/bin/sh\necho hi\n"), 0o755)) require.NoError(t, os.WriteFile(filepath.Join(workDir, "action.sh"), []byte("#!/bin/sh\necho hi\n"), 0o755))
require.NoError(t, gitCmd("-C", workDir, "add", "action.sh")) require.NoError(t, gitCmd("-C", workDir, "add", "action.sh"))
require.NoError(t, gitCmd("-C", workDir, "commit", "-m", "initial")) require.NoError(t, gitCmd("-C", workDir, "commit", "-m", "initial"))
@@ -212,14 +167,11 @@ func TestGitCloneExecutor(t *testing.T) {
require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main")) require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main"))
require.NoError(t, gitCmd("-C", workDir, "push", "origin", "v2")) require.NoError(t, gitCmd("-C", workDir, "push", "origin", "v2"))
// A branch with a dash in the name (mirrors the historical scan-action@act-fails case).
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "act-fails")) require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "act-fails"))
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "branch-commit")) require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "branch-commit"))
require.NoError(t, gitCmd("-C", workDir, "push", "origin", "act-fails")) require.NoError(t, gitCmd("-C", workDir, "push", "origin", "act-fails"))
out, err := exec.Command("git", "-C", workDir, "rev-parse", "main").Output() fullSha := gitRevParse(t, workDir, "main")
require.NoError(t, err)
fullSha := strings.TrimSpace(string(out))
require.NoError(t, gitCmd("-C", workDir, "push", "origin", "main:refs/heads/"+fullSha[:4])) require.NoError(t, gitCmd("-C", workDir, "push", "origin", "main:refs/heads/"+fullSha[:4]))
@@ -265,12 +217,12 @@ func TestGitCloneExecutor(t *testing.T) {
err := clone(t.Context()) err := clone(t.Context())
if tt.Err != nil { if tt.Err != nil {
assert.Error(t, err) //nolint:testifylint // pre-existing issue from nektos/act require.Error(t, err)
assert.Equal(t, tt.Err, err) assert.Equal(t, tt.Err, err)
assert.Equal(t, tt.Err, clone(t.Context()), "a retry on the cache keeps the error") assert.Equal(t, tt.Err, clone(t.Context()), "a retry on the cache keeps the error")
return return
} }
assert.Empty(t, err) //nolint:testifylint // pre-existing issue from nektos/act require.NoError(t, err)
marker := filepath.Join(dir, "marker") marker := filepath.Join(dir, "marker")
require.NoError(t, os.WriteFile(marker, nil, 0o644)) require.NoError(t, os.WriteFile(marker, nil, 0o644))
require.NoError(t, clone(t.Context())) require.NoError(t, clone(t.Context()))
@@ -285,13 +237,7 @@ func TestGitCloneExecutor(t *testing.T) {
func TestGitCloneExecutorReclonesWhenOriginURLChanges(t *testing.T) { func TestGitCloneExecutorReclonesWhenOriginURLChanges(t *testing.T) {
t.Parallel() t.Parallel()
createRemote := func(message string) string { createRemote := func(message string) string {
remoteDir := t.TempDir() remoteDir, workDir := newGitRemote(t, message)
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
workDir := t.TempDir()
require.NoError(t, gitCmd("clone", remoteDir, workDir))
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "main"))
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", message))
require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main")) require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main"))
return remoteDir return remoteDir
@@ -341,13 +287,7 @@ func TestGitCloneExecutorReclonesWhenOriginURLChanges(t *testing.T) {
func TestGitCloneExecutorFollowsRemoteRefChanges(t *testing.T) { func TestGitCloneExecutorFollowsRemoteRefChanges(t *testing.T) {
t.Parallel() t.Parallel()
remoteDir := t.TempDir() remoteDir, workDir := newGitRemote(t, "initial", "feature-1")
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
workDir := t.TempDir()
require.NoError(t, gitCmd("clone", remoteDir, workDir))
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "main"))
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "initial"))
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "feature-1"))
require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main")) require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main"))
dir := t.TempDir() dir := t.TempDir()
@@ -380,16 +320,9 @@ func TestGitCloneExecutorFollowsRemoteRefChanges(t *testing.T) {
func TestGitCloneExecutorOfflineMode(t *testing.T) { func TestGitCloneExecutorOfflineMode(t *testing.T) {
t.Parallel() t.Parallel()
// Build a local "remote" with a single commit on main. remoteDir, workDir := newGitRemote(t, "initial")
remoteDir := t.TempDir()
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
workDir := t.TempDir()
require.NoError(t, gitCmd("clone", remoteDir, workDir))
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "main"))
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "initial"))
require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main")) require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main"))
// Prime the cache with an online clone of main.
cacheDir := t.TempDir() cacheDir := t.TempDir()
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{ require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
URL: remoteDir, URL: remoteDir,
@@ -398,8 +331,6 @@ func TestGitCloneExecutorOfflineMode(t *testing.T) {
})(t.Context())) })(t.Context()))
t.Run("cached branch resolves without fetching", func(t *testing.T) { t.Run("cached branch resolves without fetching", func(t *testing.T) {
// Offline reuse of a cached branch must succeed even though ResolveRevision(input.Ref)
// finds no local refs/heads/<ref>.
err := NewGitCloneExecutor(NewGitCloneExecutorInput{ err := NewGitCloneExecutor(NewGitCloneExecutorInput{
URL: remoteDir, URL: remoteDir,
Ref: "main", Ref: "main",
@@ -414,7 +345,6 @@ func TestGitCloneExecutorOfflineMode(t *testing.T) {
}) })
t.Run("unresolvable cached ref returns error", func(t *testing.T) { t.Run("unresolvable cached ref returns error", func(t *testing.T) {
// The ref was never cached; offline mode cannot resolve it and must return an error.
err := NewGitCloneExecutor(NewGitCloneExecutorInput{ err := NewGitCloneExecutor(NewGitCloneExecutorInput{
URL: remoteDir, URL: remoteDir,
Ref: "never-fetched", Ref: "never-fetched",
@@ -427,15 +357,7 @@ func TestGitCloneExecutorOfflineMode(t *testing.T) {
func TestGitCloneExecutorShallow(t *testing.T) { func TestGitCloneExecutorShallow(t *testing.T) {
t.Parallel() t.Parallel()
// Build a local "remote" with several commits on main plus a tag, so a full clone would pull noticeably more history than a shallow one. remoteDir, workDir := newGitRemote(t, "c1", "c2", "c3")
remoteDir := t.TempDir()
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
workDir := t.TempDir()
require.NoError(t, gitCmd("clone", remoteDir, workDir))
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "main"))
for _, m := range []string{"c1", "c2", "c3"} {
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", m))
}
require.NoError(t, gitCmd("-C", workDir, "tag", "v1")) require.NoError(t, gitCmd("-C", workDir, "tag", "v1"))
unadvertisedSHA := gitRevParse(t, workDir, "HEAD~1") unadvertisedSHA := gitRevParse(t, workDir, "HEAD~1")
require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main")) require.NoError(t, gitCmd("-C", workDir, "push", "-u", "origin", "main"))
@@ -443,25 +365,17 @@ func TestGitCloneExecutorShallow(t *testing.T) {
shallowMarker := func(dir string) string { return filepath.Join(dir, ".git", "shallow") } shallowMarker := func(dir string) string { return filepath.Join(dir, ".git", "shallow") }
t.Run("branch is cloned shallowly", func(t *testing.T) { for name, ref := range map[string]string{"branch is cloned shallowly": "main", "tag is cloned shallowly": "v1"} {
dir := t.TempDir() t.Run(name, func(t *testing.T) {
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{ dir := t.TempDir()
URL: remoteDir, Ref: "main", Dir: dir, Depth: 1, require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
})(t.Context())) URL: remoteDir, Ref: ref, Dir: dir, Depth: 1,
assert.FileExists(t, shallowMarker(dir), "clone should be shallow") })(t.Context()))
assert.Equal(t, 1, gitRevCount(t, dir), "only the tip commit should be present") assert.FileExists(t, shallowMarker(dir), "clone should be shallow")
assert.Equal(t, "c3", gitHeadSubject(t, dir)) assert.Equal(t, 1, gitRevCount(t, dir), "only the tip commit should be present")
}) assert.Equal(t, "c3", gitHeadSubject(t, dir))
})
t.Run("tag is cloned shallowly", func(t *testing.T) { }
dir := t.TempDir()
require.NoError(t, NewGitCloneExecutor(NewGitCloneExecutorInput{
URL: remoteDir, Ref: "v1", Dir: dir, Depth: 1,
})(t.Context()))
assert.FileExists(t, shallowMarker(dir), "clone should be shallow")
assert.Equal(t, 1, gitRevCount(t, dir))
assert.Equal(t, "c3", gitHeadSubject(t, dir))
})
unadvertisedRemote := func(t *testing.T, allowUnadvertised bool) string { unadvertisedRemote := func(t *testing.T, allowUnadvertised bool) string {
remote := filepath.Join(t.TempDir(), "remote.git") remote := filepath.Join(t.TempDir(), "remote.git")
@@ -541,12 +455,7 @@ func TestGitCloneExecutorShallow(t *testing.T) {
func TestGitCloneExecutorPinnedHashIgnoresShadowingRef(t *testing.T) { func TestGitCloneExecutorPinnedHashIgnoresShadowingRef(t *testing.T) {
t.Parallel() t.Parallel()
remoteDir := t.TempDir() remoteDir, workDir := newGitRemote(t, "pinned")
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
workDir := t.TempDir()
require.NoError(t, gitCmd("clone", remoteDir, workDir))
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "main"))
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "pinned"))
pinned := gitRevParse(t, workDir, "HEAD") pinned := gitRevParse(t, workDir, "HEAD")
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "decoy")) require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", "decoy"))
require.NoError(t, gitCmd("-C", workDir, "tag", pinned)) require.NoError(t, gitCmd("-C", workDir, "tag", pinned))
@@ -564,6 +473,18 @@ func TestGitCloneExecutorPinnedHashIgnoresShadowingRef(t *testing.T) {
} }
} }
func newGitRemote(t *testing.T, messages ...string) (string, string) {
t.Helper()
remoteDir, workDir := t.TempDir(), t.TempDir()
require.NoError(t, gitCmd("init", "--bare", "--initial-branch=main", remoteDir))
require.NoError(t, gitCmd("clone", remoteDir, workDir))
require.NoError(t, gitCmd("-C", workDir, "checkout", "-b", "main"))
for _, message := range messages {
require.NoError(t, gitCmd("-C", workDir, "commit", "--allow-empty", "-m", message))
}
return remoteDir, workDir
}
func gitRevParse(t *testing.T, dir, rev string) string { func gitRevParse(t *testing.T, dir, rev string) string {
t.Helper() t.Helper()
out, err := exec.Command("git", "-C", dir, "rev-parse", rev).Output() out, err := exec.Command("git", "-C", dir, "rev-parse", rev).Output()
@@ -591,9 +512,6 @@ func gitCmd(args ...string) error {
cmd := exec.Command("git", args...) cmd := exec.Command("git", args...)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
// Inject a deterministic identity and ignore the host's global/system config so commits
// succeed regardless of the host having no user.name/user.email (e.g. CI, GITHUB_ACTIONS
// unset) or a global commit.gpgsign, and without mutating the developer's ~/.gitconfig.
cmd.Env = append(os.Environ(), cmd.Env = append(os.Environ(),
"GIT_AUTHOR_NAME=Unit Test", "GIT_AUTHOR_NAME=Unit Test",
"[email protected]", "[email protected]",
@@ -695,7 +613,6 @@ func TestAcquireCloneLock(t *testing.T) {
}) })
} }
// An unresponsive remote must not pin a job: the refresh has to be interruptible.
func TestNewGitCloneExecutorFetchSendsTokenAndHonoursContext(t *testing.T) { func TestNewGitCloneExecutorFetchSendsTokenAndHonoursContext(t *testing.T) {
t.Parallel() t.Parallel()
block := make(chan struct{}) block := make(chan struct{})
+3 -9
View File
@@ -14,13 +14,8 @@ const jobErrorContextKeyVal = jobErrorContextKey("job.error")
// JobError returns the job error for current context if any // JobError returns the job error for current context if any
func JobError(ctx context.Context) error { func JobError(ctx context.Context) error {
val := ctx.Value(jobErrorContextKeyVal) container, _ := ctx.Value(jobErrorContextKeyVal).(map[string]error)
if val != nil { return container["error"]
if container, ok := val.(map[string]error); ok {
return container["error"]
}
}
return nil
} }
func SetJobError(ctx context.Context, err error) { func SetJobError(ctx context.Context, err error) {
@@ -31,6 +26,5 @@ func SetJobError(ctx context.Context, err error) {
// WithJobErrorContainer adds a value to the context as a container for an error // WithJobErrorContainer adds a value to the context as a container for an error
func WithJobErrorContainer(ctx context.Context) context.Context { func WithJobErrorContainer(ctx context.Context) context.Context {
container := map[string]error{} return context.WithValue(ctx, jobErrorContextKeyVal, map[string]error{})
return context.WithValue(ctx, jobErrorContextKeyVal, container)
} }
+9 -21
View File
@@ -26,9 +26,7 @@ type lineWriter struct {
// NewLineWriter creates a new instance of a line writer // NewLineWriter creates a new instance of a line writer
func NewLineWriter(handlers ...LineHandler) io.Writer { func NewLineWriter(handlers ...LineHandler) io.Writer {
w := new(lineWriter) return &lineWriter{handlers: handlers}
w.handlers = handlers
return w
} }
// FlushWriter flushes w if it implements Flusher. It is a no-op otherwise, so // FlushWriter flushes w if it implements Flusher. It is a no-op otherwise, so
@@ -39,24 +37,15 @@ func FlushWriter(w io.Writer) {
} }
} }
func (lw *lineWriter) Write(p []byte) (n int, err error) { func (lw *lineWriter) Write(p []byte) (int, error) {
pBuf := bytes.NewBuffer(p) for line := range bytes.SplitAfterSeq(p, []byte{'\n'}) {
written := 0 _, _ = lw.buffer.Write(line)
for { if len(line) > 0 && line[len(line)-1] == '\n' {
line, err := pBuf.ReadString('\n') lw.handleLine(lw.buffer.String())
w, _ := lw.buffer.WriteString(line) lw.buffer.Reset()
written += w
if err != nil {
if err == io.EOF {
break
}
return written, err
} }
lw.handleLine(lw.buffer.String())
lw.buffer.Reset()
} }
return len(p), nil
return written, nil
} }
// Flush emits any buffered, not-yet-newline-terminated content as a final line. // Flush emits any buffered, not-yet-newline-terminated content as a final line.
@@ -72,8 +61,7 @@ func (lw *lineWriter) Flush() {
func (lw *lineWriter) handleLine(line string) { func (lw *lineWriter) handleLine(line string) {
for _, h := range lw.handlers { for _, h := range lw.handlers {
ok := h(line) if !h(line) {
if !ok {
break break
} }
} }
+12 -34
View File
@@ -9,24 +9,24 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestLineWriter(t *testing.T) { func TestLineWriterBuffersAndFlushesLines(t *testing.T) {
lines := make([]string, 0) var lines []string
lineHandler := func(s string) bool { lineWriter := NewLineWriter(func(s string) bool {
lines = append(lines, s) lines = append(lines, s)
return true return true
} })
lineWriter := NewLineWriter(lineHandler)
assert := assert.New(t) assert := assert.New(t)
write := func(s string) { write := func(s string) {
n, err := lineWriter.Write([]byte(s)) n, err := lineWriter.Write([]byte(s))
assert.NoError(err) //nolint:testifylint // pre-existing issue from nektos/act require.NoError(t, err)
assert.Equal(len(s), n, s) assert.Equal(len(s), n, s)
} }
write("")
write("hello") write("hello")
write(" ") write(" ")
write("world!!\nextra") write("world!!\nextra")
@@ -34,39 +34,17 @@ func TestLineWriter(t *testing.T) {
write(" line\n") write(" line\n")
write("no newline here...") write("no newline here...")
assert.Len(lines, 4) expected := []string{"hello world!!\n", "extra line\n", " and another\n", "last line\n"}
assert.Equal("hello world!!\n", lines[0]) assert.Equal(expected, lines)
assert.Equal("extra line\n", lines[1])
assert.Equal(" and another\n", lines[2])
assert.Equal("last line\n", lines[3])
}
func TestLineWriterFlush(t *testing.T) {
lines := make([]string, 0)
lineHandler := func(s string) bool {
lines = append(lines, s)
return true
}
lineWriter := NewLineWriter(lineHandler)
assert := assert.New(t)
_, err := lineWriter.Write([]byte("complete line\npartial line without newline"))
assert.NoError(err) //nolint:testifylint // pre-existing pattern from nektos/act
// Only the newline-terminated line is emitted before flushing.
assert.Equal([]string{"complete line\n"}, lines)
// Flushing emits the buffered, not-yet-terminated trailing line.
FlushWriter(lineWriter) FlushWriter(lineWriter)
assert.Equal([]string{"complete line\n", "partial line without newline"}, lines) expected = append(expected, "no newline here...")
assert.Equal(expected, lines)
// Flushing again is a no-op: nothing is buffered.
FlushWriter(lineWriter) FlushWriter(lineWriter)
assert.Len(lines, 2) assert.Equal(expected, lines)
} }
func TestFlushWriterIgnoresNonFlusher(t *testing.T) { func TestFlushWriterIgnoresNonFlusher(t *testing.T) {
// FlushWriter must be a safe no-op for writers that do not buffer lines.
assert.NotPanics(t, func() { FlushWriter(io.Discard) }) assert.NotPanics(t, func() { FlushWriter(io.Discard) })
} }
+2 -4
View File
@@ -34,10 +34,8 @@ const loggerHandlerKeyVal = loggerHandlerKey("logging.Handler")
// LoggerHandler returns the extra handler the job logger sends its records to, or nil. // LoggerHandler returns the extra handler the job logger sends its records to, or nil.
// It affects the job logger, not the global logger. // It affects the job logger, not the global logger.
func LoggerHandler(ctx context.Context) logging.Handler { func LoggerHandler(ctx context.Context) logging.Handler {
if h, ok := ctx.Value(loggerHandlerKeyVal).(logging.Handler); ok { h, _ := ctx.Value(loggerHandlerKeyVal).(logging.Handler)
return h return h
}
return nil
} }
// WithLoggerHandler adds the extra handler for the job logger to the context // WithLoggerHandler adds the extra handler for the job logger to the context
+2 -5
View File
@@ -9,6 +9,7 @@ import (
"fmt" "fmt"
"io" "io"
"log/slog" "log/slog"
"maps"
"reflect" "reflect"
"runtime" "runtime"
"slices" "slices"
@@ -94,11 +95,7 @@ func (h *consoleHandler) Handle(_ context.Context, r slog.Record) error {
} }
prefixFieldClashes(data, caller != nil) prefixFieldClashes(data, caller != nil)
keys := make([]string, 0, len(data)) keys := slices.Sorted(maps.Keys(data))
for k := range data {
keys = append(keys, k)
}
slices.Sort(keys)
var b bytes.Buffer var b bytes.Buffer
if terminal && !opts.DisableColors { if terminal && !opts.DisableColors {
-2
View File
@@ -73,12 +73,10 @@ func Enabled(level Level) bool { return Default().Enabled(level) }
func With(args ...any) *Logger { return Default().With(args...) } func With(args ...any) *Logger { return Default().With(args...) }
func WithError(err error) *Logger { return Default().WithError(err) } func WithError(err error) *Logger { return Default().WithError(err) }
func Tracef(format string, args ...any) { Default().logf(LevelTrace, format, args) }
func Debugf(format string, args ...any) { Default().logf(LevelDebug, format, args) } func Debugf(format string, args ...any) { Default().logf(LevelDebug, format, args) }
func Infof(format string, args ...any) { Default().logf(LevelInfo, format, args) } func Infof(format string, args ...any) { Default().logf(LevelInfo, format, args) }
func Warnf(format string, args ...any) { Default().logf(LevelWarn, format, args) } func Warnf(format string, args ...any) { Default().logf(LevelWarn, format, args) }
func Errorf(format string, args ...any) { Default().logf(LevelError, format, args) } func Errorf(format string, args ...any) { Default().logf(LevelError, format, args) }
func Trace(args ...any) { Default().log(LevelTrace, args) }
func Debug(args ...any) { Default().log(LevelDebug, args) } func Debug(args ...any) { Default().log(LevelDebug, args) }
func Info(args ...any) { Default().log(LevelInfo, args) } func Info(args ...any) { Default().log(LevelInfo, args) }
func Warn(args ...any) { Default().log(LevelWarn, args) } func Warn(args ...any) { Default().log(LevelWarn, args) }
-1
View File
@@ -43,7 +43,6 @@ func (l *Logger) Enabled(level Level) bool {
return l.h.Enabled(l.ctx, level) return l.h.Enabled(l.ctx, level)
} }
func (l *Logger) Tracef(format string, args ...any) { l.logf(LevelTrace, format, args) }
func (l *Logger) Debugf(format string, args ...any) { l.logf(LevelDebug, format, args) } func (l *Logger) Debugf(format string, args ...any) { l.logf(LevelDebug, format, args) }
func (l *Logger) Infof(format string, args ...any) { l.logf(LevelInfo, format, args) } func (l *Logger) Infof(format string, args ...any) { l.logf(LevelInfo, format, args) }
func (l *Logger) Warnf(format string, args ...any) { l.logf(LevelWarn, format, args) } func (l *Logger) Warnf(format string, args ...any) { l.logf(LevelWarn, format, args) }
+3 -7
View File
@@ -38,6 +38,7 @@ func (c countingStringer) String() string { *c.n++; return "x" }
func TestLogger(t *testing.T) { func TestLogger(t *testing.T) {
h := logtest.NewWithLevel(logging.LevelTrace) h := logtest.NewWithLevel(logging.LevelTrace)
assert.Nil(t, h.LastRecord())
l := logging.New(h) l := logging.New(h)
l.With("job", "j1").WithError(errors.New("boom")).Warnf("step %d", 2) l.With("job", "j1").WithError(errors.New("boom")).Warnf("step %d", 2)
@@ -53,12 +54,9 @@ func TestLogger(t *testing.T) {
assert.Equal(t, "j1", job) assert.Equal(t, "j1", job)
errv, _ := recs[0].Attr("error") errv, _ := recs[0].Attr("error")
require.EqualError(t, errv.(error), "boom") require.EqualError(t, errv.(error), "boom")
assert.Equal(t, "a1 2b", recs[1].Message) // fmt.Sprint spacing assert.Equal(t, "a1 2b", recs[1].Message)
bad, _ := recs[2].Attr("!BADKEY") bad, _ := recs[2].Attr("!BADKEY")
assert.Equal(t, 7, bad) assert.Equal(t, 7, bad)
h.Reset()
assert.Nil(t, h.LastRecord())
} }
func TestLoggerSkipsFormattingWhenDisabled(t *testing.T) { func TestLoggerSkipsFormattingWhenDisabled(t *testing.T) {
@@ -117,10 +115,8 @@ func TestDefaultLevel(t *testing.T) {
func TestSlogHandler(t *testing.T) { func TestSlogHandler(t *testing.T) {
var buf strings.Builder var buf strings.Builder
slogger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug})) l := logging.New(logging.NewSlogHandler(slog.NewTextHandler(&buf, &slog.HandlerOptions{Level: slog.LevelDebug}))).With("job", "j1")
l := logging.New(logging.NewSlogHandler(slogger.Handler())).With("job", "j1")
l.Tracef("dropped")
assert.False(t, l.Enabled(logging.LevelTrace)) assert.False(t, l.Enabled(logging.LevelTrace))
l.WithError(errors.New("boom")).Errorf("fail %s", "x") l.WithError(errors.New("boom")).Errorf("fail %s", "x")
-6
View File
@@ -72,12 +72,6 @@ func (h *Handler) LastRecord() *logging.Record {
return &r return &r
} }
func (h *Handler) Reset() {
h.store.mu.Lock()
defer h.store.mu.Unlock()
h.store.records = nil
}
// SetDefault makes the process-wide Logger record into a new Handler until the test ends. // SetDefault makes the process-wide Logger record into a new Handler until the test ends.
func SetDefault(t testing.TB) *Handler { func SetDefault(t testing.TB) *Handler {
t.Helper() t.Helper()
+8 -12
View File
@@ -12,7 +12,7 @@ import (
type interfaceIP struct { type interfaceIP struct {
net.IP net.IP
net.Interface Name string
} }
// GetOutboundIP returns an outbound IP address of this machine. // GetOutboundIP returns an outbound IP address of this machine.
@@ -43,7 +43,7 @@ func GetOutboundIP() net.IP {
ip = v.IP ip = v.IP
} }
if ip.IsGlobalUnicast() { if ip.IsGlobalUnicast() {
ips = append(ips, interfaceIP{ip, i}) ips = append(ips, interfaceIP{ip, i.Name})
} }
} }
} }
@@ -59,29 +59,25 @@ func preferredIP(ips []interfaceIP) net.IP {
return nil return nil
} }
sort.Slice(ips, func(i, j int) bool { sort.Slice(ips, func(i, j int) bool {
ifi := ips[i].Interface first, second := ips[i], ips[j]
ifj := ips[j].Interface
// ethernet is preferred // ethernet is preferred
if vi, vj := strings.HasPrefix(ifi.Name, "e"), strings.HasPrefix(ifj.Name, "e"); vi != vj { if vi, vj := strings.HasPrefix(first.Name, "e"), strings.HasPrefix(second.Name, "e"); vi != vj {
return vi return vi
} }
ipi := ips[i].IP
ipj := ips[j].IP
// IPv4 is preferred // IPv4 is preferred
if vi, vj := ipi.To4() != nil, ipj.To4() != nil; vi != vj { if vi, vj := first.To4() != nil, second.To4() != nil; vi != vj {
return vi return vi
} }
// en0 is preferred to en1 // en0 is preferred to en1
if ifi.Name != ifj.Name { if first.Name != second.Name {
return ifi.Name < ifj.Name return first.Name < second.Name
} }
// fallback // fallback
return ipi.String() < ipj.String() return first.String() < second.String()
}) })
return ips[0].IP return ips[0].IP
} }
+2 -7
View File
@@ -46,9 +46,8 @@ func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor {
logger.Debugf("Building image from '%v'", input.ContextDir) logger.Debugf("Building image from '%v'", input.ContextDir)
tags := []string{input.ImageTag}
options := client.ImageBuildOptions{ options := client.ImageBuildOptions{
Tags: tags, Tags: []string{input.ImageTag},
Remove: true, Remove: true,
AuthConfigs: LoadDockerAuthConfigs(ctx), AuthConfigs: LoadDockerAuthConfigs(ctx),
Dockerfile: input.Dockerfile, Dockerfile: input.Dockerfile,
@@ -76,11 +75,7 @@ func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor {
logger.Debugf("Creating image from context dir '%s' with tag '%s' and platform '%s'", input.ContextDir, input.ImageTag, input.Platform) logger.Debugf("Creating image from context dir '%s' with tag '%s' and platform '%s'", input.ContextDir, input.ImageTag, input.Platform)
resp, err := cli.ImageBuild(ctx, buildContext, options) resp, err := cli.ImageBuild(ctx, buildContext, options)
err = logDockerResponse(logger, resp.Body, err != nil) return logDockerResponse(logger, resp.Body, err != nil)
if err != nil {
return err
}
return nil
} }
} }
+16 -54
View File
@@ -4,12 +4,12 @@
//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd)) //go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd))
// This file is exact copy of https://github.com/docker/cli/blob/4a63305d74332de5ceba7fcbccbc3cbb7412f5ba/cli/command/container/opts.go with: // This file is based on https://github.com/docker/cli/blob/4a63305d74332de5ceba7fcbccbc3cbb7412f5ba/cli/command/container/opts.go with:
// * appended with license information // * appended with license information
// * regexp and ParseVolume in place of the import-restricted internal/lazyregexp and internal/volumespec // * regexp and ParseVolume in place of the import-restricted internal/lazyregexp and internal/volumespec
// * cdiDeviceName in place of the container-device-interface parser's IsQualifiedName // * cdiDeviceName in place of the container-device-interface parser's IsQualifiedName
// * jsontext in place of json.Compact for the seccomp profile // * jsontext in place of json.Compact for the seccomp profile
// * invalidParameter from the package's errors.go, and convertPortSet/convertPortMap for the callers in docker_run.go // * invalidParameter from the package's errors.go, and convertPortSet/convertPortMap for nat port types
// //
// docker/cli is licensed under the Apache License, Version 2.0. // docker/cli is licensed under the Apache License, Version 2.0.
// See DOCKER_LICENSE for the full license text. // See DOCKER_LICENSE for the full license text.
@@ -154,9 +154,6 @@ type containerOptions struct {
init bool init bool
umask opts.UmaskOpt umask opts.UmaskOpt
annotations *opts.MapOpts annotations *opts.MapOpts
Image string
Args []string
} }
// addFlags adds all command line flags that will be used by parse to the FlagSet // addFlags adds all command line flags that will be used by parse to the FlagSet
@@ -422,12 +419,7 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
tmpfs[k] = v tmpfs[k] = v
} }
var runCmd, entrypoint []string var entrypoint []string
if len(copts.Args) > 0 {
runCmd = copts.Args
}
if copts.entrypoint != "" { if copts.entrypoint != "" {
entrypoint = []string{copts.entrypoint} entrypoint = []string{copts.entrypoint}
} else if flags.Changed("entrypoint") { } else if flags.Changed("entrypoint") {
@@ -448,36 +440,15 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
if err != nil { if err != nil {
return nil, err return nil, err
} }
portBindings := network.PortMap{} portBindings, err := convertPortMap(natPortBindings)
for port, bindings := range natPortBindings { if err != nil {
p, err := network.ParsePort(string(port)) return nil, err
if err != nil {
return nil, err
}
portBindings[p] = []network.PortBinding{}
for _, b := range bindings {
var hostIP netip.Addr
if b.HostIP != "" {
hostIP, err = netip.ParseAddr(b.HostIP)
if err != nil {
return nil, err
}
}
portBindings[p] = append(portBindings[p], network.PortBinding{
HostIP: hostIP,
HostPort: b.HostPort,
})
}
} }
// Add published ports as exposed ports. // Add published ports as exposed ports.
exposedPorts := network.PortSet{} exposedPorts, err := convertPortSet(ports)
for port := range ports { if err != nil {
p, err := network.ParsePort(string(port)) return nil, err
if err != nil {
return nil, err
}
exposedPorts[p] = struct{}{}
} }
// Merge in exposed ports to the map of published ports // Merge in exposed ports to the map of published ports
@@ -618,11 +589,10 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
deviceRequests := copts.gpus.Value() deviceRequests := copts.gpus.Value()
if len(cdiDeviceNames) > 0 { if len(cdiDeviceNames) > 0 {
cdiDeviceRequest := container.DeviceRequest{ deviceRequests = append(deviceRequests, container.DeviceRequest{
Driver: "cdi", Driver: "cdi",
DeviceIDs: cdiDeviceNames, DeviceIDs: cdiDeviceNames,
} })
deviceRequests = append(deviceRequests, cdiDeviceRequest)
} }
resources := container.Resources{ resources := container.Resources{
@@ -668,8 +638,6 @@ func parse(flags *pflag.FlagSet, copts *containerOptions, serverOS string) (*con
AttachStdout: attachStdout, AttachStdout: attachStdout,
AttachStderr: attachStderr, AttachStderr: attachStderr,
Env: envVariables, Env: envVariables,
Cmd: runCmd,
Image: copts.Image,
Volumes: volumes, Volumes: volumes,
Entrypoint: entrypoint, Entrypoint: entrypoint,
WorkingDir: copts.workingDir, WorkingDir: copts.workingDir,
@@ -810,10 +778,8 @@ func parseNetworkOpts(copts *containerOptions) (map[string]*network.EndpointSett
// For backward compatibility: if no custom options are provided for the network, // For backward compatibility: if no custom options are provided for the network,
// and only a single network is specified, omit the endpoint-configuration // and only a single network is specified, omit the endpoint-configuration
// on the client (the daemon will still create it when creating the container) // on the client (the daemon will still create it when creating the container)
if i == 0 && len(copts.netMode.Value()) == 1 { if i == 0 && len(copts.netMode.Value()) == 1 && reflect.ValueOf(*ep).IsZero() {
if ep == nil || reflect.ValueOf(*ep).IsZero() { continue
continue
}
} }
endpoints[n.Target] = ep endpoints[n.Target] = ep
} }
@@ -844,14 +810,12 @@ func applyContainerOptions(n *opts.NetworkAttachmentOpts, copts *containerOption
return invalidParameter(errors.New("conflicting options: cannot specify both --link-local-ip and per-network link-local IP addresses")) return invalidParameter(errors.New("conflicting options: cannot specify both --link-local-ip and per-network link-local IP addresses"))
} }
if copts.aliases.Len() > 0 { if copts.aliases.Len() > 0 {
n.Aliases = make([]string, copts.aliases.Len()) n.Aliases = slices.Clone(copts.aliases.GetSlice())
copy(n.Aliases, copts.aliases.GetSlice())
} }
// For a user-defined network, "--link" is an endpoint option, it creates an alias. But, // For a user-defined network, "--link" is an endpoint option, it creates an alias. But,
// for the default bridge it defines a legacy-link. // for the default bridge it defines a legacy-link.
if container.NetworkMode(n.Target).IsUserDefined() && copts.links.Len() > 0 { if container.NetworkMode(n.Target).IsUserDefined() && copts.links.Len() > 0 {
n.Links = make([]string, copts.links.Len()) n.Links = slices.Clone(copts.links.GetSlice())
copy(n.Links, copts.links.GetSlice())
} }
if copts.ipv4Address != nil { if copts.ipv4Address != nil {
if ipv4, ok := netip.AddrFromSlice(copts.ipv4Address.To4()); ok { if ipv4, ok := netip.AddrFromSlice(copts.ipv4Address.To4()); ok {
@@ -890,7 +854,6 @@ func parseNetworkAttachmentOpt(ep opts.NetworkAttachmentOpts) (*network.Endpoint
} }
epConfig.Aliases = append(epConfig.Aliases, ep.Aliases...) epConfig.Aliases = append(epConfig.Aliases, ep.Aliases...)
if len(ep.DriverOpts) > 0 { if len(ep.DriverOpts) > 0 {
epConfig.DriverOpts = make(map[string]string)
epConfig.DriverOpts = ep.DriverOpts epConfig.DriverOpts = ep.DriverOpts
} }
if len(ep.Links) > 0 { if len(ep.Links) > 0 {
@@ -934,11 +897,10 @@ func convertToStandardNotation(ports []string) ([]string, error) {
} }
func parseLoggingOpts(loggingDriver string, loggingOpts []string) (map[string]string, error) { func parseLoggingOpts(loggingDriver string, loggingOpts []string) (map[string]string, error) {
loggingOptsMap := opts.ConvertKVStringsToMap(loggingOpts)
if loggingDriver == "none" && len(loggingOpts) > 0 { if loggingDriver == "none" && len(loggingOpts) > 0 {
return map[string]string{}, fmt.Errorf("invalid logging opts for driver %s", loggingDriver) return map[string]string{}, fmt.Errorf("invalid logging opts for driver %s", loggingDriver)
} }
return loggingOptsMap, nil return opts.ConvertKVStringsToMap(loggingOpts), nil
} }
// takes a local seccomp daemon, reads the file contents for sending to the daemon // takes a local seccomp daemon, reads the file contents for sending to the daemon
File diff suppressed because it is too large. Load diff
+1 -10
View File
@@ -33,18 +33,9 @@ func logDockerResponse(logger *logging.Logger, dockerResponse io.ReadCloser, isE
defer dockerResponse.Close() defer dockerResponse.Close()
scanner := bufio.NewScanner(dockerResponse) scanner := bufio.NewScanner(dockerResponse)
msg := dockerMessage{}
for scanner.Scan() { for scanner.Scan() {
line := scanner.Bytes() line := scanner.Bytes()
var msg dockerMessage
msg.ID = ""
msg.Stream = ""
msg.Error = ""
msg.ErrorDetail.Message = ""
msg.Status = ""
msg.Progress = ""
if err := json.Unmarshal(line, &msg); err != nil { if err := json.Unmarshal(line, &msg); err != nil {
writeLog(logger, false, "Unable to unmarshal line [%s] ==> %v", string(line), err) writeLog(logger, false, "Unable to unmarshal line [%s] ==> %v", string(line), err)
continue continue
+12 -26
View File
@@ -6,6 +6,7 @@ package container
import ( import (
"testing" "testing"
specs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@@ -17,32 +18,17 @@ func TestParsePlatform(t *testing.T) {
assert.Nil(t, got) assert.Nil(t, got)
}) })
t.Run("os/arch", func(t *testing.T) { for input, want := range map[string]specs.Platform{
got, err := parsePlatform("linux/amd64") "linux/amd64": {OS: "linux", Architecture: "amd64"},
require.NoError(t, err) "linux/arm/v7": {OS: "linux", Architecture: "arm", Variant: "v7"},
require.NotNil(t, got) "Linux/AMD64/V8": {OS: "linux", Architecture: "amd64", Variant: "v8"},
assert.Equal(t, "linux", got.OS) } {
assert.Equal(t, "amd64", got.Architecture) t.Run(input, func(t *testing.T) {
assert.Empty(t, got.Variant) got, err := parsePlatform(input)
}) require.NoError(t, err)
assert.Equal(t, &want, got)
t.Run("os/arch/variant", func(t *testing.T) { })
got, err := parsePlatform("linux/arm/v7") }
require.NoError(t, err)
require.NotNil(t, got)
assert.Equal(t, "linux", got.OS)
assert.Equal(t, "arm", got.Architecture)
assert.Equal(t, "v7", got.Variant)
})
t.Run("input is lowercased", func(t *testing.T) {
got, err := parsePlatform("Linux/AMD64/V8")
require.NoError(t, err)
require.NotNil(t, got)
assert.Equal(t, "linux", got.OS)
assert.Equal(t, "amd64", got.Architecture)
assert.Equal(t, "v8", got.Variant)
})
for _, bad := range []string{ for _, bad := range []string{
"amd64", "amd64",
+33 -53
View File
@@ -50,15 +50,12 @@ const drainGracePeriod = 2 * time.Second
// NewContainer creates a reference to a container // NewContainer creates a reference to a container
func NewContainer(input *NewContainerInput) ExecutionsEnvironment { func NewContainer(input *NewContainerInput) ExecutionsEnvironment {
cr := new(containerReference)
cr.input = input
// Resolved up front because the image pull runs before the container is created. // Resolved up front because the image pull runs before the container is created.
cf := createFlagsFromOptions(input.allOptions()) cf := createFlagsFromOptions(input.allOptions())
if cf.platform != "" { if cf.platform != "" {
cr.input.Platform = cf.platform input.Platform = cf.platform
} }
cr.pullPolicy = cf.pull return &containerReference{input: input, pullPolicy: cf.pull}
return cr
} }
// supportsContainerImagePlatform reports whether the Docker server API version // supportsContainerImagePlatform reports whether the Docker server API version
@@ -394,17 +391,16 @@ func GetDockerClient(ctx context.Context) (cli client.APIClient, err error) {
return cli, nil return cli, nil
} }
func GetHostInfo(ctx context.Context) (info system.Info, err error) { func GetHostInfo(ctx context.Context) (system.Info, error) {
var cli client.APIClient cli, err := GetDockerClient(ctx)
cli, err = GetDockerClient(ctx)
if err != nil { if err != nil {
return info, err return system.Info{}, err
} }
defer cli.Close() defer cli.Close()
result, err := cli.Info(ctx, client.InfoOptions{}) result, err := cli.Info(ctx, client.InfoOptions{})
if err != nil { if err != nil {
return info, err return system.Info{}, err
} }
return result.Info, nil return result.Info, nil
@@ -559,32 +555,32 @@ func (input *NewContainerInput) allOptions() string {
return strings.TrimSpace(input.RunnerOptions + " " + input.WorkflowOptions) return strings.TrimSpace(input.RunnerOptions + " " + input.WorkflowOptions)
} }
func (cr *containerReference) mergeContainerConfigs(ctx context.Context, config *container.Config, hostConfig *container.HostConfig) (*container.Config, *container.HostConfig, error) { func (cr *containerReference) mergeContainerConfigs(ctx context.Context, config *container.Config, hostConfig *container.HostConfig) error {
logger := common.Logger(ctx) logger := common.Logger(ctx)
options := cr.input.allOptions() options := cr.input.allOptions()
if options == "" { if options == "" {
return config, hostConfig, nil return nil
} }
// For Gitea, checked here because the parse below is what would read those files // For Gitea, checked here because the parse below is what would read those files
if err := rejectHostReadingOptions(cr.input.WorkflowOptions); err != nil { if err := rejectHostReadingOptions(cr.input.WorkflowOptions); err != nil {
return nil, nil, err return err
} }
// parse configuration from CLI container.options // parse configuration from CLI container.options
flags, copts, cf, err := parseContainerOptions(options) flags, copts, cf, err := parseContainerOptions(options)
if err != nil { if err != nil {
return nil, nil, err return err
} }
if err := cf.validate(); err != nil { if err := cf.validate(); err != nil {
return nil, nil, fmt.Errorf("cannot process container options: '%s': '%w'", options, err) return fmt.Errorf("cannot process container options: '%s': '%w'", options, err)
} }
if len(copts.netMode.Value()) == 0 { if len(copts.netMode.Value()) == 0 {
if err = copts.netMode.Set(cr.input.NetworkMode); err != nil { if err = copts.netMode.Set(cr.input.NetworkMode); err != nil {
return nil, nil, fmt.Errorf("cannot parse networkmode=%s. This is an internal error and should not happen: '%w'", cr.input.NetworkMode, err) return fmt.Errorf("cannot parse networkmode=%s. This is an internal error and should not happen: '%w'", cr.input.NetworkMode, err)
} }
} }
@@ -597,7 +593,7 @@ func (cr *containerReference) mergeContainerConfigs(ctx context.Context, config
containerConfig, err := parse(flags, copts, runtime.GOOS) containerConfig, err := parse(flags, copts, runtime.GOOS)
if err != nil { if err != nil {
return nil, nil, fmt.Errorf("cannot process container options: '%s': '%w'", options, err) return fmt.Errorf("cannot process container options: '%s': '%w'", options, err)
} }
// workflow aliases join the runner's own, deduped so a re-create cannot grow the input // workflow aliases join the runner's own, deduped so a re-create cannot grow the input
for _, endpoint := range containerConfig.NetworkingConfig.EndpointsConfig { for _, endpoint := range containerConfig.NetworkingConfig.EndpointsConfig {
@@ -612,7 +608,7 @@ func (cr *containerReference) mergeContainerConfigs(ctx context.Context, config
if !hostConfig.Privileged { if !hostConfig.Privileged {
trusted, err := parseOptionsHostConfig(cr.input.RunnerOptions) trusted, err := parseOptionsHostConfig(cr.input.RunnerOptions)
if err != nil { if err != nil {
return nil, nil, err return err
} }
sanitizeOptionsHostConfig(logger, containerConfig.HostConfig, trusted) sanitizeOptionsHostConfig(logger, containerConfig.HostConfig, trusted)
} }
@@ -624,7 +620,7 @@ func (cr *containerReference) mergeContainerConfigs(ctx context.Context, config
logger.Debugf("Custom container.HostConfig from options ==> %+v", containerConfig.HostConfig) logger.Debugf("Custom container.HostConfig from options ==> %+v", containerConfig.HostConfig)
cr.sanitizeConfig(ctx, nil, containerConfig.HostConfig) // a dropped volume must not displace the runner's own cr.sanitizeConfig(ctx, containerConfig.HostConfig) // a dropped volume must not displace the runner's own
overlayVolumes(hostConfig, containerConfig.HostConfig) overlayVolumes(hostConfig, containerConfig.HostConfig)
binds := hostConfig.Binds binds := hostConfig.Binds
mounts := hostConfig.Mounts mounts := hostConfig.Mounts
@@ -645,7 +641,7 @@ func (cr *containerReference) mergeContainerConfigs(ctx context.Context, config
hostConfig.NetworkMode = networkMode hostConfig.NetworkMode = networkMode
logger.Debugf("Merged container.HostConfig ==> %+v", hostConfig) logger.Debugf("Merged container.HostConfig ==> %+v", hostConfig)
return config, hostConfig, nil return nil
} }
func (cr *containerReference) create(capAdd, capDrop []string) common.Executor { func (cr *containerReference) create(capAdd, capDrop []string) common.Executor {
@@ -711,8 +707,7 @@ func (cr *containerReference) create(capAdd, capDrop []string) common.Executor {
AutoRemove: input.AutoRemove, AutoRemove: input.AutoRemove,
} }
config, hostConfig, err = cr.mergeContainerConfigs(ctx, config, hostConfig) if err := cr.mergeContainerConfigs(ctx, config, hostConfig); err != nil {
if err != nil {
return err return err
} }
@@ -721,18 +716,15 @@ func (cr *containerReference) create(capAdd, capDrop []string) common.Executor {
} }
// For Gitea // For Gitea
config, hostConfig = cr.sanitizeConfig(ctx, config, hostConfig) cr.sanitizeConfig(ctx, hostConfig)
var networkingConfig *network.NetworkingConfig var networkingConfig *network.NetworkingConfig
n := hostConfig.NetworkMode n := hostConfig.NetworkMode
// IsUserDefined and IsHost are broken on windows // IsUserDefined and IsHost are broken on windows
if n.IsUserDefined() && n != "host" && len(input.NetworkAliases) > 0 { if n.IsUserDefined() && n != "host" && len(input.NetworkAliases) > 0 {
endpointConfig := &network.EndpointSettings{
Aliases: input.NetworkAliases,
}
networkingConfig = &network.NetworkingConfig{ networkingConfig = &network.NetworkingConfig{
EndpointsConfig: map[string]*network.EndpointSettings{ EndpointsConfig: map[string]*network.EndpointSettings{
input.NetworkMode: endpointConfig, input.NetworkMode: {Aliases: input.NetworkAliases},
}, },
} }
} }
@@ -773,15 +765,11 @@ func (cr *containerReference) exec(cmd []string, env map[string]string, user, wo
logger.Debugf("Exec command '%s'", cmd) logger.Debugf("Exec command '%s'", cmd)
isTerminal := cr.input.AllocatePTY isTerminal := cr.input.AllocatePTY
var wd string wd := cr.input.WorkingDir
if workdir != "" { if strings.HasPrefix(workdir, "/") {
if strings.HasPrefix(workdir, "/") { wd = workdir
wd = workdir } else if workdir != "" {
} else { wd = fmt.Sprintf("%s/%s", cr.input.WorkingDir, workdir)
wd = fmt.Sprintf("%s/%s", cr.input.WorkingDir, workdir)
}
} else {
wd = cr.input.WorkingDir
} }
logger.Debugf("Working directory '%s'", wd) logger.Debugf("Working directory '%s'", wd)
@@ -806,8 +794,7 @@ func (cr *containerReference) exec(cmd []string, env map[string]string, user, wo
} }
defer resp.Close() defer resp.Close()
err = cr.waitForCommand(ctx, resp.HijackedResponse) if err := cr.waitForCommand(ctx, resp.HijackedResponse); err != nil {
if err != nil {
return err return err
} }
@@ -909,17 +896,14 @@ func (cr *containerReference) copyDir(dstPath, srcPath string, useGitIgnore, ski
return err return err
} }
logger.Debugf("Writing tarball %s from %s", tarFile.Name(), srcPath) logger.Debugf("Writing tarball %s from %s", tarFile.Name(), srcPath)
defer func(tarFile *os.File) { defer func() {
name := tarFile.Name() if err := tarFile.Close(); !errors.Is(err, os.ErrClosed) {
err := tarFile.Close()
if !errors.Is(err, os.ErrClosed) {
logger.Error(err) logger.Error(err)
} }
err = os.Remove(name) if err := os.Remove(tarFile.Name()); err != nil {
if err != nil {
logger.Error(err) logger.Error(err)
} }
}(tarFile) }()
extractionRoot := "/" extractionRoot := "/"
if actPath := cr.GetActPath(); strings.HasPrefix(dstPath+"/", actPath+"/") { if actPath := cr.GetActPath(); strings.HasPrefix(dstPath+"/", actPath+"/") {
extractionRoot = actPath // keeps tar entries from crossing the image's /var/run symlink extractionRoot = actPath // keeps tar entries from crossing the image's /var/run symlink
@@ -929,8 +913,7 @@ func (cr *containerReference) copyDir(dstPath, srcPath string, useGitIgnore, ski
} }
logger.Debugf("Extracting content from '%s' to '%s'", tarFile.Name(), dstPath) logger.Debugf("Extracting content from '%s' to '%s'", tarFile.Name(), dstPath)
_, err = tarFile.Seek(0, 0) if _, err := tarFile.Seek(0, 0); err != nil {
if err != nil {
return fmt.Errorf("failed to seek tar archive: %w", err) return fmt.Errorf("failed to seek tar archive: %w", err)
} }
return cr.upload(ctx, extractionRoot, tarFile) return cr.upload(ctx, extractionRoot, tarFile)
@@ -1098,9 +1081,7 @@ func parseOptionsHostConfig(options string) (*container.HostConfig, error) {
return containerConfig.HostConfig, nil return containerConfig.HostConfig, nil
} }
// For Gitea func (cr *containerReference) sanitizeConfig(ctx context.Context, hostConfig *container.HostConfig) {
// sanitizeConfig remove the invalid configurations from `config` and `hostConfig`
func (cr *containerReference) sanitizeConfig(ctx context.Context, config *container.Config, hostConfig *container.HostConfig) (*container.Config, *container.HostConfig) {
logger := common.Logger(ctx) logger := common.Logger(ctx)
matcher := newValidVolumeMatcher(ctx, cr.input.ValidVolumes) matcher := newValidVolumeMatcher(ctx, cr.input.ValidVolumes)
@@ -1149,13 +1130,12 @@ func (cr *containerReference) sanitizeConfig(ctx context.Context, config *contai
} }
} }
hostConfig.Mounts = sanitizedMounts hostConfig.Mounts = sanitizedMounts
return config, hostConfig
} }
func SanitizeBinds(ctx context.Context, validVolumes, binds []string) []string { func SanitizeBinds(ctx context.Context, validVolumes, binds []string) []string {
cr := &containerReference{input: &NewContainerInput{ValidVolumes: validVolumes}} cr := &containerReference{input: &NewContainerInput{ValidVolumes: validVolumes}}
_, hostConfig := cr.sanitizeConfig(ctx, nil, &container.HostConfig{Binds: binds}) hostConfig := &container.HostConfig{Binds: binds}
cr.sanitizeConfig(ctx, hostConfig)
return hostConfig.Binds return hostConfig.Binds
} }
+74 -142
View File
@@ -186,11 +186,9 @@ func TestDockerExecAbort(t *testing.T) {
}, nil) }, nil)
cr := &containerReference{ cr := &containerReference{
id: "123", id: "123",
cli: client, cli: client,
input: &NewContainerInput{ input: &NewContainerInput{},
Image: "image",
},
} }
channel := make(chan error) channel := make(chan error)
@@ -213,12 +211,10 @@ func TestDockerExecAbort(t *testing.T) {
func TestDockerExecFailure(t *testing.T) { func TestDockerExecFailure(t *testing.T) {
ctx := t.Context() ctx := t.Context()
conn := &mockConn{}
client := &mockDockerClient{} client := &mockDockerClient{}
client.On("ExecCreate", ctx, "123", mock.AnythingOfType("client.ExecCreateOptions")).Return(mobyclient.ExecCreateResult{ID: "id"}, nil) client.On("ExecCreate", ctx, "123", mock.AnythingOfType("client.ExecCreateOptions")).Return(mobyclient.ExecCreateResult{ID: "id"}, nil)
client.On("ExecAttach", ctx, "id", mock.AnythingOfType("client.ExecAttachOptions")).Return(mobyclient.ExecAttachResult{ client.On("ExecAttach", ctx, "id", mock.AnythingOfType("client.ExecAttachOptions")).Return(mobyclient.ExecAttachResult{
Conn: conn, Conn: &mockConn{},
Reader: bufio.NewReader(strings.NewReader("output")), Reader: bufio.NewReader(strings.NewReader("output")),
}, nil) }, nil)
client.On("ExecInspect", ctx, "id", mobyclient.ExecInspectOptions{}).Return(mobyclient.ExecInspectResult{ client.On("ExecInspect", ctx, "id", mobyclient.ExecInspectOptions{}).Return(mobyclient.ExecInspectResult{
@@ -226,11 +222,9 @@ func TestDockerExecFailure(t *testing.T) {
}, nil) }, nil)
cr := &containerReference{ cr := &containerReference{
id: "123", id: "123",
cli: client, cli: client,
input: &NewContainerInput{ input: &NewContainerInput{},
Image: "image",
},
} }
err := cr.exec([]string{""}, map[string]string{}, "user", "workdir")(ctx) err := cr.exec([]string{""}, map[string]string{}, "user", "workdir")(ctx)
@@ -239,7 +233,6 @@ func TestDockerExecFailure(t *testing.T) {
assert.Equal(t, ExitCodeError(1), exitErr) assert.Equal(t, ExitCodeError(1), exitErr)
assert.Equal(t, "Process completed with exit code 1.", err.Error()) assert.Equal(t, "Process completed with exit code 1.", err.Error())
conn.AssertExpectations(t)
client.AssertExpectations(t) client.AssertExpectations(t)
} }
@@ -254,9 +247,6 @@ func stdcopyFrame(stream stdcopy.StdType, payload string) []byte {
return b return b
} }
// TestDockerAttachFlushesTrailingLine verifies that wait() blocks until the
// attach() streaming goroutine has drained and flushed the container's output,
// so a final line without a trailing newline is not lost.
func TestDockerAttachFlushesTrailingLine(t *testing.T) { func TestDockerAttachFlushesTrailingLine(t *testing.T) {
ctx := t.Context() ctx := t.Context()
@@ -277,18 +267,13 @@ func TestDockerAttachFlushesTrailingLine(t *testing.T) {
statusCh := make(chan container.WaitResponse, 1) statusCh := make(chan container.WaitResponse, 1)
statusCh <- container.WaitResponse{StatusCode: 0} statusCh <- container.WaitResponse{StatusCode: 0}
errCh := make(chan error, 1)
client.On("ContainerWait", ctx, "123", mobyclient.ContainerWaitOptions{Condition: container.WaitConditionNotRunning}). client.On("ContainerWait", ctx, "123", mobyclient.ContainerWaitOptions{Condition: container.WaitConditionNotRunning}).
Return(mobyclient.ContainerWaitResult{ Return(mobyclient.ContainerWaitResult{Result: statusCh})
Result: (<-chan container.WaitResponse)(statusCh),
Error: (<-chan error)(errCh),
})
cr := &containerReference{ cr := &containerReference{
id: "123", id: "123",
cli: client, cli: client,
input: &NewContainerInput{ input: &NewContainerInput{
Image: "image",
Stdout: logWriter, Stdout: logWriter,
Stderr: logWriter, Stderr: logWriter,
}, },
@@ -297,10 +282,6 @@ func TestDockerAttachFlushesTrailingLine(t *testing.T) {
require.NoError(t, cr.attach()(ctx)) require.NoError(t, cr.attach()(ctx))
require.NoError(t, cr.wait()(ctx)) require.NoError(t, cr.wait()(ctx))
// wait() must have blocked until the goroutine drained AND flushed; the
// trailing, non-newline-terminated line must therefore be present. Reading
// lines here is race-free because wait() synchronizes on attachDone, which
// the goroutine closes after the final append.
assert.Equal(t, []string{"line one\n", "last line without newline"}, lines) assert.Equal(t, []string{"line one\n", "last line without newline"}, lines)
client.AssertExpectations(t) client.AssertExpectations(t)
@@ -311,24 +292,11 @@ func TestDockerWaitFailure(t *testing.T) {
statusCh := make(chan container.WaitResponse, 1) statusCh := make(chan container.WaitResponse, 1)
statusCh <- container.WaitResponse{StatusCode: 2} statusCh <- container.WaitResponse{StatusCode: 2}
errCh := make(chan error, 1)
client := &mockDockerClient{} client := &mockDockerClient{}
client.On("ContainerWait", ctx, "123", mobyclient.ContainerWaitOptions{Condition: container.WaitConditionNotRunning}). client.On("ContainerWait", ctx, "123", mobyclient.ContainerWaitOptions{Condition: container.WaitConditionNotRunning}).
Return(mobyclient.ContainerWaitResult{ Return(mobyclient.ContainerWaitResult{Result: statusCh})
Result: (<-chan container.WaitResponse)(statusCh),
Error: (<-chan error)(errCh),
})
cr := &containerReference{ err := (&containerReference{id: "123", cli: client}).wait()(ctx)
id: "123",
cli: client,
input: &NewContainerInput{
Image: "image",
},
}
err := cr.wait()(ctx)
var exitErr ExitCodeError var exitErr ExitCodeError
require.ErrorAs(t, err, &exitErr) require.ErrorAs(t, err, &exitErr)
assert.Equal(t, ExitCodeError(2), exitErr) assert.Equal(t, ExitCodeError(2), exitErr)
@@ -402,79 +370,49 @@ func TestRemoveWithoutIDUsesName(t *testing.T) {
client.AssertExpectations(t) client.AssertExpectations(t)
} }
// find() must drop a stale cached id so later Copy/Exec don't hit the
// daemon with a torn-down container.
func TestFindRevalidatesStaleID(t *testing.T) { func TestFindRevalidatesStaleID(t *testing.T) {
ctx := t.Context() ctx := t.Context()
inspectOpts := mobyclient.ContainerInspectOptions{}
notFound := cerrdefs.ErrNotFound.WithMessage("No such container") notFound := cerrdefs.ErrNotFound.WithMessage("No such container")
boom := errors.New("daemon unreachable") boom := errors.New("daemon unreachable")
newCR := func(id string) (*containerReference, *mockDockerClient) { for _, tc := range []struct {
client := &mockDockerClient{} name, cachedID string
return &containerReference{id: id, cli: client, input: &NewContainerInput{Name: "job-1"}}, client cachedErr error
lookup container.InspectResponse
lookupErr error
listed []container.Summary
wantID string
retryList bool
}{
{name: "stale id cleared, name lookup empty", cachedID: "stale", cachedErr: notFound, lookupErr: notFound},
{name: "stale id cleared, name lookup repopulates", cachedID: "stale", cachedErr: notFound, lookup: container.InspectResponse{ID: "fresh", Name: "/job-1"}, wantID: "fresh"},
{name: "id match falls back to listing", lookup: container.InspectResponse{ID: "job-1abc", Name: "/somebody-else"}, listed: []container.Summary{{ID: "fresh", Names: []string{"/job-1"}}}, wantID: "fresh"},
{name: "live id kept", cachedID: "live", wantID: "live"},
{name: "transient inspect error trusts cache", cachedID: "live", cachedErr: boom, wantID: "live"},
{name: "name lookup error falls back to listing", lookupErr: boom, listed: []container.Summary{{ID: "other", Names: []string{"/somebody-else"}}, {ID: "fresh", Names: []string{"/job-1"}}}, wantID: "fresh", retryList: true},
} {
t.Run(tc.name, func(t *testing.T) {
client := &mockDockerClient{}
cr := &containerReference{id: tc.cachedID, cli: client, input: &NewContainerInput{Name: "job-1"}}
if tc.cachedID != "" {
client.On("ContainerInspect", ctx, tc.cachedID, inspectOpts).Return(mobyclient.ContainerInspectResult{}, tc.cachedErr)
}
if tc.cachedID == "" || cerrdefs.IsNotFound(tc.cachedErr) {
client.On("ContainerInspect", ctx, "job-1", inspectOpts).Return(mobyclient.ContainerInspectResult{Container: tc.lookup}, tc.lookupErr)
}
if tc.listed != nil {
client.On("ContainerList", ctx, mobyclient.ContainerListOptions{All: true}).Return(mobyclient.ContainerListResult{Items: tc.listed}, nil).Once()
}
require.NoError(t, cr.find()(ctx))
assert.Equal(t, tc.wantID, cr.id)
if tc.retryList {
client.On("ContainerList", ctx, mobyclient.ContainerListOptions{All: true}).Return(mobyclient.ContainerListResult{}, boom)
cr.id = ""
require.ErrorIs(t, cr.find()(ctx), boom)
}
client.AssertExpectations(t)
})
} }
inspectOpts := mobyclient.ContainerInspectOptions{}
t.Run("stale id cleared, name lookup empty", func(t *testing.T) {
cr, client := newCR("stale")
client.On("ContainerInspect", ctx, "stale", inspectOpts).Return(mobyclient.ContainerInspectResult{}, notFound)
client.On("ContainerInspect", ctx, "job-1", inspectOpts).Return(mobyclient.ContainerInspectResult{}, notFound)
require.NoError(t, cr.find()(ctx))
assert.Empty(t, cr.id)
client.AssertExpectations(t)
})
t.Run("stale id cleared, name lookup repopulates", func(t *testing.T) {
cr, client := newCR("stale")
client.On("ContainerInspect", ctx, "stale", inspectOpts).Return(mobyclient.ContainerInspectResult{}, notFound)
client.On("ContainerInspect", ctx, "job-1", inspectOpts).
Return(mobyclient.ContainerInspectResult{Container: container.InspectResponse{ID: "fresh", Name: "/job-1"}}, nil)
require.NoError(t, cr.find()(ctx))
assert.Equal(t, "fresh", cr.id)
client.AssertExpectations(t)
})
t.Run("id match falls back to listing", func(t *testing.T) {
cr, client := newCR("")
client.On("ContainerInspect", ctx, "job-1", inspectOpts).
Return(mobyclient.ContainerInspectResult{Container: container.InspectResponse{ID: "job-1abc", Name: "/somebody-else"}}, nil)
client.On("ContainerList", ctx, mobyclient.ContainerListOptions{All: true}).Return(mobyclient.ContainerListResult{Items: []container.Summary{
{ID: "fresh", Names: []string{"/job-1"}},
}}, nil)
require.NoError(t, cr.find()(ctx))
assert.Equal(t, "fresh", cr.id)
client.AssertExpectations(t)
})
t.Run("live id kept", func(t *testing.T) {
cr, client := newCR("live")
client.On("ContainerInspect", ctx, "live", inspectOpts).Return(mobyclient.ContainerInspectResult{}, nil)
require.NoError(t, cr.find()(ctx))
assert.Equal(t, "live", cr.id)
client.AssertExpectations(t)
})
t.Run("transient inspect error trusts cache", func(t *testing.T) {
cr, client := newCR("live")
client.On("ContainerInspect", ctx, "live", inspectOpts).Return(mobyclient.ContainerInspectResult{}, boom)
require.NoError(t, cr.find()(ctx))
assert.Equal(t, "live", cr.id)
client.AssertExpectations(t)
})
t.Run("name lookup error falls back to listing", func(t *testing.T) {
cr, client := newCR("")
client.On("ContainerInspect", ctx, "job-1", inspectOpts).Return(mobyclient.ContainerInspectResult{}, boom)
client.On("ContainerList", ctx, mobyclient.ContainerListOptions{All: true}).Return(mobyclient.ContainerListResult{Items: []container.Summary{
{ID: "other", Names: []string{"/somebody-else"}},
{ID: "fresh", Names: []string{"/job-1"}},
}}, nil).Once()
require.NoError(t, cr.find()(ctx))
assert.Equal(t, "fresh", cr.id)
client.On("ContainerList", ctx, mobyclient.ContainerListOptions{All: true}).Return(mobyclient.ContainerListResult{}, boom)
cr.id = ""
require.ErrorIs(t, cr.find()(ctx), boom)
client.AssertExpectations(t)
})
} }
func TestRunnerArchCachedPerRun(t *testing.T) { func TestRunnerArchCachedPerRun(t *testing.T) {
@@ -568,9 +506,6 @@ func TestPublicCopyPipelineHandlesStaleID(t *testing.T) {
} }
} }
// Type assert containerReference implements ExecutionsEnvironment
var _ ExecutionsEnvironment = &containerReference{}
func TestCheckVolumes(t *testing.T) { func TestCheckVolumes(t *testing.T) {
testCases := []struct { testCases := []struct {
desc string desc string
@@ -665,10 +600,11 @@ func TestCheckVolumes(t *testing.T) {
ValidVolumes: tc.validVolumes, ValidVolumes: tc.validVolumes,
}, },
} }
_, hostConf := cr.sanitizeConfig(ctx, &container.Config{}, &container.HostConfig{Binds: tc.binds, Mounts: tc.mounts}) hostConf := &container.HostConfig{Binds: tc.binds, Mounts: tc.mounts}
cr.sanitizeConfig(ctx, hostConf)
assert.Equal(t, tc.expectedBinds, hostConf.Binds) assert.Equal(t, tc.expectedBinds, hostConf.Binds)
assert.ElementsMatch(t, tc.expectedMounts, hostConf.Mounts) assert.ElementsMatch(t, tc.expectedMounts, hostConf.Mounts)
assert.Len(t, hook.AllRecords(), len(tc.binds)+len(tc.mounts)-len(tc.expectedBinds)-len(tc.expectedMounts)) // every drop is warned about assert.Len(t, hook.AllRecords(), len(tc.binds)+len(tc.mounts)-len(tc.expectedBinds)-len(tc.expectedMounts))
}) })
} }
} }
@@ -688,8 +624,6 @@ func TestMergeContainerConfigsDropsVolumeDriversFromWorkflows(t *testing.T) {
assert.Equal(t, "job-escape", hostConfig.Mounts[0].Source) assert.Equal(t, "job-escape", hostConfig.Mounts[0].Source)
} }
// Both of these are read here, on the runner, so a workflow could read the runner's files
// and environment with them.
func TestMergeContainerConfigsKeepsTheRunnersFilesAndEnvToItself(t *testing.T) { func TestMergeContainerConfigsKeepsTheRunnersFilesAndEnvToItself(t *testing.T) {
hostFile := filepath.Join(t.TempDir(), "host.env") hostFile := filepath.Join(t.TempDir(), "host.env")
require.NoError(t, os.WriteFile(hostFile, []byte("STOLEN=from-the-host\n"), 0o600)) require.NoError(t, os.WriteFile(hostFile, []byte("STOLEN=from-the-host\n"), 0o600))
@@ -699,16 +633,12 @@ func TestMergeContainerConfigsKeepsTheRunnersFilesAndEnvToItself(t *testing.T) {
logger := logging.New(logtest.New()) logger := logging.New(logtest.New())
cr := &containerReference{input: &NewContainerInput{NetworkMode: "bridge", WorkflowOptions: option}} cr := &containerReference{input: &NewContainerInput{NetworkMode: "bridge", WorkflowOptions: option}}
_, _, err := cr.mergeContainerConfigs(common.WithLogger(t.Context(), logger), &container.Config{}, &container.HostConfig{}) require.ErrorContains(t, cr.mergeContainerConfigs(common.WithLogger(t.Context(), logger), &container.Config{}, &container.HostConfig{}), "not allowed in a workflow")
require.ErrorContains(t, err, "not allowed in a workflow")
// the runner reading its own files is what those options are for
cr = &containerReference{input: &NewContainerInput{NetworkMode: "bridge", RunnerOptions: option}} cr = &containerReference{input: &NewContainerInput{NetworkMode: "bridge", RunnerOptions: option}}
_, _, err = cr.mergeContainerConfigs(common.WithLogger(t.Context(), logger), &container.Config{}, &container.HostConfig{}) require.NoError(t, cr.mergeContainerConfigs(common.WithLogger(t.Context(), logger), &container.Config{}, &container.HostConfig{}))
require.NoError(t, err)
} }
// a bare name is no longer resolved from the runner's environment, for either source
logger := logging.New(logtest.New()) logger := logging.New(logtest.New())
cr := &containerReference{input: &NewContainerInput{ cr := &containerReference{input: &NewContainerInput{
NetworkMode: "bridge", NetworkMode: "bridge",
@@ -716,8 +646,8 @@ func TestMergeContainerConfigsKeepsTheRunnersFilesAndEnvToItself(t *testing.T) {
WorkflowOptions: "--env RUNNER_SECRET --env GIVEN=value", WorkflowOptions: "--env RUNNER_SECRET --env GIVEN=value",
}} }}
config, _, err := cr.mergeContainerConfigs(common.WithLogger(t.Context(), logger), &container.Config{}, &container.HostConfig{}) config := &container.Config{}
require.NoError(t, err) require.NoError(t, cr.mergeContainerConfigs(common.WithLogger(t.Context(), logger), config, &container.HostConfig{}))
assert.Equal(t, []string{"RUNNER_SECRET", "RUNNER_SECRET", "GIVEN=value"}, config.Env) assert.Equal(t, []string{"RUNNER_SECRET", "RUNNER_SECRET", "GIVEN=value"}, config.Env)
} }
@@ -754,7 +684,6 @@ func TestSanitizeOptionsHostConfig(t *testing.T) {
assert.Equal(t, &container.HostConfig{}, hostConfig) assert.Equal(t, &container.HostConfig{}, hostConfig)
} }
// mergeOptions merges both option sources into a bare container, returning the result and its log.
func mergeOptions(t *testing.T, runnerOptions, workflowOptions string, privileged bool) (*container.HostConfig, *logtest.Handler) { func mergeOptions(t *testing.T, runnerOptions, workflowOptions string, privileged bool) (*container.HostConfig, *logtest.Handler) {
t.Helper() t.Helper()
logger, hook := logtest.NewLogger() logger, hook := logtest.NewLogger()
@@ -766,12 +695,12 @@ func mergeOptions(t *testing.T, runnerOptions, workflowOptions string, privilege
ValidVolumes: []string{"**"}, ValidVolumes: []string{"**"},
}} }}
_, hostConfig, err := cr.mergeContainerConfigs(common.WithLogger(t.Context(), logger), &container.Config{}, &container.HostConfig{ hostConfig := &container.HostConfig{
Privileged: privileged, Privileged: privileged,
UsernsMode: container.UsernsMode("private"), UsernsMode: container.UsernsMode("private"),
NetworkMode: container.NetworkMode("bridge"), NetworkMode: container.NetworkMode("bridge"),
}) }
require.NoError(t, err) require.NoError(t, cr.mergeContainerConfigs(common.WithLogger(t.Context(), logger), &container.Config{}, hostConfig))
return hostConfig, hook return hostConfig, hook
} }
@@ -825,23 +754,26 @@ func TestCheckVolumesRejectsEscapingHostPaths(t *testing.T) {
} }
escapingPath := allowed + string(filepath.Separator) + ".." + string(filepath.Separator) + "denied" escapingPath := allowed + string(filepath.Separator) + ".." + string(filepath.Separator) + "denied"
_, hostConf := cr.sanitizeConfig(ctx, &container.Config{}, &container.HostConfig{ hostConf := &container.HostConfig{
Binds: []string{escapingPath + ":/mnt"}, Binds: []string{escapingPath + ":/mnt"},
}) }
cr.sanitizeConfig(ctx, hostConf)
assert.Empty(t, hostConf.Binds) assert.Empty(t, hostConf.Binds)
linkPath := filepath.Join(allowed, "link") linkPath := filepath.Join(allowed, "link")
if err := os.Symlink(denied, linkPath); err != nil { if err := os.Symlink(denied, linkPath); err != nil {
t.Skipf("cannot create symlink: %v", err) t.Skipf("cannot create symlink: %v", err)
} }
_, hostConf = cr.sanitizeConfig(ctx, &container.Config{}, &container.HostConfig{ hostConf = &container.HostConfig{
Binds: []string{linkPath + ":/mnt"}, Binds: []string{linkPath + ":/mnt"},
}) }
cr.sanitizeConfig(ctx, hostConf)
assert.Empty(t, hostConf.Binds) assert.Empty(t, hostConf.Binds)
_, hostConf = cr.sanitizeConfig(ctx, &container.Config{}, &container.HostConfig{ hostConf = &container.HostConfig{
Binds: []string{filepath.Join(linkPath, "missing") + ":/mnt"}, Binds: []string{filepath.Join(linkPath, "missing") + ":/mnt"},
}) }
cr.sanitizeConfig(ctx, hostConf)
assert.Empty(t, hostConf.Binds) assert.Empty(t, hostConf.Binds)
} }
@@ -851,11 +783,12 @@ func TestSanitizeConfigKeepsTmpfsMounts(t *testing.T) {
for _, validVolumes := range [][]string{nil, {"data"}, {"**:ro"}} { for _, validVolumes := range [][]string{nil, {"data"}, {"**:ro"}} {
logger, hook := logtest.NewLogger() logger, hook := logtest.NewLogger()
_, sanitized := (&containerReference{input: &NewContainerInput{ValidVolumes: validVolumes}}).sanitizeConfig(common.WithLogger(t.Context(), logger), &container.Config{}, &container.HostConfig{Mounts: []mount.Mount{ sanitized := &container.HostConfig{Mounts: []mount.Mount{
hostConfig.Mounts[0], hostConfig.Mounts[0],
{Type: mount.TypeVolume, Target: "/anonymous"}, {Type: mount.TypeVolume, Target: "/anonymous"},
{Type: mount.TypeTmpfs, Source: "/etc", Target: "/etc-copy"}, {Type: mount.TypeTmpfs, Source: "/etc", Target: "/etc-copy"},
}}) }}
(&containerReference{input: &NewContainerInput{ValidVolumes: validVolumes}}).sanitizeConfig(common.WithLogger(t.Context(), logger), sanitized)
assert.Equal(t, hostConfig.Mounts, sanitized.Mounts) assert.Equal(t, hostConfig.Mounts, sanitized.Mounts)
assert.Len(t, hook.AllRecords(), 2) assert.Len(t, hook.AllRecords(), 2)
} }
@@ -951,11 +884,11 @@ func TestMergeContainerConfigsVolumesReplaceRunnerMounts(t *testing.T) {
}, },
} }
_, hostConf, err := cr.mergeContainerConfigs(ctx, &container.Config{}, &container.HostConfig{ hostConf := &container.HostConfig{
Binds: []string{"/var/run/docker.sock:/var/run/docker.sock"}, Binds: []string{"/var/run/docker.sock:/var/run/docker.sock"},
Mounts: []mount.Mount{{Type: mount.TypeVolume, Source: "act-toolcache", Target: "/opt/hostedtoolcache"}}, Mounts: []mount.Mount{{Type: mount.TypeVolume, Source: "act-toolcache", Target: "/opt/hostedtoolcache"}},
}) }
require.NoError(t, err) require.NoError(t, cr.mergeContainerConfigs(ctx, &container.Config{}, hostConf))
assert.Equal(t, []string{"/var/run/docker.sock:/var/run/docker.sock", "/host/tools:/opt/hostedtoolcache"}, hostConf.Binds) assert.Equal(t, []string{"/var/run/docker.sock:/var/run/docker.sock", "/host/tools:/opt/hostedtoolcache"}, hostConf.Binds)
assert.Empty(t, hostConf.Mounts) assert.Empty(t, hostConf.Mounts)
} }
@@ -1007,8 +940,7 @@ func TestMergeContainerConfigsKeepsNetworkAliasesFromOptions(t *testing.T) {
NetworkMode: "job-network", NetworkAliases: []string{"redis"}, WorkflowOptions: "--network-alias redis-primary", NetworkMode: "job-network", NetworkAliases: []string{"redis"}, WorkflowOptions: "--network-alias redis-primary",
}} }}
_, _, err := cr.mergeContainerConfigs(common.WithLogger(t.Context(), logger), &container.Config{}, &container.HostConfig{}) require.NoError(t, cr.mergeContainerConfigs(common.WithLogger(t.Context(), logger), &container.Config{}, &container.HostConfig{}))
require.NoError(t, err)
assert.Equal(t, []string{"redis", "redis-primary"}, cr.input.NetworkAliases) assert.Equal(t, []string{"redis", "redis-primary"}, cr.input.NetworkAliases)
} }
+27 -57
View File
@@ -7,6 +7,7 @@ package container
import ( import (
"archive/tar" "archive/tar"
"bytes" "bytes"
"cmp"
"context" "context"
"errors" "errors"
"fmt" "fmt"
@@ -64,15 +65,11 @@ func (e *HostEnvironment) processGroup(ctx context.Context) *process.Group {
} }
func (e *HostEnvironment) Create(_, _ []string) common.Executor { func (e *HostEnvironment) Create(_, _ []string) common.Executor {
return func(ctx context.Context) error { return common.NewPipelineExecutor()
return nil
}
} }
func (e *HostEnvironment) Close() common.Executor { func (e *HostEnvironment) Close() common.Executor {
return func(ctx context.Context) error { return common.NewPipelineExecutor()
return nil
}
} }
func (e *HostEnvironment) Copy(destPath string, files ...*FileEntry) common.Executor { func (e *HostEnvironment) Copy(destPath string, files ...*FileEntry) common.Executor {
@@ -168,15 +165,11 @@ func (e *HostEnvironment) GetContainerArchive(ctx context.Context, srcPath strin
} }
func (e *HostEnvironment) Pull(_ bool) common.Executor { func (e *HostEnvironment) Pull(_ bool) common.Executor {
return func(ctx context.Context) error { return common.NewPipelineExecutor()
return nil
}
} }
func (e *HostEnvironment) Start(_ bool) common.Executor { func (e *HostEnvironment) Start(_ bool) common.Executor {
return func(ctx context.Context) error { return common.NewPipelineExecutor()
return nil
}
} }
type ptyWriter struct { type ptyWriter struct {
@@ -193,7 +186,6 @@ func (w *ptyWriter) Write(buf []byte) (int, error) {
} }
if w.dirtyLine || len(buf) > 1 && buf[len(buf)-2] != '\n' { if w.dirtyLine || len(buf) > 1 && buf[len(buf)-2] != '\n' {
_, _ = w.Out.Write([]byte("\n")) _, _ = w.Out.Write([]byte("\n"))
return n, io.EOF
} }
return n, io.EOF return n, io.EOF
} }
@@ -242,40 +234,25 @@ func writeKeepAlive(ppty io.Writer) {
} }
} }
func copyPtyOutput(writer io.Writer, ppty io.Reader, finishLog context.CancelFunc) {
defer func() {
finishLog()
}()
if _, err := io.Copy(writer, ppty); err != nil {
return
}
}
func (e *HostEnvironment) UpdateFromImageEnv(_ *map[string]string) common.Executor { func (e *HostEnvironment) UpdateFromImageEnv(_ *map[string]string) common.Executor {
return func(ctx context.Context) error { return common.NewPipelineExecutor()
return nil
}
} }
func getEnvListFromMap(env map[string]string) []string { func getEnvListFromMap(env map[string]string) []string {
envList := make([]string, 0) envList := make([]string, 0, len(env))
for k, v := range env { for key, value := range env {
envList = append(envList, fmt.Sprintf("%s=%s", k, v)) envList = append(envList, key+"="+value)
} }
return envList return envList
} }
func (e *HostEnvironment) exec(ctx context.Context, command []string, cmdline string, env map[string]string, _, workdir string) error { func (e *HostEnvironment) exec(ctx context.Context, command []string, cmdline string, env map[string]string, workdir string) error {
envList := getEnvListFromMap(env) wd := e.Path
var wd string
if workdir != "" { if workdir != "" {
if filepath.IsAbs(workdir) { wd = workdir
wd = workdir if !filepath.IsAbs(workdir) {
} else {
wd = filepath.Join(e.Path, workdir) wd = filepath.Join(e.Path, workdir)
} }
} else {
wd = e.Path
} }
// Flush any buffered, not-yet-newline-terminated trailing line, as the docker backend // Flush any buffered, not-yet-newline-terminated trailing line, as the docker backend
// does in waitForCommand, so the final line of a command's output is not lost. // does in waitForCommand, so the final line of a command's output is not lost.
@@ -288,9 +265,8 @@ func (e *HostEnvironment) exec(ctx context.Context, command []string, cmdline st
cmd := exec.CommandContext(ctx, f) cmd := exec.CommandContext(ctx, f)
cmd.Path = f cmd.Path = f
cmd.Args = command cmd.Args = command
cmd.Stdin = nil
cmd.Stdout = e.StdOut cmd.Stdout = e.StdOut
cmd.Env = envList cmd.Env = getEnvListFromMap(env)
cmd.Stderr = e.StdOut cmd.Stderr = e.StdOut
cmd.Dir = wd cmd.Dir = wd
cmd.SysProcAttr = process.SysProcAttr(cmdline, false) cmd.SysProcAttr = process.SysProcAttr(cmdline, false)
@@ -322,7 +298,10 @@ func (e *HostEnvironment) exec(ctx context.Context, command []string, cmdline st
writer = &ptyWriter{Out: e.StdOut} writer = &ptyWriter{Out: e.StdOut}
var finishLog context.CancelFunc var finishLog context.CancelFunc
logctx, finishLog = context.WithCancel(context.Background()) logctx, finishLog = context.WithCancel(context.Background())
go copyPtyOutput(writer, ppty, finishLog) go func() {
defer finishLog()
_, _ = io.Copy(writer, ppty)
}()
go writeKeepAlive(ppty) go writeKeepAlive(ppty)
} }
if err := cmd.Start(); err != nil { if err := cmd.Start(); err != nil {
@@ -357,13 +336,13 @@ func (e *HostEnvironment) exec(ctx context.Context, command []string, cmdline st
return err return err
} }
func (e *HostEnvironment) Exec(command []string /*cmdline string, */, env map[string]string, user, workdir string) common.Executor { func (e *HostEnvironment) Exec(command []string, env map[string]string, _user, workdir string) common.Executor {
return e.ExecWithCmdLine(command, "", env, user, workdir) return e.ExecWithCmdLine(command, "", env, workdir)
} }
func (e *HostEnvironment) ExecWithCmdLine(command []string, cmdline string, env map[string]string, user, workdir string) common.Executor { func (e *HostEnvironment) ExecWithCmdLine(command []string, cmdline string, env map[string]string, workdir string) common.Executor {
return func(ctx context.Context) error { return func(ctx context.Context) error {
if err := e.exec(ctx, command, cmdline, env, user, workdir); err != nil { if err := e.exec(ctx, command, cmdline, env, workdir); err != nil {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return fmt.Errorf("this step has been cancelled: %w", err) return fmt.Errorf("this step has been cancelled: %w", err)
@@ -605,8 +584,7 @@ func (*HostEnvironment) GetPathVariableName() string {
} }
func (e *HostEnvironment) DefaultPathVariable() string { func (e *HostEnvironment) DefaultPathVariable() string {
v, _ := os.LookupEnv(e.GetPathVariableName()) return os.Getenv(e.GetPathVariableName())
return v
} }
func (*HostEnvironment) JoinPathVariable(paths ...string) string { func (*HostEnvironment) JoinPathVariable(paths ...string) string {
@@ -616,30 +594,22 @@ func (*HostEnvironment) JoinPathVariable(paths ...string) string {
// Reference for Arch values for runner.arch // Reference for Arch values for runner.arch
// https://docs.github.com/en/actions/learn-github-actions/contexts#runner-context // https://docs.github.com/en/actions/learn-github-actions/contexts#runner-context
func goArchToActionArch(arch string) string { func goArchToActionArch(arch string) string {
archMapper := map[string]string{ return cmp.Or(map[string]string{
"x86_64": "X64", "x86_64": "X64",
"amd64": "X64", "amd64": "X64",
"386": "X86", "386": "X86",
"arm": "ARM", "arm": "ARM",
"aarch64": "ARM64", "aarch64": "ARM64",
"arm64": "ARM64", "arm64": "ARM64",
} }[arch], arch)
if arch, ok := archMapper[arch]; ok {
return arch
}
return arch
} }
func goOsToActionOs(os string) string { func goOsToActionOs(os string) string {
osMapper := map[string]string{ return cmp.Or(map[string]string{
"linux": "Linux", "linux": "Linux",
"darwin": "macOS", "darwin": "macOS",
"windows": "Windows", "windows": "Windows",
} }[os], os)
if os, ok := osMapper[os]; ok {
return os
}
return os
} }
func (e *HostEnvironment) GetRunnerContext(_ context.Context) map[string]any { func (e *HostEnvironment) GetRunnerContext(_ context.Context) map[string]any {
+62 -182
View File
@@ -10,7 +10,6 @@ import (
"context" "context"
"io" "io"
"os" "os"
"path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
@@ -24,7 +23,6 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// Type assert HostEnvironment implements ExecutionsEnvironment
var _ ExecutionsEnvironment = &HostEnvironment{} var _ ExecutionsEnvironment = &HostEnvironment{}
func TestActionPlatformNames(t *testing.T) { func TestActionPlatformNames(t *testing.T) {
@@ -61,32 +59,20 @@ func TestCopyDir(t *testing.T) {
} }
func TestGetContainerArchive(t *testing.T) { func TestGetContainerArchive(t *testing.T) {
dir := t.TempDir()
ctx := t.Context() ctx := t.Context()
e := &HostEnvironment{ e := &HostEnvironment{Path: t.TempDir()}
Path: filepath.Join(dir, "path"),
TmpDir: filepath.Join(dir, "tmp"),
ToolCache: filepath.Join(dir, "tool_cache"),
ActPath: filepath.Join(dir, "act_path"),
StdOut: os.Stdout,
Workdir: path.Join("testdata", "scratch"),
}
_ = os.MkdirAll(e.Path, 0o700)
_ = os.MkdirAll(e.TmpDir, 0o700)
_ = os.MkdirAll(e.ToolCache, 0o700)
_ = os.MkdirAll(e.ActPath, 0o700)
expectedContent := []byte("sdde/7sh") expectedContent := []byte("sdde/7sh")
err := os.WriteFile(filepath.Join(e.Path, "action.yml"), expectedContent, 0o600) err := os.WriteFile(filepath.Join(e.Path, "action.yml"), expectedContent, 0o600)
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act require.NoError(t, err)
archive, err := e.GetContainerArchive(ctx, e.Path) archive, err := e.GetContainerArchive(ctx, e.Path)
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act require.NoError(t, err)
defer archive.Close() defer archive.Close()
reader := tar.NewReader(archive) reader := tar.NewReader(archive)
h, err := reader.Next() h, err := reader.Next()
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act require.NoError(t, err)
assert.Equal(t, "action.yml", h.Name) assert.Equal(t, "action.yml", h.Name)
content, err := io.ReadAll(reader) content, err := io.ReadAll(reader)
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act require.NoError(t, err)
assert.Equal(t, expectedContent, content) assert.Equal(t, expectedContent, content)
_, err = reader.Next() _, err = reader.Next()
assert.ErrorIs(t, err, io.EOF) assert.ErrorIs(t, err, io.EOF)
@@ -96,19 +82,8 @@ func TestHostEnvironmentExecExitCode(t *testing.T) {
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
t.Skip("uses POSIX shell") t.Skip("uses POSIX shell")
} }
dir := t.TempDir()
ctx := t.Context() ctx := t.Context()
e := &HostEnvironment{ e := &HostEnvironment{Path: t.TempDir(), StdOut: io.Discard}
Path: filepath.Join(dir, "path"),
TmpDir: filepath.Join(dir, "tmp"),
ToolCache: filepath.Join(dir, "tool_cache"),
ActPath: filepath.Join(dir, "act_path"),
StdOut: io.Discard,
Workdir: filepath.Join(dir, "path"),
}
for _, p := range []string{e.Path, e.TmpDir, e.ToolCache, e.ActPath} {
assert.NoError(t, os.MkdirAll(p, 0o700)) //nolint:testifylint // test setup
}
err := e.Exec([]string{"sh", "-c", "exit 3"}, map[string]string{"PATH": os.Getenv("PATH")}, "", "")(ctx) err := e.Exec([]string{"sh", "-c", "exit 3"}, map[string]string{"PATH": os.Getenv("PATH")}, "", "")(ctx)
var exitErr ExitCodeError var exitErr ExitCodeError
@@ -130,20 +105,12 @@ func TestHostEnvironmentAllocatePTY(t *testing.T) {
{name: "on", allocPTY: true, expect: "TTY"}, {name: "on", allocPTY: true, expect: "TTY"},
} { } {
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
dir := t.TempDir()
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
e := &HostEnvironment{ e := &HostEnvironment{
Path: filepath.Join(dir, "path"), Path: t.TempDir(),
TmpDir: filepath.Join(dir, "tmp"),
ToolCache: filepath.Join(dir, "tool_cache"),
ActPath: filepath.Join(dir, "act_path"),
StdOut: buf, StdOut: buf,
Workdir: filepath.Join(dir, "path"),
AllocatePTY: tc.allocPTY, AllocatePTY: tc.allocPTY,
} }
for _, p := range []string{e.Path, e.TmpDir, e.ToolCache, e.ActPath} {
require.NoError(t, os.MkdirAll(p, 0o700))
}
err := e.Exec( err := e.Exec(
[]string{"sh", "-c", "[ -t 1 ] && printf TTY || printf NOTTY"}, []string{"sh", "-c", "[ -t 1 ] && printf TTY || printf NOTTY"},
@@ -156,49 +123,26 @@ func TestHostEnvironmentAllocatePTY(t *testing.T) {
} }
} }
func TestHostEnvironmentRemovePreservesWorkdirByDefault(t *testing.T) { func TestHostEnvironmentRemoveWorkdir(t *testing.T) {
ctx := common.WithLogger(t.Context(), logging.Default()) for name, owned := range map[string]bool{"preserves caller workdir": false, "cleans owned workdir": true} {
base := t.TempDir() t.Run(name, func(t *testing.T) {
miscRoot := filepath.Join(base, "misc") miscRoot, workdir := t.TempDir(), t.TempDir()
path := filepath.Join(miscRoot, "hostexecutor") e := &HostEnvironment{
require.NoError(t, os.MkdirAll(path, 0o700)) Path: filepath.Join(miscRoot, "hostexecutor"),
workdir := filepath.Join(base, "workspace", "owner", "repo") Workdir: workdir,
require.NoError(t, os.MkdirAll(workdir, 0o700)) CleanWorkdir: owned,
CleanUp: func() { _ = os.RemoveAll(miscRoot) },
e := &HostEnvironment{ }
Path: path, require.NoError(t, os.MkdirAll(e.Path, 0o700))
Workdir: workdir, require.NoError(t, e.Remove()(t.Context()))
CleanUp: func() { _, err := os.Stat(workdir)
_ = os.RemoveAll(miscRoot) if owned {
}, assert.ErrorIs(t, err, os.ErrNotExist)
StdOut: os.Stdout, } else {
require.NoError(t, err)
}
})
} }
require.NoError(t, e.Remove()(ctx))
_, err := os.Stat(workdir)
require.NoError(t, err)
}
func TestHostEnvironmentRemoveCleansWorkdirWhenOwned(t *testing.T) {
ctx := common.WithLogger(t.Context(), logging.Default())
base := t.TempDir()
miscRoot := filepath.Join(base, "misc")
path := filepath.Join(miscRoot, "hostexecutor")
require.NoError(t, os.MkdirAll(path, 0o700))
workdir := filepath.Join(base, "workspace", "123", "owner", "repo")
require.NoError(t, os.MkdirAll(workdir, 0o700))
e := &HostEnvironment{
Path: path,
Workdir: workdir,
CleanWorkdir: true,
CleanUp: func() {
_ = os.RemoveAll(miscRoot)
},
StdOut: os.Stdout,
}
require.NoError(t, e.Remove()(ctx))
_, err := os.Stat(workdir)
assert.ErrorIs(t, err, os.ErrNotExist)
} }
func TestRemoveAllWithContextDoesNotHangOnStuckDelete(t *testing.T) { func TestRemoveAllWithContextDoesNotHangOnStuckDelete(t *testing.T) {
@@ -211,9 +155,6 @@ func TestRemoveAllWithContextDoesNotHangOnStuckDelete(t *testing.T) {
<-release <-release
return nil return nil
} }
// removeAllWithContext intentionally leaks the delete goroutine on timeout,
// and that goroutine still references removeAll. Unblock it and wait for it
// to return before restoring the var, so the restore can't race the read.
t.Cleanup(func() { t.Cleanup(func() {
close(release) close(release)
<-stubDone <-stubDone
@@ -227,87 +168,42 @@ func TestRemoveAllWithContextDoesNotHangOnStuckDelete(t *testing.T) {
require.ErrorIs(t, err, context.DeadlineExceeded) require.ErrorIs(t, err, context.DeadlineExceeded)
} }
// TestHostEnvironmentRemoveDoesNotHangOnStuckCleanUp guards against a stalled func TestHostEnvironmentRemoveDoesNotHang(t *testing.T) {
// CleanUp callback (e.g. an os.RemoveAll blocked by an AV/EDR filter driver or for _, phase := range []string{"CleanUp callback", "path removal"} {
// an unresponsive mount) wedging the runner slot forever at "Cleaning up t.Run(phase, func(t *testing.T) {
// container". Remove must time out the callback and complete job teardown. origTimeout := hostCleanupTimeout
func TestHostEnvironmentRemoveDoesNotHangOnStuckCleanUp(t *testing.T) { hostCleanupTimeout = 100 * time.Millisecond
// Keep the suite fast: shrink the per-phase teardown timeout for this test. t.Cleanup(func() { hostCleanupTimeout = origTimeout })
orig := hostCleanupTimeout release, stubDone := make(chan struct{}), make(chan struct{})
hostCleanupTimeout = 100 * time.Millisecond block := func() {
t.Cleanup(func() { hostCleanupTimeout = orig }) defer close(stubDone)
<-release
ctx := common.WithLogger(t.Context(), logging.Default()) }
base := t.TempDir() e := &HostEnvironment{Path: filepath.Join(t.TempDir(), "misc", "hostexecutor"), StdOut: os.Stdout}
path := filepath.Join(base, "misc", "hostexecutor") if phase == "CleanUp callback" {
require.NoError(t, os.MkdirAll(path, 0o700)) e.CleanUp = block
} else {
release := make(chan struct{}) origRemoveAll := removeAll
t.Cleanup(func() { close(release) }) // unblock the leaked goroutine at test end removeAll = func(string) error {
block()
e := &HostEnvironment{ return nil
Path: path, }
CleanUp: func() { t.Cleanup(func() { removeAll = origRemoveAll })
<-release // simulate a delete syscall stuck indefinitely }
}, t.Cleanup(func() {
StdOut: os.Stdout, close(release)
} <-stubDone
})
done := make(chan error, 1) require.NoError(t, os.MkdirAll(e.Path, 0o700))
go func() { done <- e.Remove()(ctx) }() done := make(chan error, 1)
go func() { done <- e.Remove()(common.WithLogger(t.Context(), logging.Default())) }()
select { select {
case err := <-done: case err := <-done:
require.NoError(t, err) require.NoError(t, err)
case <-time.After(10 * time.Second): case <-time.After(10 * time.Second):
t.Fatal("Remove() hung on a stuck CleanUp callback") t.Fatalf("Remove() hung on a stuck %s", phase)
} }
} })
// TestHostEnvironmentRemoveDoesNotHangOnStuckPathRemoval guards against a
// stalled os.RemoveAll on the misc/workspace paths (same AV/EDR wedge as
// #1023) wedging job completion after the CleanUp callback has already timed
// out or finished.
func TestHostEnvironmentRemoveDoesNotHangOnStuckPathRemoval(t *testing.T) {
origTimeout := hostCleanupTimeout
hostCleanupTimeout = 100 * time.Millisecond
t.Cleanup(func() { hostCleanupTimeout = origTimeout })
release := make(chan struct{})
stubDone := make(chan struct{})
origRemoveAll := removeAll
removeAll = func(string) error {
defer close(stubDone)
<-release
return nil
}
// The stuck delete goroutine outlives the timed-out Remove and still reads
// removeAll; unblock it and wait before restoring to avoid a restore/read race.
t.Cleanup(func() {
close(release)
<-stubDone
removeAll = origRemoveAll
})
ctx := common.WithLogger(t.Context(), logging.Default())
base := t.TempDir()
path := filepath.Join(base, "misc", "hostexecutor")
require.NoError(t, os.MkdirAll(path, 0o700))
e := &HostEnvironment{
Path: path,
StdOut: os.Stdout,
}
done := make(chan error, 1)
go func() { done <- e.Remove()(ctx) }()
select {
case err := <-done:
require.NoError(t, err)
case <-time.After(10 * time.Second):
t.Fatal("Remove() hung on a stuck path removal")
} }
} }
@@ -315,17 +211,11 @@ func TestBuildWindowsWorkspaceKillScript(t *testing.T) {
t.Run("single dir", func(t *testing.T) { t.Run("single dir", func(t *testing.T) {
s := buildWindowsWorkspaceKillScript([]string{`C:\workspace\job1`}) s := buildWindowsWorkspaceKillScript([]string{`C:\workspace\job1`})
assert.Contains(t, s, `$paths = @('C:\workspace\job1')`) assert.Contains(t, s, `$paths = @('C:\workspace\job1')`)
// Self-PID guard is essential — without it the script could taskkill
// the PowerShell process running it.
assert.Contains(t, s, "$selfPid = $PID") assert.Contains(t, s, "$selfPid = $PID")
assert.Contains(t, s, "$_.ProcessId -eq $selfPid") assert.Contains(t, s, "$_.ProcessId -eq $selfPid")
// Must match both ExecutablePath (binaries from the workspace) and
// CommandLine (system binaries invoked with workspace paths in args),
// both bounded by dir+separator so a name-prefix sibling is spared.
assert.Contains(t, s, `$prefix = $p + '\'`) assert.Contains(t, s, `$prefix = $p + '\'`)
assert.Contains(t, s, "$_.ExecutablePath.StartsWith($prefix") assert.Contains(t, s, "$_.ExecutablePath.StartsWith($prefix")
assert.Contains(t, s, "$_.CommandLine.IndexOf($prefix") assert.Contains(t, s, "$_.CommandLine.IndexOf($prefix")
// Each matched PID must be tree-killed, not just stopped.
assert.Contains(t, s, "taskkill.exe /PID $_.ProcessId /T /F") assert.Contains(t, s, "taskkill.exe /PID $_.ProcessId /T /F")
}) })
@@ -338,25 +228,16 @@ func TestBuildWindowsWorkspaceKillScript(t *testing.T) {
assert.Contains(t, s, `'C:\work\path'`) assert.Contains(t, s, `'C:\work\path'`)
assert.Contains(t, s, `'C:\work\workdir'`) assert.Contains(t, s, `'C:\work\workdir'`)
assert.Contains(t, s, `'C:\Users\runner\AppData\Local\Temp\job-42'`) assert.Contains(t, s, `'C:\Users\runner\AppData\Local\Temp\job-42'`)
// Commas between entries — no trailing comma, no leading comma.
assert.Contains(t, s, `'C:\work\path','C:\work\workdir',`) assert.Contains(t, s, `'C:\work\path','C:\work\workdir',`)
}) })
t.Run("path with single quote is escaped", func(t *testing.T) { t.Run("path with single quote is escaped", func(t *testing.T) {
// In PowerShell single-quoted strings the only special char is the
// quote itself, escaped by doubling. A workspace path that ever
// contained `'` would inject a command into the script otherwise.
s := buildWindowsWorkspaceKillScript([]string{`C:\work\it's\path`}) s := buildWindowsWorkspaceKillScript([]string{`C:\work\it's\path`})
assert.Contains(t, s, `'C:\work\it''s\path'`) assert.Contains(t, s, `'C:\work\it''s\path'`)
// And it must NOT appear unescaped — otherwise the quote would
// terminate the literal early.
assert.NotContains(t, s, `'C:\work\it's\path'`) assert.NotContains(t, s, `'C:\work\it's\path'`)
}) })
t.Run("path with wildcard metacharacters is matched literally", func(t *testing.T) { t.Run("path with wildcard metacharacters is matched literally", func(t *testing.T) {
// A path containing [ ] ? * must be embedded verbatim and matched with
// ordinal String methods, not -like, otherwise the metacharacters would
// be interpreted as wildcards and the leftover process could escape.
s := buildWindowsWorkspaceKillScript([]string{`C:\work\[job]?1`}) s := buildWindowsWorkspaceKillScript([]string{`C:\work\[job]?1`})
assert.Contains(t, s, `'C:\work\[job]?1'`) assert.Contains(t, s, `'C:\work\[job]?1'`)
assert.NotContains(t, s, "-like") assert.NotContains(t, s, "-like")
@@ -366,7 +247,6 @@ func TestBuildWindowsWorkspaceKillScript(t *testing.T) {
t.Run("empty dir list still produces a valid script", func(t *testing.T) { t.Run("empty dir list still produces a valid script", func(t *testing.T) {
s := buildWindowsWorkspaceKillScript(nil) s := buildWindowsWorkspaceKillScript(nil)
// Empty array literal — script runs, matches nothing, is a no-op.
assert.Contains(t, s, "$paths = @()") assert.Contains(t, s, "$paths = @()")
assert.Contains(t, s, "Get-CimInstance Win32_Process") assert.Contains(t, s, "Get-CimInstance Win32_Process")
}) })
@@ -16,6 +16,8 @@ import (
type LinuxContainerEnvironmentExtensions struct{} type LinuxContainerEnvironmentExtensions struct{}
var windowsPathRegex = regexp.MustCompile(`^([a-zA-Z]):\\(.+)$`)
// Resolves the equivalent host path inside the container // Resolves the equivalent host path inside the container
// This is required for windows and WSL 2 to translate things like C:\Users\Myproject to /mnt/users/Myproject // This is required for windows and WSL 2 to translate things like C:\Users\Myproject to /mnt/users/Myproject
// For use in docker volumes and binds // For use in docker volumes and binds
@@ -31,8 +33,6 @@ func (*LinuxContainerEnvironmentExtensions) ToContainerPath(path string) string
return "" return ""
} }
// Test if the path is a windows path
windowsPathRegex := regexp.MustCompile(`^([a-zA-Z]):\\(.+)$`)
windowsPathComponents := windowsPathRegex.FindStringSubmatch(abspath) windowsPathComponents := windowsPathRegex.FindStringSubmatch(abspath)
// Return as-is if no match // Return as-is if no match
@@ -43,11 +43,7 @@ func (*LinuxContainerEnvironmentExtensions) ToContainerPath(path string) string
// Convert to WSL2-compatible path if it is a windows path // Convert to WSL2-compatible path if it is a windows path
// NOTE: Cannot use filepath because it will use the wrong path separators assuming we want the path to be windows // NOTE: Cannot use filepath because it will use the wrong path separators assuming we want the path to be windows
// based if running on Windows, and because we are feeding this to Docker, GoLang auto-path-translate doesn't work. // based if running on Windows, and because we are feeding this to Docker, GoLang auto-path-translate doesn't work.
driveLetter := strings.ToLower(windowsPathComponents[1]) return "/mnt/" + strings.ToLower(windowsPathComponents[1]) + "/" + strings.ReplaceAll(windowsPathComponents[2], `\`, `/`)
translatedPath := strings.ReplaceAll(windowsPathComponents[2], `\`, `/`)
// Should make something like /mnt/c/Users/person/My Folder/MyActProject
result := strings.Join([]string{"/mnt", driveLetter, translatedPath}, `/`)
return result
} }
func (*LinuxContainerEnvironmentExtensions) GetActPath() string { func (*LinuxContainerEnvironmentExtensions) GetActPath() string {
@@ -5,15 +5,13 @@
package container package container
import ( import (
"fmt"
"os" "os"
"runtime" "runtime"
"strings" "strings"
"testing" "testing"
"gitea.com/gitea/runner/act/common/logging"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestContainerPath(t *testing.T) { func TestContainerPath(t *testing.T) {
@@ -23,42 +21,27 @@ func TestContainerPath(t *testing.T) {
workDir string workDir string
} }
linuxcontainerext := &LinuxContainerEnvironmentExtensions{} cwd, err := os.Getwd()
require.NoError(t, err)
tests := []containerPathJob{
{"/home/act/go/src/github.com/nektos/act", "/home/act/go/src/github.com/nektos/act", ""},
{"/home/act", `/home/act/`, ""},
{cwd, ".", ""},
}
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
cwd, err := os.Getwd()
if err != nil {
logging.Error(err)
}
rootDrive := os.Getenv("SystemDrive") rootDrive := os.Getenv("SystemDrive")
rootDriveLetter := strings.ReplaceAll(strings.ToLower(rootDrive), `:`, "") tests = []containerPathJob{
for _, v := range []containerPathJob{
{"/mnt/c/Users/act/go/src/github.com/nektos/act", "C:\\Users\\act\\go\\src\\github.com\\nektos\\act\\", ""}, {"/mnt/c/Users/act/go/src/github.com/nektos/act", "C:\\Users\\act\\go\\src\\github.com\\nektos\\act\\", ""},
{"/mnt/f/work/dir", `F:\work\dir`, ""}, {"/mnt/f/work/dir", `F:\work\dir`, ""},
{"/mnt/c/windows/to/unix", "windows\\to\\unix", rootDrive + "\\"}, {"/mnt/c/windows/to/unix", "windows\\to\\unix", rootDrive + "\\"},
{fmt.Sprintf("/mnt/%v/act", rootDriveLetter), "act", rootDrive + "\\"}, {"/mnt/" + strings.ReplaceAll(strings.ToLower(rootDrive), `:`, "") + "/act", "act", rootDrive + "\\"},
} {
if v.workDir != "" {
t.Chdir(v.workDir)
}
assert.Equal(t, v.destinationPath, linuxcontainerext.ToContainerPath(v.sourcePath))
} }
}
t.Chdir(cwd) for _, test := range tests {
} else { if test.workDir != "" {
cwd, err := os.Getwd() t.Chdir(test.workDir)
if err != nil {
logging.Error(err)
}
for _, v := range []containerPathJob{
{"/home/act/go/src/github.com/nektos/act", "/home/act/go/src/github.com/nektos/act", ""},
{"/home/act", `/home/act/`, ""},
{cwd, ".", ""},
} {
assert.Equal(t, v.destinationPath, linuxcontainerext.ToContainerPath(v.sourcePath))
} }
assert.Equal(t, test.destinationPath, (&LinuxContainerEnvironmentExtensions{}).ToContainerPath(test.sourcePath))
} }
} }
@@ -67,5 +50,4 @@ type typeAssertMockContainer struct {
LinuxContainerEnvironmentExtensions LinuxContainerEnvironmentExtensions
} }
// Type assert Container + LinuxContainerEnvironmentExtensions implements ExecutionsEnvironment
var _ ExecutionsEnvironment = &typeAssertMockContainer{} var _ ExecutionsEnvironment = &typeAssertMockContainer{}
-1
View File
@@ -75,7 +75,6 @@ func parseEnvFile(e Container, srcPath string, env *map[string]string) common.Ex
if err := s.Err(); err != nil { if err := s.Err(); err != nil {
return fmt.Errorf("reading env file: %w", err) return fmt.Errorf("reading env file: %w", err)
} }
env = &localEnv
return nil return nil
} }
} }
+30 -95
View File
@@ -12,7 +12,6 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"golang.org/x/text/encoding"
"golang.org/x/text/encoding/unicode" "golang.org/x/text/encoding/unicode"
) )
@@ -22,42 +21,40 @@ func newTestHostEnv(t *testing.T) (*HostEnvironment, string) {
return e, filepath.Join(e.Path, "envfile") return e, filepath.Join(e.Path, "envfile")
} }
func TestParseEnvFileSingleLine(t *testing.T) { func TestParseEnvFile(t *testing.T) {
e, envPath := newTestHostEnv(t) largeValue := strings.Repeat("x", 2*1024*1024)
require.NoError(t, os.WriteFile(envPath, []byte("FOO=bar\nBAZ=qux\n"), 0o600)) encodeUTF16 := func(endianness unicode.Endianness) string {
content, err := unicode.UTF16(endianness, unicode.UseBOM).NewEncoder().Bytes([]byte("FOO=bar\r\nMULTI<<EOF\r\nline1\r\nEOF\r\n"))
env := map[string]string{} require.NoError(t, err)
require.NoError(t, parseEnvFile(e, envPath, &env)(t.Context())) return string(content)
assert.Equal(t, "bar", env["FOO"]) }
assert.Equal(t, "qux", env["BAZ"]) for _, test := range []struct {
} name, content string
want map[string]string
func TestParseEnvFileMultiLine(t *testing.T) { }{
e, envPath := newTestHostEnv(t) {"single line", "FOO=bar\nBAZ=qux\n", map[string]string{"FOO": "bar", "BAZ": "qux"}},
content := "FOO<<EOF\nline1\nline2\nEOF\n" {"blank lines", "\nFOO=bar\n\n \nBAZ=qux\n\n", map[string]string{"FOO": "bar", "BAZ": "qux"}},
require.NoError(t, os.WriteFile(envPath, []byte(content), 0o600)) {"multiline", "FOO<<EOF\nline1\nline2\nEOF\n", map[string]string{"FOO": "line1\nline2"}},
{"multiline blank lines", "FOO<<EOF\nline1\n\nline2\nEOF\n", map[string]string{"FOO": "line1\n\nline2"}},
env := map[string]string{} {"multiline leading blank line", "FOO<<EOF\n\nline2\nEOF\n", map[string]string{"FOO": "\nline2"}},
require.NoError(t, parseEnvFile(e, envPath, &env)(t.Context())) {"large value within limit", "FOO<<EOF\n" + largeValue + "\nEOF\n", map[string]string{"FOO": largeValue}},
assert.Equal(t, "line1\nline2", env["FOO"]) {"UTF8 BOM", "\xef\xbb\xbfFOO=bar\n", map[string]string{"FOO": "bar"}},
} {"UTF16 little endian", encodeUTF16(unicode.LittleEndian), map[string]string{"FOO": "bar", "MULTI": "line1"}},
{"UTF16 big endian", encodeUTF16(unicode.BigEndian), map[string]string{"FOO": "bar", "MULTI": "line1"}},
func TestParseEnvFileLargeValueWithinLimit(t *testing.T) { } {
e, envPath := newTestHostEnv(t) t.Run(test.name, func(t *testing.T) {
big := strings.Repeat("x", 2*1024*1024) e, envPath := newTestHostEnv(t)
content := "FOO<<EOF\n" + big + "\nEOF\n" require.NoError(t, os.WriteFile(envPath, []byte(test.content), 0o600))
require.NoError(t, os.WriteFile(envPath, []byte(content), 0o600)) env := map[string]string{}
require.NoError(t, parseEnvFile(e, envPath, &env)(t.Context()))
env := map[string]string{} assert.Equal(t, test.want, env)
require.NoError(t, parseEnvFile(e, envPath, &env)(t.Context())) })
assert.Equal(t, big, env["FOO"]) }
} }
func TestParseEnvFileLineExceedsBufferReportsScannerError(t *testing.T) { func TestParseEnvFileLineExceedsBufferReportsScannerError(t *testing.T) {
e, envPath := newTestHostEnv(t) e, envPath := newTestHostEnv(t)
tooBig := strings.Repeat("x", 17*1024*1024) // over the 16 MiB cap require.NoError(t, os.WriteFile(envPath, []byte("FOO<<EOF\n"+strings.Repeat("x", 17*1024*1024)+"\nEOF\n"), 0o600))
content := "FOO<<EOF\n" + tooBig + "\nEOF\n"
require.NoError(t, os.WriteFile(envPath, []byte(content), 0o600))
env := map[string]string{} env := map[string]string{}
err := parseEnvFile(e, envPath, &env)(t.Context()) err := parseEnvFile(e, envPath, &env)(t.Context())
@@ -65,68 +62,6 @@ func TestParseEnvFileLineExceedsBufferReportsScannerError(t *testing.T) {
assert.Contains(t, err.Error(), "reading env file") assert.Contains(t, err.Error(), "reading env file")
} }
// Regression test: a blank line used to fail the job at "Complete Job", after
// every step had already been recorded as successful.
func TestParseEnvFileBlankLines(t *testing.T) {
e, envPath := newTestHostEnv(t)
require.NoError(t, os.WriteFile(envPath, []byte("\nFOO=bar\n\n \nBAZ=qux\n\n"), 0o600))
env := map[string]string{}
require.NoError(t, parseEnvFile(e, envPath, &env)(t.Context()))
assert.Equal(t, "bar", env["FOO"])
assert.Equal(t, "qux", env["BAZ"])
}
// blank lines inside a heredoc value are content, not separators
func TestParseEnvFileMultiLineKeepsBlankLines(t *testing.T) {
e, envPath := newTestHostEnv(t)
require.NoError(t, os.WriteFile(envPath, []byte("FOO<<EOF\nline1\n\nline2\nEOF\n"), 0o600))
env := map[string]string{}
require.NoError(t, parseEnvFile(e, envPath, &env)(t.Context()))
assert.Equal(t, "line1\n\nline2", env["FOO"])
require.NoError(t, os.WriteFile(envPath, []byte("FOO<<EOF\n\nline2\nEOF\n"), 0o600))
env = map[string]string{}
require.NoError(t, parseEnvFile(e, envPath, &env)(t.Context()))
assert.Equal(t, "\nline2", env["FOO"])
}
func TestParseEnvFileUTF8BOM(t *testing.T) {
e, envPath := newTestHostEnv(t)
content := append([]byte{0xEF, 0xBB, 0xBF}, []byte("FOO=bar\n")...)
require.NoError(t, os.WriteFile(envPath, content, 0o600))
env := map[string]string{}
require.NoError(t, parseEnvFile(e, envPath, &env)(t.Context()))
assert.Equal(t, "bar", env["FOO"])
}
// Windows host mode: PowerShell 5.1 redirection writes UTF-16, which used to be
// unrecognisable as KEY=VALUE, so the writes were silently ignored.
func TestParseEnvFileUTF16(t *testing.T) {
tests := []struct {
name string
encoder *encoding.Encoder
}{
{"little endian", unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewEncoder()},
{"big endian", unicode.UTF16(unicode.BigEndian, unicode.UseBOM).NewEncoder()},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e, envPath := newTestHostEnv(t)
content, err := tt.encoder.Bytes([]byte("FOO=bar\r\nMULTI<<EOF\r\nline1\r\nEOF\r\n"))
require.NoError(t, err)
require.NoError(t, os.WriteFile(envPath, content, 0o600))
env := map[string]string{}
require.NoError(t, parseEnvFile(e, envPath, &env)(t.Context()))
assert.Equal(t, "bar", env["FOO"])
assert.Equal(t, "line1", env["MULTI"])
})
}
}
func TestParseEnvFileMissingDelimiter(t *testing.T) { func TestParseEnvFileMissingDelimiter(t *testing.T) {
e, envPath := newTestHostEnv(t) e, envPath := newTestHostEnv(t)
require.NoError(t, os.WriteFile(envPath, []byte("FOO<<EOF\nline1\nline2\n"), 0o600)) require.NoError(t, os.WriteFile(envPath, []byte("FOO<<EOF\nline1\nline2\n"), 0o600))
-1
View File
@@ -1 +0,0 @@
testfile
+4 -9
View File
@@ -43,7 +43,6 @@ func (tc TarCollector) WriteFile(fpath string, fi fs.FileInfo, linkName string,
// update the name to correctly reflect the desired destination when untaring // update the name to correctly reflect the desired destination when untaring
header.Name = path.Join(tc.DstDir, fpath) header.Name = path.Join(tc.DstDir, fpath)
header.Mode = int64(fi.Mode()) header.Mode = int64(fi.Mode())
header.ModTime = fi.ModTime()
header.Uid = tc.UID header.Uid = tc.UID
header.Gid = tc.GID header.Gid = tc.GID
@@ -58,10 +57,8 @@ func (tc TarCollector) WriteFile(fpath string, fi fs.FileInfo, linkName string,
} }
// copy file data into tar writer // copy file data into tar writer
if _, err := io.Copy(tc.TarWriter, f); err != nil { _, err = io.Copy(tc.TarWriter, f)
return err return err
}
return nil
} }
type CopyCollector struct { type CopyCollector struct {
@@ -87,10 +84,8 @@ func (cc *CopyCollector) WriteFile(fpath string, fi fs.FileInfo, linkName string
return err return err
} }
defer df.Close() defer df.Close()
if _, err := io.Copy(df, f); err != nil { _, err = io.Copy(df, f)
return err return err
}
return nil
} }
type FileCollector struct { type FileCollector struct {
+2 -3
View File
@@ -117,8 +117,7 @@ func SetSha(ctx context.Context, ghc *model.GithubContext, repoPath string) {
if ghc.Sha == "" { if ghc.Sha == "" {
sha, err := pinned(ctx, "sha:"+repoPath, func() (string, error) { sha, err := pinned(ctx, "sha:"+repoPath, func() (string, error) {
_, revision, err := findGitRevision(ctx, repoPath) return findGitRevision(ctx, repoPath)
return revision, err
}) })
if err != nil { if err != nil {
logger.Warnf("unable to get git revision: %v", err) logger.Warnf("unable to get git revision: %v", err)
@@ -139,5 +138,5 @@ func SetRepositoryAndOwner(ctx context.Context, ghc *model.GithubContext, repoPa
} }
ghc.Repository = repo ghc.Repository = repo
} }
ghc.RepositoryOwner = strings.Split(ghc.Repository, "/")[0] ghc.RepositoryOwner, _, _ = strings.Cut(ghc.Repository, "/")
} }
+5 -32
View File
@@ -9,28 +9,18 @@ import (
"errors" "errors"
"testing" "testing"
"gitea.com/gitea/runner/act/common/logging"
"gitea.dev/actionslib/pkg/model" "gitea.dev/actionslib/pkg/model"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestSetRef(t *testing.T) { func TestSetRef(t *testing.T) {
logging.SetLevel(logging.LevelDebug)
oldFindGitRef := findGitRef oldFindGitRef := findGitRef
oldFindGitRevision := findGitRevision
defer func() { findGitRef = oldFindGitRef }() defer func() { findGitRef = oldFindGitRef }()
defer func() { findGitRevision = oldFindGitRevision }()
findGitRef = func(ctx context.Context, file string) (string, error) { findGitRef = func(_ctx context.Context, _file string) (string, error) {
return "refs/heads/master", nil return "refs/heads/master", nil
} }
findGitRevision = func(ctx context.Context, file string) (string, string, error) {
return "", "1234fakesha", nil
}
tables := []struct { tables := []struct {
eventName string eventName string
event map[string]any event map[string]any
@@ -114,7 +104,7 @@ func TestSetRef(t *testing.T) {
} }
t.Run("no-default-branch", func(t *testing.T) { t.Run("no-default-branch", func(t *testing.T) {
findGitRef = func(ctx context.Context, file string) (string, error) { findGitRef = func(_ctx context.Context, _file string) (string, error) {
return "", errors.New("no default branch") return "", errors.New("no default branch")
} }
@@ -130,19 +120,13 @@ func TestSetRef(t *testing.T) {
} }
func TestSetSha(t *testing.T) { func TestSetSha(t *testing.T) {
logging.SetLevel(logging.LevelDebug)
oldFindGitRef := findGitRef oldFindGitRef := findGitRef
oldFindGitRevision := findGitRevision oldFindGitRevision := findGitRevision
defer func() { findGitRef = oldFindGitRef }() defer func() { findGitRef = oldFindGitRef }()
defer func() { findGitRevision = oldFindGitRevision }() defer func() { findGitRevision = oldFindGitRevision }()
findGitRef = func(ctx context.Context, file string) (string, error) { findGitRevision = func(_ctx context.Context, _file string) (string, error) {
return "refs/heads/master", nil return "1234fakesha", nil
}
findGitRevision = func(ctx context.Context, file string) (string, string, error) {
return "", "1234fakesha", nil
} }
tables := []struct { tables := []struct {
@@ -177,11 +161,6 @@ func TestSetSha(t *testing.T) {
}, },
sha: "deployment-sha", sha: "deployment-sha",
}, },
{
eventName: "release",
event: map[string]any{},
sha: "1234fakesha",
},
{ {
eventName: "push", eventName: "push",
event: map[string]any{ event: map[string]any{
@@ -195,18 +174,12 @@ func TestSetSha(t *testing.T) {
event: map[string]any{}, event: map[string]any{},
sha: "1234fakesha", sha: "1234fakesha",
}, },
{
eventName: "no-event",
event: map[string]any{},
sha: "1234fakesha",
},
} }
for _, table := range tables { for _, table := range tables {
t.Run(table.eventName, func(t *testing.T) { t.Run(table.eventName, func(t *testing.T) {
ghc := &model.GithubContext{ ghc := &model.GithubContext{
EventName: table.eventName, EventName: table.eventName,
BaseRef: "master",
Event: table.event, Event: table.event,
} }
@@ -219,7 +192,7 @@ func TestSetSha(t *testing.T) {
t.Run("pinned checkout keeps the first sha and ref", func(t *testing.T) { t.Run("pinned checkout keeps the first sha and ref", func(t *testing.T) {
ctx := WithPinnedCheckout(t.Context()) ctx := WithPinnedCheckout(t.Context())
for _, head := range []string{"first", "moved"} { for _, head := range []string{"first", "moved"} {
findGitRevision = func(context.Context, string) (string, string, error) { return "", head, nil } findGitRevision = func(context.Context, string) (string, error) { return head, nil }
findGitRef = func(context.Context, string) (string, error) { return "refs/heads/" + head, nil } findGitRef = func(context.Context, string) (string, error) { return "refs/heads/" + head, nil }
ghc := &model.GithubContext{Event: map[string]any{}} ghc := &model.GithubContext{Event: map[string]any{}}
SetSha(ctx, ghc, "/some/dir") SetSha(ctx, ghc, "/some/dir")
+11 -23
View File
@@ -13,35 +13,23 @@ import (
"testing" "testing"
) )
func TestLookPath2SearchesPathAndEmptyElement(t *testing.T) { func TestLookPath2SearchesPathAndEmptyElementOrUsesDirectPath(t *testing.T) {
dir := t.TempDir() dir := t.TempDir()
exe := filepath.Join(dir, "tool") exe := filepath.Join(dir, "tool")
if err := os.WriteFile(exe, []byte("#!/bin/sh\n"), 0o755); err != nil { if err := os.WriteFile(exe, []byte("#!/bin/sh\n"), 0o755); err != nil {
t.Fatal(err) t.Fatal(err)
} }
got, err := LookPath2("tool", map[string]string{"PATH": string(filepath.ListSeparator) + dir}) for file, searchPath := range map[string]string{"tool": string(filepath.ListSeparator) + dir, exe: ""} {
if err != nil { t.Run(file, func(t *testing.T) {
t.Fatal(err) got, err := LookPath2(file, map[string]string{"PATH": searchPath})
} if err != nil {
if got != exe { t.Fatal(err)
t.Fatalf("LookPath2() = %q, want %q", got, exe) }
} if got != exe {
} t.Fatalf("LookPath2() = %q, want %q", got, exe)
}
func TestLookPath2DirectPathDoesNotSearchPath(t *testing.T) { })
dir := t.TempDir()
exe := filepath.Join(dir, "tool")
if err := os.WriteFile(exe, []byte("#!/bin/sh\n"), 0o755); err != nil {
t.Fatal(err)
}
got, err := LookPath2(exe, map[string]string{"PATH": ""})
if err != nil {
t.Fatal(err)
}
if got != exe {
t.Fatalf("LookPath2() = %q, want %q", got, exe)
} }
} }
+3 -9
View File
@@ -28,21 +28,15 @@ func chkStat(file string) error {
} }
func hasExt(file string) bool { func hasExt(file string) bool {
i := strings.LastIndex(file, ".") return strings.LastIndex(file, ".") > strings.LastIndexAny(file, `:\/`)
if i < 0 {
return false
}
return strings.LastIndexAny(file, `:\/`) < i
} }
func findExecutable(file string, exts []string) (string, error) { func findExecutable(file string, exts []string) (string, error) {
if len(exts) == 0 { if len(exts) == 0 {
return file, chkStat(file) return file, chkStat(file)
} }
if hasExt(file) { if hasExt(file) && chkStat(file) == nil {
if chkStat(file) == nil { return file, nil
return file, nil
}
} }
for _, e := range exts { for _, e := range exts {
if f := file + e; chkStat(f) == nil { if f := file + e; chkStat(f) == nil {
+44 -66
View File
@@ -8,7 +8,6 @@ import (
"context" "context"
"crypto/rand" "crypto/rand"
"crypto/sha256" "crypto/sha256"
"embed"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt" "fmt"
@@ -26,6 +25,8 @@ import (
"gitea.com/gitea/runner/act/common/git" "gitea.com/gitea/runner/act/common/git"
"gitea.com/gitea/runner/act/container" "gitea.com/gitea/runner/act/container"
_ "embed"
"gitea.dev/actionslib/pkg/model" "gitea.dev/actionslib/pkg/model"
"github.com/kballard/go-shellquote" "github.com/kballard/go-shellquote"
) )
@@ -47,11 +48,13 @@ type fileWriter func(filename string, data []byte, perm fs.FileMode) error
type runAction func(step actionStep, actionDir string, remoteAction *remoteAction) common.Executor type runAction func(step actionStep, actionDir string, remoteAction *remoteAction) common.Executor
//go:embed res/trampoline.js //go:embed res/trampoline.js
var trampoline embed.FS var trampoline string
var ( var (
ContainerImageExistsLocally = container.ImageExistsLocally ContainerImageExistsLocally = container.ImageExistsLocally
ContainerNewDockerBuildExecutor = container.NewDockerBuildExecutor ContainerNewDockerBuildExecutor = container.NewDockerBuildExecutor
dockerActionNamePattern = regexp.MustCompile("[^a-zA-Z0-9]")
inputEnvPattern = regexp.MustCompile("[^A-Z0-9-]")
) )
func readActionImpl(ctx context.Context, step *model.Step, actionDir, actionPath string, readFile actionYamlReader, writeFile fileWriter) (*model.Action, error) { func readActionImpl(ctx context.Context, step *model.Step, actionDir, actionPath string, readFile actionYamlReader, writeFile fileWriter) (*model.Action, error) {
@@ -85,38 +88,31 @@ func readActionImpl(ctx context.Context, step *model.Step, actionDir, actionPath
logger.Debugf("Using synthetic action %v for Dockerfile", action) logger.Debugf("Using synthetic action %v for Dockerfile", action)
return action, nil return action, nil
} }
if step.With != nil { if val, ok := step.With["args"]; ok {
if val, ok := step.With["args"]; ok { if err := writeFile(filepath.Join(actionDir, actionPath, "trampoline.js"), []byte(trampoline), 0o644); err != nil {
var b []byte return nil, err
if b, err = trampoline.ReadFile("res/trampoline.js"); err != nil {
return nil, err
}
err2 := writeFile(filepath.Join(actionDir, actionPath, "trampoline.js"), b, 0o644)
if err2 != nil {
return nil, err2
}
action := &model.Action{
Name: "(Synthetic)",
Inputs: map[string]model.Input{
"cwd": {
Description: "(Actual working directory)",
Required: false,
Default: filepath.Join(actionDir, actionPath),
},
"command": {
Description: "(Actual program)",
Required: false,
Default: val,
},
},
Runs: model.ActionRuns{
Using: "node12",
Main: "trampoline.js",
},
}
logger.Debugf("Using synthetic action %v", action)
return action, nil
} }
action := &model.Action{
Name: "(Synthetic)",
Inputs: map[string]model.Input{
"cwd": {
Description: "(Actual working directory)",
Required: false,
Default: filepath.Join(actionDir, actionPath),
},
"command": {
Description: "(Actual program)",
Required: false,
Default: val,
},
},
Runs: model.ActionRuns{
Using: "node12",
Main: "trampoline.js",
},
}
logger.Debugf("Using synthetic action %v", action)
return action, nil
} }
} }
} }
@@ -125,8 +121,7 @@ func readActionImpl(ctx context.Context, step *model.Step, actionDir, actionPath
} }
defer closer.Close() defer closer.Close()
action, err := model.ReadAction(reader) return model.ReadAction(reader)
return action, err
} }
func maybeCopyToActionDir(ctx context.Context, step actionStep, actionDir, actionPath, containerActionDir string) error { func maybeCopyToActionDir(ctx context.Context, step actionStep, actionDir, actionPath, containerActionDir string) error {
@@ -166,7 +161,7 @@ func runActionImpl(step actionStep, actionDir string, remoteAction *remoteAction
return func(ctx context.Context) error { return func(ctx context.Context) error {
logger := common.Logger(ctx) logger := common.Logger(ctx)
actionPath := "" actionPath := ""
if remoteAction != nil && remoteAction.Path != "" { if remoteAction != nil {
actionPath = remoteAction.Path actionPath = remoteAction.Path
} }
@@ -178,7 +173,6 @@ func runActionImpl(step actionStep, actionDir string, remoteAction *remoteAction
return err return err
} }
actionLocation := path.Join(actionDir, actionPath)
actionName, containerActionDir := getContainerActionPaths(step) actionName, containerActionDir := getContainerActionPaths(step)
logger.Debugf("type=%v actionDir=%s actionPath=%s workdir=%s actionCacheDir=%s actionName=%s containerActionDir=%s", stepModel.Type(), actionDir, actionPath, rc.Config.Workdir, rc.ActionCacheDir(), actionName, containerActionDir) logger.Debugf("type=%v actionDir=%s actionPath=%s workdir=%s actionCacheDir=%s actionName=%s containerActionDir=%s", stepModel.Type(), actionDir, actionPath, rc.Config.Workdir, rc.ActionCacheDir(), actionName, containerActionDir)
@@ -196,7 +190,7 @@ func runActionImpl(step actionStep, actionDir string, remoteAction *remoteAction
return rc.JobContainer.Exec(containerArgs, *step.getEnv(), "", "")(ctx) return rc.JobContainer.Exec(containerArgs, *step.getEnv(), "", "")(ctx)
case x.IsDocker(): case x.IsDocker():
location := actionLocation location := path.Join(actionDir, actionPath)
if remoteAction == nil { if remoteAction == nil {
location = containerActionDir location = containerActionDir
} }
@@ -215,12 +209,9 @@ func runActionImpl(step actionStep, actionDir string, remoteAction *remoteAction
rc.ApplyExtraPath(ctx, step.getEnv()) rc.ApplyExtraPath(ctx, step.getEnv())
execFileName := action.Runs.Main + ".out" execFileName := action.Runs.Main + ".out"
buildArgs := []string{"go", "build", "-o", execFileName, action.Runs.Main}
execArgs := []string{filepath.Join(containerActionDir, execFileName)}
return common.NewPipelineExecutor( return common.NewPipelineExecutor(
rc.JobContainer.Exec(buildArgs, *step.getEnv(), "", containerActionDir), rc.JobContainer.Exec([]string{"go", "build", "-o", execFileName, action.Runs.Main}, *step.getEnv(), "", containerActionDir),
rc.JobContainer.Exec(execArgs, *step.getEnv(), "", ""), rc.JobContainer.Exec([]string{filepath.Join(containerActionDir, execFileName)}, *step.getEnv(), "", ""),
)(ctx) )(ctx)
default: default:
return fmt.Errorf("the runs.using key must be one of: %v, got %s", []string{ return fmt.Errorf("the runs.using key must be one of: %v, got %s", []string{
@@ -259,16 +250,14 @@ func dockerActionImageTag(repository, actionName string, localAction bool) strin
name = path.Join(repository, actionName) name = path.Join(repository, actionName)
} }
// The human-readable name is sanitized by collapsing every non-alphanumeric character to "-". // The human-readable name is sanitized by collapsing every non-alphanumeric character to "-".
sanitized := regexp.MustCompile("[^a-zA-Z0-9]").ReplaceAllString(name, "-") sanitized := dockerActionNamePattern.ReplaceAllString(name, "-")
if localAction { if localAction {
// For local actions a short hash of the raw repository and action path is appended so the tag stays unique per repository. // For local actions a short hash of the raw repository and action path is appended so the tag stays unique per repository.
sum := sha256.Sum256([]byte(repository + "\x00" + actionName)) sum := sha256.Sum256([]byte(repository + "\x00" + actionName))
sanitized += "-" + hex.EncodeToString(sum[:])[:12] sanitized += "-" + hex.EncodeToString(sum[:])[:12]
} }
// "-dockeraction" ensures that "./", "./test " won't get converted to "act-:latest", "act-test-:latest" which are invalid docker image names // "-dockeraction" ensures that "./", "./test " won't get converted to "act-:latest", "act-test-:latest" which are invalid docker image names
image := fmt.Sprintf("%s-dockeraction:%s", sanitized, "latest") return strings.ToLower("act-" + strings.TrimLeft(sanitized+"-dockeraction:latest", "-"))
image = "act-" + strings.TrimLeft(image, "-")
return strings.ToLower(image)
} }
// One image per step and job: its stages share a build, and concurrent jobs never run each other's. // One image per step and job: its stages share a build, and concurrent jobs never run each other's.
@@ -485,20 +474,15 @@ func newStepContainer(ctx context.Context, step step, image string, cmd, entrypo
} }
func populateEnvsFromSavedState(env *map[string]string, step actionStep, rc *RunContext) { func populateEnvsFromSavedState(env *map[string]string, step actionStep, rc *RunContext) {
state, ok := rc.IntraActionState[step.getStepModel().ID] for name, value := range rc.IntraActionState[step.getStepModel().ID] {
if ok { (*env)["STATE_"+name] = value
for name, value := range state {
envName := "STATE_" + name
(*env)[envName] = value
}
} }
} }
func populateEnvsFromInput(ctx context.Context, env *map[string]string, action *model.Action, rc *RunContext) error { func populateEnvsFromInput(ctx context.Context, env *map[string]string, action *model.Action, rc *RunContext) error {
eval := rc.NewExpressionEvaluator(ctx) eval := rc.NewExpressionEvaluator(ctx)
for inputID, input := range action.Inputs { for inputID, input := range action.Inputs {
envKey := regexp.MustCompile("[^A-Z0-9-]").ReplaceAllString(strings.ToUpper(inputID), "_") envKey := "INPUT_" + inputEnvPattern.ReplaceAllString(strings.ToUpper(inputID), "_")
envKey = "INPUT_" + envKey
if _, ok := (*env)[envKey]; !ok { if _, ok := (*env)[envKey]; !ok {
var err error var err error
if (*env)[envKey], err = eval.Interpolate(ctx, input.Default); err != nil { if (*env)[envKey], err = eval.Interpolate(ctx, input.Default); err != nil {
@@ -637,12 +621,9 @@ func runPreStep(step actionStep) common.Executor {
rc.ApplyExtraPath(ctx, step.getEnv()) rc.ApplyExtraPath(ctx, step.getEnv())
execFileName := action.Runs.Pre + ".out" execFileName := action.Runs.Pre + ".out"
buildArgs := []string{"go", "build", "-o", execFileName, action.Runs.Pre}
execArgs := []string{filepath.Join(containerActionDir, execFileName)}
return common.NewPipelineExecutor( return common.NewPipelineExecutor(
rc.JobContainer.Exec(buildArgs, *step.getEnv(), "", containerActionDir), rc.JobContainer.Exec([]string{"go", "build", "-o", execFileName, action.Runs.Pre}, *step.getEnv(), "", containerActionDir),
rc.JobContainer.Exec(execArgs, *step.getEnv(), "", ""), rc.JobContainer.Exec([]string{filepath.Join(containerActionDir, execFileName)}, *step.getEnv(), "", ""),
)(ctx) )(ctx)
default: default:
return nil return nil
@@ -653,7 +634,7 @@ func runPreStep(step actionStep) common.Executor {
func shouldRunPostStep(step actionStep) common.Conditional { func shouldRunPostStep(step actionStep) common.Conditional {
return func(ctx context.Context) bool { return func(ctx context.Context) bool {
log := common.Logger(ctx) log := common.Logger(ctx)
stepResults := step.getRunContext().getStepsContext() stepResults := step.getRunContext().StepResults
stepResult := stepResults[step.getStepModel().ID] stepResult := stepResults[step.getStepModel().ID]
if stepResult == nil { if stepResult == nil {
@@ -731,12 +712,9 @@ func runPostStep(step actionStep) common.Executor {
rc.ApplyExtraPath(ctx, step.getEnv()) rc.ApplyExtraPath(ctx, step.getEnv())
execFileName := action.Runs.Post + ".out" execFileName := action.Runs.Post + ".out"
buildArgs := []string{"go", "build", "-o", execFileName, action.Runs.Post}
execArgs := []string{filepath.Join(containerActionDir, execFileName)}
return common.NewPipelineExecutor( return common.NewPipelineExecutor(
rc.JobContainer.Exec(buildArgs, *step.getEnv(), "", containerActionDir), rc.JobContainer.Exec([]string{"go", "build", "-o", execFileName, action.Runs.Post}, *step.getEnv(), "", containerActionDir),
rc.JobContainer.Exec(execArgs, *step.getEnv(), "", ""), rc.JobContainer.Exec([]string{filepath.Join(containerActionDir, execFileName)}, *step.getEnv(), "", ""),
)(ctx) )(ctx)
default: default:
+8 -13
View File
@@ -8,7 +8,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"regexp"
"slices" "slices"
"strconv" "strconv"
"strings" "strings"
@@ -32,8 +31,7 @@ func evaluateCompositeInputAndEnv(ctx context.Context, parent *RunContext, step
ee := parent.NewStepExpressionEvaluator(ctx, step) ee := parent.NewStepExpressionEvaluator(ctx, step)
for inputID, input := range step.getActionModel().Inputs { for inputID, input := range step.getActionModel().Inputs {
envKey := regexp.MustCompile("[^A-Z0-9-]").ReplaceAllString(strings.ToUpper(inputID), "_") envKey := "INPUT_" + inputEnvPattern.ReplaceAllString(strings.ToUpper(inputID), "_")
envKey = "INPUT_" + strings.ToUpper(envKey)
// lookup if key is defined in the step but the already // lookup if key is defined in the step but the already
// evaluated value from the environment // evaluated value from the environment
@@ -211,19 +209,16 @@ func (rc *RunContext) compositeExecutor(action *model.Action) *compositeSteps {
} }
stepID := step.getStepModel().ID stepID := step.getStepModel().ID
stepPre := rc.newCompositeCommandExecutor(step.pre()) preSteps = append(preSteps, newCompositeStepLogExecutor(rc.newCompositeCommandExecutor(step.pre()), stepID))
preSteps = append(preSteps, newCompositeStepLogExecutor(stepPre, stepID))
steps = append(steps, newCompositeStepLogExecutor(rc.newCompositeCommandExecutor(step.main()), stepID)) steps = append(steps, newCompositeStepLogExecutor(rc.newCompositeCommandExecutor(step.main()), stepID))
// run the post executor in reverse order // run the post executor in reverse order
stepPost := rc.newCompositeCommandExecutor(step.post())
if postExecutor != nil { if postExecutor != nil {
stepPost := rc.newCompositeCommandExecutor(step.post()) stepPost = stepPost.Finally(postExecutor)
postExecutor = newCompositeStepLogExecutor(stepPost.Finally(postExecutor), stepID)
} else {
stepPost := rc.newCompositeCommandExecutor(step.post())
postExecutor = newCompositeStepLogExecutor(stepPost, stepID)
} }
postExecutor = newCompositeStepLogExecutor(stepPost, stepID)
} }
steps = append(steps, common.JobError) steps = append(steps, common.JobError)
@@ -250,12 +245,12 @@ func newCompositeStepLogExecutor(runStep common.Executor, stepID string) common.
ctx = WithCompositeStepLogger(ctx, stepID) ctx = WithCompositeStepLogger(ctx, stepID)
logger := common.Logger(ctx) logger := common.Logger(ctx)
err := runStep(ctx) err := runStep(ctx)
if err == nil {
err = ctx.Err()
}
if err != nil { if err != nil {
logger.Errorf("##[error]%s", EscapeCommandData(err.Error())) logger.Errorf("##[error]%s", EscapeCommandData(err.Error()))
common.SetJobError(ctx, err) common.SetJobError(ctx, err)
} else if ctx.Err() != nil {
logger.Errorf("##[error]%s", EscapeCommandData(ctx.Err().Error()))
common.SetJobError(ctx, ctx.Err())
} }
return nil return nil
} }
+2 -28
View File
@@ -71,9 +71,9 @@ func TestAppendUniqueMasks(t *testing.T) {
want []string want []string
}{ }{
{ {
name: "appends new masks", name: "appends each new mask once",
dst: []string{"a"}, dst: []string{"a"},
src: []string{"b", "c"}, src: []string{"b", "c", "b", "a"},
want: []string{"a", "b", "c"}, want: []string{"a", "b", "c"},
}, },
{ {
@@ -82,12 +82,6 @@ func TestAppendUniqueMasks(t *testing.T) {
src: []string{"a", "b"}, src: []string{"a", "b"},
want: []string{"a", "b"}, want: []string{"a", "b"},
}, },
{
name: "deduplicates within src",
dst: []string{"a"},
src: []string{"b", "b", "a"},
want: []string{"a", "b"},
},
{ {
name: "empty src leaves dst unchanged", name: "empty src leaves dst unchanged",
dst: []string{"a"}, dst: []string{"a"},
@@ -103,26 +97,6 @@ func TestAppendUniqueMasks(t *testing.T) {
} }
} }
// TestAppendUniqueMasksNoExponentialGrowth reproduces the exponential growth of
// the parent's Masks slice observed with nested/repeated composite actions. A
// composite RunContext is seeded with its parent's masks and the whole seeded
// slice was previously appended back into the parent, doubling its length on
// every composite action.
func TestAppendUniqueMasksNoExponentialGrowth(t *testing.T) {
parentMasks := []string{"secret"}
for range 20 {
// compositeRC.Masks starts as a copy of the parent's masks (it is
// seeded with parent.Masks in newCompositeRunContext).
compositeMasks := make([]string, len(parentMasks))
copy(compositeMasks, parentMasks)
parentMasks = appendUniqueMasks(parentMasks, compositeMasks)
}
assert.Equal(t, []string{"secret"}, parentMasks)
}
func TestCompositeStepIfReadsJobStatusExceptInItsOwnMainSteps(t *testing.T) { func TestCompositeStepIfReadsJobStatusExceptInItsOwnMainSteps(t *testing.T) {
failStep := func(rc *RunContext) { failStep := func(rc *RunContext) {
rc.StepResults["failed"] = &model.StepResult{Conclusion: model.StepStatusFailure} rc.StepResults["failed"] = &model.StepResult{Conclusion: model.StepStatusFailure}
+26 -96
View File
@@ -126,7 +126,7 @@ runs:
return strings.NewReader(tt.fileContent), closerFunc(func() { closed = true }), nil return strings.NewReader(tt.fileContent), closerFunc(func() { closed = true }), nil
} }
writeFile := func(filename string, data []byte, perm fs.FileMode) error { writeFile := func(filename string, _data []byte, perm fs.FileMode) error {
assert.Equal(t, "actionDir/actionPath/trampoline.js", filename) assert.Equal(t, "actionDir/actionPath/trampoline.js", filename)
assert.Equal(t, fs.FileMode(0o644), perm) assert.Equal(t, fs.FileMode(0o644), perm)
return nil return nil
@@ -134,7 +134,7 @@ runs:
action, err := readActionImpl(t.Context(), tt.step, "actionDir", "actionPath", readFile, writeFile) action, err := readActionImpl(t.Context(), tt.step, "actionDir", "actionPath", readFile, writeFile)
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act require.NoError(t, err)
assert.Equal(t, tt.expected, action) assert.Equal(t, tt.expected, action)
assert.Equal(t, tt.filename != "", closed) assert.Equal(t, tt.filename != "", closed)
@@ -144,94 +144,27 @@ runs:
func TestActionRunner(t *testing.T) { func TestActionRunner(t *testing.T) {
t.Chdir(t.TempDir()) t.Chdir(t.TempDir())
table := []struct { for _, tt := range []struct {
name string name string
step actionStep inputs map[string]model.Input
state map[string]string
expectedEnv map[string]string expectedEnv map[string]string
}{ }{
{ {
name: "with-input", name: "with-input",
step: &stepActionRemote{ inputs: map[string]model.Input{"key": {Default: "default value"}},
Step: &model.Step{
Uses: "org/repo/path@ref",
},
RunContext: &RunContext{
Config: &Config{},
Run: &model.Run{
JobID: "job",
Workflow: &model.Workflow{
Jobs: map[string]*model.Job{
"job": {
Name: "job",
},
},
},
},
},
action: &model.Action{
Inputs: map[string]model.Input{
"key": {
Default: "default value",
},
},
Runs: model.ActionRuns{
Using: "node16",
},
},
env: map[string]string{},
remoteAction: mustNewRemoteAction(t, "org/repo/path@ref"),
},
expectedEnv: map[string]string{"INPUT_KEY": "default value"}, expectedEnv: map[string]string{"INPUT_KEY": "default value"},
}, },
{ {
name: "restore-saved-state", name: "restore-saved-state",
step: &stepActionRemote{ state: map[string]string{"name": "state value"},
Step: &model.Step{
ID: "step",
Uses: "org/repo/path@ref",
},
RunContext: &RunContext{
ActionPath: "path",
Config: &Config{},
Run: &model.Run{
JobID: "job",
Workflow: &model.Workflow{
Jobs: map[string]*model.Job{
"job": {
Name: "job",
},
},
},
},
CurrentStep: "post-step",
StepResults: map[string]*model.StepResult{
"step": {},
},
IntraActionState: map[string]map[string]string{
"step": {
"name": "state value",
},
},
},
action: &model.Action{
Runs: model.ActionRuns{
Using: "node16",
},
},
env: map[string]string{},
remoteAction: mustNewRemoteAction(t, "org/repo/path@ref"),
},
expectedEnv: map[string]string{"STATE_name": "state value"}, expectedEnv: map[string]string{"STATE_name": "state value"},
}, },
} } {
for _, tt := range table {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
ctx := t.Context()
cm := &containerMock{} cm := &containerMock{}
containerActionDir := "/var/run/act/actions/" + model.UsesHash("/org/repo/path@ref") + "/" containerActionDir := "/var/run/act/actions/" + model.UsesHash("/org/repo/path@ref") + "/"
cm.On("CopyDir", containerActionDir, "dir/", false, true).Return(func(ctx context.Context) error { return nil }) cm.On("CopyDir", containerActionDir, "dir/", false, true).Return(func(_ctx context.Context) error { return nil })
envMatcher := mock.MatchedBy(func(env map[string]string) bool { envMatcher := mock.MatchedBy(func(env map[string]string) bool {
for k, v := range tt.expectedEnv { for k, v := range tt.expectedEnv {
@@ -241,14 +174,22 @@ func TestActionRunner(t *testing.T) {
} }
return true return true
}) })
cm.On("Exec", []string{"node", "--preserve-symlinks-main", containerActionDir + "path"}, envMatcher, "", "").Return(func(_ctx context.Context) error { return nil })
cm.On("Exec", []string{"node", "--preserve-symlinks-main", containerActionDir + "path"}, envMatcher, "", "").Return(func(ctx context.Context) error { return nil }) remoteAction := mustNewRemoteAction(t, "org/repo/path@ref")
step := &stepActionRemote{
tt.step.getRunContext().JobContainer = cm Step: &model.Step{ID: "step", Uses: "org/repo/path@ref"},
RunContext: &RunContext{
err := runActionImpl(tt.step, "dir", mustNewRemoteAction(t, "org/repo/path@ref"))(ctx) Config: &Config{},
Run: &model.Run{JobID: "job", Workflow: &model.Workflow{Jobs: map[string]*model.Job{"job": {}}}},
assert.NoError(t, err) //nolint:testifylint // pre-existing issue from nektos/act IntraActionState: map[string]map[string]string{"step": tt.state},
JobContainer: cm,
},
action: &model.Action{Inputs: tt.inputs, Runs: model.ActionRuns{Using: "node16"}},
env: map[string]string{},
remoteAction: remoteAction,
}
require.NoError(t, runActionImpl(step, "dir", remoteAction)(t.Context()))
cm.AssertExpectations(t) cm.AssertExpectations(t)
}) })
} }
@@ -322,7 +263,6 @@ func TestNewStepContainerDoesNotUseDockerSecrets(t *testing.T) {
newStepContainer(ctx, step, "registry.example.com/action:tag", nil, nil, "") newStepContainer(ctx, step, "registry.example.com/action:tag", nil, nil, "")
// DOCKER_USERNAME/DOCKER_PASSWORD should not be injected as pull credentials for docker action containers.
assert.Empty(t, captured.Username) assert.Empty(t, captured.Username)
assert.Empty(t, captured.Password) assert.Empty(t, captured.Password)
assert.True(t, captured.AutoRemove) assert.True(t, captured.AutoRemove)
@@ -478,15 +418,11 @@ func TestExecAsDockerHoldsCloneLockForRemoteUncached(t *testing.T) {
} }
func TestDockerActionImageTag(t *testing.T) { func TestDockerActionImageTag(t *testing.T) {
// Remote actions already carry a unique, ref-scoped actionName (the uses
// hash), so the tag must be left untouched for backwards compatibility.
assert.Equal(t, assert.Equal(t,
"act-abc123-dockeraction:latest", "act-abc123-dockeraction:latest",
dockerActionImageTag("owner/repo", "abc123", false), dockerActionImageTag("owner/repo", "abc123", false),
) )
// Local actions keep a human-readable, repository-namespaced prefix and gain a short hash suffix that makes the tag unique per (repository, actionName).
// See https://gitea.com/gitea/runner/issues/1039.
assert.Equal(t, assert.Equal(t,
"act-owner-repo-baca2daaa2fe-dockeraction:latest", "act-owner-repo-baca2daaa2fe-dockeraction:latest",
dockerActionImageTag("owner/repo", "./", true), dockerActionImageTag("owner/repo", "./", true),
@@ -496,16 +432,11 @@ func TestDockerActionImageTag(t *testing.T) {
dockerActionImageTag("owner/repo", "./sub", true), dockerActionImageTag("owner/repo", "./sub", true),
) )
// Sanitizing every non-alphanumeric character to "-" is lossy, so distinct inputs can collapse to the same readable prefix.
// The hash suffix must keep such cases apart, otherwise an image built for one repository is reused for another.
collisions := [][2]struct { collisions := [][2]struct {
repoName string repoName string
actionName string actionName string
}{ }{
// Two different repositories, both `uses: ./`: "a/b-c" and "a-b/c" both sanitize to "a-b-c".
{{"a/b-c", "./"}, {"a-b/c", "./"}}, {{"a/b-c", "./"}, {"a-b/c", "./"}},
// A repository's root action vs another repository's sub-path action:
// "owner/repo-a" + "./" and "owner/repo" + "./a" both sanitize to "owner-repo-a".
{{"owner/repo-a", "./"}, {"owner/repo", "./a"}}, {{"owner/repo-a", "./"}, {"owner/repo", "./a"}},
} }
for _, c := range collisions { for _, c := range collisions {
@@ -517,7 +448,6 @@ func TestDockerActionImageTag(t *testing.T) {
) )
} }
// Distinct local actions within the same repository keep distinct tags.
assert.NotEqual(t, assert.NotEqual(t,
dockerActionImageTag("owner/repo", "./", true), dockerActionImageTag("owner/repo", "./", true),
dockerActionImageTag("owner/repo", "./sub", true), dockerActionImageTag("owner/repo", "./sub", true),
+79 -131
View File
@@ -66,145 +66,93 @@ func TestCancelledJobStatusEnablesAlwaysAndCancelledSteps(t *testing.T) {
assert.Equal(t, "cancelled", rc.Run.Job().Result) assert.Equal(t, "cancelled", rc.Run.Job().Result)
} }
// TestMainStepsExecutorRunsAlwaysStepsAfterCancel verifies that newMainStepsExecutor does func TestMainStepsExecutorRunsAlwaysStepsAfterInterruption(t *testing.T) {
// not abandon the remaining steps when the run is cancelled mid-pipeline. The later step for name, wantError := range map[string]error{"cancel": context.Canceled, "timeout": context.DeadlineExceeded} {
// still runs (so a main-stage always() step is reached), it runs under a fresh, t.Run(name, func(t *testing.T) {
// non-cancelled context, and the job is marked cancelled. The interrupt error is still rc := createIfTestRunContext(map[string]*model.Job{
// propagated so callers up the chain see the cancellation. "job1": createJob(t, `runs-on: ubuntu-latest`, ""),
func TestMainStepsExecutorRunsAlwaysStepsAfterCancel(t *testing.T) { })
rc := createIfTestRunContext(map[string]*model.Job{ var ctx context.Context
"job1": createJob(t, `runs-on: ubuntu-latest`, ""), var interrupt func()
}) if wantError == context.Canceled {
ctx, interrupt = context.WithCancel(t.Context())
defer interrupt()
} else {
deadline := newControllableDeadlineContext(t.Context())
ctx, interrupt = deadline, deadline.expire
}
ctx, cancel := context.WithCancel(t.Context()) var ran []string
defer cancel() var laterStepCtxErr error
err := newMainStepsExecutor(rc, []common.Executor{
func(_ctx context.Context) error {
ran = append(ran, "step1")
interrupt()
return nil
},
func(ctx context.Context) error {
ran = append(ran, "always-step")
laterStepCtxErr = ctx.Err()
return nil
},
})(ctx)
var ran []string require.ErrorIs(t, err, wantError)
var laterStepCtxErr error assert.Equal(t, []string{"step1", "always-step"}, ran)
steps := []common.Executor{ require.NoError(t, laterStepCtxErr)
func(_ context.Context) error { assert.Equal(t, wantError == context.Canceled, rc.jobCancelled)
ran = append(ran, "step1") if wantError == context.DeadlineExceeded {
cancel() // server cancellation lands while step1 runs assert.True(t, rc.jobFailed)
return nil assert.Equal(t, "failure", rc.getJobContext().Status)
}, }
func(c context.Context) error { })
ran = append(ran, "always-step")
laterStepCtxErr = c.Err()
return nil
},
} }
err := newMainStepsExecutor(rc, steps)(ctx)
require.ErrorIs(t, err, context.Canceled, "interrupt error is propagated")
assert.Equal(t, []string{"step1", "always-step"}, ran, "the always() step still runs after cancel")
require.NoError(t, laterStepCtxErr, "remaining steps run under a fresh, non-cancelled context")
assert.True(t, rc.jobCancelled, "the job is marked cancelled")
} }
// TestMainStepsExecutorMarksFailedOnTimeoutBetweenSteps guards the timeout path's symmetry with the cancel path. func TestStepsExecutorRunsMainStepsAfterPreInterruption(t *testing.T) {
// When the job deadline (timeout-minutes) lands in the gap between two steps, the job must be marked as failed (not cancelled), for name, tt := range map[string]struct {
// so always()/failure() cleanup steps run while default success() steps skip, and so the timed-out job is not reported as success. cancel bool
func TestMainStepsExecutorMarksFailedOnTimeoutBetweenSteps(t *testing.T) { preErr error
rc := createIfTestRunContext(map[string]*model.Job{ wantError error
"job1": createJob(t, `runs-on: ubuntu-latest`, ""), }{
}) "cancel": {cancel: true, wantError: context.Canceled},
"failure": {preErr: assert.AnError, wantError: assert.AnError},
} {
t.Run(name, func(t *testing.T) {
rc := createIfTestRunContext(map[string]*model.Job{
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
})
ctx := t.Context()
var cancel context.CancelFunc
if tt.cancel {
ctx, cancel = context.WithCancel(ctx)
defer cancel()
}
ctx := newControllableDeadlineContext(t.Context()) var ran []string
var mainStepCtxErr error
err := newStepsExecutor(rc, []common.Executor{
func(_ctx context.Context) error {
ran = append(ran, "pre1")
if tt.cancel {
cancel()
}
return tt.preErr
},
}, []common.Executor{
func(ctx context.Context) error {
ran = append(ran, "always-step")
mainStepCtxErr = ctx.Err()
return nil
},
})(ctx)
var ran []string require.ErrorIs(t, err, tt.wantError)
var laterStepCtxErr error assert.Equal(t, []string{"pre1", "always-step"}, ran)
steps := []common.Executor{ require.NoError(t, mainStepCtxErr)
func(context.Context) error { assert.Equal(t, tt.cancel, rc.jobCancelled)
ran = append(ran, "step1") })
ctx.expire()
return nil
},
func(c context.Context) error {
ran = append(ran, "always-step")
laterStepCtxErr = c.Err()
return nil
},
} }
err := newMainStepsExecutor(rc, steps)(ctx)
require.ErrorIs(t, err, context.DeadlineExceeded, "the timeout error is propagated")
assert.Equal(t, []string{"step1", "always-step"}, ran, "the always() step still runs after a timeout")
require.NoError(t, laterStepCtxErr, "remaining steps run under a fresh, non-expired context")
assert.True(t, rc.jobFailed, "a job timeout marks the job failed")
assert.False(t, rc.jobCancelled, "a timeout is not a cancellation")
// The status the real main-step `if` evaluation sees: "failure", so default success() steps skip while always()/failure() steps run.
assert.Equal(t, "failure", rc.getJobContext().Status)
}
// TestStepsExecutorRunsMainStepsAfterPreCancel verifies that a cancellation landing during the
// pre phase does not abandon the main steps: newStepsExecutor still runs the main-steps executor,
// so a main-stage always()/cancelled() step is reached (under a fresh, non-cancelled context),
// the job is marked cancelled, and the cancellation is propagated. Before the fix the `.Then(...)`
// short-circuit skipped the main steps entirely when a pre step was cancelled.
func TestStepsExecutorRunsMainStepsAfterPreCancel(t *testing.T) {
rc := createIfTestRunContext(map[string]*model.Job{
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
})
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
var ran []string
var mainStepCtxErr error
preSteps := []common.Executor{
func(_ context.Context) error {
ran = append(ran, "pre1")
cancel() // server cancellation lands during the pre phase
return nil
},
}
steps := []common.Executor{
func(c context.Context) error {
ran = append(ran, "always-step")
mainStepCtxErr = c.Err()
return nil
},
}
err := newStepsExecutor(rc, preSteps, steps)(ctx)
require.ErrorIs(t, err, context.Canceled, "the cancellation is propagated")
assert.Equal(t, []string{"pre1", "always-step"}, ran, "the main always() step runs after a pre-phase cancel")
require.NoError(t, mainStepCtxErr, "the main step runs under a fresh, non-cancelled context")
assert.True(t, rc.jobCancelled, "the job is marked cancelled")
}
// TestStepsExecutorRunsMainStepsAfterPreFailure verifies that a failing pre step does not abandon
// the main steps: they still run (so a main-stage always()/failure() step is reached), and the
// pre-step error is propagated so the job is reported as failed. The main steps' own `if`
// evaluation is what skips success()-default steps, so running them here is safe.
func TestStepsExecutorRunsMainStepsAfterPreFailure(t *testing.T) {
rc := createIfTestRunContext(map[string]*model.Job{
"job1": createJob(t, `runs-on: ubuntu-latest`, ""),
})
var ran []string
preSteps := []common.Executor{
func(_ context.Context) error {
ran = append(ran, "pre1")
return assert.AnError
},
}
steps := []common.Executor{
func(_ context.Context) error {
ran = append(ran, "always-step")
return nil
},
}
err := newStepsExecutor(rc, preSteps, steps)(t.Context())
require.ErrorIs(t, err, assert.AnError, "the pre-step error is propagated")
assert.Equal(t, []string{"pre1", "always-step"}, ran, "the main always() step runs after a pre-step failure")
assert.False(t, rc.jobCancelled, "a pre-step failure is not a cancellation")
} }
// TestPreStepFailureAffectsMainStepIfStatus verifies the status path used by real // TestPreStepFailureAffectsMainStepIfStatus verifies the status path used by real
+11 -19
View File
@@ -15,10 +15,10 @@ import (
) )
// parseActionCommand parses like GitHub's runner, but rejects a bare \r GitHub would split at and keeps a value's leading '=' so masks match. // parseActionCommand parses like GitHub's runner, but rejects a bare \r GitHub would split at and keeps a value's leading '=' so masks match.
func parseActionCommand(line, resumeCommand string) (command string, properties map[string]string, data string, legacy, ok bool) { func parseActionCommand(line, resumeCommand string) (command string, properties map[string]string, data string, ok bool) {
line = strings.TrimRight(line, "\r\n") line = strings.TrimRight(line, "\r\n")
if strings.ContainsAny(line, "\r\n") { if strings.ContainsAny(line, "\r\n") {
return "", nil, "", false, false return "", nil, "", false
} }
trimmed := strings.TrimLeftFunc(line, unicode.IsSpace) trimmed := strings.TrimLeftFunc(line, unicode.IsSpace)
for _, legacy := range []bool{false, true} { for _, legacy := range []bool{false, true} {
@@ -54,9 +54,9 @@ func parseActionCommand(line, resumeCommand string) (command string, properties
properties[strings.ToLower(key)] = unescapeProperty(value) properties[strings.ToLower(key)] = unescapeProperty(value)
} }
} }
return command, properties, unescapeData(data), legacy, true return command, properties, unescapeData(data), true
} }
return "", nil, "", false, false return "", nil, "", false
} }
// commandHandler reports whether a line is logged, keeping per-handler command state as GitHub does. // commandHandler reports whether a line is logged, keeping per-handler command state as GitHub does.
@@ -66,7 +66,7 @@ func (rc *RunContext) commandHandler(ctx context.Context) common.LineHandler {
echo := debug echo := debug
resumeCommand := "" resumeCommand := ""
return func(line string) bool { return func(line string) bool {
command, kvPairs, arg, _, ok := parseActionCommand(line, resumeCommand) command, kvPairs, arg, ok := parseActionCommand(line, resumeCommand)
if !ok { if !ok {
return true return true
} }
@@ -143,7 +143,7 @@ func formatAnnotation(level string, properties map[string]string, message string
// serviceCommandHandler applies only ::add-mask::, so a service cannot set the job's env, outputs or state. // serviceCommandHandler applies only ::add-mask::, so a service cannot set the job's env, outputs or state.
func (rc *RunContext) serviceCommandHandler(ctx context.Context) common.LineHandler { func (rc *RunContext) serviceCommandHandler(ctx context.Context) common.LineHandler {
return func(line string) bool { return func(line string) bool {
command, _, arg, _, ok := parseActionCommand(line, "") command, _, arg, ok := parseActionCommand(line, "")
if !ok || command != "add-mask" { if !ok || command != "add-mask" {
return true return true
} }
@@ -158,7 +158,9 @@ const allowUnsecureCommandsVar = "ACTIONS_ALLOW_UNSECURE_COMMANDS"
// not run, recording the error that fails the step. GitHub disabled both because a step that // not run, recording the error that fails the step. GitHub disabled both because a step that
// echoes untrusted content can use them to set NODE_OPTIONS or PATH for every later step. // echoes untrusted content can use them to set NODE_OPTIONS or PATH for every later step.
func (rc *RunContext) refuseUnsecureCommand(ctx context.Context, command string) bool { func (rc *RunContext) refuseUnsecureCommand(ctx context.Context, command string) bool {
if rc.allowUnsecureCommandsOptIn() { if isTruthyEnv(rc.currentStepEnv()[allowUnsecureCommandsVar]) ||
isTruthyEnv(rc.Env[allowUnsecureCommandsVar]) ||
isTruthyEnv(rc.GlobalEnv[allowUnsecureCommandsVar]) {
return false return false
} }
@@ -176,14 +178,6 @@ func (rc *RunContext) refuseUnsecureCommand(ctx context.Context, command string)
return true return true
} }
// allowUnsecureCommandsOptIn reports whether the workflow itself asked for the deprecated
// commands, from any env scope, as it can on GitHub.
func (rc *RunContext) allowUnsecureCommandsOptIn() bool {
return isTruthyEnv(rc.currentStepEnv()[allowUnsecureCommandsVar]) ||
isTruthyEnv(rc.Env[allowUnsecureCommandsVar]) ||
isTruthyEnv(rc.GlobalEnv[allowUnsecureCommandsVar])
}
// isTruthyEnv mirrors GitHub's bool.TryParse: only "true", in any casing. // isTruthyEnv mirrors GitHub's bool.TryParse: only "true", in any casing.
func isTruthyEnv(v string) bool { func isTruthyEnv(v string) bool {
return strings.EqualFold(strings.TrimSpace(v), "true") return strings.EqualFold(strings.TrimSpace(v), "true")
@@ -230,17 +224,15 @@ func (rc *RunContext) setEnvFile(ctx context.Context, kvPairs map[string]string,
} }
func (rc *RunContext) setOutput(ctx context.Context, kvPairs map[string]string, arg string) { func (rc *RunContext) setOutput(ctx context.Context, kvPairs map[string]string, arg string) {
logger := common.Logger(ctx)
stepID := rc.CurrentStep stepID := rc.CurrentStep
outputName := kvPairs["name"]
result, ok := rc.StepResults[stepID] result, ok := rc.StepResults[stepID]
if !ok { if !ok {
logger.Infof("No outputs registered for step '%s'", stepID) common.Logger(ctx).Infof("No outputs registered for step '%s'", stepID)
return return
} }
result.Outputs[outputName] = arg result.Outputs[kvPairs["name"]] = arg
} }
func (rc *RunContext) addPath(arg string) { func (rc *RunContext) addPath(arg string) {
Loaded 50 of 107 files, more files were not shown because too many files have changed in this diff. Show more