From db4a743b777d0b0b3a48c8e2bd0b33c85871acbb Mon Sep 17 00:00:00 2001 From: shuheiktgw Date: Tue, 23 Nov 2021 17:20:42 +0900 Subject: [PATCH] Check the private SSH key existence in the node e2e tests --- test/e2e_node/remote/ssh.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/test/e2e_node/remote/ssh.go b/test/e2e_node/remote/ssh.go index eefa1000924..63a72557b11 100644 --- a/test/e2e_node/remote/ssh.go +++ b/test/e2e_node/remote/ssh.go @@ -103,9 +103,12 @@ func SSHNoSudo(host string, cmd ...string) (string, error) { // runSSHCommand executes the ssh or scp command, adding the flag provided --ssh-options func runSSHCommand(cmd string, args ...string) (string, error) { - if *sshKey != "" { - args = append([]string{"-i", *sshKey}, args...) - } else if key, found := sshDefaultKeyMap[*sshEnv]; found { + if key := getPrivateSSHKey(); len(key) != 0 { + if _, err := os.Stat(key); err != nil { + klog.Errorf("private SSH key (%s) not found. Check if the SSH key is configured properly:, err: %v", key, err) + return "", fmt.Errorf("private SSH key (%s) does not exist", key) + } + args = append([]string{"-i", key}, args...) } if env, found := sshOptionsMap[*sshEnv]; found { @@ -122,3 +125,16 @@ func runSSHCommand(cmd string, args ...string) (string, error) { } return string(output), nil } + +// getPrivateSSHKey returns the path to ssh private key +func getPrivateSSHKey() string { + if *sshKey != "" { + return *sshKey + } + + if key, found := sshDefaultKeyMap[*sshEnv]; found { + return key + } + + return "" +}