Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/firecracker
Path: blob/main/tests/integration_tests/functional/test_snapshot_version.py
1958 views
1
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
# SPDX-License-Identifier: Apache-2.0
3
"""Basic tests scenarios for snapshot save/restore."""
4
5
import platform
6
import pytest
7
from framework.builder import SnapshotBuilder, MicrovmBuilder
8
9
import host_tools.network as net_tools # pylint: disable=import-error
10
11
# Firecracker v0.23 used 16 IRQ lines. For virtio devices,
12
# IRQs are available from 5 to 23, so the maximum number
13
# of devices allowed at the same time was 11.
14
FC_V0_23_MAX_DEVICES_ATTACHED = 11
15
16
17
def _create_and_start_microvm_with_net_devices(test_microvm,
18
network_config=None,
19
devices_no=0):
20
test_microvm.spawn()
21
# Set up a basic microVM: configure the boot source and
22
# add a root device.
23
test_microvm.basic_config(track_dirty_pages=True)
24
25
# Add network devices on top of the already configured rootfs for a
26
# total of (`devices_no` + 1) devices.
27
for i in range(devices_no):
28
# Create tap before configuring interface.
29
_tap, _host_ip, _guest_ip = test_microvm.ssh_network_config(
30
network_config,
31
str(i)
32
)
33
test_microvm.start()
34
35
if network_config is not None:
36
ssh_connection = net_tools.SSHConnection(test_microvm.ssh_config)
37
# Verify if guest can run commands.
38
exit_code, _, _ = ssh_connection.execute_command("sync")
39
assert exit_code == 0
40
41
42
def test_create_v0_23_snapshot(test_microvm_with_ssh):
43
"""Exercise creating a snapshot targeting v0.23 on all platforms."""
44
test_microvm = test_microvm_with_ssh
45
46
_create_and_start_microvm_with_net_devices(test_microvm)
47
48
snapshot_builder = SnapshotBuilder(test_microvm)
49
# Create directory and files for saving snapshot state and memory.
50
_snapshot_dir = snapshot_builder.create_snapshot_dir()
51
52
# Pause microVM for snapshot.
53
response = test_microvm.vm.patch(state='Paused')
54
assert test_microvm.api_session.is_status_no_content(response.status_code)
55
56
response = test_microvm.snapshot.create(
57
mem_file_path="/snapshot/vm.mem",
58
snapshot_path="/snapshot/vm.vmstate",
59
diff=True,
60
version="0.23.0"
61
)
62
if platform.machine() == "x86_64":
63
assert test_microvm.api_session.is_status_no_content(
64
response.status_code)
65
elif platform.machine() == "aarch64":
66
assert test_microvm.api_session.is_status_bad_request(
67
response.status_code)
68
assert "Cannot translate microVM version to snapshot data version"\
69
in response.text
70
71
72
@pytest.mark.skipif(
73
platform.machine() != "x86_64",
74
reason="Exercises specific x86_64 functionality."
75
)
76
def test_create_with_prev_device_count(test_microvm_with_ssh, network_config):
77
"""Create snapshot with expected device count for previous versions."""
78
test_microvm = test_microvm_with_ssh
79
80
# Create and start a microVM with (`FC_V0_23_MAX_DEVICES_ATTACHED` - 1)
81
# network devices.
82
devices_no = FC_V0_23_MAX_DEVICES_ATTACHED - 1
83
_create_and_start_microvm_with_net_devices(test_microvm,
84
network_config,
85
devices_no)
86
87
snapshot_builder = SnapshotBuilder(test_microvm)
88
# Create directory and files for saving snapshot state and memory.
89
_snapshot_dir = snapshot_builder.create_snapshot_dir()
90
91
# Pause and create a snapshot of the microVM. Firecracker v0.23 allowed a
92
# maximum of `FC_V0_23_MAX_DEVICES_ATTACHED` virtio devices at a time.
93
# This microVM has `FC_V0_23_MAX_DEVICES_ATTACHED` devices, including the
94
# rootfs, so snapshotting should succeed.
95
test_microvm.pause_to_snapshot(
96
mem_file_path="/snapshot/vm.mem",
97
snapshot_path="/snapshot/vm.vmstate",
98
diff=True,
99
version="0.23.0")
100
101
102
@pytest.mark.skipif(
103
platform.machine() != "x86_64",
104
reason="Exercises specific x86_64 functionality."
105
)
106
def test_create_with_too_many_devices(test_microvm_with_ssh, network_config):
107
"""Create snapshot with unexpected device count for previous versions."""
108
test_microvm = test_microvm_with_ssh
109
110
# Create and start a microVM with `FC_V0_23_MAX_DEVICES_ATTACHED`
111
# network devices.
112
devices_no = FC_V0_23_MAX_DEVICES_ATTACHED
113
_create_and_start_microvm_with_net_devices(test_microvm,
114
network_config,
115
devices_no)
116
117
snapshot_builder = SnapshotBuilder(test_microvm)
118
# Create directory and files for saving snapshot state and memory.
119
_snapshot_dir = snapshot_builder.create_snapshot_dir()
120
121
# Pause microVM for snapshot.
122
response = test_microvm.vm.patch(state='Paused')
123
assert test_microvm.api_session.is_status_no_content(response.status_code)
124
125
# Attempt to create a snapshot with version: `0.23.0`. Firecracker
126
# v0.23 allowed a maximum of `FC_V0_23_MAX_DEVICES_ATTACHED` virtio
127
# devices at a time. This microVM has `FC_V0_23_MAX_DEVICES_ATTACHED`
128
# network devices on top of the rootfs, so the limit is exceeded.
129
response = test_microvm.snapshot.create(
130
mem_file_path="/snapshot/vm.mem",
131
snapshot_path="/snapshot/vm.vmstate",
132
diff=True,
133
version="0.23.0"
134
)
135
assert test_microvm.api_session.is_status_bad_request(response.status_code)
136
assert "Too many devices attached" in response.text
137
138
139
def test_create_invalid_version(bin_cloner_path):
140
"""Test scenario: create snapshot targeting invalid version."""
141
# Use a predefined vm instance.
142
builder = MicrovmBuilder(bin_cloner_path)
143
test_microvm = builder.build_vm_nano().vm
144
test_microvm.start()
145
146
try:
147
# Target an invalid Firecracker version string.
148
test_microvm.pause_to_snapshot(
149
mem_file_path="/vm.mem",
150
snapshot_path="/vm.vmstate",
151
diff=False,
152
version="invalid")
153
except AssertionError as error:
154
# Check if proper error is returned.
155
assert "Invalid microVM version format" in \
156
str(error)
157
else:
158
assert False, "Negative test failed"
159
160
try:
161
# Target a valid version string but with no snapshot support.
162
test_microvm.pause_to_snapshot(
163
mem_file_path="/vm.mem",
164
snapshot_path="/vm.vmstate",
165
diff=False,
166
version="0.22.0")
167
except AssertionError as error:
168
# Check if proper error is returned.
169
assert "Cannot translate microVM version to snapshot data version" in \
170
str(error)
171
else:
172
assert False, "Negative test failed"
173
174