(t *testing.T)
| 39 | echo "this is not the agent you are looking for"` |
| 40 | |
| 41 | func TestAgentScript(t *testing.T) { |
| 42 | t.Parallel() |
| 43 | |
| 44 | t.Run("Valid", func(t *testing.T) { |
| 45 | t.Parallel() |
| 46 | |
| 47 | ctx := testutil.Context(t, testutil.WaitShort) |
| 48 | script := serveScript(t, bashEcho) |
| 49 | |
| 50 | output := testutil.NewWaitBuffer() |
| 51 | // This is intentionally ran in single quotes to mimic how a customer may |
| 52 | // embed our script. Our scripts should not include any single quotes. |
| 53 | // nolint:gosec |
| 54 | cmd := exec.CommandContext(ctx, "sh", "-c", "sh -c '"+script+"'") |
| 55 | cmd.Stdout = output |
| 56 | cmd.Stderr = output |
| 57 | require.NoError(t, cmd.Start()) |
| 58 | |
| 59 | err := cmd.Wait() |
| 60 | if err != nil { |
| 61 | var exitErr *exec.ExitError |
| 62 | if errors.As(err, &exitErr) { |
| 63 | require.Equal(t, 0, exitErr.ExitCode()) |
| 64 | } else { |
| 65 | t.Fatalf("unexpected err: %s", err) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | t.Log(output.String()) |
| 70 | require.NoError(t, err) |
| 71 | // Ignore debug output from `set -x`, we're only interested in the last line. |
| 72 | lines := strings.Split(strings.TrimSpace(output.String()), "\n") |
| 73 | lastLine := lines[len(lines)-1] |
| 74 | // When we use the "bashEcho" binary, we should expect the arguments provided |
| 75 | // as the response to executing our script. |
| 76 | require.Equal(t, versionOutput, lastLine) |
| 77 | }) |
| 78 | |
| 79 | t.Run("Invalid", func(t *testing.T) { |
| 80 | t.Parallel() |
| 81 | |
| 82 | ctx := testutil.Context(t, testutil.WaitShort) |
| 83 | script := serveScript(t, unexpectedEcho) |
| 84 | |
| 85 | output := testutil.NewWaitBuffer() |
| 86 | // This is intentionally ran in single quotes to mimic how a customer may |
| 87 | // embed our script. Our scripts should not include any single quotes. |
| 88 | // nolint:gosec |
| 89 | cmd := exec.CommandContext(ctx, "sh", "-c", "sh -c '"+script+"'") |
| 90 | cmd.WaitDelay = time.Second |
| 91 | cmd.Stdout = output |
| 92 | cmd.Stderr = output |
| 93 | require.NoError(t, cmd.Start()) |
| 94 | |
| 95 | done := make(chan error, 1) |
| 96 | var wg sync.WaitGroup |
| 97 | wg.Add(1) |
| 98 | go func() { |
nothing calls this directly
no test coverage detected