Skip to content

Commit

Permalink
Set some metadata in the environment when captain invokes a retry com…
Browse files Browse the repository at this point in the history
…mand (#96)
  • Loading branch information
kylekthompson authored Dec 17, 2024
1 parent d9e0281 commit 4d096cb
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 21 deletions.
5 changes: 0 additions & 5 deletions .mint/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ on:
trigger: push
commit-sha: ${{ event.git.sha }}
branch: ${{ event.git.branch }}
pull_request:
init:
trigger: pull_request
commit-sha: ${{ event.git.sha }}
branch: ${{ event.git.branch }}

concurrency-pools:
- id: rwx-research/captain:ci:${{ init.branch }}:${{ init.trigger }}
Expand Down
12 changes: 6 additions & 6 deletions .mint/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ tasks:
- key: build
use: [captain-source, mage]
run: mage
env:
env:
CGO_ENABLED: 0
LDFLAGS: -w -s -X github.com/rwx-research/captain-cli.Version=testing-${{ init.commit-sha }}

- key: test
use: [build, ginkgo]
env:
env:
CGO_ENABLED: 0
LDFLAGS: -w -s -X github.com/rwx-research/captain-cli.Version=testing-${{ init.commit-sha }}
REPORT: true
Expand All @@ -75,19 +75,19 @@ tasks:

- key: test-alternate-filename
use: [build, ginkgo]
env:
env:
CGO_ENABLED: 0
LDFLAGS: -w -s -X github.com/rwx-research/captain-cli.Version=testing-${{ init.commit-sha }}
REPORT: true
RWX_ACCESS_TOKEN: ${{ secrets.RWX_ACCESS_TOKEN }}
RWX_ACCESS_TOKEN_STAGING: ${{ secrets.RWX_ACCESS_TOKEN_STAGING }} # used from within the tests against staging
run: |
mv .captain/config.yaml .captain/config.yml
./captain run captain-cli-ginkgo
./captain run captain-cli-ginkgo --command mage unittest
- key: test-without-config
use: [build, ginkgo]
env:
env:
CGO_ENABLED: 0
LDFLAGS: -w -s -X github.com/rwx-research/captain-cli.Version=testing-${{ init.commit-sha }}
REPORT: true
Expand All @@ -96,7 +96,7 @@ tasks:
run: |
rm -rf .captain
./captain run \
--command "mage test" \
--command "mage unittest" \
--fail-on-upload-error true \
--reporter github-step-summary \
--test-results report.xml
2 changes: 1 addition & 1 deletion internal/cli/abq.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (s Service) setAbqExitCode(ctx context.Context, captainErr error) error {
"set-exit-code",
"--run-id", state.RunID,
"--exit-code", fmt.Sprint(exitCode),
}, os.Stdout, false)
}, os.Stdout, false, []string{})
if err != nil {
err = errors.Wrap(err, "Error setting ABQ exit code")
}
Expand Down
24 changes: 16 additions & 8 deletions internal/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (s Service) RunSuite(ctx context.Context, cfg RunConfig) (finalErr error) {
}

