diff --git a/.gitea/workflows/test.yml b/.gitea/workflows/test.yml index f4ae9efe..48b46551 100644 --- a/.gitea/workflows/test.yml +++ b/.gitea/workflows/test.yml @@ -57,6 +57,15 @@ jobs: make coverage-report cat .tmp/coverage.md >> "$GITHUB_STEP_SUMMARY" + cross: + name: cross-compile + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + - uses: ./.gitea/actions/go-setup + - name: cross-compile + run: make cross-check + windows: name: test on windows runs-on: ubuntu-latest-kvm diff --git a/AGENTS.md b/AGENTS.md index 912d8884..b2309c91 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -11,7 +11,7 @@ - Read `DEVELOPMENT.md` for internals and conventions - Ensure no trailing whitespace in edited files - Run `make fmt` after `.go` edits, `make tidy` after `go.mod` edits, and `make checks` for the non-lint source checks -- Lint what changed with `make lint-go`, and `make lint-go-windows` for Windows and platform-split files +- Lint what changed with `make lint-go`, and `make lint-go-windows` for Windows and platform-split files. Run `make cross-check` after platform-split edits - Fix the cause rather than disabling a linter or weakening a test. Where unavoidable, use the narrowest scope with a trailing comment giving the reason - Run single go tests with `go test -run '^TestName$' ./modulepath/`. `make test` self-skips the integration tests without docker or network, `make test-dind` runs the daemon-facing tests against the built dind image - Write the fewest, fastest tests covering the behavior, extending an existing one where possible. Prefer unit tests where logic is testable in isolation diff --git a/Makefile b/Makefile index 1e67c018..dbd2f626 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,8 @@ GOVULNCHECK_PACKAGE ?= golang.org/x/vuln/cmd/govulncheck@v1.9.0 # renovate: data GOTEST_FLAGS ?= -race -timeout 20m -parallel 8 +CROSS_TARGETS ?= darwin/arm64 freebsd/amd64 linux/arm netbsd/amd64 openbsd/amd64 solaris/amd64 # one per platform file set, linux/arm for 32-bit syscall fields + STATIC ?= EXTLDFLAGS ?= ifneq ($(STATIC),) @@ -100,6 +102,12 @@ deps-tools: ## install tool dependencies .PHONY: checks checks: tidy-check fmt-check security-check ## run the non-lint source checks +.PHONY: cross-check +cross-check: $(addprefix cross-check/,$(CROSS_TARGETS)) ## check that code and tests compile on every supported platform + +cross-check/%: + GOOS=$(word 1,$(subst /, ,$*)) GOARCH=$(word 2,$(subst /, ,$*)) CGO_ENABLED=0 $(GO) vet -gcflags='all=-N -l' ./... + .PHONY: lint lint: lint-go lint-go-windows ## lint everything diff --git a/act/container/docker_cli_test.go b/act/container/docker_cli_test.go index 1bf73eef..e9ccee67 100644 --- a/act/container/docker_cli_test.go +++ b/act/container/docker_cli_test.go @@ -2,6 +2,8 @@ // Copyright 2022 The nektos/act Authors. All rights reserved. // SPDX-License-Identifier: MIT +//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd || freebsd)) + // This file is based on https://github.com/docker/cli/blob/4a63305d74332de5ceba7fcbccbc3cbb7412f5ba/cli/command/container/opts_test.go with: // * appended with license information // * added tests for the locally changed parseDevice, validateDevice and invalidParameter diff --git a/act/container/docker_create_flags_test.go b/act/container/docker_create_flags_test.go index ca7bc180..3af2a481 100644 --- a/act/container/docker_create_flags_test.go +++ b/act/container/docker_create_flags_test.go @@ -1,6 +1,8 @@ // Copyright 2026 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT +//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd || freebsd)) + package container import ( diff --git a/act/container/docker_images_test.go b/act/container/docker_images_test.go index 0c57e5e2..13a5cc22 100644 --- a/act/container/docker_images_test.go +++ b/act/container/docker_images_test.go @@ -2,6 +2,8 @@ // Copyright 2020 The nektos/act Authors. All rights reserved. // SPDX-License-Identifier: MIT +//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd || freebsd)) + package container import ( diff --git a/act/container/docker_network_test.go b/act/container/docker_network_test.go index 6c6853c3..e56f263c 100644 --- a/act/container/docker_network_test.go +++ b/act/container/docker_network_test.go @@ -1,6 +1,8 @@ // Copyright 2026 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT +//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd || freebsd)) + package container import ( diff --git a/act/container/docker_platform_test.go b/act/container/docker_platform_test.go index 470cb3c7..4bbd783a 100644 --- a/act/container/docker_platform_test.go +++ b/act/container/docker_platform_test.go @@ -1,6 +1,8 @@ // Copyright 2026 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT +//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd || freebsd)) + package container import ( diff --git a/act/container/docker_pull_test.go b/act/container/docker_pull_test.go index 18c1c38c..6b5b0c24 100644 --- a/act/container/docker_pull_test.go +++ b/act/container/docker_pull_test.go @@ -2,6 +2,8 @@ // Copyright 2020 The nektos/act Authors. All rights reserved. // SPDX-License-Identifier: MIT +//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd || freebsd)) + package container import ( diff --git a/act/container/docker_run_test.go b/act/container/docker_run_test.go index 634ea70c..8bfcd4a2 100644 --- a/act/container/docker_run_test.go +++ b/act/container/docker_run_test.go @@ -2,6 +2,8 @@ // Copyright 2020 The nektos/act Authors. All rights reserved. // SPDX-License-Identifier: MIT +//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd || freebsd)) + package container import ( diff --git a/act/container/docker_stub.go b/act/container/docker_stub.go index dfc4077f..30e9e403 100644 --- a/act/container/docker_stub.go +++ b/act/container/docker_stub.go @@ -16,8 +16,13 @@ import ( "gitea.com/gitea/runner/act/common" "github.com/moby/moby/api/types/system" + "github.com/moby/moby/client" ) +func GetDockerClient(context.Context) (client.APIClient, error) { + return nil, errors.ErrUnsupported +} + func IsolatedCacheContainer(context.Context, netip.Addr, string) (string, error) { return "", nil } @@ -25,13 +30,13 @@ func IsolatedCacheContainer(context.Context, netip.Addr, string) (string, error) // ImageExistsLocally returns a boolean indicating if an image with the // requested name, tag and architecture exists in the local docker image store func ImageExistsLocally(ctx context.Context, imageName, platform string) (bool, error) { - return false, errors.New("Unsupported Operation") + return false, errors.ErrUnsupported } // RemoveImage removes image from local store, the function is used to run different // container image architectures func RemoveImage(ctx context.Context, imageName string, force, pruneChildren bool) (bool, error) { - return false, errors.New("Unsupported Operation") + return false, errors.ErrUnsupported } func RemoveDockerJobResources(_ context.Context, _ string) error { @@ -43,20 +48,20 @@ func NewDockerProxy(_ context.Context, _ string) *DockerProxy { } func StartDockerProxy(daemonSocket, dir, job string) (*DockerProxy, error) { - return nil, errors.New("Unsupported Operation") + return nil, errors.ErrUnsupported } // NewDockerBuildExecutor function to create a run executor for the container func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor { return func(ctx context.Context) error { - return errors.New("Unsupported Operation") + return errors.ErrUnsupported } } // NewDockerPullExecutor function to create a run executor for the container func NewDockerPullExecutor(input NewDockerPullExecutorInput) common.Executor { return func(ctx context.Context) error { - return errors.New("Unsupported Operation") + return errors.ErrUnsupported } } diff --git a/act/container/helpers_test.go b/act/container/helpers_test.go index 88e13ff6..420218c6 100644 --- a/act/container/helpers_test.go +++ b/act/container/helpers_test.go @@ -1,6 +1,8 @@ // Copyright 2026 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT +//go:build !(WITHOUT_DOCKER || !(linux || darwin || windows || netbsd || freebsd)) + package container import ( diff --git a/internal/pkg/disk/free_openbsd.go b/internal/pkg/disk/free_openbsd.go new file mode 100644 index 00000000..54fbc181 --- /dev/null +++ b/internal/pkg/disk/free_openbsd.go @@ -0,0 +1,17 @@ +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +//go:build openbsd + +package disk + +import "golang.org/x/sys/unix" + +// FreeBytes reports the space available to an unprivileged user on the volume holding path. +func FreeBytes(path string) (uint64, error) { + var stat unix.Statfs_t + if err := unix.Statfs(path, &stat); err != nil { + return 0, err + } + return availableBytes(stat.F_bavail, stat.F_bsize), nil +} diff --git a/internal/pkg/disk/free_other.go b/internal/pkg/disk/free_other.go index ed1df0cf..b414f1b6 100644 --- a/internal/pkg/disk/free_other.go +++ b/internal/pkg/disk/free_other.go @@ -1,7 +1,7 @@ // Copyright 2026 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT -//go:build !aix && !darwin && !dragonfly && !freebsd && !linux && !netbsd && !openbsd && !solaris && !windows +//go:build !unix && !windows package disk diff --git a/internal/pkg/disk/free_statfs.go b/internal/pkg/disk/free_statfs.go new file mode 100644 index 00000000..c5112b2c --- /dev/null +++ b/internal/pkg/disk/free_statfs.go @@ -0,0 +1,17 @@ +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +//go:build aix || darwin || dragonfly || freebsd || linux + +package disk + +import "golang.org/x/sys/unix" + +// FreeBytes reports the space available to an unprivileged user on the volume holding path. +func FreeBytes(path string) (uint64, error) { + var stat unix.Statfs_t + if err := unix.Statfs(path, &stat); err != nil { + return 0, err + } + return availableBytes(stat.Bavail, stat.Bsize), nil +} diff --git a/internal/pkg/disk/free_statvfs.go b/internal/pkg/disk/free_statvfs.go new file mode 100644 index 00000000..d80e7309 --- /dev/null +++ b/internal/pkg/disk/free_statvfs.go @@ -0,0 +1,18 @@ +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +//go:build netbsd || solaris + +package disk + +import "golang.org/x/sys/unix" + +// FreeBytes reports the space available to an unprivileged user on the volume holding path. +func FreeBytes(path string) (uint64, error) { + var stat unix.Statvfs_t + if err := unix.Statvfs(path, &stat); err != nil { + return 0, err + } + // statvfs(2) counts blocks in f_frsize units, f_bsize is the preferred I/O size and differs on ZFS + return availableBytes(stat.Bavail, stat.Frsize), nil +} diff --git a/internal/pkg/disk/free_unix.go b/internal/pkg/disk/free_unix.go index e87d8bca..09ed8a18 100644 --- a/internal/pkg/disk/free_unix.go +++ b/internal/pkg/disk/free_unix.go @@ -1,17 +1,10 @@ // Copyright 2026 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT -//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris +//go:build unix package disk -import "golang.org/x/sys/unix" - -// FreeBytes reports the space available to an unprivileged user on the volume holding path. -func FreeBytes(path string) (uint64, error) { - var stat unix.Statfs_t - if err := unix.Statfs(path, &stat); err != nil { - return 0, err - } - return uint64(stat.Bavail) * uint64(stat.Bsize), nil //nolint:unconvert // Bavail/Bsize signedness differs by platform +func availableBytes[Count, Size ~int32 | ~int64 | ~uint32 | ~uint64](count Count, size Size) uint64 { + return uint64(max(count, 0)) * uint64(size) // signed BSD counts go negative once root fills the reserved blocks } diff --git a/internal/pkg/disk/free_unix_test.go b/internal/pkg/disk/free_unix_test.go new file mode 100644 index 00000000..718b1820 --- /dev/null +++ b/internal/pkg/disk/free_unix_test.go @@ -0,0 +1,22 @@ +// Copyright 2026 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +//go:build unix + +package disk + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestFreeBytes(t *testing.T) { + free, err := FreeBytes(t.TempDir()) + require.NoError(t, err) + assert.Positive(t, free) + + assert.Equal(t, uint64(1536), availableBytes(int64(3), uint32(512))) + assert.Zero(t, availableBytes(int64(-3), uint32(512))) +}