Path: blob/main/tests/integration_tests/functional/test_snapshot_version.py
1958 views
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.1# SPDX-License-Identifier: Apache-2.02"""Basic tests scenarios for snapshot save/restore."""34import platform5import pytest6from framework.builder import SnapshotBuilder, MicrovmBuilder78import host_tools.network as net_tools # pylint: disable=import-error910# Firecracker v0.23 used 16 IRQ lines. For virtio devices,11# IRQs are available from 5 to 23, so the maximum number12# of devices allowed at the same time was 11.13FC_V0_23_MAX_DEVICES_ATTACHED = 11141516def _create_and_start_microvm_with_net_devices(test_microvm,17network_config=None,18devices_no=0):19test_microvm.spawn()20# Set up a basic microVM: configure the boot source and21# add a root device.22test_microvm.basic_config(track_dirty_pages=True)2324# Add network devices on top of the already configured rootfs for a25# total of (`devices_no` + 1) devices.26for i in range(devices_no):27# Create tap before configuring interface.28_tap, _host_ip, _guest_ip = test_microvm.ssh_network_config(29network_config,30str(i)31)32test_microvm.start()3334if network_config is not None:35ssh_connection = net_tools.SSHConnection(test_microvm.ssh_config)36# Verify if guest can run commands.37exit_code, _, _ = ssh_connection.execute_command("sync")38assert exit_code == 0394041def test_create_v0_23_snapshot(test_microvm_with_ssh):42"""Exercise creating a snapshot targeting v0.23 on all platforms."""43test_microvm = test_microvm_with_ssh4445_create_and_start_microvm_with_net_devices(test_microvm)4647snapshot_builder = SnapshotBuilder(test_microvm)48# Create directory and files for saving snapshot state and memory.49_snapshot_dir = snapshot_builder.create_snapshot_dir()5051# Pause microVM for snapshot.52response = test_microvm.vm.patch(state='Paused')53assert test_microvm.api_session.is_status_no_content(response.status_code)5455response = test_microvm.snapshot.create(56mem_file_path="/snapshot/vm.mem",57snapshot_path="/snapshot/vm.vmstate",58diff=True,59version="0.23.0"60)61if platform.machine() == "x86_64":62assert test_microvm.api_session.is_status_no_content(63response.status_code)64elif platform.machine() == "aarch64":65assert test_microvm.api_session.is_status_bad_request(66response.status_code)67assert "Cannot translate microVM version to snapshot data version"\68in response.text697071@pytest.mark.skipif(72platform.machine() != "x86_64",73reason="Exercises specific x86_64 functionality."74)75def test_create_with_prev_device_count(test_microvm_with_ssh, network_config):76"""Create snapshot with expected device count for previous versions."""77test_microvm = test_microvm_with_ssh7879# Create and start a microVM with (`FC_V0_23_MAX_DEVICES_ATTACHED` - 1)80# network devices.81devices_no = FC_V0_23_MAX_DEVICES_ATTACHED - 182_create_and_start_microvm_with_net_devices(test_microvm,83network_config,84devices_no)8586snapshot_builder = SnapshotBuilder(test_microvm)87# Create directory and files for saving snapshot state and memory.88_snapshot_dir = snapshot_builder.create_snapshot_dir()8990# Pause and create a snapshot of the microVM. Firecracker v0.23 allowed a91# maximum of `FC_V0_23_MAX_DEVICES_ATTACHED` virtio devices at a time.92# This microVM has `FC_V0_23_MAX_DEVICES_ATTACHED` devices, including the93# rootfs, so snapshotting should succeed.94test_microvm.pause_to_snapshot(95mem_file_path="/snapshot/vm.mem",96snapshot_path="/snapshot/vm.vmstate",97diff=True,98version="0.23.0")99100101@pytest.mark.skipif(102platform.machine() != "x86_64",103reason="Exercises specific x86_64 functionality."104)105def test_create_with_too_many_devices(test_microvm_with_ssh, network_config):106"""Create snapshot with unexpected device count for previous versions."""107test_microvm = test_microvm_with_ssh108109# Create and start a microVM with `FC_V0_23_MAX_DEVICES_ATTACHED`110# network devices.111devices_no = FC_V0_23_MAX_DEVICES_ATTACHED112_create_and_start_microvm_with_net_devices(test_microvm,113network_config,114devices_no)115116snapshot_builder = SnapshotBuilder(test_microvm)117# Create directory and files for saving snapshot state and memory.118_snapshot_dir = snapshot_builder.create_snapshot_dir()119120# Pause microVM for snapshot.121response = test_microvm.vm.patch(state='Paused')122assert test_microvm.api_session.is_status_no_content(response.status_code)123124# Attempt to create a snapshot with version: `0.23.0`. Firecracker125# v0.23 allowed a maximum of `FC_V0_23_MAX_DEVICES_ATTACHED` virtio126# devices at a time. This microVM has `FC_V0_23_MAX_DEVICES_ATTACHED`127# network devices on top of the rootfs, so the limit is exceeded.128response = test_microvm.snapshot.create(129mem_file_path="/snapshot/vm.mem",130snapshot_path="/snapshot/vm.vmstate",131diff=True,132version="0.23.0"133)134assert test_microvm.api_session.is_status_bad_request(response.status_code)135assert "Too many devices attached" in response.text136137138def test_create_invalid_version(bin_cloner_path):139"""Test scenario: create snapshot targeting invalid version."""140# Use a predefined vm instance.141builder = MicrovmBuilder(bin_cloner_path)142test_microvm = builder.build_vm_nano().vm143test_microvm.start()144145try:146# Target an invalid Firecracker version string.147test_microvm.pause_to_snapshot(148mem_file_path="/vm.mem",149snapshot_path="/vm.vmstate",150diff=False,151version="invalid")152except AssertionError as error:153# Check if proper error is returned.154assert "Invalid microVM version format" in \155str(error)156else:157assert False, "Negative test failed"158159try:160# Target a valid version string but with no snapshot support.161test_microvm.pause_to_snapshot(162mem_file_path="/vm.mem",163snapshot_path="/vm.vmstate",164diff=False,165version="0.22.0")166except AssertionError as error:167# Check if proper error is returned.168assert "Cannot translate microVM version to snapshot data version" in \169str(error)170else:171assert False, "Negative test failed"172173174