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

Commit 1627192

Browse files
author
Yifan Gu
committed
e2e: Add simple cluster type to make tests migration easier.
Calling GetCluster() in each test can returns a Cluster type that stores the list of master and worker nodes, which provide SSH() and Reboot() interfaces.
1 parent 8dfb446 commit 1627192

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

e2e/main_test.go

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

10-
"golang.org/x/crypto/ssh"
11-
1210
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1311
"k8s.io/client-go/kubernetes"
1412
"k8s.io/client-go/pkg/api/v1"

e2e/node.go

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,27 @@ package e2e
33
import (
44
"fmt"
55

6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
67
"k8s.io/client-go/pkg/api/v1"
78

89
"golang.org/x/crypto/ssh"
910
)
1011

12+
const (
13+
LabelNodeRoleMaster = "node-role.kubernetes.io/master"
14+
)
15+
1116
type Node struct {
12-
apiNode *v1.Node
17+
*v1.Node
1318
}
1419

15-
func NewNode(n *v1.Node) *Node {
16-
return &Node{apiNode: n}
20+
func newNode(n *v1.Node) *Node {
21+
return &Node{n}
1722
}
1823

1924
func (n *Node) ExternalIP() string {
2025
var host string
21-
for _, addr := range n.apiNode.Status.Addresses {
26+
for _, addr := range n.Status.Addresses {
2227
if addr.Type == v1.NodeExternalIP {
2328
host = addr.Address
2429
break
@@ -30,7 +35,7 @@ func (n *Node) ExternalIP() string {
3035
func (n *Node) SSH(cmd string) (stdout, stderr []byte, err error) {
3136
host := n.ExternalIP()
3237
if host == "" {
33-
return nil, nil, fmt.Errorf("cannot find external IP for node %q", n.apiNode.Name)
38+
return nil, nil, fmt.Errorf("cannot find external IP for node %q", n.Name)
3439
}
3540
return sshClient.SSH(host, cmd)
3641
}
@@ -47,3 +52,35 @@ func (n *Node) Reboot() error {
4752
}
4853
return nil
4954
}
55+
56+
// IsMaster returns true if the node's labels contains "node-role.kubernetes.io/master".
57+
func (n *Node) IsMaster() bool {
58+
_, ok := n.Labels[LabelNodeRoleMaster]
59+
return ok
60+
}
61+
62+
// Cluster is a simple abstraction to make writing tests easier.
63+
type Cluster struct {
64+
Masters []*Node
65+
Workers []*Node
66+
}
67+
68+
// GetCluster can be called in every test to return a *Cluster object.
69+
func GetCluster() (*Cluster, error) {
70+
var c Cluster
71+
72+
nodelist, err := client.CoreV1().Nodes().List(metav1.ListOptions{})
73+
if err != nil {
74+
return nil, err
75+
}
76+
77+
for _, n := range nodelist.Items {
78+
nn := newNode(&n)
79+
if nn.IsMaster() {
80+
c.Masters = append(c.Masters, nn)
81+
} else {
82+
c.Workers = append(c.Workers, nn)
83+
}
84+
}
85+
return &c, nil
86+
}

e2e/ssh_client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ type SSHClient struct {
1919
}
2020

2121
func InitSSHClient(keypath string) {
22-
sshClient = NewSSHClientOrDie(keypath)
22+
sshClient = newSSHClientOrDie(keypath)
2323
}
2424

25-
// NewSSHClientOrDie tries to create an ssh client.
25+
// newSSHClientOrDie tries to create an ssh client.
2626
// If $SSH_AUTH_SOCK is set, the use the ssh agent to create the client,
2727
// otherwise read the private key directly.
28-
func NewSSHClientOrDie(keypath string) *SSHClient {
28+
func newSSHClientOrDie(keypath string) *SSHClient {
2929
var authMethod ssh.AuthMethod
3030

3131
sock := os.Getenv("SSH_AUTH_SOCK")

0 commit comments

Comments
 (0)