Skip to content

Commit 21097ee

Browse files
committed
Add test to check if ssh is working
The test checks if ssh is working on the guest by attempting to use the credentials generated by images verify to login into the guest and to run a simple command (e.g. 'echo hello'). Signed-off-by: Felix Matouschek <[email protected]>
1 parent e7d11a9 commit 21097ee

File tree

6 files changed

+65
-0
lines changed

6 files changed

+65
-0
lines changed

artifacts/centos/centos.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ func (c *centos) UserData(data *docs.UserData) string {
118118
func (c *centos) Tests() []api.ArtifactTest {
119119
return []api.ArtifactTest{
120120
tests.GuestOsInfo,
121+
tests.SSH,
121122
}
122123
}
123124

artifacts/centosstream/centos-stream.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ func (c *centos) UserData(data *docs.UserData) string {
106106
func (c *centos) Tests() []api.ArtifactTest {
107107
return []api.ArtifactTest{
108108
tests.GuestOsInfo,
109+
tests.SSH,
109110
}
110111
}
111112

artifacts/fedora/fedora.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func (f *fedora) UserData(data *docs.UserData) string {
8787
func (f *fedora) Tests() []api.ArtifactTest {
8888
return []api.ArtifactTest{
8989
tests.GuestOsInfo,
90+
tests.SSH,
9091
}
9192
}
9293

artifacts/rhcos/rhcos.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func (r *rhcos) UserData(data *docs.UserData) string {
7474
func (r *rhcos) Tests() []api.ArtifactTest {
7575
return []api.ArtifactTest{
7676
tests.GuestOsInfo,
77+
tests.SSH,
7778
}
7879
}
7980

artifacts/rhcosprerelease/rhcos.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func (r *rhcos) UserData(data *docs.UserData) string {
9292
func (r *rhcos) Tests() []api.ArtifactTest {
9393
return []api.ArtifactTest{
9494
tests.GuestOsInfo,
95+
tests.SSH,
9596
}
9697
}
9798

pkg/tests/ssh.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package tests
2+
3+
import (
4+
"fmt"
5+
6+
"golang.org/x/crypto/ssh"
7+
v1 "kubevirt.io/api/core/v1"
8+
kvirtcli "kubevirt.io/client-go/kubecli"
9+
"kubevirt.io/containerdisks/pkg/api"
10+
)
11+
12+
func SSH(vmi *v1.VirtualMachineInstance, params *api.ArtifactTestParams) error {
13+
kvirtClient, err := kvirtcli.GetKubevirtClient()
14+
if err != nil {
15+
return err
16+
}
17+
18+
signer, err := ssh.NewSignerFromKey(params.PrivateKey)
19+
if err != nil {
20+
return err
21+
}
22+
23+
config := &ssh.ClientConfig{
24+
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
25+
User: params.Username,
26+
Auth: []ssh.AuthMethod{
27+
ssh.PublicKeys(signer),
28+
},
29+
}
30+
31+
return retryTest(func() error {
32+
return testSSH(vmi, kvirtClient, config)
33+
})
34+
}
35+
36+
func testSSH(vmi *v1.VirtualMachineInstance, kvirtClient kvirtcli.KubevirtClient, config *ssh.ClientConfig) error {
37+
tunnel, err := kvirtClient.VirtualMachineInstance(vmi.Namespace).PortForward(vmi.Name, 22, "tcp")
38+
if err != nil {
39+
return fmt.Errorf("failed to forward ssh port: %w", err)
40+
}
41+
42+
conn := tunnel.AsConn()
43+
addr := fmt.Sprintf("vmi/%s.%s:22", vmi.Name, vmi.Namespace)
44+
sshConn, chans, reqs, err := ssh.NewClientConn(conn, addr, config)
45+
if err != nil {
46+
return err
47+
}
48+
49+
session, err := ssh.NewClient(sshConn, chans, reqs).NewSession()
50+
if err != nil {
51+
return err
52+
}
53+
54+
err = session.Run("echo hello")
55+
if err != nil {
56+
return err
57+
}
58+
59+
return nil
60+
}

0 commit comments

Comments
 (0)