tests/image: pass the ssh key from boot types

Soon, images will be run non-locally (AWS, Azure). For the remote ones it's
potentially dangerous to use the publicly available key-pair. This change
prepares the codebase for specifying different keys than the pre-generated
one.
This commit is contained in:
Ondřej Budai 2020-04-02 11:04:37 +02:00 committed by Tom Gundersen
parent 1a8379f2cd
commit 640a9fcd92

View file

@ -125,14 +125,14 @@ func (*timeoutError) Error() string { return "" }
// that 10 seconds or if systemd-is-running returns starting. // that 10 seconds or if systemd-is-running returns starting.
// It returns nil if systemd-is-running returns running or degraded. // It returns nil if systemd-is-running returns running or degraded.
// It can also return other errors in other error cases. // It can also return other errors in other error cases.
func trySSHOnce(address string, ns *netNS) error { func trySSHOnce(address string, privateKey string, ns *netNS) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
cmdName := "ssh" cmdName := "ssh"
cmdArgs := []string{ cmdArgs := []string{
"-p", "22", "-p", "22",
"-i", privateKeyPath, "-i", privateKey,
"-o", "StrictHostKeyChecking=no", "-o", "StrictHostKeyChecking=no",
"-o", "UserKnownHostsFile=/dev/null", "-o", "UserKnownHostsFile=/dev/null",
"redhat@" + address, "redhat@" + address,
@ -180,10 +180,10 @@ func trySSHOnce(address string, ns *netNS) error {
// testSSH tests the running image using ssh. // testSSH tests the running image using ssh.
// It tries 20 attempts before giving up. If a major error occurs, it might // It tries 20 attempts before giving up. If a major error occurs, it might
// return earlier. // return earlier.
func testSSH(t *testing.T, address string, ns *netNS) { func testSSH(t *testing.T, address string, privateKey string, ns *netNS) {
const attempts = 20 const attempts = 20
for i := 0; i < attempts; i++ { for i := 0; i < attempts; i++ {
err := trySSHOnce(address, ns) err := trySSHOnce(address, privateKey, ns)
if err == nil { if err == nil {
// pass the test // pass the test
return return
@ -212,7 +212,7 @@ func testBoot(t *testing.T, imagePath string, bootType string, outputID string)
case "qemu-extract": case "qemu-extract":
err := withNetworkNamespace(func(ns netNS) error { err := withNetworkNamespace(func(ns netNS) error {
return withBootedQemuImage(imagePath, ns, func() error { return withBootedQemuImage(imagePath, ns, func() error {
testSSH(t, "localhost", &ns) testSSH(t, "localhost", privateKeyPath, &ns)
return nil return nil
}) })
}) })
@ -221,7 +221,7 @@ func testBoot(t *testing.T, imagePath string, bootType string, outputID string)
case "nspawn": case "nspawn":
err := withNetworkNamespace(func(ns netNS) error { err := withNetworkNamespace(func(ns netNS) error {
return withBootedNspawnImage(imagePath, outputID, ns, func() error { return withBootedNspawnImage(imagePath, outputID, ns, func() error {
testSSH(t, "localhost", &ns) testSSH(t, "localhost", privateKeyPath, &ns)
return nil return nil
}) })
}) })
@ -231,7 +231,7 @@ func testBoot(t *testing.T, imagePath string, bootType string, outputID string)
err := withNetworkNamespace(func(ns netNS) error { err := withNetworkNamespace(func(ns netNS) error {
return withExtractedTarArchive(imagePath, func(dir string) error { return withExtractedTarArchive(imagePath, func(dir string) error {
return withBootedNspawnDirectory(dir, outputID, ns, func() error { return withBootedNspawnDirectory(dir, outputID, ns, func() error {
testSSH(t, "localhost", &ns) testSSH(t, "localhost", privateKeyPath, &ns)
return nil return nil
}) })
}) })