Skip to content

Commit 3902613

Browse files
xaionaroinsomniacslk
authored andcommitted
Add flag "skip_if_empty_host" to teststep "sshcmd"
The flag allows to consider a target successful even if "host" field is empty (and ssh connection cannot be performed). It could be used if sshcmd should be executed not for all hosts.
1 parent 4ecee91 commit 3902613

1 file changed

Lines changed: 30 additions & 10 deletions

File tree

plugins/teststeps/sshcmd/sshcmd.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,16 @@ const defaultTimeoutParameter = "10m"
5454

5555
// SSHCmd is used to run arbitrary commands as test steps.
5656
type SSHCmd struct {
57-
Host *test.Param
58-
Port *test.Param
59-
User *test.Param
60-
PrivateKeyFile *test.Param
61-
Password *test.Param
62-
Executable *test.Param
63-
Args []test.Param
64-
Expect *test.Param
65-
Timeout *test.Param
57+
Host *test.Param
58+
Port *test.Param
59+
User *test.Param
60+
PrivateKeyFile *test.Param
61+
Password *test.Param
62+
Executable *test.Param
63+
Args []test.Param
64+
Expect *test.Param
65+
Timeout *test.Param
66+
SkipIfEmptyHost *test.Param
6667
}
6768

6869
// Name returns the plugin name.
@@ -96,6 +97,23 @@ func (ts *SSHCmd) Run(ctx statectx.Context, ch test.TestStepChannels, params tes
9697
return fmt.Errorf("cannot expand host parameter: %v", err)
9798
}
9899

100+
if len(host) == 0 {
101+
shouldSkip := false
102+
if !ts.SkipIfEmptyHost.IsEmpty() {
103+
var err error
104+
shouldSkip, err = strconv.ParseBool(ts.SkipIfEmptyHost.String())
105+
if err != nil {
106+
return fmt.Errorf("cannot expand 'skip_if_empty_host' parameter value '%s': %w", ts.SkipIfEmptyHost, err)
107+
}
108+
}
109+
110+
if shouldSkip {
111+
return nil
112+
} else {
113+
return fmt.Errorf("host value is empty")
114+
}
115+
}
116+
99117
portStr, err := ts.Port.Expand(target)
100118
if err != nil {
101119
return fmt.Errorf("cannot expand port parameter: %v", err)
@@ -172,7 +190,7 @@ func (ts *SSHCmd) Run(ctx statectx.Context, ch test.TestStepChannels, params tes
172190
}
173191

174192
// connect to the host
175-
addr := net.JoinHostPort(host, strconv.Itoa(int(port)))
193+
addr := net.JoinHostPort(host, strconv.Itoa(port))
176194
client, err := ssh.Dial("tcp", addr, &config)
177195
if err != nil {
178196
return fmt.Errorf("cannot connect to SSH server %s: %v", addr, err)
@@ -300,6 +318,8 @@ func (ts *SSHCmd) validateAndPopulate(params test.TestStepParameters) error {
300318
} else {
301319
ts.Timeout = params.GetOne("timeout")
302320
}
321+
322+
ts.SkipIfEmptyHost = params.GetOne("skip_if_empty_host")
303323
return nil
304324
}
305325

0 commit comments

Comments
 (0)