forked from firecracker-microvm/firecracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_tests.rs
459 lines (389 loc) · 15.6 KB
/
integration_tests.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use std::io::{Seek, SeekFrom};
use std::time::Duration;
use std::{io, thread};
use snapshot::Snapshot;
use utils::tempfile::TempFile;
use vmm::builder::{build_microvm_for_boot, build_microvm_from_snapshot, setup_serial_device};
use vmm::persist::{self, snapshot_state_sanity_check, MicrovmState};
use vmm::resources::VmResources;
use vmm::seccomp_filters::{get_filters, SeccompConfig};
use vmm::utilities::mock_devices::MockSerialInput;
use vmm::utilities::mock_resources::{MockVmResources, NOISY_KERNEL_IMAGE};
#[cfg(target_arch = "x86_64")]
use vmm::utilities::test_utils::dirty_tracking_vmm;
use vmm::utilities::test_utils::{create_vmm, default_vmm};
use vmm::version_map::VERSION_MAP;
use vmm::vmm_config::instance_info::InstanceInfo;
use vmm::vmm_config::snapshot::{CreateSnapshotParams, SnapshotType};
use vmm::{EventManager, FcExitCode};
#[test]
fn test_setup_serial_device() {
let read_tempfile = TempFile::new().unwrap();
let read_handle = MockSerialInput(read_tempfile.into_file());
let mut event_manager = EventManager::new().unwrap();
assert!(setup_serial_device(
&mut event_manager,
Box::new(read_handle),
Box::new(io::stdout()),
)
.is_ok());
}
#[test]
fn test_build_microvm() {
// Error case: no boot source configured.
{
let resources: VmResources = MockVmResources::new().into();
let mut event_manager = EventManager::new().unwrap();
let mut empty_seccomp_filters = get_filters(SeccompConfig::None).unwrap();
let vmm_ret = build_microvm_for_boot(
&InstanceInfo::default(),
&resources,
&mut event_manager,
&mut empty_seccomp_filters,
);
assert_eq!(format!("{:?}", vmm_ret.err()), "Some(MissingKernelConfig)");
}
// Success case.
let (vmm, mut _evmgr) = default_vmm(None);
// On x86_64, the vmm should exit once its workload completes and signals the exit event.
// On aarch64, the test kernel doesn't exit, so the vmm is force-stopped.
#[cfg(target_arch = "x86_64")]
_evmgr.run_with_timeout(500).unwrap();
#[cfg(target_arch = "aarch64")]
vmm.lock().unwrap().stop(FcExitCode::Ok);
assert_eq!(
vmm.lock().unwrap().shutdown_exit_code(),
Some(FcExitCode::Ok)
);
}
#[test]
fn test_pause_resume_microvm() {
// Tests that pausing and resuming a microVM work as expected.
let (vmm, _) = default_vmm(None);
// There's a race between this thread and the vcpu thread, but this thread
// should be able to pause vcpu thread before it finishes running its test-binary.
assert!(vmm.lock().unwrap().pause_vm().is_ok());
// Pausing again the microVM should not fail (microVM remains in the
// `Paused` state).
assert!(vmm.lock().unwrap().pause_vm().is_ok());
assert!(vmm.lock().unwrap().resume_vm().is_ok());
vmm.lock().unwrap().stop(FcExitCode::Ok);
}
#[test]
fn test_dirty_bitmap_error() {
// Error case: dirty tracking disabled.
let (vmm, _) = default_vmm(None);
// The vmm will start with dirty page tracking = OFF.
// With dirty tracking disabled, the underlying KVM_GET_DIRTY_LOG ioctl will fail
// with errno 2 (ENOENT) because KVM can't find any guest memory regions with dirty
// page tracking enabled.
assert_eq!(
format!("{:?}", vmm.lock().unwrap().get_dirty_bitmap().err()),
"Some(DirtyBitmap(Error(2)))"
);
vmm.lock().unwrap().stop(FcExitCode::Ok);
}
#[test]
#[cfg(target_arch = "x86_64")]
fn test_dirty_bitmap_success() {
// The vmm will start with dirty page tracking = ON.
let (vmm, _) = dirty_tracking_vmm(Some(NOISY_KERNEL_IMAGE));
// Let it churn for a while and dirty some pages...
thread::sleep(Duration::from_millis(100));
let bitmap = vmm.lock().unwrap().get_dirty_bitmap().unwrap();
let num_dirty_pages: u32 = bitmap
.iter()
.map(|(_, bitmap_per_region)| {
// Gently coerce to u32
let num_dirty_pages_per_region: u32 =
bitmap_per_region.iter().map(|n| n.count_ones()).sum();
num_dirty_pages_per_region
})
.sum();
assert!(num_dirty_pages > 0);
vmm.lock().unwrap().stop(FcExitCode::Ok);
}
#[test]
fn test_disallow_snapshots_without_pausing() {
let (vmm, _) = default_vmm(Some(NOISY_KERNEL_IMAGE));
// Verify saving state while running is not allowed.
// Can't do unwrap_err() because MicrovmState doesn't impl Debug.
match vmm.lock().unwrap().save_state() {
Err(err) => assert!(format!("{:?}", err).contains("NotAllowed")),
Ok(_) => panic!("Should not be allowed."),
};
// Pause microVM.
vmm.lock().unwrap().pause_vm().unwrap();
// It is now allowed.
vmm.lock().unwrap().save_state().unwrap();
// Stop.
vmm.lock().unwrap().stop(FcExitCode::Ok);
}
fn verify_create_snapshot(is_diff: bool) -> (TempFile, TempFile) {
let snapshot_file = TempFile::new().unwrap();
let memory_file = TempFile::new().unwrap();
let (vmm, _) = create_vmm(Some(NOISY_KERNEL_IMAGE), is_diff);
// Be sure that the microVM is running.
thread::sleep(Duration::from_millis(200));
// Pause microVM.
vmm.lock().unwrap().pause_vm().unwrap();
// Create snapshot.
let snapshot_type = match is_diff {
true => SnapshotType::Diff,
false => SnapshotType::Full,
};
let snapshot_params = CreateSnapshotParams {
snapshot_type,
snapshot_path: snapshot_file.as_path().to_path_buf(),
mem_file_path: memory_file.as_path().to_path_buf(),
version: Some(String::from("0.24.0")),
};
{
let mut locked_vmm = vmm.lock().unwrap();
persist::create_snapshot(&mut locked_vmm, &snapshot_params, VERSION_MAP.clone()).unwrap();
}
vmm.lock().unwrap().stop(FcExitCode::Ok);
// Check that we can deserialize the microVM state from `snapshot_file`.
let snapshot_path = snapshot_file.as_path().to_path_buf();
let snapshot_file_metadata = std::fs::metadata(snapshot_path).unwrap();
let snapshot_len = snapshot_file_metadata.len() as usize;
let restored_microvm_state: MicrovmState = Snapshot::load(
&mut snapshot_file.as_file(),
snapshot_len,
VERSION_MAP.clone(),
)
.unwrap();
// Check memory file size.
let memory_file_size_mib = memory_file.as_file().metadata().unwrap().len() >> 20;
assert_eq!(
restored_microvm_state.vm_info.mem_size_mib,
memory_file_size_mib
);
// Verify deserialized data.
// The default vmm has no devices and one vCPU.
assert_eq!(restored_microvm_state.device_states.block_devices.len(), 0);
assert_eq!(restored_microvm_state.device_states.net_devices.len(), 0);
assert!(restored_microvm_state.device_states.vsock_device.is_none());
assert_eq!(restored_microvm_state.vcpu_states.len(), 1);
(snapshot_file, memory_file)
}
fn verify_load_snapshot(snapshot_file: TempFile, memory_file: TempFile) {
use vm_memory::GuestMemoryMmap;
use vmm::memory_snapshot::SnapshotMemory;
let mut event_manager = EventManager::new().unwrap();
let mut empty_seccomp_filters = get_filters(SeccompConfig::None).unwrap();
// Deserialize microVM state.
let snapshot_file_metadata = snapshot_file.as_file().metadata().unwrap();
let snapshot_len = snapshot_file_metadata.len() as usize;
snapshot_file.as_file().seek(SeekFrom::Start(0)).unwrap();
let microvm_state: MicrovmState = Snapshot::load(
&mut snapshot_file.as_file(),
snapshot_len,
VERSION_MAP.clone(),
)
.unwrap();
let mem = GuestMemoryMmap::restore(
Some(memory_file.as_file()),
µvm_state.memory_state,
false,
)
.unwrap();
let vm_resources = &mut VmResources::default();
// Build microVM from state.
let vmm = build_microvm_from_snapshot(
&InstanceInfo::default(),
&mut event_manager,
microvm_state,
mem,
None,
false,
&mut empty_seccomp_filters,
vm_resources,
)
.unwrap();
// For now we're happy we got this far, we don't test what the guest is actually doing.
vmm.lock().unwrap().stop(FcExitCode::Ok);
}
#[test]
fn test_create_and_load_snapshot() {
// Create diff snapshot.
let (snapshot_file, memory_file) = verify_create_snapshot(true);
// Create a new microVm from snapshot. This only tests code-level logic; it verifies
// that a microVM can be built with no errors from given snapshot.
// It does _not_ verify that the guest is actually restored properly. We're using
// python integration tests for that.
verify_load_snapshot(snapshot_file, memory_file);
// Create full snapshot.
let (snapshot_file, memory_file) = verify_create_snapshot(false);
// Create a new microVm from snapshot. This only tests code-level logic; it verifies
// that a microVM can be built with no errors from given snapshot.
// It does _not_ verify that the guest is actually restored properly. We're using
// python integration tests for that.
verify_load_snapshot(snapshot_file, memory_file);
}
#[test]
fn test_snapshot_load_sanity_checks() {
use vmm::persist::SnapShotStateSanityCheckError;
use vmm::vmm_config::machine_config::MAX_SUPPORTED_VCPUS;
let mut microvm_state = get_microvm_state_from_snapshot();
assert!(snapshot_state_sanity_check(µvm_state).is_ok());
// Remove memory regions.
microvm_state.memory_state.regions.clear();
// Validate sanity checks fail because there is no mem region in state.
assert_eq!(
snapshot_state_sanity_check(µvm_state),
Err(SnapShotStateSanityCheckError::NoMemory)
);
// Create MAX_SUPPORTED_VCPUS vCPUs starting from 1 vCPU.
for _ in 0..(MAX_SUPPORTED_VCPUS as f64).log2() as usize {
microvm_state
.vcpu_states
.append(&mut microvm_state.vcpu_states.clone());
}
// After this line we will have 33 vCPUs, FC max si 32.
microvm_state
.vcpu_states
.push(microvm_state.vcpu_states[0].clone());
// Validate sanity checks fail because there are too many vCPUs.
assert_eq!(
snapshot_state_sanity_check(µvm_state),
Err(SnapShotStateSanityCheckError::InvalidVcpuCount)
);
// Remove all vCPUs states from microvm state.
microvm_state.vcpu_states.clear();
// Validate sanity checks fail because there is no vCPU in state.
assert_eq!(
snapshot_state_sanity_check(µvm_state),
Err(SnapShotStateSanityCheckError::InvalidVcpuCount)
);
}
fn get_microvm_state_from_snapshot() -> MicrovmState {
// Create a diff snapshot
let (snapshot_file, _) = verify_create_snapshot(true);
// Deserialize the microVM state.
let snapshot_file_metadata = snapshot_file.as_file().metadata().unwrap();
let snapshot_len = snapshot_file_metadata.len() as usize;
snapshot_file.as_file().seek(SeekFrom::Start(0)).unwrap();
Snapshot::load(
&mut snapshot_file.as_file(),
snapshot_len,
VERSION_MAP.clone(),
)
.unwrap()
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_snapshot_cpu_vendor() {
use vmm::persist::validate_cpu_vendor;
let microvm_state = get_microvm_state_from_snapshot();
// Check if the snapshot created above passes validation since
// the snapshot was created locally.
assert!(validate_cpu_vendor(µvm_state).is_ok());
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_snapshot_cpu_vendor_mismatch() {
use vmm::persist::validate_cpu_vendor;
let mut microvm_state = get_microvm_state_from_snapshot();
// Check if the snapshot created above passes validation since
// the snapshot was created locally.
assert_eq!(validate_cpu_vendor(µvm_state), Ok(true));
// Modify the vendor id in CPUID.
for entry in microvm_state.vcpu_states[0].cpuid.as_mut_slice().iter_mut() {
if entry.function == 0 && entry.index == 0 {
// Fail if vendor id is NULL as this needs furhter investigation.
assert_ne!(entry.ebx, 0);
assert_ne!(entry.ecx, 0);
assert_ne!(entry.edx, 0);
entry.ebx = 0;
break;
}
}
// It succeeds in checking if the CPU vendor is valid, in this process it discovers the CPU
// vendor not valid.
assert_eq!(validate_cpu_vendor(µvm_state), Ok(false));
// Negative test: remove the vendor id from cpuid.
for entry in microvm_state.vcpu_states[0].cpuid.as_mut_slice().iter_mut() {
if entry.function == 0 && entry.index == 0 {
entry.function = 1234;
}
}
// It succeeds in checking if the CPU vendor is valid, in this process it discovers the CPU
// vendor not valid.
assert_eq!(
validate_cpu_vendor(µvm_state),
Err(vmm::persist::ValidateCpuVendorError::Snapshot(
cpuid::common::Error::NotSupported
))
);
}
#[cfg(target_arch = "x86_64")]
#[test]
fn test_snapshot_cpu_vendor_missing() {
use vmm::persist::validate_cpu_vendor;
let mut microvm_state = get_microvm_state_from_snapshot();
// Check if the snapshot created above passes validation since
// the snapshot was created locally.
assert!(validate_cpu_vendor(µvm_state).is_ok());
// Negative test: remove the vendor id from cpuid.
for entry in microvm_state.vcpu_states[0].cpuid.as_mut_slice().iter_mut() {
if entry.function == 0 && entry.index == 0 {
entry.function = 1234;
}
}
// This must fail as the cpu vendor entry does not exist.
assert!(validate_cpu_vendor(µvm_state).is_err());
}
#[cfg(target_arch = "aarch64")]
#[test]
fn test_snapshot_cpu_vendor() {
use vmm::persist::validate_cpu_manufacturer_id;
let microvm_state = get_microvm_state_from_snapshot();
// Check if the snapshot created above passes validation since
// the snapshot was created locally.
assert!(validate_cpu_manufacturer_id(µvm_state).is_ok());
}
#[cfg(target_arch = "aarch64")]
#[test]
fn test_snapshot_cpu_vendor_missing() {
use arch::regs::MIDR_EL1;
use vmm::persist::{validate_cpu_manufacturer_id, ValidateCpuManufacturerIdError};
let mut microvm_state = get_microvm_state_from_snapshot();
// Check if the snapshot created above passes validation since
// the snapshot was created locally.
assert_eq!(validate_cpu_manufacturer_id(µvm_state), Ok(true));
// Remove the MIDR_EL1 value from the VCPU states, by setting it to 0
for state in microvm_state.vcpu_states.as_mut_slice().iter_mut() {
for reg in state.regs.as_mut_slice().iter_mut() {
if reg.id == MIDR_EL1 {
reg.id = 0;
}
}
}
assert!(matches!(
validate_cpu_manufacturer_id(µvm_state),
Err(ValidateCpuManufacturerIdError::Snapshot(_))
));
}
#[cfg(target_arch = "aarch64")]
#[test]
fn test_snapshot_cpu_vendor_mismatch() {
use arch::regs::MIDR_EL1;
use vmm::persist::validate_cpu_manufacturer_id;
let mut microvm_state = get_microvm_state_from_snapshot();
// Check if the snapshot created above passes validation since
// the snapshot was created locally.
assert_eq!(validate_cpu_manufacturer_id(µvm_state), Ok(true));
// Change the MIDR_EL1 value from the VCPU states, to contain an
// invalid manufacturer ID
for state in microvm_state.vcpu_states.as_mut_slice().iter_mut() {
for reg in state.regs.as_mut_slice().iter_mut() {
if reg.id == MIDR_EL1 {
reg.addr = 0x710FD081;
}
}
}
assert_eq!(validate_cpu_manufacturer_id(µvm_state), Ok(false));
}