This repository has been archived by the owner on Dec 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The helper package provides a type Command with attributes about ignite execution and methods to execute ignite commands with error checks. This simplifies a lot of the duplicate code in the e2e tests and provides reusable code to make e2e tests less verbose. This Command can be used to run commands for ignite and ignited.
- Loading branch information
Showing
1 changed file
with
74 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"testing" | ||
|
||
"gotest.tools/assert" | ||
) | ||
|
||
const ( | ||
DefaultVMImage = "weaveworks/ignite-ubuntu" | ||
) | ||
|
||
// Command is an ignite command execution helper. It takes a binary and the | ||
// arguments to run with the binary. It provides a chaining methods to | ||
// fascilitate easy construction of the command. | ||
type Command struct { | ||
bin string | ||
T *testing.T | ||
Cmd *exec.Cmd | ||
} | ||
|
||
// NewCommand takes a go test testing.T and path to ignite binary and returns | ||
// an initialized Command. | ||
func NewCommand(t *testing.T, binPath string) *Command { | ||
return &Command{ | ||
T: t, | ||
bin: binPath, | ||
Cmd: exec.Command(binPath), | ||
} | ||
} | ||
|
||
// New resets the command. This should be used to reuse an existing Command and | ||
// pass different arguments by method chaining. | ||
func (c *Command) New() *Command { | ||
// NOTE: Create a whole new instance of Command. Only assigning a new | ||
// exec.Command to c.Cmd results in "exec: Stdout already set" error. Reuse | ||
// of command is not allowed as per the os/exec docs. | ||
return &Command{ | ||
T: c.T, | ||
bin: c.bin, | ||
Cmd: exec.Command(c.bin), | ||
} | ||
} | ||
|
||
// With accepts arguments to be used with the command. It returns Command and | ||
// supports method chaining. | ||
func (c *Command) With(args ...string) *Command { | ||
c.Cmd.Args = append(c.Cmd.Args, args...) | ||
return c | ||
} | ||
|
||
// WithRuntime sets the runtime argument. | ||
func (c *Command) WithRuntime(arg string) *Command { | ||
return c.With("--runtime=" + arg) | ||
} | ||
|
||
// WithNetwork sets the network argument. | ||
func (c *Command) WithNetwork(arg string) *Command { | ||
return c.With("--network-plugin=" + arg) | ||
} | ||
|
||
// Run executes the command and performs an error check. It results in fatal | ||
// exit of the test if an error is encountered. In order to continue the test | ||
// on encountering an error, call Command.Cmd.CombinedOutput() or the | ||
// appropriate method to execute the command separately. | ||
func (c *Command) Run() { | ||
out, err := c.Cmd.CombinedOutput() | ||
assert.Check(c.T, err, fmt.Sprintf("cmd: \n%q\n%s", c.Cmd, out)) | ||
if err != nil { | ||
c.T.Fatalf("failed to run %q: %v", c.Cmd, err) | ||
} | ||
} |