Skip to content

Commit

Permalink
Add error handling to executors
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonBaeumer committed Nov 19, 2021
1 parent d2d94ca commit c4b2ea1
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 24 deletions.
18 changes: 11 additions & 7 deletions pkg/runtime/docker_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type DockerExecutor struct {
}

// Execute executes the script inside a docker container
func (e DockerExecutor) Execute(test TestCase) TestResult {
func (e DockerExecutor) Execute(test TestCase) (TestResult, error) {
log.Printf("DOCKER_HOST: %s \n", os.Getenv("DOCKER_HOST"))
log.Printf("DOCKER_CERT_PATH: %s \n", os.Getenv("DOCKER_CERT_PATH"))
log.Printf("DOCKER_API_VERSION: %s \n", os.Getenv("DOCKER_API_VERSION"))
Expand All @@ -39,7 +39,7 @@ func (e DockerExecutor) Execute(test TestCase) TestResult {
test.Result.Error = err
return TestResult{
TestCase: test,
}
}, nil
}

authConfig := types.AuthConfig{
Expand All @@ -60,10 +60,14 @@ func (e DockerExecutor) Execute(test TestCase) TestResult {
test.Result.Error = fmt.Errorf("could not pull image '%s' with error: '%s'", e.Image, err)
return TestResult{
TestCase: test,
}
}, nil
}
buf := bytes.Buffer{}
buf.ReadFrom(reader)
_, err = buf.ReadFrom(reader)
if err != nil {
return TestResult{}, fmt.Errorf("Error reading buffer in docker executor %w", err)
}

log.Printf("Pull log image'%s':\n %s\n", e.Image, buf.String())

var env []string
Expand All @@ -84,15 +88,15 @@ func (e DockerExecutor) Execute(test TestCase) TestResult {
test.Result.Error = fmt.Errorf("could not pull image '%s' with error: '%s'", e.Image, err)
return TestResult{
TestCase: test,
}
}, nil
}

log.Printf("Started container %s %s\n", e.Image, resp.ID)
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
test.Result.Error = fmt.Errorf("could not pull image '%s' with error: '%s'", e.Image, err)
return TestResult{
TestCase: test,
}
}, nil
}
duration := time.Duration(1 * time.Second)
defer cli.ContainerStop(ctx, resp.ID, &duration)
Expand Down Expand Up @@ -130,5 +134,5 @@ func (e DockerExecutor) Execute(test TestCase) TestResult {
log.Println("title: '"+test.Title+"'", " Stdout: ", test.Result.Stdout)
log.Println("title: '"+test.Title+"'", " Stderr: ", test.Result.Stderr)

return Validate(test)
return Validate(test), nil
}
2 changes: 1 addition & 1 deletion pkg/runtime/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package runtime

// Executor interface which will be implemented by all available executors, like ssh or local
type Executor interface {
Execute(test TestCase) TestResult
Execute(test TestCase) (TestResult, error)
}
8 changes: 4 additions & 4 deletions pkg/runtime/local_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ func NewLocalExecutor() Executor {
}

// Execute will execute the given test on the current node
func (e LocalExecutor) Execute(test TestCase) TestResult {
func (e LocalExecutor) Execute(test TestCase) (TestResult, error) {
timeoutOpt, err := createTimeoutOption(test.Command.Timeout)
if err != nil {
test.Result = CommandResult{Error: err}
return TestResult{
TestCase: test,
}
}, nil
}

envOpt := createEnvVarsOption(test)
Expand All @@ -47,7 +47,7 @@ func (e LocalExecutor) Execute(test TestCase) TestResult {

return TestResult{
TestCase: test,
}
}, nil
}

log.Println("title: '"+test.Title+"'", " Command: ", test.Command.Cmd)
Expand All @@ -65,7 +65,7 @@ func (e LocalExecutor) Execute(test TestCase) TestResult {
log.Println("title: '"+test.Title+"'", " Stdout: ", test.Result.Stdout)
log.Println("title: '"+test.Title+"'", " Stderr: ", test.Result.Stderr)

return Validate(test)
return Validate(test), nil
}

