Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit be7ba74

Browse files
committedApr 13, 2022
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 <fmatouschek@redhat.com>
1 parent 8d4b3db commit be7ba74

File tree

6 files changed

+66
-0
lines changed

6 files changed

+66
-0
lines changed
 

‎artifacts/centos/centos.go

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

‎artifacts/centosstream/centos-stream.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func (c *centos) UserData(data *docs.UserData) string {
101101
func (c *centos) Tests() []api.ArtifactTest {
102102
return []api.ArtifactTest{
103103
tests.GuestOsInfo,
104+
tests.SSH,
104105
}
105106
}
106107

‎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
@@ -73,6 +73,7 @@ func (r *rhcos) UserData(data *docs.UserData) string {
7373
func (r *rhcos) Tests() []api.ArtifactTest {
7474
return []api.ArtifactTest{
7575
tests.GuestOsInfo,
76+
tests.SSH,
7677
}
7778
}
7879

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

0 commit comments

Comments
 (0)
Failed to load comments.