Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit 60da9fb

Browse files
author
Yifan Gu
committed
e2e: Add node.go to abstract node.SSH() and node.Reboot().
All nodes will share a global ssh client, which can be initialized during the start of the tests by giving the path to the private key.
1 parent cc7a9b3 commit 60da9fb

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

e2e/main_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"testing"
88
"time"
99

10+
"golang.org/x/crypto/ssh"
11+
1012
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1113
"k8s.io/client-go/kubernetes"
1214
"k8s.io/client-go/pkg/api/v1"

e2e/node.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package e2e
2+
3+
import (
4+
"fmt"
5+
6+
"k8s.io/client-go/pkg/api/v1"
7+
8+
"golang.org/x/crypto/ssh"
9+
)
10+
11+
type Node struct {
12+
apiNode *v1.Node
13+
}
14+
15+
func NewNode(n *v1.Node) *Node {
16+
return &Node{apiNode: n}
17+
}
18+
19+
func (n *Node) ExternalIP() string {
20+
var host string
21+
for _, addr := range n.apiNode.Status.Addresses {
22+
if addr.Type == v1.NodeExternalIP {
23+
host = addr.Address
24+
break
25+
}
26+
}
27+
return host
28+
}
29+
30+
func (n *Node) SSH(cmd string) (stdout, stderr []byte, err error) {
31+
host := n.ExternalIP()
32+
if host == "" {
33+
return nil, nil, fmt.Errorf("cannot find external IP for node %q", n.apiNode.Name)
34+
}
35+
return sshClient.SSH(host, cmd)
36+
}
37+
38+
func (n *Node) Reboot() error {
39+
stdout, stderr, err := n.SSH("sudo reboot")
40+
if _, ok := err.(*ssh.ExitMissingError); ok {
41+
// A terminated session is perfectly normal during reboot.
42+
err = nil
43+
}
44+
45+
if err != nil {
46+
return fmt.Errorf("issuing reboot command failed\nstdout:%s\nstderr:%s", stdout, stderr)
47+
}
48+
return nil
49+
}

e2e/ssh_client.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ import (
1111
"golang.org/x/crypto/ssh/agent"
1212
)
1313

14+
// The global SSH client used by the tests.
15+
var sshClient *SSHClient
16+
1417
type SSHClient struct {
1518
*ssh.ClientConfig
1619
}
1720

21+
func InitSSHClient(keypath string) {
22+
sshClient = NewSSHClientOrDie(keypath)
23+
}
24+
1825
// NewSSHClientOrDie tries to create an ssh client.
1926
// If $SSH_AUTH_SOCK is set, the use the ssh agent to create the client,
2027
// otherwise read the private key directly.

0 commit comments

Comments
 (0)