func createEnvVarsOption(test TestCase) func(c *cmd.Command) {
Expand Down
16 changes: 11 additions & 5 deletions pkg/runtime/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (r *Runner) Run(tests []TestCase) <-chan TestResult {
var wg sync.WaitGroup
wg.Add(1)


go func(tests chan TestCase) {
defer wg.Done()

Expand All @@ -36,19 +37,24 @@ func (r *Runner) Run(tests []TestCase) <-chan TestResult {
t.Nodes = []string{"local"}
}

for _, n := range t.Nodes {
for _, node := range t.Nodes {
result := TestResult{}
for i := 1; i <= t.Command.GetRetries(); i++ {

if t.Skip {
result = TestResult{TestCase: t, Skipped: true, Node: n}
result = TestResult{TestCase: t, Skipped: true, Node: node}
break
}

e := r.getExecutor(n)
result = e.Execute(t)
result.Node = n
e := r.getExecutor(node)
result, err := e.Execute(t)
if err != nil {
panic(fmt.Sprintf("[FATAL] %s [%s]: %s", t.Title, node, err.Error()))
}

result.Node = node
result.Tries = i
fmt.Println(result)

if result.ValidationResult.Success {
break
Expand Down
16 changes: 9 additions & 7 deletions pkg/runtime/ssh_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"net"
"strings"
"time"
)

var _ Executor = (*SSHExecutor)(nil)
Expand Down Expand Up @@ -49,7 +50,7 @@ func NewSSHExecutor(host string, user string, opts ...func(e *SSHExecutor)) Exec
}

// Execute executes a command on a remote host viá SSH
func (e SSHExecutor) Execute(test TestCase) TestResult {
func (e SSHExecutor) Execute(test TestCase) (TestResult, error) {
if test.Command.InheritEnv {
panic("Inherit env is not supported viá SSH")
}
Expand All @@ -72,19 +73,20 @@ func (e SSHExecutor) Execute(test TestCase) TestResult {
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
Timeout: 10*time.Second,
}

// create ssh connection
conn, err := ssh.Dial("tcp", e.Host, sshConf)
if err != nil {
log.Fatal(err)
return TestResult{}, fmt.Errorf("Failed to connect to ssh %w", err)
}

// start session
session, err := conn.NewSession()
defer session.Close()
if err != nil {
log.Fatal(err)
return TestResult{}, fmt.Errorf("Failed to create a new ssh session %w", err)
}

var stdoutBuffer bytes.Buffer
Expand All @@ -96,11 +98,11 @@ func (e SSHExecutor) Execute(test TestCase) TestResult {
err := session.Setenv(k, v)
if err != nil {
test.Result = CommandResult{
Error: fmt.Errorf("Failed setting env variables, maybe ssh server is configured to only accept LC_ prefixed env variables. Error: %s", err),
Error: fmt.Errorf("Failed setting env variables, maybe ssh server is configured to only accept LC_ prefixed env variables. %w", err),
}
return TestResult{
TestCase: test,
}
}, nil
}
}

Expand All @@ -125,7 +127,7 @@ func (e SSHExecutor) Execute(test TestCase) TestResult {

return TestResult{
TestCase: test,
}
}, nil
}

test.Result = CommandResult{
Expand All @@ -138,7 +140,7 @@ func (e SSHExecutor) Execute(test TestCase) TestResult {
log.Println("title: '"+test.Title+"'", " Stdout: ", test.Result.Stdout)
log.Println("title: '"+test.Title+"'", " Stderr: ", test.Result.Stderr)

return Validate(test)
return Validate(test), nil
}

func (e SSHExecutor) createSigner() ssh.Signer {
Expand Down

0 comments on commit c4b2ea1

Please sign in to comment.