Skip to content

Commit c77f9d3

Browse files
SimonBaeumerdylanhitt
authored andcommitted
Add error handling to executors
1 parent 714326d commit c77f9d3

5 files changed

Lines changed: 36 additions & 24 deletions

File tree

pkg/runtime/docker_executor.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type DockerExecutor struct {
2828
}
2929

3030
// Execute executes the script inside a docker container
31-
func (e DockerExecutor) Execute(test TestCase) TestResult {
31+
func (e DockerExecutor) Execute(test TestCase) (TestResult, error) {
3232
log.Printf("DOCKER_HOST: %s \n", os.Getenv("DOCKER_HOST"))
3333
log.Printf("DOCKER_CERT_PATH: %s \n", os.Getenv("DOCKER_CERT_PATH"))
3434
log.Printf("DOCKER_API_VERSION: %s \n", os.Getenv("DOCKER_API_VERSION"))
@@ -39,7 +39,7 @@ func (e DockerExecutor) Execute(test TestCase) TestResult {
3939
test.Result.Error = err
4040
return TestResult{
4141
TestCase: test,
42-
}
42+
}, nil
4343
}
4444

4545
authConfig := types.AuthConfig{
@@ -60,10 +60,14 @@ func (e DockerExecutor) Execute(test TestCase) TestResult {
6060
test.Result.Error = fmt.Errorf("could not pull image '%s' with error: '%s'", e.Image, err)
6161
return TestResult{
6262
TestCase: test,
63-
}
63+
}, nil
6464
}
6565
buf := bytes.Buffer{}
66-
buf.ReadFrom(reader)
66+
_, err = buf.ReadFrom(reader)
67+
if err != nil {
68+
return TestResult{}, fmt.Errorf("Error reading buffer in docker executor %w", err)
69+
}
70+
6771
log.Printf("Pull log image'%s':\n %s\n", e.Image, buf.String())
6872

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

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

133-
return Validate(test)
137+
return Validate(test), nil
134138
}

pkg/runtime/executor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ package runtime
22

33
// Executor interface which will be implemented by all available executors, like ssh or local
44
type Executor interface {
5-
Execute(test TestCase) TestResult
5+
Execute(test TestCase) (TestResult, error)
66
}

pkg/runtime/local_executor.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ func NewLocalExecutor() Executor {
2121
}
2222

2323
// Execute will execute the given test on the current node
24-
func (e LocalExecutor) Execute(test TestCase) TestResult {
24+
func (e LocalExecutor) Execute(test TestCase) (TestResult, error) {
2525
timeoutOpt, err := createTimeoutOption(test.Command.Timeout)
2626
if err != nil {
2727
test.Result = CommandResult{Error: err}
2828
return TestResult{
2929
TestCase: test,
30-
}
30+
}, nil
3131
}
3232

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

4848
return TestResult{
4949
TestCase: test,
50-
}
50+
}, nil
5151
}
5252

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

68-
return Validate(test)
68+
return Validate(test), nil
6969
}
7070

7171
func createEnvVarsOption(test TestCase) func(c *cmd.Command) {

pkg/runtime/runner.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func (r *Runner) Run(tests []TestCase) <-chan TestResult {
2727
var wg sync.WaitGroup
2828
wg.Add(1)
2929

30+
3031
go func(tests chan TestCase) {
3132
defer wg.Done()
3233

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

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

4344
if t.Skip {
44-
result = TestResult{TestCase: t, Skipped: true, Node: n}
45+
result = TestResult{TestCase: t, Skipped: true, Node: node}
4546
break
4647
}
4748

48-
e := r.getExecutor(n)
49-
result = e.Execute(t)
50-
result.Node = n
49+
e := r.getExecutor(node)
50+
result, err := e.Execute(t)
51+
if err != nil {
52+
panic(fmt.Sprintf("[FATAL] %s [%s]: %s", t.Title, node, err.Error()))
53+
}
54+
55+
result.Node = node
5156
result.Tries = i
57+
fmt.Println(result)
5258

5359
if result.ValidationResult.Success {
5460
break

pkg/runtime/ssh_executor.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99
"net"
1010
"strings"
11+
"time"
1112
)
1213

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

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

7779
// create ssh connection
7880
conn, err := ssh.Dial("tcp", e.Host, sshConf)
7981
if err != nil {
80-
log.Fatal(err)
82+
return TestResult{}, fmt.Errorf("Failed to connect to ssh %w", err)
8183
}
8284

8385
// start session
8486
session, err := conn.NewSession()
8587
defer session.Close()
8688
if err != nil {
87-
log.Fatal(err)
89+
return TestResult{}, fmt.Errorf("Failed to create a new ssh session %w", err)
8890
}
8991

9092
var stdoutBuffer bytes.Buffer
@@ -96,11 +98,11 @@ func (e SSHExecutor) Execute(test TestCase) TestResult {
9698
err := session.Setenv(k, v)
9799
if err != nil {
98100
test.Result = CommandResult{
99-
Error: fmt.Errorf("Failed setting env variables, maybe ssh server is configured to only accept LC_ prefixed env variables. Error: %s", err),
101+
Error: fmt.Errorf("Failed setting env variables, maybe ssh server is configured to only accept LC_ prefixed env variables. %w", err),
100102
}
101103
return TestResult{
102104
TestCase: test,
103-
}
105+
}, nil
104106
}
105107
}
106108

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

126128
return TestResult{
127129
TestCase: test,
128-
}
130+
}, nil
129131
}
130132

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

141-
return Validate(test)
143+
return Validate(test), nil
142144
}
143145

144146
func (e SSHExecutor) createSigner() ssh.Signer {

0 commit comments

Comments
 (0)