* test(credentials): make command fixtures Windows-portable (#5748) Replace POSIX-only credential test commands with a shared shell-portable protocol, exercise their intended failure and non-invocation semantics on native Windows, and add the focused required CI selector. Agent-Signature: Codex-unknown-model-unknown-reasoning on behalf of Ewen Cuthiell * test(ci): require each Windows credential fixture to pass Include the standalone credentialcmd protocol test in the existing Windows step. One package/test map drives both selection and exact terminal checks, so missing, skipped, failed, or duplicate tests cannot report success. Resolve native Go once and reuse it for the test invocation. Bead: saa-cgh.66 Agent-Signature: unknown-model/unknown-reasoning * test(ci): select the first Go application on PATH Hosted Windows images can expose more than one Go application. Preserve setup-go's PATH precedence, select one absolute application and reuse it. Bead: saa-cgh.66 Agent-Signature: unknown-model/unknown-reasoning * chore(bazel): sync BUILD.bazel srcs for the new credentialcmd test helper Base landed the Bazel PR gates (#6881/#6887) after this PR was authored, so the PR's new internal/testutil/credentialcmd package and internal/creds testmain_test.go were never enumerated in any BUILD.bazel. Two already enumerated test srcs (internal/creds/command_test.go and internal/storage/dolt/gateway_credential_test.go) import the new package, so `bazel build //internal/creds:all` fails GoCompilePkg without this. Generated by `make bazel-sync` (gazelle); a second sync is a no-op. The internal/storage/dolt version_staging_completeness_test.go srcs row is pre-existing base drift that gazelle corrects in the same pass, not PR content. * test(credentialcmd): pin the Windows gate's assertions and stop claiming foreign helpers Maintainer review fixes for PR #5749, adoption iteration 1. The synthesis is `approve` with no required changes; all four findings are minor or nit. A maintainer bazel commit already exists on this branch, so the push was owed regardless and none of these cost anything extra. No production code changes. F1 — the new required Windows gate pinned its own wiring but almost none of its assertions. The guard checked three package paths and one test name, so it stayed green if someone dropped six of the nine test names from $expected, deleted the `Action -in 'fail', 'skip'` throw, or deleted the exactly-one-PASS loop -- leaving a required gate whose only real guarantee was "go test exited 0", which a fully skipped suite also satisfies. The pinned slice now carries all nine test names plus both load-bearing guard tokens, keeping the file's existing substring idiom. F2 — the property the fixture exists for was unassertable end to end. Emit is written to produce byte-exact stdout across sh and the Windows .cmd trampoline, but parseCredential runs bytes.TrimSpace before every comparison, so every existing assertion is blind to a trailing newline: a trampoline changed to `echo %OUT%` would keep the whole suite green. TestCommandSourceRealShell now wraps the existing credRunner package seam to capture the helper's stdout as the shell produced it, and asserts it byte-exact. The real shell stays in the loop, so this remains end-to-end rather than becoming a second in-process test. F3 — Dispatch claimed any process where BEADS_TEST_CREDENTIAL_COMMAND_HELPER=1, on the env alone and before inspecting argv, exiting 97 when the sentinel was absent. command() arms that variable in the *parent* process and BEADS_-prefixed variables reach every re-exec'd child through the integration allowlist, so the first composition with a package that re-execs its own test binary -- which internal/storage/dolt already does -- would have died with a wrong-subsystem "missing protocol sentinel". Both gates are now required, and an env-armed process without the sentinel falls through to its own TestMain. The second half of that finding's prescription is deliberately NOT applied. Adding BEADS_TEST_CREDENTIAL_COMMAND_HELPER to helperSubprocessSentinels reds TestHelperSubprocessSentinelsAreComplete: that guard has a reverse-direction control, and its regex looks for a literal os.Getenv("BEADS_..._HELPER") in internal/storage/dolt's own test sources. This sentinel is read in internal/testutil/credentialcmd, so the entry registers as a stale one. Measured, not assumed -- the failure is "helperSubprocessSentinels lists BEADS_TEST_CREDENTIAL_COMMAND_HELPER, but no test source reads it". With the Dispatch gate fixed the entry also buys nothing: a dolt helper child already carries its own sentinel, so isHelperSubprocess is true for it either way. F4 — the exported constructors documented themselves as "returns a unique shell command" while replacing PATH for the rest of the calling test, setting env vars via t.Setenv (which bans t.Parallel), and silently rejecting an empty payload. The shared contract is now stated once above the three of them, and Emit fails fast with an explanation instead of letting the shell drop the empty payload word and the helper exit 97. No caller passes an empty payload today. Each fix was checked by reverting it and confirming its own pin reddens: the dropped test name reds the guard by name; the trailing-newline wrong implementation reds the new raw-stdout assertion while the sibling end-to-end test stays green, which is the blindness the finding described; and reverting the Dispatch gate reproduces `(97, true)`. `make bazel-sync` is a no-op -- every edit landed in a file already enumerated in srcs. --------- Co-authored-by: Gas City Adopt-PR <adopt-pr@gascity.local> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
187 lines
6.2 KiB
Go
187 lines
6.2 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 os.Getenv(helperProcessEnv) != "1" {
|
|
return 0, false
|
|
}
|
|
|
|
for i, arg := range os.Args {
|
|
if arg == helperSentinel {
|
|
return runProtocol(os.Args[i+1:], os.Stdout, os.Stderr), true
|
|
}
|
|
}
|
|
return 0, false
|
|
}
|
|
|
|
// 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()
|
|
}
|
|
}
|