MCPcopy Create free account
hub / github.com/docker/secrets-engine / TestRunCommand

Function TestRunCommand

plugins/pass/commands/run_test.go:239–297  ·  view source on GitHub ↗

TestRunCommand covers cobra-level behavior that does not depend on a running daemon. Resolution behavior is covered by TestResolveEnv.

(t *testing.T)

Source from the content-addressed store, hash-verified

237// TestRunCommand covers cobra-level behavior that does not depend on a running
238// daemon. Resolution behavior is covered by TestResolveEnv.
239func TestRunCommand(t *testing.T) {
240 exe, err := os.Executable()
241 require.NoError(t, err)
242
243 t.Run("no command given returns arg error", func(t *testing.T) {
244 cmd := RunCommand()
245 cmd.SetArgs([]string{})
246 cmd.SetContext(t.Context())
247 cmd.SetOut(testWriter{t})
248 cmd.SetErr(testWriter{t})
249 err := cmd.Execute()
250 require.Error(t, err)
251 assert.Contains(t, err.Error(), "requires at least 1 arg")
252 })
253
254 t.Run("forwards child exit code", func(t *testing.T) {
255 // Spawn a wrapper subprocess that runs RunCommand internally. The
256 // wrapper execs a grandchild (this test binary in helper mode) that
257 // exits with code 42. The wrapper's env contains no se:// references,
258 // so RunCommand never contacts the daemon. RunCommand calls
259 // os.Exit(42) on the ExitError, so the wrapper process itself exits
260 // 42, which we observe via exec.ExitError.
261 sub := exec.CommandContext(t.Context(), exe)
262 sub.Env = append(os.Environ(),
263 helperWrapperEnv+"=1",
264 helperActiveEnv+"=1",
265 helperExitEnv+"=42",
266 )
267 err := sub.Run()
268 var exitErr *exec.ExitError
269 require.True(t, errors.As(err, &exitErr), "expected ExitError, got %v", err)
270 assert.Equal(t, 42, exitErr.ExitCode())
271 })
272
273 t.Run("forwards SIGINT and exits 130", func(t *testing.T) {
274 if runtime.GOOS == "windows" {
275 t.Skip("SIGINT cross-process semantics differ on Windows")
276 }
277
278 sub := exec.CommandContext(t.Context(), exe)
279 sub.Env = append(os.Environ(),
280 helperWrapperEnv+"=1",
281 helperActiveEnv+"=1",
282 helperSleepEnv+"=1",
283 )
284 stderr, err := sub.StderrPipe()
285 require.NoError(t, err)
286 require.NoError(t, sub.Start())
287
288 waitForReady(t, stderr)
289
290 require.NoError(t, sub.Process.Signal(syscall.SIGINT))
291
292 err = sub.Wait()
293 var exitErr *exec.ExitError
294 require.True(t, errors.As(err, &exitErr), "expected ExitError, got %v", err)
295 assert.Equal(t, 130, exitErr.ExitCode())
296 })

Callers

nothing calls this directly

Calls 4

RunCommandFunction · 0.85
waitForReadyFunction · 0.85
RunMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected