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

Commit cc7a9b3

Browse files
author
Yifan Gu
committed
e2e: Add a simple ssh client.
The client has the same interface as the one in pluton framework, makes it easier for us to migrate tests.
1 parent 38f60ec commit cc7a9b3

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

e2e/smoke_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestSmoke(t *testing.T) {
1919
}
2020
d, ok := di.(*v1beta1.Deployment)
2121
if !ok {
22-
t.Fatalf("expected manifest to decode into *api.Service, got %T", di)
22+
t.Fatalf("expected manifest to decode into *api.deployment, got %T", di)
2323
}
2424
_, err = client.ExtensionsV1beta1().Deployments(namespace).Create(d)
2525
if err != nil {

e2e/ssh_client.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package e2e
2+
3+
import (
4+
"bytes"
5+
"io/ioutil"
6+
"log"
7+
"net"
8+
"os"
9+
10+
"golang.org/x/crypto/ssh"
11+
"golang.org/x/crypto/ssh/agent"
12+
)
13+
14+
type SSHClient struct {
15+
*ssh.ClientConfig
16+
}
17+
18+
// NewSSHClientOrDie tries to create an ssh client.
19+
// If $SSH_AUTH_SOCK is set, the use the ssh agent to create the client,
20+
// otherwise read the private key directly.
21+
func NewSSHClientOrDie(keypath string) *SSHClient {
22+
var authMethod ssh.AuthMethod
23+
24+
sock := os.Getenv("SSH_AUTH_SOCK")
25+
if sock != "" {
26+
log.Println("Creating ssh client with ssh agent")
27+
sshAgent, err := net.Dial("unix", sock)
28+
if err != nil {
29+
panic(err)
30+
}
31+
32+
authMethod = ssh.PublicKeysCallback(agent.NewClient(sshAgent).Signers)
33+
} else {
34+
log.Println("Creating ssh client with private key")
35+
key, err := ioutil.ReadFile(keypath)
36+
if err != nil {
37+
panic(err)
38+
}
39+
40+
signer, err := ssh.ParsePrivateKey(key)
41+
if err != nil {
42+
panic(err)
43+
}
44+
45+
authMethod = ssh.PublicKeys(signer)
46+
}
47+
48+
sshConfig := &ssh.ClientConfig{
49+
User: "core", // TODO(yifan): Assume all nodes are container linux nodes for now.
50+
Auth: []ssh.AuthMethod{authMethod},
51+
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
52+
}
53+
54+
return &SSHClient{sshConfig}
55+
}
56+
57+
func (c *SSHClient) SSH(host, cmd string) (stdout, stderr []byte, err error) {
58+
client, err := ssh.Dial("tcp", host+":22", c.ClientConfig) // TODO(yifan): Assume all nodes are listening on :22 for ssh requests for now.
59+
if err != nil {
60+
return nil, nil, err
61+
}
62+
defer client.Conn.Close()
63+
64+
session, err := client.NewSession()
65+
if err != nil {
66+
return nil, nil, err
67+
}
68+
defer session.Close()
69+
70+
outBuf := bytes.NewBuffer(nil)
71+
errBuf := bytes.NewBuffer(nil)
72+
session.Stdout = outBuf
73+
session.Stderr = errBuf
74+
75+
err = session.Run(cmd)
76+
77+
stdout = bytes.TrimSpace(outBuf.Bytes())
78+
stderr = bytes.TrimSpace(errBuf.Bytes())
79+
80+
return stdout, stderr, err
81+
}

0 commit comments

Comments
 (0)