tests/image: allow to run ssh without network namespace

Soon, images will be run non-locally (AWS, Azure). For this, we cannot
use ssh in a namespaced environment. This commit makes the namespace for ssh
optional.
This commit is contained in:
Ondřej Budai 2020-03-27 12:58:50 +01:00 committed by Tom Gundersen
parent e593bceede
commit 7baacda6ef

View file

@ -125,19 +125,28 @@ func (*timeoutError) Error() string { return "" }
// that 10 seconds or if systemd-is-running returns starting.
// It returns nil if systemd-is-running returns running or degraded.
// It can also return other errors in other error cases.
func trySSHOnce(ns netNS) error {
func trySSHOnce(ns *netNS) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
cmd := ns.NamespacedCommandContext(
ctx,
"ssh",
cmdName := "ssh"
cmdArgs := []string{
"-p", "22",
"-i", privateKeyPath,
"-o", "StrictHostKeyChecking=no",
"-o", "UserKnownHostsFile=/dev/null",
"redhat@localhost",
"systemctl --wait is-system-running",
)
}
var cmd *exec.Cmd
if ns != nil {
cmd = ns.NamespacedCommandContext(ctx, cmdName, cmdArgs...)
} else {
cmd = exec.CommandContext(ctx, cmdName, cmdArgs...)
}
output, err := cmd.Output()
if ctx.Err() == context.DeadlineExceeded {
@ -171,7 +180,7 @@ func trySSHOnce(ns netNS) error {
// testSSH tests the running image using ssh.
// It tries 20 attempts before giving up. If a major error occurs, it might
// return earlier.
func testSSH(t *testing.T, ns netNS) {
func testSSH(t *testing.T, ns *netNS) {
const attempts = 20
for i := 0; i < attempts; i++ {
err := trySSHOnce(ns)
@ -203,18 +212,18 @@ func testBoot(t *testing.T, imagePath string, bootType string, outputID string)
fallthrough
case "qemu-extract":
return withBootedQemuImage(imagePath, ns, func() error {
testSSH(t, ns)
testSSH(t, &ns)
return nil
})
case "nspawn":
return withBootedNspawnImage(imagePath, outputID, ns, func() error {
testSSH(t, ns)
testSSH(t, &ns)
return nil
})
case "nspawn-extract":
return withExtractedTarArchive(imagePath, func(dir string) error {
return withBootedNspawnDirectory(dir, outputID, ns, func() error {
testSSH(t, ns)
testSSH(t, &ns)
return nil
})
})