diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index c690f16b15..9d4bf430ee 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -151,6 +151,41 @@ jobs: - name: Check license policy and attribution run: go run ./hack/licenses --check + unit-tests: + name: Unit Tests + # Report-only, and one line from required. The suite's intermittent + # failures were a poll defect, now fixed; the one remaining wall-clock + # test, TestWaitContainerRunningFailsFastOnExit, has a wide margin. + # Promoting it means a slow runner can block an unrelated pull. + needs: [changes, precommit, lint] + if: needs.changes.outputs.go == 'true' + runs-on: ubuntu-latest + timeout-minutes: 45 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + + - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7 + with: + go-version-file: go.mod + + - name: Create coverage output directory + run: mkdir -p dist + + # Mirrors `task cli:test`. -p 4 caps parallelism because the wall-clock + # assertions are exceeded at full parallelism on a busy runner. + - name: Run unit tests + run: | + # shellcheck disable=SC2046 # the package list is meant to become arguments + go test $(go list ./... | grep -v -e /test -e /e2e) -race -p 4 -coverprofile=dist/profile.out -covermode=atomic + + - name: Upload coverage profile + if: always() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: unit-coverage-profile + path: dist/profile.out + if-no-files-found: warn + integration-tests-unprivileged: name: Test ${{ matrix.label }} on ${{ matrix.runner }} needs: [changes, build-cli] diff --git a/cmd/internal/agentworkspace/docker_darwin_test.go b/cmd/internal/agentworkspace/docker_darwin_test.go index 2be8490f48..70d5435d6e 100644 --- a/cmd/internal/agentworkspace/docker_darwin_test.go +++ b/cmd/internal/agentworkspace/docker_darwin_test.go @@ -5,6 +5,7 @@ package agentworkspace import ( "os" "path/filepath" + "slices" "strings" "testing" ) @@ -28,11 +29,19 @@ func TestFindDarwinDockerCLIAtKnownPath(t *testing.T) { } } +// The full candidate list also contains machine-wide docker paths, so the +// result is not asserted against it; that depends on the host. func TestFindDarwinDockerCLIRancherDesktopPath(t *testing.T) { home := t.TempDir() rancher := filepath.Join(home, ".rd", "bin", "docker") writeExecutable(t, rancher) - path, err := findDarwinDockerCLIInPaths(darwinDockerCandidatePaths(home)) + + candidates := darwinDockerCandidatePaths(home) + if !slices.Contains(candidates, rancher) { + t.Fatalf("candidate paths %v missing rancher path %q", candidates, rancher) + } + + path, err := findDarwinDockerCLIInPaths([]string{rancher}) if err != nil || path != rancher { t.Fatalf("find docker CLI = %q, %v; want %q", path, err, rancher) } diff --git a/hack/sign_commit/main_test.go b/hack/sign_commit/main_test.go index 65d6e72207..9348c0f93b 100644 --- a/hack/sign_commit/main_test.go +++ b/hack/sign_commit/main_test.go @@ -405,6 +405,8 @@ func TestChangedFilesDetectsWorkingTree(t *testing.T) { gitRun(t, dir, "init", "--quiet") gitRun(t, dir, "config", "user.email", "t@t.com") gitRun(t, dir, "config", "user.name", "test") + // Contributors of this project commonly sign globally, which breaks this. + gitRun(t, dir, "config", "commit.gpgsign", "false") gitRun(t, dir, "checkout", "-b", testMainBranch) writeFile(t, filepath.Join(dir, "committed.txt"), "x") writeFile(t, filepath.Join(dir, "staged.txt"), "s") diff --git a/pkg/docker/helper.go b/pkg/docker/helper.go index ce9336fb47..f43b27cf04 100644 --- a/pkg/docker/helper.go +++ b/pkg/docker/helper.go @@ -22,6 +22,10 @@ import ( "k8s.io/apimachinery/pkg/util/wait" ) +// postDeadlineDiagnosticTimeout bounds the un-deadlined inspect after a poll +// deadline fires; too small a budget loses the daemon error to a killed child. +const postDeadlineDiagnosticTimeout = 2 * time.Second + // DockerBuilder represents the Docker builder types. type DockerBuilder int @@ -515,7 +519,7 @@ func (r *DockerHelper) WaitContainerRunning(ctx context.Context, containerID str // daemon error can still be captured for the final diagnostic. diagnosticCtx, diagnosticCancel := context.WithTimeout( context.WithoutCancel(ctx), - 250*time.Millisecond, + postDeadlineDiagnosticTimeout, ) details, err = r.InspectContainers(diagnosticCtx, []string{containerID}) diagnosticCancel() diff --git a/pkg/driver/docker/lifecycle.go b/pkg/driver/docker/lifecycle.go index 6dc365b202..b5fbf5409c 100644 --- a/pkg/driver/docker/lifecycle.go +++ b/pkg/driver/docker/lifecycle.go @@ -391,6 +391,15 @@ func (d *dockerDriver) waitForLocalImage(ctx context.Context, image string) erro lastErr = err return false, nil } + // A deadline that fires mid-inspect kills the subprocess, so the + // failure arrives as "signal: killed". That is the poll timing + // out, not the daemon failing, so let the deadline decide. + if errors.Is(ctx.Err(), context.DeadlineExceeded) { + if lastErr == nil { + lastErr = docker.ErrImageNotFound + } + return false, nil + } return false, err }, ) diff --git a/pkg/driver/docker/lifecycle_test.go b/pkg/driver/docker/lifecycle_test.go index c8aa4ed607..7bec9ca672 100644 --- a/pkg/driver/docker/lifecycle_test.go +++ b/pkg/driver/docker/lifecycle_test.go @@ -87,6 +87,36 @@ func withShortImageInspectPoll(t *testing.T) { }) } +// A deadline firing mid-inspect must still report the image missing: callers +// branch on that to avoid pulling a locally-built image. +func TestEnsureImage_PollDeadlineDuringInspectReportsNotFound(t *testing.T) { + withShortImageInspectPoll(t) + imageInspectPollTimeout = 200 * time.Millisecond + + dir := t.TempDir() + marker := filepath.Join(dir, "pulled") + bin := filepath.Join(dir, "docker-fake") + // The inspect outlives the poll deadline, so it is always killed. + script := `#!/bin/sh +case "$1" in + inspect) sleep 5 ;; + pull) echo pulled > "` + marker + `" ;; +esac +` + require.NoError(t, os.WriteFile(bin, []byte(script), 0o755)) //nolint:gosec + + d := &dockerDriver{Docker: &docker.DockerHelper{DockerCommand: bin}} + + err := d.EnsureImage( + context.Background(), + &driver.RunOptions{Image: testImageRef, ImageBuilt: true}, + ) + + require.Error(t, err) + assert.ErrorIs(t, err, docker.ErrImageNotFound) + assert.NoFileExists(t, marker) +} + func TestEnsureImage_BuiltImageNeverFoundDoesNotPull(t *testing.T) { withShortImageInspectPoll(t) marker := filepath.Join(t.TempDir(), "pulled") @@ -308,7 +338,9 @@ esac ctx, cancel := context.WithCancel(context.Background()) go func() { - deadline := time.Now().Add(time.Second) + // Cancelling before exec starts kills the preceding inspect instead, + // losing the context error. The deadline is only a backstop. + deadline := time.Now().Add(30 * time.Second) for time.Now().Before(deadline) { if _, err := os.Stat(ready); err == nil { break