diff --git a/tests/utils/ssh/ssh.go b/tests/utils/ssh/ssh.go index a253d6c8f..b0937b48b 100644 --- a/tests/utils/ssh/ssh.go +++ b/tests/utils/ssh/ssh.go @@ -18,30 +18,10 @@ package ssh import ( "log" - "os" "os/exec" "strings" ) -const ( - sshKeyOptPath = "-i /root/.ssh/id_rsa" -) - -// InvokeCommand - can be consumed by test directly to invoke -// any command on the remote host. -// ip: remote machine address to execute on the machine -// cmd: A command string to be executed on the remote host as per -func InvokeCommand(ip, cmd string) (string, error) { - sshKeyOpt := strings.Split(os.Getenv("SSH_KEY_OPT"), " ") - if sshKeyOpt == nil { - sshKeyOpt = strings.Split(sshKeyOptPath, " ") - } - sshIdentity := []string{sshKeyOpt[0], sshKeyOpt[1], "-q", "-kTax", "-o StrictHostKeyChecking=no"} - - out, err := exec.Command("/usr/bin/ssh", append(sshIdentity, "root@"+ip, cmd)...).CombinedOutput() - return strings.TrimSpace(string(out[:])), err -} - // InvokeCommandLocally - can be consumed by test directly to invoke // any command locally. // cmd: A command string to be executed on the remote host as per diff --git a/tests/utils/ssh/ssh_win.go b/tests/utils/ssh/ssh_win.go new file mode 100644 index 000000000..9b6b255c0 --- /dev/null +++ b/tests/utils/ssh/ssh_win.go @@ -0,0 +1,39 @@ +// Copyright 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Exposes util to invoke remote commands on windows hosts via ssh. + +// +build winutil + +package ssh + +import ( + "fmt" + "os/exec" + "strings" +) + +// sshTemplate is the ssh command template for Windows hosts. +const sshTemplate = "/usr/bin/ssh -q -o StrictHostKeyChecking=no root@%s '%s'; exit" + +// InvokeCommand invokes the given command on the given host via ssh. +func InvokeCommand(ip, cmdStr string) (string, error) { + // OpenSSH sessions terminate sporadically when a pty isn't allocated. + // The -t flag doesn't work with OpenSSH on Windows, so we wrap the ssh call + // within a bash session as a workaround, so that a pty is created. + cmd := exec.Command("/bin/bash") + cmd.Stdin = strings.NewReader(fmt.Sprintf(sshTemplate, ip, cmdStr)) + out, err := cmd.CombinedOutput() + return strings.TrimSpace(string(out[:])), err +} diff --git a/tests/utils/ssh/sshdefault.go b/tests/utils/ssh/sshdefault.go new file mode 100644 index 000000000..bafb9a82e --- /dev/null +++ b/tests/utils/ssh/sshdefault.go @@ -0,0 +1,43 @@ +// Copyright 2017 VMware, Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Exposes util to invoke remote commands on non-windows hosts via ssh. + +// +build !winutil + +package ssh + +import ( + "os" + "os/exec" + "strings" +) + +// sshKeyOptPath is the option to specify the rsa key while connecting via ssh. +const sshKeyOptPath = "-i /root/.ssh/id_rsa" + +// InvokeCommand - can be consumed by test directly to invoke +// any command on the remote host. +// ip: remote machine address to execute on the machine +// cmd: A command string to be executed on the remote host as per +func InvokeCommand(ip, cmd string) (string, error) { + sshKeyOpt := strings.Split(os.Getenv("SSH_KEY_OPT"), " ") + if sshKeyOpt == nil { + sshKeyOpt = strings.Split(sshKeyOptPath, " ") + } + sshIdentity := []string{sshKeyOpt[0], sshKeyOpt[1], "-q", "-kTax", "-o StrictHostKeyChecking=no"} + + out, err := exec.Command("/usr/bin/ssh", append(sshIdentity, "root@"+ip, cmd)...).CombinedOutput() + return strings.TrimSpace(string(out[:])), err +}