Skip to content

Commit 51f038b

Browse files
SimonBaeumerdylanhitt
authored andcommitted
Add error handling to executors
1 parent 58622c2 commit 51f038b

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
@@ -29,7 +29,7 @@ type DockerExecutor struct {
2929
}
3030

3131
// Execute executes the script inside a docker container
32-
func (e DockerExecutor) Execute(test TestCase) TestResult {
32+
func (e DockerExecutor) Execute(test TestCase) (TestResult, error) {
3333
log.Printf("DOCKER_HOST: %s \n", os.Getenv("DOCKER_HOST"))
3434
log.Printf("DOCKER_CERT_PATH: %s \n", os.Getenv("DOCKER_CERT_PATH"))
3535
log.Printf("DOCKER_API_VERSION: %s \n", os.Getenv("DOCKER_API_VERSION"))
@@ -40,7 +40,7 @@ func (e DockerExecutor) Execute(test TestCase) TestResult {
4040
test.Result.Error = err
4141
return TestResult{
4242
TestCase: test,
43-
}
43+
}, nil
4444
}
4545

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

7074
var env []string
@@ -86,15 +90,15 @@ func (e DockerExecutor) Execute(test TestCase) TestResult {
8690
test.Result.Error = fmt.Errorf("could not pull image '%s' with error: '%s'", e.Image, err)
8791
return TestResult{
8892
TestCase: test,
89-
}
93+
}, nil
9094
}
9195

9296
log.Printf("Started container %s %s\n", e.Image, resp.ID)
9397
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
9498
test.Result.Error = fmt.Errorf("could not pull image '%s' with error: '%s'", e.Image, err)
9599
return TestResult{
96100
TestCase: test,
97-
}
101+
}, nil
98102
}
99103

100104
duration := time.Duration(1 * time.Second)
@@ -140,5 +144,5 @@ func (e DockerExecutor) Execute(test TestCase) TestResult {
140144
log.Println("title: '"+test.Title+"'", " Stdout: ", test.Result.Stdout)
141145
log.Println("title: '"+test.Title+"'", " Stderr: ", test.Result.Stderr)
142146

143-
return Validate(test)
147+
return Validate(test), nil
144148
}

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)