beads/internal/testutil/credentialcmd/credentialcmd.go
Julian Knutsen fe6bd6301f
ci: fix red nightly (Bazel-skip allowlist, credential helper init); rerun-safe Bazel artifacts (#7096)
* fix(ci): allowlist the scripts tests that skip under Bazel, and check new ones before merge

nightly.yml's skip-parity check (equivalence.py --go-test-json) went red
on 2026-10-02 (run 36955306415): TestBazelGatedLanesNeverRetryFlakyTests
and TestCheckShardCoverageScript pass under go test and deliberately
t.Skip under Bazel (their inputs are not in //scripts:scripts_test's
runfiles), but had no `skip` allowlist entry. Step 2's
TestShardScriptsListOnlyRealTests and TestShardScriptNarrowingCheck, now
on main, have the same shape and would have turned the next nightly red.

Add the four entries, and TestBazelOnlySkipsAreAllowlisted: it parses
every _test.go file of the module and requires each top-level test whose
t.Skip is guarded by TEST_SRCDIR or bazeltest.IsBazel() (directly or
through a helper) to have an entry, so a new one fails under go test
before the merge instead of in the next nightly. It skips under Bazel
itself and is allowlisted too.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(test): keep cmd/bd's integration init out of the credential-command helper process

nightly.yml's Full Test Suite (go test -tags=integration ./...) failed
TestApplyInitGatewayCredentialAdoptsToken and ...FailsClosed on
2026-10-02 (run 36955306415): credentialcmd re-execs the test binary as
the credential helper, and cli_fast_test.go's integration-tagged init()
runs in that child before TestMain can dispatch it, tries `go build .`
from the helper's working directory and panics, so the helper exits 2.
Add credentialcmd.IsHelperProcess (Dispatch's two gates) and return from
that init early, as it already does for the worktree-remove helper.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* ci(bazel): overwrite fixed-name artifacts so "Re-run failed jobs" does not 409

A job re-run is the same workflow run, and upload-artifact v4+ refuses a
second artifact of the same name there (409 Conflict). bazel-test uploads
the bd package even after its tests fail, so re-running a failed Bazel /
test lane, the documented recovery for an eviction or a flake, failed on
the upload even when every test passed. Set overwrite: true on all eight
uploads in bazel.yml (the bd package, BUILD sync patch, every lane's test
logs) and policy-test it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(test): keep skip-scan fixture functions out of line-based test discovery

The fixture source in bazel_skip_allowlist_test.go had top-level
`func TestXxx(t *testing.T)` lines, which equivalence.py (and the CI shard
scripts) count as tests of the scripts package; write them with a FUNC
placeholder and substitute at write time.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local>
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-02 05:42:05 -07:00

202 lines
6.7 KiB
Go

// Package credentialcmd provides shell-portable credential-command fixtures
// for tests that need to exercise the production shell boundary.
package credentialcmd
import (
"encoding/base64"
"fmt"
"io"
"os"
"strings"
"sync"
"sync/atomic"
"testing"
)
const (
helperProcessEnv = "BEADS_TEST_CREDENTIAL_COMMAND_HELPER"
helperSentinel = "beads-credential-command-helper"
helperTestRun = "-test.run=NoTestsMatchCredentialCommandHelper"
malformedExit = 97
)
var (
commandSequence atomic.Uint64
fixtureOnce sync.Once
fixture preparedFixture
)
type preparedFixture struct {
root string
shellName string
shellDir string
shellPath string
helperPath string
targetPath string
targetEnv string
err error
}
// Dispatch handles an opted-in credential helper invocation. TestMain callers
// must call it before ordinary package setup so the helper process does not
// allocate suite resources.
//
// Both gates must hold before this claims the process: the env var AND the argv
// sentinel. The env gate alone is not sufficient, because command() arms
// helperProcessEnv in the *parent* test process and BEADS_-prefixed variables
// reach every re-exec'd child through integration.FilterEnv's allowlist. A
// package that re-execs its own test binary for an unrelated reason —
// internal/storage/dolt does exactly that, and maintains helperSubprocessSentinels
// for the hazard — would otherwise have its child claimed here and killed with a
// wrong-subsystem exit 97 the moment the two compose. An env-armed process
// without the sentinel is simply not ours, so it falls through to its own
// TestMain rather than being refused.
func Dispatch() (int, bool) {
if i := helperArgIndex(); i >= 0 {
return runProtocol(os.Args[i+1:], os.Stdout, os.Stderr), true
}
return 0, false
}
// IsHelperProcess reports whether this process is a credential helper
// re-exec (the same two gates as Dispatch). Package init functions, which run
// before TestMain can call Dispatch, use it to skip suite setup the helper
// never needs: cmd/bd's integration-tagged init would otherwise try to build
// bd from the helper's working directory and panic.
func IsHelperProcess() bool {
return helperArgIndex() >= 0
}
func helperArgIndex() int {
if os.Getenv(helperProcessEnv) != "1" {
return -1
}
for i, arg := range os.Args {
if arg == helperSentinel {
return i
}
}
return -1
}
// Constructor contract — Emit, Exit23 and Marker all share it, and it is more
// than "returns a string":
//
// - They replace PATH for the remainder of the calling test with the isolated
// shell directory, and set two to three environment variables. Production
// code invoked later in the same test resolves binaries under that narrowed
// PATH, so any lookup it makes is affected.
// - They use t.Setenv, which makes the calling test permanently incompatible
// with t.Parallel.
// - Each returns a *distinct* command string, which is load-bearing: the
// production credential cache is keyed by command text, so identical
// fixtures would otherwise hit a 60s cross-test cache.
//
// Cleanup removes the package process's suite-scoped transport artifacts after
// all tests have completed.
func Cleanup() error {
if fixture.root == "" {
return nil
}
return os.RemoveAll(fixture.root)
}
// Emit returns a unique shell command that writes value to stdout without a
// trailing newline. See the constructor contract above for the PATH/env side
// effects every constructor here shares.
//
// value must not be empty. The payload is appended as one word, and both sh and
// cmd word-split an empty argument away, so the helper would receive only
// "emit" and exit 97 "malformed protocol" — a confusing way to reach a case the
// production parser rejects anyway ("credential command produced no output").
// Use Marker for a command that runs but yields nothing usable.
func Emit(t *testing.T, value string) string {
t.Helper()
if value == "" {
t.Fatalf("credentialcmd.Emit: empty value is unsupported; the shell drops the empty payload word " +
"and the helper exits 97. Use Marker for a command that runs and produces no usable output.")
}
return command(t, "emit", []byte(value))
}
// Exit23 returns a unique shell command that exits with status 23. See the
// constructor contract above for the PATH/env side effects every constructor
// here shares.
func Exit23(t *testing.T) string {
t.Helper()
return command(t, "exit23", nil)
}
// Marker returns a unique shell command that writes an invocation marker. See
// the constructor contract above for the PATH/env side effects every
// constructor here shares.
func Marker(t *testing.T, path string) string {
t.Helper()
return command(t, "marker", []byte(path))
}
// AssertMarkerAbsent fails when a Marker command ran unexpectedly.
func AssertMarkerAbsent(t *testing.T, path string) {
t.Helper()
if _, err := os.Stat(path); err == nil {
t.Fatalf("credential command ran unexpectedly and wrote %q", path)
} else if !os.IsNotExist(err) {
t.Fatalf("inspect credential command marker %q: %v", path, err)
}
}
func command(t *testing.T, operation string, payload []byte) string {
t.Helper()
fixtureOnce.Do(prepareFixture)
if fixture.err != nil {
t.Fatalf("prepare credential command fixture: %v", fixture.err)
}
if !strings.Contains(fixture.helperPath, " ") {
t.Fatalf("credential helper path does not exercise quoting: %q", fixture.helperPath)
}
executableEnv := fmt.Sprintf("BEADS_TEST_CREDENTIAL_COMMAND_EXE_%d", commandSequence.Add(1))
executable := configurePlatformCommand(t, executableEnv)
t.Setenv(helperProcessEnv, "1")
parts := []string{executable, helperTestRun, "--", helperSentinel, operation}
if payload != nil {
parts = append(parts, base64.RawURLEncoding.EncodeToString(payload))
}
return strings.Join(parts, " ")
}
func runProtocol(args []string, stdout, stderr io.Writer) int {
malformed := func() int {
fmt.Fprintln(stderr, "credential command helper: malformed protocol")
return malformedExit
}
switch {
case len(args) == 2 && args[0] == "emit":
payload, err := base64.RawURLEncoding.DecodeString(args[1])
if err != nil {
return malformed()
}
if _, err := stdout.Write(payload); err != nil {
fmt.Fprintf(stderr, "credential command helper: write stdout: %v\n", err)
return malformedExit
}
return 0
case len(args) == 1 && args[0] == "exit23":
return 23
case len(args) == 2 && args[0] == "marker":
payload, err := base64.RawURLEncoding.DecodeString(args[1])
if err != nil || len(payload) == 0 {
return malformed()
}
if err := os.WriteFile(string(payload), []byte("invoked"), 0o600); err != nil {
fmt.Fprintf(stderr, "credential command helper: write marker: %v\n", err)
return malformedExit
}
return 0
default:
return malformed()
}
}