Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/pr-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
11 changes: 10 additions & 1 deletion cmd/internal/agentworkspace/docker_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package agentworkspace
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
)
Expand All @@ -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)
}
Expand Down
2 changes: 2 additions & 0 deletions hack/sign_commit/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 5 additions & 1 deletion pkg/docker/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down
9 changes: 9 additions & 0 deletions pkg/driver/docker/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
)
Expand Down
34 changes: 33 additions & 1 deletion pkg/driver/docker/lifecycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down
Loading