forked from ovh/venom
/
executor.go
171 lines (147 loc) · 4.16 KB
/
executor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package ssh
import (
"bytes"
"fmt"
"io/ioutil"
"os/user"
"strconv"
"strings"
"time"
"github.com/fsamin/go-dump"
"github.com/mitchellh/mapstructure"
"golang.org/x/crypto/ssh"
"github.com/runabove/venom"
)
// Name for test ssh
const Name = "ssh"
// New returns a new Test Exec
func New() venom.Executor {
return &Executor{}
}
// Executor represents a Test Exec
type Executor struct {
Host string `json:"host,omitempty" yaml:"host,omitempty"`
Command string `json:"command,omitempty" yaml:"command,omitempty"`
User string `json:"user,omitempty" yaml:"user,omitempty"`
Password string `json:"password,omitempty" yaml:"password,omitempty"`
PrivateKey string `json:"privatekey,omitempty" yaml:"privatekey,omitempty"`
}
// Result represents a step result
type Result struct {
Executor Executor `json:"executor,omitempty" yaml:"executor,omitempty"`
Systemout string `json:"systemout,omitempty" yaml:"systemout,omitempty"`
Systemerr string `json:"systemerr,omitempty" yaml:"systemerr,omitempty"`
Err string `json:"err,omitempty" yaml:"err,omitempty"`
Code string `json:"code,omitempty" yaml:"code,omitempty"`
TimeSeconds float64 `json:"timeseconds,omitempty" yaml:"timeseconds,omitempty"`
TimeHuman string `json:"timehuman,omitempty" yaml:"timehuman,omitempty"`
}
// GetDefaultAssertions return default assertions for type exec
func (Executor) GetDefaultAssertions() *venom.StepAssertions {
return &venom.StepAssertions{Assertions: []string{"result.code ShouldEqual 0"}}
}
// Run execute TestStep of type exec
func (Executor) Run(testCaseContext venom.TestCaseContext, l venom.Logger, step venom.TestStep) (venom.ExecutorResult, error) {
var t Executor
if err := mapstructure.Decode(step, &t); err != nil {
return nil, err
}
if t.Command == "" {
return nil, fmt.Errorf("Invalid command")
}
start := time.Now()
result := Result{Executor: t}
client, session, err := connectToHost(t.User, t.Password, t.PrivateKey, t.Host)
if err != nil {
result.Err = err.Error()
} else {
defer client.Close()
stdout := &bytes.Buffer{}
stderr := &bytes.Buffer{}
session.Stderr = stderr
session.Stdout = stdout
if err := session.Run(t.Command); err != nil {
if exiterr, ok := err.(*ssh.ExitError); ok {
status := exiterr.ExitStatus()
result.Code = strconv.Itoa(status)
} else if _, ok := err.(*ssh.ExitMissingError); ok {
result.Code = strconv.Itoa(127)
result.Err = err.Error()
} else {
result.Code = strconv.Itoa(137)
result.Err = err.Error()
}
} else {
result.Code = "0"
}
result.Systemerr = stderr.String()
result.Systemout = stdout.String()
}
elapsed := time.Since(start)
result.TimeSeconds = elapsed.Seconds()
result.TimeHuman = fmt.Sprintf("%s", elapsed)
return dump.ToMap(result, dump.WithDefaultLowerCaseFormatter())
}
func connectToHost(u, pass, key, host string) (*ssh.Client, *ssh.Session, error) {
//Default user is current username
if u == "" {
osUser, err := user.Current()
if err != nil {
return nil, nil, err
}
u = osUser.Username
}
//If password is set, use it
var auth []ssh.AuthMethod
if pass != "" {
auth = []ssh.AuthMethod{ssh.Password(pass)}
} else {
//Load the the private key
key, err := privateKey(key)
if err != nil {
return nil, nil, err
}
auth = []ssh.AuthMethod{ssh.PublicKeys(key)}
}
sshConfig := &ssh.ClientConfig{
User: u,
Auth: auth,
}
//If host doen't contain port, set the default port
if !strings.Contains(host, ":") {
host += ":22"
}
//Open the tcp connection
client, err := ssh.Dial("tcp", host, sshConfig)
if err != nil {
return nil, nil, err
}
//New ssh session
session, err := client.NewSession()
if err != nil {
client.Close()
return nil, nil, err
}
return client, session, nil
}
func privateKey(file string) (key ssh.Signer, err error) {
//Default private key is $HOME/.ssh/id_rsa
if file == "" {
usr, err := user.Current()
if err != nil {
return nil, err
}
file = usr.HomeDir + "/.ssh/id_rsa"
}
//Read the file
buf, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
//Parse it
key, err = ssh.ParsePrivateKey(buf)
if err != nil {
return nil, err
}
return key, nil
}