Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/firecracker
Path: blob/main/tests/integration_tests/functional/test_shut_down.py
1958 views
1
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
# SPDX-License-Identifier: Apache-2.0
3
"""Tests scenarios for shutting down Firecracker/VM."""
4
import os
5
import time
6
import json
7
import platform
8
9
import framework.utils as utils
10
11
import host_tools.logging as log_tools
12
import host_tools.network as net_tools # pylint: disable=import-error
13
14
15
def test_reboot(test_microvm_with_ssh, network_config):
16
"""Test reboot from guest kernel."""
17
test_microvm = test_microvm_with_ssh
18
test_microvm.jailer.daemonize = False
19
test_microvm.spawn()
20
21
# We don't need to monitor the memory for this test because we are
22
# just rebooting and the process dies before pmap gets the RSS.
23
test_microvm.memory_monitor = None
24
25
# Set up the microVM with 4 vCPUs, 256 MiB of RAM, 0 network ifaces, and
26
# a root file system with the rw permission. The network interfaces is
27
# added after we get a unique MAC and IP.
28
test_microvm.basic_config(vcpu_count=4)
29
_tap, _, _ = test_microvm.ssh_network_config(network_config, '1')
30
31
# Configure metrics system.
32
metrics_fifo_path = os.path.join(test_microvm.path, 'metrics_fifo')
33
metrics_fifo = log_tools.Fifo(metrics_fifo_path)
34
response = test_microvm.metrics.put(
35
metrics_path=test_microvm.create_jailed_resource(metrics_fifo.path)
36
)
37
assert test_microvm.api_session.is_status_no_content(response.status_code)
38
39
test_microvm.start()
40
41
# Get Firecracker PID so we can count the number of threads.
42
firecracker_pid = test_microvm.jailer_clone_pid
43
44
# Get number of threads in Firecracker
45
cmd = 'ps -o nlwp {} | tail -1 | awk \'{{print $1}}\''.format(
46
firecracker_pid
47
)
48
_, stdout, _ = utils.run_cmd(cmd)
49
nr_of_threads = stdout.rstrip()
50
assert int(nr_of_threads) == 6
51
52
# Consume existing metrics
53
lines = metrics_fifo.sequential_reader(100)
54
assert len(lines) == 1
55
# Rebooting Firecracker sends an exit event and should gracefully kill.
56
# the instance.
57
ssh_connection = net_tools.SSHConnection(test_microvm.ssh_config)
58
59
ssh_connection.execute_command("reboot")
60
61
while True:
62
# Pytest's timeout will kill the test even if the loop doesn't exit.
63
try:
64
os.kill(firecracker_pid, 0)
65
time.sleep(0.01)
66
except OSError:
67
break
68
69
# Consume existing metrics
70
lines = metrics_fifo.sequential_reader(100)
71
assert len(lines) == 1
72
73
if platform.machine() != "x86_64":
74
log_data = test_microvm.log_data
75
assert "Received KVM_SYSTEM_EVENT: type: 2, event: 0" in log_data
76
assert "Vmm is stopping." in log_data
77
78
# Make sure that the FC process was not killed by a seccomp fault
79
assert json.loads(lines[0])["seccomp"]["num_faults"] == 0
80
81