Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Add ignite e2e test helper package
Browse files Browse the repository at this point in the history
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
darkowlzz committed Aug 30, 2020
1 parent cc76e20 commit 16cd4d7
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions e2e/util/command.go
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)
}
}

0 comments on commit 16cd4d7

Please sign in to comment.