Skip to content

Commit 7991d9f

Browse files
committed
Tests for snapshot network renames
1 parent 28c80dd commit 7991d9f

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

tests/framework/microvm.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from enum import Enum
2727
from functools import lru_cache
2828
from pathlib import Path
29-
from typing import Optional
29+
from typing import Optional, Dict
3030

3131
from tenacity import retry, stop_after_attempt, wait_fixed
3232

@@ -919,6 +919,7 @@ def restore_from_snapshot(
919919
snapshot: Snapshot,
920920
resume: bool = False,
921921
uffd_path: Path = None,
922+
rename_interfaces: Dict[str, str] = None,
922923
):
923924
"""Restore a snapshot"""
924925
jailed_snapshot = snapshot.copy_to_chroot(Path(self.chroot()))
@@ -941,11 +942,16 @@ def restore_from_snapshot(
941942
if uffd_path is not None:
942943
mem_backend = {"backend_type": "Uffd", "backend_path": str(uffd_path)}
943944

945+
iface_overrides = []
946+
if rename_interfaces:
947+
iface_overrides = [{"iface_id": k, "host_dev_name": v} for k,v in rename_interfaces.items()]
948+
944949
self.api.snapshot_load.put(
945950
mem_backend=mem_backend,
946951
snapshot_path=str(jailed_vmstate),
947952
enable_diff_snapshots=snapshot.is_diff,
948953
resume_vm=resume,
954+
network_overrides=iface_overrides,
949955
)
950956
return jailed_snapshot
951957

@@ -986,13 +992,13 @@ def thread_backtraces(self):
986992
)
987993
return "\n".join(backtraces)
988994

989-
def wait_for_up(self, timeout=10):
995+
def wait_for_up(self, timeout=10, iface=0):
990996
"""Wait for guest running inside the microVM to come up and respond.
991997
992998
:param timeout: seconds to wait.
993999
"""
9941000
try:
995-
rc, stdout, stderr = self.ssh.run("true", timeout)
1001+
rc, stdout, stderr = self.ssh_iface(iface).run("true", timeout)
9961002
except subprocess.TimeoutExpired:
9971003
print(
9981004
f"Remote command did not respond within {timeout}s\n\n"

tests/integration_tests/functional/test_snapshot_basic.py

+29
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
import re
99
import shutil
1010
import time
11+
import dataclasses
1112
from pathlib import Path
1213

1314
import pytest
1415

1516
import host_tools.drive as drive_tools
17+
import host_tools.network as net_tools
1618
from framework.microvm import SnapshotType
1719
from framework.utils import check_filesystem, check_output
1820
from framework.utils_vsock import (
@@ -577,3 +579,30 @@ def test_vmgenid(guest_kernel_linux_6_1, rootfs, microvm_factory, snapshot_type)
577579

578580
# Update the base for next iteration
579581
base_snapshot = snapshot
582+
583+
def test_snapshot_rename_interface(uvm_nano, microvm_factory):
584+
"""
585+
Test that we can restore a snapshot and point its interface to a
586+
different host interface.
587+
"""
588+
base_iface = net_tools.NetIfaceConfig.with_id(0)
589+
590+
vm = uvm_nano
591+
iface1 = dataclasses.replace(base_iface, tap_name="tap1")
592+
vm.add_net_iface(iface=iface1)
593+
# Create an interface but don't attach it to the device
594+
vm.start()
595+
vm.wait_for_up()
596+
597+
snapshot = vm.snapshot_full()
598+
599+
restored_vm = microvm_factory.build()
600+
restored_vm.spawn()
601+
iface2 = dataclasses.replace(base_iface, tap_name="tap2")
602+
snapshot.net_ifaces.clear()
603+
snapshot.net_ifaces.append(iface2)
604+
restored_vm.restore_from_snapshot(snapshot, rename_interfaces=
605+
{base_iface.dev_name: "tap2"})
606+
restored_vm.resume()
607+
restored_vm.wait_for_up()
608+

0 commit comments

Comments
 (0)