diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index 1c5d24c56e..e02c688fb9 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -31,6 +31,7 @@ jobs: - '.goreleaser.yml' - 'e2e/**' - 'hack/microsandbox/**' + - 'hack/provider_update_ci/**' - 'hack/run-managed-policy-e2e.sh' - '.github/workflows/pr-ci.yml' - 'hack/licenses/**' @@ -133,6 +134,38 @@ jobs: go test -race -short -count=1 -v ./e2e/tests/up-docker-compose \ -run '^TestExpectedUIDMapping$' + provider-update-windows: + name: Provider Update Tests (windows-latest) + needs: [changes] + if: needs.changes.outputs.go == 'true' + runs-on: windows-latest + # Allow cold Go compilation and the post-job cache save to complete. + timeout-minutes: 30 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + + - name: setup Go + uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7 + with: + go-version-file: go.mod + + - name: test Go CI helpers on Windows + run: go test -race -count=1 -timeout 10m ./hack/microsandbox ./hack/provider_update_ci + + - name: run provider update tests and require Windows coverage + shell: pwsh + run: go run ./hack/provider_update_ci --artifacts-dir "$env:RUNNER_TEMP" + + - name: upload provider update test JSON on failure + if: failure() + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: provider-update-windows-test-json + path: | + ${{ runner.temp }}/provider-update-windows.jsonl + ${{ runner.temp }}/provider-update-windows.exitcode + if-no-files-found: ignore + can-read-secret: name: Can Read Secret runs-on: ubuntu-latest @@ -158,7 +191,8 @@ jobs: - runner: ubuntu-latest - runner: macos-latest - runner: windows-latest - job_timeout_minutes: 30 + # Cold builds need room to save the Go cache after artifact uploads. + job_timeout_minutes: 45 runs-on: ${{ matrix.runner }} timeout-minutes: ${{ matrix.job_timeout_minutes || 20 }} steps: @@ -167,8 +201,6 @@ jobs: - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7 with: go-version-file: go.mod - # Windows cache archiving can consume the post-job budget after a successful build. - cache: ${{ runner.os != 'Windows' }} - name: get operating system lowercase id: os @@ -219,6 +251,19 @@ jobs: name: devsy-${{ steps.os.outputs.runner_os }} path: dist/devsy-${{ steps.os.outputs.runner_os }}_*/devsy-${{ steps.os.outputs.runner_os }}-* + - name: build MicroSandbox CI helper + if: runner.os == 'Linux' + env: + CGO_ENABLED: 0 + run: go build -trimpath -o "${RUNNER_TEMP}/devsy-ci-microsandbox" ./hack/microsandbox + + - name: upload MicroSandbox CI helper + if: runner.os == 'Linux' + uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7 + with: + name: devsy-ci-microsandbox-linux + path: ${{ runner.temp }}/devsy-ci-microsandbox + - name: build e2e test binary if: matrix.runner == 'ubuntu-latest' || matrix.runner == 'windows-latest' shell: bash @@ -383,6 +428,15 @@ jobs: name: e2e-test-linux path: ./e2e/ + - name: download MicroSandbox CI helper + uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 + with: + name: devsy-ci-microsandbox-linux + path: ${{ runner.temp }} + + - name: setup MicroSandbox CI helper + run: chmod +x "${RUNNER_TEMP}/devsy-ci-microsandbox" + - name: setup executable shell: bash working-directory: ./e2e @@ -394,9 +448,6 @@ jobs: chmod +x ./e2e.test ls -R ./bin/ - - name: test Ginkgo report validator - run: python3 -m unittest discover -s hack/microsandbox -p 'test_*.py' - - name: cache microsandbox release bundle (Linux) uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 with: @@ -405,82 +456,17 @@ jobs: - name: install checksum-pinned microsandbox (Linux) timeout-minutes: 5 - run: | - archive="${RUNNER_TEMP}/microsandbox-linux-x86_64-v0.7.7.tar.gz" - if [ ! -f "$archive" ]; then - curl -fsSL --retry 3 -o "$archive" \ - https://github.com/superradcompany/microsandbox/releases/download/v0.7.7/microsandbox-linux-x86_64.tar.gz - fi - echo "b3cc4a5e3f52dfdd938a6f67ac4a9a959ddfe304bab56de4964044b8613f01bb $archive" | sha256sum -c - - staging="$(mktemp -d)" - trap 'rm -rf "$staging"' EXIT - tar -xzf "$archive" -C "$staging" - mkdir -p "$HOME/.microsandbox/bin" "$HOME/.microsandbox/lib" "$HOME/.local/bin" - install -m 755 "$staging/msb" "$HOME/.microsandbox/bin/msb" - install -m 644 "$staging/libkrunfw.so.5.6.1" "$HOME/.microsandbox/lib/" - ln -sf libkrunfw.so.5.6.1 "$HOME/.microsandbox/lib/libkrunfw.so.5" - ln -sf libkrunfw.so.5 "$HOME/.microsandbox/lib/libkrunfw.so" - ln -sf "$HOME/.microsandbox/bin/msb" "$HOME/.local/bin/msb" - echo "$HOME/.local/bin" >> "$GITHUB_PATH" - "$HOME/.local/bin/msb" --version - sudo test -r /dev/kvm - sudo test -w /dev/kvm + run: '"${RUNNER_TEMP}/devsy-ci-microsandbox" install-linux' - name: run MicroSandbox test shell: bash - working-directory: ./e2e env: GH_USERNAME: ${{ github.repository_owner }} GH_ACCESS_TOKEN: ${{ github.token }} GH_CREDENTIAL_USERNAME: x-access-token TEST_TIMEOUT: 1200s DEVSY_REQUIRE_MICROSANDBOX: "true" - run: | - set -uo pipefail - report="${RUNNER_TEMP}/microsandbox-${{ matrix.label }}.json" - test_log="${RUNNER_TEMP}/microsandbox-${{ matrix.label }}.log" - command=(./e2e.test -test.v -ginkgo.v -test.timeout "${TEST_TIMEOUT}" - -ginkgo.fail-on-empty -ginkgo.flake-attempts=1 - --ginkgo.label-filter="${{ matrix.label }}" -ginkgo.json-report="$report") - fixture_environment=() - if [ "${{ matrix.label }}" = "up-provider-microsandbox-managed-recreate" ]; then - command=(../hack/run-managed-policy-e2e.sh "${command[@]}") - fixture_environment=(DEVSY_REQUIRE_MSB_MANAGED_POLICY_FIXTURE=true "RUNNER_TEMP=${RUNNER_TEMP}") - fi - set +e - sudo \ - DEVSY_REQUIRE_MICROSANDBOX="${DEVSY_REQUIRE_MICROSANDBOX}" \ - GH_USERNAME="${GH_USERNAME}" \ - GH_ACCESS_TOKEN="${GH_ACCESS_TOKEN}" \ - GH_CREDENTIAL_USERNAME="${GH_CREDENTIAL_USERNAME}" \ - KUBECONFIG="${KUBECONFIG:-$HOME/.kube/config}" \ - ${DOCKER_HOST:+DOCKER_HOST="${DOCKER_HOST}"} \ - PATH="${PATH}" \ - "${fixture_environment[@]}" "${command[@]}" 2>&1 | tee "$test_log" - pipeline_status=("${PIPESTATUS[@]}") - set -e - test_status=${pipeline_status[0]} - log_status=${pipeline_status[1]} - chown_status=0 - if [ -e "$report" ]; then - sudo chown "$(id -u):$(id -g)" "$report" || chown_status=$? - fi - report_status=0 - if [ "$chown_status" -ne 0 ]; then - echo "::error::could not make Ginkgo report readable for artifact upload" - report_status="$chown_status" - else - python3 ../hack/microsandbox/verify_report.py \ - --report "$report" --label "${{ matrix.label }}" \ - --expected-count "${{ matrix.expected-spec-count }}" || report_status=$? - fi - if [ "$test_status" -ne 0 ]; then - exit "$test_status" - fi - if [ "$log_status" -ne 0 ]; then - exit "$log_status" - fi - exit "$report_status" + run: '"${RUNNER_TEMP}/devsy-ci-microsandbox" run --label "${{ matrix.label }}" --expected-count "${{ matrix.expected-spec-count }}"' - name: upload MicroSandbox test evidence if: always() @@ -1247,6 +1233,7 @@ jobs: - lint - agent-skill - unit-tests + - provider-update-windows - licenses - build-cli - integration-tests-unprivileged @@ -1284,6 +1271,7 @@ jobs: check_result "lint" "${{ needs.lint.result }}" false || status=1 check_result "agent-skill" "${{ needs.agent-skill.result }}" false || status=1 check_result "unit-tests" "${{ needs.unit-tests.result }}" true || status=1 + check_result "provider-update-windows" "${{ needs.provider-update-windows.result }}" true || status=1 check_result "licenses" "${{ needs.licenses.result }}" true || status=1 check_result "build-cli" "${{ needs.build-cli.result }}" true || status=1 check_result "integration-tests-unprivileged" "${{ needs.integration-tests-unprivileged.result }}" true || status=1 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index aeebe549db..db53e2fc90 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -62,10 +62,10 @@ repos: rev: v2.13.2 hooks: - id: golangci-lint - language_version: 1.26.8 + language_version: 1.26.9 args: ["--timeout=10m"] - id: golangci-lint-fmt - language_version: 1.26.8 + language_version: 1.26.9 - repo: local hooks: - id: golangci-lint-ci-parity diff --git a/cmd/workspace/gpg_tunnel_test.go b/cmd/workspace/gpg_tunnel_test.go index 66254ad433..d8440e42b6 100644 --- a/cmd/workspace/gpg_tunnel_test.go +++ b/cmd/workspace/gpg_tunnel_test.go @@ -11,6 +11,7 @@ import ( "path/filepath" "slices" "strings" + "sync" "testing" "time" @@ -104,7 +105,7 @@ func TestGPGTunnelRebindsWhenManagedReverseForwardExits(t *testing.T) { replacement := tunnel.forward tunnel.stopForward() waitForManagedReverseForward(t, replacement) - assertReverseForwardListenerReleased(t, forwards.listeners[1]) + assertManagedReverseForwardListenerClosed(t, forwards.listeners[1]) } type testGPGReverseForwards struct { @@ -112,7 +113,7 @@ type testGPGReverseForwards struct { userMapping string userForward *managedReverseForward userListener net.Listener - listeners []net.Listener + listeners []*closeObservedListener } func (f *testGPGReverseForwards) start( @@ -164,13 +165,30 @@ func assertUserReverseForwardActive( } } -func assertReverseForwardListenerReleased(t *testing.T, stopped net.Listener) { +func assertManagedReverseForwardListenerClosed(t *testing.T, stopped *closeObservedListener) { t.Helper() - if listener, err := net.Listen("tcp", stopped.Addr().String()); err != nil { - t.Fatalf("replacement listener still bound after stop: %v", err) - } else { - _ = listener.Close() + select { + case <-stopped.closed: + default: + t.Fatal("managed forward completed without closing its owned listener") + } +} + +type closeObservedListener struct { + net.Listener + closed chan struct{} + once sync.Once +} + +func (l *closeObservedListener) Close() error { + if err := l.Listener.Close(); err != nil { + if errors.Is(err, net.ErrClosed) { + l.once.Do(func() { close(l.closed) }) + } + return err } + l.once.Do(func() { close(l.closed) }) + return nil } func TestGPGTunnelRetriesUserMappingsAfterInitialBindFailure(t *testing.T) { @@ -210,11 +228,12 @@ func TestGPGTunnelRetriesUserMappingsAfterInitialBindFailure(t *testing.T) { func startTestManagedReverseForward( ctx context.Context, client *ssh.Client, -) (*managedReverseForward, net.Listener, error) { +) (*managedReverseForward, *closeObservedListener, error) { listener, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { return nil, nil, err } + observed := &closeObservedListener{Listener: listener, closed: make(chan struct{})} forwardCtx, cancel := context.WithCancel(ctx) done := make(chan error, 1) go runManagedReverseForward(managedReverseForwardRun{ @@ -224,14 +243,14 @@ func startTestManagedReverseForward( forward: boundReverseForward{ portMapping: "gpg socket", mapping: port.Mapping{ - Host: port.Address{Protocol: "tcp", Address: listener.Addr().String()}, + Host: port.Address{Protocol: "tcp", Address: observed.Addr().String()}, Container: port.Address{Protocol: "tcp", Address: "127.0.0.1:1"}, }, - listener: listener, + listener: observed, }, doneChan: done, }) - return &managedReverseForward{cancel: cancel, done: done}, listener, nil + return &managedReverseForward{cancel: cancel, done: done}, observed, nil } func waitForManagedReverseForward(t *testing.T, forward *managedReverseForward) { diff --git a/e2e/tests/up/MICROSANDBOX.md b/e2e/tests/up/MICROSANDBOX.md index 54d42bba27..2ec8de2598 100644 --- a/e2e/tests/up/MICROSANDBOX.md +++ b/e2e/tests/up/MICROSANDBOX.md @@ -25,13 +25,16 @@ callbacks into the session. ## Continuous integration The dedicated `integration-tests-microsandbox` job in -`.github/workflows/pr-ci.yml` owns the suite catalogue and common setup. Each -suite still gets a fresh runner, a 25-minute job limit and a 20-minute test limit, +`.github/workflows/pr-ci.yml` owns the suite catalogue, runner configuration and +artifact uploads. The Go utility in `hack/microsandbox` installs and checks the +pinned Linux bundle and owns test execution, managed fixture selection, output +capture and report validation. The Linux build job supplies its CI-only executable; +it is not a Devsy release artifact. Each suite still gets a fresh runner, a 25-minute job limit and a 20-minute test limit, with one attempt. The checked runtime bundle, build artifacts, required KVM gate and credentials are configured once in the job definition. Each matrix entry names its Ginkgo label and expected scenario count. -`hack/microsandbox/verify_report.py` requires a real successful run with that +The helper's `verify-report` command requires a real successful run with that exact count, split equally between the explicit `microsandbox-builtin` and `microsandbox-external` labels. Missing cases, skips, retries, dry runs and programmatic focus fail the gate. Reports and test output are retained even when diff --git a/go.mod b/go.mod index 328e494ef4..de5e61b09f 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/devsy-org/devsy -go 1.26.8 +go 1.26.9 require ( al.essio.dev/pkg/shellescape v1.6.1 diff --git a/hack/microsandbox/command.go b/hack/microsandbox/command.go new file mode 100644 index 0000000000..09c73d4e33 --- /dev/null +++ b/hack/microsandbox/command.go @@ -0,0 +1,33 @@ +package main + +import ( + "errors" + "io" + "os/exec" +) + +type suiteConfig struct { + label string + expectedCount int + repositoryDir string + tempDir string + testTimeout string +} + +type commandRunner func(*exec.Cmd) error + +type commandOutput struct { + stdout io.Writer + stderr io.Writer +} + +func commandExitCode(err error) int { + if err == nil { + return 0 + } + var exitErr *exec.ExitError + if errors.As(err, &exitErr) && exitErr.ExitCode() > 0 { + return exitErr.ExitCode() + } + return 1 +} diff --git a/hack/microsandbox/install.go b/hack/microsandbox/install.go new file mode 100644 index 0000000000..8c08b3c698 --- /dev/null +++ b/hack/microsandbox/install.go @@ -0,0 +1,290 @@ +package main + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "fmt" + "io" + "os" + "os/exec" + "path/filepath" + "strings" +) + +const ( + microsandboxVersion = "v0.7.7" + microsandboxURL = "https://github.com/superradcompany/microsandbox/releases/download/" + + "v0.7.7/microsandbox-linux-x86_64.tar.gz" + microsandboxSHA256 = "b3cc4a5e3f52dfdd938a6f67ac4a9a959ddfe304bab56de4964044b8613f01bb" + microsandboxArchive = "microsandbox-linux-x86_64-v0.7.7.tar.gz" + microsandboxLibrary = "libkrunfw.so.5.6.1" +) + +type installerBundle struct { + filename string + url string + sha256 string +} + +type linuxInstaller struct { + ctx context.Context + command commandRunner + output commandOutput + bundle installerBundle +} + +func installLinux(ctx context.Context, run commandRunner, output commandOutput) error { + installer := linuxInstaller{ + ctx: ctx, command: run, output: output, + bundle: installerBundle{ + filename: microsandboxArchive, + url: microsandboxURL, + sha256: microsandboxSHA256, + }, + } + return installer.install() +} + +type installerPaths struct { + runnerTemp string + archive string + githubPath string + home string + binDir string + libDir string + localBin string +} + +func (installer *linuxInstaller) install() (err error) { + paths, err := installer.validateEnvironment() + if err != nil { + return err + } + if err := installer.prepareArchive(paths); err != nil { + return err + } + staging, err := os.MkdirTemp(paths.runnerTemp, "microsandbox-install-") + if err != nil { + return fmt.Errorf("create MicroSandbox staging directory: %w", err) + } + defer func() { err = errors.Join(err, os.RemoveAll(staging)) }() + if err := installer.extractArchive(paths.archive, staging); err != nil { + return err + } + if err := installer.installFiles(staging, paths); err != nil { + return err + } + return installer.activate(paths) +} + +func (installer *linuxInstaller) validateEnvironment() (installerPaths, error) { + bundle := installer.bundle + runnerTemp, err := requiredInstallerEnvironment("RUNNER_TEMP") + if err != nil { + return installerPaths{}, err + } + if err := existingInstallerDirectory(runnerTemp); err != nil { + return installerPaths{}, err + } + githubPath, err := requiredInstallerEnvironment("GITHUB_PATH") + if err != nil { + return installerPaths{}, err + } + home, err := requiredInstallerEnvironment("HOME") + if err != nil { + return installerPaths{}, err + } + if err := validateInstallerBundle(bundle); err != nil { + return installerPaths{}, err + } + installRoot := filepath.Join(home, ".microsandbox") + return installerPaths{ + runnerTemp: runnerTemp, + archive: filepath.Join(runnerTemp, bundle.filename), + githubPath: githubPath, + home: home, + binDir: filepath.Join(installRoot, "bin"), + libDir: filepath.Join(installRoot, "lib"), + localBin: filepath.Join(home, ".local", "bin"), + }, nil +} + +func requiredInstallerEnvironment(name string) (string, error) { + value := os.Getenv(name) + if value == "" { + return "", fmt.Errorf("%s must be set by the workflow", name) + } + return value, nil +} + +func existingInstallerDirectory(path string) error { + info, err := os.Stat(path) + if err != nil || !info.IsDir() { + return fmt.Errorf( + "RUNNER_TEMP must name an existing directory: %s", + path, + ) + } + return nil +} + +func validateInstallerBundle(bundle installerBundle) error { + if bundle.filename == "" || filepath.Base(bundle.filename) != bundle.filename || + bundle.url == "" { + return errors.New("invalid pinned MicroSandbox bundle metadata") + } + if len(bundle.sha256) != sha256.Size*2 { + return errors.New("invalid pinned MicroSandbox SHA-256") + } + if _, err := hex.DecodeString(bundle.sha256); err != nil { + return errors.New("invalid pinned MicroSandbox SHA-256") + } + return nil +} + +func (installer *linuxInstaller) prepareArchive(paths installerPaths) error { + if _, err := os.Stat(paths.archive); errors.Is(err, os.ErrNotExist) { + if err := installer.run( + "curl", + "-fsSL", + "--retry", + "3", + "-o", + paths.archive, + installer.bundle.url, + ); err != nil { + downloadErr := fmt.Errorf( + "download pinned MicroSandbox %s: %w", + microsandboxVersion, + err, + ) + return errors.Join(downloadErr, removeInstallerArchive(paths.archive)) + } + } else if err != nil { + return fmt.Errorf("inspect cached MicroSandbox archive: %w", err) + } + actual, err := installerFileSHA256(paths.archive) + if err != nil { + return fmt.Errorf("checksum pinned MicroSandbox archive: %w", err) + } + if !strings.EqualFold(actual, installer.bundle.sha256) { + mismatchErr := fmt.Errorf("MicroSandbox archive checksum mismatch: got %s", actual) + return errors.Join(mismatchErr, removeInstallerArchive(paths.archive)) + } + return nil +} + +func removeInstallerArchive(path string) error { + if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) { + return fmt.Errorf("remove unusable MicroSandbox archive: %w", err) + } + return nil +} + +func (installer *linuxInstaller) extractArchive(archive, staging string) error { + if err := installer.run("tar", "-xzf", archive, "-C", staging); err != nil { + return fmt.Errorf("extract pinned MicroSandbox archive: %w", err) + } + return nil +} + +func (installer *linuxInstaller) installFiles(staging string, paths installerPaths) error { + for _, dir := range []string{paths.binDir, paths.libDir, paths.localBin} { + if err := os.MkdirAll(dir, 0o755); err != nil { + return fmt.Errorf("create MicroSandbox install directory: %w", err) + } + } + msbPath := filepath.Join(paths.binDir, "msb") + if err := copyInstallerFile(filepath.Join(staging, "msb"), msbPath, 0o755); err != nil { + return fmt.Errorf("install MicroSandbox executable: %w", err) + } + libraryPath := filepath.Join(paths.libDir, microsandboxLibrary) + if err := copyInstallerFile( + filepath.Join(staging, microsandboxLibrary), + libraryPath, + 0o644, + ); err != nil { + return fmt.Errorf("install MicroSandbox library: %w", err) + } + for _, link := range []struct{ path, target, label string }{ + {filepath.Join(paths.libDir, "libkrunfw.so.5"), microsandboxLibrary, "library ABI"}, + {filepath.Join(paths.libDir, "libkrunfw.so"), "libkrunfw.so.5", "library"}, + {filepath.Join(paths.localBin, "msb"), msbPath, "executable"}, + } { + if err := replaceInstallerSymlink(link.path, link.target); err != nil { + return fmt.Errorf("link MicroSandbox %s: %w", link.label, err) + } + } + if err := appendInstallerPath(paths.githubPath, paths.localBin); err != nil { + return fmt.Errorf("append MicroSandbox to workflow PATH: %w", err) + } + return nil +} + +func (installer *linuxInstaller) activate(paths installerPaths) error { + msbPath := filepath.Join(paths.binDir, "msb") + if err := installer.run(msbPath, "--version"); err != nil { + return fmt.Errorf("check MicroSandbox version: %w", err) + } + for _, permission := range []struct{ flag, name string }{{"-r", "read"}, {"-w", "write"}} { + if err := installer.run("sudo", "test", permission.flag, "/dev/kvm"); err != nil { + return fmt.Errorf("check /dev/kvm %s access: %w", permission.name, err) + } + } + return nil +} + +func (installer *linuxInstaller) run(name string, args ...string) error { + cmd := exec.CommandContext(installer.ctx, name, args...) + cmd.Stdout = installer.output.stdout + cmd.Stderr = installer.output.stderr + if err := installer.command(cmd); err != nil { + return fmt.Errorf("%s: %w", name, err) + } + return nil +} + +func installerFileSHA256(path string) (_ string, err error) { + file, err := os.Open(path) + if err != nil { + return "", err + } + defer func() { err = errors.Join(err, file.Close()) }() + digest := sha256.New() + if _, err := io.Copy(digest, file); err != nil { + return "", err + } + return hex.EncodeToString(digest.Sum(nil)), nil +} + +func copyInstallerFile(source, destination string, mode os.FileMode) (err error) { + in, err := os.Open(source) + if err != nil { + return err + } + defer func() { err = errors.Join(err, in.Close()) }() + out, err := os.OpenFile(destination, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, mode) + if err != nil { + return err + } + _, copyErr := io.Copy(out, in) + return errors.Join(copyErr, out.Chmod(mode), out.Close()) +} + +func replaceInstallerSymlink(path, target string) error { + if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) { + return err + } + return os.Symlink(target, path) +} + +func appendInstallerPath(path, entry string) (err error) { + file, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o600) + if err != nil { + return err + } + _, writeErr := fmt.Fprintln(file, entry) + return errors.Join(writeErr, file.Close()) +} diff --git a/hack/microsandbox/install_test.go b/hack/microsandbox/install_test.go new file mode 100644 index 0000000000..b4ba93955f --- /dev/null +++ b/hack/microsandbox/install_test.go @@ -0,0 +1,363 @@ +//go:build !windows + +package main + +import ( + "archive/tar" + "bytes" + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "io" + "os" + "os/exec" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" +) + +const ( + installerTarCommand = "tar" + installerBinaryName = "msb" + installerSudoCommand = "sudo" + installerTestCommand = "test" + installerTestURL = "https://github.com/superradcompany/microsandbox/releases/download/" + + "v0.7.7/microsandbox-linux-x86_64.tar.gz" +) + +func TestInstallLinuxPinnedBundle(t *testing.T) { + require.Equal(t, "v0.7.7", microsandboxVersion) + require.Equal(t, installerTestURL, microsandboxURL) + require.Equal( + t, + "b3cc4a5e3f52dfdd938a6f67ac4a9a959ddfe304bab56de4964044b8613f01bb", + microsandboxSHA256, + ) + require.Equal(t, "microsandbox-linux-x86_64-v0.7.7.tar.gz", microsandboxArchive) +} + +func TestInstallLinuxBundleUsesCachedArchiveAndInstallsFiles(t *testing.T) { + fixture := newInstallerFixture(t) + archive := installerTarFixture(t) + fixture.cacheArchive(t, archive) + + runner := &installerTestRunner{fixture: fixture} + var stdout, stderr bytes.Buffer + require.NoError(t, fixture.installer(context.Background(), runner.run, + commandOutput{stdout: &stdout, stderr: &stderr}).install(), stderr.String()) + require.Equal(t, "MicroSandbox v0.7.7\n", stdout.String()) + require.Equal( + t, + []string{ + installerTarCommand, + installerBinaryName, + installerSudoCommand, + installerSudoCommand, + }, + runner.names, + ) + installerAssertInstalled(t, fixture) + installerAssertOnlyCachedArchive(t, fixture.runnerTemp, fixture.bundle.filename) +} + +func TestInstallLinuxBundleDownloadsPinnedArchiveBeforeExtraction(t *testing.T) { + fixture := newInstallerFixture(t) + runner := &installerTestRunner{fixture: fixture, archive: installerTarFixture(t)} + fixture.bundle.sha256 = installerDigest(runner.archive) + runner.fixture = fixture + require.NoError(t, fixture.installer(context.Background(), runner.run, + commandOutput{stdout: io.Discard, stderr: io.Discard}).install()) + require.Equal( + t, + []string{ + "curl", + installerTarCommand, + installerBinaryName, + installerSudoCommand, + installerSudoCommand, + }, + runner.names, + ) + require.True(t, runner.downloaded) + installerAssertInstalled(t, fixture) +} + +func TestInstallLinuxBundleRejectsBadChecksumBeforeExtraction(t *testing.T) { + fixture := newInstallerFixture(t) + validArchive := installerTarFixture(t) + fixture.cacheArchive(t, []byte("corrupt cached archive")) + fixture.bundle.sha256 = installerDigest(validArchive) + runner := &installerTestRunner{fixture: fixture, archive: validArchive} + + err := fixture.installer(context.Background(), runner.run, + commandOutput{stdout: io.Discard, stderr: io.Discard}).install() + require.ErrorContains(t, err, "checksum mismatch") + require.Empty(t, runner.names) + _, err = os.Stat(filepath.Join(fixture.home, ".microsandbox")) + require.ErrorIs(t, err, os.ErrNotExist) + installerAssertRunnerTempEmpty(t, fixture.runnerTemp) + + err = fixture.installer(context.Background(), runner.run, + commandOutput{stdout: io.Discard, stderr: io.Discard}).install() + require.NoError(t, err) + require.True(t, runner.downloaded) + installerAssertInstalled(t, fixture) +} + +func TestInstallLinuxBundleRemovesPartialDownloadForRetry(t *testing.T) { + fixture := newInstallerFixture(t) + validArchive := installerTarFixture(t) + fixture.bundle.sha256 = installerDigest(validArchive) + runner := &installerTestRunner{ + fixture: fixture, archive: validArchive, + partialArchive: []byte("partial archive"), failDownload: errors.New("curl exited 22"), + } + + err := fixture.installer(context.Background(), runner.run, + commandOutput{stdout: io.Discard, stderr: io.Discard}).install() + require.ErrorIs(t, err, runner.failDownload) + installerAssertRunnerTempEmpty(t, fixture.runnerTemp) + + runner.failDownload = nil + runner.partialArchive = nil + err = fixture.installer(context.Background(), runner.run, + commandOutput{stdout: io.Discard, stderr: io.Discard}).install() + require.NoError(t, err) + require.True(t, runner.downloaded) + installerAssertInstalled(t, fixture) +} + +func TestInstallLinuxBundlePreservesKVMCommandFailure(t *testing.T) { + fixture := newInstallerFixture(t) + fixture.cacheArchive(t, installerTarFixture(t)) + failure := errors.New("sudo test exited 23") + runner := &installerTestRunner{fixture: fixture, failKVMWrite: failure} + + err := fixture.installer(context.Background(), runner.run, + commandOutput{stdout: io.Discard, stderr: io.Discard}).install() + require.ErrorIs(t, err, failure) + require.Equal( + t, + []string{ + installerTarCommand, + installerBinaryName, + installerSudoCommand, + installerSudoCommand, + }, + runner.names, + ) + installerAssertInstalled(t, fixture) +} + +func TestInstallLinuxBundleCleansStagingWhenExtractionFails(t *testing.T) { + fixture := newInstallerFixture(t) + fixture.cacheArchive(t, installerTarFixture(t)) + failure := errors.New("tar exited 17") + runner := &installerTestRunner{fixture: fixture, failExtraction: failure} + + err := fixture.installer(context.Background(), runner.run, + commandOutput{stdout: io.Discard, stderr: io.Discard}).install() + require.ErrorIs(t, err, failure) + installerAssertOnlyCachedArchive(t, fixture.runnerTemp, fixture.bundle.filename) +} + +type installerTestFixture struct { + runnerTemp string + home string + githubPath string + bundle installerBundle +} + +func newInstallerFixture(t *testing.T) installerTestFixture { + t.Helper() + runnerTemp := t.TempDir() + home := t.TempDir() + githubPath := filepath.Join(t.TempDir(), "github-path") + require.NoError(t, os.WriteFile(githubPath, nil, 0o600)) + t.Setenv("RUNNER_TEMP", runnerTemp) + t.Setenv("HOME", home) + t.Setenv("GITHUB_PATH", githubPath) + return installerTestFixture{ + runnerTemp: runnerTemp, + home: home, + githubPath: githubPath, + bundle: installerBundle{ + filename: microsandboxArchive, + url: "https://example.invalid/microsandbox.tar.gz", + }, + } +} + +func (fixture installerTestFixture) installer( + ctx context.Context, + run commandRunner, + output commandOutput, +) *linuxInstaller { + return &linuxInstaller{ctx: ctx, command: run, output: output, bundle: fixture.bundle} +} + +func (fixture *installerTestFixture) cacheArchive(t *testing.T, archive []byte) { + t.Helper() + path := filepath.Join(fixture.runnerTemp, fixture.bundle.filename) + require.NoError(t, os.WriteFile(path, archive, 0o600)) + fixture.bundle.sha256 = installerDigest(archive) +} + +type installerTestRunner struct { + fixture installerTestFixture + archive []byte + partialArchive []byte + failDownload error + failExtraction error + failKVMWrite error + names []string + downloaded bool +} + +func (runner *installerTestRunner) run(cmd *exec.Cmd) error { + name := filepath.Base(cmd.Args[0]) + runner.names = append(runner.names, name) + if name == "curl" { + return runner.downloadArchive(cmd.Args[1:]) + } + if runner.failExtraction != nil && name == installerTarCommand { + return runner.failExtraction + } + switch name { + case installerTarCommand: + return installerExtractFixture(cmd.Args[1:]) + case installerBinaryName: + _, _ = io.WriteString(cmd.Stdout, "MicroSandbox v0.7.7\n") + case installerSudoCommand: + return runner.checkKVM(cmd.Args[1:]) + default: + return errors.New("unexpected installer command: " + name) + } + return nil +} + +func (runner *installerTestRunner) downloadArchive(args []string) error { + want := []string{ + "-fsSL", "--retry", "3", "-o", + filepath.Join(runner.fixture.runnerTemp, runner.fixture.bundle.filename), + runner.fixture.bundle.url, + } + if len(args) != len(want) { + return errors.New("unexpected curl arguments") + } + for index := range want { + if args[index] != want[index] { + return errors.New("unexpected curl arguments") + } + } + runner.downloaded = true + archive := runner.archive + if runner.failDownload != nil { + archive = runner.partialArchive + } + if err := os.WriteFile(want[4], archive, 0o600); err != nil { + return err + } + return runner.failDownload +} + +func (runner *installerTestRunner) checkKVM(args []string) error { + if len(args) != 3 || args[0] != installerTestCommand || + (args[1] != "-r" && args[1] != "-w") || args[2] != "/dev/kvm" { + return errors.New("unexpected KVM check arguments") + } + if args[1] == "-w" { + return runner.failKVMWrite + } + return nil +} + +func installerTarFixture(t *testing.T) []byte { + t.Helper() + var data bytes.Buffer + writer := tar.NewWriter(&data) + for _, entry := range []struct { + name string + mode int64 + body string + }{ + {name: installerBinaryName, mode: 0o755, body: "#!/bin/sh\necho MicroSandbox v0.7.7\n"}, + {name: microsandboxLibrary, mode: 0o644, body: "library fixture"}, + } { + require.NoError(t, writer.WriteHeader(&tar.Header{ + Name: entry.name, Mode: entry.mode, Size: int64(len(entry.body)), Typeflag: tar.TypeReg, + })) + _, err := io.WriteString(writer, entry.body) + require.NoError(t, err) + } + require.NoError(t, writer.Close()) + return data.Bytes() +} + +func installerDigest(data []byte) string { + digest := sha256.Sum256(data) + return hex.EncodeToString(digest[:]) +} + +func installerExtractFixture(args []string) error { + if len(args) != 4 || args[0] != "-xzf" || args[2] != "-C" { + return errors.New("unexpected tar arguments") + } + staging := args[3] + msb := filepath.Join(staging, installerBinaryName) + content := []byte("#!/bin/sh\necho MicroSandbox v0.7.7\n") + if err := os.WriteFile(msb, content, 0o600); err != nil { + return err + } + return os.WriteFile( + filepath.Join(staging, microsandboxLibrary), + []byte("library fixture"), + 0o600, + ) +} + +func installerAssertInstalled(t *testing.T, fixture installerTestFixture) { + t.Helper() + msb := filepath.Join(fixture.home, ".microsandbox", "bin", installerBinaryName) + library := filepath.Join(fixture.home, ".microsandbox", "lib", microsandboxLibrary) + msbInfo, err := os.Stat(msb) + require.NoError(t, err) + require.Equal(t, os.FileMode(0o755), msbInfo.Mode().Perm()) + libraryInfo, err := os.Stat(library) + require.NoError(t, err) + require.Equal(t, os.FileMode(0o644), libraryInfo.Mode().Perm()) + installerAssertLinks(t, fixture.home, msb) + pathBytes, err := os.ReadFile(fixture.githubPath) + require.NoError(t, err) + require.Equal(t, filepath.Join(fixture.home, ".local", "bin")+"\n", string(pathBytes)) +} + +func installerAssertLinks(t *testing.T, home, msb string) { + t.Helper() + for _, link := range []struct{ name, target string }{ + {name: filepath.Join(home, ".microsandbox", "lib", "libkrunfw.so.5"), target: microsandboxLibrary}, + {name: filepath.Join(home, ".microsandbox", "lib", "libkrunfw.so"), target: "libkrunfw.so.5"}, + {name: filepath.Join(home, ".local", "bin", installerBinaryName), target: msb}, + } { + got, err := os.Readlink(link.name) + require.NoError(t, err) + require.Equal(t, link.target, got) + } +} + +func installerAssertOnlyCachedArchive(t *testing.T, dir, archive string) { + t.Helper() + entries, err := os.ReadDir(dir) + require.NoError(t, err) + require.Len(t, entries, 1) + require.Equal(t, archive, entries[0].Name()) +} + +func installerAssertRunnerTempEmpty(t *testing.T, dir string) { + t.Helper() + entries, err := os.ReadDir(dir) + require.NoError(t, err) + require.Empty(t, entries) +} diff --git a/hack/microsandbox/main.go b/hack/microsandbox/main.go new file mode 100644 index 0000000000..464a1d0549 --- /dev/null +++ b/hack/microsandbox/main.go @@ -0,0 +1,124 @@ +package main + +import ( + "context" + "flag" + "fmt" + "io" + "os" + "os/exec" + "runtime" +) + +const ( + installLinuxCommand = "install-linux" + runSuiteCommand = "run" + verifyReportCommand = "verify-report" +) + +func main() { + os.Exit(runCLI(os.Args[1:], os.Stdout, os.Stderr)) +} + +func runCLI(args []string, stdout, stderr io.Writer) int { + if len(args) == 0 { + _, _ = fmt.Fprintln( + stderr, + "usage: devsy-ci-microsandbox ", + ) + return 2 + } + switch args[0] { + case installLinuxCommand: + return installLinuxCLI(args[1:], stdout, stderr) + case runSuiteCommand: + return runSuiteCLI(args[1:], stdout, stderr) + case verifyReportCommand: + return verifyReportCLI(args[1:], stdout, stderr) + default: + _, _ = fmt.Fprintf(stderr, "unknown command: %s\n", args[0]) + return 2 + } +} + +func installLinuxCLI(args []string, stdout, stderr io.Writer) int { + if len(args) != 0 || runtime.GOOS != "linux" { + _, _ = fmt.Fprintln(stderr, "install-linux requires Linux and takes no arguments") + return 2 + } + output := commandOutput{stdout: stdout, stderr: stderr} + err := installLinux(context.Background(), commandRunner((*exec.Cmd).Run), output) + if err != nil { + _, _ = fmt.Fprintf(stderr, "::error::%v\n", err) + } + return commandExitCode(err) +} + +func runSuiteCLI(args []string, stdout, stderr io.Writer) int { + flags := flag.NewFlagSet(runSuiteCommand, flag.ContinueOnError) + flags.SetOutput(stderr) + cfg := suiteConfig{} + flags.StringVar(&cfg.label, "label", "", "Ginkgo suite label") + flags.IntVar(&cfg.expectedCount, "expected-count", 0, "required scenario count") + flags.StringVar(&cfg.repositoryDir, "repository-dir", ".", "Devsy checkout directory") + flags.StringVar( + &cfg.tempDir, + "artifacts-dir", + os.Getenv("RUNNER_TEMP"), + "runner artifact directory", + ) + flags.StringVar( + &cfg.testTimeout, + "test-timeout", + os.Getenv("TEST_TIMEOUT"), + "Ginkgo test deadline", + ) + if err := flags.Parse(args); err != nil { + return 2 + } + if flags.NArg() != 0 { + _, _ = fmt.Fprintln(stderr, "run does not accept positional arguments") + return 2 + } + return runSuite( + context.Background(), + cfg, + commandRunner((*exec.Cmd).Run), + commandOutput{stdout: stdout, stderr: stderr}, + ) +} + +func verifyReportCLI(args []string, stdout, stderr io.Writer) int { + flags := flag.NewFlagSet(verifyReportCommand, flag.ContinueOnError) + flags.SetOutput(stderr) + report := flags.String("report", "", "Ginkgo JSON report path") + label := flags.String("label", "", "Ginkgo suite label") + count := flags.Int("expected-count", 0, "required scenario count") + if err := flags.Parse(args); err != nil { + return 2 + } + if flags.NArg() != 0 || !validReportArguments(*report, *label, *count) { + _, _ = fmt.Fprintln( + stderr, + "verify-report requires a report, label and positive even expected-count", + ) + return 2 + } + if err := validateReport(*report, *label, *count); err != nil { + _, _ = fmt.Fprintf(stderr, "::error::%v\n", err) + return 1 + } + if _, err := fmt.Fprintf( + stdout, + "%s: %d specs passed in one attempt each\n", + *label, + *count, + ); err != nil { + return 1 + } + return 0 +} + +func validReportArguments(report, label string, count int) bool { + return report != "" && label != "" && isPositiveEven(count) +} diff --git a/hack/microsandbox/main_test.go b/hack/microsandbox/main_test.go new file mode 100644 index 0000000000..acb273a8bb --- /dev/null +++ b/hack/microsandbox/main_test.go @@ -0,0 +1,48 @@ +package main + +import ( + "bytes" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestCLIRejectsInvalidArguments(t *testing.T) { + cases := [][]string{ + nil, + {"unknown"}, + {runSuiteCommand, "--unknown-flag"}, + {runSuiteCommand, "unexpected-positional"}, + {verifyReportCommand}, + { + verifyReportCommand, + "--report", + "missing.json", + "--label", + "cli-report", + "--expected-count", + "3", + }, + {installLinuxCommand, "unexpected-positional"}, + } + for _, args := range cases { + var stdout, stderr bytes.Buffer + require.Equal(t, 2, runCLI(args, &stdout, &stderr), "%v: %s", args, stderr.String()) + require.NotEmpty(t, stderr.String()) + require.Empty(t, stdout.String()) + } +} + +func TestCLIVerifyReportRejectsMalformedJSON(t *testing.T) { + path := filepath.Join(t.TempDir(), "report.json") + require.NoError(t, os.WriteFile(path, []byte("{"), 0o600)) + var stdout, stderr bytes.Buffer + code := runCLI([]string{ + verifyReportCommand, "--report", path, "--label", "cli-report", "--expected-count", "2", + }, &stdout, &stderr) + require.Equal(t, 1, code) + require.Contains(t, stderr.String(), "::error::") + require.Empty(t, stdout.String()) +} diff --git a/hack/microsandbox/report.go b/hack/microsandbox/report.go new file mode 100644 index 0000000000..3d6f62ab2d --- /dev/null +++ b/hack/microsandbox/report.go @@ -0,0 +1,285 @@ +package main + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "os" +) + +type ginkgoSuiteConfigReport struct { + DryRun *bool `json:"DryRun"` +} + +type ginkgoSuiteReport struct { + SuiteConfig *ginkgoSuiteConfigReport `json:"SuiteConfig"` + SuiteSucceeded *bool `json:"SuiteSucceeded"` + SuiteHasProgrammaticFocus *bool `json:"SuiteHasProgrammaticFocus"` + SpecReports []json.RawMessage `json:"SpecReports"` +} + +type ginkgoSpecReport struct { + ContainerHierarchyLabels json.RawMessage `json:"ContainerHierarchyLabels"` + LeafNodeLabels json.RawMessage `json:"LeafNodeLabels"` + LeafNodeType string `json:"LeafNodeType"` + State string `json:"State"` + NumAttempts int `json:"NumAttempts"` +} + +type selectedGinkgoSpec struct { + report ginkgoSpecReport + labels map[string]struct{} +} + +func validateReport(path, label string, expectedCount int) error { + if !isPositiveEven(expectedCount) { + return errors.New("expected-count must be a positive even number") + } + specs, err := readSuiteSpecs(path) + if err != nil { + return err + } + selected, err := selectLabeledSpecs(specs, label, expectedCount) + if err != nil { + return err + } + return validateSelectedSpecs(selected, expectedCount) +} + +func selectLabeledSpecs( + specs []json.RawMessage, + label string, + capacity int, +) ([]selectedGinkgoSpec, error) { + selected := make([]selectedGinkgoSpec, 0, capacity) + for index, rawSpec := range specs { + item, err := decodeSelectedSpec(rawSpec, index) + if err != nil { + return nil, err + } + if _, ok := item.labels[label]; ok { + selected = append(selected, item) + } + } + if len(selected) != capacity { + return nil, fmt.Errorf( + "label %q: expected %d specs, found %d", + label, + capacity, + len(selected), + ) + } + return selected, nil +} + +func decodeSelectedSpec(raw json.RawMessage, index int) (selectedGinkgoSpec, error) { + if !isJSONKind(raw, '{') { + return selectedGinkgoSpec{}, fmt.Errorf( + "report contains a malformed spec entry at index %d", + index, + ) + } + var spec ginkgoSpecReport + if err := json.Unmarshal(raw, &spec); err != nil { + return selectedGinkgoSpec{}, fmt.Errorf( + "report contains a malformed spec entry at index %d: %w", + index, + err, + ) + } + labels, err := specLabels(spec) + if err != nil { + return selectedGinkgoSpec{}, fmt.Errorf( + "report contains a malformed spec entry at index %d: %w", + index, + err, + ) + } + return selectedGinkgoSpec{report: spec, labels: labels}, nil +} + +func readSuiteSpecs(path string) ([]json.RawMessage, error) { + data, err := os.ReadFile(path) + if err != nil { + return nil, fmt.Errorf("cannot read Ginkgo JSON report: %w", err) + } + if err := requireReportArray(data); err != nil { + return nil, err + } + var reports []json.RawMessage + if err := json.Unmarshal(data, &reports); err != nil { + return nil, fmt.Errorf("cannot read Ginkgo JSON report: %w", err) + } + if len(reports) != 1 || !isJSONKind(reports[0], '{') { + return nil, errors.New("expected one Ginkgo suite report") + } + return parseSuiteSpecs(reports[0]) +} + +func requireReportArray(data []byte) error { + if isJSONKind(data, '[') { + return nil + } + if !json.Valid(data) { + return fmt.Errorf("cannot read Ginkgo JSON report: %w", json.Unmarshal(data, new(any))) + } + return errors.New("expected one Ginkgo suite report") +} + +func parseSuiteSpecs(data json.RawMessage) ([]json.RawMessage, error) { + var suite ginkgoSuiteReport + if err := json.Unmarshal(data, &suite); err != nil { + return nil, fmt.Errorf("suite report is malformed: %w", err) + } + if err := validateSuiteExecution(suite); err != nil { + return nil, err + } + if suite.SpecReports == nil { + return nil, errors.New("report has no spec list") + } + return suite.SpecReports, nil +} + +func validateSuiteExecution(suite ginkgoSuiteReport) error { + if suite.SuiteConfig == nil || suite.SuiteConfig.DryRun == nil || *suite.SuiteConfig.DryRun { + return errors.New("report does not prove a real test run") + } + if suite.SuiteSucceeded == nil || !*suite.SuiteSucceeded { + return errors.New("suite did not succeed") + } + if suite.SuiteHasProgrammaticFocus == nil || *suite.SuiteHasProgrammaticFocus { + return errors.New("report has programmatic focus") + } + return nil +} + +func validateSelectedSpecs(selected []selectedGinkgoSpec, expectedCount int) error { + providers := [2]int{} + for index, item := range selected { + provider, err := validateSelectedSpec(item, index) + if err != nil { + return err + } + providers[provider]++ + } + if providers[0] != expectedCount/2 || providers[1] != expectedCount/2 { + return fmt.Errorf( + "provider label counts (builtin=%d, external=%d), expected %d each", + providers[0], + providers[1], + expectedCount/2, + ) + } + return nil +} + +func validateSelectedSpec(item selectedGinkgoSpec, index int) (int, error) { + builtin := hasLabel(item.labels, "microsandbox-builtin") + external := hasLabel(item.labels, "microsandbox-external") + if builtin == external { + return 0, fmt.Errorf("selected spec %d must have exactly one provider label", index+1) + } + if item.report.LeafNodeType != "It" { + return 0, fmt.Errorf("selected report %d is not an It spec", index+1) + } + if item.report.State != "passed" { + return 0, fmt.Errorf("selected spec %d state is %q, not passed", index+1, item.report.State) + } + if item.report.NumAttempts != 1 { + return 0, fmt.Errorf( + "selected spec %d used %d attempts, expected one", + index+1, + item.report.NumAttempts, + ) + } + if external { + return 1, nil + } + return 0, nil +} + +func hasLabel(labels map[string]struct{}, label string) bool { + _, ok := labels[label] + return ok +} + +func specLabels(spec ginkgoSpecReport) (map[string]struct{}, error) { + leaves, err := decodeStringLabels(spec.LeafNodeLabels) + if err != nil { + return nil, err + } + groups, err := decodeLabelGroups(spec.ContainerHierarchyLabels) + if err != nil { + return nil, err + } + labels := make(map[string]struct{}, len(leaves)) + for _, label := range leaves { + labels[label] = struct{}{} + } + for _, hierarchy := range groups { + for _, label := range hierarchy { + labels[label] = struct{}{} + } + } + return labels, nil +} + +func decodeStringLabels(raw json.RawMessage) ([]string, error) { + if len(raw) == 0 || isJSONNull(raw) { + return nil, nil + } + if !isJSONKind(raw, '[') { + return nil, errors.New("label field is not a string list") + } + var values []json.RawMessage + if err := json.Unmarshal(raw, &values); err != nil { + return nil, err + } + labels := make([]string, 0, len(values)) + for _, value := range values { + if !isJSONKind(value, '"') { + return nil, errors.New("label list contains a non-string value") + } + var label string + if err := json.Unmarshal(value, &label); err != nil { + return nil, err + } + labels = append(labels, label) + } + return labels, nil +} + +func decodeLabelGroups(raw json.RawMessage) ([][]string, error) { + if len(raw) == 0 || isJSONNull(raw) { + return nil, nil + } + if !isJSONKind(raw, '[') { + return nil, errors.New("container hierarchy labels are not a list") + } + var groups []json.RawMessage + if err := json.Unmarshal(raw, &groups); err != nil { + return nil, err + } + labels := make([][]string, 0, len(groups)) + for _, group := range groups { + if !isJSONKind(group, '[') { + return nil, errors.New("container hierarchy contains a non-list group") + } + decoded, err := decodeStringLabels(group) + if err != nil { + return nil, fmt.Errorf("container hierarchy group: %w", err) + } + labels = append(labels, decoded) + } + return labels, nil +} + +func isJSONNull(raw json.RawMessage) bool { + return bytes.Equal(bytes.TrimSpace(raw), []byte("null")) +} + +func isJSONKind(data []byte, kind byte) bool { + trimmed := bytes.TrimSpace(data) + return len(trimmed) > 0 && trimmed[0] == kind +} diff --git a/hack/microsandbox/report_test.go b/hack/microsandbox/report_test.go new file mode 100644 index 0000000000..f01630a602 --- /dev/null +++ b/hack/microsandbox/report_test.go @@ -0,0 +1,261 @@ +package main + +import ( + "encoding/json" + "os" + "path/filepath" + "strings" + "testing" +) + +const ( + reportTestLabel = "up-provider-microsandbox-images" + numAttemptsKey = "NumAttempts" + realRunError = "real test run" + focusError = "programmatic focus" + builtinProviderLabel = "microsandbox-builtin" + externalProviderLabel = "microsandbox-external" +) + +func TestValidateReportAcceptsExactSuccessfulProviderMatrix(t *testing.T) { + path := writeReportFixture(t, validReportFor(reportTestLabel)) + if err := validateReport(path, reportTestLabel, 2); err != nil { + t.Fatal(err) + } +} + +func TestValidateReportAllowsUnselectedSkippedSpecs(t *testing.T) { + report := validReportFor(reportTestLabel) + suite := report[0].(map[string]any) + specs := suite["SpecReports"].([]any) + specs = append(specs, map[string]any{"State": "skipped", numAttemptsKey: 0}) + suite["SpecReports"] = specs + if err := validateReport(writeReportFixture(t, report), reportTestLabel, 2); err != nil { + t.Fatal(err) + } +} + +func TestValidateReportRejectsMissingMalformedAndWrongRoot(t *testing.T) { + directory := t.TempDir() + assertReportErrorContains(t, filepath.Join(directory, "missing.json"), "cannot read") + malformed := filepath.Join(directory, "malformed.json") + if err := os.WriteFile(malformed, []byte("{"), 0o600); err != nil { + t.Fatal(err) + } + assertReportErrorContains(t, malformed, "cannot read") + for _, value := range []string{`{}`, `[]`, `[{},{}]`, `[null]`} { + path := filepath.Join(directory, "shape.json") + if err := os.WriteFile(path, []byte(value), 0o600); err != nil { + t.Fatal(err) + } + assertReportErrorContains(t, path, "expected one Ginkgo suite") + } +} + +func assertReportErrorContains(t *testing.T, path, want string) { + t.Helper() + err := validateReport(path, reportTestLabel, 2) + if err == nil || !strings.Contains(err.Error(), want) { + t.Fatalf("validateReport error = %v, want %q", err, want) + } +} + +func TestValidateReportRequiresExplicitSuiteEvidence(t *testing.T) { + cases := []struct { + name string + edit func(map[string]any) + want string + }{ + {"missing config", func(s map[string]any) { delete(s, "SuiteConfig") }, realRunError}, + { + "missing dry run field", + func(s map[string]any) { delete(s["SuiteConfig"].(map[string]any), "DryRun") }, + realRunError, + }, + { + "dry run", + func(s map[string]any) { s["SuiteConfig"].(map[string]any)["DryRun"] = true }, + realRunError, + }, + { + "missing suite success", + func(s map[string]any) { delete(s, "SuiteSucceeded") }, + "suite did not succeed", + }, + { + "failed suite", + func(s map[string]any) { s["SuiteSucceeded"] = false }, + "suite did not succeed", + }, + { + "missing focus field", + func(s map[string]any) { delete(s, "SuiteHasProgrammaticFocus") }, + focusError, + }, + {focusError, func(s map[string]any) { s["SuiteHasProgrammaticFocus"] = true }, focusError}, + {"missing spec list", func(s map[string]any) { delete(s, "SpecReports") }, "no spec list"}, + {"null spec list", func(s map[string]any) { s["SpecReports"] = nil }, "no spec list"}, + } + for _, test := range cases { + t.Run(test.name, func(t *testing.T) { + report := validReportFor(reportTestLabel) + test.edit(report[0].(map[string]any)) + err := validateReport(writeReportFixture(t, report), reportTestLabel, 2) + if err == nil || !strings.Contains(err.Error(), test.want) { + t.Fatalf("validateReport() error = %v, want %q", err, test.want) + } + }) + } +} + +func TestValidateReportRejectsMalformedSpecRecords(t *testing.T) { + for _, mutate := range []func(map[string]any){ + func(s map[string]any) { s["LeafNodeLabels"] = "not-a-list" }, + func(s map[string]any) { s["LeafNodeLabels"] = []any{externalProviderLabel, nil} }, + func(s map[string]any) { s["ContainerHierarchyLabels"] = []any{"not-a-label-list"} }, + func(s map[string]any) { s["ContainerHierarchyLabels"] = []any{nil} }, + func(s map[string]any) { s["ContainerHierarchyLabels"] = []any{[]any{"suite-label", nil}} }, + func(s map[string]any) { s["LeafNodeType"] = 7 }, + } { + report := validReportFor(reportTestLabel) + mutate(report[0].(map[string]any)["SpecReports"].([]any)[0].(map[string]any)) + requireReportFailure(t, report, "malformed spec") + } + path := writeReportFixture(t, []any{map[string]any{ + "SuiteConfig": map[string]any{"DryRun": false}, "SuiteSucceeded": true, + "SuiteHasProgrammaticFocus": false, "SpecReports": []any{nil}, + }}) + if err := validateReport( + path, + reportTestLabel, + 2, + ); err == nil || + !strings.Contains(err.Error(), "malformed spec") { + t.Fatalf("non-object spec error = %v", err) + } +} + +func TestValidateReportRejectsSkippedPendingAndFailedSelectedSpecs(t *testing.T) { + for _, state := range []string{"skipped", "pending", "failed"} { + t.Run(state, func(t *testing.T) { + report := validReportFor(reportTestLabel) + report[0].(map[string]any)["SpecReports"].([]any)[0].(map[string]any)["State"] = state + requireReportFailure(t, report, "not passed") + }) + } +} + +func TestValidateReportRejectsRetryAndNonItSelectedSpec(t *testing.T) { + for name, field := range map[string]struct { + key string + value any + want string + }{ + "retry": {key: numAttemptsKey, value: 2, want: "expected one"}, + "not an It": {key: "LeafNodeType", value: "ReportEntry", want: "not an It spec"}, + } { + t.Run(name, func(t *testing.T) { + report := validReportFor(reportTestLabel) + report[0].(map[string]any)["SpecReports"].([]any)[0].(map[string]any)[field.key] = field.value + requireReportFailure(t, report, field.want) + }) + } +} + +func TestValidateReportRejectsWrongLabelAndCount(t *testing.T) { + path := writeReportFixture(t, validReportFor(reportTestLabel)) + for _, test := range []struct { + label string + count int + want string + }{ + {label: "up-provider-microsandbox-storage", count: 2, want: "found 0"}, + {label: reportTestLabel, count: 4, want: "expected 4 specs, found 2"}, + } { + if err := validateReport( + path, + test.label, + test.count, + ); err == nil || + !strings.Contains(err.Error(), test.want) { + t.Errorf("validateReport(%q, %d) = %v", test.label, test.count, err) + } + } +} + +func TestValidateReportRejectsProviderCoverageErrors(t *testing.T) { + cases := []struct { + name string + labels []string + want string + }{ + {"missing", nil, "exactly one provider label"}, + { + "ambiguous", + []string{builtinProviderLabel, externalProviderLabel}, + "exactly one provider label", + }, + {"unbalanced", []string{builtinProviderLabel}, "provider label counts"}, + } + for _, test := range cases { + t.Run(test.name, func(t *testing.T) { + report := validReportFor(reportTestLabel) + report[0].(map[string]any)["SpecReports"].([]any)[1].(map[string]any)["LeafNodeLabels"] = test.labels + requireReportFailure(t, report, test.want) + }) + } +} + +func requireReportFailure(t *testing.T, report any, want string) { + t.Helper() + err := validateReport(writeReportFixture(t, report), reportTestLabel, 2) + if err == nil || !strings.Contains(err.Error(), want) { + t.Fatalf("validateReport error = %v, want %q", err, want) + } +} + +func TestValidateReportRequiresPositiveEvenCount(t *testing.T) { + for _, count := range []int{0, -2, 1, 3} { + if err := validateReport( + "", + reportTestLabel, + count, + ); err == nil || + !strings.Contains(err.Error(), "positive even") { + t.Errorf("expected count %d error = %v", count, err) + } + } +} + +func writeReportFixture(t *testing.T, report any) string { + t.Helper() + data, err := json.Marshal(report) + if err != nil { + t.Fatal(err) + } + path := filepath.Join(t.TempDir(), "report.json") + if err := os.WriteFile(path, data, 0o600); err != nil { + t.Fatal(err) + } + return path +} + +func validReportFor(label string) []any { + providers := []string{builtinProviderLabel, externalProviderLabel} + specs := make([]any, 2) + for index := range specs { + specs[index] = map[string]any{ + "ContainerHierarchyLabels": [][]string{{label}}, + "LeafNodeLabels": []string{providers[index%len(providers)]}, + "LeafNodeType": "It", + "State": "passed", + numAttemptsKey: 1, + } + } + return []any{map[string]any{ + "SuiteConfig": map[string]any{"DryRun": false}, + "SuiteSucceeded": true, + "SuiteHasProgrammaticFocus": false, + "SpecReports": specs, + }} +} diff --git a/hack/microsandbox/run.go b/hack/microsandbox/run.go new file mode 100644 index 0000000000..9dc5a9d342 --- /dev/null +++ b/hack/microsandbox/run.go @@ -0,0 +1,250 @@ +package main + +import ( + "context" + "errors" + "fmt" + "io" + "os" + "os/exec" + "path/filepath" + "regexp" + "strconv" +) + +const managedPolicyLabel = "up-provider-microsandbox-managed-recreate" + +var suiteLabelPattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9-]*$`) + +func runSuite( + ctx context.Context, + cfg suiteConfig, + run commandRunner, + output commandOutput, +) int { + output = normalizedCommandOutput(output) + if err := validateSuiteConfig(cfg); err != nil { + _, _ = fmt.Fprintf(output.stderr, "::error::%v\n", err) + return 2 + } + if run == nil { + _, _ = fmt.Fprintln(output.stderr, "::error::command runner is required") + return 2 + } + + execution := executeSuite(ctx, cfg, run, output) + reportStatus := validateSuiteReport(run, cfg, output) + if execution.testStatus != 0 { + return execution.testStatus + } + if execution.captureErr != nil { + _, _ = fmt.Fprintf( + output.stderr, + "::error::could not capture MicroSandbox test output: %v\n", + execution.captureErr, + ) + return 1 + } + return reportStatus +} + +func normalizedCommandOutput(output commandOutput) commandOutput { + if output.stdout == nil { + output.stdout = io.Discard + } + if output.stderr == nil { + output.stderr = io.Discard + } + return output +} + +type suiteExecution struct { + testStatus int + captureErr error +} + +func executeSuite( + ctx context.Context, + cfg suiteConfig, + run commandRunner, + output commandOutput, +) suiteExecution { + args, err := suiteSudoArguments(cfg) + if err != nil { + _, _ = fmt.Fprintf(output.stderr, "::error::%v\n", err) + return suiteExecution{testStatus: 2} + } + + reportPath := filepath.Join(cfg.tempDir, "microsandbox-"+cfg.label+".json") + logPath := filepath.Join(cfg.tempDir, "microsandbox-"+cfg.label+".log") + logFile, logOpenErr := os.OpenFile(logPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o600) + var capture *suiteOutputCapture + if logFile == nil { + capture = &suiteOutputCapture{console: output.stdout} + capture.record(logOpenErr) + } else { + capture = &suiteOutputCapture{console: output.stdout, log: logFile} + } + + commandArgs := []string{ + "./e2e.test", "-test.v", "-ginkgo.v", "-test.timeout", cfg.testTimeout, + "-ginkgo.fail-on-empty", "-ginkgo.flake-attempts=1", + "--ginkgo.label-filter=" + cfg.label, + "-ginkgo.json-report=" + reportPath, + } + if cfg.label == managedPolicyLabel { + commandArgs = append([]string{"../hack/run-managed-policy-e2e.sh"}, commandArgs...) + } + args = append(args, commandArgs...) + cmd := exec.CommandContext(ctx, "sudo", args...) + cmd.Dir = filepath.Join(cfg.repositoryDir, "e2e") + cmd.Stdout = capture + cmd.Stderr = capture + + commandErr := run(cmd) + testStatus := commandExitCode(commandErr) + if commandErr != nil { + _, _ = fmt.Fprintf( + output.stderr, + "::error::MicroSandbox test command failed: %v\n", + commandErr, + ) + } + if logFile != nil { + capture.record(logFile.Close()) + } + return suiteExecution{testStatus: testStatus, captureErr: capture.err} +} + +func validateSuiteConfig(cfg suiteConfig) error { + if cfg.label == "" || !isValidSuiteLabel(cfg.label) { + return fmt.Errorf("invalid Ginkgo label: %s", cfg.label) + } + if !isPositiveEven(cfg.expectedCount) { + return errors.New("expected-spec-count must be a positive even integer") + } + if !directoryExists(cfg.repositoryDir) { + return errors.New("repository directory is required") + } + if !directoryExists(cfg.tempDir) { + return errors.New("RUNNER_TEMP must name an existing directory") + } + if cfg.testTimeout == "" { + return errors.New("TEST_TIMEOUT must be set by the workflow") + } + return nil +} + +func isPositiveEven(value int) bool { + return value > 0 && value%2 == 0 +} + +func directoryExists(path string) bool { + if path == "" { + return false + } + info, err := os.Stat(path) + return err == nil && info.IsDir() +} + +func isValidSuiteLabel(label string) bool { + return suiteLabelPattern.MatchString(label) +} + +func suiteSudoArguments(cfg suiteConfig) ([]string, error) { + names := []string{ + "DEVSY_REQUIRE_MICROSANDBOX", "GH_USERNAME", "GH_ACCESS_TOKEN", + "GH_CREDENTIAL_USERNAME", "PATH", + } + values := make([]string, 0, len(names)+3) + for _, name := range names { + value, ok := os.LookupEnv(name) + if !ok || value == "" { + return nil, fmt.Errorf("%s must be set by the workflow", name) + } + values = append(values, name+"="+value) + } + kubeconfig := os.Getenv("KUBECONFIG") + if kubeconfig == "" { + home := os.Getenv("HOME") + if home == "" { + return nil, errors.New("HOME must be set to resolve the default KUBECONFIG") + } + kubeconfig = filepath.Join(home, ".kube", "config") + } + values = append(values, "KUBECONFIG="+kubeconfig) + if dockerHost := os.Getenv("DOCKER_HOST"); dockerHost != "" { + values = append(values, "DOCKER_HOST="+dockerHost) + } + if cfg.label == managedPolicyLabel { + values = append(values, + "DEVSY_REQUIRE_MSB_MANAGED_POLICY_FIXTURE=true", + "RUNNER_TEMP="+cfg.tempDir, + ) + } + return values, nil +} + +func validateSuiteReport( + run commandRunner, + cfg suiteConfig, + output commandOutput, +) int { + reportPath := filepath.Join(cfg.tempDir, "microsandbox-"+cfg.label+".json") + uidGid := strconv.Itoa(os.Getuid()) + ":" + strconv.Itoa(os.Getgid()) + if _, err := os.Stat(reportPath); err == nil { + chown := exec.Command("sudo", "chown", uidGid, reportPath) + if err := run(chown); err != nil { + _, _ = fmt.Fprintln( + output.stderr, + "::error::could not make Ginkgo report readable for artifact upload", + ) + return commandExitCode(err) + } + } else if !errors.Is(err, os.ErrNotExist) { + _, _ = fmt.Fprintf(output.stderr, "::error::could not inspect Ginkgo report: %v\n", err) + return 1 + } + if err := validateReport(reportPath, cfg.label, cfg.expectedCount); err != nil { + _, _ = fmt.Fprintf(output.stderr, "::error::%v\n", err) + return 1 + } + if _, err := fmt.Fprintf( + output.stdout, + "%s: %d specs passed in one attempt each\n", + cfg.label, + cfg.expectedCount, + ); err != nil { + return 1 + } + return 0 +} + +type suiteOutputCapture struct { + console io.Writer + log io.Writer + err error +} + +func (capture *suiteOutputCapture) Write(data []byte) (int, error) { + capture.writeTo(capture.console, data) + capture.writeTo(capture.log, data) + return len(data), nil +} + +func (capture *suiteOutputCapture) writeTo(destination io.Writer, data []byte) { + if destination == nil { + return + } + written, err := destination.Write(data) + if err == nil && written != len(data) { + err = io.ErrShortWrite + } + capture.record(err) +} + +func (capture *suiteOutputCapture) record(err error) { + if err != nil && capture.err == nil { + capture.err = err + } +} diff --git a/hack/microsandbox/run_test.go b/hack/microsandbox/run_test.go new file mode 100644 index 0000000000..2b121b5442 --- /dev/null +++ b/hack/microsandbox/run_test.go @@ -0,0 +1,387 @@ +package main + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "io" + "os" + "os/exec" + "path/filepath" + "runtime" + "slices" + "strconv" + "strings" + "testing" +) + +func TestRunSuiteCapturesOutputAndPreservesInvocationContract(t *testing.T) { + cfg, tempDir := newSuiteRunFixture(t, "up-provider-microsandbox-images") + var console, stderr bytes.Buffer + var commands [][]string + run := func(cmd *exec.Cmd) error { + commands = append(commands, append([]string(nil), cmd.Args...)) + if suiteCommandIsChown(cmd) { + return nil + } + if cmd.Dir != filepath.Join(cfg.repositoryDir, "e2e") { + t.Errorf("working directory = %q", cmd.Dir) + } + if err := writeSuiteReportFromCommand(cmd); err != nil { + return err + } + _, _ = io.WriteString(cmd.Stdout, "fixture stdout\n") + _, _ = io.WriteString(cmd.Stderr, "fixture stderr\n") + return nil + } + + if code := runSuite( + context.Background(), + cfg, + run, + commandOutput{stdout: &console, stderr: &stderr}, + ); code != 0 { + t.Fatalf("runSuite() = %d; stderr=%s", code, stderr.String()) + } + if len(commands) != 2 || !suiteCommandIsChownArgs(commands[1]) { + t.Fatalf("commands = %#v", commands) + } + command := commands[0] + assertSuiteInvocation(t, command, cfg, tempDir) + assertSuiteOutput(t, console.String(), stderr.String(), cfg) +} + +func assertSuiteInvocation(t *testing.T, command []string, cfg suiteConfig, tempDir string) { + t.Helper() + expectedCommand := []string{ + "./e2e.test", "-test.v", "-ginkgo.v", "-test.timeout", cfg.testTimeout, + "-ginkgo.fail-on-empty", "-ginkgo.flake-attempts=1", + "--ginkgo.label-filter=" + cfg.label, + "-ginkgo.json-report=" + filepath.Join(tempDir, "microsandbox-"+cfg.label+".json"), + } + start := slices.Index(command, "./e2e.test") + if start < 0 || !slices.Equal(command[start:], expectedCommand) { + t.Fatalf("test command args = %v, want suffix %v", command, expectedCommand) + } + expectedEnvironment := []string{ + "DEVSY_REQUIRE_MICROSANDBOX=true", "GH_USERNAME=owner", "GH_ACCESS_TOKEN=never-print-token", + "GH_CREDENTIAL_USERNAME=x-access-token", "PATH=/bin", "KUBECONFIG=/tmp/kubeconfig", + "DOCKER_HOST=unix:///tmp/docker.sock", + } + if !slices.Equal(command[1:start], expectedEnvironment) { + t.Fatalf("sudo environment = %v, want exactly %v", command[1:start], expectedEnvironment) + } + if slices.Contains(command[1:start], "UNRELATED_SECRET=not-forwarded") { + t.Fatal("unrelated secret was forwarded to sudo") + } +} + +func assertSuiteOutput(t *testing.T, console, stderr string, cfg suiteConfig) { + t.Helper() + log, err := os.ReadFile(filepath.Join(cfg.tempDir, "microsandbox-"+cfg.label+".log")) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(log), "fixture stdout") || + !strings.Contains(string(log), "fixture stderr") || + !strings.Contains(console, "fixture stdout") || + !strings.Contains(console, "fixture stderr") { + t.Fatalf("combined output missing from log or console: log=%q console=%q", log, console) + } + if strings.Contains(console+stderr+string(log), "never-print-token") { + t.Fatal("test credential leaked to output") + } + if !strings.Contains(console, cfg.label+": 2 specs passed in one attempt each") { + t.Fatalf("success validation message missing: %q", console) + } +} + +func TestRunSuiteManagedPolicyUsesWrapperAndFixtureEnvironment(t *testing.T) { + cfg, tempDir := newSuiteRunFixture(t, managedPolicyLabel) + var commands [][]string + run := func(cmd *exec.Cmd) error { + commands = append(commands, append([]string(nil), cmd.Args...)) + if suiteCommandIsChown(cmd) { + return nil + } + return writeSuiteReportFromCommand(cmd) + } + var stdout, stderr bytes.Buffer + if code := runSuite( + context.Background(), + cfg, + run, + commandOutput{stdout: &stdout, stderr: &stderr}, + ); code != 0 { + t.Fatalf("runSuite() = %d; stderr=%s", code, stderr.String()) + } + if len(commands) != 2 || !stringArgsContain(commands[0], "../hack/run-managed-policy-e2e.sh") { + t.Fatalf("managed invocation = %#v", commands) + } + for _, want := range []string{ + "DEVSY_REQUIRE_MSB_MANAGED_POLICY_FIXTURE=true", "RUNNER_TEMP=" + tempDir, + } { + if !stringArgsContain(commands[0], want) { + t.Errorf("managed invocation lacks %q: %v", want, commands[0]) + } + } +} + +func TestRunSuitePreservesNativeExitPrecedenceAndArtifacts(t *testing.T) { + cfg, tempDir := newSuiteRunFixture(t, "up-provider-microsandbox-images") + var console, stderr bytes.Buffer + run := func(cmd *exec.Cmd) error { + if suiteCommandIsChown(cmd) { + return nil + } + if err := writeSuiteReportFromCommand(cmd); err != nil { + return err + } + _, _ = io.WriteString(cmd.Stdout, "retained output\n") + return exitWithStatus(23) + } + code := runSuite( + context.Background(), + cfg, + run, + commandOutput{stdout: &console, stderr: &stderr}, + ) + if code != 23 { + t.Fatalf("runSuite() = %d, want native failure status 23", code) + } + assertArtifactsRetained(t, tempDir, cfg.label) + assertNativeFailureReported(t, stderr.String(), console.String(), cfg.label) +} + +func assertArtifactsRetained(t *testing.T, tempDir, label string) { + t.Helper() + for _, name := range []string{ + "microsandbox-" + label + ".json", + "microsandbox-" + label + ".log", + } { + if info, err := os.Stat(filepath.Join(tempDir, name)); err != nil || info.IsDir() { + t.Errorf("artifact %s not retained: info=%v err=%v", name, info, err) + } + } +} + +func assertNativeFailureReported(t *testing.T, stderr, console, label string) { + t.Helper() + if !strings.Contains(stderr, "test command failed") { + t.Fatalf("native failure was not recorded: %q", stderr) + } + if !strings.Contains(console, label+": 2 specs passed in one attempt each") { + t.Fatalf("readable report was not validated after test failure: %q", console) + } +} + +func TestRunSuiteReportsMissingReportAfterNativeFailure(t *testing.T) { + cfg, _ := newSuiteRunFixture(t, "up-provider-microsandbox-images") + var stderr bytes.Buffer + run := func(cmd *exec.Cmd) error { + if suiteCommandIsChown(cmd) { + t.Fatal("chown ran without a report") + } + return exitWithStatus(23) + } + if code := runSuite( + context.Background(), + cfg, + run, + commandOutput{stderr: &stderr}, + ); code != 23 { + t.Fatalf("runSuite() = %d", code) + } + if !strings.Contains(stderr.String(), "cannot read Ginkgo JSON report") || + !strings.Contains(stderr.String(), "test command failed") { + t.Fatalf("native and report failures not both retained: %q", stderr.String()) + } +} + +func TestRunSuiteReturnsReportErrorsAfterSuccessfulE2E(t *testing.T) { + t.Run("missing report", func(t *testing.T) { + cfg, _ := newSuiteRunFixture(t, "up-provider-microsandbox-images") + var stderr bytes.Buffer + run := func(cmd *exec.Cmd) error { + if suiteCommandIsChown(cmd) { + t.Fatal("chown ran without a report") + } + return nil + } + if code := runSuite( + context.Background(), + cfg, + run, + commandOutput{stderr: &stderr}, + ); code != 1 { + t.Fatalf("runSuite() = %d", code) + } + if !strings.Contains(stderr.String(), "cannot read Ginkgo JSON report") { + t.Fatalf("missing report error = %q", stderr.String()) + } + }) + t.Run("report chown failure", func(t *testing.T) { + cfg, _ := newSuiteRunFixture(t, "up-provider-microsandbox-images") + var stderr bytes.Buffer + run := func(cmd *exec.Cmd) error { + if suiteCommandIsChown(cmd) { + return exitWithStatus(43) + } + return writeSuiteReportFromCommand(cmd) + } + if code := runSuite( + context.Background(), + cfg, + run, + commandOutput{stderr: &stderr}, + ); code != 43 { + t.Fatalf("runSuite() = %d, want chown exit status 43", code) + } + if !strings.Contains(stderr.String(), "could not make Ginkgo report readable") { + t.Fatalf("chown failure not reported: %q", stderr.String()) + } + }) +} + +func TestRunSuiteContinuesAfterOutputLogFailure(t *testing.T) { + cfg, tempDir := newSuiteRunFixture(t, "up-provider-microsandbox-images") + if err := os.Mkdir( + filepath.Join(tempDir, "microsandbox-"+cfg.label+".log"), + 0o700, + ); err != nil { + t.Fatal(err) + } + var console, stderr bytes.Buffer + var testStarted bool + run := func(cmd *exec.Cmd) error { + if suiteCommandIsChown(cmd) { + return nil + } + testStarted = true + if err := writeSuiteReportFromCommand(cmd); err != nil { + return err + } + _, _ = io.WriteString(cmd.Stdout, "console remains live\n") + return nil + } + code := runSuite( + context.Background(), + cfg, + run, + commandOutput{stdout: &console, stderr: &stderr}, + ) + if code == 0 || !testStarted || !strings.Contains(console.String(), "console remains live") { + t.Fatalf("runSuite() = %d, started=%t, console=%q", code, testStarted, console.String()) + } + if !strings.Contains(stderr.String(), "could not capture MicroSandbox test output") { + t.Fatalf("capture failure not reported: %q", stderr.String()) + } +} + +func TestRunSuiteOutputWriterFailureDoesNotMaskNativeExit(t *testing.T) { + cfg, _ := newSuiteRunFixture(t, "up-provider-microsandbox-images") + var stderr bytes.Buffer + run := func(cmd *exec.Cmd) error { + if suiteCommandIsChown(cmd) { + return nil + } + if err := writeSuiteReportFromCommand(cmd); err != nil { + return err + } + _, _ = io.WriteString(cmd.Stdout, "writer error") + return exitWithStatus(23) + } + code := runSuite( + context.Background(), + cfg, + run, + commandOutput{stdout: failingWriter{}, stderr: &stderr}, + ) + if code != 23 || !strings.Contains(stderr.String(), "test command failed") { + t.Fatalf("runSuite() = %d, stderr=%q", code, stderr.String()) + } +} + +func TestRunSuiteReportsMissingRequiredEnvironmentBeforeInvocation(t *testing.T) { + cfg, _ := newSuiteRunFixture(t, "up-provider-microsandbox-images") + t.Setenv("GH_USERNAME", "") + var stderr bytes.Buffer + run := func(*exec.Cmd) error { + t.Fatal("test command ran with missing required environment") + return nil + } + if code := runSuite(context.Background(), cfg, run, commandOutput{stderr: &stderr}); code != 2 { + t.Fatalf("runSuite() = %d", code) + } + if !strings.Contains(stderr.String(), "GH_USERNAME must be set") { + t.Fatalf("missing environment error = %q", stderr.String()) + } +} + +func newSuiteRunFixture(t *testing.T, label string) (suiteConfig, string) { + t.Helper() + tempDir := t.TempDir() + repository := t.TempDir() + if err := os.Mkdir(filepath.Join(repository, "e2e"), 0o700); err != nil { + t.Fatal(err) + } + t.Setenv("DEVSY_REQUIRE_MICROSANDBOX", "true") + t.Setenv("GH_USERNAME", "owner") + t.Setenv("GH_ACCESS_TOKEN", "never-print-token") + t.Setenv("GH_CREDENTIAL_USERNAME", "x-access-token") + t.Setenv("KUBECONFIG", "/tmp/kubeconfig") + t.Setenv("PATH", "/bin") + t.Setenv("DOCKER_HOST", "unix:///tmp/docker.sock") + t.Setenv("UNRELATED_SECRET", "not-forwarded") + t.Setenv("HOME", t.TempDir()) + return suiteConfig{ + label: label, expectedCount: 2, repositoryDir: repository, + tempDir: tempDir, testTimeout: "1200s", + }, tempDir +} + +func writeSuiteReportFromCommand(cmd *exec.Cmd) error { + var reportPath, label string + for _, arg := range cmd.Args { + if value, ok := strings.CutPrefix(arg, "-ginkgo.json-report="); ok { + reportPath = value + } + if value, ok := strings.CutPrefix(arg, "--ginkgo.label-filter="); ok { + label = value + } + } + if reportPath == "" || label == "" { + return errors.New("Ginkgo report or label argument missing") + } + data, err := json.Marshal(validReportFor(label)) + if err != nil { + return err + } + return os.WriteFile(reportPath, data, 0o600) +} + +func suiteCommandIsChown(cmd *exec.Cmd) bool { + return suiteCommandIsChownArgs(cmd.Args) +} + +func suiteCommandIsChownArgs(args []string) bool { + return len(args) > 1 && args[1] == "chown" +} + +func stringArgsContain(args []string, want string) bool { + return slices.Contains(args, want) +} + +type failingWriter struct{} + +func (failingWriter) Write([]byte) (int, error) { return 0, errors.New("fixture writer failure") } + +func exitWithStatus(status int) error { + if runtime.GOOS == "windows" { + // The suite fixture deliberately replaces PATH; locate the native shell explicitly. + shell := filepath.Join(os.Getenv("SystemRoot"), "System32", "cmd.exe") + return exec.Command(shell, "/c", "exit", strconv.Itoa(status)).Run() + } + return exec.Command("sh", "-c", "exit "+strconv.Itoa(status)).Run() +} diff --git a/hack/microsandbox/test_verify_report.py b/hack/microsandbox/test_verify_report.py deleted file mode 100644 index 15a2720cfb..0000000000 --- a/hack/microsandbox/test_verify_report.py +++ /dev/null @@ -1,125 +0,0 @@ -import json -from pathlib import Path -import tempfile -import unittest - -from verify_report import validate_report - - -LABEL = "up-provider-microsandbox-images" -PROVIDERS = ("microsandbox-builtin", "microsandbox-external") - - -def report(count=2): - return [{ - "SuiteConfig": {"DryRun": False}, - "SuiteSucceeded": True, - "SuiteHasProgrammaticFocus": False, - "SpecReports": [ - { - "ContainerHierarchyLabels": [[LABEL]], - "LeafNodeLabels": [PROVIDERS[index % 2]], - "LeafNodeType": "It", - "State": "passed", - "NumAttempts": 1, - } - for index in range(count) - ], - }] - - -class VerifyReportTest(unittest.TestCase): - def setUp(self): - self.directory = tempfile.TemporaryDirectory() - self.path = Path(self.directory.name) / "report.json" - - def tearDown(self): - self.directory.cleanup() - - def write(self, value): - self.path.write_text(json.dumps(value), encoding="utf-8") - - def test_accepts_exact_successful_label_count(self): - self.write(report()) - self.assertEqual(validate_report(self.path, LABEL, 2), 2) - - def test_rejects_missing_report(self): - with self.assertRaisesRegex(ValueError, "cannot read"): - validate_report(self.path, LABEL, 2) - - def test_rejects_malformed_report(self): - self.path.write_text("{", encoding="utf-8") - with self.assertRaisesRegex(ValueError, "cannot read"): - validate_report(self.path, LABEL, 2) - - def test_rejects_skipped_or_pending_spec(self): - for state in ("skipped", "pending"): - data = report() - data[0]["SpecReports"][0]["State"] = state - self.write(data) - with self.subTest(state=state), self.assertRaisesRegex(ValueError, "not passed"): - validate_report(self.path, LABEL, 2) - - def test_rejects_failed_suite_or_spec(self): - data = report() - data[0]["SuiteSucceeded"] = False - self.write(data) - with self.assertRaisesRegex(ValueError, "suite did not succeed"): - validate_report(self.path, LABEL, 2) - data = report() - data[0]["SpecReports"][0]["State"] = "failed" - self.write(data) - with self.assertRaisesRegex(ValueError, "not passed"): - validate_report(self.path, LABEL, 2) - - def test_rejects_dry_run(self): - data = report() - data[0]["SuiteConfig"]["DryRun"] = True - self.write(data) - with self.assertRaisesRegex(ValueError, "real test run"): - validate_report(self.path, LABEL, 2) - - def test_rejects_programmatic_focus(self): - data = report() - data[0]["SuiteHasProgrammaticFocus"] = True - self.write(data) - with self.assertRaisesRegex(ValueError, "programmatic focus"): - validate_report(self.path, LABEL, 2) - - def test_rejects_retry(self): - data = report() - data[0]["SpecReports"][1]["NumAttempts"] = 2 - self.write(data) - with self.assertRaisesRegex(ValueError, "expected one"): - validate_report(self.path, LABEL, 2) - - def test_rejects_wrong_label(self): - self.write(report()) - with self.assertRaisesRegex(ValueError, "found 0"): - validate_report(self.path, "up-provider-microsandbox-storage", 2) - - def test_rejects_wrong_count(self): - self.write(report()) - with self.assertRaisesRegex(ValueError, "expected 3 specs, found 2"): - validate_report(self.path, LABEL, 3) - - def test_rejects_missing_provider_coverage(self): - data = report() - data[0]["SpecReports"][1]["LeafNodeLabels"] = [PROVIDERS[0]] - self.write(data) - with self.assertRaisesRegex(ValueError, "provider label counts"): - validate_report(self.path, LABEL, 2) - - def test_rejects_ambiguous_or_missing_provider_label(self): - for labels in ([], list(PROVIDERS)): - data = report() - data[0]["SpecReports"][0]["LeafNodeLabels"] = labels - self.write(data) - with self.subTest(labels=labels), self.assertRaisesRegex( - ValueError, "exactly one provider label" - ): - validate_report(self.path, LABEL, 2) - - -if __name__ == "__main__": - unittest.main() diff --git a/hack/microsandbox/verify_report.py b/hack/microsandbox/verify_report.py deleted file mode 100644 index e6f8b90d99..0000000000 --- a/hack/microsandbox/verify_report.py +++ /dev/null @@ -1,106 +0,0 @@ -#!/usr/bin/env python3 -"""Validate that one MicroSandbox Ginkgo label completed its expected specs.""" - -import argparse -import json -from pathlib import Path -import sys - - -def _spec_labels(spec): - labels = spec.get("LeafNodeLabels") or [] - hierarchy = spec.get("ContainerHierarchyLabels") or [] - if not isinstance(labels, list) or any(not isinstance(label, str) for label in labels): - raise ValueError("spec report has malformed leaf labels") - if not isinstance(hierarchy, list) or any( - not isinstance(group, list) or any(not isinstance(label, str) for label in group) - for group in hierarchy - ): - raise ValueError("spec report has malformed container labels") - result = set(labels) - for group in hierarchy: - result.update(group) - return result - - -def _read_suite_specs(path): - try: - reports = json.loads(Path(path).read_text(encoding="utf-8")) - except (OSError, UnicodeError, json.JSONDecodeError) as exc: - raise ValueError(f"cannot read Ginkgo JSON report: {exc}") from exc - - if not isinstance(reports, list) or len(reports) != 1 or not isinstance(reports[0], dict): - raise ValueError("expected one Ginkgo suite report") - suite = reports[0] - config = suite.get("SuiteConfig") - if not isinstance(config, dict) or config.get("DryRun") is not False: - raise ValueError("report does not prove a real test run") - if suite.get("SuiteSucceeded") is not True: - raise ValueError("Ginkgo suite did not succeed") - if suite.get("SuiteHasProgrammaticFocus") is not False: - raise ValueError("report has programmatic focus") - specs = suite.get("SpecReports") - if not isinstance(specs, list): - raise ValueError("report has no spec list") - return specs - - -def _select_specs(specs, expected_label, expected_count): - if any(not isinstance(spec, dict) for spec in specs): - raise ValueError("report contains a malformed spec entry") - selected = [spec for spec in specs if expected_label in _spec_labels(spec)] - if len(selected) != expected_count: - raise ValueError( - f"label {expected_label!r}: expected {expected_count} specs, found {len(selected)}" - ) - return selected - - -def _validate_selected_specs(selected, expected_count): - providers = {"microsandbox-builtin": 0, "microsandbox-external": 0} - for index, spec in enumerate(selected, start=1): - spec_labels = _spec_labels(spec) - provider_labels = spec_labels & providers.keys() - if len(provider_labels) != 1: - raise ValueError(f"selected spec {index} must have exactly one provider label") - providers[next(iter(provider_labels))] += 1 - if spec.get("LeafNodeType") != "It": - raise ValueError(f"selected report {index} is not an It spec") - if spec.get("State") != "passed": - raise ValueError(f"selected spec {index} state is {spec.get('State')!r}, not passed") - if spec.get("NumAttempts") != 1: - raise ValueError( - f"selected spec {index} used {spec.get('NumAttempts')!r} attempts, expected one" - ) - if any(count != expected_count // 2 for count in providers.values()): - raise ValueError( - f"provider label counts are {providers}, expected {expected_count // 2} each" - ) - return len(selected) - - -def validate_report(path, expected_label, expected_count): - specs = _read_suite_specs(path) - selected = _select_specs(specs, expected_label, expected_count) - return _validate_selected_specs(selected, expected_count) - - -def main(argv=None): - parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument("--report", required=True, type=Path) - parser.add_argument("--label", required=True) - parser.add_argument("--expected-count", required=True, type=int) - args = parser.parse_args(argv) - if args.expected_count <= 0 or args.expected_count % 2: - parser.error("--expected-count must be a positive even number") - try: - count = validate_report(args.report, args.label, args.expected_count) - except ValueError as exc: - print(f"::error::{exc}", file=sys.stderr) - return 1 - print(f"{args.label}: {count} specs passed in one attempt each") - return 0 - - -if __name__ == "__main__": - raise SystemExit(main()) diff --git a/hack/provider_update_ci/capture.go b/hack/provider_update_ci/capture.go new file mode 100644 index 0000000000..8b755d496a --- /dev/null +++ b/hack/provider_update_ci/capture.go @@ -0,0 +1,196 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "maps" + "strings" + "sync" +) + +type testEvent struct { + Action string `json:"Action"` + Package string `json:"Package"` + Test string `json:"Test"` + Output string `json:"Output"` +} + +type testID struct { + packagePath string + name string +} + +type outputCapture struct { + mu sync.Mutex + log io.Writer + stdout io.Writer + stderr io.Writer + stdoutPending []byte + stderrPending []byte + states map[testID]testState + listed map[testID]struct{} + writeErr error +} + +func newOutputCapture(log, stdout, stderr io.Writer) *outputCapture { + return &outputCapture{ + log: log, stdout: stdout, stderr: stderr, + states: make(map[testID]testState), listed: make(map[testID]struct{}), + } +} + +type streamWriter struct { + capture *outputCapture + stdout bool +} + +func (c *outputCapture) stdoutWriter() io.Writer { return streamWriter{capture: c, stdout: true} } +func (c *outputCapture) stderrWriter() io.Writer { return streamWriter{capture: c} } + +func (w streamWriter) Write(p []byte) (int, error) { + c := w.capture + c.mu.Lock() + defer c.mu.Unlock() + + console := c.stderr + if w.stdout { + console = c.stdout + } + if err := writeAll(console, p); err != nil { + c.recordError(fmt.Errorf("write to console: %w", err)) + } + c.writeArtifact(p, w.stdout) + return len(p), nil +} + +func writeAll(writer io.Writer, data []byte) error { + for len(data) > 0 { + n, err := writer.Write(data) + data = data[n:] + if err != nil { + return err + } + if n == 0 { + return io.ErrShortWrite + } + } + return nil +} + +func (c *outputCapture) writeArtifact(data []byte, stdout bool) { + pending := &c.stderrPending + if stdout { + pending = &c.stdoutPending + } + *pending = append(*pending, data...) + for { + newline := bytes.IndexByte(*pending, '\n') + if newline < 0 { + return + } + line := (*pending)[:newline+1] + if stdout { + c.readEvent(line[:newline]) + } + if err := writeAll(c.log, line); err != nil { + c.recordError(fmt.Errorf("write artifact: %w", err)) + } + *pending = (*pending)[newline+1:] + } +} + +func (c *outputCapture) readEvent(line []byte) { + var event testEvent + if json.Unmarshal(bytes.TrimSpace(line), &event) != nil || event.Package == "" { + return + } + if event.Test == "" { + c.readListedTests(event.Package, event.Output) + return + } + id := testID{packagePath: event.Package, name: event.Test} + c.applyTestEvent(id, event.Action) +} + +func (c *outputCapture) applyTestEvent(id testID, action string) { + state := c.states[id] + switch action { + case "run": + state.started = true + case "pass": + state.passed = true + case "skip": + state.skipped = true + case "fail": + state.failed = true + default: + return + } + c.states[id] = state +} + +func (c *outputCapture) readListedTests(packagePath, output string) { + for line := range strings.SplitSeq(output, "\n") { + name := strings.TrimSuffix(line, "\r") + if isListedTestName(name) { + c.listed[testID{packagePath: packagePath, name: name}] = struct{}{} + } + } +} + +func isListedTestName(name string) bool { + if !strings.HasPrefix(name, "Test") || strings.ContainsAny(name, "/ \t\r\n") { + return false + } + return true +} + +func (c *outputCapture) finish() { + c.mu.Lock() + defer c.mu.Unlock() + for _, stream := range []struct { + pending *[]byte + stdout bool + }{{&c.stdoutPending, true}, {&c.stderrPending, false}} { + if len(*stream.pending) == 0 { + continue + } + if stream.stdout { + c.readEvent(*stream.pending) + } + if err := writeAll(c.log, *stream.pending); err != nil { + c.recordError(fmt.Errorf("write artifact: %w", err)) + } + *stream.pending = nil + } +} + +func (c *outputCapture) events() map[testID]testState { + c.mu.Lock() + defer c.mu.Unlock() + snapshot := make(map[testID]testState, len(c.states)) + maps.Copy(snapshot, c.states) + return snapshot +} + +func (c *outputCapture) listedTests() map[testID]struct{} { + c.mu.Lock() + defer c.mu.Unlock() + snapshot := make(map[testID]struct{}, len(c.listed)) + maps.Copy(snapshot, c.listed) + return snapshot +} + +func (c *outputCapture) err() error { + c.mu.Lock() + defer c.mu.Unlock() + return c.writeErr +} + +func (c *outputCapture) recordError(err error) { + if c.writeErr == nil { + c.writeErr = err + } +} diff --git a/hack/provider_update_ci/coverage.go b/hack/provider_update_ci/coverage.go new file mode 100644 index 0000000000..c22a62eabd --- /dev/null +++ b/hack/provider_update_ci/coverage.go @@ -0,0 +1,69 @@ +package main + +import ( + "fmt" + "sort" +) + +type testState struct { + started bool + passed bool + skipped bool + failed bool +} + +func validateTestCoverage(expected map[testID]struct{}, events map[testID]testState) []string { + if len(expected) == 0 { + return []string{"go test produced no discoverable test roots"} + } + + var issues []string + for id := range expected { + if _, ok := events[id]; !ok { + issues = append( + issues, + fmt.Sprintf( + "discovered test produced no JSON events: %s (%s)", + id.name, + id.packagePath, + ), + ) + } + } + + for id, state := range events { + issues = append(issues, observedTestIssues(id, state)...) + } + + sort.Strings(issues) + return issues +} + +func observedTestIssues(id testID, state testState) []string { + var issues []string + if state.skipped { + issues = append( + issues, + fmt.Sprintf("observed test was skipped: %s (%s)", id.name, id.packagePath), + ) + } + if state.failed { + issues = append( + issues, + fmt.Sprintf("observed test failed: %s (%s)", id.name, id.packagePath), + ) + } + if !state.started { + issues = append( + issues, + fmt.Sprintf("observed test did not start: %s (%s)", id.name, id.packagePath), + ) + } + if !state.passed { + issues = append( + issues, + fmt.Sprintf("observed test did not pass: %s (%s)", id.name, id.packagePath), + ) + } + return issues +} diff --git a/hack/provider_update_ci/coverage_test.go b/hack/provider_update_ci/coverage_test.go new file mode 100644 index 0000000000..b19418dd85 --- /dev/null +++ b/hack/provider_update_ci/coverage_test.go @@ -0,0 +1,77 @@ +package main + +import ( + "io" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestCoverageIncludesDiscoveredRootsAndEverySubtest(t *testing.T) { + root := fixtureTests[0] + child := testID{packagePath: root.packagePath, name: root.name + "/new_case"} + expected := map[testID]struct{}{root: {}} + events := map[testID]testState{ + root: {started: true, passed: true}, + child: {started: true, skipped: true}, + } + require.NotEmpty( + t, + validateTestCoverage(expected, events), + "a new skipped subtest must fail without a registry edit", + ) + events[child] = testState{started: true, passed: true} + require.Empty(t, validateTestCoverage(expected, events)) +} + +func TestCoverageFailsClosed(t *testing.T) { + id := fixtureTests[0] + for name, state := range map[string]testState{ + "missing run": {passed: true}, + "unfinished": {started: true}, + "skipped": {started: true, skipped: true}, + "failed": {started: true, failed: true}, + } { + t.Run(name, func(t *testing.T) { + require.NotEmpty( + t, + validateTestCoverage(map[testID]struct{}{id: {}}, map[testID]testState{id: state}), + ) + }) + } + require.NotEmpty(t, validateTestCoverage(nil, nil)) + require.NotEmpty(t, validateTestCoverage(map[testID]struct{}{id: {}}, nil)) +} + +func TestCoverageCannotSubstituteAnotherPackage(t *testing.T) { + id := fixtureTests[0] + other := testID{packagePath: providerUpdatePackages[1], name: id.name} + require.NotEmpty(t, validateTestCoverage( + map[testID]struct{}{id: {}}, + map[testID]testState{other: {started: true, passed: true}}, + )) +} + +func TestCaptureDiscoversRootsAndSeparatesPackages(t *testing.T) { + capture := newOutputCapture(io.Discard, io.Discard, io.Discard) + capture.readEvent([]byte(`{"Action":"output","Package":"one","Output":"TestNewCase\n"}`)) + capture.readEvent([]byte(`{"Action":"output","Package":"two","Output":"TestNewCase\n"}`)) + capture.readEvent( + []byte(`{"Action":"output","Package":"one","Test":"TestParent","Output":"TestBogus\n"}`), + ) + require.Len(t, capture.listedTests(), 2) + for _, name := range []string{"TestNewCase/sub", "Test new", "ok package", ""} { + require.False(t, isListedTestName(name)) + } +} + +func TestCLIDiscoveryFailsClosed(t *testing.T) { + for _, mode := range []string{"empty-discovery", "missing-package", "discovery-failure"} { + t.Run(mode, func(t *testing.T) { + setUpFakeGo(t, mode, "0") + _, stderr, code := runCLIProcess(t, "--artifacts-dir", t.TempDir()) + require.NotZero(t, code) + require.Contains(t, stderr, "discover") + }) + } +} diff --git a/hack/provider_update_ci/discovery.go b/hack/provider_update_ci/discovery.go new file mode 100644 index 0000000000..88b0992f10 --- /dev/null +++ b/hack/provider_update_ci/discovery.go @@ -0,0 +1,40 @@ +package main + +import ( + "fmt" + "io" + "os/exec" +) + +func providerTestCommand(selection string) *exec.Cmd { + args := []string{"test", "-race", "-json", "-count=1", "-timeout", windowsTestTimeout} + args = append(args, providerUpdatePackages...) + args = append(args, selection, providerUpdateRunPattern) + return exec.Command("go", args...) +} + +func discoverProviderTests(log, stdout, stderr io.Writer) (map[testID]struct{}, error) { + capture := newOutputCapture(log, stdout, stderr) + command := providerTestCommand("-list") + command.Stdout = capture.stdoutWriter() + command.Stderr = capture.stderrWriter() + err := command.Run() + capture.finish() + if err != nil { + return nil, fmt.Errorf("discover provider update tests: %w", err) + } + if err := capture.err(); err != nil { + return nil, fmt.Errorf("capture provider update test discovery: %w", err) + } + expected := capture.listedTests() + packages := make(map[string]bool) + for id := range expected { + packages[id.packagePath] = true + } + for _, name := range providerUpdatePackages { + if !packages[name] { + return nil, fmt.Errorf("no provider update tests discovered in %s", name) + } + } + return expected, nil +} diff --git a/hack/provider_update_ci/main.go b/hack/provider_update_ci/main.go new file mode 100644 index 0000000000..f3449a11b6 --- /dev/null +++ b/hack/provider_update_ci/main.go @@ -0,0 +1,209 @@ +package main + +import ( + "errors" + "flag" + "fmt" + "io" + "os" + "os/exec" + "path/filepath" + "strconv" +) + +const ( + defaultArtifactName = "provider-update-windows" + windowsTestTimeout = "10m" + providerUpdateRunPattern = "^(TestProviderUpdate|TestStatUpdatePath|" + + "TestExtractConfinedUpdateTar|TestGNUArchiveFixture)" +) + +var providerUpdatePackages = []string{ + "github.com/devsy-org/devsy/pkg/workspace", + "github.com/devsy-org/devsy/pkg/provider", +} + +func main() { + os.Exit(runCLI(os.Args[1:])) +} + +func runCLI(args []string) int { + flags := flag.NewFlagSet("provider-update-ci", flag.ContinueOnError) + flags.SetOutput(os.Stderr) + artifactsDir := os.Getenv("RUNNER_TEMP") + flags.StringVar( + &artifactsDir, "artifacts-dir", artifactsDir, "directory for test output artifacts", + ) + if err := flags.Parse(args); err != nil { + return 2 + } + if flags.NArg() != 0 { + reportf(os.Stderr, "unexpected positional arguments\n") + return 2 + } + if artifactsDir == "" { + reportf(os.Stderr, "--artifacts-dir or RUNNER_TEMP is required\n") + return 2 + } + return runProviderUpdateTests(artifactsDir, os.Stdout, os.Stderr) +} + +func runProviderUpdateTests(artifactsDir string, stdout, stderr io.Writer) int { + logFile, capture, err := openTestArtifacts(artifactsDir, stdout, stderr) + if err != nil { + reportf(stderr, "%v\n", err) + return 1 + } + + expected, discoveryErr := discoverProviderTests(logFile, stdout, stderr) + result := testRunResult{discoveryErr: discoveryErr} + if discoveryErr == nil { + result = runGoTests(capture) + } + capture.finish() + result.exitCode = executionExitCode(result) + result.captureErr = capture.err() + exitWriteErr, logCloseErr := finishTestArtifacts(artifactsDir, logFile, result.exitCode) + result.exitWriteErr = exitWriteErr + result.logCloseErr = logCloseErr + reportExecutionErrors(stderr, result) + if result.exitCode != 0 { + return result.exitCode + } + return validateResult(result, expected, capture.events(), stderr) +} + +type testRunResult struct { + discoveryErr error + startErr error + waitErr error + exitCode int + exitWriteErr error + logCloseErr error + captureErr error +} + +func openTestArtifacts( + artifactsDir string, + stdout, stderr io.Writer, +) (*os.File, *outputCapture, error) { + if err := os.MkdirAll(artifactsDir, 0o755); err != nil { + return nil, nil, fmt.Errorf("create artifact directory: %w", err) + } + logPath := filepath.Join(artifactsDir, defaultArtifactName+".jsonl") + logFile, err := os.OpenFile(logPath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644) + if err != nil { + return nil, nil, fmt.Errorf("create Go test output artifact: %w", err) + } + return logFile, newOutputCapture(logFile, stdout, stderr), nil +} + +func runGoTests(capture *outputCapture) testRunResult { + command := providerTestCommand("-run") + command.Stdout = capture.stdoutWriter() + command.Stderr = capture.stderrWriter() + startErr := command.Start() + if startErr != nil { + return testRunResult{startErr: startErr} + } + return testRunResult{waitErr: command.Wait()} +} + +func finishTestArtifacts(artifactsDir string, logFile *os.File, goExitCode int) (error, error) { + exitPath := filepath.Join(artifactsDir, defaultArtifactName+".exitcode") + exitWriteErr := os.WriteFile(exitPath, []byte(strconv.Itoa(goExitCode)), 0o644) + logCloseErr := logFile.Close() + return exitWriteErr, logCloseErr +} + +func reportExecutionErrors(stderr io.Writer, result testRunResult) { + if result.discoveryErr != nil { + reportf(stderr, "::error::%v\n", result.discoveryErr) + } + reportStartError(stderr, result.startErr) + reportWaitError(stderr, result) + reportArtifactErrors(stderr, result) + reportCaptureError(stderr, result.captureErr) +} + +func reportStartError(stderr io.Writer, err error) { + if err != nil { + reportf(stderr, "start Go provider update tests: %v\n", err) + } +} + +func reportWaitError(stderr io.Writer, result testRunResult) { + if result.exitCode != 0 && result.startErr == nil && result.discoveryErr == nil { + reportf( + stderr, + "::error::Provider update tests failed with exit code %d\n", + result.exitCode, + ) + } + if result.waitErr == nil || result.exitCode != 1 { + return + } + if _, ok := errors.AsType[*exec.ExitError](result.waitErr); !ok { + reportf(stderr, "wait for Go provider update tests: %v\n", result.waitErr) + } +} + +func reportArtifactErrors(stderr io.Writer, result testRunResult) { + if result.exitWriteErr != nil { + reportf(stderr, "write Go test exit-code artifact: %v\n", result.exitWriteErr) + } + if result.logCloseErr != nil { + reportf(stderr, "close Go test output artifact: %v\n", result.logCloseErr) + } +} + +func reportCaptureError(stderr io.Writer, err error) { + if err != nil { + reportf(stderr, "capture Go test output: %v\n", err) + } +} + +func reportf(stderr io.Writer, format string, args ...any) { + _, _ = fmt.Fprintf(stderr, format, args...) +} + +func validateResult( + result testRunResult, + expected map[testID]struct{}, + events map[testID]testState, + stderr io.Writer, +) int { + issues := validateTestCoverage(expected, events) + for _, issue := range issues { + reportf(stderr, "::error::%s\n", issue) + } + if len(issues) != 0 || + result.exitWriteErr != nil || + result.logCloseErr != nil || + result.captureErr != nil { + return 1 + } + return 0 +} + +func processExitCode(err error) int { + if err == nil { + return 0 + } + if exitErr, ok := errors.AsType[*exec.ExitError](err); ok { + if code := exitErr.ExitCode(); code >= 0 { + return code + } + } + return 1 +} + +func executionExitCode(result testRunResult) int { + if result.discoveryErr != nil { + return processExitCode(result.discoveryErr) + } + if result.startErr != nil { + return 1 + } + return processExitCode(result.waitErr) +} diff --git a/hack/provider_update_ci/main_test.go b/hack/provider_update_ci/main_test.go new file mode 100644 index 0000000000..3ac2beb376 --- /dev/null +++ b/hack/provider_update_ci/main_test.go @@ -0,0 +1,387 @@ +package main + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io" + "os" + "os/exec" + "path/filepath" + "runtime" + "strings" + "testing" +) + +const ( + fakeModeEnv = "DEVSY_PROVIDER_UPDATE_CI_FAKE_MODE" + cliModeEnv = "DEVSY_PROVIDER_UPDATE_CI_RUN_CLI" + fakeExitEnv = "DEVSY_PROVIDER_UPDATE_CI_FAKE_EXIT" + largeLineEnv = "DEVSY_PROVIDER_UPDATE_CI_LARGE_LINE" + fakeGoBasename = "go" + missingMode = "missing" + skipMode = "skip" + noPassMode = "no-pass" + multipleMode = "multiple" +) + +var fixtureTests = []testID{ + {packagePath: providerUpdatePackages[0], name: "TestProviderUpdateFirst"}, + {packagePath: providerUpdatePackages[0], name: "TestProviderUpdateSecond"}, + {packagePath: providerUpdatePackages[1], name: "TestProviderUpdateFirst"}, + {packagePath: providerUpdatePackages[1], name: "TestProviderUpdateSecond"}, +} + +var fakeCaseExceptions = map[string]map[int][]string{ + missingMode: {0: nil, 1: nil}, + skipMode: {0: {"skip"}}, + noPassMode: {0: {"run"}}, + multipleMode: { + 0: nil, + 1: {"skip"}, + 2: {"run"}, + 3: {"fail"}, + }, +} + +func TestMain(m *testing.M) { + if isFakeGoInvocation(os.Args[0]) { + os.Exit(runFakeGo()) + } + if os.Getenv(cliModeEnv) == "1" { + os.Exit(runCLI(os.Args[1:])) + } + os.Exit(m.Run()) +} + +func runFakeGo() int { + if !isExpectedGoCommand(os.Args[1:]) { + reportf(os.Stderr, "unexpected go arguments: %s\n", strings.Join(os.Args[1:], " ")) + return 91 + } + if os.Args[len(os.Args)-2] == "-list" { + return emitFakeDiscovery(os.Getenv(fakeModeEnv)) + } + emitFakeTestEvents(os.Getenv(fakeModeEnv)) + reportf(os.Stderr, "fake Go stderr\n") + return fakeGoExitCode() +} + +func isFakeGoInvocation(path string) bool { + base := filepath.Base(path) + return base == fakeGoBasename || base == fakeGoBasename+".exe" +} + +func isExpectedGoCommand(args []string) bool { + if len(args) != 10 || (args[8] != "-run" && args[8] != "-list") { + return false + } + expected := []string{ + "test", "-race", "-json", "-count=1", "-timeout", "10m", + providerUpdatePackages[0], providerUpdatePackages[1], args[8], providerUpdateRunPattern, + } + return equalStrings(args, expected) +} + +func emitFakeDiscovery(mode string) int { + if mode == "discovery-failure" { + return 9 + } + for _, id := range fixtureTests { + if mode == "empty-discovery" || + (mode == "missing-package" && id.packagePath == providerUpdatePackages[1]) { + continue + } + writeFakeEvent("output", testID{packagePath: id.packagePath}, id.name+"\n") + } + return 0 +} + +func equalStrings(left, right []string) bool { + if len(left) != len(right) { + return false + } + for index := range left { + if left[index] != right[index] { + return false + } + } + return true +} + +func emitFakeTestEvents(mode string) { + for index, name := range fixtureTests { + if actions, except := fakeCaseActions(mode, index); except { + for _, action := range actions { + writeFakeEvent(action, name, "") + } + continue + } + writeFakeEvent("run", name, "") + writeFakeEvent("pass", name, "") + } + emitLargeFakeLine() +} + +func fakeCaseActions(mode string, index int) ([]string, bool) { + actions, ok := fakeCaseExceptions[mode][index] + return actions, ok +} + +func emitLargeFakeLine() { + if os.Getenv(largeLineEnv) != "1" { + return + } + writeFakeEvent( + "output", fixtureTests[0], strings.Repeat("x", 70*1024), + ) +} + +func fakeGoExitCode() int { + code := os.Getenv(fakeExitEnv) + if code == "" { + return 0 + } + var exitCode int + if _, err := fmt.Sscan(code, &exitCode); err != nil { + return 92 + } + return exitCode +} + +func writeFakeEvent(action string, id testID, output string) { + event := testEvent{Action: action, Package: id.packagePath, Test: id.name, Output: output} + if err := json.NewEncoder(os.Stdout).Encode(event); err != nil { + reportf(os.Stderr, "encode fake event: %v\n", err) + } +} + +func TestCLIStreamsAndRetainsArtifacts(t *testing.T) { + dir := t.TempDir() + setUpFakeGo(t, "success", "0") + t.Setenv(largeLineEnv, "1") + + stdout, stderr, code := runCLIProcess(t, "--artifacts-dir", dir) + if code != 0 || stdout == "" { + t.Fatalf("expected streamed output; stdout=%q stderr=%q", stdout, stderr) + } + if !strings.Contains(stderr, "fake Go stderr") { + t.Fatalf("stderr was not streamed: %q", stderr) + } + log, err := os.ReadFile(filepath.Join(dir, defaultArtifactName+".jsonl")) + if err != nil { + t.Fatal(err) + } + if !bytes.Contains(log, []byte(strings.Repeat("x", 70*1024))) { + t.Fatal("large JSON line was not retained in the artifact") + } + exitCode, err := os.ReadFile(filepath.Join(dir, defaultArtifactName+".exitcode")) + if err != nil || string(exitCode) != "0" { + t.Fatalf("exit-code artifact = %q, err=%v; want 0", exitCode, err) + } +} + +func TestCLIGateReportsAllFailures(t *testing.T) { + for _, mode := range []string{missingMode, skipMode, noPassMode, multipleMode} { + t.Run(mode, func(t *testing.T) { + dir := t.TempDir() + setUpFakeGo(t, mode, "0") + _, stderr, code := runCLIProcess(t, "--artifacts-dir", dir) + if code == 0 { + t.Fatal("CLI succeeded despite missing or invalid critical tests") + } + assertGateMode(t, mode, stderr) + }) + } +} + +func assertGateMode(t *testing.T, mode, stderr string) { + t.Helper() + switch mode { + case multipleMode: + assertMultipleGateIssues(t, stderr) + case missingMode: + assertMissingGateIssues(t, stderr) + default: + if !strings.Contains(stderr, "::error::") { + t.Fatalf("no validation issue reported: %s", stderr) + } + } +} + +func assertMultipleGateIssues(t *testing.T, stderr string) { + t.Helper() + for _, issue := range fixtureTests[:4] { + if !strings.Contains(stderr, issue.name) { + t.Errorf("missing issue for %q in %s", issue, stderr) + } + } + if strings.Count(stderr, "::error::") < 4 { + t.Errorf("reported %d errors, want 4: %s", strings.Count(stderr, "::error::"), stderr) + } +} + +func assertMissingGateIssues(t *testing.T, stderr string) { + t.Helper() + for _, name := range fixtureTests[:2] { + if !strings.Contains(stderr, "produced no JSON events: "+name.name) { + t.Errorf("missing report for %q: %s", name, stderr) + } + } +} + +func TestCLIPreservesNativeGoExitCodeAndArtifacts(t *testing.T) { + dir := t.TempDir() + setUpFakeGo(t, "success", "7") + stdout, stderr, code := runCLIProcess(t, "--artifacts-dir", dir) + if !strings.Contains(stdout, "\"Action\":\"pass\"") || + !strings.Contains(stderr, "fake Go stderr") { + t.Fatalf("child output was not streamed: stdout=%q stderr=%q", stdout, stderr) + } + exitArtifact, err := os.ReadFile(filepath.Join(dir, defaultArtifactName+".exitcode")) + if err != nil || string(exitArtifact) != "7" { + t.Fatalf("exit-code artifact = %q, err=%v; want 7", exitArtifact, err) + } + jsonArtifact, err := os.ReadFile(filepath.Join(dir, defaultArtifactName+".jsonl")) + if err != nil || !bytes.Contains(jsonArtifact, []byte(`"Action":"pass"`)) { + t.Fatalf("JSON artifact did not retain child output: err=%v", err) + } + if code != 7 { + t.Fatalf("CLI exit code = %d, want native Go code 7", code) + } +} + +func TestCaptureFailureIsReportedAndDoesNotStopDrain(t *testing.T) { + dir := t.TempDir() + setUpFakeGo(t, "success", "0") + var stderr bytes.Buffer + code := runProviderUpdateTests(dir, failingWriter{}, &stderr) + if code == 0 { + t.Fatal("capture failure should fail the helper") + } + if !strings.Contains(stderr.String(), "capture provider update test discovery") { + t.Fatalf("capture failure was not reported: %q", stderr.String()) + } + if strings.Count(stderr.String(), "capture provider update test discovery:") != 1 { + t.Fatalf("capture errors were not bounded to the first failure: %q", stderr.String()) + } + if _, err := os.Stat(filepath.Join(dir, defaultArtifactName+".exitcode")); err != nil { + t.Fatalf("child was not drained and exit artifact written: %v", err) + } +} + +func TestNativeGoExitCodeWinsOverExitArtifactFailure(t *testing.T) { + dir := t.TempDir() + setUpFakeGo(t, "success", "7") + if err := os.Mkdir(filepath.Join(dir, defaultArtifactName+".exitcode"), 0o755); err != nil { + t.Fatal(err) + } + _, stderr, code := runCLIProcess(t, "--artifacts-dir", dir) + if !strings.Contains(stderr, "write Go test exit-code artifact") { + t.Fatalf("artifact write failure was not reported: %q", stderr) + } + if code != 7 { + t.Fatalf("CLI exit code = %d, want native Go code 7", code) + } +} + +func TestGoStartFailureWritesFailureExitArtifact(t *testing.T) { + dir := t.TempDir() + t.Setenv("PATH", t.TempDir()) + _, stderr, code := runCLIProcess(t, "--artifacts-dir", dir) + if code != 1 || !strings.Contains(stderr, "discover provider update tests") { + t.Fatalf("unexpected start failure result: code=%d stderr=%q", code, stderr) + } + exitArtifact, err := os.ReadFile(filepath.Join(dir, defaultArtifactName+".exitcode")) + if err != nil || string(exitArtifact) != "1" { + t.Fatalf("exit-code artifact = %q, err=%v; want 1", exitArtifact, err) + } +} + +func TestDefaultArtifactsDirectoryUsesRunnerTemp(t *testing.T) { + dir := t.TempDir() + setUpFakeGo(t, "success", "0") + t.Setenv("RUNNER_TEMP", dir) + stdout, stderr, code := runCLIProcess(t) + if code != 0 || stdout == "" || strings.Contains(stderr, "required") { + t.Fatalf("CLI did not use RUNNER_TEMP: stdout=%q stderr=%q", stdout, stderr) + } + if _, err := os.Stat(filepath.Join(dir, defaultArtifactName+".jsonl")); err != nil { + t.Fatalf("JSON artifact missing under RUNNER_TEMP: %v", err) + } +} + +func setUpFakeGo(t *testing.T, mode, exitCode string) { + t.Helper() + dir := t.TempDir() + name := fakeGoBasename + if runtime.GOOS == "windows" { + name += ".exe" + } + binary, err := os.Executable() + if err != nil { + t.Fatal(err) + } + if err := copyFile(binary, filepath.Join(dir, name)); err != nil { + t.Fatal(err) + } + if runtime.GOOS != "windows" { + if err := os.Chmod(filepath.Join(dir, name), 0o755); err != nil { + t.Fatal(err) + } + } + t.Setenv("PATH", dir+string(os.PathListSeparator)+os.Getenv("PATH")) + t.Setenv(fakeModeEnv, mode) + t.Setenv(fakeExitEnv, exitCode) +} + +func runCLIProcess(t *testing.T, args ...string) (string, string, int) { + t.Helper() + binary, err := os.Executable() + if err != nil { + t.Fatal(err) + } + command := exec.Command(binary, args...) + command.Env = append(os.Environ(), cliModeEnv+"=1") + var capturedOut, capturedErr bytes.Buffer + command.Stdout = &capturedOut + command.Stderr = &capturedErr + err = command.Run() + code := 0 + if err != nil { + exitErr, ok := errors.AsType[*exec.ExitError](err) + if !ok { + t.Fatalf("run helper CLI: %v", err) + } + code = exitErr.ExitCode() + } + return capturedOut.String(), capturedErr.String(), code +} + +func copyFile(source, destination string) error { + input, err := os.Open(source) + if err != nil { + return err + } + output, err := os.OpenFile(destination, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o700) + if err != nil { + return err + } + _, copyErr := io.Copy(output, input) + inputCloseErr := input.Close() + closeErr := output.Close() + if copyErr != nil { + return copyErr + } + if inputCloseErr != nil { + return inputCloseErr + } + return closeErr +} + +type failingWriter struct{} + +func (failingWriter) Write([]byte) (int, error) { + return 0, errors.New("intentional writer failure") +} diff --git a/mise.lock b/mise.lock index 510893da2d..1540b57535 100644 --- a/mise.lock +++ b/mise.lock @@ -20,17 +20,17 @@ url_api = "https://api.github.com/repos/cli/cli/releases/assets/542974295" provenance = "github-attestations" [[tools.go]] -version = "1.26.8" +version = "1.26.9" backend = "core:go" -specifiers = ["1.26.8"] +specifiers = ["1.26.9"] [tools.go."platforms.linux-x64"] -checksum = "sha256:d0f743b33e8d8945e6b1f432edd15785c70507121d6e2a723b21285eddf8b57b" -url = "https://dl.google.com/go/go1.26.8.linux-amd64.tar.gz" +checksum = "sha256:42d158b4d8f7b61ac0a830567c940a86098fb7aac52e467a5ebec03ef5cc2f8d" +url = "https://dl.google.com/go/go1.26.9.linux-amd64.tar.gz" [tools.go."platforms.macos-arm64"] -checksum = "sha256:a012b25b571bd0138a03dcd25375ceba866fe5ca822f426d2c66a4de56fd3f4b" -url = "https://dl.google.com/go/go1.26.8.darwin-arm64.tar.gz" +checksum = "sha256:f9bb7c0a02506c5d9bf0d1eb1f7ee6c7684f844ae49558308a0427b830e022cc" +url = "https://dl.google.com/go/go1.26.9.darwin-arm64.tar.gz" [[tools.golangci-lint]] version = "2.13.2" diff --git a/mise.toml b/mise.toml index b730560849..4a4a33351d 100644 --- a/mise.toml +++ b/mise.toml @@ -7,7 +7,7 @@ lockfile_platforms = ["linux-x64", "macos-arm64"] locked = true [tools] -go = "1.26.8" +go = "1.26.9" node = "24" task = "3.53.1" golangci-lint = "2.13.2" diff --git a/pkg/provider/download.go b/pkg/provider/download.go index f224999aea..9e45774111 100644 --- a/pkg/provider/download.go +++ b/pkg/provider/download.go @@ -157,10 +157,18 @@ func downloadWithRetry( binaryName string, binary *ProviderBinary, targetFolder string, +) (string, error) { + return downloadWithRetryUsing(binaryName, binary, func() (string, error) { + return downloadBinary(ctx, binaryName, binary, targetFolder) + }) +} + +func downloadWithRetryUsing( + binaryName string, binary *ProviderBinary, fetch func() (string, error), ) (string, error) { var binaryPath string err := retry.OnError(downloadBackoff, isRetriableError, func() error { - path, err := downloadBinary(ctx, binaryName, binary, targetFolder) + path, err := fetch() if err != nil { return err } diff --git a/pkg/provider/update_archive.go b/pkg/provider/update_archive.go new file mode 100644 index 0000000000..a55ff091d7 --- /dev/null +++ b/pkg/provider/update_archive.go @@ -0,0 +1,462 @@ +package provider + +import ( + "archive/tar" + "archive/zip" + "bufio" + "compress/gzip" + "context" + "errors" + "fmt" + "io" + "io/fs" + "os" + "path/filepath" + "runtime" + "strings" + + "github.com/devsy-org/devsy/pkg/download" + "github.com/devsy-org/devsy/pkg/gitcredentials" + "github.com/devsy-org/devsy/pkg/hash" + "github.com/google/uuid" +) + +// DownloadUpdateBinaries prepares an already isolated provider update tree. +// Unlike the ordinary downloader it confines archive materialization with os.Root. +// Caller validates/seeds the tree and separately handles absolute local references. +func DownloadUpdateBinaries( + ctx context.Context, + binaries map[string][]*ProviderBinary, + target string, +) (map[string]string, error) { + if err := validateUpdateBinaryKeys(binaries); err != nil { + return nil, err + } + result := map[string]string{} + for key, locations := range binaries { + binary := updateBinaryForPlatform(locations) + if binary == nil { + return nil, fmt.Errorf( + "no update binary %s matches %s/%s", + key, + runtime.GOOS, + runtime.GOARCH, + ) + } + path, err := prepareUpdateBinary(ctx, key, binary, target) + if err != nil { + return nil, err + } + result[key] = path + } + // A reused alias can observe another requested family's later replacement. + // Recheck the complete prepared result before any live publication. + for key, locations := range binaries { + binary := updateBinaryForPlatform(locations) + if !updateBinaryMatches(result[key], binary.Checksum) { + return nil, fmt.Errorf( + "prepared update binary %s no longer matches its requested checksum", + key, + ) + } + } + return result, nil +} + +func validateUpdateBinaryKeys(binaries map[string][]*ProviderBinary) error { + for key := range binaries { + if key == "" || optionNameRegEx.MatchString(key) { + return fmt.Errorf("unsafe provider update binary key %q", key) + } + } + return nil +} + +func updateBinaryForPlatform(locations []*ProviderBinary) *ProviderBinary { + for _, binary := range locations { + if binary.OS == runtime.GOOS && binary.Arch == runtime.GOARCH { + return binary + } + } + return nil +} + +func prepareUpdateBinary( + ctx context.Context, + key string, + binary *ProviderBinary, + target string, +) (path string, err error) { + if filepath.IsAbs(binary.Path) { + return "", fmt.Errorf( + "update preparation must validate absolute reference separately: %s", + binary.Path, + ) + } + family := filepath.Join(target, strings.ToLower(key)) + path = getBinaryPath(binary, family) + // Installed aliases are reusable, but checksum misses must not delete their targets. + if updateBinaryMatches(path, binary.Checksum) { + return path, nil + } + archive := isRemotePath(binary.Path) && binary.ArchivePath != "" + if archive && !cachedUpdateBinaryAvailable(binary) { + return prepareFreshUpdateArchive(ctx, key, binary, target) + } + return prepareSelectedUpdate(ctx, key, binary, target) +} + +func prepareSelectedUpdate( + ctx context.Context, + key string, + binary *ProviderBinary, + target string, +) (result string, err error) { + family := filepath.Join(target, strings.ToLower(key)) + path := getBinaryPath(binary, family) + archive := isRemotePath(binary.Path) && binary.ArchivePath != "" + restoreDestination, err := prepareUpdateDestination(ctx, target, path) + if err != nil { + return "", fmt.Errorf("isolate update destination for %s: %w", key, err) + } + defer func() { + if restoreDestination != nil { + err = errors.Join(err, restoreDestination()) + } + }() + if !archive { + return downloadBinaryForPlatform(ctx, key, []*ProviderBinary{binary}, target) + } + if fromCache(binary, family) { + return getBinaryPath(binary, family), nil + } + // A failed cache copy falls back to replacing the archive family. Restore + // selected-entry parents first so their modes are not applied to fresh archive directories. + if restoreErr := restoreDestination(); restoreErr != nil { + restoreDestination = nil + return "", fmt.Errorf("restore staged update destination permissions: %w", restoreErr) + } + restoreDestination = nil + return prepareFreshUpdateArchive(ctx, key, binary, target) +} + +func prepareFreshUpdateArchive( + ctx context.Context, + key string, + binary *ProviderBinary, + target string, +) (path string, err error) { + family := filepath.Join(target, strings.ToLower(key)) + // An uncached archive replaces the whole family; only its staged parent needs + // temporary write access because the family reset handles descendant modes. + restoreFamily, err := prepareUpdateDestinationParent(target, family) + if err != nil { + return "", fmt.Errorf("prepare update family parent for %s: %w", key, err) + } + defer func() { err = errors.Join(err, restoreFamily()) }() + return downloadWithRetryUsing(key, binary, func() (string, error) { + return downloadConfinedUpdateArchive(ctx, binary, family) + }) +} + +func cachedUpdateBinaryAvailable(binary *ProviderBinary) bool { + path, err := getCachedBinaryPath(binary.Path) + return err == nil && updateBinaryMatches(path, binary.Checksum) +} + +// Match without the legacy verifier's destructive checksum-mismatch cleanup. +func updateBinaryMatches(path, checksum string) bool { + if _, err := os.Stat(path); err != nil { + return false + } + if checksum == "" { + return true + } + actual, err := hash.File(path) + return err == nil && strings.EqualFold(actual, checksum) +} + +func downloadConfinedUpdateArchive( + ctx context.Context, + binary *ProviderBinary, + family string, +) (_ string, err error) { + if err := validUpdateArchiveName(binary.ArchivePath); err != nil { + return "", err + } + if err := removeUpdateArchiveFamily(family); err != nil { + return "", err + } + if err := os.MkdirAll(family, dirPerms); err != nil { + return "", err + } + root, err := os.OpenRoot(family) + if err != nil { + return "", err + } + defer func() { err = errors.Join(err, root.Close()) }() + if err = fetchUpdateArchive(ctx, root, binary.Path); err != nil { + return "", err + } + if err = finalizeUpdateArchive(ctx, root, binary.ArchivePath); err != nil { + return "", err + } + return getBinaryPath(binary, family), nil +} + +// Each download attempt replaces only its owned staged family; cache hits bypass this path. +func removeUpdateArchiveFamily(family string) error { + info, err := os.Lstat(family) + if errors.Is(err, fs.ErrNotExist) { + return nil + } + if err != nil { + return err + } + if info.IsDir() { + if err := makeUpdateArchiveFamilyWritable(family); err != nil { + return err + } + } + return os.RemoveAll(family) +} + +func makeUpdateArchiveFamilyWritable(family string) error { + root, err := os.OpenRoot(family) + if err != nil { + return err + } + walkErr := fs.WalkDir( + root.FS(), + ".", + func(name string, entry fs.DirEntry, walkErr error) error { + if walkErr != nil { + return walkErr + } + if entry.Type()&os.ModeSymlink != 0 { + return nil + } + return root.Chmod(name, 0o700) + }, + ) + return errors.Join(walkErr, root.Close()) +} + +func finalizeUpdateArchive(ctx context.Context, root *os.Root, selected string) error { + if err := validateUpdateArchiveLinks(ctx, root); err != nil { + return err + } + file, err := root.Open(selected) + if err != nil { + return err + } + return errors.Join(file.Chmod(filePerms), file.Close()) +} + +func fetchUpdateArchive(ctx context.Context, root *os.Root, url string) (err error) { + body, err := download.File(ctx, url, download.WithCredentialResolver(gitcredentials.Resolver{})) + if err != nil { + return fmt.Errorf("download update archive: %w", err) + } + defer func() { err = errors.Join(err, body.Close()) }() + switch { + case strings.HasSuffix(url, zipSuffix): + return extractConfinedUpdateZip(ctx, root, body) + case isGzipOrTar(url): + return extractConfinedUpdateTar(ctx, root, body) + default: + return fmt.Errorf("unrecognized update archive format %s", url) + } +} + +func validUpdateArchiveName(name string) error { + clean := filepath.Clean(name) + if name == "" || filepath.IsAbs(name) || clean == ".." || + strings.HasPrefix(clean, ".."+string(filepath.Separator)) { + return fmt.Errorf("unsafe update archive member %q", name) + } + return nil +} + +func updateArchiveParents(root *os.Root, name string) error { + if err := validUpdateArchiveName(name); err != nil { + return err + } + return root.MkdirAll(filepath.Dir(name), 0o755) +} + +func openUpdateTarSource(input io.Reader) (io.ReadCloser, error) { + buffered := bufio.NewReader(input) + signature, err := buffered.Peek(2) + if err != nil { + return nil, err + } + if signature[0] == 31 && signature[1] == 139 { + return gzip.NewReader(buffered) + } + return io.NopCloser(buffered), nil +} + +func extractConfinedUpdateTar(ctx context.Context, root *os.Root, input io.Reader) (err error) { + source, err := openUpdateTarSource(input) + if err != nil { + return err + } + defer func() { err = errors.Join(err, source.Close()) }() + archive := tar.NewReader(source) + for { + if err := ctx.Err(); err != nil { + return err + } + header, err := archive.Next() + if errors.Is(err, io.EOF) { + return nil + } + if err != nil { + return err + } + if err = materializeUpdateTarEntry(root, archive, header); err != nil { + return fmt.Errorf("confined update archive member %q: %w", header.Name, err) + } + } +} + +func materializeUpdateTarEntry(root *os.Root, input io.Reader, header *tar.Header) error { + if header.Typeflag == tar.TypeXGlobalHeader { + return nil + } + name := filepath.Clean(header.Name) + if err := updateArchiveParents(root, header.Name); err != nil { + return err + } + switch header.Typeflag { + case tar.TypeDir: + return root.MkdirAll(name, 0o755) + case tar.TypeSymlink: + return materializeUpdateTarSymlink(root, name, header.Linkname) + case tar.TypeLink: + return materializeUpdateTarLink(root, name, header.Linkname) + case tar.TypeReg, tar.TypeCont, tar.TypeGNUSparse: + // archive/tar.Reader expands GNU sparse holes into the logical file stream. + return writeConfinedUpdateFile(root, name, input, header.FileInfo().Mode().Perm()|0o600) + default: + return fmt.Errorf("unsupported update archive entry type %d", header.Typeflag) + } +} + +func materializeUpdateTarSymlink(root *os.Root, name, target string) error { + if filepath.IsAbs(target) { + return fmt.Errorf("absolute archive symlink is unsupported: %s", target) + } + // Archive symlink targets use slash separators; os.Root.Symlink does not + // normalize them on Windows as os.Symlink does. + return root.Symlink(filepath.FromSlash(target), name) +} + +func materializeUpdateTarLink(root *os.Root, name, target string) error { + if err := validUpdateArchiveName(target); err != nil { + return err + } + return root.Link(filepath.Clean(target), name) +} + +func writeConfinedUpdateFile(root *os.Root, name string, input io.Reader, mode fs.FileMode) error { + file, err := root.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, mode.Perm()) + if err != nil { + return err + } + _, copyErr := io.Copy(file, input) + return errors.Join(copyErr, file.Chmod(mode.Perm()), file.Close()) +} + +func extractConfinedUpdateZip(ctx context.Context, root *os.Root, input io.Reader) (err error) { + spoolName := ".update-zip-" + uuid.NewString() + spool, err := root.OpenFile(spoolName, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o600) + if err != nil { + return err + } + defer func() { err = errors.Join(err, spool.Close(), root.Remove(spoolName)) }() + size, err := io.Copy(spool, input) + if err != nil { + return err + } + archive, err := zip.NewReader(spool, size) + if err != nil { + return err + } + for _, entry := range archive.File { + if err = ctx.Err(); err != nil { + return err + } + if filepath.Clean(entry.Name) == spoolName { + return errors.New("archive member conflicts with owned spool") + } + if err = materializeUpdateZipEntry(root, entry); err != nil { + return fmt.Errorf("confined update zip member %q: %w", entry.Name, err) + } + } + return nil +} + +func materializeUpdateZipEntry(root *os.Root, entry *zip.File) (err error) { + if err := updateArchiveParents(root, entry.Name); err != nil { + return err + } + name := filepath.Clean(entry.Name) + if entry.FileInfo().IsDir() { + return root.MkdirAll(name, 0o755) + } + body, err := entry.Open() + if err != nil { + return err + } + defer func() { err = errors.Join(err, body.Close()) }() + if entry.Mode()&os.ModeSymlink != 0 { + return materializeUpdateZipLink(root, name, body) + } + if !entry.Mode().IsRegular() { + return fmt.Errorf("unsupported update zip file type %s", entry.Mode()) + } + const limit = 2 << 30 + limited := &io.LimitedReader{R: body, N: limit + 1} + err = writeConfinedUpdateFile(root, name, limited, entry.Mode()) + if limited.N == 0 { + err = errors.Join(err, fmt.Errorf("update zip entry exceeds %d bytes", limit)) + } + return err +} + +func materializeUpdateZipLink(root *os.Root, name string, body io.Reader) error { + target, err := io.ReadAll(io.LimitReader(body, 4097)) + if err != nil { + return err + } + if len(target) > 4096 { + return errors.New("archive symlink target exceeds 4096 bytes") + } + if filepath.IsAbs(string(target)) { + return errors.New("absolute archive symlink is unsupported") + } + return root.Symlink(filepath.FromSlash(string(target)), name) +} + +func validateUpdateArchiveLinks(ctx context.Context, root *os.Root) error { + return fs.WalkDir(root.FS(), ".", func(name string, entry fs.DirEntry, walkErr error) error { + if walkErr != nil { + return walkErr + } + if err := ctx.Err(); err != nil { + return err + } + if entry.Type()&os.ModeSymlink != 0 { + info, err := StatUpdatePath(root, name) + if err != nil { + return fmt.Errorf("unsafe update archive symlink %s: %w", name, err) + } + if !info.IsDir() && !info.Mode().IsRegular() { + return fmt.Errorf("unsupported archive symlink target %s", name) + } + } + return nil + }) +} diff --git a/pkg/provider/update_archive_contiguous_test.go b/pkg/provider/update_archive_contiguous_test.go new file mode 100644 index 0000000000..a8c58881e1 --- /dev/null +++ b/pkg/provider/update_archive_contiguous_test.go @@ -0,0 +1,87 @@ +package provider + +import ( + "archive/tar" + "bytes" + "context" + "os" + "path/filepath" + "testing" + + "github.com/devsy-org/devsy/pkg/extract" + "github.com/stretchr/testify/require" +) + +func TestExtractConfinedUpdateTarPreservesContiguousFiles(t *testing.T) { + archive := contiguousUpdateArchive(t) + wantExecutable := []byte("#!/bin/sh\necho contiguous\n") + wantSidecar := []byte("provider metadata\n") + + legacyDir := t.TempDir() + require.NoError(t, extract.Extract(bytes.NewReader(archive), legacyDir)) + // #nosec G304 -- legacyDir and archive are owned entirely by this fixture. + legacyExecutable, err := os.ReadFile(filepath.Join(legacyDir, "bin", "runtime")) + require.NoError(t, err) + require.Equal(t, wantExecutable, legacyExecutable) + // #nosec G304 -- legacyDir and archive are owned entirely by this fixture. + legacySidecar, err := os.ReadFile(filepath.Join(legacyDir, "bin", "provider.json")) + require.NoError(t, err) + require.Equal(t, wantSidecar, legacySidecar) + + destination := filepath.Join(t.TempDir(), "provider") + require.NoError(t, os.Mkdir(destination, 0o700)) + root, err := os.OpenRoot(destination) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, root.Close()) }) + + require.NoError(t, extractConfinedUpdateTar( + context.Background(), root, bytes.NewReader(archive), + )) + require.NoError(t, finalizeUpdateArchive( + context.Background(), root, updateArchiveExecutablePath, + )) + + executable, err := root.ReadFile(updateArchiveExecutablePath) + require.NoError(t, err) + require.Equal(t, wantExecutable, executable) + executableInfo, err := root.Stat(updateArchiveExecutablePath) + require.NoError(t, err) + assertUpdateArchiveFileMode(t, executableInfo, 0o755) + + sidecar, err := root.ReadFile(updateArchiveSidecarPath) + require.NoError(t, err) + require.Equal(t, wantSidecar, sidecar) + sidecarInfo, err := root.Stat(updateArchiveSidecarPath) + require.NoError(t, err) + assertUpdateArchiveFileMode(t, sidecarInfo, 0o640) +} + +func contiguousUpdateArchive(t *testing.T) []byte { + t.Helper() + var buffer bytes.Buffer + writer := tar.NewWriter(&buffer) + executable := []byte("#!/bin/sh\necho contiguous\n") + require.NoError(t, writer.WriteHeader(&tar.Header{ + Name: updateArchiveExecutablePath, Typeflag: tar.TypeCont, Mode: 0o755, + Size: int64(len(executable)), Format: tar.FormatGNU, + })) + _, err := writer.Write(executable) + require.NoError(t, err) + sidecar := []byte("provider metadata\n") + require.NoError(t, writer.WriteHeader(&tar.Header{ + Name: updateArchiveSidecarPath, Typeflag: tar.TypeCont, Mode: 0o640, + Size: int64(len(sidecar)), Format: tar.FormatGNU, + })) + _, err = writer.Write(sidecar) + require.NoError(t, err) + require.NoError(t, writer.Close()) + + reader := tar.NewReader(bytes.NewReader(buffer.Bytes())) + for _, expected := range []string{updateArchiveExecutablePath, updateArchiveSidecarPath} { + header, err := reader.Next() + require.NoError(t, err) + require.Equal(t, expected, header.Name) + require.Equal(t, byte(tar.TypeCont), header.Typeflag) + } + return buffer.Bytes() +} diff --git a/pkg/provider/update_archive_sparse_test.go b/pkg/provider/update_archive_sparse_test.go new file mode 100644 index 0000000000..122af55765 --- /dev/null +++ b/pkg/provider/update_archive_sparse_test.go @@ -0,0 +1,136 @@ +package provider + +import ( + "archive/tar" + "bytes" + "context" + "fmt" + "io" + "os" + "path/filepath" + "runtime" + "testing" + + "github.com/devsy-org/devsy/pkg/extract" + "github.com/stretchr/testify/require" +) + +const ( + updateArchiveExecutablePath = "bin/runtime" + updateArchiveSidecarPath = "bin/provider.json" +) + +func TestExtractConfinedUpdateTarExpandsGNUSparseExecutable(t *testing.T) { + archive := gnuSparseUpdateArchive(t) + wantExecutable := []byte("#!/bin\x00\x00\x00sh\n") + wantSidecar := []byte("provider metadata\n") + + legacyDir := t.TempDir() + require.NoError(t, extract.Extract(bytes.NewReader(archive), legacyDir)) + // #nosec G304 -- legacyDir and the archive are owned entirely by this fixture. + legacyExecutable, err := os.ReadFile(filepath.Join(legacyDir, "bin", "runtime")) + require.NoError(t, err) + require.Equal(t, wantExecutable, legacyExecutable) + + destination := filepath.Join(t.TempDir(), "provider") + require.NoError(t, os.Mkdir(destination, 0o700)) + root, err := os.OpenRoot(destination) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, root.Close()) }) + + require.NoError(t, extractConfinedUpdateTar( + context.Background(), root, bytes.NewReader(archive), + )) + require.NoError(t, finalizeUpdateArchive( + context.Background(), root, updateArchiveExecutablePath, + )) + + executable, err := root.ReadFile(updateArchiveExecutablePath) + require.NoError(t, err) + require.Equal(t, wantExecutable, executable) + executableInfo, err := root.Stat(updateArchiveExecutablePath) + require.NoError(t, err) + assertUpdateArchiveFileMode(t, executableInfo, 0o755) + + sidecar, err := root.ReadFile(updateArchiveSidecarPath) + require.NoError(t, err) + require.Equal(t, wantSidecar, sidecar) + sidecarInfo, err := root.Stat(updateArchiveSidecarPath) + require.NoError(t, err) + assertUpdateArchiveFileMode(t, sidecarInfo, 0o640) +} + +func assertUpdateArchiveFileMode(t *testing.T, info os.FileInfo, unixMode os.FileMode) { + t.Helper() + require.True(t, info.Mode().IsRegular()) + if runtime.GOOS == windowsOS { + // Windows reports writable regular files as 0666 instead of Unix permission bits. + require.Equal(t, os.FileMode(0o666), info.Mode().Perm()) + return + } + require.Equal(t, unixMode, info.Mode().Perm()) +} + +// This fixture is a GNU sparse tar stream parsed by archive/tar.Reader. The +// regular-file header is patched to the GNU sparse type with two data extents. +func gnuSparseUpdateArchive(t *testing.T) []byte { + t.Helper() + var buf bytes.Buffer + writer := tar.NewWriter(&buf) + physical := []byte("#!/binsh\n") + require.NoError(t, writer.WriteHeader(&tar.Header{ + Name: updateArchiveExecutablePath, + Mode: 0o755, + Size: int64(len(physical)), + Format: tar.FormatGNU, + })) + _, err := writer.Write(physical) + require.NoError(t, err) + sidecar := []byte("provider metadata\n") + require.NoError(t, writer.WriteHeader(&tar.Header{ + Name: updateArchiveSidecarPath, Mode: 0o640, Size: int64(len(sidecar)), + })) + _, err = writer.Write(sidecar) + require.NoError(t, err) + require.NoError(t, writer.Close()) + + archive := buf.Bytes() + header := archive[:512] + header[156] = tar.TypeGNUSparse + putSparseNumber(t, header[386:398], 0) + putSparseNumber(t, header[398:410], 6) + putSparseNumber(t, header[410:422], 9) + putSparseNumber(t, header[422:434], 3) + putSparseNumber(t, header[483:495], 12) + for i := 148; i < 156; i++ { + header[i] = ' ' + } + var checksum int + for _, b := range header { + checksum += int(b) + } + copy(header[148:156], fmt.Sprintf("%06o\x00 ", checksum)) + return archive +} + +func putSparseNumber(t *testing.T, field []byte, value int64) { + t.Helper() + text := fmt.Sprintf("%011o\x00", value) + require.Len(t, text, len(field)) + copy(field, text) +} + +func TestGNUArchiveFixtureHasExpandedSparseData(t *testing.T) { + archive := gnuSparseUpdateArchive(t) + reader := tar.NewReader(bytes.NewReader(archive)) + header, err := reader.Next() + require.NoError(t, err) + require.Equal(t, byte(tar.TypeGNUSparse), header.Typeflag) + require.Equal(t, int64(12), header.Size) + body, err := io.ReadAll(reader) + require.NoError(t, err) + require.Equal(t, []byte("#!/bin\x00\x00\x00sh\n"), body) + header, err = reader.Next() + require.NoError(t, err) + require.Equal(t, updateArchiveSidecarPath, header.Name) +} diff --git a/pkg/provider/update_destination.go b/pkg/provider/update_destination.go new file mode 100644 index 0000000000..489ae6c18b --- /dev/null +++ b/pkg/provider/update_destination.go @@ -0,0 +1,413 @@ +package provider + +import ( + "context" + "errors" + "fmt" + "io" + "io/fs" + "os" + "path/filepath" + "slices" + "strings" +) + +// prepareUpdateDestination isolates destination aliases and retains temporary +// write access only to staged parents it must mutate. Installed read-only reuse +// bypasses this helper. Callers must restore the original modes before publishing. +func prepareUpdateDestination( + ctx context.Context, + stageRoot, selectedPath string, +) (restore func() error, err error) { + stageRoot, selected, err := absoluteUpdateDestination(stageRoot, selectedPath) + if err != nil { + return nil, err + } + root, err := os.OpenRoot(stageRoot) + if err != nil { + return nil, err + } + permissions := updateDestinationPermissions{root: root, changed: map[string]fs.FileMode{}} + if err := permissions.isolateDestination(ctx, selected); err != nil { + return nil, errors.Join(err, permissions.restore()) + } + return permissions.restore, nil +} + +// prepareUpdateDestinationParent makes an archive family's staged parent writable +// while the family is removed and recreated, then restores its original mode. +func prepareUpdateDestinationParent(stageRoot, selectedPath string) (func() error, error) { + stageRoot, selected, err := absoluteUpdateDestination(stageRoot, selectedPath) + if err != nil { + return nil, err + } + root, err := os.OpenRoot(stageRoot) + if err != nil { + return nil, err + } + permissions := updateDestinationPermissions{root: root, changed: map[string]fs.FileMode{}} + if err := permissions.allowWrite(filepath.Dir(selected)); err != nil { + return nil, errors.Join(err, permissions.restore()) + } + return permissions.restore, nil +} + +func absoluteUpdateDestination(stageRoot, selectedPath string) (string, string, error) { + stageRoot, err := filepath.Abs(stageRoot) + if err != nil { + return "", "", err + } + selectedPath, err = filepath.Abs(selectedPath) + if err != nil { + return "", "", err + } + selected, err := updateDestinationRelative(stageRoot, selectedPath) + if err != nil { + return "", "", err + } + if selected == "." { + return "", "", errors.New("provider update destination cannot be the staging root") + } + // Keep canonical target comparisons consistent on hosts whose temporary + // directory has a symlinked prefix, while retaining the lexical selection. + canonical, err := filepath.EvalSymlinks(stageRoot) + return canonical, selected, err +} + +func (p *updateDestinationPermissions) isolateDestination( + ctx context.Context, + selected string, +) error { + parts := strings.Split(selected, string(filepath.Separator)) + for index := range parts { + name := filepath.Join(parts[:index+1]...) + continued, err := p.isolateEntry(ctx, name, index == len(parts)-1) + if err != nil { + return err + } + if !continued { + return nil + } + } + return nil +} + +func (p *updateDestinationPermissions) isolateEntry( + ctx context.Context, name string, leaf bool, +) (bool, error) { + if err := ctx.Err(); err != nil { + return false, err + } + info, err := p.root.Lstat(name) + if errors.Is(err, fs.ErrNotExist) { + return false, p.allowExistingParentWrite(filepath.Dir(name)) + } + if err != nil { + return false, err + } + if leaf { + return false, p.isolateLeaf(name, info) + } + if err := p.isolateAncestor(ctx, name, info); err != nil { + return false, err + } + return true, nil +} + +func (p *updateDestinationPermissions) isolateLeaf(name string, info fs.FileInfo) error { + if !info.IsDir() { + if err := p.allowWrite(filepath.Dir(name)); err != nil { + return err + } + } + return removeUpdateDestinationLeaf(p.root, name, info) +} + +func (p *updateDestinationPermissions) isolateAncestor( + ctx context.Context, name string, info fs.FileInfo, +) error { + if info.Mode()&os.ModeSymlink != 0 { + if err := p.allowWrite(filepath.Dir(name)); err != nil { + return err + } + } + return isolateUpdateAncestor(ctx, p.root, name, info) +} + +type updateDestinationPermissions struct { + root *os.Root + changed map[string]fs.FileMode + order []string +} + +func (p *updateDestinationPermissions) allowWrite(name string) error { + if _, ok := p.changed[name]; ok { + return nil + } + info, err := p.root.Lstat(name) + if err != nil { + return err + } + if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 { + return fmt.Errorf("provider update destination parent is not a directory: %s", name) + } + mode := info.Mode() + if mode.Perm()&0o200 == 0 { + if err := p.root.Chmod(name, mode|0o200); err != nil { + return fmt.Errorf("make staged update parent writable %s: %w", name, err) + } + p.changed[name] = mode + p.order = append(p.order, name) + } + return nil +} + +func (p *updateDestinationPermissions) allowExistingParentWrite(name string) error { + if _, err := p.root.Lstat(name); errors.Is(err, fs.ErrNotExist) { + return nil + } else if err != nil { + return err + } + return p.allowWrite(name) +} + +func (p *updateDestinationPermissions) restore() error { + var err error + for _, name := range slices.Backward(p.order) { + err = errors.Join(err, p.root.Chmod(name, p.changed[name])) + } + return errors.Join(err, p.root.Close()) +} + +func removeUpdateDestinationLeaf(root *os.Root, name string, info fs.FileInfo) error { + if !info.IsDir() { + return root.Remove(name) + } + return nil +} + +func isolateUpdateAncestor( + ctx context.Context, + root *os.Root, + name string, + info fs.FileInfo, +) error { + if info.Mode()&os.ModeSymlink != 0 { + return detachUpdateDirectory(ctx, root, root.Name(), name) + } + if !info.IsDir() { + return fmt.Errorf("provider update destination ancestor is not a directory: %s", name) + } + return nil +} + +func updateDestinationRelative(root, path string) (string, error) { + name, err := filepath.Rel(root, path) + if err != nil { + return "", err + } + if !filepath.IsLocal(name) { + return "", fmt.Errorf("provider update destination escapes staging: %s", path) + } + return name, nil +} + +func resolveUpdateDestination(root *os.Root, stageRoot, name string) (string, error) { + // Root.Stat checks confinement before EvalSymlinks supplies the canonical + // target needed to preserve a copied link's meaning after relocation. + if _, err := StatUpdatePath(root, name); err != nil { + return "", err + } + resolved, err := filepath.EvalSymlinks(filepath.Join(stageRoot, name)) + if err != nil { + return "", err + } + return updateDestinationRelative(stageRoot, resolved) +} + +func detachUpdateDirectory(ctx context.Context, root *os.Root, stageRoot, name string) (err error) { + source, err := resolveUpdateDirectory(root, stageRoot, name) + if err != nil { + return err + } + parent := filepath.Join(stageRoot, filepath.Dir(name)) + temporary, err := os.MkdirTemp(parent, ".update-destination-") + if err != nil { + return err + } + defer func() { + if temporary != "" { + err = errors.Join(err, removeUpdateDestinationClone(temporary)) + } + }() + clone := updateDirectoryClone{ + root: root, stage: stageRoot, source: source, destination: name, temporary: temporary, + } + if err := clone.copy(ctx); err != nil { + return err + } + if err := ctx.Err(); err != nil { + return err + } + if err := root.Remove(name); err != nil { + return err + } + temporaryName := filepath.Join(filepath.Dir(name), filepath.Base(temporary)) + if err := root.Rename(temporaryName, name); err != nil { + return err + } + temporary = "" + return nil +} + +func resolveUpdateDirectory(root *os.Root, stageRoot, name string) (string, error) { + source, err := resolveUpdateDestination(root, stageRoot, name) + if err != nil { + return "", err + } + info, err := StatUpdatePath(root, source) + if err != nil { + return "", err + } + if !info.IsDir() { + return "", fmt.Errorf( + "provider update ancestor alias does not target a directory: %s", + name, + ) + } + return source, nil +} + +type updateDirectoryClone struct { + root *os.Root + stage, source, destination, temporary string +} + +type updateDirectoryMode struct { + name string + mode fs.FileMode +} + +func (c *updateDirectoryClone) copy(ctx context.Context) (err error) { + source, err := c.root.OpenRoot(c.source) + if err != nil { + return err + } + defer func() { err = errors.Join(err, source.Close()) }() + out, err := os.OpenRoot(c.temporary) + if err != nil { + return err + } + defer func() { err = errors.Join(err, out.Close()) }() + directories, err := c.copyContents(ctx, source, out) + if err != nil { + return err + } + for _, directory := range slices.Backward(directories) { + if err := out.Chmod(directory.name, directory.mode); err != nil { + return err + } + } + return nil +} + +func (c *updateDirectoryClone) copyContents( + ctx context.Context, + source, out *os.Root, +) ([]updateDirectoryMode, error) { + var directories []updateDirectoryMode + err := fs.WalkDir(source.FS(), ".", func(name string, entry fs.DirEntry, walkErr error) error { + if walkErr != nil { + return walkErr + } + if err := ctx.Err(); err != nil { + return err + } + // A link to an ancestor can place this temporary clone inside its source. + if filepath.Join(c.stage, c.source, name) == c.temporary { + return fs.SkipDir + } + info, err := entry.Info() + if err != nil { + return err + } + if info.IsDir() { + directories = append( + directories, + updateDirectoryMode{name: name, mode: info.Mode().Perm()}, + ) + return out.MkdirAll(name, 0o700) + } + return c.copyEntry(source, out, name, info) + }) + return directories, err +} + +func (c *updateDirectoryClone) copyEntry( + source, out *os.Root, + name string, + info fs.FileInfo, +) error { + if info.Mode()&os.ModeSymlink != 0 { + return c.copyLink(name) + } + if !info.Mode().IsRegular() { + return fmt.Errorf("unsupported provider update destination member: %s", name) + } + in, err := source.Open(name) + if err != nil { + return err + } + file, err := out.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600) + if err != nil { + return errors.Join(err, in.Close()) + } + _, copyErr := io.Copy(file, in) + return errors.Join(copyErr, file.Chmod(info.Mode().Perm()), file.Close(), in.Close()) +} + +func (c *updateDirectoryClone) copyLink(name string) error { + target, err := resolveUpdateDestination(c.root, c.stage, filepath.Join(c.source, name)) + if err != nil { + return err + } + parent := filepath.Join(c.stage, c.destination, filepath.Dir(name)) + rebased, err := filepath.Rel(parent, filepath.Join(c.stage, target)) + if err != nil { + return err + } + temporary, err := updateDestinationRelative(c.stage, c.temporary) + if err != nil { + return err + } + // Use the whole stage root: Windows determines directory-link type by + // resolving its target, which can intentionally lie outside this clone. + return c.root.Symlink(rebased, filepath.Join(temporary, name)) +} + +// Only an unpromoted owned clone is made writable for removal. Never traverse +// symlink directories or chmod their targets during cleanup. +func removeUpdateDestinationClone(path string) error { + root, err := os.OpenRoot(path) + if err != nil { + return err + } + walkErr := fs.WalkDir( + root.FS(), + ".", + func(name string, entry fs.DirEntry, walkErr error) error { + if walkErr != nil { + return walkErr + } + if entry.Type()&os.ModeSymlink == 0 { + info, err := entry.Info() + if err != nil { + return err + } + return root.Chmod(name, info.Mode().Perm()|0o700) + } + return nil + }, + ) + return errors.Join(walkErr, root.Close(), os.RemoveAll(path)) +} diff --git a/pkg/provider/update_stat.go b/pkg/provider/update_stat.go new file mode 100644 index 0000000000..13131f3f69 --- /dev/null +++ b/pkg/provider/update_stat.go @@ -0,0 +1,39 @@ +package provider + +import ( + "io/fs" + "os" + "path/filepath" + "runtime" + "syscall" +) + +// StatUpdatePath retains Root confinement while avoiding a Windows Root.Stat +// panic when a terminal relative link normalizes to its own parent directory. +// Windows cleans link targets before traversal; POSIX must retain kernel ordering. +func StatUpdatePath(root *os.Root, name string) (fs.FileInfo, error) { + if runtime.GOOS != "windows" { + return root.Stat(name) + } + original := name + // Match Root's link traversal bound without following arbitrary targets. + for remaining := 8; ; remaining-- { + parent, alias := updatePathParentAlias(root, name) + if !alias { + return root.Stat(name) + } + if remaining == 0 { + return nil, &os.PathError{Op: "stat", Path: original, Err: syscall.ELOOP} + } + name = parent + } +} + +func updatePathParentAlias(root *os.Root, name string) (string, bool) { + target, err := root.Readlink(name) + if err != nil || filepath.IsAbs(target) || filepath.VolumeName(target) != "" { + return "", false + } + parent := filepath.Dir(name) + return parent, parent != name && filepath.Join(parent, target) == parent +} diff --git a/pkg/provider/update_stat_test.go b/pkg/provider/update_stat_test.go new file mode 100644 index 0000000000..cf343e9a1b --- /dev/null +++ b/pkg/provider/update_stat_test.go @@ -0,0 +1,90 @@ +package provider + +import ( + "errors" + "os" + "path/filepath" + "runtime" + "syscall" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestStatUpdatePath(t *testing.T) { + base := t.TempDir() + requireUpdateStatSymlinks(t, base) + owned := filepath.Join(base, "owned") + nested := filepath.Join(owned, "nested") + require.NoError(t, os.MkdirAll(filepath.Join(nested, "sub"), 0o700)) + require.NoError(t, os.WriteFile(filepath.Join(owned, "file"), []byte("owned"), 0o600)) + require.NoError(t, os.Mkdir(filepath.Join(owned, "directory"), 0o700)) + require.NoError(t, os.Symlink(".", filepath.Join(nested, "dot"))) + require.NoError( + t, + os.Symlink("sub"+string(filepath.Separator)+"..", filepath.Join(nested, "cancelled")), + ) + require.NoError(t, os.Symlink("../file", filepath.Join(nested, "regular"))) + require.NoError(t, os.Symlink("../directory", filepath.Join(nested, "directory-link"))) + + outside := filepath.Join(base, "outside") + require.NoError(t, os.Mkdir(outside, 0o700)) + sentinel := filepath.Join(outside, "sentinel") + require.NoError(t, os.WriteFile(sentinel, []byte("untouched"), 0o600)) + require.NoError(t, os.Symlink("../../outside", filepath.Join(nested, "escape"))) + require.NoError(t, os.Symlink("cycle-b", filepath.Join(nested, "cycle-a"))) + require.NoError(t, os.Symlink("cycle-a", filepath.Join(nested, "cycle-b"))) + + root, err := os.OpenRoot(owned) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, root.Close()) }) + + t.Run("nested_dot_directory", func(t *testing.T) { + info, err := StatUpdatePath(root, filepath.Join("nested", "dot")) + require.NoError(t, err) + require.True(t, info.IsDir()) + }) + t.Run("nested_cancelled_child", func(t *testing.T) { + info, err := StatUpdatePath(root, filepath.Join("nested", "cancelled")) + require.NoError(t, err) + require.True(t, info.IsDir()) + }) + t.Run("regular_file", func(t *testing.T) { + info, err := StatUpdatePath(root, filepath.Join("nested", "regular")) + require.NoError(t, err) + require.True(t, info.Mode().IsRegular()) + require.Equal(t, int64(len("owned")), info.Size()) + }) + t.Run("directory_target", func(t *testing.T) { + info, err := StatUpdatePath(root, filepath.Join("nested", "directory-link")) + require.NoError(t, err) + require.True(t, info.IsDir()) + }) + t.Run("external_target", func(t *testing.T) { + _, err := StatUpdatePath(root, filepath.Join("nested", "escape")) + require.Error(t, err) + // #nosec G304 -- Sentinel belongs to this test's private temporary directory. + actual, readErr := os.ReadFile(sentinel) + require.NoError(t, readErr) + require.Equal(t, []byte("untouched"), actual) + }) + t.Run("cycle", func(t *testing.T) { + _, err := StatUpdatePath(root, filepath.Join("nested", "cycle-a")) + require.Error(t, err) + }) +} + +func requireUpdateStatSymlinks(t *testing.T, base string) { + t.Helper() + if runtime.GOOS == "windows" { + probe := filepath.Join(base, "symlink-capability") + err := os.Symlink(".", probe) + // ERROR_PRIVILEGE_NOT_HELD is Windows errno 1314; other setup errors must fail. + const privilegeNotHeld = syscall.Errno(1314) + if errors.Is(err, privilegeNotHeld) { + t.Skipf("Windows account cannot create symbolic links: %v", err) + } + require.NoError(t, err) + require.NoError(t, os.Remove(probe)) + } +} diff --git a/pkg/workspace/provider.go b/pkg/workspace/provider.go index 51371bcbb7..2d6bc3e253 100644 --- a/pkg/workspace/provider.go +++ b/pkg/workspace/provider.go @@ -365,16 +365,7 @@ func updateProvider(ctx context.Context, p ProviderParams) (*provider.ProviderCo return nil, err } - err = config.UpdateConfig(p.DevsyConfig.DefaultContext, "", func(c *config.Config) error { - cleanupOldOptions(c, providerConfig) - clearInitialized(c, providerConfig.Name) - return nil - }) - if err != nil { - return nil, err - } - - if err := downloadAndSaveProvider(ctx, p, providerConfig); err != nil { + if err := replaceProviderPrepared(ctx, p, providerConfig); err != nil { return nil, err } diff --git a/pkg/workspace/provider_update_absolute_reference_test.go b/pkg/workspace/provider_update_absolute_reference_test.go new file mode 100644 index 0000000000..785037faa6 --- /dev/null +++ b/pkg/workspace/provider_update_absolute_reference_test.go @@ -0,0 +1,231 @@ +package workspace + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "github.com/devsy-org/devsy/pkg/provider" + "github.com/stretchr/testify/require" +) + +const absoluteReferenceAliasKey = "ALIAS" + +func absoluteReferenceArchiveBinary(t *testing.T, content []byte) *provider.ProviderBinary { + t.Helper() + server := compatibilityArchiveServer( + t, + singleExecutableUpdateArchive(t, content), + ) + binary := updateTestBinary(server.URL+"/provider.tar", content) + binary.ArchivePath = updateRuntimeName + return binary +} + +func TestProviderUpdateAbsoluteReferenceUsesFinalStagedTarget(t *testing.T) { + t.Run("unchanged managed target succeeds", testUnchangedManagedAbsoluteReference) + t.Run("changed managed target with matching new checksum succeeds", func(t *testing.T) { + assertManagedAbsoluteReplacementAccepted(t, false, true) + }) + t.Run("replaced managed symlink with matching new checksum succeeds", func(t *testing.T) { + assertManagedAbsoluteReplacementAccepted(t, true, true) + }) + t.Run("new managed target with matching checksum succeeds", func(t *testing.T) { + assertManagedAbsoluteReplacementAccepted(t, false, false) + }) + + t.Run("changed managed target rejects and preserves live state", func(t *testing.T) { + assertChangedManagedAbsoluteReferenceRejected(t, false) + }) + + t.Run("contained symlink follows changed staged target", func(t *testing.T) { + assertChangedManagedAbsoluteReferenceRejected(t, true) + }) + + t.Run("external symlink into managed tree fails closed", testExternalSymlinkIntoManagedTree) + t.Run( + "case-variant managed path cannot bypass validation", + testCaseVariantManagedAbsoluteReference, + ) + + t.Run( + "outside reference remains untouched while managed tree updates", + testOutsideAbsoluteReferenceUnchanged, + ) +} + +func assertManagedAbsoluteReplacementAccepted(t *testing.T, throughSymlink, seedTarget bool) { + t.Helper() + f := newPreservationFixture(t) + oldContent := []byte("old managed executable") + newContent := []byte("new managed executable") + if seedTarget { + f.seedBinary("B", oldContent) + } + aliasPath := f.binaryPath("B") + aliasKey := absoluteReferenceAliasKey + replacementKey := "B" + if throughSymlink { + requireProviderUpdateSymlinks(t) + aliasKey = "REFERENCE" + replacementKey = absoluteReferenceAliasKey + aliasPath = f.binaryPath(absoluteReferenceAliasKey) + require.NoError(t, os.MkdirAll(filepath.Dir(aliasPath), 0o700)) + require.NoError(t, os.Symlink(filepath.Join("..", "b", updateRuntimeName), aliasPath)) + } + alias := updateTestBinary(aliasPath, newContent) + err := f.update(map[string][]*provider.ProviderBinary{ + aliasKey: {alias}, + replacementKey: {absoluteReferenceArchiveBinary(t, newContent)}, + }) + require.NoError(t, err) + // #nosec G304 -- aliasPath is confined to this fixture's owned provider tree. + actual, err := os.ReadFile(aliasPath) + require.NoError(t, err) + require.Equal(t, newContent, actual) + info, err := os.Lstat(aliasPath) + require.NoError(t, err) + require.True(t, info.Mode().IsRegular()) + stored, err := provider.LoadProviderConfig(testDefaultContext, f.name) + require.NoError(t, err) + require.Equal(t, aliasPath, stored.Binaries[aliasKey][0].Path) + require.Equal(t, alias.Checksum, stored.Binaries[aliasKey][0].Checksum) + if throughSymlink { + preserved, readErr := os.ReadFile(f.binaryPath("B")) + require.NoError(t, readErr) + require.Equal(t, oldContent, preserved) + } + f.assertAssociation() + assertNoUpdateStaging(t, f) +} + +func testUnchangedManagedAbsoluteReference(t *testing.T) { + f := newPreservationFixture(t) + managedContent := []byte("unchanged managed executable") + f.seedBinary("B", managedContent) + + err := f.update(map[string][]*provider.ProviderBinary{ + absoluteReferenceAliasKey: {updateTestBinary(f.binaryPath("B"), managedContent)}, + "C": { + absoluteReferenceArchiveBinary(t, []byte("updated independent executable")), + }, + }) + require.NoError(t, err) + actual, err := os.ReadFile(f.binaryPath("B")) + require.NoError(t, err) + require.Equal(t, managedContent, actual) + stored, err := provider.LoadProviderConfig(testDefaultContext, f.name) + require.NoError(t, err) + require.Equal(t, f.binaryPath("B"), stored.Binaries[absoluteReferenceAliasKey][0].Path) + f.assertAssociation() + assertNoUpdateStaging(t, f) +} + +func testExternalSymlinkIntoManagedTree(t *testing.T) { + requireProviderUpdateSymlinks(t) + f := newPreservationFixture(t) + managedContent := []byte("managed executable") + f.seedBinary("B", managedContent) + externalAlias := filepath.Join(t.TempDir(), "external-alias") + require.NoError(t, os.Symlink(f.binaryPath("B"), externalAlias)) + + err := f.update(map[string][]*provider.ProviderBinary{ + absoluteReferenceAliasKey: {updateTestBinary(externalAlias, managedContent)}, + "B": { + absoluteReferenceArchiveBinary(t, []byte("updated managed executable")), + }, + }) + require.ErrorContains(t, err, "external alias; unsupported") + f.assertFailedPreservation() + assertNoUpdateStaging(t, f) + info, statErr := os.Lstat(externalAlias) + require.NoError(t, statErr) + require.NotZero(t, info.Mode()&os.ModeSymlink) +} + +func testOutsideAbsoluteReferenceUnchanged(t *testing.T) { + f := newPreservationFixture(t) + outsideContent := []byte("independently owned executable") + outsidePath := filepath.Join(t.TempDir(), "outside-runtime") + f.put(outsidePath, outsideContent) + f.seedBinary("B", []byte("old managed executable")) + + err := f.update(map[string][]*provider.ProviderBinary{ + "OUTSIDE": {updateTestBinary(outsidePath, outsideContent)}, + "B": {absoluteReferenceArchiveBinary(t, []byte("new managed executable"))}, + }) + require.NoError(t, err) + // #nosec G304 -- outsidePath was created inside this test's owned temp directory. + actual, err := os.ReadFile(outsidePath) + require.NoError(t, err) + require.Equal(t, outsideContent, actual) + stored, err := provider.LoadProviderConfig(testDefaultContext, f.name) + require.NoError(t, err) + require.Equal(t, outsidePath, stored.Binaries["OUTSIDE"][0].Path) + f.assertAssociation() + assertNoUpdateStaging(t, f) +} + +func testCaseVariantManagedAbsoluteReference(t *testing.T) { + f := newPreservationFixture(t) + oldContent := []byte("old managed executable") + f.seedBinary("B", oldContent) + caseVariantRoot := strings.ToUpper(f.binaries) + liveInfo, err := os.Stat(f.binaries) + require.NoError(t, err) + variantInfo, err := os.Stat(caseVariantRoot) + if os.IsNotExist(err) { + t.Skip("case-variant path does not resolve on this filesystem") + } + require.NoError(t, err) + if !os.SameFile(liveInfo, variantInfo) { + t.Skip("case-variant path is a different directory on this filesystem") + } + variantPath := filepath.Join(caseVariantRoot, "b", updateRuntimeName) + err = f.update(map[string][]*provider.ProviderBinary{ + absoluteReferenceAliasKey: {updateTestBinary(variantPath, oldContent)}, + "B": { + absoluteReferenceArchiveBinary(t, []byte("new managed executable")), + }, + }) + require.Error(t, err) + f.assertFailedPreservation() + assertNoUpdateStaging(t, f) +} + +func assertChangedManagedAbsoluteReferenceRejected(t *testing.T, throughSymlink bool) { + t.Helper() + f := newPreservationFixture(t) + oldContent := []byte("old managed executable") + newContent := []byte("new managed executable") + f.seedBinary("B", oldContent) + aliasPath := f.binaryPath("B") + aliasKey := absoluteReferenceAliasKey + replacementKey := "B" + if throughSymlink { + requireProviderUpdateSymlinks(t) + aliasKey = "REFERENCE" + replacementKey = absoluteReferenceAliasKey + aliasPath = f.binaryPath(absoluteReferenceAliasKey) + require.NoError(t, os.MkdirAll(filepath.Dir(aliasPath), 0o700)) + require.NoError(t, os.Symlink(filepath.Join("..", "b", updateRuntimeName), aliasPath)) + } + + updated := map[string][]*provider.ProviderBinary{ + aliasKey: {updateTestBinary(aliasPath, oldContent)}, + replacementKey: {absoluteReferenceArchiveBinary(t, newContent)}, + } + err := f.update(updated) + require.ErrorContains(t, err, "absolute provider binary checksum mismatch") + f.assertFailedPreservation() + assertNoUpdateStaging(t, f) + if throughSymlink { + info, statErr := os.Lstat(aliasPath) + require.NoError(t, statErr) + require.NotZero(t, info.Mode()&os.ModeSymlink) + target, readErr := os.Readlink(aliasPath) + require.NoError(t, readErr) + require.Equal(t, filepath.Join("..", "b", updateRuntimeName), target) + } +} diff --git a/pkg/workspace/provider_update_archive_test.go b/pkg/workspace/provider_update_archive_test.go new file mode 100644 index 0000000000..d640bb9264 --- /dev/null +++ b/pkg/workspace/provider_update_archive_test.go @@ -0,0 +1,81 @@ +package workspace + +import ( + "archive/tar" + "bytes" + "compress/gzip" + "net/http" + "os" + "path/filepath" + "runtime" + "testing" + + "github.com/devsy-org/devsy/pkg/provider" + "github.com/stretchr/testify/require" +) + +type ownedArchiveEntry struct{ name, target, body string } + +func ownedContainmentArchive(t *testing.T, entries []ownedArchiveEntry) []byte { + t.Helper() + var data bytes.Buffer + gz := gzip.NewWriter(&data) + writer := tar.NewWriter(gz) + for _, entry := range entries { + header := &tar.Header{Name: entry.name, Mode: 0o700, Size: int64(len(entry.body))} + if entry.target != "" { + header.Typeflag = tar.TypeSymlink + header.Linkname = entry.target + header.Size = 0 + } + require.NoError(t, writer.WriteHeader(header)) + if entry.body != "" { + _, err := writer.Write([]byte(entry.body)) + require.NoError(t, err) + } + } + require.NoError(t, writer.Close()) + require.NoError(t, gz.Close()) + return data.Bytes() +} + +func TestProviderUpdateArchivePreservesOwnedDaemon(t *testing.T) { + requireProviderUpdateSymlinks(t) + f := newPreservationFixture(t) + f.seedBinary(updateRuntimeKey, []byte("old runtime")) + entries := []ownedArchiveEntry{ + {name: "linkA", target: "."}, + {name: "linkB", target: "linkA/.."}, + {name: "linkC", target: "linkB/.."}, + {name: "linkD", target: "linkC/.."}, + {name: "linkD/daemon/owned-state", body: "archive escaped staging into owned daemon"}, + {name: updateRuntimeName, body: updateExecutableContent}, + } + if runtime.GOOS == updateWindowsOS { + // Windows cleans link targets before traversal, so use a direct relative + // escape to the fixture-owned daemon rather than the POSIX link-order chain. + entries = []ownedArchiveEntry{ + {name: "escape", target: filepath.Join("..", "..", "..", "daemon")}, + {name: "escape/owned-state", body: "archive escaped staging into owned daemon"}, + {name: updateRuntimeName, body: updateExecutableContent}, + } + } + archive := ownedContainmentArchive(t, entries) + server := ownedProviderUpdateServer( + t, + func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write(archive) }, + ) + binary := updateTestBinary(server.URL+"/owned.tar.gz", []byte(updateExecutableContent)) + binary.ArchivePath = updateRuntimeName + err := f.update(map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}}) + require.Error( + t, + err, + "confined preparation must reject the member before writing outside staging", + ) + actual, readErr := os.ReadFile(filepath.Join(f.dir, "daemon", "owned-state")) + require.NoError(t, readErr) + t.Logf("owned daemon sentinel after preparation error=%q", actual) + f.assertFailedPreservation() + assertNoUpdateStaging(t, f) +} diff --git a/pkg/workspace/provider_update_extended_test.go b/pkg/workspace/provider_update_extended_test.go new file mode 100644 index 0000000000..a468cf5032 --- /dev/null +++ b/pkg/workspace/provider_update_extended_test.go @@ -0,0 +1,411 @@ +package workspace + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "runtime" + "strings" + "sync" + "testing" + "time" + + "github.com/devsy-org/devsy/pkg/config" + "github.com/devsy-org/devsy/pkg/provider" + "github.com/devsy-org/devsy/pkg/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const ( + updateRuntimeKey = "RUNTIME" + updateRuntimeName = "runtime" + updateKeptOption = "KEEP" + updateObsoleteOption = "OBSOLETE" + updateNoopCommand = "true" + updateReplacementVersion = "2.0.0" + updateFirstKey = "FIRST" + updateSecondKey = "SECOND" + updateWindowsOS = "windows" + updateSidecarPath = "lib/sidecar" + updateExecutableContent = "new executable" + updateUnrelatedContent = "keep" + updateRestoreFailure = "restore failure" + updateBackupPhase = "backup" + updatePromotionPhase = "promotion" + updateCurrentLink = "current" + updateMiddleLink = "middle" + updateEntryLink = "entry" +) + +type preservationFixture struct { + t *testing.T + config *config.Config + name, dir, binaries string + before map[string][]byte + options map[string]config.OptionValue + workspace *provider.Workspace +} + +func newPreservationFixture(t *testing.T) *preservationFixture { + t.Helper() + setupTestPathManager(t) + t.Setenv(config.EnvHome, t.TempDir()) + config.ResetPathManager() + f := &preservationFixture{ + t: t, + name: "update-preservation-extended", + before: map[string][]byte{}, + options: map[string]config.OptionValue{ + updateKeptOption: {Value: "kept", UserProvided: true}, + updateObsoleteOption: {Value: "retain on failure", UserProvided: true}, + }, + } + f.config = &config.Config{ + DefaultContext: testDefaultContext, + Contexts: map[string]*config.ContextConfig{testDefaultContext: { + DefaultProvider: f.name, + Providers: map[string]*config.ProviderConfig{ + f.name: {Initialized: true, InitAttempted: true, Options: f.options}, + }, + }}, + } + require.NoError(t, config.SaveConfig(f.config)) + old := &provider.ProviderConfig{ + Name: f.name, + Version: "1.0.0", + Exec: provider.ProviderCommands{Command: []string{updateNoopCommand}}, + Source: provider.ProviderSource{ + Internal: true, + Raw: "microsandbox", + }, + Options: map[string]*types.Option{updateKeptOption: {}, updateObsoleteOption: {}}, + } + require.NoError(t, provider.SaveProviderConfig(testDefaultContext, old)) + var err error + f.dir, err = provider.GetProviderDir(testDefaultContext, f.name) + require.NoError(t, err) + f.binaries, err = provider.GetProviderBinariesDir(testDefaultContext, f.name) + require.NoError(t, err) + path := filepath.Join(f.dir, provider.ProviderConfigFile) + // #nosec G304 -- Path is the manifest in this fixture's isolated provider directory. + f.before[path], err = os.ReadFile(path) + require.NoError(t, err) + f.put(filepath.Join(f.dir, "owned-sentinel"), []byte("untouched provider content")) + f.put(filepath.Join(f.dir, "daemon", "owned-state"), []byte("untouched daemon state")) + f.workspace = &provider.Workspace{ + ID: "existing-workspace", + UID: "original-uid", + Context: testDefaultContext, + Provider: provider.WorkspaceProviderConfig{ + Name: f.name, + Options: map[string]config.OptionValue{ + "WORKSPACE_OPTION": {Value: "original", UserProvided: true}, + }, + }, + } + require.NoError(t, provider.SaveWorkspaceConfig(f.workspace)) + t.Logf("isolated provider root: %s", f.dir) + return f +} + +func (f *preservationFixture) put(path string, data []byte) { + f.t.Helper() + require.NoError(f.t, os.MkdirAll(filepath.Dir(path), 0o700)) + // #nosec G306 -- The fixture writes executable bytes to test provider binary preservation. + require.NoError(f.t, os.WriteFile(path, data, 0o700)) + f.before[path] = append([]byte(nil), data...) +} + +func (f *preservationFixture) binaryPath(key string) string { + return filepath.Join(f.binaries, strings.ToLower(key), updateRuntimeName) +} + +func (f *preservationFixture) seedBinary(key string, content []byte) { + f.t.Helper() + f.put(f.binaryPath(key), content) + manifestPath := filepath.Join(f.dir, provider.ProviderConfigFile) + // #nosec G304 -- Manifest path belongs to this fixture's isolated provider directory. + raw, err := os.ReadFile(manifestPath) + require.NoError(f.t, err) + var old provider.ProviderConfig + require.NoError(f.t, json.Unmarshal(raw, &old)) + if old.Binaries == nil { + old.Binaries = map[string][]*provider.ProviderBinary{} + } + old.Binaries[key] = []*provider.ProviderBinary{ + updateTestBinary("unavailable-original-source/runtime", content), + } + require.NoError(f.t, provider.SaveProviderConfig(testDefaultContext, &old)) + // #nosec G304 -- Snapshot the manifest in this test-owned provider directory. + f.before[manifestPath], err = os.ReadFile(manifestPath) + require.NoError(f.t, err) +} + +func updateTestDigest(data []byte) string { + sum := sha256.Sum256(data) + return hex.EncodeToString(sum[:]) +} + +func updateTestBinary(path string, content []byte) *provider.ProviderBinary { + return &provider.ProviderBinary{ + OS: runtime.GOOS, + Arch: runtime.GOARCH, + Name: updateRuntimeName, + Path: path, + Checksum: updateTestDigest(content), + } +} + +func (f *preservationFixture) update(binaries map[string][]*provider.ProviderBinary) error { + f.t.Helper() + replacement := &provider.ProviderConfig{ + Name: f.name, + Version: updateReplacementVersion, + Exec: provider.ProviderCommands{Command: []string{updateNoopCommand}}, + Options: map[string]*types.Option{updateKeptOption: {}}, + Binaries: binaries, + } + raw, err := json.Marshal(replacement) + require.NoError(f.t, err) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + _, err = updateProvider( + ctx, + ProviderParams{ + DevsyConfig: f.config, + ProviderName: f.name, + Raw: raw, + Source: &provider.ProviderSource{ + Raw: "owned-replacement", + URL: "http://owned.invalid/provider.yaml", + }, + }, + ) + require.NoError(f.t, ctx.Err(), "update must terminate by concrete result, not deadline") + f.t.Logf("actual update result: %v", err) + return err +} + +func (f *preservationFixture) assertAssociation() *config.ProviderConfig { + f.t.Helper() + stored, err := config.LoadConfig(testDefaultContext, "") + require.NoError(f.t, err) + assert.Equal(f.t, f.name, stored.Current().DefaultProvider) + workspace, err := provider.LoadWorkspaceConfig(testDefaultContext, f.workspace.ID) + require.NoError(f.t, err) + assert.Equal(f.t, f.workspace.UID, workspace.UID) + assert.Equal(f.t, f.workspace.Provider, workspace.Provider) + state := stored.Current().Providers[f.name] + require.NotNil(f.t, state) + f.t.Logf( + "workspace and default association retained; initialized=%v options=%v", + state.Initialized, + state.Options, + ) + return state +} + +func (f *preservationFixture) assertFailedPreservation() { + f.t.Helper() + for path, expected := range f.before { + // #nosec G304 -- Every snapshot path is created and owned by this fixture. + actual, err := os.ReadFile(path) + f.t.Logf("preserved path %s: exists=%v", path, err == nil) + if assert.NoError(f.t, err, "existing bytes must survive: %s", path) { + assert.Equal(f.t, expected, actual, path) + } + } + state := f.assertAssociation() + assert.True(f.t, state.Initialized) + assert.True(f.t, state.InitAttempted) + assert.Equal(f.t, f.options, state.Options) +} + +func ownedProviderUpdateServer(t *testing.T, handler http.HandlerFunc) *httptest.Server { + t.Helper() + server := httptest.NewServer(handler) + t.Cleanup(func() { + server.CloseClientConnections() + server.Close() + t.Log("owned HTTP server closed and joined") + }) + return server +} + +func TestProviderUpdateSameNameHTTPFailurePreservesExisting(t *testing.T) { + f := newPreservationFixture(t) + old := []byte("old same-name runtime") + f.seedBinary(updateRuntimeKey, old) + var mu sync.Mutex + requests := 0 + oldPresentDuringRequest := false + server := ownedProviderUpdateServer(t, func(w http.ResponseWriter, r *http.Request) { + data, err := os.ReadFile(f.binaryPath(updateRuntimeKey)) + mu.Lock() + requests++ + oldPresentDuringRequest = err == nil && string(data) == string(old) + mu.Unlock() + http.Error(w, "owned artifact unavailable", http.StatusNotFound) + }) + err := f.update( + map[string][]*provider.ProviderBinary{ + updateRuntimeKey: { + updateTestBinary(server.URL+"/runtime", []byte("different checksum")), + }, + }, + ) + require.ErrorContains(t, err, "received status code 404") + mu.Lock() + count, present := requests, oldPresentDuringRequest + mu.Unlock() + require.Equal(t, 1, count) + t.Logf("old same-name bytes present during HTTP request: %v", present) + assert.True( + t, + present, + "live old binary must not be removed before a replacement download succeeds", + ) + f.assertFailedPreservation() +} + +func TestProviderUpdatePartialMultiBinaryFailurePreservesExisting(t *testing.T) { + f := newPreservationFixture(t) + for _, key := range []string{updateFirstKey, updateSecondKey} { + f.seedBinary(key, []byte("old "+key)) + } + replacement := []byte("new complete bytes unique to " + f.dir) + var mu sync.Mutex + requests := 0 + firstKey := "" + firstPublishedBeforeFailure := false + server := ownedProviderUpdateServer(t, func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + defer mu.Unlock() + requests++ + if requests == 1 { + firstKey = strings.TrimPrefix(r.URL.Path, "/") + _, _ = w.Write(replacement) + return + } + data, err := os.ReadFile(f.binaryPath(firstKey)) + firstPublishedBeforeFailure = err == nil && string(data) == string(replacement) + http.Error(w, "second requested artifact deliberately fails", http.StatusNotFound) + }) + err := f.update(map[string][]*provider.ProviderBinary{ + updateFirstKey: { + updateTestBinary(server.URL+"/FIRST", replacement), + }, + updateSecondKey: {updateTestBinary(server.URL+"/SECOND", replacement)}, + }) + require.ErrorContains(t, err, "received status code 404") + mu.Lock() + count, first, published := requests, firstKey, firstPublishedBeforeFailure + mu.Unlock() + require.Equal(t, 2, count, "must complete first download before second request fails") + require.Contains(t, []string{updateFirstKey, updateSecondKey}, first) + t.Logf( + "actual first completed artifact=%s; replacement bytes visible in live tree before second failure=%v", + first, + published, + ) + assert.False( + t, + published, + "a partial replacement must not publish any new live bytes before all preparation succeeds", + ) + f.assertFailedPreservation() +} + +func TestProviderUpdateInstalledOnlyOfflineReuse(t *testing.T) { + f := newPreservationFixture(t) + content := []byte("valid already installed offline executable") + f.seedBinary(updateRuntimeKey, content) + // A relative source that does not exist: only the valid installed target can satisfy this update. + relative := filepath.Join( + "definitely-absent-provider-test", + filepath.Base(f.dir), + "source-runtime", + ) + _, statErr := os.Stat(relative) + require.ErrorIs(t, statErr, os.ErrNotExist) + err := f.update( + map[string][]*provider.ProviderBinary{ + updateRuntimeKey: {updateTestBinary(relative, content)}, + }, + ) + require.NoError(t, err) + actual, err := os.ReadFile(f.binaryPath(updateRuntimeKey)) + require.NoError(t, err) + assert.Equal(t, content, actual) + for _, path := range []string{filepath.Join(f.dir, "owned-sentinel"), filepath.Join(f.dir, "daemon", "owned-state")} { + // #nosec G304 -- These are fixture-owned sentinel and daemon paths. + actual, err = os.ReadFile(path) + require.NoError(t, err) + assert.Equal(t, f.before[path], actual) + } + // Read persisted JSON directly: internal-load compatibility overlays must not hide stored state. + raw, err := os.ReadFile(filepath.Join(f.dir, provider.ProviderConfigFile)) + require.NoError(t, err) + var replacement provider.ProviderConfig + require.NoError(t, json.Unmarshal(raw, &replacement)) + assert.Equal(t, updateReplacementVersion, replacement.Version) + assert.Equal(t, f.name, replacement.Name) + assert.Equal(t, "owned-replacement", replacement.Source.Raw) + state := f.assertAssociation() + assert.False(t, state.Initialized) + assert.True(t, state.InitAttempted) + assert.Equal( + t, + map[string]config.OptionValue{updateKeptOption: f.options[updateKeptOption]}, + state.Options, + ) + t.Log( + "PASS: installed-only matching checksum reused without source file or network; " + + "successful-update state semantics preserved", + ) +} + +func TestProviderUpdateAbsoluteChecksumMismatchPreservesExternal(t *testing.T) { + f := newPreservationFixture(t) + external := filepath.Join(t.TempDir(), "externally-owned-runtime") + content := []byte("external local executable owned independently") + f.put(external, content) + require.False(t, strings.HasPrefix(external, f.dir+string(filepath.Separator))) + err := f.update( + map[string][]*provider.ProviderBinary{ + updateRuntimeKey: {updateTestBinary(external, []byte("mismatched expected content"))}, + }, + ) + require.Error(t, err) + require.ErrorContains(t, err, "download binaries") + t.Logf( + "absolute source is outside providerDir; observed error after mismatch: %s", + fmt.Sprint(err), + ) + f.assertFailedPreservation() +} + +// Windows may require a privilege or Developer Mode to create symbolic links. +// Only tests that exercise links use this check; ordinary update tests still run. +func requireProviderUpdateSymlinks(t *testing.T) { + t.Helper() + root := t.TempDir() + file := filepath.Join(root, "file") + require.NoError(t, os.WriteFile(file, []byte("owned link capability control"), 0o600)) + dir := filepath.Join(root, "directory") + require.NoError(t, os.Mkdir(dir, 0o700)) + for _, target := range []string{file, dir} { + err := os.Symlink(target, target+"-link") + if err != nil && runtime.GOOS == updateWindowsOS { + t.Skipf("symbolic links unavailable on this Windows runner: %v", err) + } + require.NoError(t, err) + } +} diff --git a/pkg/workspace/provider_update_permissions_unix_test.go b/pkg/workspace/provider_update_permissions_unix_test.go new file mode 100644 index 0000000000..7a4aee50cc --- /dev/null +++ b/pkg/workspace/provider_update_permissions_unix_test.go @@ -0,0 +1,223 @@ +//go:build !windows + +package workspace + +import ( + "archive/tar" + "io/fs" + "net/http" + "os" + "path/filepath" + "sync/atomic" + "testing" + + "github.com/devsy-org/devsy/pkg/provider" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestProviderUpdateReadOnlyDirectorySeed(t *testing.T) { + if os.Geteuid() == 0 { + t.Skip("requires POSIX permission enforcement for an ordinary user") + } + for _, failure := range []bool{false, true} { + t.Run( + map[bool]string{false: "installed-only success", true: "download failure preserves live modes"}[failure], + func(t *testing.T) { + f := newPreservationFixture(t) + content := []byte("installed-only executable") + f.seedBinary(updateRuntimeKey, content) + ancillary, modes := seedReadOnlyAncillaryTree(t, f) + binary := updateTestBinary("unavailable-original-source/runtime", content) + if failure { + server := ownedProviderUpdateServer( + t, + func(w http.ResponseWriter, r *http.Request) { + http.Error(w, "owned missing replacement", http.StatusNotFound) + }, + ) + binary = updateTestBinary(server.URL+"/runtime", []byte("new replacement")) + } + err := f.update(map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}}) + if failure { + require.ErrorContains( + t, + err, + "404", + "seeding must succeed before the concrete download failure", + ) + f.assertFailedPreservation() + } else { + require.NoError(t, err) + // #nosec G304 -- Ancillary path belongs to the isolated archive fixture. + actual, readErr := os.ReadFile(ancillary) + require.NoError(t, readErr) + assert.Equal(t, f.before[ancillary], actual) + assertSelectedExecutable( + t, + f.binaryPath(updateRuntimeKey), + content, + binary.Checksum, + ) + } + for path, mode := range modes { + info, statErr := os.Stat(path) + require.NoError(t, statErr) + assert.Equal(t, mode, info.Mode().Perm(), path) + } + assertNoUpdateStaging(t, f) + }, + ) + } +} + +func seedReadOnlyAncillaryTree( + t *testing.T, + f *preservationFixture, +) (string, map[string]os.FileMode) { + t.Helper() + ancillary := filepath.Join(f.binaries, "unrelated", "read-only", "data") + f.put(ancillary, []byte("unchanged ancillary bytes")) + modes := map[string]os.FileMode{ + filepath.Dir(ancillary): 0o550, + filepath.Join(f.binaries, "unrelated"): 0o770, + } + t.Cleanup(func() { makeOwnedUpdateTreeRemovable(t, f.dir) }) + for path, mode := range modes { + require.NoError(t, os.Chmod(path, mode)) + } + return ancillary, modes +} + +func makeOwnedUpdateTreeRemovable(t *testing.T, directory string) { + t.Helper() + root, err := os.OpenRoot(directory) + if !assert.NoError(t, err) { + return + } + defer func() { assert.NoError(t, root.Close()) }() + err = fs.WalkDir(root.FS(), ".", func(name string, entry fs.DirEntry, walkErr error) error { + if walkErr != nil { + return walkErr + } + if entry.IsDir() { + // #nosec G302 -- Owner rwx is required to remove directories in this test-owned tree. + return root.Chmod(name, 0o700) + } + return nil + }) + assert.NoError(t, err, "make only this test-owned tree removable") +} + +func TestProviderUpdateReadOnlySelectedArchiveFamily(t *testing.T) { + if os.Geteuid() == 0 { + t.Skip("requires POSIX permission enforcement for an ordinary user") + } + for _, failure := range []bool{false, true} { + t.Run( + map[bool]string{false: "fresh download", true: "failed download"}[failure], + func(t *testing.T) { + f := newPreservationFixture(t) + f.seedBinary(updateRuntimeKey, []byte("old selected runtime")) + f.seedBinary("UNRELATED", []byte("unrelated runtime")) + oldMember := seedReadOnlySelectedUpdateFamily(t, f) + unrelatedInfo, err := os.Stat(f.binaryPath("UNRELATED")) + require.NoError(t, err) + content := []byte("fresh readonly-family replacement") + archive := singleExecutableUpdateArchive(t, content) + server := ownedProviderUpdateServer( + t, + func(w http.ResponseWriter, _ *http.Request) { + if failure { + http.Error(w, "owned replacement missing", http.StatusNotFound) + return + } + _, _ = w.Write(archive) + }, + ) + binary := updateTestBinary(server.URL+"/readonly.tar", content) + binary.ArchivePath = updateRuntimeName + err = f.update(map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}}) + if failure { + require.ErrorContains(t, err, "404") + f.assertFailedPreservation() + memberInfo, statErr := os.Stat(oldMember) + require.NoError(t, statErr) + assert.Equal(t, os.FileMode(0o440), memberInfo.Mode().Perm()) + dirInfo, statErr := os.Stat(filepath.Dir(oldMember)) + require.NoError(t, statErr) + assert.Equal(t, os.FileMode(0o550), dirInfo.Mode().Perm()) + } else { + require.NoError(t, err) + assertSelectedExecutable( + t, + f.binaryPath(updateRuntimeKey), + content, + binary.Checksum, + ) + require.NoDirExists(t, filepath.Dir(oldMember)) + } + actual, readErr := os.ReadFile(f.binaryPath("UNRELATED")) + require.NoError(t, readErr) + assert.Equal(t, f.before[f.binaryPath("UNRELATED")], actual) + actualInfo, statErr := os.Stat(f.binaryPath("UNRELATED")) + require.NoError(t, statErr) + assert.Equal(t, unrelatedInfo.Mode().Perm(), actualInfo.Mode().Perm()) + assertNoUpdateStaging(t, f) + }, + ) + } +} + +func seedReadOnlySelectedUpdateFamily(t *testing.T, f *preservationFixture) string { + t.Helper() + oldMember := filepath.Join(f.binaries, updateRuntimeName, "old-readonly", "member") + f.put(oldMember, []byte("obsolete selected-family sidecar")) + t.Cleanup(func() { makeOwnedUpdateTreeRemovable(t, f.dir) }) + // #nosec G302 -- Owned fixture must exercise removal of a read-only selected family. + require.NoError(t, os.Chmod(oldMember, 0o440)) + // #nosec G302 -- Owned fixture preserves readable, non-writable directory permissions. + require.NoError(t, os.Chmod(filepath.Dir(oldMember), 0o550)) + return oldMember +} + +func TestProviderUpdateReadOnlyNestedSelectedExecutable(t *testing.T) { + if os.Geteuid() == 0 { + t.Skip("requires POSIX permission enforcement for an ordinary user") + } + f := newPreservationFixture(t) + f.seedBinary("UNRELATED", []byte(updateUnrelatedContent)) + selected := "readonly/runtime" + oldPath := filepath.Join(f.binaries, updateRuntimeName, filepath.FromSlash(selected)) + f.put(oldPath, []byte("old selected executable inside readonly directory")) + t.Cleanup(func() { makeOwnedUpdateTreeRemovable(t, f.dir) }) + // #nosec G302 -- Owned executable deliberately has no owner-write permission. + require.NoError(t, os.Chmod(oldPath, 0o550)) + // #nosec G302 -- Owned fixture directory must remain searchable but non-writable. + require.NoError(t, os.Chmod(filepath.Dir(oldPath), 0o550)) + content := []byte("new selected executable after safe archive family reset") + archive := compatibilityTar(t, false, []*tar.Header{ + {Name: "readonly", Typeflag: tar.TypeDir, Mode: 0o755}, + {Name: selected, Typeflag: tar.TypeReg, Mode: 0o755, Size: int64(len(content))}, + }, map[string][]byte{selected: content}) + var requests atomic.Int64 + server := ownedProviderUpdateServer(t, func(w http.ResponseWriter, _ *http.Request) { + requests.Add(1) + _, _ = w.Write(archive) + }) + binary := updateTestBinary(server.URL+"/readonly-selected.tar", content) + binary.ArchivePath = selected + require.NoError(t, f.update(map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}})) + require.EqualValues( + t, + 1, + requests.Load(), + "fresh archive must be fetched without a seeded cache", + ) + assertSelectedExecutable(t, oldPath, content, binary.Checksum) + actual, err := os.ReadFile(f.binaryPath("UNRELATED")) + require.NoError(t, err) + assert.Equal(t, f.before[f.binaryPath("UNRELATED")], actual) + f.assertAssociation() + assertNoUpdateStaging(t, f) +} diff --git a/pkg/workspace/provider_update_prepared.go b/pkg/workspace/provider_update_prepared.go new file mode 100644 index 0000000000..3b0d9e26b7 --- /dev/null +++ b/pkg/workspace/provider_update_prepared.go @@ -0,0 +1,690 @@ +package workspace + +import ( + "context" + "errors" + "fmt" + "io" + "io/fs" + "os" + "path/filepath" + "runtime" + "slices" + "strings" + + "github.com/devsy-org/devsy/pkg/config" + "github.com/devsy-org/devsy/pkg/hash" + "github.com/devsy-org/devsy/pkg/log" + "github.com/devsy-org/devsy/pkg/provider" +) + +// The caller holds the provider operation lock. This protects preparation +// failures, not concurrent readers or a crash between separate file commits. +func replaceProviderPrepared( + ctx context.Context, + p ProviderParams, + replacement *provider.ProviderConfig, +) (err error) { + return replaceProviderPreparedWithOps(ctx, p, replacement, defaultProviderUpdateCommitOps()) +} + +func replaceProviderPreparedWithOps( + ctx context.Context, + p ProviderParams, + replacement *provider.ProviderConfig, + ops providerUpdateCommitOps, +) (err error) { + prepared, err := prepareProviderReplacement(ctx, p, replacement) + if err != nil { + return err + } + defer func() { + if prepared.retain { + err = errors.Join( + err, + fmt.Errorf("provider recovery artifacts retained at %s", prepared.stage), + ) + } else if cleanupErr := ops.removeStage(prepared.stage); cleanupErr != nil { + if prepared.committed && err == nil { + log.Warnf( + "remove provider update staging %s after commit: %v", + prepared.stage, + cleanupErr, + ) + return + } + err = errors.Join( + err, + fmt.Errorf("remove owned provider update staging: %w", cleanupErr), + ) + } + }() + if err = ctx.Err(); err != nil { + return err + } + return prepared.commit(p, replacement, ops) +} + +type preparedProviderReplacement struct { + stage, live, manifest string + oldManifest []byte + oldMode fs.FileMode + replaceBinaries bool + hadBinaries bool + backupMoved bool + installed bool + committed bool + retain bool +} + +func prepareProviderReplacement( + ctx context.Context, + p ProviderParams, + replacement *provider.ProviderConfig, +) (_ *preparedProviderReplacement, err error) { + prepared, err := newPreparedProviderReplacement(p, replacement) + if err != nil { + return nil, err + } + defer func() { + if err != nil { + err = errors.Join(err, removeProviderUpdateStage(prepared.stage)) + } + }() + if !prepared.replaceBinaries { + return prepared, ctx.Err() + } + if err = prepared.prepareBinaries(ctx, replacement.Binaries); err != nil { + return nil, err + } + return prepared, nil +} + +func newPreparedProviderReplacement( + p ProviderParams, + replacement *provider.ProviderConfig, +) (_ *preparedProviderReplacement, err error) { + dir, err := provider.GetProviderDir(p.DevsyConfig.DefaultContext, replacement.Name) + if err != nil { + return nil, err + } + if err = requireRegularProviderPath(dir, true); err != nil { + return nil, err + } + old, mode, err := readProviderUpdateManifest(dir) + if err != nil { + return nil, err + } + stage, err := os.MkdirTemp(dir, ".update-staging-") + if err != nil { + return nil, err + } + defer func() { + if err != nil { + err = errors.Join(err, removeProviderUpdateStage(stage)) + } + }() + if err = saveProviderUpdateBackup(stage, old, mode); err != nil { + return nil, err + } + return &preparedProviderReplacement{ + stage: stage, live: filepath.Join(dir, "binaries"), + manifest: filepath.Join(dir, provider.ProviderConfigFile), oldManifest: old, oldMode: mode, + replaceBinaries: len(replacement.Binaries) > 0, + }, nil +} + +func readProviderUpdateManifest(dir string) (data []byte, mode fs.FileMode, err error) { + root, err := os.OpenRoot(dir) + if err != nil { + return nil, 0, err + } + defer func() { err = errors.Join(err, root.Close()) }() + info, err := root.Lstat(provider.ProviderConfigFile) + if err != nil { + return nil, 0, err + } + if !info.Mode().IsRegular() { + return nil, 0, errors.New("provider update manifest must be a regular file") + } + data, err = root.ReadFile(provider.ProviderConfigFile) + return data, info.Mode().Perm(), err +} + +func saveProviderUpdateBackup(stage string, data []byte, mode fs.FileMode) (err error) { + root, err := os.OpenRoot(stage) + if err != nil { + return err + } + defer func() { err = errors.Join(err, root.Close()) }() + return root.WriteFile("old-provider.json", data, mode) +} + +func (p *preparedProviderReplacement) prepareBinaries( + ctx context.Context, + binaries map[string][]*provider.ProviderBinary, +) (err error) { + target := filepath.Join(p.stage, "binaries") + p.hadBinaries, err = copyProviderBinaryTree(ctx, p.live, target) + if err != nil { + return err + } + locations, absoluteReferences, err := stagedProviderLocations(ctx, binaries, p.live, target) + if err != nil { + return fmt.Errorf("download binaries: validate preparation: %w", err) + } + if _, err = provider.DownloadUpdateBinaries(ctx, locations, target); err != nil { + return fmt.Errorf("download binaries: %w", err) + } + if err = ctx.Err(); err != nil { + return err + } + // Archive sidecars are promoted together, but no link may lead outside staging. + _, err = copyProviderBinaryTree(ctx, target, "") + if err != nil { + return err + } + return validateStagedAbsoluteReferences(ctx, absoluteReferences) +} + +func validateStagedAbsoluteReferences( + ctx context.Context, + references []stagedAbsoluteProviderReference, +) error { + for _, reference := range references { + if err := ctx.Err(); err != nil { + return err + } + if err := validateAbsoluteProviderBinaryAtPath( + reference.binary, + reference.stagedPath, + ); err != nil { + return fmt.Errorf( + "absolute provider binary reference changed during preparation: %w", + err, + ) + } + } + return nil +} + +func requireRegularProviderPath(path string, directory bool) error { + info, err := os.Lstat(path) + if err != nil { + return err + } + if info.Mode()&os.ModeSymlink != 0 { + return fmt.Errorf("unsafe provider update symlink: %s", path) + } + if directory && info.IsDir() || !directory && info.Mode().IsRegular() { + return nil + } + return fmt.Errorf("unsupported provider update file type: %s", path) +} + +// A blank destination validates without copying. Seed regular bytes independently; +// contained relative links are copied as links only after their source graph validates. +func copyProviderBinaryTree(ctx context.Context, source, destination string) (_ bool, err error) { + info, err := os.Lstat(source) + if os.IsNotExist(err) { + if destination != "" { + return false, os.MkdirAll(destination, 0o700) + } + return false, nil + } + if err != nil { + return false, err + } + if !info.IsDir() { + return false, fmt.Errorf("unsafe provider update binaries root: %s", source) + } + root, err := os.OpenRoot(source) + if err != nil { + return false, err + } + defer func() { err = errors.Join(err, root.Close()) }() + if err = validateProviderSeed(ctx, root, source); err != nil { + return false, err + } + if destination == "" { + return true, nil + } + return true, copyProviderSeed(ctx, root, destination) +} + +type providerSeedCopier struct { + source, destination *os.Root + links map[string]string + directoryModes map[string]fs.FileMode + directories []string +} + +func copyProviderSeed(ctx context.Context, source *os.Root, destination string) (err error) { + if err = os.MkdirAll(destination, 0o700); err != nil { + return err + } + out, err := os.OpenRoot(destination) + if err != nil { + return err + } + defer func() { err = errors.Join(err, out.Close()) }() + copier := &providerSeedCopier{ + source: source, destination: out, + links: map[string]string{}, directoryModes: map[string]fs.FileMode{}, + } + err = fs.WalkDir(source.FS(), ".", func(name string, entry fs.DirEntry, walkErr error) error { + if walkErr != nil { + return walkErr + } + if err := ctx.Err(); err != nil { + return err + } + return copier.copyEntry(name, entry) + }) + if err != nil { + return err + } + if err = copier.copyLinks(); err != nil { + return err + } + if err = copier.applyDirectoryModes(); err != nil { + return err + } + return validateProviderSeed(ctx, out, destination) +} + +func (c *providerSeedCopier) applyDirectoryModes() error { + // WalkDir records parents before children; apply final modes only after all writes. + for _, name := range slices.Backward(c.directories) { + if err := c.destination.Chmod(name, c.directoryModes[name]); err != nil { + return err + } + } + return nil +} + +func (c *providerSeedCopier) copyEntry(name string, entry fs.DirEntry) error { + info, err := entry.Info() + if err != nil { + return err + } + if info.Mode()&os.ModeSymlink != 0 { + c.links[name], err = c.source.Readlink(name) + return err + } + if info.IsDir() { + c.directoryModes[name] = info.Mode() + c.directories = append(c.directories, name) + return c.destination.MkdirAll(name, 0o700) + } + return copyProviderSeedFile(c.source, c.destination, name, info.Mode()) +} + +func copyProviderSeedFile(source, destination *os.Root, name string, mode fs.FileMode) error { + in, err := source.Open(name) + if err != nil { + return err + } + target, err := destination.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_EXCL, mode.Perm()) + if err != nil { + return errors.Join(err, in.Close()) + } + _, copyErr := io.Copy(target, in) + return errors.Join(copyErr, target.Chmod(mode.Perm()), target.Close(), in.Close()) +} + +func (c *providerSeedCopier) copyLinks() error { + // Windows determines the link type from its target, so create dependencies first. + for len(c.links) > 0 { + copied := false + for name, target := range c.links { + // Keep dot components intact: a preceding symlink can change their resolution. + _, err := provider.StatUpdatePath( + c.destination, + filepath.Dir(name)+string(filepath.Separator)+target, + ) + if errors.Is(err, fs.ErrNotExist) { + continue + } + if err != nil { + return err + } + if err = c.destination.Symlink(target, name); err != nil { + return err + } + delete(c.links, name) + copied = true + } + if !copied { + return errors.New("provider seed link dependencies could not be resolved") + } + } + return nil +} + +func validateProviderSeed(ctx context.Context, root *os.Root, display string) error { + return fs.WalkDir(root.FS(), ".", func(name string, entry fs.DirEntry, walkErr error) error { + if walkErr != nil { + return walkErr + } + if err := ctx.Err(); err != nil { + return err + } + if err := validateProviderSeedEntry(root, name, entry); err != nil { + return fmt.Errorf("provider update seed %s: %w", display, err) + } + return nil + }) +} + +func validateProviderSeedEntry(root *os.Root, name string, entry fs.DirEntry) error { + if entry.Type()&os.ModeSymlink != 0 { + resolved, err := provider.StatUpdatePath(root, name) + if err != nil { + return fmt.Errorf("unsafe provider update symlink %s: %w", name, err) + } + if !resolved.IsDir() && !resolved.Mode().IsRegular() { + return fmt.Errorf("unsupported provider link target: %s", name) + } + return nil + } + if !entry.IsDir() && !entry.Type().IsRegular() { + return fmt.Errorf("unsupported provider update file type: %s", name) + } + return nil +} + +// Absolute locations are references, never downloader targets. Managed references +// are checked after preparation, which can replace or create their targets. +// Outside references are checked in place without destructive checksum cleanup. +func stagedProviderLocations( + ctx context.Context, + binaries map[string][]*provider.ProviderBinary, + live, stagedRoot string, +) (map[string][]*provider.ProviderBinary, []stagedAbsoluteProviderReference, error) { + staged := make(map[string][]*provider.ProviderBinary, len(binaries)) + var absoluteReferences []stagedAbsoluteProviderReference + for key, locations := range binaries { + if err := ctx.Err(); err != nil { + return nil, nil, err + } + staged[key] = locations + reference, absolute, err := stagedProviderLocationReference(locations, live, stagedRoot) + if err != nil { + return nil, nil, err + } + if reference != nil { + absoluteReferences = append(absoluteReferences, *reference) + } + if absolute { + delete(staged, key) + } + } + return staged, absoluteReferences, nil +} + +func stagedProviderLocationReference( + locations []*provider.ProviderBinary, + live, stagedRoot string, +) (*stagedAbsoluteProviderReference, bool, error) { + for _, location := range locations { + if location.OS != runtime.GOOS || location.Arch != runtime.GOARCH { + continue + } + if !filepath.IsAbs(location.Path) { + return nil, false, validateProviderUpdateLocation(location) + } + stagedPath, managed, err := projectManagedAbsoluteReference(location.Path, live, stagedRoot) + if err != nil { + return nil, true, err + } + if managed { + return &stagedAbsoluteProviderReference{ + binary: location, + stagedPath: stagedPath, + }, true, nil + } + return nil, true, validateAbsoluteProviderBinary(location) + } + return nil, false, nil +} + +type stagedAbsoluteProviderReference struct { + binary *provider.ProviderBinary + stagedPath string +} + +func projectManagedAbsoluteReference(path, live, stagedRoot string) (string, bool, error) { + pathAbs, err := filepath.Abs(path) + if err != nil { + return "", false, err + } + liveAbs, err := filepath.Abs(live) + if err != nil { + return "", false, err + } + stagedPath, managed, err := projectLexicalManagedReference( + path, + pathAbs, + liveAbs, + stagedRoot, + ) + if err != nil || managed { + return stagedPath, managed, err + } + return rejectExternalManagedAlias(path, liveAbs) +} + +func projectLexicalManagedReference( + path, pathAbs, liveAbs, stagedRoot string, +) (string, bool, error) { + relative, err := filepath.Rel(liveAbs, pathAbs) + if err != nil || !filepath.IsLocal(relative) { + return "", false, nil + } + if filepath.Clean(path) != path { + return "", false, fmt.Errorf("ambiguous absolute provider reference path: %s", path) + } + return filepath.Join(stagedRoot, relative), true, nil +} + +func rejectExternalManagedAlias(path, liveAbs string) (string, bool, error) { + // An external symlink into the live tree remains attached to the promoted path, + // but cannot be projected without changing its traversal semantics. + resolvedPath, err := filepath.EvalSymlinks(path) + if err != nil { + return "", false, fmt.Errorf("resolve absolute provider reference %s: %w", path, err) + } + contained, err := sharesManagedDirectoryAncestor(liveAbs, resolvedPath) + if err != nil { + return "", false, err + } + if contained { + return "", false, unsupportedExternalManagedAlias(path) + } + return "", false, nil +} + +func unsupportedExternalManagedAlias(path string) error { + return fmt.Errorf( + "absolute provider reference resolves into managed binaries through an external alias; unsupported: %s", + path, + ) +} + +func sharesManagedDirectoryAncestor(live, resolvedPath string) (bool, error) { + liveInfo, err := os.Stat(live) + if os.IsNotExist(err) { + return false, nil + } + if err != nil { + return false, fmt.Errorf("stat managed provider binaries %s: %w", live, err) + } + for ancestor := resolvedPath; ; ancestor = filepath.Dir(ancestor) { + info, err := os.Stat(ancestor) + if err != nil { + return false, fmt.Errorf( + "stat absolute provider reference ancestor %s: %w", + ancestor, + err, + ) + } + if os.SameFile(liveInfo, info) { + return true, nil + } + parent := filepath.Dir(ancestor) + if parent == ancestor { + return false, nil + } + } +} + +func validateProviderUpdateLocation(location *provider.ProviderBinary) error { + for _, target := range []string{location.Name, location.ArchivePath} { + clean := filepath.Clean(target) + if filepath.IsAbs(target) || clean == ".." || + strings.HasPrefix(clean, ".."+string(filepath.Separator)) { + return fmt.Errorf("unsafe provider update binary target: %s", target) + } + } + return nil +} + +func validateAbsoluteProviderBinary(location *provider.ProviderBinary) error { + return validateAbsoluteProviderBinaryAtPath(location, location.Path) +} + +func validateAbsoluteProviderBinaryAtPath(location *provider.ProviderBinary, path string) error { + info, err := os.Stat(path) + if err != nil { + return err + } + if !info.Mode().IsRegular() { + return fmt.Errorf("absolute provider binary is not regular: %s", location.Path) + } + if location.Checksum == "" { + return nil + } + sum, err := hash.File(path) + if err != nil { + return err + } + if !strings.EqualFold(sum, location.Checksum) { + return fmt.Errorf("absolute provider binary checksum mismatch: %s", location.Path) + } + return nil +} + +// Per-call operations keep phase-failure tests deterministic without mutable globals. +type providerUpdateCommitOps struct { + rename func(string, string) error + saveManifest func(string, *provider.ProviderConfig) error + updateConfig func(string, string, func(*config.Config) error) error + restoreManifest func(string, []byte, fs.FileMode) error + removeStage func(string) error +} + +func defaultProviderUpdateCommitOps() providerUpdateCommitOps { + return providerUpdateCommitOps{ + rename: os.Rename, + saveManifest: provider.SaveProviderConfig, + updateConfig: config.UpdateConfig, + restoreManifest: provider.WriteFileAtomic, + removeStage: removeProviderUpdateStage, + } +} + +func (p *preparedProviderReplacement) commit( + params ProviderParams, replacement *provider.ProviderConfig, ops providerUpdateCommitOps, +) error { + if p.replaceBinaries { + if p.hadBinaries { + if err := ops.rename(p.live, filepath.Join(p.stage, "backup")); err != nil { + return fmt.Errorf("backup old provider binaries: %w", err) + } + p.backupMoved = true + } + if err := ops.rename(filepath.Join(p.stage, "binaries"), p.live); err != nil { + return errors.Join( + fmt.Errorf("promote provider binaries: %w", err), + p.restoreBinaries(ops), + ) + } + p.installed = true + } + if err := ops.saveManifest(params.DevsyConfig.DefaultContext, replacement); err != nil { + return p.recoverCommit(err, ops) + } + if err := ops.updateConfig(params.DevsyConfig.DefaultContext, "", func(c *config.Config) error { + cleanupOldOptions(c, replacement) + clearInitialized(c, replacement.Name) + return nil + }); err != nil { + return p.recoverCommit(err, ops) + } + p.committed = true + return nil +} + +func (p *preparedProviderReplacement) recoverCommit( + original error, + ops providerUpdateCommitOps, +) error { + // SaveProviderConfig can return a sync error after rename; restore even then. + manifestErr := ops.restoreManifest(p.manifest, p.oldManifest, p.oldMode) + binariesErr := p.restoreBinaries(ops) + if manifestErr != nil || binariesErr != nil { + p.retain = true + } + return errors.Join(original, manifestErr, binariesErr) +} + +func (p *preparedProviderReplacement) restoreBinaries(ops providerUpdateCommitOps) error { + if p.installed { + // Retain failed new artifacts too; do not remove a live tree if this move fails. + if err := ops.rename(p.live, filepath.Join(p.stage, "failed-new")); err != nil { + p.retain = true + return fmt.Errorf("retain failed provider binaries: %w", err) + } + p.installed = false + } + if p.backupMoved { + if err := ops.rename(filepath.Join(p.stage, "backup"), p.live); err != nil { + p.retain = true + return fmt.Errorf("restore provider binary backup: %w", err) + } + p.backupMoved = false + } + return nil +} + +// Only a disposable owned staging tree reaches this helper; retained recovery +// artifacts and the live provider tree must never be made writable for cleanup. +func removeProviderUpdateStage(stage string) error { + root, err := os.OpenRoot(stage) + if errors.Is(err, fs.ErrNotExist) { + return nil + } + if err != nil { + return err + } + walkErr := fs.WalkDir( + root.FS(), + ".", + func(name string, entry fs.DirEntry, walkErr error) error { + if walkErr != nil { + return walkErr + } + if !entry.IsDir() { + return nil + } + info, err := entry.Info() + if err != nil { + return err + } + return root.Chmod(name, info.Mode()|0o700) + }, + ) + closeErr := root.Close() + return errors.Join(walkErr, closeErr, os.RemoveAll(stage)) +} diff --git a/pkg/workspace/provider_update_prepared_test.go b/pkg/workspace/provider_update_prepared_test.go new file mode 100644 index 0000000000..cf2aeeed7b --- /dev/null +++ b/pkg/workspace/provider_update_prepared_test.go @@ -0,0 +1,1516 @@ +package workspace + +import ( + "archive/tar" + "archive/zip" + "bytes" + "compress/gzip" + "context" + "encoding/json" + "errors" + "io" + "io/fs" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "runtime" + "strings" + "sync/atomic" + "testing" + "time" + + "github.com/devsy-org/devsy/pkg/config" + "github.com/devsy-org/devsy/pkg/provider" + "github.com/devsy-org/devsy/pkg/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +const updateLinkedSidecarPath = "current/lib/sidecar" + +// compatibilityArchiveServer serves bytes from an owned httptest server. The +// common fixture cleanup closes idle connections and joins the server. +func compatibilityArchiveServer(t *testing.T, archive []byte) *httptest.Server { + t.Helper() + return ownedProviderUpdateServer(t, func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write(archive) + }) +} + +func compatibilityTar( + t *testing.T, + gzipArchive bool, + entries []*tar.Header, + bodies map[string][]byte, +) []byte { + t.Helper() + var out bytes.Buffer + var writer io.Writer = &out + var gz *gzip.Writer + if gzipArchive { + gz = gzip.NewWriter(&out) + writer = gz + } + tarWriter := tar.NewWriter(writer) + for _, header := range entries { + require.NoError(t, tarWriter.WriteHeader(header)) + if body := bodies[header.Name]; len(body) != 0 { + _, err := tarWriter.Write(body) + require.NoError(t, err) + } + } + require.NoError(t, tarWriter.Close()) + if gz != nil { + require.NoError(t, gz.Close()) + } + return out.Bytes() +} + +func singleExecutableUpdateArchive(t *testing.T, content []byte) []byte { + t.Helper() + return compatibilityTar(t, false, []*tar.Header{ + {Name: updateRuntimeName, Typeflag: tar.TypeReg, Mode: 0o755, Size: int64(len(content))}, + }, map[string][]byte{updateRuntimeName: content}) +} + +func compatibilityZip(t *testing.T, entries []struct { + name string + mode os.FileMode + body []byte +}, +) []byte { + t.Helper() + var out bytes.Buffer + writer := zip.NewWriter(&out) + for _, item := range entries { + header := &zip.FileHeader{Name: item.name} + header.SetMode(item.mode) + member, err := writer.CreateHeader(header) + require.NoError(t, err) + _, err = member.Write(item.body) + require.NoError(t, err) + } + require.NoError(t, writer.Close()) + return out.Bytes() +} + +func assertSelectedExecutable(t *testing.T, path string, content []byte, checksum string) { + t.Helper() + actual, err := os.ReadFile( + path, + ) // #nosec G304 -- Selected executable belongs to the isolated update fixture. + require.NoError(t, err) + assert.Equal(t, content, actual) + info, err := os.Stat(path) + require.NoError(t, err) + if runtime.GOOS != updateWindowsOS { + assert.NotZero( + t, + info.Mode().Perm()&0o111, + "selected executable should retain executable permission", + ) + } + assert.Equal( + t, + checksum, + updateTestDigest(actual), + "selected archive member should match its declared checksum", + ) +} + +func TestProviderUpdateTarAndTgzCompatibility(t *testing.T) { + for _, gzipArchive := range []bool{false, true} { + t.Run(map[bool]string{false: "tar", true: "tgz"}[gzipArchive], func(t *testing.T) { + f := newPreservationFixture(t) + f.seedBinary("UNRELATED", []byte("keep unrelated installed binary")) + runtimeBytes := []byte("ordinary archive runtime bytes") + sidecarBytes := []byte("ordinary archive sibling sidecar") + archive := compatibilityTar(t, gzipArchive, []*tar.Header{ + { + Name: updateRuntimeName, + Typeflag: tar.TypeReg, + Mode: 0o755, + Size: int64(len(runtimeBytes)), + }, + { + Name: updateSidecarPath, + Typeflag: tar.TypeReg, + Mode: 0o750, + Size: int64(len(sidecarBytes)), + }, + }, map[string][]byte{updateRuntimeName: runtimeBytes, updateSidecarPath: sidecarBytes}) + extension := ".tar" + if gzipArchive { + extension = ".tgz" + } + server := compatibilityArchiveServer(t, archive) + binary := updateTestBinary(server.URL+"/provider"+extension, runtimeBytes) + binary.ArchivePath = updateRuntimeName + require.NoError( + t, + f.update(map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}}), + ) + assertSelectedExecutable( + t, + f.binaryPath(updateRuntimeKey), + runtimeBytes, + binary.Checksum, + ) + sidecar := filepath.Join(f.binaries, updateRuntimeName, "lib", "sidecar") + actual, err := os.ReadFile( + sidecar, + ) // #nosec G304 -- Sidecar belongs to the isolated update fixture. + require.NoError(t, err) + assert.Equal(t, sidecarBytes, actual) + sidecarInfo, err := os.Stat(sidecar) + require.NoError(t, err) + if runtime.GOOS != updateWindowsOS { + assert.Equal( + t, + os.FileMode(0o750), + sidecarInfo.Mode().Perm(), + "ancillary executable mode must survive", + ) + } + unrelated, err := os.ReadFile(f.binaryPath("UNRELATED")) + require.NoError(t, err) + assert.Equal(t, []byte("keep unrelated installed binary"), unrelated) + }) + } +} + +func TestProviderUpdateZipCompatibility(t *testing.T) { + for _, includeLink := range []bool{false, true} { + t.Run( + map[bool]string{false: "plain archive", true: "contained file symlink"}[includeLink], + func(t *testing.T) { + if includeLink { + requireProviderUpdateSymlinks(t) + } + assertProviderUpdateZipCompatibility(t, includeLink) + }, + ) + } +} + +func assertProviderUpdateZipCompatibility(t *testing.T, includeLink bool) { + t.Helper() + f := newPreservationFixture(t) + runtimeBytes := []byte("ordinary zip runtime bytes") + sidecarBytes := []byte("ordinary zip ancillary sibling") + entries := []struct { + name string + mode os.FileMode + body []byte + }{ + {name: updateRuntimeName, mode: os.ModePerm | 0o755, body: runtimeBytes}, + {name: updateSidecarPath, mode: os.ModePerm | 0o644, body: sidecarBytes}, + } + if includeLink { + entries = append(entries, struct { + name string + mode os.FileMode + body []byte + }{ + name: "runtime-link", mode: os.ModeSymlink | 0o777, body: []byte(updateRuntimeName), + }) + } + archive := compatibilityZip(t, entries) + server := compatibilityArchiveServer(t, archive) + binary := updateTestBinary(server.URL+"/provider.zip", runtimeBytes) + binary.ArchivePath = updateRuntimeName + require.NoError(t, f.update(map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}})) + assertSelectedExecutable(t, f.binaryPath(updateRuntimeKey), runtimeBytes, binary.Checksum) + if includeLink { + link := filepath.Join(f.binaries, updateRuntimeName, "runtime-link") + target, err := os.Readlink(link) + require.NoError(t, err) + assert.Equal(t, updateRuntimeName, target) + linked, err := os.ReadFile( + link, + ) // #nosec G304 -- Contained link belongs to the isolated update fixture. + require.NoError(t, err) + assert.Equal(t, runtimeBytes, linked) + } + sidecar, err := os.ReadFile(filepath.Join(f.binaries, updateRuntimeName, "lib", "sidecar")) + require.NoError(t, err) + assert.Equal(t, sidecarBytes, sidecar) +} + +func TestProviderUpdateTarContainedDirectoryLinkThenRegularMember(t *testing.T) { + requireProviderUpdateSymlinks(t) + f := newPreservationFixture(t) + runtimeBytes := []byte("runtime beneath contained directory link") + sidecarBytes := []byte("sibling reached through contained directory link") + archive := compatibilityTar(t, false, []*tar.Header{ + {Name: "releases", Typeflag: tar.TypeDir, Mode: 0o755}, + {Name: "releases/v1", Typeflag: tar.TypeDir, Mode: 0o755}, + {Name: updateCurrentLink, Typeflag: tar.TypeSymlink, Linkname: "releases/v1", Mode: 0o777}, + { + Name: "current/runtime", + Typeflag: tar.TypeReg, + Mode: 0o755, + Size: int64(len(runtimeBytes)), + }, + { + Name: updateLinkedSidecarPath, + Typeflag: tar.TypeReg, + Mode: 0o644, + Size: int64(len(sidecarBytes)), + }, + }, map[string][]byte{"current/runtime": runtimeBytes, updateLinkedSidecarPath: sidecarBytes}) + server := compatibilityArchiveServer(t, archive) + binary := updateTestBinary(server.URL+"/provider.tar", runtimeBytes) + binary.ArchivePath = filepath.Join(updateCurrentLink, updateRuntimeName) + require.NoError(t, f.update(map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}})) + assertSelectedExecutable( + t, + filepath.Join(f.binaries, updateRuntimeName, updateCurrentLink, updateRuntimeName), + runtimeBytes, + binary.Checksum, + ) + link := filepath.Join(f.binaries, updateRuntimeName, updateCurrentLink) + target, err := os.Readlink(link) + require.NoError(t, err) + assert.Equal(t, filepath.Join("releases", "v1"), filepath.FromSlash(target)) + actual, err := os.ReadFile( + filepath.Join(f.binaries, updateRuntimeName, "releases", "v1", "lib", "sidecar"), + ) + require.NoError(t, err) + assert.Equal(t, sidecarBytes, actual) +} + +func TestProviderUpdateZipContainedDirectoryLinkThenRegularMember(t *testing.T) { + requireProviderUpdateSymlinks(t) + f := newPreservationFixture(t) + runtimeBytes := []byte("runtime beneath contained zip directory link") + sidecarBytes := []byte("sibling reached through contained zip directory link") + archive := compatibilityZip(t, []struct { + name string + mode os.FileMode + body []byte + }{ + {name: "releases/", mode: os.ModeDir | 0o755}, + {name: "releases/v1/", mode: os.ModeDir | 0o755}, + {name: updateCurrentLink, mode: os.ModeSymlink | 0o777, body: []byte("releases/v1")}, + {name: "current/" + updateRuntimeName, mode: os.ModePerm | 0o755, body: runtimeBytes}, + {name: updateLinkedSidecarPath, mode: os.ModePerm | 0o644, body: sidecarBytes}, + }) + server := compatibilityArchiveServer(t, archive) + binary := updateTestBinary(server.URL+"/provider.zip", runtimeBytes) + binary.ArchivePath = filepath.Join(updateCurrentLink, updateRuntimeName) + require.NoError(t, f.update(map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}})) + assertSelectedExecutable( + t, + filepath.Join(f.binaries, updateRuntimeName, updateCurrentLink, updateRuntimeName), + runtimeBytes, + binary.Checksum, + ) + link := filepath.Join(f.binaries, updateRuntimeName, updateCurrentLink) + target, err := os.Readlink(link) + require.NoError(t, err) + assert.Equal(t, filepath.Join("releases", "v1"), filepath.FromSlash(target)) + actual, err := os.ReadFile( + filepath.Join(f.binaries, updateRuntimeName, "releases", "v1", "lib", "sidecar"), + ) + require.NoError(t, err) + assert.Equal(t, sidecarBytes, actual) +} + +func TestProviderUpdateTarHardlinkCompatibility(t *testing.T) { + f := newPreservationFixture(t) + runtimeBytes := []byte("hard-linked executable bytes") + archive := compatibilityTar(t, true, []*tar.Header{ + { + Name: updateRuntimeName, + Typeflag: tar.TypeReg, + Linkname: "", + Mode: 0o755, + Size: int64(len(runtimeBytes)), + }, + {Name: "runtime-copy", Typeflag: tar.TypeLink, Linkname: updateRuntimeName, Mode: 0o755}, + }, map[string][]byte{updateRuntimeName: runtimeBytes}) + server := compatibilityArchiveServer(t, archive) + binary := updateTestBinary(server.URL+"/provider.tgz", runtimeBytes) + binary.ArchivePath = updateRuntimeName + require.NoError(t, f.update(map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}})) + assertSelectedExecutable(t, f.binaryPath(updateRuntimeKey), runtimeBytes, binary.Checksum) + copyPath := filepath.Join(f.binaries, updateRuntimeName, "runtime-copy") + // #nosec G304 -- Hardlink bytes are read only from the isolated archive fixture. + copyBytes, err := os.ReadFile(copyPath) + require.NoError(t, err) + assert.Equal(t, runtimeBytes, copyBytes) + selectedInfo, err := os.Stat(f.binaryPath(updateRuntimeKey)) + require.NoError(t, err) + copyInfo, err := os.Stat(copyPath) + require.NoError(t, err) + assert.True( + t, + os.SameFile(selectedInfo, copyInfo), + "tar hardlink should remain a hardlink after update preparation", + ) +} + +func TestProviderUpdateContainedSeedLinkInstalledOnlyReuse(t *testing.T) { + requireProviderUpdateSymlinks(t) + f := newPreservationFixture(t) + content := []byte("installed executable reachable through a contained seed link") + versionDir := filepath.Join(f.binaries, updateRuntimeName, "releases", "v1") + // #nosec G301 -- The owned fixture reproduces a searchable installed binary directory. + require.NoError(t, os.MkdirAll(versionDir, 0o755)) + versionBinary := filepath.Join(versionDir, updateRuntimeName) + // #nosec G306 -- This test checks reuse of an installed executable with its executable mode. + require.NoError(t, os.WriteFile(versionBinary, content, 0o755)) + link := filepath.Join(f.binaries, updateRuntimeName, updateCurrentLink) + require.NoError(t, os.Symlink(filepath.Join("releases", "v1"), link)) + binaryPath := filepath.Join(updateCurrentLink, updateRuntimeName) + binary := updateTestBinary("https://offline.invalid/provider.tgz", content) + binary.ArchivePath = binaryPath + require.NoError(t, f.update(map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}})) + assertSelectedExecutable( + t, + filepath.Join(f.binaries, updateRuntimeName, binaryPath), + content, + binary.Checksum, + ) + target, err := os.Readlink(link) + require.NoError(t, err) + assert.Equal(t, filepath.Join("releases", "v1"), filepath.FromSlash(target)) + // #nosec G304 -- Installed executable belongs to the isolated update fixture. + actual, err := os.ReadFile(versionBinary) + require.NoError(t, err) + assert.Equal(t, content, actual) +} + +func TestProviderUpdateSelectedArchiveChecksumMismatchDoesNotPublish(t *testing.T) { + f := newPreservationFixture(t) + f.seedBinary(updateRuntimeKey, []byte("previous selected executable")) + selected := []byte("new archive selected executable") + archive := compatibilityTar(t, true, []*tar.Header{ + {Name: updateRuntimeName, Typeflag: tar.TypeReg, Mode: 0o755, Size: int64(len(selected))}, + {Name: updateSidecarPath, Typeflag: tar.TypeReg, Mode: 0o644, Size: int64(len(selected))}, + }, map[string][]byte{updateRuntimeName: selected, updateSidecarPath: selected}) + server := compatibilityArchiveServer(t, archive) + binary := updateTestBinary(server.URL+"/provider.tgz", []byte("wrong selected checksum bytes")) + binary.ArchivePath = updateRuntimeName + err := f.update(map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}}) + require.ErrorContains(t, err, "checksum") + f.assertFailedPreservation() +} + +func preparedUpdateParams( + t *testing.T, + f *preservationFixture, + binaries map[string][]*provider.ProviderBinary, +) (ProviderParams, *provider.ProviderConfig) { + t.Helper() + replacement := &provider.ProviderConfig{ + Name: f.name, + Version: updateReplacementVersion, + Exec: provider.ProviderCommands{Command: []string{updateNoopCommand}}, + Options: map[string]*types.Option{ + updateKeptOption: {}, + }, + Binaries: binaries, + Source: provider.ProviderSource{Raw: "candidate-new"}, + } + raw, err := json.Marshal(replacement) + require.NoError(t, err) + return ProviderParams{ + DevsyConfig: f.config, + ProviderName: f.name, + Raw: raw, + Source: &replacement.Source, + }, replacement +} + +func assertNoUpdateStaging(t *testing.T, f *preservationFixture) { + t.Helper() + entries, err := os.ReadDir(f.dir) + require.NoError(t, err) + for _, entry := range entries { + assert.False(t, strings.HasPrefix(entry.Name(), ".update-staging-"), entry.Name()) + } +} + +func TestProviderUpdateChecksumFailure(t *testing.T) { + f := newPreservationFixture(t) + f.seedBinary(updateRuntimeKey, []byte("old")) + server := ownedProviderUpdateServer( + t, + func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte("bad checksum")) }, + ) + err := f.update( + map[string][]*provider.ProviderBinary{ + updateRuntimeKey: {updateTestBinary(server.URL+"/runtime", []byte("expected"))}, + }, + ) + require.Error(t, err) + f.assertFailedPreservation() + assertNoUpdateStaging(t, f) +} + +func TestProviderUpdateCancellation(t *testing.T) { + f := newPreservationFixture(t) + f.seedBinary(updateRuntimeKey, []byte("old")) + entered, handlerDone := make(chan struct{}), make(chan struct{}) + server := ownedProviderUpdateServer(t, func(_ http.ResponseWriter, r *http.Request) { + defer close(handlerDone) + close(entered) + <-r.Context().Done() + }) + params, _ := preparedUpdateParams( + t, + f, + map[string][]*provider.ProviderBinary{ + updateRuntimeKey: {updateTestBinary(server.URL+"/runtime", []byte("new"))}, + }, + ) + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + done := make(chan error, 1) + joined := make(chan struct{}) + go func() { + defer close(joined) + _, err := updateProvider(ctx, params) + done <- err + }() + cleanupProviderUpdateCaller(t, cancel, joined) + select { + case <-entered: + case <-time.After(3 * time.Second): + t.Fatal("request did not start") + } + cancel() + select { + case err := <-done: + require.ErrorIs(t, err, context.Canceled) + case <-time.After(3 * time.Second): + t.Fatal("update did not join") + } + select { + case <-handlerDone: + case <-time.After(3 * time.Second): + t.Fatal("handler did not join") + } + f.assertFailedPreservation() + assertNoUpdateStaging(t, f) +} + +func cleanupProviderUpdateCaller(t *testing.T, cancel context.CancelFunc, joined <-chan struct{}) { + t.Helper() + t.Cleanup(func() { + cancel() + select { + case <-joined: + case <-time.After(3 * time.Second): + t.Error("update caller did not join during cleanup") + } + }) +} + +func TestProviderUpdateRejectsExistingSymlink(t *testing.T) { + requireProviderUpdateSymlinks(t) + f := newPreservationFixture(t) + f.seedBinary(updateRuntimeKey, []byte("old")) + external := filepath.Join(t.TempDir(), "outside") + require.NoError(t, os.WriteFile(external, []byte("untouched"), 0o600)) + require.NoError(t, os.Symlink(external, filepath.Join(f.binaries, "outside-link"))) + err := f.update( + map[string][]*provider.ProviderBinary{ + updateRuntimeKey: {updateTestBinary("absent-old-source", []byte("old"))}, + }, + ) + require.ErrorContains(t, err, "unsafe provider update symlink") + // #nosec G304 -- Outside sentinel is a separate test-owned temporary file. + data, err := os.ReadFile(external) + require.NoError(t, err) + assert.Equal(t, "untouched", string(data)) + f.assertFailedPreservation() + assertNoUpdateStaging(t, f) +} + +func TestProviderUpdateValidAbsoluteAndNoBinary(t *testing.T) { + for _, absolute := range []bool{false, true} { + t.Run( + map[bool]string{false: "no binaries", true: "absolute reference"}[absolute], + func(t *testing.T) { + f := newPreservationFixture(t) + f.seedBinary("OLD", []byte("preserved unused bytes")) + binaries := map[string][]*provider.ProviderBinary{} + if absolute { + path := filepath.Join(t.TempDir(), "external") + f.put(path, []byte("external")) + binaries[updateRuntimeKey] = []*provider.ProviderBinary{ + updateTestBinary(path, []byte("external")), + } + } + require.NoError(t, f.update(binaries)) + for path, before := range f.before { + if filepath.Base(path) == provider.ProviderConfigFile { + continue + } + // #nosec G304 -- Snapshot path comes only from the test-owned fixture. + after, err := os.ReadFile(path) + require.NoError(t, err) + assert.Equal(t, before, after) + } + state := f.assertAssociation() + assert.False(t, state.Initialized) + assert.Equal( + t, + map[string]config.OptionValue{updateKeptOption: f.options[updateKeptOption]}, + state.Options, + ) + assertNoUpdateStaging(t, f) + }, + ) + } +} + +func TestProviderUpdateArchiveSuccessKeepsSiblings(t *testing.T) { + f := newPreservationFixture(t) + f.seedBinary(updateRuntimeKey, []byte("old")) + f.seedBinary("UNRELATED", []byte(updateUnrelatedContent)) + var archive bytes.Buffer + gz := gzip.NewWriter(&archive) + tarWriter := tar.NewWriter(gz) + for name, data := range map[string]string{ + updateRuntimeName: updateExecutableContent, + updateSidecarPath: "required sidecar", + } { + require.NoError( + t, + tarWriter.WriteHeader(&tar.Header{Name: name, Mode: 0o755, Size: int64(len(data))}), + ) + _, err := tarWriter.Write([]byte(data)) + require.NoError(t, err) + } + require.NoError(t, tarWriter.Close()) + require.NoError(t, gz.Close()) + server := ownedProviderUpdateServer( + t, + func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write(archive.Bytes()) }, + ) + binary := updateTestBinary(server.URL+"/runtime.tar.gz", []byte(updateExecutableContent)) + binary.ArchivePath = updateRuntimeName + require.NoError(t, f.update(map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}})) + for path, want := range map[string]string{ + f.binaryPath(updateRuntimeKey): updateExecutableContent, + filepath.Join(f.binaries, updateRuntimeName, "lib", "sidecar"): "required sidecar", + f.binaryPath("UNRELATED"): updateUnrelatedContent, + filepath.Join(f.dir, "daemon", "owned-state"): "untouched daemon state", + } { + // #nosec G304 -- Snapshot path comes only from the test-owned fixture. + got, err := os.ReadFile(path) + require.NoError(t, err) + assert.Equal(t, want, string(got)) + } + assertNoUpdateStaging(t, f) +} + +func TestProviderUpdateSynchronousCommitRestoration(t *testing.T) { + for _, phase := range []string{ + updateBackupPhase, updatePromotionPhase, "manifest after save", "config before save", updateRestoreFailure, + } { + t.Run(phase, func(t *testing.T) { + f := newPreservationFixture(t) + f.seedBinary(updateRuntimeKey, []byte("old")) + params, replacement := preparedUpdateParams( + t, + f, + map[string][]*provider.ProviderBinary{ + updateRuntimeKey: { + updateTestBinary("unavailable-original-source/runtime", []byte("old")), + }, + }, + ) + prepared, err := prepareProviderReplacement(context.Background(), params, replacement) + require.NoError(t, err) + defer func() { require.NoError(t, os.RemoveAll(prepared.stage)) }() + sentinel := errors.New("owned injected " + phase) + ops := updateCommitFailureOps(phase, sentinel) + err = prepared.commit(params, replacement, ops) + require.ErrorIs(t, err, sentinel) + if phase == updateRestoreFailure { + require.True(t, prepared.retain) + backup, readErr := os.ReadFile(filepath.Join(prepared.stage, "old-provider.json")) + require.NoError(t, readErr) + assert.Equal(t, f.before[prepared.manifest], backup) + require.ErrorContains(t, err, "owned restore failure") + } else { + require.False(t, prepared.retain) + f.assertFailedPreservation() + } + }) + } +} + +func TestProviderUpdateStageCleanupFailure(t *testing.T) { + for _, tc := range []struct { + name string + committed bool + }{{name: "after commit", committed: true}, {name: "before commit"}} { + t.Run(tc.name, func(t *testing.T) { + f := newPreservationFixture(t) + params, replacement, updated := cleanupFailureUpdateFixture(t, f) + + cleanupErr := errors.New("owned staging cleanup failure") + var stage string + ops := defaultProviderUpdateCommitOps() + ops.removeStage = func(path string) error { + stage = path + return cleanupErr + } + t.Cleanup(func() { + if stage != "" { + require.NoError(t, os.RemoveAll(stage)) + } + }) + if !tc.committed { + commitErr := errors.New("owned manifest commit failure") + ops.saveManifest = func(string, *provider.ProviderConfig) error { return commitErr } + // The failed commit is rolled back; its original cause and the + // staging cleanup failure must both remain visible. + err := replaceProviderPreparedWithOps( + context.Background(), + params, + replacement, + ops, + ) + require.ErrorIs(t, err, commitErr) + require.ErrorIs(t, err, cleanupErr) + require.DirExists(t, stage) + f.assertFailedPreservation() + return + } + + require.NoError( + t, + replaceProviderPreparedWithOps(context.Background(), params, replacement, ops), + ) + require.DirExists( + t, + stage, + "the simulated cleanup failure leaves only disposable staging data", + ) + assertCommittedCleanupUpdate(t, f, updated) + }) + } +} + +func cleanupFailureUpdateFixture( + t *testing.T, + f *preservationFixture, +) (ProviderParams, *provider.ProviderConfig, []byte) { + t.Helper() + f.seedBinary(updateRuntimeKey, []byte("old provider runtime")) + updated := []byte("updated provider runtime") + server := compatibilityArchiveServer( + t, + singleExecutableUpdateArchive(t, updated), + ) + binary := updateTestBinary(server.URL+"/runtime.tar", updated) + binary.ArchivePath = updateRuntimeName + params, replacement := preparedUpdateParams(t, f, map[string][]*provider.ProviderBinary{ + updateRuntimeKey: {binary}, + }) + return params, replacement, updated +} + +func assertCommittedCleanupUpdate(t *testing.T, f *preservationFixture, updated []byte) { + t.Helper() + assertSelectedExecutable(t, f.binaryPath(updateRuntimeKey), updated, updateTestDigest(updated)) + stored, err := provider.LoadProviderConfig(testDefaultContext, f.name) + require.NoError(t, err) + assert.Equal(t, updateReplacementVersion, stored.Version) + state := f.assertAssociation() + assert.False(t, state.Initialized) + assert.True(t, state.InitAttempted) + assert.Equal( + t, + map[string]config.OptionValue{updateKeptOption: f.options[updateKeptOption]}, + state.Options, + ) +} + +func updateCommitFailureOps(phase string, sentinel error) providerUpdateCommitOps { + ops := defaultProviderUpdateCommitOps() + switch phase { + case updateBackupPhase, updatePromotionPhase: + failAt := map[string]int{updateBackupPhase: 1, updatePromotionPhase: 2}[phase] + originalRename := ops.rename + calls := 0 + ops.rename = func(from, to string) error { + calls++ + if calls == failAt { + return sentinel + } + return originalRename(from, to) + } + case "manifest after save": + ops.saveManifest = func(context string, p *provider.ProviderConfig) error { + if err := provider.SaveProviderConfig(context, p); err != nil { + return err + } + return sentinel + } + case "config before save", updateRestoreFailure: + ops.updateConfig = func(string, string, func(*config.Config) error) error { return sentinel } + } + if phase == updateRestoreFailure { + ops.restoreManifest = func(string, []byte, fs.FileMode) error { return errors.New("owned restore failure") } + } + return ops +} + +func TestProviderUpdateCachedReplacement(t *testing.T) { + f := newPreservationFixture(t) + f.seedBinary(updateRuntimeKey, []byte("old")) + content := []byte("new cached replacement " + f.dir) + server := ownedProviderUpdateServer( + t, + func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write(content) }, + ) + binaries := map[string][]*provider.ProviderBinary{ + updateRuntimeKey: {updateTestBinary(server.URL+"/runtime", content)}, + } + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + _, err := provider.DownloadBinaries(ctx, binaries, t.TempDir()) + require.NoError(t, err) + server.Close() + require.NoError(t, f.update(binaries)) + actual, err := os.ReadFile(f.binaryPath(updateRuntimeKey)) + require.NoError(t, err) + assert.Equal(t, content, actual) + assertNoUpdateStaging(t, f) +} + +func TestProviderUpdateRejectsEscapingBinaryTarget(t *testing.T) { + f := newPreservationFixture(t) + f.seedBinary(updateRuntimeKey, []byte("old")) + declaration := updateTestBinary("https://owned.invalid/runtime", []byte("new")) + declaration.Name = "../../../../daemon/owned-state" + err := f.update(map[string][]*provider.ProviderBinary{updateRuntimeKey: {declaration}}) + require.ErrorContains(t, err, "unsafe provider update binary target") + f.assertFailedPreservation() + assertNoUpdateStaging(t, f) +} + +func TestProviderUpdateCachedArchiveReplacement(t *testing.T) { + f := newPreservationFixture(t) + f.seedBinary(updateRuntimeKey, []byte("old selected executable")) + sidecar := filepath.Join(f.binaries, updateRuntimeName, "lib", "seeded-sidecar") + f.put(sidecar, []byte("existing sidecar retained by legacy executable cache")) + content := []byte("cached archive replacement " + f.dir) + archive := compatibilityTar(t, true, []*tar.Header{ + {Name: updateRuntimeName, Typeflag: tar.TypeReg, Mode: 0o755, Size: int64(len(content))}, + }, map[string][]byte{updateRuntimeName: content}) + server := compatibilityArchiveServer(t, archive) + binary := updateTestBinary(server.URL+"/provider.tgz", content) + binary.ArchivePath = updateRuntimeName + binaries := map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}} + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + _, err := provider.DownloadBinaries(ctx, binaries, t.TempDir()) + require.NoError(t, err) + server.Close() + require.NoError( + t, + f.update(binaries), + "closed archive server requires existing executable cache reuse", + ) + assertSelectedExecutable(t, f.binaryPath(updateRuntimeKey), content, binary.Checksum) + actual, err := os.ReadFile( + sidecar, + ) // #nosec G304 -- Sidecar belongs to the isolated update fixture. + require.NoError(t, err) + assert.Equal(t, f.before[sidecar], actual) + assertNoUpdateStaging(t, f) +} + +func TestProviderUpdateContainedSeedDirectoryLinkChain(t *testing.T) { + requireProviderUpdateSymlinks(t) + f := newPreservationFixture(t) + content := []byte("installed executable reached through a directory link chain") + base := filepath.Join(f.binaries, updateRuntimeName) + f.put(filepath.Join(base, "releases", "v1", updateRuntimeName), content) + // Create source links in dependency order; staging must not depend on map iteration order. + targets := map[string]string{ + updateCurrentLink: filepath.Join("releases", "v1"), + updateMiddleLink: updateCurrentLink, + updateEntryLink: updateMiddleLink, + } + for _, name := range []string{updateCurrentLink, updateMiddleLink, updateEntryLink} { + require.NoError(t, os.Symlink(targets[name], filepath.Join(base, name))) + } + binary := updateTestBinary("https://offline.invalid/linked-provider.tgz", content) + binary.ArchivePath = filepath.Join(updateEntryLink, updateRuntimeName) + require.NoError(t, f.update(map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}})) + for name, want := range targets { + link := filepath.Join(base, name) + target, err := os.Readlink(link) + require.NoError(t, err) + assert.Equal(t, want, target) + info, err := os.Stat(link) + require.NoError(t, err) + assert.True(t, info.IsDir(), "seed link %s must retain directory traversal", name) + assertSelectedExecutable( + t, + filepath.Join(link, updateRuntimeName), + content, + binary.Checksum, + ) + } + assertNoUpdateStaging(t, f) +} + +func TestProviderUpdateFailedBinaryRestorationRetainsBackup(t *testing.T) { + f := newPreservationFixture(t) + content := []byte("old executable retained for recovery") + f.seedBinary(updateRuntimeKey, content) + binaries := map[string][]*provider.ProviderBinary{ + updateRuntimeKey: {updateTestBinary("unavailable-original-source/runtime", content)}, + } + params, replacement := preparedUpdateParams(t, f, binaries) + prepared, err := prepareProviderReplacement(context.Background(), params, replacement) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, os.RemoveAll(prepared.stage)) }) + commitErr := errors.New("owned config commit failure") + restoreErr := errors.New("owned binary backup restoration failure") + ops := defaultProviderUpdateCommitOps() + ops.updateConfig = func(string, string, func(*config.Config) error) error { return commitErr } + rename := ops.rename + backup := filepath.Join(prepared.stage, updateBackupPhase) + ops.rename = func(from, to string) error { + if from == backup { + return restoreErr + } + return rename(from, to) + } + err = prepared.commit(params, replacement, ops) + require.ErrorIs(t, err, commitErr) + require.ErrorIs(t, err, restoreErr) + require.True(t, prepared.retain, "failed restoration must retain recoverable artifacts") + assertSelectedExecutable( + t, + filepath.Join(backup, updateRuntimeName, updateRuntimeName), + content, + updateTestDigest(content), + ) + // #nosec G304 -- Manifest belongs to the isolated prepared-update fixture. + manifest, err := os.ReadFile(prepared.manifest) + require.NoError(t, err) + assert.Equal(t, f.before[prepared.manifest], manifest, "manifest restoration still runs") + state := f.assertAssociation() + assert.True(t, state.Initialized) + assert.True(t, state.InitAttempted) + assert.Equal(t, f.options, state.Options) + require.DirExists(t, filepath.Join(prepared.stage, "failed-new")) + require.NoDirExists( + t, + prepared.live, + "failed rename must not claim the old binaries were restored", + ) +} + +func TestProviderUpdatePAXGlobalMetadata(t *testing.T) { + f := newPreservationFixture(t) + f.seedBinary(updateRuntimeKey, []byte("old PAX runtime")) + content := []byte("new executable after global metadata") + const metadata = "global-metadata" + archive := compatibilityTar(t, false, []*tar.Header{ + { + Name: metadata, + Typeflag: tar.TypeXGlobalHeader, + PAXRecords: map[string]string{"comment": "owned global metadata"}, + }, + {Name: updateRuntimeName, Typeflag: tar.TypeReg, Mode: 0o755, Size: int64(len(content))}, + }, map[string][]byte{updateRuntimeName: content}) + server := compatibilityArchiveServer(t, archive) + binary := updateTestBinary(server.URL+"/global.tar", content) + binary.ArchivePath = updateRuntimeName + err := f.update(map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}}) + t.Logf("PAX global-header update result: %v", err) + require.NoError(t, err) + assertSelectedExecutable(t, f.binaryPath(updateRuntimeKey), content, binary.Checksum) + require.NoFileExists(t, filepath.Join(f.binaries, updateRuntimeName, metadata)) + require.NoDirExists(t, filepath.Join(f.binaries, updateRuntimeName, metadata)) + assertNoUpdateStaging(t, f) +} + +const updateArchiveLink = "runtime-link" + +func linkedReplacementArchive(t *testing.T, linkType byte, content []byte, extra string) []byte { + t.Helper() + entries := []*tar.Header{ + {Name: updateRuntimeName, Typeflag: tar.TypeReg, Mode: 0o755, Size: int64(len(content))}, + {Name: updateArchiveLink, Typeflag: linkType, Linkname: updateRuntimeName, Mode: 0o777}, + {Name: updateSidecarPath, Typeflag: tar.TypeReg, Mode: 0o644, Size: int64(len(content))}, + } + bodies := map[string][]byte{updateRuntimeName: content, updateSidecarPath: content} + if extra != "" { + entries = append( + entries, + &tar.Header{Name: extra, Typeflag: tar.TypeReg, Mode: 0o644, Size: int64(len(content))}, + ) + bodies[extra] = content + } + return compatibilityTar(t, false, entries, bodies) +} + +func assertLinkedReplacement(t *testing.T, f *preservationFixture, linkType byte, content []byte) { + t.Helper() + selected := f.binaryPath(updateRuntimeKey) + assertSelectedExecutable(t, selected, content, updateTestDigest(content)) + link := filepath.Join(f.binaries, updateRuntimeName, updateArchiveLink) + // #nosec G304 -- Link is part of this test-owned provider archive. + linked, err := os.ReadFile(link) + require.NoError(t, err) + assert.Equal(t, content, linked) + if linkType == tar.TypeSymlink { + target, err := os.Readlink(link) + require.NoError(t, err) + assert.Equal(t, updateRuntimeName, target) + } else { + selectedInfo, err := os.Stat(selected) + require.NoError(t, err) + linkInfo, err := os.Stat(link) + require.NoError(t, err) + assert.True(t, os.SameFile(selectedInfo, linkInfo)) + } + sidecar, err := os.ReadFile(filepath.Join(f.binaries, updateRuntimeName, "lib", "sidecar")) + require.NoError(t, err) + assert.Equal(t, content, sidecar) + for _, path := range []string{ + f.binaryPath("UNRELATED"), + filepath.Join(f.dir, "owned-sentinel"), + filepath.Join(f.dir, "daemon", "owned-state"), + } { + // #nosec G304 -- These paths were populated and snapshotted by this isolated fixture. + actual, err := os.ReadFile(path) + require.NoError(t, err) + assert.Equal(t, f.before[path], actual) + } + assert.False(t, f.assertAssociation().Initialized) + assertNoUpdateStaging(t, f) +} + +func TestProviderUpdateConsecutiveLinkedArchives(t *testing.T) { + for _, linkType := range []byte{tar.TypeSymlink, tar.TypeLink} { + t.Run( + map[byte]string{tar.TypeSymlink: "symlink", tar.TypeLink: "hardlink"}[linkType], + func(t *testing.T) { + if linkType == tar.TypeSymlink { + requireProviderUpdateSymlinks(t) + } + f := newPreservationFixture(t) + f.seedBinary("UNRELATED", []byte("unrelated installed bytes")) + first, second := []byte("first linked release"), []byte("second linked release") + const stale = "obsolete-release-member" + archives := [][]byte{ + linkedReplacementArchive(t, linkType, first, stale), + linkedReplacementArchive(t, linkType, second, ""), + } + var requests atomic.Int64 + server := ownedProviderUpdateServer( + t, + func(w http.ResponseWriter, _ *http.Request) { + index := requests.Add(1) - 1 + if index >= int64(len(archives)) { + http.Error(w, "unexpected additional request", http.StatusBadRequest) + return + } + _, _ = w.Write(archives[index]) + }, + ) + for _, content := range [][]byte{first, second} { + binary := updateTestBinary(server.URL+"/linked.tar", content) + binary.ArchivePath = updateRuntimeName + err := f.update( + map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}}, + ) + t.Logf("consecutive archive request=%d result=%v", requests.Load(), err) + require.NoError(t, err) + assertLinkedReplacement(t, f, linkType, content) + } + require.EqualValues( + t, + 2, + requests.Load(), + "changed checksum must fetch a new archive", + ) + require.NoFileExists(t, filepath.Join(f.binaries, updateRuntimeName, stale)) + }, + ) + } +} + +func truncatedLinkedArchive(t *testing.T, linkType byte, content []byte) []byte { + t.Helper() + const partial = "attempt-only-member" + var data bytes.Buffer + writer := tar.NewWriter(&data) + for _, header := range []*tar.Header{ + {Name: updateRuntimeName, Typeflag: tar.TypeReg, Mode: 0o755, Size: int64(len(content))}, + {Name: updateArchiveLink, Typeflag: linkType, Linkname: updateRuntimeName, Mode: 0o777}, + {Name: partial, Typeflag: tar.TypeReg, Mode: 0o644, Size: int64(len(content))}, + {Name: "incomplete", Typeflag: tar.TypeReg, Mode: 0o644, Size: 128}, + } { + require.NoError(t, writer.WriteHeader(header)) + if header.Name == "incomplete" { + _, err := writer.Write([]byte("short")) + require.NoError(t, err) + break + } + if header.Typeflag == tar.TypeReg { + _, err := writer.Write(content) + require.NoError(t, err) + } + } + // Deliberately truncated member follows a completed link; this is a retriable tar read error. + require.Error(t, writer.Close()) + return data.Bytes() +} + +func TestProviderUpdateRetriesLinkedArchiveFromFreshFamily(t *testing.T) { + for _, linkType := range []byte{tar.TypeSymlink, tar.TypeLink} { + t.Run( + map[byte]string{tar.TypeSymlink: "symlink", tar.TypeLink: "hardlink"}[linkType], + func(t *testing.T) { + if linkType == tar.TypeSymlink { + requireProviderUpdateSymlinks(t) + } + f := newPreservationFixture(t) + f.seedBinary(updateRuntimeKey, []byte("old live runtime")) + f.seedBinary("UNRELATED", []byte("unrelated installed bytes")) + content := []byte("complete archive after retry") + first := truncatedLinkedArchive(t, linkType, content) + second := linkedReplacementArchive(t, linkType, content, "") + var requests atomic.Int64 + server := ownedProviderUpdateServer( + t, + func(w http.ResponseWriter, _ *http.Request) { + if requests.Add(1) == 1 { + _, _ = w.Write(first) + return + } + _, _ = w.Write(second) + }, + ) + binary := updateTestBinary(server.URL+"/retry.tar", content) + binary.ArchivePath = updateRuntimeName + err := f.update(map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}}) + t.Logf("linked archive retry requests=%d result=%v", requests.Load(), err) + require.EqualValues(t, 2, requests.Load()) + require.NoError(t, err) + assertLinkedReplacement(t, f, linkType, content) + require.NoFileExists( + t, + filepath.Join(f.binaries, updateRuntimeName, "attempt-only-member"), + ) + require.NoFileExists(t, filepath.Join(f.binaries, updateRuntimeName, "incomplete")) + }, + ) + } +} + +func TestProviderUpdateFamilyAliasPreservesUnrelatedBinary(t *testing.T) { + for _, cache := range []bool{false, true} { + t.Run( + map[bool]string{false: "fresh archive", true: "offline executable cache"}[cache], + func(t *testing.T) { + requireProviderUpdateSymlinks(t) + f := newPreservationFixture(t) + old := []byte("unrelated executable must not change") + f.seedBinary("UNRELATED", old) + alias := filepath.Join(f.binaries, updateRuntimeName) + require.NoError(t, os.Symlink("unrelated", alias)) + content := []byte("replacement through alias " + f.dir) + archive := singleExecutableUpdateArchive(t, content) + var requests atomic.Int64 + server := ownedProviderUpdateServer( + t, + func(w http.ResponseWriter, _ *http.Request) { + requests.Add(1) + _, _ = w.Write(archive) + }, + ) + binary := updateTestBinary(server.URL+"/family-alias.tar", content) + binary.ArchivePath = updateRuntimeName + binaries := map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}} + if cache { + warmProviderUpdateCache(t, binaries, server) + } + require.NoError(t, f.update(binaries)) + require.EqualValues( + t, + 1, + requests.Load(), + "offline cache case must not fetch again", + ) + assertSelectedExecutable( + t, + f.binaryPath(updateRuntimeKey), + content, + binary.Checksum, + ) + actual, err := os.ReadFile(f.binaryPath("UNRELATED")) + t.Logf( + "unrelated binary after successful replacement: read error=%v bytes=%q", + err, + actual, + ) + require.NoError( + t, + err, + "updating an alias family must preserve the original target family", + ) + assert.Equal( + t, + old, + actual, + "cache preparation must not overwrite another family's executable", + ) + assertNoUpdateStaging(t, f) + }, + ) + } +} + +func warmProviderUpdateCache( + t *testing.T, + binaries map[string][]*provider.ProviderBinary, + server *httptest.Server, +) { + t.Helper() + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + _, err := provider.DownloadBinaries(ctx, binaries, t.TempDir()) + require.NoError(t, err) + server.Close() +} + +func TestProviderUpdateFamilyAliasMatchingChecksumReuse(t *testing.T) { + requireProviderUpdateSymlinks(t) + f := newPreservationFixture(t) + content := []byte("valid installed executable behind a contained family alias") + f.seedBinary("UNRELATED", content) + alias := filepath.Join(f.binaries, updateRuntimeName) + require.NoError(t, os.Symlink("unrelated", alias)) + binary := updateTestBinary("https://offline.invalid/matching-family.tar", content) + binary.ArchivePath = updateRuntimeName + require.NoError(t, f.update(map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}})) + assertSelectedExecutable(t, f.binaryPath(updateRuntimeKey), content, binary.Checksum) + assertSelectedExecutable(t, f.binaryPath("UNRELATED"), content, binary.Checksum) + target, err := os.Readlink(alias) + require.NoError(t, err) + assert.Equal(t, "unrelated", target, "matching installed reuse should not reset the family") + assertNoUpdateStaging(t, f) +} + +type updateAliasFixture struct { + fixture *preservationFixture + selected string + preserved map[string]os.FileMode +} + +func newUpdateAliasFixture(t *testing.T, kind string) updateAliasFixture { + t.Helper() + requireProviderUpdateSymlinks(t) + f := newPreservationFixture(t) + f.seedBinary("UNRELATED", []byte("unrelated executable retained through alias replacement")) + unrelatedSidecar := filepath.Join(f.binaries, "unrelated", "sidecar") + f.put(unrelatedSidecar, []byte("unrelated ancillary content")) + family := filepath.Join(f.binaries, updateRuntimeName) + selected := updateRuntimeName + if kind == "family" { + require.NoError(t, os.Symlink("unrelated", family)) + } else { + require.NoError(t, os.MkdirAll(family, 0o700)) + if kind == "nested" { + selected = "bin/runtime" + require.NoError(t, os.Symlink("../unrelated", filepath.Join(family, "bin"))) + } else { + require.Equal(t, "leaf", kind) + require.NoError( + t, + os.Symlink("../unrelated/runtime", filepath.Join(family, updateRuntimeName)), + ) + } + f.put(filepath.Join(family, "sidecar"), []byte("selected family ancillary content")) + } + preserved := map[string]os.FileMode{} + for _, path := range []string{f.binaryPath("UNRELATED"), unrelatedSidecar, filepath.Join(family, "sidecar")} { + info, err := os.Stat(path) + require.NoError(t, err) + preserved[path] = info.Mode().Perm() + if _, ok := f.before[path]; !ok { + f.before[path], err = os.ReadFile( + path, + ) // #nosec G304 -- Snapshot of an owned fixture alias. + require.NoError(t, err) + } + } + return updateAliasFixture{fixture: f, selected: selected, preserved: preserved} +} + +func (a updateAliasFixture) assertReplacement( + t *testing.T, + binary *provider.ProviderBinary, + content []byte, +) { + t.Helper() + selected := filepath.Join(a.fixture.binaries, updateRuntimeName, filepath.FromSlash(a.selected)) + assertSelectedExecutable(t, selected, content, binary.Checksum) + for path, mode := range a.preserved { + actual, err := os.ReadFile( + path, + ) // #nosec G304 -- All paths belong to this test-owned provider tree. + require.NoError(t, err) + assert.Equal(t, a.fixture.before[path], actual, path) + info, err := os.Stat(path) + require.NoError(t, err) + assert.Equal(t, mode, info.Mode().Perm(), path) + } + a.fixture.assertAssociation() + assertNoUpdateStaging(t, a.fixture) +} + +func TestProviderUpdateNestedAndLeafAliasOfflineCache(t *testing.T) { + for _, kind := range []string{"nested", "leaf"} { + t.Run(kind, func(t *testing.T) { + a := newUpdateAliasFixture(t, kind) + content := []byte("cached independent replacement " + a.fixture.dir) + archive := compatibilityTar(t, false, []*tar.Header{ + {Name: a.selected, Typeflag: tar.TypeReg, Mode: 0o755, Size: int64(len(content))}, + }, map[string][]byte{a.selected: content}) + var requests atomic.Int64 + server := ownedProviderUpdateServer(t, func(w http.ResponseWriter, _ *http.Request) { + requests.Add(1) + _, _ = w.Write(archive) + }) + binary := updateTestBinary(server.URL+"/nested-alias.tar", content) + binary.ArchivePath = a.selected + binaries := map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}} + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + _, err := provider.DownloadBinaries(ctx, binaries, t.TempDir()) + require.NoError(t, err) + server.Close() + require.NoError( + t, + a.fixture.update(binaries), + "warmed executable cache must work with the source offline", + ) + require.EqualValues( + t, + 1, + requests.Load(), + "replacement must not fetch the archive again", + ) + a.assertReplacement(t, binary, content) + }) + } +} + +func TestProviderUpdatePlainAndLocalAncestorAlias(t *testing.T) { + for _, local := range []bool{false, true} { + t.Run( + map[bool]string{false: "plain remote", true: "relative local"}[local], + func(t *testing.T) { + a := newUpdateAliasFixture(t, "family") + content := []byte("independent plain replacement " + a.fixture.dir) + binary := updateTestBinary("source-runtime", content) + var requests atomic.Int64 + if local { + t.Chdir(t.TempDir()) + require.NoError(t, os.WriteFile(binary.Path, content, 0o600)) + } else { + server := ownedProviderUpdateServer( + t, + func(w http.ResponseWriter, _ *http.Request) { + requests.Add(1) + _, _ = w.Write(content) + }, + ) + binary.Path = server.URL + "/plain-runtime" + } + require.NoError( + t, + a.fixture.update( + map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}}, + ), + ) + if local { + actual, err := os.ReadFile( + binary.Path, + ) // #nosec G304 -- Relative source in owned temporary cwd. + require.NoError(t, err) + assert.Equal(t, content, actual) + } else { + require.EqualValues(t, 1, requests.Load()) + } + a.assertReplacement(t, binary, content) + }, + ) + } +} + +func TestProviderUpdateAncestorRootAliasOfflineCache(t *testing.T) { + requireProviderUpdateSymlinks(t) + f := newPreservationFixture(t) + old := []byte("original root descendant remains unchanged") + f.seedBinary("UNRELATED", old) + f.put( + filepath.Join(f.binaries, "unrelated", "sidecar"), + []byte("retained root descendant sidecar"), + ) + require.NoError(t, os.Symlink(".", filepath.Join(f.binaries, updateRuntimeName))) + require.NoError(t, os.Symlink("unrelated", filepath.Join(f.binaries, "directory-link"))) + require.NoError(t, os.Symlink("unrelated/runtime", filepath.Join(f.binaries, "file-link"))) + content := []byte("root alias cached replacement " + f.dir) + selected := "unrelated/runtime" + archive := compatibilityTar(t, false, []*tar.Header{ + {Name: selected, Typeflag: tar.TypeReg, Mode: 0o755, Size: int64(len(content))}, + }, map[string][]byte{selected: content}) + var requests atomic.Int64 + server := ownedProviderUpdateServer(t, func(w http.ResponseWriter, _ *http.Request) { + requests.Add(1) + _, _ = w.Write(archive) + }) + binary := updateTestBinary(server.URL+"/ancestor-root.tar", content) + binary.ArchivePath = selected + binaries := map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}} + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + _, err := provider.DownloadBinaries(ctx, binaries, t.TempDir()) + require.NoError(t, err) + server.Close() + require.NoError( + t, + f.update(binaries), + "ancestor-root cloning must complete within the fixture deadline", + ) + require.EqualValues(t, 1, requests.Load(), "offline consumption must not refetch") + family := filepath.Join(f.binaries, updateRuntimeName) + assertSelectedExecutable( + t, + filepath.Join(family, filepath.FromSlash(selected)), + content, + binary.Checksum, + ) + assertSelectedExecutable(t, f.binaryPath("UNRELATED"), old, updateTestDigest(old)) + assertRebasedUpdateRootLinks(t, f, family, old) + assertNoUpdateStaging(t, f) +} + +func assertRebasedUpdateRootLinks(t *testing.T, f *preservationFixture, family string, old []byte) { + t.Helper() + for _, link := range []string{"directory-link", "file-link"} { + path := filepath.Join(family, link) + info, err := os.Lstat(path) + require.NoError(t, err) + require.NotZero(t, info.Mode()&os.ModeSymlink) + resolved, err := filepath.EvalSymlinks(path) + require.NoError(t, err) + original, err := filepath.EvalSymlinks(filepath.Join(f.binaries, link)) + require.NoError(t, err) + assert.Equal( + t, + original, + resolved, + "cloned link must keep the original target, not the replaced executable", + ) + } + for _, path := range []string{ + filepath.Join(family, "file-link"), filepath.Join(family, "directory-link", updateRuntimeName), + } { + assertSelectedExecutable(t, path, old, updateTestDigest(old)) + } + for _, base := range []string{f.binaries, family} { + path := filepath.Join(base, "unrelated", "sidecar") + actual, err := os.ReadFile(path) // #nosec G304 -- Original or cloned test-owned sidecar. + require.NoError(t, err) + assert.Equal(t, f.before[filepath.Join(f.binaries, "unrelated", "sidecar")], actual) + } + entries, err := os.ReadDir(family) + require.NoError(t, err) + for _, entry := range entries { + assert.NotContains( + t, + entry.Name(), + ".update-destination-", + "temporary clone must not copy itself", + ) + } + f.assertAssociation() +} + +func TestProviderUpdateConflictingReusedFamilyAlias(t *testing.T) { + requireProviderUpdateSymlinks(t) + f := newPreservationFixture(t) + old := []byte("both selected families initially expose these old bytes") + f.seedBinary(updateFirstKey, old) + f.seedBinary(updateSecondKey, old) + firstFamily := filepath.Dir(f.binaryPath(updateFirstKey)) + require.NoError(t, os.Remove(f.binaryPath(updateFirstKey))) + require.NoError(t, os.Remove(firstFamily)) + target := filepath.Base(filepath.Dir(f.binaryPath(updateSecondKey))) + require.NoError(t, os.Symlink(target, firstFamily)) + content := []byte("only the second family requests these new bytes") + var missingRequests, replacementRequests atomic.Int64 + server := ownedProviderUpdateServer(t, func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/missing-first" { + missingRequests.Add(1) + http.Error(w, "owned first-family source deliberately unavailable", http.StatusNotFound) + return + } + replacementRequests.Add(1) + _, _ = w.Write(content) + }) + first := updateTestBinary(server.URL+"/missing-first", old) + second := updateTestBinary(server.URL+"/new-second", content) + err := f.update(map[string][]*provider.ProviderBinary{ + updateFirstKey: {first}, updateSecondKey: {second}, + }) + require.EqualValues(t, 1, replacementRequests.Load()) + t.Logf("first-family fetches=%d; update result=%v", missingRequests.Load(), err) + if err == nil { + // Independent materialization is permitted, but alias reuse must never + // publish the second family's bytes under the first family's checksum. + assertSelectedExecutable(t, f.binaryPath(updateFirstKey), old, first.Checksum) + assertSelectedExecutable(t, f.binaryPath(updateSecondKey), content, second.Checksum) + } else { + require.True( + t, + strings.Contains(err.Error(), "404") || strings.Contains(err.Error(), "checksum"), + "both map iteration orders must fail for the conflict, not an unrelated error: %v", + err, + ) + f.assertFailedPreservation() + link, linkErr := os.Readlink(firstFamily) + require.NoError(t, linkErr) + assert.Equal(t, target, link) + } + assertNoUpdateStaging(t, f) +} + +func TestProviderUpdateEmptyBinaryKeyPreservesUnrelatedFamily(t *testing.T) { + f := newPreservationFixture(t) + content := []byte("empty-key selected executable") + f.seedBinary("UNRELATED", []byte("retained unrelated installed executable")) + archive := compatibilityTar(t, false, []*tar.Header{ + {Name: updateRuntimeName, Typeflag: tar.TypeReg, Mode: 0o755, Size: int64(len(content))}, + }, map[string][]byte{updateRuntimeName: content}) + server := compatibilityArchiveServer(t, archive) + binary := updateTestBinary(server.URL+"/empty-key.tar", content) + binary.ArchivePath = updateRuntimeName + + err := f.update(map[string][]*provider.ProviderBinary{"": {binary}}) + require.ErrorContains(t, err, "unsafe provider update binary key") + f.assertFailedPreservation() + assertNoUpdateStaging(t, f) +} diff --git a/pkg/workspace/provider_update_preservation_test.go b/pkg/workspace/provider_update_preservation_test.go new file mode 100644 index 0000000000..b02742dd4d --- /dev/null +++ b/pkg/workspace/provider_update_preservation_test.go @@ -0,0 +1,182 @@ +package workspace + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "runtime" + "sync/atomic" + "testing" + "time" + + "github.com/devsy-org/devsy/pkg/config" + "github.com/devsy-org/devsy/pkg/provider" + "github.com/devsy-org/devsy/pkg/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestProviderUpdateFailedDownloadPreservesExisting(t *testing.T) { + f := newFailedDownloadFixture(t) + var requests atomic.Int64 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requests.Add(1) + http.Error(w, "owned replacement artifact deliberately unavailable", http.StatusNotFound) + })) + t.Cleanup(func() { + server.CloseClientConnections() + server.Close() + t.Log( + "owned HTTP server closed and handlers joined; t.TempDir cleanup removes isolated provider/config/workspace state", + ) + }) + digest := sha256.Sum256([]byte(f.directory + " never served replacement")) + replacement := &provider.ProviderConfig{ + Name: f.name, Version: updateReplacementVersion, + Exec: provider.ProviderCommands{Command: []string{updateNoopCommand}}, + Options: map[string]*types.Option{updateKeptOption: {}}, + Binaries: map[string][]*provider.ProviderBinary{"NEW_RUNTIME": {{ + OS: runtime.GOOS, Arch: runtime.GOARCH, Name: "new-runtime", + Path: server.URL + "/runtime", Checksum: hex.EncodeToString(digest[:]), + }}}, + } + raw, err := json.Marshal(replacement) + require.NoError(t, err) + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + _, err = updateProvider(ctx, ProviderParams{ + DevsyConfig: f.config, + ProviderName: f.name, + Raw: raw, + Source: &provider.ProviderSource{ + URL: server.URL + "/provider.yaml", + Raw: server.URL + "/provider.yaml", + }, + }) + require.ErrorContains(t, err, "download binaries") + require.NoError(t, ctx.Err(), "failure must be artifact HTTP status, not deadline") + require.EqualValues(t, 1, requests.Load()) + t.Logf("actual updateProvider failure: %v", err) + f.assertPreserved(t) +} + +func (f *failedDownloadFixture) assertPreserved(t *testing.T) { + t.Helper() + for path, before := range f.files { + // #nosec G304 -- Snapshot paths are created only by the isolated preservation fixture. + after, readErr := os.ReadFile(path) + relative, relErr := filepath.Rel(f.directory, path) + require.NoError(t, relErr) + if assert.NoError( + t, + readErr, + "existing artifact %s must survive failed replacement", + relative, + ) { + assert.Equal(t, before, after, "existing artifact %s bytes must survive", relative) + } + t.Logf("preservation artifact %s: present=%v", relative, readErr == nil) + } + stored, err := config.LoadConfig(testDefaultContext, "") + require.NoError(t, err) + state := stored.Current().Providers[f.name] + require.NotNil(t, state) + assert.True(t, state.Initialized, "failed replacement must preserve initialized state") + assert.True(t, state.InitAttempted) + assert.Equal( + t, + f.options, + state.Options, + "failed replacement must preserve all existing options", + ) + assert.Equal(t, f.name, stored.Current().DefaultProvider) + t.Logf( + "persisted state: initialized=%v initAttempted=%v keep=%q obsolete=%q", + state.Initialized, + state.InitAttempted, + state.Options[updateKeptOption].Value, + state.Options[updateObsoleteOption].Value, + ) + loadedWorkspace, err := provider.LoadWorkspaceConfig(testDefaultContext, f.workspace.ID) + require.NoError(t, err) + assert.Equal(t, f.workspace.UID, loadedWorkspace.UID) + assert.Equal(t, f.workspace.Provider, loadedWorkspace.Provider) + t.Logf("workspace association retained: id=%s uid=%s provider=%s", loadedWorkspace.ID, + loadedWorkspace.UID, loadedWorkspace.Provider.Name) +} + +type failedDownloadFixture struct { + config *config.Config + name, directory string + options map[string]config.OptionValue + files map[string][]byte + workspace *provider.Workspace +} + +func newFailedDownloadFixture(t *testing.T) *failedDownloadFixture { + t.Helper() + setupTestPathManager(t) + t.Setenv(config.EnvHome, t.TempDir()) + config.ResetPathManager() + const name = "update-preservation-test" + oldOptions := map[string]config.OptionValue{ + updateKeptOption: {Value: "old-kept-value", UserProvided: true}, + updateObsoleteOption: {Value: "old-obsolete-value", UserProvided: true}, + } + initial := &config.Config{ + DefaultContext: testDefaultContext, + Contexts: map[string]*config.ContextConfig{ + testDefaultContext: { + DefaultProvider: name, + Providers: map[string]*config.ProviderConfig{ + name: {Initialized: true, InitAttempted: true, Options: oldOptions}, + }, + }, + }, + } + require.NoError(t, config.SaveConfig(initial)) + oldProvider := &provider.ProviderConfig{ + Name: name, Version: "1.0.0", + Exec: provider.ProviderCommands{Command: []string{updateNoopCommand}}, + Source: provider.ProviderSource{Internal: true, Raw: "microsandbox"}, + Options: map[string]*types.Option{updateKeptOption: {}, updateObsoleteOption: {}}, + } + require.NoError(t, provider.SaveProviderConfig(testDefaultContext, oldProvider)) + providerDir, err := provider.GetProviderDir(testDefaultContext, name) + require.NoError(t, err) + binariesDir, err := provider.GetProviderBinariesDir(testDefaultContext, name) + require.NoError(t, err) + oldBinary := filepath.Join(binariesDir, "old-runtime", "old-runtime") + require.NoError(t, os.MkdirAll(filepath.Dir(oldBinary), 0o700)) + // #nosec G306 -- The fixture must preserve an executable, not only a non-executable data file. + require.NoError(t, os.WriteFile(oldBinary, []byte("old executable bytes"), 0o700)) + sentinel := filepath.Join(providerDir, "owned-sentinel") + require.NoError(t, os.WriteFile(sentinel, []byte("existing provider user data"), 0o600)) + oldFiles := map[string][]byte{} + for _, path := range []string{filepath.Join(providerDir, provider.ProviderConfigFile), oldBinary, sentinel} { + // #nosec G304 -- These manifest, binary and sentinel paths are test-owned fixture files. + oldFiles[path], err = os.ReadFile(path) + require.NoError(t, err) + } + workspace := &provider.Workspace{ + ID: "preserved-ws", + UID: "original-uid", + Context: testDefaultContext, + Provider: provider.WorkspaceProviderConfig{ + Name: name, + Options: map[string]config.OptionValue{ + "WORKSPACE_OPTION": {Value: "unchanged", UserProvided: true}, + }, + }, + } + require.NoError(t, provider.SaveWorkspaceConfig(workspace)) + return &failedDownloadFixture{ + config: initial, name: name, directory: providerDir, + options: oldOptions, files: oldFiles, workspace: workspace, + } +} diff --git a/pkg/workspace/provider_update_readonly_destination_test.go b/pkg/workspace/provider_update_readonly_destination_test.go new file mode 100644 index 0000000000..1f07c11f8c --- /dev/null +++ b/pkg/workspace/provider_update_readonly_destination_test.go @@ -0,0 +1,178 @@ +//go:build !windows + +package workspace + +import ( + "net/http" + "os" + "path/filepath" + "sync/atomic" + "testing" + + "github.com/devsy-org/devsy/pkg/provider" + "github.com/stretchr/testify/require" +) + +type readOnlyDirectUpdateCase struct { + name string + missingLeaf bool + missingFamily bool + failure bool + reuse bool +} + +func TestProviderUpdateDirectBinaryUnderReadOnlyParent(t *testing.T) { + if os.Geteuid() == 0 { + t.Skip("requires POSIX permission enforcement for an ordinary user") + } + + cases := []readOnlyDirectUpdateCase{ + {name: "replace existing leaf"}, + {name: "failed download restores modes", failure: true}, + {name: "matching checksum reuses installed leaf", reuse: true}, + {name: "create missing leaf", missingLeaf: true}, + {name: "create missing family", missingFamily: true}, + } + for _, testCase := range cases { + t.Run(testCase.name, func(t *testing.T) { + runReadOnlyDirectUpdateCase(t, testCase) + }) + } +} + +func runReadOnlyDirectUpdateCase(t *testing.T, testCase readOnlyDirectUpdateCase) { + t.Helper() + f, selected, parent := seedReadOnlyDirectUpdateCase(t, testCase) + old := []byte("old owner-writable selected executable") + updated := []byte("new direct remote executable") + if testCase.reuse { + updated = old + } + var requests atomic.Int64 + server := ownedProviderUpdateServer(t, func(w http.ResponseWriter, _ *http.Request) { + requests.Add(1) + if testCase.failure { + http.Error(w, "owned replacement unavailable", http.StatusNotFound) + return + } + _, _ = w.Write(updated) + }) + binary := updateTestBinary(server.URL+"/runtime", updated) + err := f.update(map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}}) + if testCase.failure { + assertFailedReadOnlyDirectUpdate(t, f, err, requests.Load()) + } else { + require.NoError(t, err) + wantRequests := int64(1) + if testCase.reuse { + wantRequests = 0 + } + require.EqualValues(t, wantRequests, requests.Load()) + assertSelectedExecutable(t, selected, updated, binary.Checksum) + f.assertAssociation() + } + if !testCase.missingFamily { + info, statErr := os.Stat(parent) + require.NoError(t, statErr) + require.Equal( + t, + os.FileMode(0o550), + info.Mode().Perm(), + "staged parent mode must be restored before publication", + ) + } + assertNoUpdateStaging(t, f) +} + +func seedReadOnlyDirectUpdateCase( + t *testing.T, + testCase readOnlyDirectUpdateCase, +) (*preservationFixture, string, string) { + t.Helper() + f := newPreservationFixture(t) + f.seedBinary(updateRuntimeKey, []byte("old owner-writable selected executable")) + selected := f.binaryPath(updateRuntimeKey) + parent := filepath.Dir(selected) + t.Cleanup(func() { makeOwnedUpdateTreeRemovable(t, f.dir) }) + if testCase.missingFamily { + require.NoError(t, os.RemoveAll(parent)) + } else { + if testCase.missingLeaf { + require.NoError(t, os.Remove(selected)) + } else { + // #nosec G302 -- The fixture reproduces a writable file below a read-only parent. + require.NoError(t, os.Chmod(selected, 0o700)) + } + // #nosec G302 -- The fixture reproduces ordinary staged-copy permission preservation. + require.NoError(t, os.Chmod(parent, 0o550)) + } + return f, selected, parent +} + +func assertFailedReadOnlyDirectUpdate( + t *testing.T, + f *preservationFixture, + err error, + requests int64, +) { + t.Helper() + require.ErrorContains(t, err, "404") + require.EqualValues(t, 1, requests) + f.assertFailedPreservation() +} + +func TestDownloadUpdateBinaryUnderReadOnlyStagingRoot(t *testing.T) { + if os.Geteuid() == 0 { + t.Skip("requires POSIX permission enforcement for an ordinary user") + } + + for _, missingFamily := range []bool{false, true} { + name := "existing read-only family" + if missingFamily { + name = "missing family" + } + t.Run(name, func(t *testing.T) { + root := filepath.Join(t.TempDir(), "binaries") + require.NoError(t, os.Mkdir(root, 0o700)) + selected := filepath.Join(root, "runtime", updateRuntimeName) + if !missingFamily { + require.NoError(t, os.Mkdir(filepath.Dir(selected), 0o700)) + // #nosec G306 -- Test input is intentionally owner-writable under a read-only directory. + require.NoError(t, os.WriteFile(selected, []byte("old staged bytes"), 0o700)) + // #nosec G302 -- Exercise permission preservation for an existing nested staged directory. + require.NoError(t, os.Chmod(filepath.Dir(selected), 0o550)) + } + unrelated := filepath.Join(root, "unrelated", "kept") + require.NoError(t, os.MkdirAll(filepath.Dir(unrelated), 0o700)) + require.NoError(t, os.WriteFile(unrelated, []byte("unrelated staged data"), 0o600)) + t.Cleanup(func() { makeOwnedUpdateTreeRemovable(t, root) }) + // #nosec G302 -- The staged root is deliberately read-only for the direct downloader. + require.NoError(t, os.Chmod(root, 0o550)) + + updated := []byte("replacement staged binary") + server := ownedProviderUpdateServer(t, func(w http.ResponseWriter, _ *http.Request) { + _, _ = w.Write(updated) + }) + binary := updateTestBinary(server.URL+"/runtime", updated) + paths, err := provider.DownloadUpdateBinaries( + t.Context(), + map[string][]*provider.ProviderBinary{updateRuntimeKey: {binary}}, + root, + ) + require.NoError(t, err) + require.Equal(t, selected, paths[updateRuntimeKey]) + assertSelectedExecutable(t, selected, updated, binary.Checksum) + actual, err := os.ReadFile(unrelated) // #nosec G304 -- Fixture-owned stage file. + require.NoError(t, err) + require.Equal(t, []byte("unrelated staged data"), actual) + info, err := os.Stat(root) + require.NoError(t, err) + require.Equal(t, os.FileMode(0o550), info.Mode().Perm()) + if !missingFamily { + info, err = os.Stat(filepath.Dir(selected)) + require.NoError(t, err) + require.Equal(t, os.FileMode(0o550), info.Mode().Perm()) + } + }) + } +} diff --git a/sites/docs-devsy-sh/content/docs/managing-providers/manage-providers.mdx b/sites/docs-devsy-sh/content/docs/managing-providers/manage-providers.mdx index 8f7d8718e4..ae8ff7a51e 100644 --- a/sites/docs-devsy-sh/content/docs/managing-providers/manage-providers.mdx +++ b/sites/docs-devsy-sh/content/docs/managing-providers/manage-providers.mdx @@ -154,6 +154,10 @@ devsy provider set-source ../path-to/provider.yaml In the desktop app, use the provider's **Select version**, **Update**, and **Initialize** actions. +Devsy prepares the replacement provider’s installation binaries (`binaries`) before changing the installed provider. Runtime binaries declared under `agent.binaries` are prepared later by workspace operations. A download, checksum, archive, or cancellation error during preparation keeps the installed binaries, provider settings, initialization state, and workspace associations intact. Absolute local binary paths are checked without modifying the referenced files. References into the managed binaries directory must also match the prepared result; ambiguous aliases into that directory are rejected before publication. + +If publishing the prepared update fails, Devsy attempts to restore the previous files. If restoration fails, the error identifies retained recovery artifacts; keep those files for recovery. This does not guarantee recovery after a process or machine crash. Initialization runs after publication: an initialization failure leaves the new provider installed and available for another initialization attempt. + Existing workspaces keep the provider version they were created with. Recreate one with `devsy workspace up --recreate` to pick up the new version. ## Remove a provider