Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nil-pointer exception in SSH authentication #181

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions pkg/runtime/docker_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,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 @@ -40,7 +40,7 @@ func (e DockerExecutor) Execute(test TestCase) TestResult {
test.Result.Error = err
return TestResult{
TestCase: test,
}
}, nil
}

authConfig := types.AuthConfig{
Expand All @@ -61,10 +61,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 @@ -86,15 +90,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)
Expand Down Expand Up @@ -140,5 +144,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
9 changes: 6 additions & 3 deletions pkg/runtime/local_executor_darwin_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package runtime

import (
"github.com/stretchr/testify/assert"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRuntime_WithInheritFromShell(t *testing.T) {
Expand All @@ -18,7 +19,8 @@ func TestRuntime_WithInheritFromShell(t *testing.T) {
}

e := LocalExecutor{}
got := e.Execute(test)
got, err := e.Execute(test)
assert.NoError(t, err)

assert.Equal(t, "test", got.TestCase.Result.Stdout)
}
Expand All @@ -40,7 +42,8 @@ func TestRuntime_WithInheritFromShell_Overwrite(t *testing.T) {
}

e := LocalExecutor{}
got := e.Execute(test)
got, err := e.Execute(test)
assert.NoError(t, err)

assert.Equal(t, "overwrite from-parent", got.TestCase.Result.Stdout)
}
12 changes: 8 additions & 4 deletions pkg/runtime/local_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package runtime

import (
"fmt"
"github.com/stretchr/testify/assert"
"runtime"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRuntime_WithEnvVariables(t *testing.T) {
Expand All @@ -29,7 +30,8 @@ func TestRuntime_WithEnvVariables(t *testing.T) {
}

e := LocalExecutor{}
got := e.Execute(s)
got, err := e.Execute(s)
assert.NoError(t, err)
assert.True(t, got.ValidationResult.Success)
}

Expand All @@ -42,7 +44,8 @@ func Test_runTestShouldReturnError(t *testing.T) {
}

e := LocalExecutor{}
got := e.Execute(test)
got, err := e.Execute(test)
assert.NoError(t, err)

if runtime.GOOS == "windows" {
assert.Contains(t, got.TestCase.Result.Error.Error(), "chdir /home/invalid")
Expand All @@ -60,7 +63,8 @@ func TestRuntime_WithInvalidDuration(t *testing.T) {
}

e := LocalExecutor{}
got := e.Execute(test)
got, err := e.Execute(test)
assert.NoError(t, err)

assert.Equal(t, `time: unknown unit "lightyears" in duration "600lightyears"`, got.TestCase.Result.Error.Error())
}
14 changes: 9 additions & 5 deletions pkg/runtime/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,22 @@ 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

if result.ValidationResult.Success {
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