// Run sub-command
ctx, cmdErr := s.runCommand(ctx, runCommand.commandArgs, stdout, true)
ctx, cmdErr := s.runCommand(ctx, runCommand.commandArgs, stdout, true, []string{})
defer func() {
if abqErr := s.setAbqExitCode(ctx, finalErr); abqErr != nil {
finalErr = errors.Wrap(finalErr, abqErr.Error())
Expand Down Expand Up @@ -544,6 +544,11 @@ func (s Service) attemptRetries(
}

ias.setCommandID(i + 1)
env := []string{
fmt.Sprintf("CAPTAIN_RETRY_ATTEMPT_NUMBER=%v", retries+1),
fmt.Sprintf("CAPTAIN_RETRY_INVOCATION_NUMBER=%v", i+1),
fmt.Sprintf("CAPTAIN_RETRY_COMMAND_ID=%v-%v", retries+1, i+1),
}

s.Log.Infoln()
s.Log.Infoln(strings.Repeat("-", 80))
Expand Down Expand Up @@ -582,7 +587,7 @@ func (s Service) attemptRetries(
)
}

if _, err := s.runCommand(ctx, preRetryArgs, stdout, false); err != nil {
if _, err := s.runCommand(ctx, preRetryArgs, stdout, false, env); err != nil {
return flattenedTestResults, flattenedNewlyExecutedTestResults, true, errors.Wrapf(
err,
"Error while executing %q",
Expand All @@ -591,7 +596,7 @@ func (s Service) attemptRetries(
}
}

_, cmdErr := s.runCommand(ctx, args, stdout, false)
_, cmdErr := s.runCommand(ctx, args, stdout, false, env)

for _, postRetryCommand := range cfg.PostRetryCommands {
postRetryArgs, err := shellwords.Parse(postRetryCommand)
Expand All @@ -603,7 +608,7 @@ func (s Service) attemptRetries(
)
}

if _, err := s.runCommand(ctx, postRetryArgs, stdout, false); err != nil {
if _, err := s.runCommand(ctx, postRetryArgs, stdout, false, env); err != nil {
return flattenedTestResults, flattenedNewlyExecutedTestResults, true, errors.Wrapf(
err,
"Error while executing %q",
Expand Down Expand Up @@ -687,17 +692,20 @@ func (s Service) runCommand(
args []string,
stdout io.Writer,
setAbqEnviron bool,
env []string,
) (context.Context, error) {
var environ []string

if setAbqEnviron {
ctx, environ = s.applyAbqEnvironment(ctx)
newCtx, environ := s.applyAbqEnvironment(ctx)
ctx = newCtx
if len(environ) > 0 {
env = append(env, environ...)
}
}

cmd, err := s.TaskRunner.NewCommand(ctx, exec.CommandConfig{
Name: args[0],
Args: args[1:],
Env: environ,
Env: env,
Stdout: stdout,
Stderr: os.Stderr,
})
Expand Down
17 changes: 16 additions & 1 deletion internal/cli/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,7 @@ var _ = Describe("Run", func() {
}

runConfig = cli.RunConfig{
Args: []string{arg},
Command: arg,
TestResultsFileGlob: testResultsFilePath,
SuiteID: "test",
Retries: 1,
Expand Down Expand Up @@ -1316,10 +1316,25 @@ var _ = Describe("Run", func() {
newCommand := func(_ context.Context, cfg exec.CommandConfig) (exec.Command, error) {
switch cfg.Name {
case "pre":
Expect(cfg.Env).To(ContainElement(ContainSubstring("CAPTAIN_RETRY_ATTEMPT_NUMBER=")))
Expect(cfg.Env).To(ContainElement(ContainSubstring("CAPTAIN_RETRY_INVOCATION_NUMBER=")))
Expect(cfg.Env).To(ContainElement(ContainSubstring("CAPTAIN_RETRY_COMMAND_ID=")))
return mockPreRetryCommand, nil
case "post":
Expect(cfg.Env).To(ContainElement(ContainSubstring("CAPTAIN_RETRY_ATTEMPT_NUMBER=")))
Expect(cfg.Env).To(ContainElement(ContainSubstring("CAPTAIN_RETRY_INVOCATION_NUMBER=")))
Expect(cfg.Env).To(ContainElement(ContainSubstring("CAPTAIN_RETRY_COMMAND_ID=")))
return mockPostRetryCommand, nil
default:
if strings.Contains(cfg.Name, "retry") {
Expect(cfg.Env).To(ContainElement(ContainSubstring("CAPTAIN_RETRY_ATTEMPT_NUMBER=")))
Expect(cfg.Env).To(ContainElement(ContainSubstring("CAPTAIN_RETRY_INVOCATION_NUMBER=")))
Expect(cfg.Env).To(ContainElement(ContainSubstring("CAPTAIN_RETRY_COMMAND_ID=")))
} else {
Expect(cfg.Env).NotTo(ContainElement(ContainSubstring("CAPTAIN_RETRY_ATTEMPT_NUMBER=")))
Expect(cfg.Env).NotTo(ContainElement(ContainSubstring("CAPTAIN_RETRY_INVOCATION_NUMBER=")))
Expect(cfg.Env).NotTo(ContainElement(ContainSubstring("CAPTAIN_RETRY_COMMAND_ID=")))
}
return mockCommand, nil
}
}
Expand Down

0 comments on commit 4d096cb

Please sign in to comment.