-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support on.fail.exec actions #13
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Use and distribution licensed under the Apache license version 2. | ||
// | ||
// See the COPYING file in the root project directory for full text. | ||
|
||
package exec | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"os/exec" | ||
"testing" | ||
|
||
gdtcontext "github.com/gdt-dev/gdt/context" | ||
"github.com/gdt-dev/gdt/debug" | ||
gdterrors "github.com/gdt-dev/gdt/errors" | ||
"github.com/google/shlex" | ||
) | ||
|
||
// Action describes a single execution of one or more commands via the | ||
// operating system's `exec` family of functions. | ||
type Action struct { | ||
// Exec is the exact command to execute. | ||
// | ||
// You may execute more than one command but must include the `shell` field | ||
// to indicate that the command should be run in a shell. It is best | ||
// practice, however, to simply use multiple `exec` specs instead of | ||
// executing multiple commands in a single shell call. | ||
Exec string `yaml:"exec"` | ||
// Shell is the specific shell to use in executing the command. If empty | ||
// (the default), no shell is used to execute the command and instead the | ||
// operating system's `exec` family of calls is used. | ||
Shell string `yaml:"shell,omitempty"` | ||
} | ||
|
||
// Do performs a single command or shell execution returning the corresponding | ||
// exit code and any runtime error. The `outbuf` and `errbuf` buffers will be | ||
// filled with the contents of the command's stdout and stderr pipes | ||
// respectively. | ||
func (a *Action) Do( | ||
ctx context.Context, | ||
t *testing.T, | ||
outbuf *bytes.Buffer, | ||
errbuf *bytes.Buffer, | ||
exitcode *int, | ||
) error { | ||
var target string | ||
var args []string | ||
if a.Shell == "" { | ||
// Parse time already validated exec string parses into valid shell | ||
// args | ||
args, _ = shlex.Split(a.Exec) | ||
target = args[0] | ||
args = args[1:] | ||
} else { | ||
target = a.Shell | ||
args = []string{"-c", a.Exec} | ||
} | ||
|
||
debug.Println(ctx, t, "exec: %s %s", target, args) | ||
|
||
var cmd *exec.Cmd | ||
cmd = exec.CommandContext(ctx, target, args...) | ||
|
||
outpipe, err := cmd.StdoutPipe() | ||
if err != nil { | ||
return err | ||
} | ||
errpipe, err := cmd.StderrPipe() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = cmd.Start() | ||
if gdtcontext.TimedOut(ctx, err) { | ||
return gdterrors.ErrTimeoutExceeded | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
if outbuf != nil { | ||
outbuf.ReadFrom(outpipe) | ||
} | ||
if errbuf != nil { | ||
errbuf.ReadFrom(errpipe) | ||
} | ||
|
||
err = cmd.Wait() | ||
if gdtcontext.TimedOut(ctx, err) { | ||
return gdterrors.ErrTimeoutExceeded | ||
} | ||
if err != nil && exitcode != nil { | ||
eerr, _ := err.(*exec.ExitError) | ||
ec := eerr.ExitCode() | ||
*exitcode = ec | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Use and distribution licensed under the Apache license version 2. | ||
// | ||
// See the COPYING file in the root project directory for full text. | ||
|
||
package exec | ||
|
||
// On describes actions that can be taken upon certain conditions. | ||
type On struct { | ||
// Fail contains one or more actions to take if any of a Spec's assertions | ||
// fail. | ||
// | ||
// For example, if you wanted to grep a log file in the event that no | ||
// connectivity on a particular IP:PORT combination could be made you might | ||
// do this: | ||
// | ||
// ```yaml | ||
// tests: | ||
// - exec: nc -z $HOST $PORT | ||
// on: | ||
// fail: | ||
// exec: grep ERROR /var/log/myapp.log | ||
// ``` | ||
// | ||
// The `grep ERROR /var/log/myapp.log` command will only be executed if | ||
// there is no connectivity to $HOST:$PORT and the results of that grep | ||
// will be directed to the test's output. You can use the `gdt.WithDebug()` | ||
// function to configure additional `io.Writer`s to direct this output to. | ||
Fail *Action `yaml:"fail,omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: on-fail-exec | ||
description: a scenario that has an on.fail.exec clause | ||
tests: | ||
- exec: echo "cat" | ||
assert: | ||
out: | ||
is: cat | ||
# Unfortunately there's not really any good way of testing things like this | ||
# except by manually causing an assertion to fail in the test case and checking | ||
# to see if the `on.fail` action was taken and debug output emitted to the | ||
# console. | ||
# | ||
# When I change `assert.out.is` above to "dat" instead of "cat", I get the | ||
# correct behaviour: | ||
# | ||
# === RUN TestOnFail | ||
# === RUN TestOnFail/on-fail-exec | ||
# action.go:59: exec: echo [cat] | ||
# eval.go:35: assertion failed: not equal: expected dat but got cat | ||
# action.go:59: exec: echo [bad kitty] | ||
# eval.go:46: on.fail.exec: stdout: bad kitty | ||
# === NAME TestOnFail | ||
# eval_test.go:256: | ||
# Error Trace: /home/jaypipes/src/github.com/gdt-dev/gdt/plugin/exec/eval_test.go:256 | ||
# Error: Should be false | ||
# Test: TestOnFail | ||
# --- FAIL: TestOnFail (0.00s) | ||
# --- FAIL: TestOnFail/on-fail-exec (0.00s) | ||
on: | ||
fail: | ||
exec: echo "bad kitty" | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🐈